#include <bits/stdc++.h>
using namespace std;
struct Line {
long long a, b;
Line() : a(0), b(LLONG_MAX) {}
Line(long long a, long long b) : a(a), b(b) {}
long long calc(long long x) {
return a * x + b;
}
};
struct LiChao {
vector<Line> lc;
void init(int n) {
lc.resize(n * 4, Line());
}
void upd(int id, int l, int r, Line L) {
int mid = (l + r) >> 1;
bool left = L.calc(l) < lc[id].calc(l);
bool m = L.calc(mid) < lc[id].calc(mid);
if (m) {
swap(lc[id], L);
}
if (l == r) return;
if (left != m) {
upd(id * 2, l, mid, L);
} else {
upd(id * 2 + 1, mid + 1, r, L);
}
}
long long get(int id, int l, int r, int x) {
if (l == r) {
return lc[id].calc(x);
}
int mid = (l + r) >> 1;
if (x <= mid) {
return min(lc[id].calc(x), get(id * 2, l, mid, x));
}
return min(lc[id].calc(x), get(id * 2 + 1, mid + 1, r, x));
}
};
const int MAXN = 1e6 + 5;
LiChao T;
long long h[MAXN], w[MAXN];
long long dp[MAXN];
long long p1(int x) {
return -2 * h[x];
}
long long p2(int x) {
return dp[x] + h[x] * h[x] - w[x];
}
long long p3(int x) {
return h[x] * h[x] + w[x - 1];
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i <= n; i++) {
cin >> w[i];
w[i] += w[i - 1];
}
T.init(MAXN + 1);
dp[1] = 0;
T.upd(1, 0, MAXN, {p1(1), p2(1)});
for (int i = 2; i <= n; i++) {
dp[i] = T.get(1, 0, MAXN, h[i]) + p3(i);
T.upd(1, 0, MAXN, {p1(i), p2(i)});
}
cout << dp[n];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while (t--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |