제출 #994949

#제출 시각아이디문제언어결과실행 시간메모리
994949pavementJobs (BOI24_jobs)C++17
69 / 100
187 ms55380 KiB
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back using ll = long long; using ill = pair<int, ll>; int N, x[300005]; ll s, profit; vector<int> adj[300005]; priority_queue<ill, vector<ill>, greater<ill> > pq[300005]; void print(priority_queue<ill, vector<ill>, greater<ill> > tmp) { while (!tmp.empty()) { auto [r, p] = tmp.top(); cout << r << ' ' << p << '\n'; tmp.pop(); } } void dfs(int u) { for (auto v : adj[u]) { dfs(v); if (pq[u].size() < pq[v].size()) { swap(pq[u], pq[v]); } while (!pq[v].empty()) { pq[u].push(pq[v].top()); pq[v].pop(); } } if (x[u] >= 0) { pq[u].emplace(0, x[u]); } else { int r = -x[u]; ll p = x[u]; while (!pq[u].empty() && r + p >= pq[u].top().first) { p += pq[u].top().second; pq[u].pop(); } while (!pq[u].empty() && p < 0) { auto [nr, np] = pq[u].top(); r = max((ll)r, nr - p); p += np; pq[u].pop(); } if (p >= 0) { pq[u].emplace(r, p); } } //~ cout << "PRINTING " << u << '\n'; //~ print(pq[u]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> s; for (int i = 1, p; i <= N; i++) { cin >> x[i] >> p; adj[p].pb(i); } dfs(0); while (!pq[0].empty() && s >= pq[0].top().first) { s += pq[0].top().second; profit += pq[0].top().second; pq[0].pop(); } cout << profit << '\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...