#P17015. [GESP202606 七级] 消消乐

    ID: 19305 远端评测题 1000ms 512MiB 尝试: 12 已通过: 5 显示难度普及 上传者: 标签>动态规划 DP记忆化搜索区间 DP2026GESP

[GESP202606 七级] 消消乐

Problem Description

You are given an array a=[a1,,an]a = [a_1, \ldots, a_n] consisting of nn integers. Each time, you may perform the following operation on the array aa until aa becomes empty:

  • Choose an element in aa, gain a score equal to the sum of its adjacent elements on both sides, and then delete this element from aa.

In particular, if an adjacent element does not exist, its value is considered to be 00. For example, for a=[1,2,3]a = [1, 2, 3], you can do the following operations:

  • Choose element 22, gain a score of 1+31 + 3, after deleting 22 we have a=[1,3]a = [1, 3];
  • Choose element 11, gain a score of 0+30 + 3, after deleting 11 we have a=[3]a = [3];
  • Choose element 33, gain a score of 0+00 + 0, after deleting 33 the array aa becomes empty.

What is the maximum possible total score you can obtain?

Input Format

The first line contains a positive integer nn, representing the length of the array.

The second line contains nn non-negative integers a1,,ana_1, \ldots, a_n, representing the integers in the array aa.

Output Format

Output one line containing one integer, representing the maximum total score you can obtain.

6
1 6 3 2 9 1
55
5
3 1415 926 53 58
5771

Hint

Constraints

For 40%40\% of the testdata, it is guaranteed that 1n501 \le n \le 50 and 0ai1030 \le a_i \le 10^3.

For all testdata, it is guaranteed that 1n1001 \le n \le 100 and 0ai1090 \le a_i \le 10^9.

Translated by ChatGPT 5