#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define int long long
const int inf = 1e18;
const int maxn = 1e6 + 1;
struct line {
int k, b;
line() {}
line(int k, int b): k(k), b(b) {}
int get(int x) { return k * x + b; }
};
line tree[maxn * 4];
void init(line l) {
fill(tree, tree + maxn * 4, l);
}
int get(int i, int l, int r, int x) {
if (l + 1 == r) return tree[i].get(x);
int m = (l + r) / 2;
if (x < m) return min(tree[i].get(x), get(i * 2, l, m, x));
return min(tree[i].get(x), get(i * 2 + 1, m, r, x));
}
void upd(int i, int l, int r, line ln) {
int m = (l + r) / 2;
bool lt = ln.get(l) < tree[i].get(l);
bool md = ln.get(m) < tree[i].get(m);
if (md) {
swap(tree[i], ln);
}
if (l + 1 == r) return;
if (lt != md) {
upd(i * 2, l, m, ln);
} else {
upd(i * 2 + 1, m, r, ln);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> h(n);
for (int& i : h) cin >> i;
vector<int> w(n);
for (int& i : w) cin >> i;
vector<int> dp(n, inf);
dp[0] = 0;
vector<int> pref(n + 1, 0);
for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + w[i - 1];
init(line(-2 * h[0], h[0] * h[0]));
for (int i = 1; i < n; ++i) {
dp[i] = get(1, 0, maxn, h[i]) + h[i] * h[i] + pref[i];
upd(1, 0, maxn, line(-2 * h[i], dp[i] - pref[i + 1] + h[i] * h[i]));
}
cout << dp.back();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
62828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
66000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
62828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |