This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<set>
#include<algorithm>
typedef long long ll;
typedef std::pair<ll, ll> pl;
#define X first
#define Y second
const int MX = 1000010;
int n, a[MX];
ll s[MX], ss[MX];
ll d[MX];
struct lines{
pl p[MX];
int f;
void init(){ f = 0; }
void push(pl x){
while (f>1 && (double)(p[f - 1].X - p[f - 2].X) * (x.Y - p[f - 1].Y) >= (double)(x.X - p[f - 1].X) * (p[f - 1].Y - p[f - 2].Y))--f;
p[f++] = x;
}
ll get(ll x){
ll ret = 0;
for (int i = 0; i < f; i++)ret = std::max(ret, x * p[i].X + p[i].Y);
return ret;
}
}Lin;
int main(){
scanf("%d", &n);
for (int i = 1; i <= n; i++)scanf("%d", a + i);
for (int i = 1; i <= n; i++)s[i] = s[i - 1] + a[i];
for (int i = 1; i <= n; i++)ss[i] = ss[i - 1] + s[i];
Lin.init();
Lin.push(pl(0, 0));
for (int i = 1; i <= n; i++){
d[i] = Lin.get(s[i]) + s[i] * i - ss[i-1];
if (d[i] < d[i - 1])d[i] = d[i - 1];
Lin.push(pl(-i, d[i] + ss[i - 1]));
//printf("%d : %d\n", i, d[i]);
}
printf("%lld", d[n]);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |