Description
Input
Output
Sample Input
4
100 1
15 15
20 5
1 100
输入解释:
共有4块土地.
100 1
15 15
20 5
1 100
输入解释:
共有4块土地.
Sample Output
500
HINT
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000000+5;
int read()
{
int x=0,f=1;
char ch;
ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n,a,b,c;
int w[maxn],q[maxn];
long long s[maxn],f[maxn];
long long g(int i)
{
return a*s[i]*s[i]+b*s[i];
}
long long h(int j)
{
return f[j]+a*s[j]*s[j]-b*s[j];
}
double slope(int j1,int j2)
{
return (h(j1)-h(j2))/(2.0*a*(s[j1]-s[j2]));
}
long long js(int x)
{
return a*x*x+b*x+c;
}
int main()
{
int i;
n=read();
a=read();b=read();c=read();
for(i=1;i<=n;i++)
w[i]=read();
for(i=1;i<=n;i++)
s[i]=s[i-1]+w[i];
int h=1,p=0;
q[++p]=0;
for(i=1;i<=n;i++)
{
while(p>h&&slope(q[h],q[h+1])<=s[i]) h++;
int t=q[h];
f[i]=f[t]+js(s[i]-s[t]);
while(p>h&&slope(q[p],i)<slope(q[p-1],q[p])) p--;
q[++p]=i;
}
printf("%lld",f[n]);
return 0;
}