Submission #1011732

#TimeUsernameProblemLanguageResultExecution timeMemory
1011732ttamxJobs (BOI24_jobs)C++17
11 / 100
203 ms71584 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N=3e5+5; int n; ll s; ll a[N]; vector<int> adj[N]; struct DP{ map<ll,ll> mp; void insert(ll x,ll d){ mp[x]+=d; } int size(){ return mp.size(); } void pop(ll x){ auto it=mp.begin(); ll cur=-x; ll last=x; for(;it!=mp.end()&&cur<0;it=mp.erase(it)){ last=max(last,it->first-cur); cur+=it->second; } for(;it!=mp.end();it=mp.erase(it)){ cur+=it->second; } if(cur>0)insert(last,cur); insert(0,0); } }dp[N]; void dfs(int u){ for(auto v:adj[u]){ dfs(v); if(dp[v].size()>dp[u].size())swap(dp[u],dp[v]); for(auto [x,y]:dp[v].mp)dp[u].insert(x,y); } if(a[u]>=0)dp[u].insert(0,a[u]); else dp[u].pop(-a[u]); } int main(){ cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> s; for(int i=1;i<=n;i++){ int p; cin >> a[i] >> p; adj[p].emplace_back(i); } dfs(0); ll ans=0; for(auto [x,y]:dp[0].mp)if(x<=s)s+=y,ans+=y; cout << ans; }
#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...