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 mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
using ll = long long;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using ld = long double;
int N, A[200005];
ll ans;
multiset<pair<ll, ll> > s, s2;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i];
s2.emplace(A[i], i);
s.emplace(i, A[i]);
}
for (int j = 1; j <= (N + 1) / 2; j++) {
vector<set<pair<ll, ll> >::iterator> to_erase, to_erase2;
auto [x, idx] = *s2.rbegin();
ans += x;
auto it = s.find(mp(idx, x));
x = -x;
to_erase.pb(it);
to_erase2.pb(--s2.end());
++it;
if (it != s.end()) {
to_erase.pb(it);
to_erase2.pb(s2.find(mp(it->second, it->first)));
x += it->second;
}
--it;
if (it != s.begin()) {
--it;
to_erase.pb(it);
to_erase2.pb(s2.find(mp(it->second, it->first)));
x += it->second;
++it;
}
s.emplace(idx, x);
s2.emplace(x, idx);
for (auto it_2 : to_erase) {
s.erase(it_2);
}
for (auto it_2 : to_erase2) {
s2.erase(it_2);
}
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |