cswork / OAOpen practice workspace

AmazonOA #214

Get Maximum

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyStacks & Heaps

Choose a non-empty contiguous range and take a positive integer number of items from each pile, no more than that pile's capacity, so that the amounts taken strictly increase from left to right. Find the maximum number of items that can be taken. A prefix that takes 0 contributes nothing to the total and can simply be dropped, so it does not affect the optimal answer.

Input

The first line contains n; the second line contains the pile capacities. 1≤n≤5000, 1≤capacity≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

6
7 4 5 2 6 5

Output:

12

Example 2

Input:

6
2 9 4 7 5 2

Output:

16

Example 3

Input:

4
2 5 6 7

Output:

20

Example notes

The answers are 12, 16 and 20.

Editorial

Loading solution…