#include <bits/stdc++.h>
#define DIM 100010
#define DIMM 1000010
#define INF 2000000000
using namespace std;
pair <int,int> aint[DIMM*4];
int h[DIM],w[DIM],sp[DIM],dp[DIM];
int n,i,maxi;
int f (int a, int b, int x){
return a*x+b;
}
void add (int nod, int st, int dr, int a, int b){
if (st == dr){
if (f(a,b,st) < f(aint[nod].first,aint[nod].second,st))
aint[nod] = make_pair (a,b);
return;
}
int mid = (st+dr)>>1;
int ok_mid = 0, ok_dr = 0;
if (f(a,b,mid) < f(aint[nod].first,aint[nod].second,mid))
ok_mid = 1;
if (f(a,b,dr) < f(aint[nod].first,aint[nod].second,dr))
ok_dr = 1;
if (!ok_mid && !ok_dr) /// ma duc in stanga
add (nod<<1,st,mid,a,b);
else {
if (!ok_mid && ok_dr)
add ((nod<<1)|1,mid+1,dr,a,b);
else {
/// inseamna ca ok_mid = 1
swap (aint[nod].first,a);
swap (aint[nod].second,b);
if (ok_dr)
add (nod<<1,st,mid,a,b);
else add ((nod<<1)|1,mid+1,dr,a,b);
}
}
}
int query (int nod, int st, int dr, int x){
if (st == dr)
return f(aint[nod].first,aint[nod].second,x);
int mid = (st+dr)>>1;
int sol = INF;
if (x <= mid)
sol = query (nod<<1,st,mid,x);
else sol = query ((nod<<1)|1,mid+1,dr,x);
return min (sol,f(aint[nod].first,aint[nod].second,x));
}
int main (){
cin>>n;
for (i=1;i<=n;i++){
cin>>h[i];
maxi = max (maxi,h[i]);
}
for (i=1;i<=n;i++){
cin>>w[i];
sp[i] = sp[i-1] + w[i];
}
/// pe primul si ultimul le iau obligatoriu
/// dp[i] - costul minim sa ajung in i
//for (i=2;i<=n;i++)
// dp[i] = (h[i]-h[1])*(h[i]-h[1]) + sp[i-1]-sp[1];
add (1,0,maxi,-2*h[1],h[1]*h[1]-w[1]);
for (i=2;i<=n;i++){
dp[i] = h[i]*h[i] + sp[i-1] + query (1,0,maxi,h[i]);
add (1,0,maxi,-2*h[i],dp[i]+h[i]*h[i]-sp[i]);
}
cout<<dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
111 ms |
3072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |