#P2426. 删数

删数

Description

There are NN distinct positive integers x1,x2,,xNx_1, x_2, \dots, x_N arranged in a row. At each operation, you may remove a consecutive block of ii numbers (1iN1 \le i \le N) from either the left end or the right end only. After removing, NiN - i numbers remain; continue applying the same operation until all numbers are removed.

Each operation has a value. Suppose you remove all numbers from position ii to position kk (inclusive). The operation value is xixk×(ki+1)|x_i - x_k| \times (k - i + 1). If you remove exactly one number, the operation value equals the value of that number.

Find how to operate to obtain the maximum possible total value, and output that maximum total value.

Input Format

  • The first line contains a positive integer NN.
  • The second line contains NN space-separated distinct positive integers.

Output Format

Output one line containing a single positive integer: the maximum total value.

6
54 29 196 21 133 118

768

Hint

Sample explanation and notes

It is possible to achieve the maximum value in 3 operations. First, remove the first 3 numbers: 5454, 2929, 196196, with operation value 426. Second, among the remaining three numbers (21,133,118)(21, 133, 118), remove the last number 118118, with operation value 118118. Third, remove the remaining two numbers 2121 and 133133, with operation value 224224. The total operation value is 426+118+224=768426 + 118 + 224 = 768.

Constraints

3N1003 \le N \le 100, 1xi10001 \le x_i \le 1000.

Translated by ChatGPT 5