#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct line {
ll slope,con;
} s[1<<22];
ll f(line a, ll x) {
return a.slope*x+a.con;
}
int n,mx=1000000;
ll h[100001],w[100001],sum,dp[100001];
void add(line a, int l, int r, int idx) {
int mid=(l+r)/2;
bool m = f(a,mid) < f(s[idx],mid);
bool lef = f(a,l) < f(s[idx],l);
if (m) swap(s[idx],a);
if (r-l==1) return;
else if (lef!=m) add(a,l,mid,2*idx);
else add(a,mid,r,2*idx+1);
}
ll query(int l, int r, int idx, int x) {
int m=(l+r)/2;
if (r-l==1) return f(s[idx],x);
else if (x<m) return min(f(s[idx],x),query(l,m,2*idx,x));
else return min(f(s[idx],x),query(m,r,2*idx+1,x));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=n; ++i) cin>>h[i];
for (int i=1; i<=n; ++i) cin>>w[i], sum+=w[i];
dp[1]=-w[1];
for (int i=0; i<(1<<22); ++i) s[i]={-2*h[1],dp[1]+h[1]*h[1]};
for (int i=2; i<=n; ++i) {
dp[i]=query(0,mx,1,h[i])+h[i]*h[i]-w[i];
add({-2*h[i],dp[i]+h[i]*h[i]},0,mx,1);
}
cout<<dp[n]+sum;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
66004 KB |
Output is correct |
2 |
Correct |
27 ms |
65884 KB |
Output is correct |
3 |
Correct |
26 ms |
65868 KB |
Output is correct |
4 |
Correct |
27 ms |
65996 KB |
Output is correct |
5 |
Correct |
26 ms |
65956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
69352 KB |
Output is correct |
2 |
Correct |
68 ms |
69292 KB |
Output is correct |
3 |
Correct |
66 ms |
69324 KB |
Output is correct |
4 |
Correct |
62 ms |
69128 KB |
Output is correct |
5 |
Correct |
61 ms |
69176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
66004 KB |
Output is correct |
2 |
Correct |
27 ms |
65884 KB |
Output is correct |
3 |
Correct |
26 ms |
65868 KB |
Output is correct |
4 |
Correct |
27 ms |
65996 KB |
Output is correct |
5 |
Correct |
26 ms |
65956 KB |
Output is correct |
6 |
Correct |
75 ms |
69352 KB |
Output is correct |
7 |
Correct |
68 ms |
69292 KB |
Output is correct |
8 |
Correct |
66 ms |
69324 KB |
Output is correct |
9 |
Correct |
62 ms |
69128 KB |
Output is correct |
10 |
Correct |
61 ms |
69176 KB |
Output is correct |
11 |
Correct |
78 ms |
69492 KB |
Output is correct |
12 |
Correct |
70 ms |
69232 KB |
Output is correct |
13 |
Correct |
64 ms |
69324 KB |
Output is correct |
14 |
Correct |
71 ms |
69448 KB |
Output is correct |
15 |
Correct |
59 ms |
69172 KB |
Output is correct |
16 |
Correct |
60 ms |
69184 KB |
Output is correct |
17 |
Correct |
54 ms |
69324 KB |
Output is correct |
18 |
Correct |
55 ms |
69308 KB |
Output is correct |