cswork / OAOpen practice workspace

AmazonOA #108

Calculate Warehouse Efficiency (Probably for Supply Chain related NG positions --Fungible-- :)

Estimated MediumJudge-ready

Topics (AI-labelled)Stacks & HeapsGreedy

Each day, first pick one package that is still in the warehouse and has not been picked before, adding its weight to the efficiency; then ship out the current leftmost and rightmost packages (only one shipment when a single package remains), until the warehouse is empty. Maximize the total efficiency.

Input

The first line contains n (1..200000), and the second line contains n weights (0..10⁹).

Output

Output a single integer answer.

Example 1

Input:

6
4 4 8 5 3 2

Output:

17

Example 2

Input:

7
2 1 8 5 6 2 4

Output:

23

Example 3

Input:

1
9

Output:

9

Example notes

Example 1: over three days pick 4, 5, 8 in order, for a total of 17. Example 2: over four days you can pick 4, 6, 8, 5, for a total of 23. Example 3: on the only day pick 9, for a total of 9.

Editorial

Loading solution…