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 <iostream>
#include <set>
using namespace std;
long long int a[200005];
int main() {
long long int n, res = 0;
cin >> n;
set<int> s;
set<pair<long long int, int>> t;
for (int i = 0; i < n; i++) {
cin >> a[i];
s.insert(i);
t.insert({a[i], i});
}
for (int i = 1; i <= (n + 1) / 2; i++) {
long long int h = (*t.rbegin()).first, k = (*t.rbegin()).second;
res += h;
if (s.size() > 2) {
if (k == *s.begin()) {
t.erase({a[k], k}); s.erase(k);
int l = *s.begin();
t.erase({a[l], l}); s.erase(l);
}
else if (k == *s.rbegin()) {
t.erase({a[k], k}); s.erase(k);
int l = *s.rbegin();
t.erase({a[l], l}); s.erase(l);
}
else {
int l = *prev(s.lower_bound(k)), r = *s.upper_bound(k);
t.erase({a[l], l}); t.erase({a[r], r});
s.erase(l); s.erase(r);
t.erase({a[k], k});
a[k] = l + r - a[k];
t.insert({a[k], k});
}
}
cout << res << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |