Taller to the Left

easy data-structures stack monotonic-stack 1000 ms · 256 MB

Taller to the Left

There are n people standing in a row, numbered from 1 to n from left to right. Person i has height h_i.

For every person i, find the nearest person to their left who is strictly taller than them. That is the largest index j with j < i and h_j > h_i. If there is no such person, the answer for i is 0.

Input

The first line contains an integer n (1 ≤ n ≤ 5·10^5), the number of people.

The second line contains n integers h_1, h_2, …, h_n (1 ≤ h_i ≤ 10^9), the heights.

Output

Print n integers separated by spaces: the i-th of them is the index of the nearest strictly taller person to the left of person i, or 0 if there is none.

Notes

In the first example, person 4 (height 4) is not blocked by person 3, who has the same height, so their answer is person 1 (height 5). Person 6 is the tallest of everyone, so their answer is 0.

Ties never count: a person of equal height is not taller.

Example 1

Input

6
5 3 4 4 2 6

Output

0 1 1 1 4 0

Example 2

Input

8
2 9 4 1 7 3 8 5

Output

0 0 2 3 2 5 2 7

Hints · reveal one at a time if you're stuck

Hint 1

Scanning left from every person is too slow on a row of increasing or equal heights. If a person j is to the left of a person k with h_j ≤ h_k, can j ever be the answer for anyone standing to the right of k?

Hint 2

Keep only the candidates that are still strictly taller than everyone after them, using a stack of indices whose heights strictly decrease. For each person, pop everyone who is not strictly taller than them; the stack's top is then the answer. Equal heights must be popped.

report an issue Prefer your own editor? sooks prep 6 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.