#STK0000075. Phần tử lớn hơn tiếp theo (Next Greater Element)

Phần tử lớn hơn tiếp theo (Next Greater Element)

Next Greater Element

Source: Baekjoon Online Judge

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array A1,A2,…,ANA_1,A_2,\ldots,A_N. For each position ii, its next greater element is the first element to the right whose value is greater than AiA_i. If it does not exist, the answer at position ii is −1-1.

Input

The first line contains an integer NN. The second line contains NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N.

Output

Print NN values. The ii-th value is the next greater element of AiA_i, or −1-1 if it does not exist.

Subtasks

  • Subtask 1 (30 points): N≤2000N\le2000; all other conditions are unchanged.
  • Subtask 2 (70 points): 1≤N≤1061\le N\le10^6, 1≤Ai≤1061\le A_i\le10^6.

Examples

Input

4
3 5 2 7

Output

5 7 7 -1

Explanation

The array is 3,5,2,73,5,2,7.

  • For A1=3A_1=3, the first greater value to the right is 55, so the first answer is 55.
  • For A2=5A_2=5, the value 22 is not greater than 55, while 7>57>5, so the second answer is 77.
  • For A3=2A_3=2, the immediately following value is 7>27>2, so the third answer is 77.
  • For A4=7A_4=7, there is no element to its right, so the answer is −1-1.

Therefore the output is 5 7 7 -1.