/* Dp [I] is the minimum cost of writing I characters dp [I] = min (dp [I], dp [j] + (sum [I]-sum [j]) * (sum [I]-sum [j]) + m ); dp [I] = min (dp [I], dp [k] + (sum [I]-sum [k]) * (sum [I]-sum [k]) + m); k is better than k when j is set, k <j => dp [j] + (sum [I]-sum [j]) * (sum [I]-sum [j])> = dp [k] + (sum [I]-sum [k]) * (sum [I]-sum [k]) + draft paper ==> [(dp [j] + sum [j] * sum [j]) -(dp [k] + sum [k] * sum [k])]/2 (sum [j]-sum [k]) <= sum [I] ==> G [j, k] = (yj-yk)/(xj-xk) <= 2 * sum [I]; [When doing the question, consider it as--> (yj-yk) <= 2 * sum [I] * (xj-xk); when division is prevented ], When, k <j, otherwise k> j; when g [k, j]> g [j, I], j can be removed (that is, the top Convex Point, proof can be divided into g [j, I] <sum [I] and> sum [I) */# include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define maxn 500005int sum [maxn]; int dp [maxn]; int a [maxn]; int q [maxn]; int y (int I) {return dp [I] + sum [I] * sum [I];} int Gup (int j, int k) // molecule {return y (j)-y (k);} int Gdown (int j, int k) // denominator {return sum [j]-sum [k];} inline int ReadInt () // click the top {char ch = ge Tchar (); int data = 0; while (ch <'0' | ch> '9') {ch = getchar ();} do {data = data * 10 + ch-'0'; ch = getchar () ;}while (ch> = '0' & ch <= '9 '); return data;} int main () {int n, m; while (scanf ("% d", & n, & m )! = EOF) {sum [0] = 0; for (int I = 1; I <= n; I ++) {a [I] = ReadInt (); sum [I] = sum [I-1] + a [I];} int head = 0, tail =-1; q [+ tail] = 0; for (int I = 1; I <= n; I ++) {// head this is fine while (head <tail & Gup (q [head + 1], q [head]) <= 2 * sum [I] * Gdown (q [head + 1], q [head]) head ++; dp [I] = dp [q [head] + (sum [I]-sum [q [head]) * (sum [I]-sum [q [head]) + m; // dp equation // tail note that the denominator has a size relationship, pass the past to change the number AH, WA dead me Ah while (head <tail & Gup (I, q [tail]) * Gdown (q [tail], q [tail-1]) <= Gup (q [tail], q [tail-1]) * Gdown (I, q [tail]) // k <j <I => tail, tail-1, I; tail --; q [++ tail] = I;} printf ("% d \ n ", dp [n]);} return 0 ;}