#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t z;
const int mxn = 2e5+5;
int n;
ll h[mxn];
ll w[mxn];
ll dp[mxn];
ll pref[mxn];
ll p(ll a){
return a*a;
}
const ll is_query = -(1LL<<62);
struct Line {
ll m, b;
mutable function<const Line*()> succ;
bool operator<(const Line& rhs) const {
if (rhs.b != is_query) return m < rhs.m;
const Line* s = succ();
if (!s) return 0;
ll x = rhs.m;
return b - s->b < (s->m - m) * x;
}
};
struct HullDynamic : public multiset<Line> {
bool bad(iterator y) {
auto z = next(y);
if (y == begin()) {
if (z == end()) return 0;
return y->m == z->m && y->b <= z->b;
}
auto x = prev(y);
if (z == end()) return y->m == x->m && y->b <= x->b;
return 1.0 * (x->b - y->b)*(z->m - y->m) >= 1.0 * (y->b - z->b)*(y->m - x->m);
}
void insert_line(ll m, ll b) {
auto y = insert({ m, b });
y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
if (bad(y)) { erase(y); return; }
while (next(y) != end() && bad(next(y))) erase(next(y));
while (y != begin() && bad(prev(y))) erase(prev(y));
}
ll eval(ll x) {
auto l = *lower_bound((Line) { x, is_query });
return l.m * x + l.b;
}
}hull;
void solve(){
cin >> n;
fr(i, 0, n){
cin >> h[i];
}
fr(i, 0, n){
cin >> w[i];
}
pref[0] = w[0];
fr(i, 1, n){
pref[i] = pref[i-1]+w[i];
}
dp[0] = 0;
hull.insert_line(2*h[0], -(dp[0] + p(h[0])-pref[0]));
fr(i, 1, n){
dp[i] = -hull.eval(h[i]) + p(h[i]) + pref[i] - w[i];
hull.insert_line(2*h[i], -(dp[i] + p(h[i]) - pref[i]));
}
cout<<dp[n-1]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
3584 KB |
Output is correct |
2 |
Correct |
76 ms |
3540 KB |
Output is correct |
3 |
Correct |
75 ms |
3476 KB |
Output is correct |
4 |
Correct |
66 ms |
3428 KB |
Output is correct |
5 |
Correct |
101 ms |
5208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
76 ms |
3584 KB |
Output is correct |
7 |
Correct |
76 ms |
3540 KB |
Output is correct |
8 |
Correct |
75 ms |
3476 KB |
Output is correct |
9 |
Correct |
66 ms |
3428 KB |
Output is correct |
10 |
Correct |
101 ms |
5208 KB |
Output is correct |
11 |
Correct |
71 ms |
4652 KB |
Output is correct |
12 |
Correct |
86 ms |
4540 KB |
Output is correct |
13 |
Correct |
46 ms |
4460 KB |
Output is correct |
14 |
Correct |
74 ms |
4680 KB |
Output is correct |
15 |
Correct |
164 ms |
13716 KB |
Output is correct |
16 |
Correct |
102 ms |
6284 KB |
Output is correct |
17 |
Correct |
29 ms |
4420 KB |
Output is correct |
18 |
Correct |
28 ms |
4408 KB |
Output is correct |