#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int N = 1e5 + 5;
const int M = 1e6 + 5;
const int inf = 1e9 + 7;
struct node{
int m, b;
double operator * (node x){
return (b - x.b) * 1. / (x.m - m);
}
int get(int x){
return m * x + b;
}
};
struct LiChao{
node tree[M << 2];
LiChao(){
for(int i=0;i<(M << 2);i++){
tree[i] = {M, inf};
}
}
void add(node v, int lx = 0, int rx = M, int x = 1){
if(lx == rx){
if(v.get(lx) > tree[x].get(lx)) tree[x] = v;
return;
}
if(v.m == tree[x].m){
tree[x].b = min(tree[x].b, v.b);
return;
}
double is = tree[x] * v;
int m = (lx + rx) >> 1;
if(is < m + 1){
if(tree[x].get(m + 1) > v.get(m + 1)) swap(tree[x], v);
add(v, lx, m, x << 1);
} else {
if(tree[x].get(m) > v.get(m)) swap(tree[x], v);
add(v, m + 1, rx, x << 1 | 1);
}
}
int get(int i, int lx = 0, int rx = M, int x = 1){
if(lx == rx){
return tree[x].get(i);
}
int m = (lx + rx) >> 1;
if(i <= m) return min(get(i, lx, m, x << 1), tree[x].get(i));
else return min(get(i, m + 1, rx, x << 1 | 1), tree[x].get(i));
}
}tree;
int h[N], w[N], dp[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
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];
}
tree.add({-2 * h[1], h[1] * h[1] - w[1]});
for(int i=2;i<=n;i++){
dp[i] = tree.get(h[i]) + h[i] * h[i] + w[i-1];
tree.add({-2 * h[i], dp[i] + h[i] * h[i] - w[i]});
}
cout<<dp[n]<<"\n";
}
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
62916 KB |
Output is correct |
2 |
Correct |
24 ms |
62924 KB |
Output is correct |
3 |
Correct |
24 ms |
62900 KB |
Output is correct |
4 |
Correct |
26 ms |
62940 KB |
Output is correct |
5 |
Correct |
27 ms |
62932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
66212 KB |
Output is correct |
2 |
Correct |
66 ms |
66248 KB |
Output is correct |
3 |
Correct |
66 ms |
66268 KB |
Output is correct |
4 |
Correct |
60 ms |
66128 KB |
Output is correct |
5 |
Incorrect |
54 ms |
66124 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
62916 KB |
Output is correct |
2 |
Correct |
24 ms |
62924 KB |
Output is correct |
3 |
Correct |
24 ms |
62900 KB |
Output is correct |
4 |
Correct |
26 ms |
62940 KB |
Output is correct |
5 |
Correct |
27 ms |
62932 KB |
Output is correct |
6 |
Correct |
69 ms |
66212 KB |
Output is correct |
7 |
Correct |
66 ms |
66248 KB |
Output is correct |
8 |
Correct |
66 ms |
66268 KB |
Output is correct |
9 |
Correct |
60 ms |
66128 KB |
Output is correct |
10 |
Incorrect |
54 ms |
66124 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |