#include using namespace std; using ll = long long; template istream& operator >> (istream& is, vector& vec) { for(T& x : vec) is >> x; return is; } template ostream& operator << (ostream& os, const vector& vec) { if(vec.empty()) return os; os << vec[0]; for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it; return os; } class LiChaoTree{ struct Line{ long long a, b; long long get(long long x){return a * x + b; } Line(long long a, long long b) : a(a), b(b) {} }; struct Node { Node *left, *right; Line line; Node(Line line) : left(nullptr), right(nullptr), line(line) {} }; const long long inf = (1ll << 60); const Line inf_line = Line{0, -inf}; Node *root; long long lx, rx; Node* _add_line(Node *nd, Line line, long long l, long long r){ if(l == r) return nullptr; if(nd == nullptr) return new Node(line); long long m = (l + r) >> 1; bool left = (line.get(l) >= nd->line.get(l)); bool mid = (line.get(m) >= nd->line.get(m)); bool right = (line.get(r) >= nd->line.get(r)); if(left && right)nd->line = line; if(left == right)return nd; if(mid) std::swap(nd->line, line); if(left != mid){ nd->left = _add_line(nd->left, line, l, m); }else{ nd->right = _add_line(nd->right, line, m, r); } return nd; } Node* _add_segment_line(long long a, long long b, Node *nd, Line line, long long l, long long r) { if(r <= a || b <= l) return nd; if(a <= l && r <= b) return _add_line(nd, line, l, r); if(nd == nullptr) nd = new Node(inf_line); long long m = (l + r) >> 1; nd->left = _add_segment_line(a, b, nd->left, line, l, m); nd->right = _add_segment_line(a, b, nd->right, line, m, r); return nd; } long long query(long long x, long long l, long long r){ Node *nd = root; long long res = -inf; while(r > l && nd != nullptr) { long long m = (l + r) >> 1; res = std::max(res, nd->line.get(x)); if(x < m) { r = m; nd = nd->left; } else { l = m; nd = nd->right; } } return res; } public: LiChaoTree(long long lx, long long rx) : lx(lx), rx(rx), root(nullptr) {} void add_line(long long a, long long b) { Line line(a, b); root = _add_line(root, line, lx, rx); } void add_segment_line(long long a, long long b, long long l, long long r) { Line line = Line{a, b}; root = _add_segment_line(l, r, root, line, lx, rx); } long long get(long long x) { return query(x, lx, rx); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n; vector w(n); cin >> w >> m; vector s(m); cin >> s; vector ca; ca.reserve(n + m); w.insert(w.begin(), 0); for(int i = 0; i < n; i++)w[i + 1] += w[i]; ca.insert(ca.end(), w.begin(), w.end()); s.insert(s.begin(), 0); for(int i = 0; i < m; i++) s[i + 1] += s[i]; ca.insert(ca.end(), s.begin(), s.end()); sort(ca.begin(), ca.end()); ca.erase(unique(ca.begin(), ca.end()), ca.end()); LiChaoTree LCT(0, ca.back() + 1); auto f = [&](ll v){ return v * v + LCT.get(v); }; int S = 0, pS = 0; ll mx = 0; for(int i = 0, si = 0, wi = 0; i < ca.size(); i++){ while(wi <= n && w[wi] == ca[i]){ wi++; S ^= 1; } while(si <= m && s[si] == ca[i]){ si++; S ^= 2; } mx = max(mx, f(ca[i])); if((S & 1) && ((S ^ pS) & 1)){ int wj = wi; while(wj + 1 <= n && w[wj + 1] == w[wj]) wj++; LCT.add_segment_line(-2 * ca[i], (ll)(ca[i]) * ca[i] + mx, 0, w[wj] + 1); } if((S & 2) && ((S ^ pS) & 2)){ int sj = si; while(sj + 1 <= m && s[sj + 1] == s[sj]) sj++; LCT.add_segment_line(-2 * ca[i], (ll)(ca[i]) * ca[i] + mx, 0, s[sj] + 1); } pS = S; } cout << mx << '\n'; }