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;
int h[100001];
int w[100001];
long long f[100001];
struct line
{
long long k,n;
};
long long get(line a,long long x)
{
return a.k*x+a.n;
}
line tree[4000005];
void add_line(line nw,int start=0,long long l=0,long long r=1000001)
{
long long m=(l+r)/2;
bool left=get(nw,l)<get(tree[start],l);
bool mid=get(nw,m)<get(tree[start],m);
if(mid)
{
swap(tree[start],nw);
}
if(r-l==1)
{
return;
}
else if(left!=mid)
{
add_line(nw,2*start+1,l,m);
}
else
{
add_line(nw,2*start+2,m,r);
}
}
long long query(int x,int start=0,long long l=0,long long r=1000001)
{
long long m=(l+r)/2;
if(r-l==1)
{
return get(tree[start],x);
}
else if(x<m)
{
return min(get(tree[start],x),query(x,2*start+1,l,m));
}
else
{
return min(get(tree[start],x),query(x,2*start+2,m,r));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> h[i];
f[i]=1000000000000000000;
}
long long tot=0;
for(int i=0;i<n;i++)
{
cin >> w[i];
tot+=w[i];
}
for(int i=0;i<4000005;i++)
{
tree[i]={0,1000000000000000000};
}
f[0]=-w[0];
add_line({-2*h[0],f[0]+h[0]*h[0]});
for(int i=1;i<n;i++)
{
f[i]=query(h[i])+h[i]*h[i]-w[i];
add_line({-2*h[i],f[i]+h[i]*h[i]});
}
cout << tot+f[n-1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |