#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const long long inf = 1e18;
struct line {
mutable long long a, b, p;
bool operator < (line o) const {
return a < o.a;
}
bool operator < (long long x) const {
return p < x;
}
};
struct lc : multiset<line, less<>> {
long long div(long long a, long long b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) {
return x -> p = inf, 0;
}
if (x -> a == y -> a) {
x -> p = x -> b >= y -> b ? inf : -inf;
} else {
x -> p = div(y -> b - x -> b, x -> a - y -> a);
}
return x -> p >= y -> p;
}
void add(long long a, long long b) {
auto z = insert({a, b, 0}), y = z++, x = y;
while (isect(y, z)) {
z = erase(z);
}
if (x != begin() && isect(--x, y)) {
isect(x, erase(y));
}
while ((y = x) != begin() && (--x) -> p >= y -> p) {
isect(x, erase(y));
}
}
long long qry(long long x) {
auto l = *lower_bound(x);
return l.a * x + l.b;
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
vector<int> h(n);
for (int &x : h) {
cin >> x;
}
lc lc;
long long sum = 0;
for (int i = 0; i < n; ++i) {
int w; cin >> w;
long long x = lc.size() ? -lc.qry(h[i]) + (long long) h[i] * h[i] + sum : 0;
sum += w;
if (i == n - 1) {
cout << x;
} else {
lc.add(2 * h[i], sum - (long long) h[i] * h[i] - x);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
1752 KB |
Output is correct |
2 |
Correct |
27 ms |
1884 KB |
Output is correct |
3 |
Correct |
28 ms |
1884 KB |
Output is correct |
4 |
Correct |
29 ms |
1884 KB |
Output is correct |
5 |
Correct |
24 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
28 ms |
1752 KB |
Output is correct |
7 |
Correct |
27 ms |
1884 KB |
Output is correct |
8 |
Correct |
28 ms |
1884 KB |
Output is correct |
9 |
Correct |
29 ms |
1884 KB |
Output is correct |
10 |
Correct |
24 ms |
2904 KB |
Output is correct |
11 |
Correct |
33 ms |
2160 KB |
Output is correct |
12 |
Correct |
27 ms |
1880 KB |
Output is correct |
13 |
Correct |
21 ms |
1880 KB |
Output is correct |
14 |
Correct |
27 ms |
1884 KB |
Output is correct |
15 |
Correct |
44 ms |
7744 KB |
Output is correct |
16 |
Correct |
24 ms |
2788 KB |
Output is correct |
17 |
Correct |
13 ms |
1884 KB |
Output is correct |
18 |
Correct |
13 ms |
1884 KB |
Output is correct |