Submission #1127750

#TimeUsernameProblemLanguageResultExecution timeMemory
1127750vladiliusJobs (BOI24_jobs)C++20
14 / 100
886 ms1114112 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define pb push_back #define ff first #define ss second int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; ll s; cin>>n>>s; vector<int> g[n + 1], a(n + 1), p(n + 1); for (int i = 1; i <= n; i++){ cin>>a[i]>>p[i]; g[p[i]].pb(i); } vector<vector<bool>> gd(n + 1, vector<bool>(n + 1)); pair<ll, int> mx; int cc = 0; function<void(int, ll)> dfs = [&](int v, ll k){ k += a[v]; if ((s + k) < 0) return; gd[cc][v] = 1; mx = max(mx, {k, -v}); for (int i: g[v]){ if (i == p[v]) continue; dfs(i, k); } }; ll out = 0; while (true){ cc++; mx = {0, 0}; dfs(0, 0); for (int i = 1; i <= n; i++){ if (gd[cc - 1][i] > gd[cc][i]){ return 1; } } if (mx.ff <= 0) break; out += mx.ff; s += mx.ff; int v = -mx.ss; while (v > 0){ a[v] = 0; v = p[v]; } } cout<<out<<"\n"; }
#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...