czy_0021 @ 2024-12-08 11:18:31
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e6+10;
int n,h[N],s[N],f[N];
struct node{
int k;
int b;
int id;
}a[N],v;
int vis(int k,int x){
return x*a[k].k+a[k].b;
}
int change(int k,int l,int r){
if(vis(k,l)>v.k*l+v.b&&vis(k,r)>v.k*r+v.b){
a[k]=v;
return 0;
}
if(vis(k,l)<=v.k*l+v.b&&vis(k,r)<=v.k*r+v.b)return 0;
int mid=(l+r)/2;
change(k*2,l,mid);
change(k*2+1,mid+1,r);
return 0;
}
int ask(int k,int l,int r,int x){
if(r<x||l>x)return 0;
int ans=vis(k,x),p=k;
int mid=(l+r)/2;
if(l==r)return p;
int t=ask(k*2,l,mid,x);
if(t!=0&&vis(t,x)<vis(p,x))p=t;
t=ask(k*2+1,mid+1,r,x);
if(t!=0&&vis(t,x)<vis(p,x))p=t;
return p;
}
void build(int k,int l,int r){
a[k].b=1e18;
if(l==r)return;
int mid=(l+r)/2;
build(k*2,l,mid);
build(k*2+1,mid+1,r);
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++)
scanf("%lld",&h[i]);
for(int i=1;i<=n;i++){
scanf("%lld",&s[i]);
s[i]+=s[i-1];
}
build(1,1,1000000);
for(int i=2;i<=n;i++){
f[i]=min((h[i]-h[1])*(h[i]-h[1])+s[i-1]-s[1],vis(ask(1,1,1000000,h[i]),h[i])+h[i]*h[i]+s[i-1]);
v.k=-2*h[i];v.b=f[i]+h[i]*h[i]-s[i];
change(1,1,1000000);
}
cout<<f[n];
return 0;
}