Ring of Racks

medium monotonic-stack stack arrays 1000 ms · 256 MB

Ring of Racks

A data center has n server racks arranged in a ring, numbered 0 to n-1 clockwise, with rack n-1 adjacent to rack 0. Each rack reports a current load a_i.

To shed load, every rack wants to forward its work to the nearest rack clockwise (increasing index, wrapping around after n-1 back to 0) whose load is strictly greater than its own. A rack may need to look all the way around the ring, possibly passing other racks several times over, but it never forwards to itself.

For each rack, find the index of the first rack clockwise with a strictly greater load, or -1 if no rack in the ring has a strictly greater load.

Input

The first line contains n (1 ≤ n ≤ 2·10^5).

The second line contains n integers a_0, a_1, …, a_{n-1} (1 ≤ a_i ≤ 10^9), the loads of the racks in clockwise order.

Output

Print n integers separated by spaces: for each rack i, the index of the first rack strictly clockwise from it with a strictly greater load, or -1 if none exists.

Notes

Many loads can be equal; equal loads never satisfy another rack's request, so ties keep looking further clockwise.

Example 1

Input

5
2 1 2 4 3

Output

3 2 3 -1 3

Example 2

Input

4
5 5 5 5

Output

-1 -1 -1 -1

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

Hint 1

On a straight array, the question "first greater element to the right" is a classic. How could you make the ring behave like a line, given that every rack looks clockwise and wraps around?

Hint 2

Walk the array twice, going from right to left, so each position can see the racks that wrap around. Keep a stack of candidate indices, and pop any candidate whose load is not strictly greater than the current one before reading off the answer.

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

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