제출 #1350781

#제출 시각아이디문제언어결과실행 시간메모리
1350781marcogambaroJobs (BOI24_jobs)C++20
11 / 100
213 ms53472 KiB
#include <bits/stdc++.h>
using namespace std;
mt19937 timmy_loves_gambling(73);
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define readv(v) do { for(auto &i: v) cin >> i; } while(0)
#define writev(v) do { for(auto i: v) cout << i << " "; } while(0)
#define _ << " " <<
#define lf "\n"
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
template<typename... Args>
using vec = vector<Args...>;
#ifndef MARCO
bool dbg = 0;
#define infor(str, ...)
#define infof(str, ...)
#else
bool dbg = 1;
#define infor(str, ...) do { fprintf(stderr, str, ##__VA_ARGS__); } while(0)
#define infof(str, ...) do { fprintf(stderr, str "\n", ##__VA_ARGS__); } while(0)
#endif

vec<ll> P, X;
vec<vec<int>> G;
vec<priority_queue<pll>> pq;
ll s;
int N;

void dfs(int v) {
	int bigc = -1;
	for(auto &u: G[v]) {
		dfs(u);
		if(pq[u].size() == 0) continue;
		if(bigc == -1 || pq[u].size() > pq[bigc].size()) bigc = u;
	}

	if(bigc == -1) {
		if(X[v] <= 0) return;
		pq[v].emplace(-X[v], X[v]);
		return;
	}

	pq[v].swap(pq[bigc]);
	for(auto &u: G[v]) {
		if(u == bigc) continue;
		while(!pq[u].empty()) {
			pq[v].emplace(pq[u].top());
			pq[u].pop();
		}
	}

	ll x = X[v];
	ll need = -X[v];

	while(!pq[v].empty()) {
		auto [nd, g] = pq[v].top();
		nd *= -1;

		if(x >= 0) {
			if(nd > x) break;
			pq[v].pop();
			x += g;
		}
		else {
			need = max(need, nd + x);
			pq[v].pop();
			x += g;
		}
	}

	if(x <= 0) return;
	pq[v].emplace(-need, x);
}

signed main(){	
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);   
    
	cin >> N;
	P = X = vec<ll>(N+1);
	G.resize(N+1);
	pq.resize(N+1);
	cin >> X[0];
	for(int i=1; i<N+1; i++) {
		cin >> X[i] >> P[i];
	}

	for(int i=1; i<=N; i++) {
		G[P[i]].push_back(i);
	}

	dfs(0);
	cout << pq[0].top().se-X[0] << "\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...