Problem 06B. Next Greater Element ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers. For every index i, find the first index j > i with aj > ai, and report aj. If no such j exists, report  − 1.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print n space-separated integers: the answer for each index, in order.

Constraints

1 ≤ n ≤ 2 ⋅ 105

1 ≤ ai ≤ 109

Sample tests

No. Standard input Standard output
1
6
2 1 2 4 3 1
4 2 4 -1 -1 -1
2
4
4 3 2 1
-1 -1 -1 -1
3
1
5
-1

0.050s 0.020s 16