Submission #1075645

#TimeUsernameProblemLanguageResultExecution timeMemory
1075645adaawfCandies (JOI18_candies)C++17
0 / 100
3 ms600 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...