#include<bits/stdc++.h>
#define int long long
typedef long double ld;
typedef long long ll;
using namespace std;
const int C = (int)5e4 + 5;
int dp[200000];
int w[200000];
int h[200000];
const int mxN = (int)1e5+10;
const int mxC = (int)1e6+10;
const int LINF = (int)1e12+10;
struct Line{
int m, c;
Line(){ m=c=LINF; }
Line(int _m, int _c){ m = _m, c = _c;}
int operator()(int x){ return m*x+c; }
} seg[2*mxC+10];
void addLine(Line a, int p=0, int l=1, int r=mxC){
if(l==r){
if(seg[p](l)>a(l)) seg[p]=a;
return;
}
int mid = (l+r)>>1; int rp = p+2*(mid-l+1);
if(a.m > seg[p].m) swap(a,seg[p]);
if(a(mid) < seg[p](mid))
swap(a,seg[p]), addLine(a,p+1,l,mid);
else addLine(a,rp,mid+1,r);
}
int query(int x, int p=0, int l=1, int r=mxC){
if(l==r) return seg[p](x);
int mid = (l+r)>>1; int rp = p+2*(mid-l+1);
if(x<=mid) return min(query(x,p+1,l,mid),seg[p](x));
return min(query(x,rp,mid+1,r),seg[p](x));
}
int32_t main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>h[i];
for(int i=1;i<=n;i++)
cin>>w[i];
for(int i=2;i<=n;i++)
w[i]+=w[i-1];
addLine({-2*h[1],h[1]*h[1]-w[1]});
for(int i=2;i<=n;i++)
{
dp[i]=query(h[i])+h[i]*h[i]+w[i-1];
addLine({-2*h[i],h[i]*h[i]-w[i]+dp[i]});
}
cout<<dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
18 ms |
31548 KB |
Output is correct |
3 |
Correct |
17 ms |
31640 KB |
Output is correct |
4 |
Correct |
17 ms |
31672 KB |
Output is correct |
5 |
Correct |
14 ms |
31620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
35012 KB |
Output is correct |
2 |
Correct |
49 ms |
34892 KB |
Output is correct |
3 |
Correct |
54 ms |
34992 KB |
Output is correct |
4 |
Correct |
47 ms |
34812 KB |
Output is correct |
5 |
Correct |
43 ms |
34904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
31572 KB |
Output is correct |
2 |
Correct |
18 ms |
31548 KB |
Output is correct |
3 |
Correct |
17 ms |
31640 KB |
Output is correct |
4 |
Correct |
17 ms |
31672 KB |
Output is correct |
5 |
Correct |
14 ms |
31620 KB |
Output is correct |
6 |
Correct |
54 ms |
35012 KB |
Output is correct |
7 |
Correct |
49 ms |
34892 KB |
Output is correct |
8 |
Correct |
54 ms |
34992 KB |
Output is correct |
9 |
Correct |
47 ms |
34812 KB |
Output is correct |
10 |
Correct |
43 ms |
34904 KB |
Output is correct |
11 |
Correct |
67 ms |
35104 KB |
Output is correct |
12 |
Correct |
57 ms |
34880 KB |
Output is correct |
13 |
Correct |
53 ms |
34992 KB |
Output is correct |
14 |
Correct |
75 ms |
35148 KB |
Output is correct |
15 |
Correct |
43 ms |
34776 KB |
Output is correct |
16 |
Correct |
41 ms |
34912 KB |
Output is correct |
17 |
Correct |
38 ms |
35024 KB |
Output is correct |
18 |
Correct |
40 ms |
35052 KB |
Output is correct |