Submission #995373

#TimeUsernameProblemLanguageResultExecution timeMemory
995373PagodePaivaJobs (BOI24_jobs)C++17
40 / 100
282 ms232516 KiB
#include<bits/stdc++.h> #define int long long using namespace std; const int N = 300010; vector <int> g[N]; vector <int> raizes; int val[N]; int pai[N]; int dp[N]; int dfs(int v, int p){ int con = 0; for(auto x : g[v]){ if(x == p) continue; dfs(x, v); con++; } if(con == 0) return dp[v] = max(0LL, val[v]); int vl = val[v]; for(auto x : g[v]){ if(x == p) continue; vl += dp[x]; } dp[v] = max(vl, 0LL); return dp[v]; } stack <pair <int, int>> s[N]; int32_t main(){ srand(time(0)); int n, c; cin >> n >> c; if(c >= 1e17){ for(int i = 1;i <= n;i++){ int p; cin >> val[i] >> p; if(p == 0) raizes.push_back(i); else g[p].push_back(i); } int res = 0; for(auto x : raizes){ res += dfs(x, x); } cout << res << '\n'; return 0; } vector<int> aux; int con = 0; for(int i = 1;i <= n;i++){ cin >> val[i] >> pai[i]; if(pai[i] == 0){ if(!aux.empty()){ reverse(aux.begin(), aux.end()); vector <pair <int, int>> res; int ac = 0; int mnac = 0; while(!aux.empty()){ // cout << aux.back() << ' ' << ac << '\n'; if(aux.back() + ac >= 0){ res.push_back({aux.back()+ac, mnac}); ac = 0; mnac = 0; } else{ ac += aux.back(); mnac = min(mnac, ac); } aux.pop_back(); } reverse(res.begin(), res.end()); for(auto x : res){ //cout << x.first << ' ' << x.second << '\n'; s[con].push(x); } con++; } } //cout << val[i] << ' '; aux.push_back(val[i]); } if(!aux.empty()){ reverse(aux.begin(), aux.end()); vector <pair <int, int>> res; int ac = 0; int mnac = 0; while(!aux.empty()){ //cout << aux.back() << ' '; if(aux.back() + ac >= 0){ //cout << aux.back() << ' '; res.push_back({aux.back()+ac, mnac}); ac = 0; mnac = 0; } else{ ac += aux.back(); mnac = min(mnac, ac); } aux.pop_back(); } reverse(res.begin(), res.end()); for(auto x : res){ // cout << x.first << ' ' << x.second << '\n'; s[con].push(x); } con++; } priority_queue <array <int, 3>> pq; for(int i = 0;i < con;i++){ if(s[i].empty()) continue; pq.push({s[i].top().second, s[i].top().first, i}); } int res = 0; while(!pq.empty()){ auto [mnac, valtot, i] = pq.top(); pq.pop(); // cout << mnac << ' '; if(c + mnac < 0) break; res += valtot; c += valtot; s[i].pop(); if(s[i].empty()) continue; pq.push({s[i].top().second, s[i].top().first, i}); } cout << res << '\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...