Submission #1131356

#TimeUsernameProblemLanguageResultExecution timeMemory
1131356mmdrzadaWorst Reporter 4 (JOI21_worst_reporter4)C++20
79 / 100
322 ms99288 KiB
#include <bits/stdc++.h>


using namespace std;


typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
 
#define sep ' '
#define F first
#define S second
#define fastIO   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define kill(x) {cout << x << endl;return;}
#define sz(x) int(x.size())
#define SP(x) setprecision(x) 
#define mod(x) (1ll*x%M+M)%M
#define p_q priority_queue
#define REP(i, k) for (int i = 0 ; i < k ; i ++)
#define mid (l+r)/2

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5+10;
int n;
int a[N], r[N], c[N];
vi adj[N];
map<int, ll> mp[N];

void dfs(int v = 0) {
	for(int neigh: adj[v]) {
		dfs(neigh);
		if (mp[v].size() < mp[neigh].size()) swap(mp[v], mp[neigh]);
		for(auto [i1, i2]: mp[neigh])
			mp[v][i1] += i2;
	}
	auto it = mp[v].upper_bound(r[v]);
	int curr = c[v];
	mp[v][r[v]] += c[v];
	
	for(; it != mp[v].end();) {
		if (it->S <= curr) {
			auto tmp = it;
			curr -= it->S;
			it++;
			mp[v].erase(tmp);
		} else {
			it->S -= curr;
			break;
		}
	}
	
}

void solve() {
	cin >> n;
	ll s = 0;
	for(int i = 0 ; i < n ; i ++) {
		cin >> a[i] >> r[i] >> c[i]; a[i] --; s += c[i]; r[i] = -r[i];
		if (i != 0) adj[a[i]].pb(i);
	}
	// subtask 2
	
	dfs();
	for(auto [a, b]: mp[0]) {
		s -= b;
	}
	cout << s << endl;
	
}

int32_t main() {
	fastIO;
	
	solve();
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...