#include // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2") #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define all(x) (x).begin(), (x).end() #define sz(x) int(x.size()) #define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n" #define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n" using namespace std; using ll = long long; using vi = vector; using vl = vector; using vs = vector; using vc = vector; using vd = vector; using vld = vector; using vvi = vector>; using vvl = vector>; using vvs = vector>; using vvc = vector>; using vvd = vector>; using vvld = vector>; using vvvi = vector>>; using vvvl = vector>>; using vvvvi = vector>>>; using vvvvl = vector>>>; using pii = pair; using pll = pair; const int INF = 1e9; const ll LINF = 2e18; template bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } bool ispow2(int i) { return i && (i & -i) == i; } bool ispow2(ll i) { return i && (i & -i) == i; } template vector make_vec(size_t a) { return vector(a); } template auto make_vec(size_t a, Ts... ts) { return vector(ts...))>(a, make_vec(ts...)); } template istream& operator>>(istream& is, vector& v) { for (int i = 0; i < int(v.size()); i++) { is >> v[i]; } return is; } template ostream& operator<<(ostream& os, const vector& v) { for (int i = 0; i < int(v.size()); i++) { os << v[i]; if (i < int(v.size()) - 1) os << ' '; } return os; } static uint32_t RandXor(){ static uint32_t x=123456789; static uint32_t y=362436069; static uint32_t z=521288629; static uint32_t w=88675123; uint32_t t; t=x^(x<<11); x=y; y=z; z=w; return w=(w^(w>>19))^(t^(t>>8)); } // segtree seg(int n)またはsegtree seg(vector vec)で初期化 // Sは型, Sを返す関数op(S a,S b)と単位元S e()を設定する // set(int p,S x): a[p]にxを代入する // get(int p): a[p]を取得する // prod(int l,int r): [l,r)をfoldした結果を返す // all_prod(): [0,n)をfoldした結果を返す // max_right(int l): bool f(S x)を渡すとl以降でf(prod(l,r))=trueとなる最大のrを返す // min_left: max_rightの逆 int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } template struct segtree{ public: segtree() : segtree(0) {} segtree(int n) : segtree(vector(n, e())) {} segtree(const vector &v) : _n(int(v.size())){ log = ceil_pow2(_n); size = 1 << log; d = vector(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) update(i); } void set(int p, S x){ assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p){ assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r){ assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r){ if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } template int max_right(int l){ return max_right(l, [](S x) { return f(x); }); } template int max_right(int l, F f){ assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do{ while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))){ while (l < size){ l = (2 * l); if (f(op(sm, d[l]))){ sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template int min_left(int r){ return min_left(r, [](S x) { return f(x); }); } template int min_left(int r, F f){ assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do{ r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))){ while (r < size){ r = (2 * r + 1); if (f(op(d[r], sm))){ sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; ll op(ll l,ll r){return min(l,r);} ll e(){return LINF;} void solve(){ ll N; cin>>N; vl A(N),X(N),Y(N); cin>>A>>X>>Y; segtree DP1(N+1); segtree DP2(N+1); vl DP(N+1,LINF); DP[0]=0; DP1.set(0,Y[0]-X[0]); DP2.set(0,Y[0]+X[0]); for(int i=1;i<=N;i++){ int p=lower_bound(all(X),A[i-1])-X.begin(); if(p) chmin(DP[i],DP1.prod(0,min(p-1,i-1)+1)+A[i-1]); if(p<=i-1) chmin(DP[i],DP2.prod(p,i)-A[i-1]); DP1.set(i,DP[i]+Y[i]-X[i]); DP2.set(i,DP[i]+Y[i]+X[i]); } cout<