#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vf first
#define vs second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
struct Line {
ll m = 0 , c = 1e18;
};
ll value(Line a , int x){
return a.m * x + a.c;
}
struct Lichao {
vector<Line> v;
int siz;
void init(int n){
siz = 1;
while(siz < n){
siz *= 2;
}
v.resize(siz * 2);
}
void set(Line l, int x , int lx , int rx){
if(rx - lx == 0){
if(value(l,lx) < value(v[x],lx)){
v[x] = l;
}
return;
}
int m = (lx + rx)/2;
if(v[x].m < l.m)swap(v[x],l);
if(value(l,m) < value(v[x] , m)){
swap(l,v[x]);
set(l , 2 * x , lx , m);
}
else {
set(l , 2 * x + 1 , m + 1 ,rx);
}
}
ll query(int x_val , int x , int lx, int rx){
ll ans = value(v[x],x_val);
if(rx == lx)return ans;
int mid = (lx + rx)/2;
if(x_val <= mid){
ans = min(ans , query(x_val , 2 *x, lx,mid));
}
else ans = min(ans , query(x_val, 2 * x + 1 , mid + 1 ,rx));
return ans;
}
};
void solve(){
int n;
cin >> n;
ll h[n];
for(int i = 0; i < n; i++){
cin >> h[i];
}
ll w[n];
for(int i = 0; i < n; i++){
cin >> w[i];
}
ll dp[n];
Lichao li;
li.init(2000005);
dp[0] = -w[0];
li.set({-2*h[0] , h[0]*h[0] - w[0]},1,0,li.siz-1);
ll ans = 0;
for(int i = 0; i < n; i++){
ans += w[i];
}
for(int i = 1; i < n; i++){
dp[i] = li.query(h[i],1,0,li.siz-1) + h[i] * h[i] - w[i];
li.set({-2*h[i] , h[i]*h[i] +dp[i]},1,0,li.siz-1);
}
cout << dp[n-1] + ans << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;
//cin >> t;
// while(t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
65884 KB |
Output is correct |
2 |
Correct |
9 ms |
65972 KB |
Output is correct |
3 |
Correct |
10 ms |
66040 KB |
Output is correct |
4 |
Correct |
10 ms |
66140 KB |
Output is correct |
5 |
Correct |
10 ms |
66136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
68436 KB |
Output is correct |
2 |
Correct |
46 ms |
68296 KB |
Output is correct |
3 |
Correct |
50 ms |
68432 KB |
Output is correct |
4 |
Correct |
38 ms |
68444 KB |
Output is correct |
5 |
Correct |
38 ms |
68444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
65884 KB |
Output is correct |
2 |
Correct |
9 ms |
65972 KB |
Output is correct |
3 |
Correct |
10 ms |
66040 KB |
Output is correct |
4 |
Correct |
10 ms |
66140 KB |
Output is correct |
5 |
Correct |
10 ms |
66136 KB |
Output is correct |
6 |
Correct |
42 ms |
68436 KB |
Output is correct |
7 |
Correct |
46 ms |
68296 KB |
Output is correct |
8 |
Correct |
50 ms |
68432 KB |
Output is correct |
9 |
Correct |
38 ms |
68444 KB |
Output is correct |
10 |
Correct |
38 ms |
68444 KB |
Output is correct |
11 |
Correct |
48 ms |
69456 KB |
Output is correct |
12 |
Correct |
52 ms |
69464 KB |
Output is correct |
13 |
Correct |
41 ms |
69456 KB |
Output is correct |
14 |
Correct |
49 ms |
69456 KB |
Output is correct |
15 |
Correct |
36 ms |
69200 KB |
Output is correct |
16 |
Correct |
36 ms |
69212 KB |
Output is correct |
17 |
Correct |
32 ms |
69460 KB |
Output is correct |
18 |
Correct |
40 ms |
69464 KB |
Output is correct |