#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; template struct ConvexHullTrick { private: bool minflag; static const T Q = numeric_limits::lowest(); struct Line { T a,b; mutable function NEX; bool operator<(const Line &other) const { if(b == Q) { const Line* nl = NEX(); if(!nl) return true; return (nl -> a - other.a) * a + (nl -> b - other.b) < 0; } else if(other.b == Q) { const Line* nl = NEX(); if(!nl) return false; return (nl -> a - a) * other.a + (nl -> b - b) > 0; } else return a < other.a; } }; bool isNotNeeded(const typename set::iterator it) { const auto nt = next(it); if(it == Lines.begin()) { if(nt == Lines.end()) return false; return (*it).a == (*nt).a && (*it).b <= (*nt).b; } else { const auto pt = prev(it); if(nt == Lines.end()) return (*it).a == (*pt).a && (*it).b <= (*pt).b; else return ((*pt).b-(*it).b) * ((*nt).a-(*it).a) >= ((*nt).b-(*it).b) * ((*pt).a-(*it).a); } } multiset Lines; public: constexpr ConvexHullTrick(bool minflag = true) : minflag(minflag) {} constexpr void add(T a,T b) noexcept { if(minflag) a = -a,b = -b; auto it = Lines.insert(Line{a,b}); (*it).NEX = [=] {return next(it) == Lines.end() ? nullptr : &*next(it);}; if(isNotNeeded(it)) Lines.erase(it); else { while(next(it) != Lines.end() && isNotNeeded(next(it))) Lines.erase(next(it)); while(it != Lines.begin() && isNotNeeded(prev(it))) Lines.erase(prev(it)); } } constexpr T get(T x) noexcept { Line l = *Lines.lower_bound(Line{x,Q}); T res = l.a * x + l.b; if(minflag) res = -res; return res; } }; int N,A[3 << 17],X[3 << 17],Y[3 << 17]; long long dp[3 << 17]; void solve() { //dp[i] = min{dp[j] + dist}; cin >> 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]; ConvexHullTrick CHT; for(int i = 1;i <= N;i++) { CHT.add(-2*X[i-1],(long long)X[i-1]*X[i-1]+(long long)Y[i-1]*Y[i-1]+dp[i-1]); dp[i] = CHT.get(A[i-1])+(long long)A[i-1]*A[i-1]; } cout << dp[N] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; //cin >> tt; while(tt--) solve(); }