#P7594. 「EZEC-8」Clean Up

「EZEC-8」Clean Up

Description

There is an interval of width nn. At position ii, there are aia_i blocks stacked up like Tetris.

You may choose any position and spend kk energy to remove at most kk blocks at that position. At the same time, at a position whose distance from the chosen position is dd, you can remove at most max(kd,0)\max(k-d,0) blocks, where the distance between two adjacent positions is 11. Note that kk must be a positive integer.

Find the minimum amount of energy needed to clear all blocks in the entire interval.

Input Format

The input has two lines.

The first line contains an integer nn.

The second line contains nn integers, where the ii-th number is aia_i.

Output Format

Output one line with one number, indicating the minimum required energy.

5
1 4 3 4 1

5

8
1 2 1 0 0 1 2 1

4

Hint

Sample Explanation

For the first sample, one optimal plan is to choose position 33 and spend 55 energy.

For the second sample, one optimal plan is to choose position 22 and spend 22 energy, then choose position 77 and spend 22 energy.


This problem uses bundled testdata.

  • Subtask 1 (5 points): n2n \leq 2.
  • Subtask 2 (20 points): n103n \leq 10^3, ai0a_i \neq 0.
  • Subtask 3 (20 points): ai0a_i \neq 0.
  • Subtask 4 (20 points): n103n \leq 10^3.
  • Subtask 5 (35 points): No special constraints.

For 100%100\% of the testdata, 1n1061 \le n \leq 10^6, 0ai1090 \leq a_i \leq 10^9.

Translated by ChatGPT 5