Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/stack-keyingi-katta-element

Next greater element

Medium 1000 ms 128 MB 60%
Share

$n$ positive integers $a_{1}, a_{2}, \dots, a_{n}$ are given. For each $a_{i}$ find the first element to its right that is strictly greater than it. If there is no such element, the answer is $-1$.

Array cells: an arrow from each element to the first greater one on its right, and the row of answers

Input

The first line contains a positive integer $n$. The second line contains $n$ positive integers $a_{i}$ separated by spaces.

Output

One line with $n$ integers separated by spaces — the answer for each element.

Note

$1 \le n \le 2 * 10^{5}$
$1 \le a_{i} \le 10^{9}$

Examples

Input #1
6
4 5 2 10 8 1
Output #1
5 10 10 -1 -1 -1
Input #2
3
3 3 3
Output #2
-1 -1 -1

Sign in to submit a solution

Signing up is free and takes a minute.