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>
#define int long long
using namespace std;
const int N = 2e5 + 5, INF = 5e18;
int n, a[N], ans;
set < int > alive;
set < pair < int, int > > q;
main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
q.insert(make_pair(a[i], i));
}
a[0] = a[n + 1] = -INF;
for(int i = 0; i <= n + 1; i++){
alive.insert(i);
}
for(int i = 1; i <= (n + 1) / 2; i++){
int pos = q.rbegin()->second,
val = q.rbegin()->first;
q.erase(--q.end());
alive.erase(pos);
ans += val;
a[pos] = -a[pos];
auto it = alive.upper_bound(pos);
if(it != alive.end()){
a[pos] += a[*it];
q.erase(make_pair(a[*it], *it));
alive.erase(it);
}
it = alive.upper_bound(pos);
if(it != alive.begin()){
it--;
a[pos] += a[*it];
q.erase(make_pair(a[*it], *it));
alive.erase(it);
}
alive.insert(pos);
q.insert(make_pair(a[pos], pos));
cout << ans << "\n";
}
}
Compilation message (stderr)
candies.cpp:18:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |