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 ll long long
#define X first
#define Y second
#define FOR(i,a,b) for(int i = a ; i <= b ; ++i)
const int N = 2e5 + 1;
typedef pair<int,int> ii;
set<ii> S1;
set<int> S2;
int a[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
ll ans = 0;
FOR(i,1,n) {
cin >> a[i];
S1.insert({a[i],i});
S2.insert(i);
}
S2.insert(0);
S2.insert(N);
FOR(i,1,(n + 1) / 2) {
int j = (*--S1.end()).Y;
cout << (ans += a[j]) << "\n";
S1.erase(ii(a[j],j));
S2.erase(j);
a[j] = -a[j];
auto L = S2.upper_bound(j);
auto R = L--;
int cnt = 0;
if (*L >= 1) {
a[j] += a[*L];
S1.erase(ii(a[*L],*L));
S2.erase(*L);
cnt++;
}
if (*R <= n) {
a[j] += a[*R];
S1.erase(ii(a[*R],*R));
S2.erase(*R);
cnt++;
}
if (cnt == 2)
S1.insert(ii(a[j],j)),
S2.insert(j);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |