Submission #962609

#TimeUsernameProblemLanguageResultExecution timeMemory
962609pavementCandies (JOI18_candies)C++17
100 / 100
453 ms30816 KiB
#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; ll ans, A[200005]; multiset<pair<ll, ll> > s, s2; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N; N += 2; A[1] = -(ll)1e16; for (int i = 2; i <= N - 1; i++) { cin >> A[i]; } A[N] = -(ll)1e16; for (int i = 1; i <= N; 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; } for (auto it_2 : to_erase) { s.erase(it_2); } for (auto it_2 : to_erase2) { s2.erase(it_2); } s.emplace(idx, x); s2.emplace(x, idx); cout << ans << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...