The meaning of problems: length \ (N (N <= 500000 ) \) sequence, each element of the sequence \ (C_i \) , the sequence is divided into several batches, each batch of the cost of \ ((\ sum c_i) 2 + M ^ \) , M is a known constant.
Analysis: Let \ (F [i] \) represents the i-th element before divided batches of the minimum cost, \ (F [i] = F [J] + ([i] SUM -sum [J]) ^ 2+ M \)
Set \ (k <j \) and j better than k, there,
\(f[j]+(sum[i]-sum[j])^2+M<f[k]+(sum[i]-sum[k])^2+M\)
Sort out the above yields,
\(\frac{f[j]+sum[j]^2-f[k]-sum[k]^2}{sum[j]-sum[k]}<2sum[i]\)
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;
inline int read(){
int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
return s*w;
}
const int N=500005;
int n,m,l,r,c[N],q[N];
LL sum[N],f[N];
inline LL count(int x,int y){return f[x]+sum[x]*sum[x]-f[y]-sum[y]*sum[y];}
int main(){
while((scanf("%d %d",&n,&m))!=EOF){
for(int i=1;i<=n;i++){
f[i]=0;q[i]=0;
c[i]=read();
sum[i]=sum[i-1]+c[i];
}
l=1,r=1;
for(int i=1;i<=n;i++){
while(l<r&&count(q[l+1],q[l])<=2*sum[i]*(sum[q[l+1]]-sum[q[l]]))l++;
f[i]=f[q[l]]+(sum[i]-sum[q[l]])*(sum[i]-sum[q[l]])+m;
while(l<r&&count(q[r],q[r-1])*(sum[i]-sum[q[r]])>=count(i,q[r])*(sum[q[r]]-sum[q[r-1]]))r--;
q[++r]=i;
}
printf("%lld\n",f[n]);
}
return 0;
}