#include <iostream>
#include <set>
using namespace std;
struct Line {
mutable long long k, m, p;
bool operator<(const Line& o) const {
return k < o.k;
}
bool operator<(long long x) const {
return p < x;
}
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const long long inf = 1000000000000000001;
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->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(long long k, long long 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));
}
}
long long query(long long x) {
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
long long h[100001];
long long w[100001];
long long dp[100001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> h[i];
}
for (int i = 1; i <= n; ++i) {
cin >> w[i];
w[i] += w[i-1];
}
LineContainer hull;
hull.add(h[1]*2, -1*h[1]*h[1]+w[1]);
for (int i=2; i<=n; ++i) {
dp[i] = w[i-1]+h[i]*h[i]-hull.query(h[i]);
hull.add(h[i]*2, -1*h[i]*h[i]+w[i]-dp[i]);
}
cout << dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
3860 KB |
Output is correct |
2 |
Correct |
55 ms |
3848 KB |
Output is correct |
3 |
Correct |
59 ms |
3732 KB |
Output is correct |
4 |
Correct |
51 ms |
3480 KB |
Output is correct |
5 |
Correct |
48 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
55 ms |
3860 KB |
Output is correct |
7 |
Correct |
55 ms |
3848 KB |
Output is correct |
8 |
Correct |
59 ms |
3732 KB |
Output is correct |
9 |
Correct |
51 ms |
3480 KB |
Output is correct |
10 |
Correct |
48 ms |
4692 KB |
Output is correct |
11 |
Correct |
56 ms |
3872 KB |
Output is correct |
12 |
Correct |
54 ms |
3668 KB |
Output is correct |
13 |
Correct |
56 ms |
3852 KB |
Output is correct |
14 |
Correct |
58 ms |
3920 KB |
Output is correct |
15 |
Correct |
60 ms |
9676 KB |
Output is correct |
16 |
Correct |
49 ms |
4692 KB |
Output is correct |
17 |
Correct |
43 ms |
3668 KB |
Output is correct |
18 |
Correct |
43 ms |
3676 KB |
Output is correct |