제출 #1032749

#제출 시각아이디문제언어결과실행 시간메모리
1032749AbitoJobs (BOI24_jobs)C++17
29 / 100
136 ms42424 KiB
#include <bits/stdc++.h> #define F first #define S second #define pb push_back #define ppb pop_back #define ep insert #define endl '\n' #define elif else if #define pow pwr #define sqrt sqrtt #define int long long #define ll long long #define y1 YONE typedef unsigned long long ull; using namespace std; const int N=3e5+5; int n,a[N],p[N],par[N]; vector<int> adj[N]; vector<vector<int>> v; void getp(int x){ p[x]+=a[x]; for (auto u:adj[x]){ p[u]+=p[x]; getp(u); }return; } void dfs(int x,int mn,int s){ if (p[x]>p[s]){ v.pb({mn-p[s],p[x]-p[s],x}); return; } mn=min(mn,p[x]); for (auto u:adj[x]) dfs(u,mn,s); return; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); cin>>n>>a[0]; for (int i=1;i<=n;i++){ cin>>a[i]>>par[i]; adj[par[i]].pb(i); } getp(0); int ans=0; priority_queue<vector<int>> pq; pq.push({0,a[0],0}); while (!pq.empty()){ if (ans+pq.top()[0]<0) break; ans+=pq.top()[1]; int x=pq.top()[2]; pq.pop(); dfs(x,0,x); for (auto u:v){ pq.push(u); } v.clear(); } cout<<ans-a[0]<<endl; 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...