ForLune_ @ 2023-04-20 19:00:46
#include <cstdio>
#include <algorithm>
#define lint long long
#define Range 0,1000000
using namespace std;
int n; lint h[100005],w[100005],k[100005],b[100005],f[100005];
int value[4000005];
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
inline lint calc1(const int x,const int id) {return k[id]*1ll*x+b[id];}
inline int calc2(const int x,const int A,const int B)
{
lint y1=calc1(x,A),y2=calc1(x,B);
return (y1==y2)?(min(A,B)):((y1<y2)?(A):(B));
}
inline void PushDown(int Value,const int l,const int r,const int u)
{
int mid=(l+r)>>1;
if(calc1(mid,Value)<calc1(mid,value[u])) swap(Value,value[u]);
if(calc1(l,Value)<calc1(l,value[u])) PushDown(Value,l,mid,u<<1);
if(calc1(r,Value)<calc1(r,value[u])) PushDown(Value,mid+1,r,u<<1|1);
}
inline int query(const int pos,const int l,const int r,const int u)
{
if(l==r) return value[u];
int mid=(l+r)>>1;
if(pos<=mid) return calc2(pos,query(pos,l,mid,u<<1),value[u]);
if(pos>mid) return calc2(pos,query(pos,mid+1,r,u<<1|1),value[u]);
return 0;
}
int main()
{
n=read(),b[0]=0x7f7f7f7f;
for(int i=1;i<=n;++i) h[i]=1ll*read();
for(int i=1;i<=n;++i) w[i]=w[i-1]+1ll*read();
k[1]=-(h[1]<<1),b[1]=h[1]*h[1]-w[1],PushDown(1,Range,1);
for(int i=2;i<=n;++i)
{
f[i]=h[i]*h[i]+w[i-1]+calc1((int)h[i],query((int)h[i],Range,1));
k[i]=-(h[i]<<1),b[i]=f[i]+h[i]*h[i]-w[i],PushDown(i,Range,1);
}
printf("%lld",f[n]);
return 0;
}