#P17457. [GESP202609 六级] 数组划分

[GESP202609 六级] 数组划分

Problem Description

You are given an array A=[a1,a2,…,an]A=[a_1,a_2,\ldots,a_n] consisting of nn integers.

You need to partition array AA into several non-empty contiguous subsegments. For any subsegment obtained by the partition, its deviation value is defined as the square of the sum of the integers in that subsegment. The deviation value of a partition plan is defined as the sum of the deviation values of all subsegments.

You need to minimize the deviation value of the partition plan.

Formally, you can partition AA into several non-empty contiguous subsegments A1,A2,…,AkA_1,A_2,\ldots,A_k such that A=A1+A2+…+AkA=A_1+A_2+\ldots+A_k, where ++ denotes concatenation of arrays. For 1≤i≤k1\le i\le k, let Ai=[a1(i),…,ami(i)]A_i=[a_1^{(i)},\ldots,a_{m_i}^{(i)}] contain mim_i integers. You need to minimize $\sum_{i=1}^{k}\left(\sum_{j=1}^{m_i}a_j^{(i)}\right)^2$.

Input Format

The first line contains a positive integer nn, representing the length of array AA.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, representing array AA.

Output Format

One line containing one integer, representing the minimum possible deviation value of the partition plan.

4
1 2 -3 4
6
6
-1 -1 4 -5 -1 4
0

Hint

For 40%40\% of the test points, it is guaranteed that 0≤ai≤500\le a_i\le 50.

For all test points, it is guaranteed that 1≤n≤20001\le n\le 2000 and −100≤ai≤100-100\le a_i\le 100.

Translated by ChatGPT 5