Skip to content
UPEEC

Next Greater Element

Medium#arrays#stack#monotonic-stack· solved by 0 · time limit 2.0 s

For each element, print the first element to its right that is strictly greater, or -1. A monotonic stack solves it in O(n)O(n).

Input

nn, then nn integers.

Output

nn answers.

Constraints

1≤n≤1051 \le n \le 10^5

Sample 1

Input

4
4 5 2 25

Output

5 25 25 -1

Sample 2

Input

4
13 7 6 12

Output

-1 12 12 -1

Sign in or join free to run and submit your solution.