考虑最普通的(dp)
[dp[i]=max(dp[j]+a*(sum[i]-sum[j])^2+b*(sum[i]-sum[j])+c
]
qwq
由于演算纸扔掉了
qwq
所以直接给出最后的柿子
设(f[x]=dp[x]+a*sum[x]^2)
[frac{f[j]-f[k]}{s[j]-s[k]}>2*a*sum[i]+b
]
所以直接维护一个上凸壳就好了啦
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk make_pair
#define ll long long
#define int long long
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 2e6+1e2;
struct Point{
int x,y,num;
};
Point q[maxn];
int n,m;
int sum[maxn];
int val[maxn];
int a,b,c;
int head=1,tail=0;
int dp[maxn];
int chacheng(Point x,Point y)
{
return x.x*y.y-x.y*y.x;
}
bool count(Point i,Point j,Point k)
{
Point x,y;
x.x=k.x-i.x;
x.y=k.y-i.y;
y.x=k.x-j.x;
y.y=k.y-j.y;
if(chacheng(x,y)>=0) return true;
return false;
}
void push(Point x)
{
while (tail>=head+1 && count(q[tail-1],q[tail],x)) tail--;
q[++tail]=x;
}
void pop(int lim)
{
while (tail>=head+1 && q[head+1].y-q[head].y>lim*(q[head+1].x-q[head].x)) head++;
}
signed main()
{
n=read();
a=read(),b=read(),c=read();
for (int i=1;i<=n;i++) val[i]=read();
for (int i=1;i<=n;i++) sum[i]=sum[i-1]+val[i];
push((Point){0,0,0});
for (int i=1;i<=n;i++)
{
pop(2*a*sum[i]+b);
int now = q[head].num;
dp[i]=dp[now]+a*(sum[i]-sum[now])*(sum[i]-sum[now])+b*(sum[i]-sum[now])+c;
push((Point){sum[i],dp[i]+a*sum[i]*sum[i],i});
}
cout<<dp[n]<<endl;
return 0;
}