#P7319. 「PMOI-4」生成树

「PMOI-4」生成树

Description

Given nn numbers, the original weight of the ii-th number is wiw_i. You need to choose these numbers one by one in some order.

Suppose it is currently the ii-th selection, and the chosen number has original weight kk. Then the weights of all other numbers that have not been chosen will each increase by (1)i+k+1×k(-1)^{i+k+1} \times k.

You need to find a selection plan such that the sum of the final weights of the nn chosen numbers is maximized.

Input Format

The first line contains a positive integer nn.

The second line contains nn integers wiw_i, representing the weight of the ii-th number.

Output Format

Output one integer in one line, representing the maximum sum of weights.

7
1 -1 -2 2 -3 3 4
66

Hint

[Sample Explanation]

Choosing the numbers with indices {7,6,5,3,4,1,2}\{7,6,5,3,4,1,2\} in order works.

[Constraints]

This problem uses bundled testdata.

  • Subtask 1 (20pts): n7n \le 7.
  • Subtask 2 (30pts): n103n \le 10^3.
  • Subtask 3 (30pts): It is guaranteed that either all wi0w_i \ge 0 or all wi0w_i \le 0.
  • Subtask 4 (20pts): No special constraints.

For 100%100\% of the testdata, 1n105,109w1091 \le n \le 10^5, -10^9 \le w \le 10^9.

Translated by ChatGPT 5