The Stairways of Saharna

Time limit0.2sMemory limit128 MB

Summary
Split a sequence into k disjoint non-decreasing subsequences to maximize the total number of chosen elements, and output this maximum for every k up to the point where all n elements are used.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Array, Sorting
Solved
No attempts yet

Problem

Saharna is a beautiful, scenic place in Moldova where, among the caves and waterfalls, one can find stones of many different shapes and sizes. In the Middle Ages these stones were used to build the stairways of fortresses, and as a rule each step of a stairway was made from a single stone.

The stones are heavy, so they lie fixed in a row in a given order. The craftsmen know the height of every stone, so they are given a sequence of integers H=(h1,h2,…,hi,…,hn)H = (h_1, h_2, \dots, h_i, \dots, h_n), where hih_i is the height of the ii-th stone.

To build a stairway, a craftsman walks along the row and picks the stones for the steps one after another, keeping their original order. A stone may be chosen only if its height is not lower than the height of the previously chosen stone. In other words, a stairway is a non-decreasing subsequence of HH (with the original order preserved).

For example, if H=(1,3,4,2,3,4,1,2,2,3,3,2)H = (1, 3, 4, 2, 3, 4, 1, 2, 2, 3, 3, 2), the underlined stones below form one stairway:

H=(1‾,3,4,2‾,3,4,1,2‾,2‾,3‾,3‾,2)H = (\underline{1}, 3, 4, \underline{2}, 3, 4, 1, \underline{2}, \underline{2}, \underline{3}, \underline{3}, 2)

Since bigger is better, to build a finer castle the craftsmen want to use as many stones as possible.

Let L(H,k)L(H, k) be the maximum number of stones that can be used to build kk stairways, where the stairways use disjoint stones and each stairway has at least one step.

For the example above, L(H,1)=6L(H, 1) = 6: the underlined stones form an optimal single stairway.

Likewise, L(H,2)=9L(H, 2) = 9. In the picture below the stones of the first stairway are marked with a single underline ( ‾\underline{\ }) and the stones of the second with a double underline ( ‾‾\underline{\underline{\ }}):

H=(1‾,3‾‾,4‾‾,2‾,3,4‾‾,1,2‾,2‾,3‾,3‾,2)H = (\underline{1}, \underline{\underline{3}}, \underline{\underline{4}}, \underline{2}, 3, \underline{\underline{4}}, 1, \underline{2}, \underline{2}, \underline{3}, \underline{3}, 2)

For k=2k = 2 the first stairway uses 6 stones and the second uses 3.

If we want three stairways, the maximum number of stones that can be used is shown below:

H=(1‾,3‾‾‾,4‾‾‾,2‾,3‾,4‾‾‾,1‾‾,2‾‾,2‾‾,3‾,3‾,2‾‾)H = (\underline{1}, \underline{\underline{\underline{3}}}, \underline{\underline{\underline{4}}}, \underline{2}, \underline{3}, \underline{\underline{\underline{4}}}, \underline{\underline{1}}, \underline{\underline{2}}, \underline{\underline{2}}, \underline{3}, \underline{3}, \underline{\underline{2}})

The triple underline marks the third stairway; the other marks keep their previous meaning. Hence L(H,3)=12L(H, 3) = 12. For k=3k = 3 the first stairway uses 5 stones, the second 4, and the third 3. Note that the first and second stairways chosen for k=3k = 3 may differ from those chosen for k=1k = 1 and k=2k = 2.

As kk takes the values 1,2,3,…1, 2, 3, \dots, at some point, for some number qq, we get L(H,q)=nL(H, q) = n, where nn is the total number of stones.

Given the sequence of heights HH, write a program that computes L(H,k)L(H, k) for every k=1,2,…,qk = 1, 2, \dots, q.

Input

The first line contains a positive integer nn. The second line contains nn positive integers h1,h2,…,hnh_1, h_2, \dots, h_n, separated by spaces.

Output

Print qq lines. The kk-th line must contain L(H,k)L(H, k) for k=1,2,…,qk = 1, 2, \dots, q, where qq is the smallest value with L(H,q)=nL(H, q) = n.

Constraints

  • 1≤n≤50001 \le n \le 5000
  • 1≤hi≤255, i=1,2,…,n1 \le h_i \le 255,\ i = 1, 2, \dots, n

Examples1

  1. Example 1

    Input
    12
    1 3 4 2 3 4 1 2 2 3 3 2
    
    Expected output
    6
    9
    12