#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("boards.in");
ofstream fout("boards.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const ll mod = 1e9 + 7;
const int mxn = 2e5, mxm = 1e5;
vt<ll>comp;
struct Line{
ll m, c;
Line(){
m = 0; c = 1e17;
}
Line(ll _m, ll _c){
m = _m; c = _c;
}
ll val(ll x){return(x * m + c);}
};
int n;
Line st[4 * mxn + 1];
void upd(int nd, int l, int r, Line curr){
if(l == r){
if(st[nd].val(comp[l]) > curr.val(comp[l])){
swap(st[nd], curr);
}
return;
}
int mid = (l + r) >> 1;
if(st[nd].m > curr.m)swap(st[nd], curr);
if(st[nd].val(comp[mid]) > curr.val(comp[mid])){
swap(st[nd], curr);
upd(nd << 1 | 1, mid + 1, r, curr);
}else{
upd(nd << 1, l, mid, curr);
}
}
ll get(int nd, int l, int r, ll x){
if(l == r){
return(st[nd].val(x));
}
int mid = (l + r) >> 1;
if(x <= comp[mid]){
return(min(st[nd].val(x), get(nd << 1, l, mid, x)));
}else{
return(min(st[nd].val(x), get(nd << 1 | 1, mid + 1, r, x)));
}
}
int find(int x){
return(lower_bound(comp.begin(), comp.end(), x) - comp.begin());
}
ll dp[mxn + 1], w[mxn + 1], h[mxn + 1], p[mxn + 1];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
forr(i, 1, n + 1){
cin >> h[i]; comp.pb(h[i]);
}
sort(comp.begin(), comp.end());
forr(i, 1, n + 1){
cin >> w[i]; p[i] = p[i - 1] + w[i];
}
int sz = comp.size();
dp[1] = 0;
Line curr = Line(-2 * h[1], dp[1] + h[1] * h[1] - p[1]);
upd(1, 0, sz - 1, curr);
for(int i = 2; i <= n; i++){
dp[i] = get(1, 0, sz - 1, h[i]) + h[i] * h[i] + p[i - 1];
Line curr = Line(-2 * h[i], dp[i] + h[i] * h[i] - p[i]);
upd(1, 0, sz - 1, curr);
}
cout << dp[n];
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12884 KB |
Output is correct |
2 |
Correct |
6 ms |
12884 KB |
Output is correct |
3 |
Correct |
6 ms |
12884 KB |
Output is correct |
4 |
Correct |
7 ms |
12876 KB |
Output is correct |
5 |
Correct |
7 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
17836 KB |
Output is correct |
2 |
Correct |
74 ms |
17836 KB |
Output is correct |
3 |
Correct |
65 ms |
17856 KB |
Output is correct |
4 |
Correct |
85 ms |
17852 KB |
Output is correct |
5 |
Correct |
39 ms |
17792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12884 KB |
Output is correct |
2 |
Correct |
6 ms |
12884 KB |
Output is correct |
3 |
Correct |
6 ms |
12884 KB |
Output is correct |
4 |
Correct |
7 ms |
12876 KB |
Output is correct |
5 |
Correct |
7 ms |
12880 KB |
Output is correct |
6 |
Correct |
70 ms |
17836 KB |
Output is correct |
7 |
Correct |
74 ms |
17836 KB |
Output is correct |
8 |
Correct |
65 ms |
17856 KB |
Output is correct |
9 |
Correct |
85 ms |
17852 KB |
Output is correct |
10 |
Correct |
39 ms |
17792 KB |
Output is correct |
11 |
Correct |
65 ms |
18068 KB |
Output is correct |
12 |
Correct |
79 ms |
17816 KB |
Output is correct |
13 |
Correct |
61 ms |
18024 KB |
Output is correct |
14 |
Correct |
85 ms |
18096 KB |
Output is correct |
15 |
Correct |
45 ms |
17692 KB |
Output is correct |
16 |
Correct |
37 ms |
17808 KB |
Output is correct |
17 |
Correct |
31 ms |
17880 KB |
Output is correct |
18 |
Correct |
31 ms |
17952 KB |
Output is correct |