Submission #1066689

#TimeUsernameProblemLanguageResultExecution timeMemory
1066689NeroZeinJobs (BOI24_jobs)C++17
40 / 100
175 ms57424 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define debug(...) #endif const int N = 3e5 + 5; int up[N]; int dep[N]; bool active[N]; int a[N], p[N]; set<int> roots; long long dp[N]; vector<int> g[N]; long long needed[N]; long long min_suf[N]; int get(int v) { return (v == 0 ? 0 : dp[v] <= 0 ? v : up[v] = get(up[v])); } void dfs(int v, long long sum) { sum += a[v]; min_suf[v] = min(0LL, min_suf[v] + a[v]); needed[v] = min(needed[v], min_suf[v]); for (int u : g[v]) { dep[u] = dep[v] + 1; min_suf[u] = min_suf[v]; needed[u] = needed[v]; dfs(u, sum); } } void activate(int v) { dp[v] += a[v]; active[v] = true; //debug(v); while (true) { //debug(v, dp[v]); if (v == 0 || dp[v] <= 0) { //cout << '\n'; return; } if (p[v] == 0 && dp[v] > 0) { roots.insert(v); } int nv = get(v); dp[nv] += dp[v]; v = nv; } //cout << '\n'; } void dfs2(int v, long long& s) { assert(dp[v] >= 0); s += a[v]; //debug(v, a[v], s, needed[v]); for (int u : g[v]) { if (dp[u] > 0 && active[u] == true) { dfs2(u, s); } else { p[u] = 0;//new root. } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; long long s; cin >> n >> s; for (int i = 1; i <= n; ++i) { cin >> a[i] >> p[i]; g[p[i]].push_back(i); } for (int i = 1; i <= n; ++i) { up[i] = p[i]; if (p[i] == 0) { dfs(i, 0); } } vector<int> ord(n); iota(ord.begin(), ord.end(), 1); sort(ord.begin(), ord.end(), [&](int i, int j) { return (needed[i] == needed[j] ? dep[i] > dep[j] : needed[i] < needed[j]); }); long long curr = s; while (true) { while (!ord.empty() && -needed[ord.back()] <= curr) { int v = ord.back(); ord.pop_back(); activate(v); } if (roots.empty()) { break; } while (!roots.empty()) { int v = *roots.begin(); roots.erase(v); dfs2(v, curr); } } cout << curr - s << '\n'; 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...