#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const ll INF = 1e18;
struct Line {
ll a;
int b;
ll eval(int x) {
return a * x + b;
}
};
namespace LiChao {
struct Node {
Line val;
Node* left;
Node* right;
Node(Line val_ = {0, 0}) {
val = val_;
left = right = NULL;
}
};
void update(Node* &root, int from, int to, Line val) {
if (root == NULL) {
root = new Node(val);
} else {
int mid = (from + to) / 2;
if (val.eval(mid) < root->val.eval(mid)) {
swap(val, root->val);
}
if (val.eval(from) < root->val.eval(from)) {
update(root->left, from, mid, val);
}
if (val.eval(to) < root->val.eval(to)) {
update(root->right, mid + 1, to, val);
}
}
}
ll query(Node* root, int from, int to, int x) {
if (root == NULL) {
return INF;
} else {
int mid = (from + to) / 2;
if (x <= mid) {
return min(query(root->left, from, mid, x), root->val.eval(x));
} else {
return min(query(root->right, mid + 1, to, x), root->val.eval(x));
}
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> h(n + 1), w(n + 1);
for (int i = 1; i <= n; i += 1) {
cin >> h[i];
}
vector<ll> pref(n + 1, 0);
for (int i = 1; i <= n; i += 1) {
cin >> w[i];
pref[i] = pref[i - 1] + w[i];
}
vector<ll> dp(n + 1, INF);
LiChao::Node* root = NULL;
dp[1] = 0;
for (int i = 1; i <= n; i += 1) {
if (i != 1) {
dp[i] = LiChao::query(root, 1, 1e6, h[i]);
dp[i] += pref[i - 1] + h[i] * h[i];
}
int a = -2 * h[i];
int b = dp[i] + h[i] * h[i] - pref[i];
LiChao::update(root, 1, 1e6, {a, b});
}
cout << dp[n] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
2836 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
17 ms |
2836 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |