#include using namespace std; template // return_type struct MongeDP { vector dp; vector pre; function cmp; // true is left better function &dp, int, int)> get_cost; MongeDP(int n, function c, function &dp, int, int)> gc) : dp(n + 1), pre(n + 1, -1), cmp(c), get_cost(gc) { deque> dcs; // decision dcs.emplace_back(0, 1, n); // transition from dp[0] is effective for [1, N] for (int i = 1; i <= n; ++i) { while (get<2>(dcs.front()) < i) dcs.pop_front(); // right bound is out-dated dp[i] = get_cost(dp, pre[i] = get<0>(dcs.front()), i); // best t is A[dcs.top(), i) while (dcs.size()) { int x, lb, rb; tie(x, lb, rb) = dcs.back(); if (lb <= i) break; // will be pop_fronted soon anyway if (!cmp(get_cost(dp, x, lb), get_cost(dp, i, lb))) { dcs.pop_back(); if (dcs.size()) get<2>(dcs.back()) = n; } else break; } int best = -1; for (int lb = i + 1, rb = n, x = get<0>(dcs.back()); lb <= rb; ) { int mb = lb + rb >> 1; if (cmp(get_cost(dp, i, mb), get_cost(dp, x, mb))) { best = mb; rb = mb - 1; } else lb = mb + 1; } if (~best) { get<2>(dcs.back()) = best - 1; dcs.emplace_back(i, best, n); } } } R operator[](int x) { return dp[x]; } }; signed main() { ios::sync_with_stdio(false); int N; cin >> N; vector A(N); for (int i = 0; i < N; ++i) cin >> A[i]; vector X(N); for (int i = 0; i < N; ++i) cin >> X[i]; vector Y(N); for (int i = 0; i < N; ++i) cin >> Y[i]; auto sqr = [](int64_t x) { return x * x; }; MongeDP mdp(N, [](int64_t x, int64_t y) { return x < y; }, [&](const vector &dp, int x, int rb) { return dp[x] + sqr(A[rb - 1] - X[x]) + sqr(Y[x]); }); cout << mdp[N] << endl; return 0; }