#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define forr(x,arr) for(auto&& x:arr) #define _overload3(_1,_2,_3,name,...) name #define _rep2(i,n) _rep3(i,0,n) #define _rep3(i,a,b) for(int i=int(a);i=int(a);i--) #define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__) #define all(x) (x).begin(),(x).end() #define bit(n) (1LL<<(n)) #define sz(x) ((int)(x).size()) #define TEN(n) ((ll)(1e##n)) #define fst first #define snd second string DBG_DLM(int &i){return(i++==0?"":", ");} #define DBG_B(exp){int i=0;os<<"{";{exp;}os<<"}";return os;} templateostream&operator<<(ostream&os,vectorv); templateostream&operator<<(ostream&os,setv); templateostream&operator<<(ostream&os,queueq); templateostream&operator<<(ostream&os,priority_queueq); templateostream&operator<<(ostream&os,pairp); templateostream&operator<<(ostream&os,mapmp); templateostream&operator<<(ostream&os,unordered_mapmp); templatevoid DBG(ostream&os,TPL t){} templatevoid DBG(ostream&os,TPL t){os<<(I==0?"":", ")<(t);DBG(os,t);} templatevoid DBG(ostream&os,pairp,string delim){os<<"("<ostream&operator<<(ostream&os,tuplet){os<<"(";DBG<0,tuple,Ts...>(os,t);os<<")";return os;} templateostream&operator<<(ostream&os,pairp){DBG(os,p,", ");return os;} templateostream&operator<<(ostream&os,vectorv){DBG_B(forr(t,v){os<ostream&operator<<(ostream&os,sets){DBG_B(forr(t,s){os<ostream&operator<<(ostream&os,queueq){DBG_B(for(;q.size();q.pop()){os<ostream&operator<<(ostream&os,priority_queueq){DBG_B(for(;q.size();q.pop()){os<ostream&operator<<(ostream&os,mapm){DBG_B(forr(p,m){os<");});} templateostream&operator<<(ostream&os,unordered_mapm){DBG_B(forr(p,m){os<");});} #define DBG_OVERLOAD(_1,_2,_3,_4,_5,_6,macro_name,...)macro_name #define DBG_LINE(){char s[99];sprintf(s,"line:%3d | ",__LINE__);cerr<;using pll=pair;using pil=pair;using pli=pair; using vs=vector;using vvs=vector;using vvvs=vector; using vb=vector;using vvb=vector;using vvvb=vector; using vi=vector;using vvi=vector;using vvvi=vector; using vl=vector;using vvl=vector;using vvvl=vector; using vd=vector;using vvd=vector;using vvvd=vector; using vpii=vector;using vvpii=vector;using vvvpii=vector; templatebool amax(A&a,const B&b){return b>a?a=b,1:0;} templatebool amin(A&a,const B&b){return bvoid print(T t){cout<void print(H h,T... t){cout<>l;return l;} string rs(){string s;cin>>s;return s;} template struct SegmentTree { const int n; const V unit_value; vector val; SegmentTree(int _n) : n(1 << (32-__builtin_clz(_n-1))), unit_value(Merge::unit()), val(n * 2, unit_value) {} V get(int i) const { return val[i + n]; } void set(int i, const V &v) { val[i + n] = v; } void build() { for (int i = n - 1; i > 0; i--) val[i] = Merge::merge(val[i << 1], val[i << 1 | 1]); } void update(int i, const V &v) { for (val[i += n] = v; i >>= 1;) { val[i] = Merge::merge(val[i << 1], val[i << 1 | 1]); } } V query(int l, int r) const { l = max(0, min(n, l)) + n; r = max(0, min(n, r)) + n; V ret1 = unit_value, ret2 = unit_value; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) ret1 = Merge::merge(ret1, val[l++]); if (r & 1) ret2 = Merge::merge(val[--r], ret2); } return Merge::merge(ret1, ret2); } }; template ostream& operator<<(ostream& os, const SegmentTree& seg) { vector vec; for (int i = 0; i < seg.n; i++) { vec.emplace_back(seg.get(i)); } os << vec; return os; } template struct MergeRangeMinQ { static V merge(const V &l, const V &r) { return l < r ? l : r; } static V unit() { return numeric_limits::max(); } }; template using SegTreeMin = SegmentTree>; ll solve() { int n = ri(); vector A(n); rep(i, n) A[i] = ri(); vector X(n); rep(i, n) X[i] = ri(); vector Y(n); rep(i, n) Y[i] = ri(); const ll inf = 1e18; /* vl dp(n+1, inf); dp[0] = 0; if (0) { auto cost = [&](int i, int j) -> ll { ll dx = abs(X[j] - A[i]); ll dy = Y[j]; return dx + dy; }; rep(i, n) { rep(j, i+1) { amin(dp[i+1], dp[j] + cost(i, j)); } } out(dp); } fill(all(dp), inf); dp[0] = 0; */ vl dp(n+1, inf); dp[0] = 0; SegTreeMin seg1(n), seg2(n); rep(i, n) { seg1.update(i, dp[i] + X[i] + Y[i]); seg2.update(i, dp[i] - X[i] + Y[i]); int idx = lower_bound(&(X[0]), &(X[i+1]), A[i]) - &(X[0]); out(i, A[i], idx); ll cand1 = +A[i] + seg2.query(0, idx); ll cand2 = idx < i+1 ? -A[i] + seg1.query(idx, i+1) : inf; out(cand1, cand2); dp[i+1] = min(cand1, cand2); } out(dp); return dp[n]; } void Main() { cout << solve() << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }