#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define ii pair<int,int>
#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define all(v) v.begin(),v.end()
#define BIT(i) ((ll)1<<(i))
#define endl "\n"
#define debug(x) for (auto p: x) cout<<p<<' ';cout<<endl
#define forw(i,j,z) for(int i=(int)j;i<=(int)z;i++)
#define forw2(i,j,z,k) for(int i=(int)j;i<=(int)z;i+=k)
#define ford(i,j,z) for (int i=(int)j;i>=(int)z;i--)
#define ford2(i,j,z,k) for (int i=(int)j;i>=(int)z;i-=k)
#define sz(a) (int)a.size()
const ll inf=(ll)1<<60;
int n;
const int N=1e5;
const int MAXVAL=1e6;
ll h[N+1];
ll pre[N+1],f[N+1];
struct Line{
ll a,b;
ll f(int x)
{
return a*x+b;
}
Line(ll a1,ll b1)
{
a=a1,b=b1;
}
Line()
{
}
};
Line seg[4*MAXVAL];
void add(int node,int l,int r,Line line)
{
if (l+1==r)
{
if (seg[node].f(l)>line.f(l))
seg[node]=line;
}
else{
int mid=(l+r)>>1;
if (seg[node].a>line.a) swap(seg[node],line);
if (seg[node].f(mid)<line.f(mid))
add((node<<1)+1,l,mid,line);
else{
swap(seg[node],line);
add((node<<1)+2,mid,r,line);
}
}
}
ll GET(int node,int l,int r,int x)
{
if (l+1==r)
return seg[node].f(x);
else{
int mid=(l+r)>>1;
if (x<mid) return min(seg[node].f(x),GET((node<<1)+1,l,mid,x));
else return min(seg[node].f(x),GET((node<<1)+2,mid,r,x));
}
}
void solve()
{
cin>>n;
forw(i,1,n) cin>>h[i];
forw(i,1,n){
cin>>pre[i];
pre[i]+=pre[i-1];
}
f[1]=0;
add(0,0,MAXVAL+1,Line(-2LL*h[1],h[1]*h[1]-pre[1]));
forw(i,2,n)
{
f[i]=GET(0,0,MAXVAL+1,h[i])+pre[i-1]+h[i]*h[i];
add(0,0,MAXVAL+1,Line(-2LL*h[i],f[i]+h[i]*h[i]-pre[i]));
}
cout<<f[n];
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//cout<<setprecision(6)<<fixed;
//time_t TIME_TU=clock();
int t=1;
//cin>>t;
while(t--)
solve();
//time_t TIME_TV=clock();
//cerr<<(db)(TIME_TV-TIME_TU)/CLOCKS_PER_SEC<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
2 ms |
4188 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
5996 KB |
Output is correct |
2 |
Correct |
41 ms |
6148 KB |
Output is correct |
3 |
Correct |
37 ms |
6172 KB |
Output is correct |
4 |
Correct |
35 ms |
5828 KB |
Output is correct |
5 |
Incorrect |
33 ms |
6244 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
2 ms |
4188 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |