This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pu push_back
#define ll long long
typedef pair <ll,ll> ii;
const int N=1e6;
long long mod=1e9;
long n,m,k;
long a[N+1],g[N+10];
ll dp[N+1];
ii b[N+10];
struct line{
long a,b,id;
};
line l[N+1],s[N+1],S[N+1];
double e[N+1];
bool cmp(line A,line B){
if(A.a!=B.a) return A.a>B.a;
else return A.b>B.b;
}
bool ok(long i){
if(l[i].a==s[k].a) return true;
if(k==1) return false;
double x1,x2;
x1=1.0*(l[i].b-s[k-1].b)/(s[k-1].a-l[i].a);
x2=1.0*(s[k].b-s[k-1].b)/(s[k-1].a-s[k].a);
return x1<=x2;
}
void tinh(){
cin>>n;
for(long i=1;i<=n;i++) {cin>>b[i].fi;}
for(int i=1;i<=n;i++) cin>>b[i].se;
g[n+1]=0;
for(int i=n;i>=1;i--) {g[i]=g[i+1]+b[i].se;
}
k=1;
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
l[1].b=b[1].fi*b[1].fi+g[2];
l[1].id=1;
l[1].a=-2*b[1].fi;
e[1]=1.0*1e18;
s[1]=l[1];
S[1]=l[1];
dp[1]=0;
for(long i=2;i<=n;i++){
long A=-2*b[i].fi,B=0;
ll res=mod;
for(int x=1;x<i;x++)
res=min(res,l[x].b + l[x].a*b[i].fi + b[i].fi*b[i].fi - g[i]);
dp[i]=res;
B=dp[i]+g[i+1]+b[i].fi*b[i].fi;
l[i].a=A;
l[i].b=B;
l[i].id=i;
if(l[i].a==s[k].a and l[i].b>=s[k].b) continue;
while(k>0 and ok(i)) k--;
k++;
s[k]=l[i];
e[k-1]=1.0*(s[k-1].b-s[k].b)/(s[k].a-s[k-1].a);
e[k]=1.0*1e18;
}
cout<<dp[n];
}
int main(){
//freopen("jday.inp","r",stdin);
//freopen("jday.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
tinh();
return 0;
}
//code của anh Nam đẹp trai nhất CHL
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |