제출 #1011708

#제출 시각아이디문제언어결과실행 시간메모리
1011708ttamxJobs (BOI24_jobs)C++17
0 / 100
345 ms92760 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; auto it=mp.find(x); if(it!=mp.begin()){ auto x=prev(it),y=it; if(x->first+x->second>=y->first){ it--; x->second+=y->second; mp.erase(y); } } for(auto x=it,y=next(it);y!=mp.end()&&x->first+x->second>=y->first;y=mp.erase(y)){ x->second+=y->second; } } int size(){ return mp.size(); } void pop(ll x){ ll sum=-x; for(auto it=mp.begin();it!=mp.end()&&(it->first<x||sum<0);it=mp.erase(it)){ sum+=it->second; } insert(0,0); if(sum>0)insert(x,sum); } }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); cout << prev(dp[0].mp.upper_bound(s))->second; }
#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...