#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int N = 1000005;
const double INF = 1.e15;
struct lineinfo {
long long slope,yadd;
double x_left,x_right;
lineinfo() {}
lineinfo(long long slope,long long yadd,double x_left,double x_right):
slope(slope),yadd(yadd),x_left(x_left),x_right(x_right) {}
};
int n;
long long inp[N],A[N],S[N],D[N];
vector<lineinfo> convex;
long long find_val(long long x) {
int lo = 0,hi = (int)convex.size()-1,mid = -1;
while(lo <= hi) {
mid = (lo + hi)/2;
double x_left = convex[mid].x_left;
double x_right = convex[mid].x_right;
if(x_left <= x && x <= x_right) {
return
(long long)convex[mid].slope * x + convex[mid].yadd;
}
else {
if(x <= x_left) {
lo = mid+1;
}
else {
hi = mid-1;
}
}
}
}
int main(void) {
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
scanf("%d",&n);
for(int i = 0; i < n; i++) {
scanf("%lld",&inp[i]);
}
A[n-1] = inp[n-1];
for(int i = n-2; i >= 0; i--) A[i] = A[i+1] + inp[i];
S[n-1] = A[n-1];
for(int i = n-2; i >= 0; i--) S[i] = S[i+1] + A[i];
if(n == 1) {
printf("%lld\n",max(inp[0],0ll));
return 0;
}
// input data processed
convex.push_back(lineinfo(0,S[0],-INF,INF));
D[0] = max(inp[0],0ll);
for(int i = 1; i < n; i++) {
long long val = find_val(A[i+1]) - S[i+1] - (i+1)*A[i+1];
D[i] = max(D[i-1], max(D[i-1] + inp[i], val));
long long a = -i,b = S[i] + D[i-1];
while((int)convex.size() > 0) {
long long c = convex.back().slope,d = convex.back().yadd;
double intersect_x = (double)(b-d)/(c-a);
if(convex.back().x_left <= intersect_x &&
intersect_x <= convex.back().x_right) {
convex.back().x_left = intersect_x;
convex.push_back(
lineinfo(a,b,-INF,intersect_x)
);
break;
}
convex.pop_back();
}
/*
printf("bsearch res: %lld\n", bsearch(A[i+1]));
long long val = bsearch(A[i+1]) - S[i+1] - (i+1)*A[i+1];
printf("val: %lld\n", val);
D[i] = max(D[i-1],val);
printf("at %d: %lld\n", i, D[i]);
*/
}
long long ans = 0;
for(int i = 0; i < n; i++)
ans = max(ans,D[i]);
printf("%lld\n",ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
32460 KB |
Output is correct |
2 |
Incorrect |
0 ms |
32460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |