#P14481. 星命定轨

    ID: 13370 远端评测题 500ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP贪心洛谷原创O2优化洛谷月赛Ad-hoc

星命定轨

Description

wwwwwza is a data structure expert, and today she found a data structure problem.

You have a sequence vv of length nn indexed starting from 11, consisting of non-negative integers, and a variable SS. Initially, S=0S=0. You can perform the following operation multiple times:

  • Choose three adjacent numbers a,b,ca,b,c in vv, then take the minimum of the numbers to the left of aa and to the right of cc (if they exist) with min(a,b,c)\min(a,b,c). Then remove these three numbers. This operation adds bb to SS.

Your task is to maximize SS.

Input Format

The first line contains an integer nn.

The second line contains nn integers, where the ii-th integer is viv_i.

Output Format

Output one line representing the answer.

10
2 4 0 7 5 7 6 9 6 8
19
9
9 9 8 2 4 4 3 5 3
18

Hint

Sample Explanation 1

Initially, the sequence is [2,4,0,7,5,7,6,9,6,8][2,4,0,7,5,7,6,9,6,8].

Perform these operations:

  1. Choose to remove the three numbers starting from the 7th position [6,9,6][6,9,6]. Now the vv array becomes [2,4,0,7,5,7,8][2,4,0,7,5,7,8]. However, the 6th and 7th positions need to take the minimum with min(6,9,6)=6\min(6,9,6)=6. Now the vv array becomes [2,4,0,7,5,6,6][2,4,0,7,5,6,6].

  2. Choose to remove the three numbers starting from the 5th position [5,6,6][5,6,6]. Now the vv array becomes [2,4,0,7][2,4,0,7]. However, the 4th and 5th positions (non-existent) need to take the minimum with min(5,6,6)=5\min(5,6,6)=5. Now the vv array becomes [2,4,0,5][2,4,0,5].

  3. Choose to remove the three numbers starting from the 1st position [2,4,0][2,4,0]. Now the vv array becomes [5][5]. However, the 0th position (non-existent) and the 1st position need to take the minimum with min(2,4,0)=0\min(2,4,0)=0. Now the vv array becomes [0][0].

S=9+6+4=19S=9+6+4=19, and it can be proven that you cannot obtain a scheme where S>19S>19.

Note that you don't need to empty the vv array.

Data Range

The test points are equally weighted with 10 points each.

Test Data nn\le
11 1010
232\sim 3 200200
454\sim 5 20002000
696\sim 9 10510^5
1010 10610^6

For all data, 1n106,0vi1091\le n \le 10^6,0\le v_i\le 10^9.

Please note the constant factor differences between different algorithms. The time and space limits for this problem are twice that of the standard solution.

Please note the special time limit for this problem.