#STK0000074. Giá trị nhỏ hơn gần nhất (Nearest Smaller Values)

Giá trị nhỏ hơn gần nhất (Nearest Smaller Values)

Nearest Smaller Values

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array x1,x2,…,xnx_1,x_2,\ldots,x_n. For every position ii, find the largest position jj such that j<ij<i and xj<xix_j<x_i. If no such position exists, the answer for position ii is 00.

Input

The first line contains an integer nn. The second line contains nn integers x1,x2,…,xnx_1,x_2,\ldots,x_n.

Output

Print nn integers. The ii-th integer is the nearest position to the left of ii whose value is smaller than xix_i, or 00 if it does not exist.

Subtasks

  • Subtask 1 (30 points): n≤2000n\le2000; all other conditions are unchanged.
  • Subtask 2 (70 points): 1≤n≤2⋅1051\le n\le 2\cdot10^5, 1≤xi≤1091\le x_i\le10^9.

Examples

Input

8
2 5 1 4 8 3 2 5

Output

0 1 0 3 4 3 3 7

Explanation

Process the positions from left to right:

  • i=1i=1: there is no element to the left, so the answer is 00.
  • i=2i=2: x2=5x_2=5. The nearest smaller value on the left is x1=2x_1=2, so the answer is 11.
  • i=3i=3: x3=1x_3=1. Neither 22 nor 55 is smaller than 11, so the answer is 00.
  • i=4i=4: x4=4x_4=4. The immediately preceding value x3=1x_3=1 is smaller, so the answer is 33.
  • i=5i=5: x5=8x_5=8. Since x4=4<8x_4=4<8, the answer is 44.
  • i=6i=6: x6=3x_6=3. The closer values 88 and 44 are not smaller than 33; x3=1<3x_3=1<3, so the answer is 33.
  • i=7i=7: x7=2x_7=2. The values 3,8,43,8,4 are not smaller than 22; x3=1<2x_3=1<2, so the answer is 33.
  • i=8i=8: x8=5x_8=5. The immediately preceding value x7=2x_7=2 is smaller, so the answer is 77.

Hence the required index sequence is 0 1 0 3 4 3 3 7.