fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;using ll=long long;using I=__int128_t;struct L{ll k,m;};I f(L a,ll x){return(I)a.k*x+(I)a.m;}bool b(L a,L c,L d){return(I)(d.m-a.m)*(a.k-c.k)<=(I)(c.m-a.m)*(a.k-d.k);}int main(){ios::sync_with_stdio(0);cin.tie(0);int n;ll A,B,C;cin>>n>>A>>B>>C;deque<L> q={{0,0}};ll p=0,dp=0,x;for(int i=0;i<n;i++){cin>>x;p+=x;while(q.size()>1&&f(q[0],p)<=f(q[1],p))q.pop_front();dp=(ll)((I)A*p*p+(I)B*p+(I)C+f(q[0],p));L nl={-2*A*p,dp+A*p*p-B*p};while(q.size()>1&&b(q[q.size()-2],q.back(),nl))q.pop_back();q.push_back(nl);}cout<<dp;}
Success #stdin #stdout 0s 5320KB
stdin
4
-1 10 -20
2 2 3 4
stdout
9