cswork / OAOpen practice workspace

AmazonOA #277

Maximum Number of Products You Can Pick

Estimated HardJudge-ready

Topics (AI-labelled)Stacks & HeapsDP Basics

Choose a non-empty contiguous subarray and take at least one item, and at most its stock, from each chosen shelf, so that the amounts taken are strictly increasing from left to right. Find the maximum total number of items. You cannot skip shelves in the middle of the chosen subarray.

Input

The first line contains n and the second line contains products. 1≤n≤5000, 1≤products[i]≤10^9. This full range comes from the Constraints in the raw body, not from the cleaned-up version truncated by HTML.

Output

Output a single integer, the answer.

Example 1

Input:

6
2 9 4 7 5 3

Output:

16

Example 2

Input:

3
7 5 3

Output:

9

Example 3

Input:

3
1 1 1

Output:

1

Example notes

The answers are 16, 9 and 1 respectively. In the second example you can choose just the first two shelves and take 4 and 5 for 9; taking all three shelves gives only 1+2+3=6.

Editorial

Loading solution…