#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} // thanks for Luzhiled's memo // https://ei1333.github.io/luzhiled/snippets/dp/monotone-minima.html template> pair, vector> monotone_minima(int H, int W, const function &f, const Compare &comp = Compare()) { vector idx(H); vector res(H); function dfs = [&](int top, int bottom, int left, int right) { if(top > bottom) return; int line = (top + bottom) / 2; T ma = 0; int mi = -1; for(int i = left; i <= right; i++) { T cst = f(line, i); if(mi == -1 || comp(cst, ma)) { ma = cst; mi = i; } } idx[line] = mi; res[line] = ma; dfs(top, line - 1, left, mi); dfs(line + 1, bottom, mi, right); }; dfs(0, H - 1, 0, W - 1); return make_pair(res, idx); } template> pair, vector> monotone_maxima(int H, int W, const function &f, const Compare &comp = Compare()) { return monotone_minima(H, W, f, comp); } const long long INF = 2002002002002002002; // A must be monotone template vector max_plus_convolution(vector A, vector B){ function f = [&](int i, int j){ if(i - j < 0 or i - j >= int(A.size())) return -INF; return A[i - j] + B[j]; }; auto [idx, dp] = monotone_maxima(B.size() + A.size() - 1, B.size(), f); return dp; } int main(){ int n; cin >> n; vector a(n); vector x(n); vector y(n); cin >> a; cin >> x; cin >> y; auto triple = [&](long long val){ val = abs(val); return val * val * val; }; auto dist = [&](int i, int j){ assert(j >= i); return triple(a[j] - x[i]) + triple(y[i]); }; auto solve = [&](int l, int r, auto solve) -> vector{ if(r == l + 1){ return vector{0LL, dist(l, l)}; } int mid = (l + r) / 2; auto L = solve(l, mid, solve); auto R = solve(mid, r, solve); vector res((r - l) + 1, INF); rep(i, int(L.size())) chmin(res[i], L[i]); rep(i, int(R.size())) chmin(res[(mid - l) + i], L[mid - l] + R[i]); function f = [&](int i, int j){ return L[j] + dist(l + j, (mid + i) - 1); }; auto [E, tmp] = monotone_minima((r - mid + 1), (mid - l), f); assert(E.size() == R.size()); rep(i, int(E.size())) chmin(res[(mid - l) + i], E[i]); return res; }; auto E = solve(0, n, solve); cout << E[n] << "\n"; // cout << E; return 0; }