This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define MAXN 1000006
typedef long long lld;
const double eps = 1e-7;
int N;
int A[MAXN];
int stk[MAXN], scnt; double cp[MAXN];
lld D[MAXN], Nu[MAXN], S[MAXN];
inline lld get_y0(int i)
{ return D[i] - Nu[i] + S[i] * i; }
inline double cross_point(int a, int b)
{
lld q1 = get_y0(a), q2 = get_y0(b);
return (double)(q1 - q2) / (b - a);
}
int main()
{
scanf("%d", &N);
for (int i=1;i<=N;i++) scanf("%d", A+i);
for (int i=1;i<=N;i++) Nu[i] = Nu[i-1] + (lld)i * A[i], S[i] = S[i-1] + A[i];
lld ans = 0;
stk[++scnt] = 0; cp[scnt] = -1e17;
for (int i=1;i<=N;i++){
int j = stk[upper_bound(cp+1, cp+scnt+1, -S[i] + eps) - cp - 1];
D[i] = max(D[i-1], get_y0(j) - S[i] * j + Nu[i]);
ans = max(ans, D[i]);
while (scnt > 1 && cross_point(stk[scnt-1], i) < cp[scnt] + eps) scnt--;
stk[++scnt] = i; cp[scnt] = cross_point(stk[scnt-1], i);
}
printf("%lld\n", ans);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |