#include using namespace std; typedef long long ll; typedef pair < int, int > ii; const int N = 5e5 + 5; int n; int a[N]; ll pre[N], presq[N]; const ll is_query = -(1LL<<62); struct Line { ll m, b; mutable function succ; bool operator<(const Line& rhs) const { if (rhs.b != is_query) return m < rhs.m; const Line* s = succ(); if (!s) return 0; ll x = rhs.m; return b - s->b < (s->m - m) * x; } }; struct HullDynamic : public multiset { // will maintain upper hull for maximum bool bad(iterator y) { auto z = next(y); if (y == begin()) { if (z == end()) return 0; return y->m == z->m && y->b <= z->b; } auto x = prev(y); if (z == end()) return y->m == x->m && y->b <= x->b; return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m); } void insert_line(ll m, ll b) { auto y = insert({ m, b }); y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; if (bad(y)) { erase(y); return; } while (next(y) != end() && bad(next(y))) erase(next(y)); while (y != begin() && bad(prev(y))) erase(prev(y)); } ll eval(ll x) { auto l = *lower_bound((Line) { x, is_query }); return l.m * x + l.b; } }trick; int main() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", a + i); pre[i] = pre[i - 1] + a[i]; presq[i] = presq[i - 1] + (ll) a[i] * a[i]; } ll ans = 0; for(int r = 1; r <= n; r++) { ll c = pre[r] * pre[r] - presq[r]; trick.insert_line(-2*pre[r-1],presq[r-1]+pre[r-1]*pre[r-1]); ans = max(ans, trick.eval(pre[r])+c); // for(int l = 1; l <= r; l++) { // ans = max(ans, pre[l - 1] * pre[l - 1] - 2 * pre[r] * pre[l - 1] + presq[l - 1]); // } } printf("%lld\n", ans/2); return 0; }