#ds601. 最大和上升子序列2

最大和上升子序列2

Maximum-Sum Increasing Subsequence 2

Problem Description

You are given an array a1,a2,…,ana_1,a_2,\dots ,a_n of length nn. Find the increasing subsequence with the maximum sum. That is, find indices p1,p2,…,pmp_1,p_2,\dots,p_m satisfying 1≤p1<p2<⋯<pm≤n1\leq p_1 < p_2 < \dots < p_m \leq n and ap1<ap2<⋯<apma_{p_1} < a_{p_2} < \dots < a_{p_m}, so as to maximize ap1+ap2+⋯+apma_{p_1} + a_{p_2} + \dots + a_{p_m}.

Input Format

The first line contains one number nn.

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

Output Format

Output one number, the answer.

Sample Input

6
3 7 4 2 6 8

Sample Output

21

Constraints

For all data, 1≤n≤2×1051\leq n\leq 2 \times 10^5 and 1≤ai≤2×1051 \leq a_i \leq 2\times 10^5 are guaranteed.