#STK0000076. Phần tử có tần suất lớn hơn tiếp theo (Next Greater Frequency)

Phần tử có tần suất lớn hơn tiếp theo (Next Greater Frequency)

Next Greater Frequency

Source: Baekjoon Online Judge

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array A1,A2,…,ANA_1,A_2,\ldots,A_N. Let F(x)F(x) be the number of occurrences of value xx in the entire array. For every position ii, find the first AjA_j to its right such that F(Aj)>F(Ai)F(A_j)>F(A_i). If it does not exist, the answer 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 element whose global frequency is greater than that of AiA_i, or −1-1 if none exists.

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

7
1 1 2 3 4 2 1

Output

-1 -1 1 2 2 1 -1

Explanation

First count occurrences in the entire array:

  • F(1)=3F(1)=3;
  • F(2)=2F(2)=2;
  • F(3)=1F(3)=1;
  • F(4)=1F(4)=1.

Now inspect each position:

  • The first two values are 11 with frequency 33, the maximum frequency in the array, so both answers are −1-1.
  • For the value 22 at position 33, the following values 3,4,23,4,2 do not have a greater frequency; the final 11 has frequency 3>23>2, so the answer is 11.
  • For the value 33 at position 44, the next value 44 also has frequency 11, while the following 22 has frequency 2>12>1, so the answer is 22.
  • For the value 44 at position 55, the next 22 has frequency 2>12>1, so the answer is 22.
  • For the value 22 at position 66, the final 11 has frequency 3>23>2, so the answer is 11.
  • The final 11 has no element to its right, so its answer is −1-1.

Thus the output is -1 -1 1 2 2 1 -1.