Submission #994921

#TimeUsernameProblemLanguageResultExecution timeMemory
994921pavementJobs (BOI24_jobs)C++17
0 / 100
193 ms47944 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 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], p = x[u];
		while (!pq[u].empty() && r >= 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(r, nr - p);
			p += np;
		}
		if (p >= 0) {
			pq[u].emplace(r, p);
		}
	}
}

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...