#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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |