#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; // https://ei1333.github.io/library/structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp template struct ConvexHullTrickAddMonotone { #define F first #define S second using P = pair; deque

H; ConvexHullTrickAddMonotone() = default; bool empty() const { return H.empty(); } void clear() { H.clear(); } static constexpr int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); } static constexpr T floor_div(T n, T d) { return n / d - ((n ^ d) < 0 and n % d != 0); } static constexpr bool check(const P& a, const P& b, const P& c) { if (b.S == a.S || c.S == b.S) return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S); // return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F); if constexpr (is_integral::value) { return floor_div(b.S - a.S, a.F - b.F) >= floor_div(c.S - b.S, b.F - c.F); } else { return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >= (c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S); } } void add(T a, T b) { if (!isMin) a *= -1, b *= -1; P line(a, b); if (empty()) { H.emplace_front(line); return; } if (H.front().F <= a) { if (H.front().F == a) { if (H.front().S <= b) return; H.pop_front(); } while (H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front(); H.emplace_front(line); } else { assert(a <= H.back().F); if (H.back().F == a) { if (H.back().S <= b) return; H.pop_back(); } while (H.size() >= 2 && check(H[H.size() - 2], H.back(), line)) H.pop_back(); H.emplace_back(line); } } static constexpr T get_y(const P& a, const T& x) { return a.F * x + a.S; } T query(T x) const { assert(!empty()); int l = -1, r = H.size() - 1; while (l + 1 < r) { int m = (l + r) >> 1; if (get_y(H[m], x) >= get_y(H[m + 1], x)) l = m; else r = m; } if (isMin) return get_y(H[r], x); return -get_y(H[r], x); } T query_monotone_inc(T x) { assert(!empty()); while (H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x)) H.pop_front(); if (isMin) return get_y(H.front(), x); return -get_y(H.front(), x); } T query_monotone_dec(T x) { assert(!empty()); while (H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x)) H.pop_back(); if (isMin) return get_y(H.back(), x); return -get_y(H.back(), x); } #undef F #undef S }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n; VI w(n + 1); rep(i, n) cin >> w[i + 1]; rep(i, n) w[i+1] += w[i]; cin >> m; VI s(m + 1); rep(i, m) cin >> s[i + 1]; rep(i, m) s[i+1] += s[i]; VI d; merge(all(s), all(w), back_inserter(d)); d.erase(unique(all(d)), d.end()); int sz = d.size(); VI a(sz-1); rep(z, 2) { int p = 0; rep(i, n) { while (d[p] < w[i]) p++; if (i % 2) continue; while (d[p] < w[i+1]) a[p++] |= 1 << z; } swap(w, s); swap(n, m); } ConvexHullTrickAddMonotone cht0, cht1; ll now = 0; rep(i, sz - 1) { ll d2 = (ll)d[i] * d[i]; if (!(a[i] & 1)) cht0.clear(); if (!(a[i] & 2)) cht1.clear(); if (a[i] & 1) cht0.add(2 * d[i], now - d2); if (a[i] & 2) cht1.add(2 * d[i], now - d2); ll nd2 = (ll)d[i+1] * d[i+1]; if (!cht0.empty()) chmin(now, cht0.query(d[i+1]) - nd2); if (!cht1.empty()) chmin(now, cht1.query(d[i+1]) - nd2); } cout << -now << '\n'; }