제출 #825185

#제출 시각아이디문제언어결과실행 시간메모리
825185NK_Magic Tree (CEOI19_magictree)C++17
47 / 100
2078 ms29704 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using ll = long long;
using vl = V<ll>;

using T = map<int, ll>;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M, K; cin >> N >> M >> K;

	V<vi> chd(N); for(int u = 1; u < N; u++) {
		int p; cin >> p; --p;
		chd[p].pb(u);
	}

	vi W(N, 0), D(N, -1);

	for(int i = 0; i < M; i++) {
		int v, d, w; cin >> v >> d >> w; --v, --d;
		D[v] = d, W[v] = w;
	}
	
	function<T(int)> dfs = [&](int u) {
		T R;
		for(auto& v : chd[u]) {
			T r = dfs(v);
			for(auto p : r) R[p.first] += p.second;
		}		

		if (D[u] != -1) {
			R[D[u]] += W[u];
			ll amt = 0;
			while(1) {
				auto it = R.upper_bound(D[u]);
				if (it == end(R)) break;

				amt += it->s;
				if (W[u] < amt) { it->s = amt - W[u]; break; }
				else R.erase(it);
			}
		}

		return R;
	};

	T ans = dfs(0);
	ll ANS = 0;
	for(auto p : ans) ANS += p.second;
	cout << ANS << nl;
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...