#include using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v) { for (int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template istream &operator>>(istream &is, vector &v) { for (T &in : v) is >> in; return is; } template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template vector make_v(size_t a) { return vector(a); } template auto make_v(size_t a, Ts... ts) { return vector(ts...))>(a, make_v(ts...)); } template typename enable_if::value == 0>::type fill_v(T &t, const V &v) { t = v; } template typename enable_if::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } template struct FixPoint : F { explicit FixPoint(F &&f) : F(forward(f)) {} template decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward(args)...); } }; template inline decltype(auto) MFP(F &&f) { return FixPoint{forward(f)}; } #line 1 "dp/monotone-minima.cpp" /** * @brief Monotone Minima * @docs docs/monotone-minima.md */ template > vector > monotone_minima(int H, int W, const function &f, const Compare &comp = Compare()) { vector > dp(H); function dfs = [&](int top, int bottom, int left, int right) { if (top > bottom) return; int line = (top + bottom) / 2; T ma; 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; } } dp[line] = make_pair(mi, ma); dfs(top, line - 1, left, mi); dfs(line + 1, bottom, mi, right); }; dfs(0, H - 1, 0, W - 1); return dp; } #line 2 "dp/online-offline-dp.cpp" /** * @brief Online Offline DP(オンライン・オフライン変換) * @docs docs/online-offline-dp.md */ template > vector online_offline_dp(int W, const function &f, const T& INF, const Compare &comp = Compare()) { vector dp(W + 1, INF); dp[0] = T(); int y_base = -1, x_base = -1; function get_cost = [&](int y, int x) { // return dp[0, x+x_base)+f[x+x_base, y+y_base) if(dp[x + x_base] == INF) return INF; else return dp[x + x_base] + f(x + x_base, y + y_base); }; function induce = [&](int l, int m, int r) { // dp[l, m) -> dp[m, r) x_base = l, y_base = m; auto ret = monotone_minima(r - m, m - l, get_cost, comp); for (int i = 0; i < ret.size(); i++) { if (comp(ret[i].second, dp[m + i])) { dp[m + i] = ret[i].second; } } }; auto dfs = [&](auto dfs, int l, int r) -> void { if (l + 1 == r) { x_base = l, y_base = l; T cst = l ? get_cost(0, -1) : 0; if (comp(cst, dp[l])) dp[l] = cst; } else { int mid = (l + r) / 2; dfs(dfs, l, mid); induce(l, mid, r); dfs(dfs, mid, r); } }; dfs(dfs, 0, W + 1); return dp; }; int main() { int n; cin >> n; vector< int > a(n), x(n), y(n); for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> x[i]; for(int i = 0; i < n; i++) cin >> y[i]; function< int64_t(int, int) > dist = [&](int i, int j) { assert(0 <= i && i < j && j <= n); int s = abs(a[j - 1] - x[i]); int t = abs(y[i]); return 1LL * s * s + 1LL * t * t; }; cout << online_offline_dp< int64_t >(n, dist, 1ll<<60).back() << endl; }