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

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

Next Greater Element II

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Treat the array as circular: after the last element comes the first element again. For every position, find the first greater value encountered when moving right around the circle. The element itself cannot be used as its own answer. If no greater value exists, print −1-1.

Input

The first line contains nn. The second line contains the nn array values.

Output

Print nn integers, the circular next greater value for each position, or −1-1.

Subtasks

  • Subtask 1 (30 points): n≤200n\le200; all other conditions are unchanged.
  • Subtask 2 (70 points): 1≤n≤1041\le n\le10^4, −109≤numsi≤109-10^9\le nums_i\le10^9.

Examples

Input

3
1 2 1

Output

2 -1 2

Explanation

The circular array is [1,2,1][1,2,1].

  • For the 11 at position 11, moving right immediately reaches 2>12>1, so the answer is 22.
  • For the 22 at position 22, moving right visits the 11 at position 33 and then wraps to the 11 at position 11; neither is greater than 22, so the answer is −1-1.
  • For the 11 at position 33, wrapping around first reaches the equal value 11 at position 11, then reaches 2>12>1 at position 22, so the answer is 22.

Therefore the output is 2 -1 2.