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;
typedef long long ll;
typedef pair<ll,ll> pii;
#define f first
#define s second
const ll inf = 1e18;
set<pii> s, ord;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n;
cin >> n;
for(int i=1; i<=n; i++){
int x;
cin >> x;
s.insert({-x, i});
ord.insert({i, -x});
}
s.insert({inf, 0}), s.insert({inf, n+1});
ord.insert({0, inf}), ord.insert({n+1, inf});
ll ans = 0;
for(int i=0; i<((n+1)>>1); i++){
pii v = *s.begin();
ans -= v.f;
cout << ans << endl;
auto it = ord.lower_bound({v.s, -inf});
--it;
pii a = *it;
s.erase(s.find({a.s, a.f}));
ord.erase(it++);
pii b = *it;
s.erase(s.find({b.s, b.f}));
ord.erase(it++);
pii c = *it;
s.erase(s.find({c.s, c.f}));
ord.erase(it++);
s.insert({a.s+c.s-b.s, b.f});
ord.insert({b.f, a.s+c.s-b.s});
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |