#include <bits/stdc++.h>
using namespace std;
typedef complex<long long> point;
#define ll long long
#define x real
#define y imag
const int MAXN = 2e5;
const int MAXH = 2e6;
const long long INF = 4e18;
long long dp[MAXN];
struct Line {
ll a, b;
ll operator()(ll x) {
return a*x+b;
}
};
struct Node {
Line seg;
Node *left = NULL, *right = NULL;
Node(Line x): seg(x) {};
};
void insert(int l, int r, Line seg, Node *nd) {
int m = (l+r)/2;
bool left = nd->seg(l) > seg(l);
bool mid = nd->seg(m) > seg(m);
if (mid) swap(seg, nd->seg);
if (r-l == 1) {
return;
} else if (left != mid) {
if (nd->left) insert(l, m, seg, nd->left);
else nd->left = new Node(seg);
} else {
if (nd->right) insert(m, r, seg, nd->right);
else nd->right = new Node(seg);
}
}
ll query(int l, int r, ll x, Node *nd) {
int m = (l+r)/2;
if (r-l == 1) {
return nd->seg(x);
} else if (x < m && nd->left) {
return min(nd->seg(x), query(l, m, x, nd->left));
} else if (nd->right) {
return min(nd->seg(x), query(m, r, x, nd->right));
} else {
return nd->seg(x);
}
}
int main() {
int n;
cin>>n;
long long h[n], w[n];
for (int i=0; i<n; ++i) cin>>h[i];
for (int i=0; i<n; ++i) cin>>w[i];
long long suf[n+1];
suf[n] = 0;
for (int i=n-1; i>=0; --i) suf[i] = suf[i+1]+w[i];
Node *root = new Node{{0, INF}};
dp[0] = 0;
insert(-MAXH, MAXH, Line{-2*h[0], h[0]*h[0]+suf[1]}, root);
for (int i=1; i<n; ++i) {
dp[i] = query(-MAXH, MAXH, h[i], root) + h[i]*h[i] - suf[i];
insert(-MAXH, MAXH, Line{-2*h[i], h[i]*h[i]+suf[i+1]+dp[i]}, root);
}
cout<<dp[n-1]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
1 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 |
63 ms |
4992 KB |
Output is correct |
2 |
Correct |
69 ms |
5060 KB |
Output is correct |
3 |
Correct |
63 ms |
5200 KB |
Output is correct |
4 |
Correct |
60 ms |
4436 KB |
Output is correct |
5 |
Correct |
51 ms |
7764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
440 KB |
Output is correct |
3 |
Correct |
1 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 |
63 ms |
4992 KB |
Output is correct |
7 |
Correct |
69 ms |
5060 KB |
Output is correct |
8 |
Correct |
63 ms |
5200 KB |
Output is correct |
9 |
Correct |
60 ms |
4436 KB |
Output is correct |
10 |
Correct |
51 ms |
7764 KB |
Output is correct |
11 |
Correct |
71 ms |
5560 KB |
Output is correct |
12 |
Correct |
68 ms |
5968 KB |
Output is correct |
13 |
Correct |
60 ms |
4944 KB |
Output is correct |
14 |
Correct |
71 ms |
6072 KB |
Output is correct |
15 |
Correct |
65 ms |
8888 KB |
Output is correct |
16 |
Correct |
53 ms |
7796 KB |
Output is correct |
17 |
Correct |
49 ms |
4536 KB |
Output is correct |
18 |
Correct |
52 ms |
4436 KB |
Output is correct |