LogiQuest

13. Next Greater Element

MediumStackMonotonic Stack

For each element, print the first element to its right that is strictly greater, or -1 if none exists.

Input format

Line 1: n Line 2: n integers

Output format

n space-separated integers.

Example 1

Input

4
4 5 2 25

Output

5 25 25 -1

Example 2

Input

4
13 7 6 12

Output

-1 12 12 -1

Constraints

1 ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 6 test cases, most of them hidden.

Loading the MyLoginn IDE…