#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
const int mxn = 1e5 + 100;
vector<ll> h(mxn), val(mxn), prf(mxn);
ll sum(int l, int r) {
return prf[r] - (l == 0 ? 0 : prf[l - 1]);
}
struct Node {
ll m = 0, b = 1e16;
};
ll f(Node a, ll x) {
return -a.m * x + a.b;
}
struct LiChao {
vector<Node> sgt;
LiChao(int sz) {
sgt.resize(4 * sz);
}
void update(int k, int l, int r, Node nv) {
int mid = (l + r) / 2;
bool f1 = f(nv, l) < f(sgt[k], l);
bool f2 = f(nv, mid) < f(sgt[k], mid);
if (f2) swap(nv, sgt[k]);
if (l == r) return;
if (f1 != f2) update(k * 2, l, mid, nv);
else update(k * 2 + 1, mid + 1, r, nv);
}
ll get(int k, int l, int r, ll x) {
if (l > x || r < x) return 1e16;
ll ans = f(sgt[k], x);
if (l == r) return ans;
int mid = (l + r) / 2;
return min({ans, get(k * 2, l, mid, x), get(k * 2 + 1, mid + 1, r, x)});
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> h[i];
for (int i = 0; i < n; i++) cin >> val[i];
prf[0] = val[0];
for (int i = 1; i < n; i++) prf[i] = prf[i - 1] + val[i];
LiChao lc(2e6 + 100);
lc.update(1, 0, 2e6, {2 * h[0], h[0] * h[0] - val[0]});
ll ans[n];
for (int i = 1; i < n; i++) {
ll best = lc.get(1, 0, 2e6, h[i]) - val[i] + h[i] * h[i];
ans[i] = best;
lc.update(1, 0, 2e6, {2 * h[i], best + h[i] * h[i]});
}
cout << ans[n - 1] + sum(0, n - 1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
127824 KB |
Output is correct |
2 |
Correct |
46 ms |
127824 KB |
Output is correct |
3 |
Correct |
45 ms |
127828 KB |
Output is correct |
4 |
Correct |
47 ms |
127824 KB |
Output is correct |
5 |
Correct |
45 ms |
127824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
129796 KB |
Output is correct |
2 |
Correct |
78 ms |
129656 KB |
Output is correct |
3 |
Correct |
78 ms |
129616 KB |
Output is correct |
4 |
Correct |
83 ms |
129652 KB |
Output is correct |
5 |
Correct |
75 ms |
129736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
127824 KB |
Output is correct |
2 |
Correct |
46 ms |
127824 KB |
Output is correct |
3 |
Correct |
45 ms |
127828 KB |
Output is correct |
4 |
Correct |
47 ms |
127824 KB |
Output is correct |
5 |
Correct |
45 ms |
127824 KB |
Output is correct |
6 |
Correct |
79 ms |
129796 KB |
Output is correct |
7 |
Correct |
78 ms |
129656 KB |
Output is correct |
8 |
Correct |
78 ms |
129616 KB |
Output is correct |
9 |
Correct |
83 ms |
129652 KB |
Output is correct |
10 |
Correct |
75 ms |
129736 KB |
Output is correct |
11 |
Correct |
88 ms |
129868 KB |
Output is correct |
12 |
Correct |
82 ms |
129616 KB |
Output is correct |
13 |
Correct |
95 ms |
129876 KB |
Output is correct |
14 |
Correct |
85 ms |
129904 KB |
Output is correct |
15 |
Correct |
77 ms |
129648 KB |
Output is correct |
16 |
Correct |
75 ms |
129764 KB |
Output is correct |
17 |
Correct |
73 ms |
129772 KB |
Output is correct |
18 |
Correct |
71 ms |
129872 KB |
Output is correct |