1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<bits/stdc++.h> using namespace std; typedef double db; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define fi first #define se second #define pb push_back #define sz(x) ((int)(x).size()) #define all(x) begin(x),end(x) #define rep(i,l,r) for(int i=(l);i<(r);++i) #define per(i,l,r) for(int i=(r)-1;i>=(l);--i) #define dd(x) cout << #x << "=" << x << ", " #define de(x) cout << #x << "=" << x << endl
const int N = 2e5 + 7; int n, a[N]; ll S[N], V[N]; #define X(i) (i) #define Y(i) (i * S[i] - V[i]) struct P { ll x, y; P() {} P(ll _x, ll _y) { x = _x, y = _y; } P operator-(const P &p) const { return P(x - p.x, y - p.y); } ll operator^(const P &p) const { return x * p.y - y * p.x; } }; bool chk(deque<P> &Q, int i, ll G) { return (Q[i + 1].y - Q[i].y) >= (Q[i + 1].x - Q[i].x) * G; } int main() { scanf("%d", &n); rep(i, 1, n + 1) scanf("%d", a + i); rep(i, 1, n + 1) S[i] = S[i - 1] + a[i]; rep(i, 1, n + 1) V[i] = V[i - 1] + i * a[i]; deque<P> Q; Q.push_back(P(X(0), Y(0))); ll ans = LLONG_MIN; rep(i, 1, n + 1) { int l = 0, r = max(0, sz(Q) - 2); while (l + 1 < r) { int z = (l + r) >> 1; chk(Q, z, S[i]) ? l = z : r = z; } int j = l; if (chk(Q, r, S[i])) j = r + 1; else if (chk(Q, l, S[i])) j = l + 1; else j = l; ll f = Q[j].y - S[i] * Q[j].x + V[i]; ans = max(ans, f); P a(X(i), Y(i)); while (sz(Q) > 1 && ((Q[sz(Q) - 2] - a) ^ (Q.back() - a)) >= 0) Q.pop_back(); Q.push_back(a); } printf("%lld", ans); return 0; }
|