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

#FT0000252. Dãy chướng ngại vật hợp lệ dài nhất tại mỗi vị trí (Find the Longest Valid Obstacle Course at Each Position)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy chướng ngại vật hợp lệ dài nhất tại mỗi vị trí (Find the Longest Valid Obstacle Course at Each Position)

Find the Longest Valid Obstacle Course at Each Position

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn obstacles in left-to-right order, numbered from 11 to nn. Obstacle ii has height hih_i.

For each position ii, choose a subsequence of obstacles from positions 1,2,…,i1,2,\ldots,i satisfying:

  1. Position ii must be selected and must be the last selected obstacle.
  2. Selected positions are strictly increasing (they need not be consecutive).
  3. Heights of selected obstacles are nondecreasing from left to right. Equal heights are allowed.

Let ansians_i be the maximum number of selected obstacles when the subsequence must end at position ii. Output all ans1,ans2,…,ansnans_1,ans_2,\ldots,ans_n.

Input

  • The first line contains an integer nn, the number of obstacles.
  • The second line contains exactly nn integers h1,h2,…,hnh_1,h_2,\ldots,h_n, the obstacle heights in position order.

Output

Print nn integers ans1,ans2,…,ansnans_1,ans_2,\ldots,ans_n on one line, separated by spaces.

Subtasks

  • Subtask 1 (20 points): 1≤n≤251\le n\le 25; 1≤hi≤100000001\le h_i\le 10000000 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤hi≤100000001\le h_i\le 10000000 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤hi≤100000001\le h_i\le 10000000 for every 1≤i≤n1\le i\le n.

Examples

Input

4
1 2 3 2

Output

1 2 3 3

Explanation

The heights are [1,2,3,2][1,2,3,2]. Consider each required ending position:

  • i=1i=1: choose [1][1], giving ans1=1ans_1=1.
  • i=2i=2: choose [1,2][1,2], giving ans2=2ans_2=2.
  • i=3i=3: choose [1,2,3][1,2,3], giving ans3=3ans_3=3.
  • i=4i=4: height 22 cannot follow height 33 because that would decrease. But positions 1,2,41,2,4 give heights [1,2,2][1,2,2], a valid course of length 33. Thus ans4=3ans_4=3.

The output is 1 2 3 3.