#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pll = pair<lint, lint>;
const int MAX_N = 1e5 + 5;
const lint MAX_H = 1e6 + 5, INF = 2e18;
int n;
lint h[MAX_N], w[MAX_N];
pll lct[60 * MAX_H];
int n_nodes, l_child[60 * MAX_H], r_child[60 * MAX_H];
void propogate(int u) {
if (l_child[u]) return;
n_nodes++, l_child[u] = n_nodes, lct[n_nodes] = {0, INF};
n_nodes++, r_child[u] = n_nodes, lct[n_nodes] = {0, INF};
}
lint eval(pll x, lint y) {
return x.first * y + x.second;
}
void update(pll x, int u = 1, lint lo = 0, lint hi = MAX_H) {
lint mid = (lo + hi) / 2;
if (eval(x, mid) < eval(lct[u], mid)) swap(x, lct[u]);
if (lo == hi) return;
propogate(u);
if (eval(x, lo) < eval(lct[u], lo)) update(x, l_child[u], lo, mid);
else update(x, r_child[u], mid + 1, hi);
}
lint query(lint i, int u = 1, lint lo = 0, lint hi = MAX_H) {
if (i < lo || i > hi) return INF;
if (lo == hi) return eval(lct[u], lo);
propogate(u);
lint mid = (lo + hi) / 2;
return min({eval(lct[u], i), query(i, l_child[u], lo, mid), query(i, r_child[u], mid + 1, hi)});
}
void init() {
n_nodes = 1;
lct[1] = {0, INF};
}
lint dp[MAX_N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
init();
dp[1] = -w[1];
update({-2 * h[1], dp[1] + h[1] * h[1]});
for (int i = 2; i <= n; i++) {
dp[i] = query(h[i]) + h[i] * h[i] - w[i];
update({-2 * h[i], dp[i] + h[i] * h[i]});
}
lint ans = dp[n];
for (int i = 1; i <= n; i++) ans += w[i];
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
9240 KB |
Output is correct |
2 |
Correct |
67 ms |
9296 KB |
Output is correct |
3 |
Correct |
66 ms |
9292 KB |
Output is correct |
4 |
Correct |
63 ms |
9040 KB |
Output is correct |
5 |
Correct |
64 ms |
14292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
66 ms |
9240 KB |
Output is correct |
7 |
Correct |
67 ms |
9296 KB |
Output is correct |
8 |
Correct |
66 ms |
9292 KB |
Output is correct |
9 |
Correct |
63 ms |
9040 KB |
Output is correct |
10 |
Correct |
64 ms |
14292 KB |
Output is correct |
11 |
Correct |
76 ms |
12136 KB |
Output is correct |
12 |
Correct |
71 ms |
11856 KB |
Output is correct |
13 |
Correct |
63 ms |
9344 KB |
Output is correct |
14 |
Correct |
91 ms |
12144 KB |
Output is correct |
15 |
Correct |
61 ms |
18632 KB |
Output is correct |
16 |
Correct |
61 ms |
14236 KB |
Output is correct |
17 |
Correct |
57 ms |
9044 KB |
Output is correct |
18 |
Correct |
65 ms |
9292 KB |
Output is correct |