也是斜率优化。。。。推下式子就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000050
using namespace std;
long long n,a[maxn],g[maxn],q[maxn],l,r,f[maxn];
long long read()
{
char ch;long long data=;
while (ch<'' || ch>'') ch=getchar();
while (ch>='' && ch<='')
{
data=data*+ch-'';
ch=getchar();
}
return data;
}
double k(long long x,long long y)
{
return (double)(g[x]-g[y])/(x-y);
}
void dp()
{
f[]=a[];l=r=;q[l]=;g[]=a[];
for (long long i=;i<=n;i++)
{
while ((r-l) && (k(q[l],q[l+])<=i)) l++;
g[i]=f[i-]+a[i]+i*(i-)/;
while ((r-l) && (k(q[r-],q[r])>k(q[r],i))) r--;
q[++r]=i;
f[i]=f[q[l]-]+a[q[l]]+(i-q[l])*(i-q[l]+)/;
}
}
int main()
{
n=read();
for (long long i=n;i>=;i--) a[i]=read();
dp();
printf("%lld\n",f[n]);
return ;
}