#include #include #include #include //#include #include #include using namespace std; //ifstream cin ("x.in"); ofstream cout ("x.out"); typedef long long i64; const int nmax = 5e5; const i64 inf = (1LL << 62) - 1 + (1LL << 62); i64 v[nmax + 1]; i64 p2[nmax + 1], s[nmax + 1]; i64 val[nmax + 1]; map< i64, int > mp; struct functie { i64 a, b; functie () { a = -inf; } functie (i64 _a, i64 _b) { a = _a, b = _b; } i64 val (i64 x) { if (a == -inf) return -inf; else return a * x + b; } } aint[4 * nmax + 1]; functie query (int nod, int x, int y, int pos) { if (x == y) { return aint[ nod ]; } int mij = (x + y) / 2; functie aux; if (pos <= mij) { aux = query(2 * nod, x, mij, pos); } else { aux = query(2 * nod + 1, mij + 1, y, pos); } if (aux.val(val[ pos ]) > aint[ nod ].val(val[ pos ])) return aux; return aint[ nod ]; } void update (int nod, int x, int y, functie f) { i64 a = f.val(val[ x ]), b = f.val(val[ y ]); i64 A = aint[ nod ].val(val[ x ]), B = aint[ nod ].val(val[ y ]); if (a >= A && b >= B) { aint[ nod ] = f; return ; } else if (a < A && b < B) { return ; } int mij = (x + y) / 2; update(2 * nod, x, mij, f); update(2 * nod + 1, mij + 1, y, f); } int main() { int n; cin >> n; for (int i = 1; i <= n; ++ i) { cin >> v[ i ]; s[ i ] = s[i - 1] + v[ i ]; p2[ i ] = p2[i - 1] + v[ i ] * v[ i ]; mp[-2 * s[ i ]] = 1; } int ind = 0; for (auto &i : mp) { i.second = ++ ind; val[ ind ] = i.first; } long long ans = -inf; if (n <= 5000) { for (int i = 1; i <= n; ++ i) { for (int j = 0; j < i; ++ j) { ans = max(ans, ((s[ i ] - s[ j ]) * (s[ i ] - s[ j ]) - (p2[ i ] - p2[ j ])) / 2); } } } else { update(1, 1, n, functie(0, 0)); for (int i = 1; i <= n; ++ i) { /// aflu functie f = query(1, 1, n, mp[-2 * s[ i ]]); ans = max(ans, (s[ i ] * s[ i ] - p2[ i ] - 2 * s[ i ] * f.a + f.b) / 2); //update update(1, 1, n, functie(s[ i ], s[ i ] * s[ i ] + p2[ i ])); } } cout << ans << "\n"; return 0; }