#P1934. 封印

封印

Description

The seal of the Well of Gods and Demons has nn layers, and each layer has a sturdiness value. When Long Ming, a demon, breaks a single layer alone, the energy consumed equals the product of that layer’s sturdiness and the square of the total number of layers nn. He can also break all seals from layer ii to layer jj (with i<ji<j). The total energy consumed equals the product of the sum of the sturdiness of layers ii and jj and the sum of the sturdiness of all layers from ii to jj inclusive. However, to avoid alarming Shu Mountain, the sum of the sturdiness of layers ii and jj must not exceed tt (this restriction does not apply when breaking a single layer).

Input Format

The first line contains two positive integers nn and tt.
The second line contains nn positive integers, where the ii‑th number is aia_i, representing the sturdiness of layer ii.

Output Format

Output a single line containing one positive integer, the minimum energy consumed.

6 10
8 5 7 9 3 5
578

Hint

Sample Explanation

First break the first layer alone, then use the Traverse Technique to break directly from the second layer to the last layer. The energy consumed is $8 \times 6^2 + (5 + 5) \times (5 + 7 + 9 + 3 + 5) = 578$.

Constraints

For 10%10\% of the testdata, n10n \le 10.
For 50%50\% of the testdata, n100n \le 100.
For 70%70\% of the testdata, n500n \le 500.
For 100%100\% of the testdata, n1000n \le 1000, ai(1in), t20000a_i (1 \le i \le n),\ t \le 20000.

Translated by ChatGPT 5