luogu#P1934. 封印

封印

Background

Long ago, the Demon Realm suffered a great drought: all wells dried up, and temperatures kept rising. To save the people, Long Ming, the king of the Yasha clan, hoped to break the Well of Gods and Demons and enter the Human Realm to “steal” the Water Spirit Pearl to repair the earth’s water veins. However, there were seals between the Six Realms, and the Well of Gods and Demons was controlled by Shu Mountain and protected by a seal. As a royal of the Demon Realm, Long Ming had the art of traversing and could pass through any position that had a gap. Yet the seal left no gaps. Helpless, Long Ming had to forcibly break the seal, which would inevitably consume vital energy. To find the Water Spirit Pearl, Long Ming had to minimize his energy consumption. He could use the Traverse Technique at the same time as breaking the seal.

Problem 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