//直接比较斜率容易出问题,因为是浮点型,wa了很多次
#include<iostream>
#include<cstdio>
#include<cmath>
#define N 500010
using namespace std;
int a[N],s[N],dp[N],id[N];
int n,m;
int up(int i,int j)
{
return dp[i]-dp[j]+s[i]*s[i]-s[j]*s[j];
}
int down(int i,int j)
{
return 2*(s[i]-s[j]);
}
int get(int x,int i)
{
return dp[x]+(s[i]-s[x])*(s[i]-s[x])+m;
}
int main()
{
//freopen("1.in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
s[0]=0;
for(int i=1;i<=n;i++){scanf("%d",&a[i]);s[i]=s[i-1]+a[i];}
int head=0,tail=1;id[0]=0;dp[0]=0;
for(int i=1;i<=n;i++)
{
while(head<tail-1&&up(id[head+1],id[head])<=s[i]*down(id[head+1],id[head]))head++;
dp[i]=get(id[head],i);
while(head<tail-1&&up(i,id[tail-1])*down(id[tail-1],id[tail-2])<=up(id[tail-1],id[tail-2])*down(i,id[tail-1]))tail--;
id[tail++]=i;
}
printf("%d\n",dp[n]);
}
return 0;
}
08-17
08-17