#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
typedef long long ll;
const ll mx = 1e6;
ll seg[4*mx+1], h[mx+1], w[mx+1], dp[mx+1];
void upd (ll u, ll bl, ll br, ll id) {
if (!seg[id]) {
seg[id] = u;
return;
}
ll mid = (bl+br)/2;
if (dp[u]-2*h[u]*mid<dp[seg[id]]-2*h[seg[id]]*mid) swap (seg[id], u);
if (bl==br) return;
if (-2*h[u]<-2*h[seg[id]]) upd (u, mid+1, br, 2*id+1);
else if (-2*h[u]>-2*h[seg[id]]) upd (u, bl, mid, 2*id);
}
ll qry (ll x, ll bl, ll br, ll id) {
if (!seg[id]) return 1e18;
if (bl==br) return dp[seg[id]] - 2*h[seg[id]]*x;
ll mid = (bl+br)/2;
if (x<=mid) return min(dp[seg[id]]-2*h[seg[id]]*x, qry(x, bl, mid, 2*id));
return min(dp[seg[id]]-2*h[seg[id]]*x, qry(x, mid+1, br, 2*id+1));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n, s = 0;
cin >> n;
rep (i,1,n) cin >> h[i];
rep (i,1,n) {
cin >> w[i];
s += w[i];
}
dp[1] = h[1]*h[1]-w[1];
upd (1, 0, mx, 1);
rep (i,2,n) {
dp[i] = qry (h[i], 0, mx, 1) + h[i]*h[i]-w[i];
if (i==n) cout << s+dp[n] << "\n";
else {
dp[i] += h[i]*h[i];
upd (i, 1, mx, 1);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
3580 KB |
Output is correct |
2 |
Correct |
42 ms |
3928 KB |
Output is correct |
3 |
Correct |
44 ms |
3800 KB |
Output is correct |
4 |
Correct |
51 ms |
3632 KB |
Output is correct |
5 |
Correct |
27 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
49 ms |
3580 KB |
Output is correct |
7 |
Correct |
42 ms |
3928 KB |
Output is correct |
8 |
Correct |
44 ms |
3800 KB |
Output is correct |
9 |
Correct |
51 ms |
3632 KB |
Output is correct |
10 |
Correct |
27 ms |
5196 KB |
Output is correct |
11 |
Correct |
49 ms |
5572 KB |
Output is correct |
12 |
Correct |
49 ms |
5436 KB |
Output is correct |
13 |
Correct |
44 ms |
4204 KB |
Output is correct |
14 |
Correct |
48 ms |
5612 KB |
Output is correct |
15 |
Correct |
29 ms |
5196 KB |
Output is correct |
16 |
Correct |
36 ms |
5196 KB |
Output is correct |
17 |
Correct |
15 ms |
3668 KB |
Output is correct |
18 |
Correct |
16 ms |
3688 KB |
Output is correct |