1 #include <cstdio>
2 #include <cstring>
3 #include <algorithm>
4 #include <iostream>
5 using namespace std;
6 typedef long long LL;
7
8 const int MAXN = 50010;
9
10 LL sum[MAXN], dp[MAXN];
11 int c[MAXN], n, L;
12
13 LL sqrt_cost(int i, int j) {
14 return j - i + sum[j] - sum[i - 1] - L;
15 }
16
17 LL cost(int i, int j) {
18 LL res = sqrt_cost(i, j);
19 return res * res;
20 }
21 /*
22 bool check(int pos, int a, int b) {
23 return dp[a] + cost(a + 1, pos) >= dp[b] + cost(b + 1, pos);
24 }
25 */
26 bool check(int pos, int a, int b) {
27 LL aa = dp[a], bb = sqrt_cost(a + 1, pos), cc = dp[b], dd = sqrt_cost(b + 1, pos);
28 return (cc - aa - 1) / (bb - dd) + 1 <= bb + dd;
29 }
30
31 int binary_search(int st, int ed, int a, int b) {
32 int l = st, r = ed;
33 while(l < r) {
34 int mid = (l + r) >> 1;
35 if(!check(mid, a, b)) l = mid + 1;
36 else r = mid;
37 }
38 return l;
39 }
40
41 struct Node {
42 int pos, best;
43 Node() {}
44 Node(int pos, int best): pos(pos), best(best) {}
45 } stk[MAXN];
46 int top;
47
48 LL solve() {
49 int v = 1;
50 stk[++top] = Node(1, 0);
51 for(int i = 1; i <= n; ++i) {
52 if(v < top && stk[v + 1].pos <= i) ++v;
53 int p = stk[v].best;
54 dp[i] = dp[p] + cost(p + 1, i);
55
56 while(v < top && check(stk[top].pos, stk[top].best, i))
57 --top;
58 int r = stk[top + 1].pos ? stk[top + 1].pos + 1 : n + 1;
59 int t = binary_search(max(stk[top].pos, i) + 1, r, stk[top].best, i);
60 if(t <= n) stk[++top] = Node(t, i);
61 }
62 return dp[n];
63 }
64
65 int main() {
66 scanf("%d%d", &n, &L);
67 for(int i = 1; i <= n; ++i) scanf("%d", &c[i]);
68 for(int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + c[i];
69 cout<<solve()<<endl;
70 }