#include "bits/stdc++.h" using namespace std; template static void amin(T &x, U y) { if (y < x) x = y; } const long long INFL = 0x3f3f3f3f3f3f3f3fLL; int main() { int N; while (~scanf("%d", &N)) { vector as(N); for (int i = 0; i < N; ++ i) scanf("%d", &as[i]); vector xs(N); for (int i = 0; i < N; ++ i) scanf("%d", &xs[i]); vector ys(N); for (int i = 0; i < N; ++ i) scanf("%d", &ys[i]); sort(as.begin(), as.end()); sort(xs.begin(), xs.end()); auto values = xs; sort(values.begin(), values.end()); values.erase(unique(values.begin(), values.end()), values.end()); auto cb = [](int x) { return (long long)x * x * x; }; auto monotoneMinima = [](int n, int m, auto f, auto out) { auto rec = [=](auto rec, int L, int R, int jL, int jR) -> void { if (L > R) return; int mid = (L + R) / 2; auto t = f(mid, jL); int jM = jL; for (int j = jL + 1; j <= jR; ++ j) { auto u = f(mid, j); if (u < t) t = u, jM = j; } out(mid, t, jM); rec(rec, L, mid - 1, jL, jM); rec(rec, mid + 1, R, jM, jR); }; rec(rec, 0, n - 1, 0, m - 1); }; auto totallyMonotoneMinima = [](int N, int M, auto f, auto out) { vector minima(N, -1); auto rec = [N, f, out, &minima](auto rec, int k, int L, const vector &js) -> void { if (L >= N) return; int s = 1 << k, n = ((N - L - 1) >> k) + 1; vector njs; for (int j : js) { int i = L + ((int)njs.size() << k); while (!njs.empty() && !(f(i, njs.back()) < f(i, j))) njs.pop_back(), i -= s; if ((int)njs.size() + 1 < n) njs.push_back(j); } rec(rec, k + 1, L + s, njs); for (int i = L, jx = 0; i < N; i += s * 2) { int jR = i + s < N ? minima[i + s] : js.back(); auto t = f(i, js[jx]); int minj = js[jx]; for (++ jx; jx != js.size() && js[jx] <= jR; ++ jx) { auto u = f(i, js[jx]); if (u < t) t = u, minj = js[jx]; } minima[i] = minj; out(i, t, minj); if (js[jx - 1] == jR) -- jx; } }; vector js; for (int j = 0; j < M; ++ j) js.push_back(j); rec(rec, 0, 0, js); assert(count(minima.begin(), minima.end(), -1) == 0); }; auto solveOffline = [totallyMonotoneMinima](const long long *prev, int m, int n, auto f, auto out) { totallyMonotoneMinima(n, m, [=](int i, int j) { return prev[j] + f(j, i); }, [=](int i, long long t, int minj) { out(i, t); }); }; vector dp(N + 1, INFL); dp[0] = 0; vector tmp(N + 1); function rec = [&](int L, int R) { if (R - L <= 1) return; int mid = (L + R) / 2; rec(L, mid); for (int i = 0; i < R - L; ++ i) tmp[i] = dp[L + i] + (L + i == N ? 0 : cb(ys[L + i])); solveOffline(tmp.data(), mid - L, R - mid, [&](int j, int i) { j += L, i += mid; return cb(abs(as[i - 1] - xs[j])); }, [&](int i, long long t) { i += mid; amin(dp[i], t); }); rec(mid, R); }; rec(0, N + 1); long long ans = dp[N]; printf("%lld\n", ans); } }