제출 #1053068

#제출 시각아이디문제언어결과실행 시간메모리
1053068Jawad_Akbar_JJJobs (BOI24_jobs)C++17
29 / 100
177 ms35664 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 and !erased[u]){ 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; s += prft; st.erase(begin(st)); while (1){ erased[ind] = 1; dfs(ind); if (erased[nxt[ind]]) break; 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...