This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
return;
}
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 (st[node].second == -1) {
return (long long) -1e18;
}
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 (stderr)
building.cpp: In lambda function:
building.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l + r >> 1;
| ~~^~~
building.cpp: In lambda function:
building.cpp:59:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |