#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include using namespace std; #define ll long long #define pii pair #define pll pair #define vi vector #define vl vector #define ov3(a, b, c, name, ...) name #define rep0(n) for(ll aaaaa = 0; aaaaa < (n); aaaaa++) #define rep1(i, n) for(ll i = 0; i < (n); i++) #define rep2(i, a, b) for(ll i = (a); i < (b); i++) #define rep(...) ov3(__VA_ARGS__, rep2, rep1, rep0)(__VA_ARGS__) #define fore(e, v) for(auto &&e : v) #define all(v) begin(v), end(v) #define si(a) (int)(size(a)) bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; } bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; } const int inf = 11e8; const ll infl = 2e18; #define i128 __int128_t template struct CHT { #define x first #define y second CHT() = default; deque v; bool empty() { return v.empty(); } void clear() { return v.clear(); } inline int sgn(ll x) { return !x ? 0 : (x < 0 ? -1 : 1); } using D = long double; inline bool check(const pll &a, const pll &b, const pll &c) { if(b.y == a.y or c.y == b.y) return sgn(b.x - a.x) * sgn(c.y - b.y) >= sgn(c.x - b.x) * sgn(b.y - a.y); return D(b.x - a.x) * sgn(c.y - b.y) / D(abs(b.y - a.y)) >= D(c.x - b.x) * sgn(b.y - a.y) / D(abs(c.y - b.y)); } void add(ll a, ll b) { if(!isMin) a *= -1, b *= -1; pll line(a, b); if(empty()) v.emplace_front(line); else { if(ll c = v[0].x; c <= a) { if(c == a) { if(v[0].y <= b) return; v.pop_front(); } while(si(v) >= 2 and check(line, v[0], v[1])) v.pop_front(); v.emplace_front(line); } else { assert(a <= v.back().x); if(v.back().x == a) { if(v.back().y <= b) return; v.pop_back(); } while(si(v) >= 2 and check(v[si(v) - 2], v.back(), line)) v.pop_back(); v.emplace_back(line); } } } ll get_y(const pll &a, const ll &x) { return a.x * x + a.y; } ll query(ll x) { assert(!empty()); int l = -1, r = si(v) - 1; while(l + 1 < r) { int m = (l + r) >> 1; if(get_y(v[m], x) >= get_y(v[m + 1], x)) l = m; else r = m; } return get_y(v[r], x) * (isMin ? 1 : -1); } ll query_monotone_inc(ll x) { assert(!empty()); while(si(v) >= 2 and get_y(v[0], x) >= get_y(v[1], x)) v.pop_front(); return get_y(v[0], x) * (isMin ? 1 : -1); } ll query_monotone_dec(ll x) { assert(!empty()); while(si(v) >= 2 and get_y(v.back(), x) >= get_y(v.end()[-2], x)) v.pop_back(); return get_y(v.back(), x) * (isMin ? 1 : -1); } #undef x #undef y }; // A[N + 1][N + 1]: Monge が i > j のみ存在しているとき、i (= 0, ..., N)行目の最小値を返す // f(i, j, v) で、j 行目の最小値が求まっている v を用いて、A[i][j] にアクセス template vector monge_rowmin(int n, const F &f) { vector mi(n + 1, numeric_limits::max()); mi[0] = 0; vector amin(n + 1); auto check = [&](int i, int j) { if(chmin(mi[i], f(i, j, mi))) { amin[i] = j; } }; check(n, 0); auto solve = [&](auto &&self, int l, int r) { if(r - l == 1) return; int mid = l + r >> 1; rep(k, amin[l], amin[r] + 1) check(mid, k); self(self, l, mid); rep(k, l + 1, mid + 1) check(r, k); self(self, mid, r); }; solve(solve, 0, n); return mi; } // monotone 行列の各行について、最小値を取る場所とその値を返す template vector> monotone_minima(int h, int w, const F &f) { vector> dp(h, pair(-1, T())); auto rec = [&](auto &&rec, int u, int d, int l, int r) { if(u > d) return; int mid = u + d >> 1; auto &[idx, mi] = dp[mid]; idx = l, mi = f(mid, l); rep(i, l + 1, r + 1) if(chmin(mi, f(mid, i))) idx = i; rec(rec, u, mid - 1, l, idx); rec(rec, mid + 1, d, idx, r); }; rec(rec, 0, h - 1, 0, w - 1); return dp; } int main() { int n; cin >> n; vl a(n), x(n), y(n); fore(e, a) cin >> e; fore(e, x) cin >> e; fore(e, y) cin >> e; auto p3 = [&](ll x) { return x * x * x; }; auto cost = [&](int i, int j) { return p3(abs(a[j - 1] - x[i])) + p3(y[i]); }; auto f = [&](int j, int i, vl &v) { return v[i] + cost(i, j); }; cout << monge_rowmin(n, f)[n] << endl; }