#include <bits/stdc++.h>
using namespace std;
const int N = 100'000 + 10;
int n;
int h[N], w[N];
int d[N];
struct Line {
long long a, b;
Line() : a(0), b(1'000'000'000'000) {}
Line(long long a, long long b) : a(a), b(b) {}
double x(const auto& rhs) { return 1.0 * (b - rhs.b) / (rhs.a - a); }
long long cal(long long x) { return a * x + b; }
} f[N << 2];
vector<int> rrh;
void insert(int s, int l, int r, Line line) {
if (l == r) {
if (f[s].cal(l - 1) > line.cal(l - 1)) f[s] = line;
return;
}
int mid = l + r >> 1;
if (f[s].a > line.a) swap(f[s], line);
if (f[s].cal(rrh[mid - 1]) <= line.cal(rrh[mid - 1])) insert(s << 1, l, mid, line);
else {
swap(f[s], line);
insert(s << 1 | 1, mid + 1, r, line);
}
}
long long query(int s, int l, int r, int i) {
if (i < l || i > r) return 1'000'000'000'000'000;
if (l == r) return f[s].cal(rrh[i - 1]);
int mid = l + r >> 1;
if (i <= mid) return min(f[s].cal(rrh[i - 1]), query(s << 1, l, mid, i));
return min(f[s].cal(rrh[i - 1]), query(s << 1 | 1, mid + 1, r, i));
}
long long dp[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= n; ++i) cin >> w[i];
for (int i = 1; i <= n; ++i) d[i] = d[i - 1] + w[i];
rrh = vector<int>(h + 1, h + n + 1);
sort(rrh.begin(), rrh.end());
rrh.resize(unique(rrh.begin(), rrh.end()) - rrh.begin());
insert(1, 1, rrh.size(), {-2 * h[1], 1ll * h[1] * h[1] - d[1]});
for (int i = 2; i <= n; ++i) {
int it = lower_bound(rrh.begin(), rrh.end(), h[i]) - rrh.begin() + 1;
dp[i] = query(1, 1, rrh.size(), it) + 1ll * h[i] * h[i] + d[i - 1];
insert(1, 1, rrh.size(), {-2 * h[i], 1ll * h[i] * h[i] - d[i] + dp[i]});
}
cout << dp[n] << "\n";
}
Compilation message
building.cpp:16:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
16 | double x(const auto& rhs) { return 1.0 * (b - rhs.b) / (rhs.a - a); }
| ^~~~
building.cpp: In function 'void insert(int, int, int, Line)':
building.cpp:26:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid = l + r >> 1;
| ~~^~~
building.cpp: In function 'long long int query(int, int, int, int)':
building.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7332 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
10068 KB |
Output is correct |
2 |
Correct |
47 ms |
10068 KB |
Output is correct |
3 |
Correct |
48 ms |
10012 KB |
Output is correct |
4 |
Correct |
40 ms |
9820 KB |
Output is correct |
5 |
Correct |
31 ms |
9976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7260 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
2 ms |
7332 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
47 ms |
10068 KB |
Output is correct |
7 |
Correct |
47 ms |
10068 KB |
Output is correct |
8 |
Correct |
48 ms |
10012 KB |
Output is correct |
9 |
Correct |
40 ms |
9820 KB |
Output is correct |
10 |
Correct |
31 ms |
9976 KB |
Output is correct |
11 |
Correct |
55 ms |
10076 KB |
Output is correct |
12 |
Correct |
57 ms |
9896 KB |
Output is correct |
13 |
Correct |
38 ms |
10152 KB |
Output is correct |
14 |
Correct |
60 ms |
10832 KB |
Output is correct |
15 |
Correct |
42 ms |
9812 KB |
Output is correct |
16 |
Correct |
32 ms |
9820 KB |
Output is correct |
17 |
Incorrect |
17 ms |
10100 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |