#d2289. 最大和上升子序列

最大和上升子序列

Maximum Sum Increasing Subsequence

Problem Description

Given an array a1,a2,…,ana_1, a_2, \dots, a_n of length nn, find the increasing subsequence with the largest sum. That is, find indices p1,p2,…,pmp_1, p_2, \dots, p_m satisfying 1≤p1<p2<⋯<pm≤n1\le p_1 < p_2 < \dots < p_m \le n and ap1<ap2<⋯<apma_{p_1} < a_{p_2} < \dots < a_{p_m}, such that ap1+ap2+⋯+apma_{p_1} + a_{p_2} + \dots + a_{p_m} is maximized.

Input Format

The first line contains an integer nn.

The next line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n.

Output Format

One number: the answer.

Sample Input

6
3 7 4 2 6 8

Sample Output

21

Constraints

For all data, 1≤n≤1000,1≤ai≤1051\le n\le 1000, 1 \le a_i \le 10^5.