#t387. 最大上升子序列和

最大上升子序列和

Maximum Increasing Subsequence Sum

Problem Description

For a sequence bib_i, when b1<b2<⋯<bsb_1<b_2<\dots<b_s, we call it increasing.

Given a sequence (a1,a2,…,aN)(a_1,a_2,\dots,a_N), we can obtain some increasing subsequences (ai1,ai2,…,aiK)(a_{i1},a_{i2},\dots,a_{iK}) where 1≤i1<i2<⋯<iK≤N1\le i_1<i_2<\dots<i_K\le N. For example, from the sequence (1,7,3,5,9,4,8)(1,7,3,5,9,4,8) we get increasing subsequences such as (1,7)(1,7) and (3,4,8)(3,4,8). The largest sum among these is 1818, from the subsequence (1,3,5,9)(1,3,5,9).

Your task is, given a sequence, to compute the maximum increasing subsequence sum. Note that the longest increasing subsequence need not have the maximum sum; for example, the sequence (100,1,2,3)(100,1,2,3) has maximum increasing subsequence sum 100100, while its longest increasing subsequence is (1,2,3)(1,2,3).

Input Format

The first line is the length of the sequence N (1≤N≤1000)N\ (1\le N\le 1000).

The second line gives the NN integers of the sequence, each in the range 00 to 1000010000 (may repeat).

Output Format

The maximum increasing subsequence sum.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

18