#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(x, v) for (auto &x : v)
using ll = long long;
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
const ll inf = 1e18;
ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return false; }
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<ll> f(n), h(n), w(n);
LineContainer q;
trav(x, h) cin >> x;
trav(x, w) cin >> x;
rep(i, 1, n) w[i] += w[i - 1];
q.add(2 * h[0], - f[0] - h[0] * h[0] + w[0]);
rep(i, 1, n) {
f[i] = h[i] * h[i] + w[i - 1] - q.query(h[i]);
q.add(2 * h[i], - f[i] - h[i] * h[i] + w[i]);
}
cout << f[n - 1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
3840 KB |
Output is correct |
2 |
Correct |
52 ms |
3780 KB |
Output is correct |
3 |
Correct |
53 ms |
3832 KB |
Output is correct |
4 |
Correct |
46 ms |
3712 KB |
Output is correct |
5 |
Correct |
43 ms |
4864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
54 ms |
3840 KB |
Output is correct |
7 |
Correct |
52 ms |
3780 KB |
Output is correct |
8 |
Correct |
53 ms |
3832 KB |
Output is correct |
9 |
Correct |
46 ms |
3712 KB |
Output is correct |
10 |
Correct |
43 ms |
4864 KB |
Output is correct |
11 |
Correct |
55 ms |
4088 KB |
Output is correct |
12 |
Correct |
53 ms |
3832 KB |
Output is correct |
13 |
Correct |
42 ms |
3840 KB |
Output is correct |
14 |
Correct |
52 ms |
3968 KB |
Output is correct |
15 |
Correct |
72 ms |
9868 KB |
Output is correct |
16 |
Correct |
45 ms |
4856 KB |
Output is correct |
17 |
Correct |
33 ms |
3840 KB |
Output is correct |
18 |
Correct |
30 ms |
3840 KB |
Output is correct |