Submission #1015020

#TimeUsernameProblemLanguageResultExecution timeMemory
101502012345678Jobs (BOI24_jobs)C++17
100 / 100
323 ms112676 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int nx=3e5+5; ll n, s, x[nx], pa[nx], res; vector<ll> d[nx]; multiset<pair<ll, ll>> ms[nx]; void dfs(int u) { for (auto v:d[u]) { dfs(v); if (ms[v].size()>ms[u].size()) swap(ms[v], ms[u]); for (auto y:ms[v]) ms[u].insert(y); } if (x[u]>=0) ms[u].insert({0, x[u]}); else { ll mn=x[u], vl=x[u]; while (vl<0&&!ms[u].empty()) { vl-=ms[u].begin()->first; mn=min(mn, vl); vl+=ms[u].begin()->first+ms[u].begin()->second; ms[u].erase(ms[u].begin()); } if (vl>=0) { mn=-mn; while (!ms[u].empty()&&ms[u].begin()->first<=mn) vl+=ms[u].begin()->second, ms[u].erase(ms[u].begin()); ms[u].insert({mn, vl}); } } } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>s; for (int i=1; i<=n; i++) cin>>x[i]>>pa[i], d[pa[i]].push_back(i); dfs(0); for (auto t:ms[0]) if (s+res>=t.first) res+=t.second; cout<<res; }
#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...