Code:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 123;
long long s[maxn], f[maxn];
int l, n, q[maxn];
inline long long re_x(int i){ return s[i]; }
inline long long re_y(int i){ return f[i] + (s[i] + l) * (s[i] + l); }
inline double get_slope(int i,int j){return (double)(re_y(i) - re_y(j)) / (re_x(i) - re_x(j)); }
int main()
{
scanf("%d%d",&n,&l);
for(int i = 1;i <= n; ++i) scanf("%lld",&s[i]), s[i] += s[i-1];
for(int i = 1;i <= n; ++i) s[i] += i;
int head = 0, tail = 0;
for(int i = 1;i <= n; ++i)
{
while(head < tail && get_slope(q[head], q[head + 1]) < 2 * s[i] ) ++ head;
f[i] = f[q[head]] + (s[i] - s[q[head]] - 1 - l) * (s[i] - s[q[head]] - 1 - l);
while(tail > head && get_slope(q[tail], i) < get_slope(i, q[tail - 1])) --tail;
q[++tail] = i;
}
printf("%lld",f[n]);
return 0;
}