#include using namespace std; using Int = long long; template struct SegmentTree{ using F = function; using G = function; Int n; F f; G g; T ti; vector dat; SegmentTree(){}; SegmentTree(Int n_,F f,G g,T ti): f(f),g(g),ti(ti){ init(n_); } void init(Int n_){ n=1; while(n v){ for(Int i=0;i=0;i--) dat[i]=f(dat[i*2+1],dat[i*2+2]); } void update(Int k,E a){ k+=n-1; dat[k]=g(dat[k],a); while(k>0){ k=(k-1)/2; dat[k]=f(dat[k*2+1],dat[k*2+2]); } } inline T query(Int a,Int b){ T vl=ti,vr=ti; for(Int l=a+n,r=b+n;l>=1,r>>=1) { if(l&1) vl=f(vl,dat[(l++)-1]); if(r&1) vr=f(dat[(--r)-1],vr); } return f(vl,vr); } Int find(Int a,Int b,function &check,Int k,Int l,Int r){ if(!check(dat[k])||r<=a||b<=l) return -1; if(k>=n-1) return k-(n-1); Int m=(l+r)>>1; Int vl=find(a,b,check,k*2+1,l,m); if(~vl) return vl; return find(a,b,check,k*2+2,m,r); } Int find(Int a,Int b,function &check){ return find(a,b,check,0,0,n); } }; //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector a(n),x(n),y(n); for(Int i=0;i>a[i]; for(Int i=0;i>x[i]; for(Int i=0;i>y[i]; const Int INF = 1e18; auto f=[](Int a,Int b){return min(a,b);}; SegmentTree sl(n,f,f,INF); SegmentTree sr(n,f,f,INF); vector dp(n+1,0); auto update= [&](Int k){ if(++k==n) return; sl.update(k,dp[k]-x[k]+y[k]); sr.update(k,dp[k]+x[k]+y[k]); }; auto query= [&](Int k){ Int t=lower_bound(x.begin(),x.end(),a[k])-x.begin(); return min(sl.query(0,t)+a[k],sr.query(t,n)-a[k]); }; update(-1); for(Int i=0;i