# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45133 | ikura355 | Candies (JOI18_candies) | C++14 | 197 ms | 13320 KiB |
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;
const int maxn = 2e5 + 5;
struct node {
int x,y;
long long val;
node(int _x = 0, int _y = 0, long long _val = 0) {
x = _x; y = _y; val = _val;
}
bool operator < (node a) const {
return a.val>val;
}
};
int n;
long long a[maxn];
int l[maxn], r[maxn];
long long sum[maxn], lval[maxn], rval[maxn];
priority_queue<node> heap;
int main() {
scanf("%d",&n); n += 2;
for(int i=2;i<=n-1;i++) scanf("%lld",&a[i]);
a[1] = a[n] = -1e16;
for(int i=1;i<=n;i++) sum[i] = sum[i-1] + a[i];
for(int i=0;i<=n;i++) {
l[i] = r[i] = -1;
if(i!=0) l[i] = i-1, lval[i] = sum[i]-sum[i-1];
if(i!=n) r[i] = i+1, rval[i] = sum[i+1]-sum[i];
if(i) heap.push(node(i-1,i,sum[i]-sum[i-1]));
}
int cur = (n-2+1)/2;
long long ans = 0;
while(!heap.empty() && cur) {
node t = heap.top(); heap.pop();
int x = t.x, y = t.y;
long long val = t.val;
if(r[x]!=y || l[y]!=x) continue;
ans += val;
int L = l[x], R = r[y];
if(L!=-1 && R!=-1) {
long long nval = lval[x] + rval[y] - val;
r[L] = R; l[R] = L;
rval[L] = lval[R] = nval;
heap.push(node(L,R,nval));
}
printf("%lld\n",ans);
cur--;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |