#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 10;
int n, p;
int s[N], h, t, q[N], f[N];
int X(int x)
{
return s[x];
}
int Y(int x)
{
return f[x] + X(x) * X(x);
}
int K(int x)
{
return 2 * (X(x) - p);
}
double slope(int x, int y)
{
return double(Y(y) - Y(x)) / double(X(y) - X(x));
}
signed main()
{
cin >> n >> p;
p ++;
for (int i = 1; i <= n; i ++)
{
int x;
cin >> x;
s[i] = s[i - 1] + x + 1;
}
h = 1, t = 0;
q[++ t] = 0;
for (int i = 1; i <= n; i ++)
{
int k = K(i);
while (h < t && Y(q[h]) - k * X(q[h]) >= Y(q[h + 1]) - k * X(q[h + 1]))
h ++;
f[i] = f[q[h]] + (s[i] - s[q[h]] - p) * (s[i] - s[q[h]] - p);
while (h < t && slope(q[t], q[t - 1]) >= slope(q[t], i))
t --;
q[++ t] = i;
}
cout << f[n] << '\n';
return 0;
}
11-09
164
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
11-09
120
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
09-08
138
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
10-29
270
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)