cswork / OAOpen practice workspace

The D. E. Shaw GroupOA #6

Get Minimum Cost

Estimated HardJudge-ready

Topics (AI-labelled)Tree & Game DPDP Basics

Each time, choose any two of the first three elements of the current array and delete them; the cost of the operation is the larger of the two deleted numbers. If fewer than three elements remain, delete all the remaining elements at once, at a cost equal to their maximum. Find the minimum total cost to empty the array.

The standard input/output, the ranges added by this site, and the examples follow this problem's description.

Input

The first line contains n (1≤n≤1000); the second line contains n positive integers, 1≤arr[i]≤10^6. The ranges are taken from the original problem's constraints.

Output

Output the minimum total cost.

Example 1

Input:

1
7

Output:

7

Example 2

Input:

2
4 2

Output:

4

Example 3

Input:

5
1 2 3 4 5

Output:

9

Editorial

Loading solution…