PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000095. Thỏ trên hàng cây (Penchick and Desert Rabbit)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thỏ trên hàng cây (Penchick and Desert Rabbit)

Penchick and Desert Rabbit

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn trees in a row, and tree ii has height aia_i. A rabbit at tree ii can jump to tree jj if j<ij<i and aj>aia_j>a_i, or if j>ij>i and aj<aia_j<a_i. It may make any number of jumps or stay where it is. For every starting position ii, determine the maximum height of any tree reachable from it.

Input

The first line contains nn. The second line contains a1,…,ana_1,\ldots,a_n.

Output

Print nn integers, one answer for every starting position.

Subtasks

  • Subtask 1 (20 points): 1≤n≤351\le n\le 35, 1≤ai≤n1\le a_i\le n.
  • Subtask 2 (30 points): 1≤n≤3001\le n\le 300, 1≤ai≤n1\le a_i\le n.
  • Subtask 3 (50 points): 1≤n≤5⋅1051\le n\le 5\cdot10^5, 1≤ai≤n1\le a_i\le n.

Examples

Input

4
2 3 1 4

Output

3 3 3 4

Explanation

The first three trees are mutually reachable; the fourth is not reachable from the first three.