Submission #914822

#TimeUsernameProblemLanguageResultExecution timeMemory
914822Amirreza_FakhriCandies (JOI18_candies)C++17
0 / 100
3 ms2648 KiB
// In the name of the God #include <bits/stdc++.h> #define ll long long #define int long long #define pb push_back #define F first #define S second #define mp make_pair #define pii pair <int, int> #define smin(x, y) (x) = min((x), (y)) #define smax(x, y) (x) = max((x), (y)) #define all(x) (x).begin(), (x).end() using namespace std; const int inf = 1e9+7; const int mod = 998244353; const int maxn = 1e5+5; int n, a[maxn], pre[maxn], nxt[maxn]; set <pii, greater <pii> > st; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; st.insert(mp(a[i], i)); pre[i] = i-1, nxt[i] = i+1; } nxt[0] = 1, pre[n+1] = n; int ans = 0; for (int i = 0; i < (n+1)/2; i++) { int v = st.begin() -> F, j = st.begin() -> S; ans += v; cout << ans << '\n'; st.erase(st.begin()); if (pre[j] == 0) { st.erase(mp(a[nxt[j]], nxt[j])); pre[nxt[nxt[j]]] = 0; nxt[0] = nxt[nxt[j]]; } else if (nxt[j] == n+1) { st.erase(mp(a[pre[j]], pre[j])); pre[nxt[nxt[j]]] = 0; nxt[0] = nxt[nxt[j]]; } else { st.erase(mp(a[pre[j]], pre[j])), st.erase(mp(a[nxt[j]], nxt[j])); a[j] = a[pre[j]]+a[nxt[j]]-a[j]; nxt[pre[nxt[nxt[j]]] = j] = nxt[nxt[j]]; pre[nxt[pre[pre[j]]] = j] = pre[pre[j]]; st.insert(mp(a[j], j)); } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...