Submission #1086850

#TimeUsernameProblemLanguageResultExecution timeMemory
1086850IBoryJobs (BOI24_jobs)C++17
40 / 100
230 ms74956 KiB
#include <bits/stdc++.h> #define pii pair<ll, ll> typedef long long ll; using namespace std; const int MAX = 300003; vector<int> G[MAX]; priority_queue<pii, vector<pii>, greater<pii>> PQ[MAX]; pii S[MAX]; ll P[MAX]; void DFS(int cur) { for (int next : G[cur]) DFS(next); auto& [a, b] = S[cur]; while (!PQ[cur].empty()) { auto [c, d] = PQ[cur].top(); if (b < c) break; if (c < d) b = b + d - c; PQ[cur].pop(); } if (!cur) return; if (a < b) PQ[cur].push(S[cur]); else { while (!PQ[cur].empty() && a >= b) { auto [c, d] = PQ[cur].top(); PQ[cur].pop(); a += c - b; b = d; } if (a < b) PQ[cur].emplace(a, b); } if (PQ[cur].size() > PQ[P[cur]].size()) swap(PQ[cur], PQ[P[cur]]); while (!PQ[cur].empty()) { auto [c, d] = PQ[cur].top(); PQ[cur].pop(); PQ[P[cur]].emplace(c, d); } } int main() { ios::sync_with_stdio(0); cin.tie(0); ll N, IS; cin >> N >> IS; S[0] = {0, IS}; for (int i = 1; i <= N; ++i) { ll s; cin >> s >> P[i]; G[P[i]].push_back(i); if (s < 0) S[i] = {-s, 0}; else S[i] = {0, s}; } DFS(0); ll ans = S[0].second - IS; cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...