#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e6 + 5;
const int INF = 4e18;
int n;
int h[maxn], w[maxn];
struct LiChaoTree{
struct line{
int m, k;
line(){}
line(int _m, int _k) : m(_m), k(_k){}
int operator()(int x){
return m * x + k;
}
};
struct node{
node *l, *r;
line ans;
node() : l(nullptr), r(nullptr), ans(line(0, INF)){}
node(node *nd) : l(nd->l), r(nd->r), ans(nd->ans){}
node(line _ans) : l(nullptr), r(nullptr), ans(_ans){}
} *rt;
void init(){ rt = new node();}
void upd(line cur, node *&nd, int l, int r){
if(!nd){
nd = new node(cur);
return;
}
if(l == r){
if(cur(l) < nd->ans(l)) nd->ans = cur;
return;
}
int m = (l + r) / 2;
if(cur(m) < nd->ans(m)) swap(cur, nd->ans);
if(cur.m < nd->ans.m) upd(cur, nd->r, m + 1, r);
else upd(cur, nd->l, l, m);
}
void upd(line cur){ upd(cur, rt, 0, maxn - 1);}
void upd(int m, int k){ upd(line(m, k), rt, 0, maxn - 1);}
int qry(int pos, node *nd, int l, int r){
if(!nd) return INF;
if(l == r) return nd->ans(pos);
int m = (l + r) / 2;
if(pos <= m) return min(nd->ans(pos), qry(pos, nd->l, l, m));
else return min(nd->ans(pos), qry(pos, nd->r, m + 1, r));
}
int qry(int val){ return qry(val, rt, 0, maxn - 1);}
} tree;
int dp[maxn];
signed main(void){
fastio;
cin>>n;
for(int i = 0; i < n; i++) cin>>h[i];
for(int i = 0; i < n; i++) cin>>w[i];
tree.init();
tree.upd(-2 * h[0], h[0] * h[0] - w[0]);
for(int i = 1; i < n; i++){
w[i] += w[i - 1];
dp[i] = tree.qry(h[i]) + w[i - 1] + h[i] * h[i];
tree.upd(-2 * h[i], dp[i] + h[i] * h[i] - w[i]);
}
cout<<dp[n - 1]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
10408 KB |
Output is correct |
2 |
Correct |
27 ms |
10572 KB |
Output is correct |
3 |
Correct |
28 ms |
10660 KB |
Output is correct |
4 |
Correct |
24 ms |
9996 KB |
Output is correct |
5 |
Correct |
18 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
26 ms |
10408 KB |
Output is correct |
7 |
Correct |
27 ms |
10572 KB |
Output is correct |
8 |
Correct |
28 ms |
10660 KB |
Output is correct |
9 |
Correct |
24 ms |
9996 KB |
Output is correct |
10 |
Correct |
18 ms |
13404 KB |
Output is correct |
11 |
Correct |
36 ms |
14164 KB |
Output is correct |
12 |
Correct |
34 ms |
13904 KB |
Output is correct |
13 |
Correct |
25 ms |
10580 KB |
Output is correct |
14 |
Correct |
34 ms |
13968 KB |
Output is correct |
15 |
Correct |
18 ms |
14424 KB |
Output is correct |
16 |
Correct |
18 ms |
13404 KB |
Output is correct |
17 |
Correct |
14 ms |
9796 KB |
Output is correct |
18 |
Correct |
20 ms |
9800 KB |
Output is correct |