#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
struct Line{
int m, k;
int val(int x){
return m * x + k;
}
};
Line node[4000050];
void modify(int i, int l, int r, Line L){
int mid = (l + r) / 2;
if(node[i].val(mid) > L.val(mid)) swap(L, node[i]);
if(l == r) return;
if(L.m == node[i].m) L = node[i];
if(L.m > node[i].m) modify(2 * i, l, mid, L);
else modify(2 * i + 1, mid + 1, r, L);
}
int query(int i, int l, int r, int pos){
int ret = node[i].val(pos);
if(l == r) return ret;
int mid = (l + r) / 2;
if(pos <= mid) return min(ret, query(2 * i, l, mid, pos));
else return min(ret, query(2 * i + 1, mid + 1, r, pos));
}
void solve(){
int n; cin >> n;
vector<int> h(n + 1), w(n + 1), dp(n + 1), S(n + 1);
for(int i = 1; i <= n; ++i) cin >> h[i];
for(int i = 1; i <= n; ++i) cin >> w[i], S[i] = S[i - 1] + w[i];
for(auto &L:node){
L.m = -2 * h[1];
L.k = h[1] * h[1] - S[1];
}
for(int i = 2; i <= n; ++i){
dp[i] = query(1, 0, 1000000, h[i]) + h[i] * h[i] + S[i] - w[i];
Line L;
L.m = -2 * h[i], L.k = h[i] * h[i] + dp[i] - S[i];
modify(1, 0, 1000000, L);
}
cout << dp[n] << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1; //cin >> t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62924 KB |
Output is correct |
2 |
Correct |
28 ms |
62924 KB |
Output is correct |
3 |
Correct |
27 ms |
62924 KB |
Output is correct |
4 |
Correct |
28 ms |
62916 KB |
Output is correct |
5 |
Correct |
28 ms |
62856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
66996 KB |
Output is correct |
2 |
Correct |
68 ms |
67016 KB |
Output is correct |
3 |
Correct |
69 ms |
67072 KB |
Output is correct |
4 |
Correct |
68 ms |
66956 KB |
Output is correct |
5 |
Correct |
59 ms |
67020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62924 KB |
Output is correct |
2 |
Correct |
28 ms |
62924 KB |
Output is correct |
3 |
Correct |
27 ms |
62924 KB |
Output is correct |
4 |
Correct |
28 ms |
62916 KB |
Output is correct |
5 |
Correct |
28 ms |
62856 KB |
Output is correct |
6 |
Correct |
70 ms |
66996 KB |
Output is correct |
7 |
Correct |
68 ms |
67016 KB |
Output is correct |
8 |
Correct |
69 ms |
67072 KB |
Output is correct |
9 |
Correct |
68 ms |
66956 KB |
Output is correct |
10 |
Correct |
59 ms |
67020 KB |
Output is correct |
11 |
Correct |
74 ms |
67204 KB |
Output is correct |
12 |
Correct |
74 ms |
67068 KB |
Output is correct |
13 |
Correct |
67 ms |
67196 KB |
Output is correct |
14 |
Correct |
78 ms |
67252 KB |
Output is correct |
15 |
Correct |
59 ms |
66844 KB |
Output is correct |
16 |
Correct |
59 ms |
67128 KB |
Output is correct |
17 |
Correct |
52 ms |
67148 KB |
Output is correct |
18 |
Correct |
58 ms |
67148 KB |
Output is correct |