#include<iostream>
#define DIM 100005
#define x first
#define y second
using namespace std;
int n, i, m;
int h[DIM], w[DIM];
pair<int, int> aint[40 * DIM], p[DIM];
long long sum, d[DIM];
long long calc(pair<int, int> p, int a){
return p.x * 1LL * a + p.y;
}
void build(int nod, int st, int dr){
aint[nod] = p[1];
if(st != dr){
int mid = (st + dr) / 2;
build(2 * nod, st, mid);
build(2 * nod + 1, mid + 1, dr);
}
}
void update(int nod, int st, int dr, pair<int, int> p){
int mid = (st + dr) / 2;
if( calc(aint[nod], mid) > calc(p, mid) ){
swap(aint[nod], p);
}
if(st != dr){
if( calc(aint[nod], st) < calc(p, st) ){
update(2 * nod + 1, mid + 1, dr, p);
}
else{
update(2 * nod, st, mid, p);
}
}
}
long long query(int nod, int st, int dr, int p){
if(st == dr){
return calc(aint[nod], p);
}
else{
int mid = (st + dr) / 2;
long long x;
if(p <= mid){
x = query(2 * nod, st, mid, p);
}
else{
x = query(2 * nod + 1, mid + 1, dr, p);
}
return min(x, calc(aint[nod], p) );
}
}
int main(){
cin>> n;
for(i = 1; i <= n; i++){
cin>> h[i];
}
for(i = 1; i <= n; i++){
cin>> w[i];
m = max(m, h[i]);
sum += w[i];
p[i].y = h[i] * 1LL * h[i] - w[i];
p[i].x = -2 * h[i];
}
build(1, 0, m);
for(i = 2; i <= n; i++){
d[i] = query(1, 0, m, h[i]) - w[i] + h[i] * 1LL * h[i];
p[i].y = d[i] + h[i] * 1LL * h[i];
update(1, 0, m, p[i]);
}
cout<< d[n] + sum;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
129 ms |
5800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Incorrect |
129 ms |
5800 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |