#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> w(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
vector<long long> pref(n);
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
}
pref[i] += w[i];
}
const int MAX = 1e6 + 5;
vector<pair<long long, long long>> st(4 * MAX, {-1, -1});
function<void(int, int, int, pair<long long, long long>)> modify = [&](int node, int l, int r, pair<long long, long long> line) {
if (st[node].first == -1) {
st[node] = line;
}
long long a = st[node].first * l + st[node].second;
long long b = line.first * l + line.second;
long long c = st[node].first * r + st[node].second;
long long d = line.first * r + line.second;
if (a >= b && c >= d) return;
if (a <= b && c <= d) {
st[node] = line;
return;
}
int mid = l + r >> 1;
if (a < b) {
swap(st[node], line);
}
if (st[node].first * mid + st[node].second >= line.first * mid + line.second) {
modify(node + node + 1, mid + 1, r, line);
} else {
swap(st[node], line);
modify(node + node, l, mid, line);
}
};
function<long long(int, int, int, int)> query = [&](int node, int l, int r, int i) {
if (l == r) {
return st[node].first * i + st[node].second;
}
int mid = l + r >> 1;
long long val = st[node].first * i + st[node].second;
if (i <= mid) {
val = max(val, query(node + node, l, mid, i));
} else {
val = max(val, query(node + node + 1, mid + 1, r, i));
}
return val;
};
modify(1, 0, MAX, {2 * h[0], -h[0] * 1LL * h[0] + w[0]});
vector<long long> dp(n);
for (int i = 1; i < n; i++) {
dp[i] = query(1, 0, MAX, h[i]) - h[i] * 1LL * h[i] - pref[i - 1];
modify(1, 0, MAX, {2 * h[i], -h[i] * 1LL * h[i] + pref[i] + dp[i]});
}
cout << -dp[n - 1] << '\n';
return 0;
}
Compilation message
building.cpp: In lambda function:
building.cpp:40:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid = l + r >> 1;
| ~~^~~
building.cpp: In lambda function:
building.cpp:55:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
62924 KB |
Output is correct |
2 |
Correct |
31 ms |
62828 KB |
Output is correct |
3 |
Correct |
27 ms |
62976 KB |
Output is correct |
4 |
Correct |
28 ms |
62940 KB |
Output is correct |
5 |
Incorrect |
27 ms |
62904 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
66316 KB |
Output is correct |
2 |
Correct |
95 ms |
66308 KB |
Output is correct |
3 |
Correct |
82 ms |
66244 KB |
Output is correct |
4 |
Correct |
79 ms |
66232 KB |
Output is correct |
5 |
Incorrect |
72 ms |
66164 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
62924 KB |
Output is correct |
2 |
Correct |
31 ms |
62828 KB |
Output is correct |
3 |
Correct |
27 ms |
62976 KB |
Output is correct |
4 |
Correct |
28 ms |
62940 KB |
Output is correct |
5 |
Incorrect |
27 ms |
62904 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |