Submission #1053085

#TimeUsernameProblemLanguageResultExecution timeMemory
1053085Jawad_Akbar_JJJobs (BOI24_jobs)C++17
29 / 100
210 ms31684 KiB
#include <iostream> #include <vector> #include <set> using namespace std; #define int long long const int N = 3e5 + 10; vector<int> vec[N]; int pr[N], erased[N], nxt[N]; set<pair<int,pair<int,int>>> st; void dfs(int u, int sum = 0, int mx = 0, bool St = 1){ if (erased[u] and !St) return; mx = max(mx, -sum); if (!St){ if (sum + pr[u] >= 0){ st.insert({mx, {sum + pr[u], u}}); return; } sum += pr[u]; } for (int i : vec[u]) dfs(i, sum, mx, 0); } signed main(){ int n, s; cin>>n>>s; int init = s; for (int i=1, dp;i<=n;i++){ cin>>pr[i]>>dp; vec[dp].push_back(i); nxt[i] = dp; } st.insert({0, {0, 0}}); while (st.size() > 0){ auto [req, p] = *begin(st); auto [prft, ind] = p; if (req > s) break; st.erase(begin(st)); if (erased[ind]) continue; s += prft; while (!erased[ind]){ erased[ind] = 1; dfs(ind); ind = nxt[ind]; } } cout<<s - init<<'\n'; }
#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...