제출 #1050081

#제출 시각아이디문제언어결과실행 시간메모리
1050081NeroZeinJobs (BOI24_jobs)C++17
14 / 100
25 ms16688 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define debug(...) #endif const int N = 1e5 + 5; int x[N]; int p[N]; bool vis[N]; bool taken[N]; vector<int> g[N]; long long sum[N]; long long need[N]; long long nrsum[N]; long long min_suf[N]; vector<pair<long long, long long>> vec; void dfs(int v) { vis[v] = true; sum[v] += x[v]; nrsum[v] += x[v]; min_suf[v] = min(0LL, min_suf[v] + x[v]); need[v] = min(need[v], min_suf[v]); if (nrsum[v] > 0) { vec.push_back({-need[v], nrsum[v]}); } //debug(v, need[v]); for (int u : g[v]) { sum[u] = sum[v]; nrsum[u] = min(0LL, nrsum[v]); min_suf[u] = min_suf[v]; need[u] = need[v]; dfs(u); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; long long s; cin >> n >> s; for (int i = 1; i <= n; ++i) { cin >> x[i] >> p[i]; g[p[i]].push_back(i); } for (int i = 1; i <= n; ++i) { if (p[i] == 0) { dfs(i); } } sort(vec.begin(), vec.end()); long long cur = s; for (auto [needed, profit] : vec) { //debug(needed, profit); if (cur >= needed) { cur += profit; } } cout << cur - s << '\n'; 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...