#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NAME"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
	if (fopen(FNAME".inp","r")){
		freopen(FNAME".inp","r",stdin);
		freopen(FNAME".out","w",stdout);
	}
}
const int MAXN = 1e5 + 5;
int n,m,K;
vector<int> graph[MAXN];
vector<long long> juice;
vector<int> ripeDay;
void init(){
	cin>>n>>m>>K;
	ripeDay.assign(n+1,0);
	juice.assign(n+1,0);
	for (int i=2;i<=n;i++){
		int p; cin>>p;
		graph[p].push_back(i);
	}
	for (int i=0;i<m;i++){
		int u,d,w;
		cin>>u>>d>>w;
		ripeDay[u] = d;
		juice[u] = w;
	}
}
#define pntphw true
// ? map<day,juice>
void dfs(int u,map<int,long long> &large){
	for (int v : graph[u]){
		map<int,long long> smol;
		dfs(v,smol);
		if (large.size() < smol.size()) large.swap(smol);
		for (const pair<int,long long> &p : smol)
			large[p.first] += p.second;
	}
	if (juice[u]){
		int day = ripeDay[u];
		large[day] += juice[u];
		long long left = juice[u];
		map<int,long long>::iterator it = large.find(day);
		while (pntphw){
			map<int,long long>::iterator nxt = next(it);
			if (nxt==large.end()) break;
			if (left >= nxt->second){
				left -= nxt->second;
				large.erase(nxt);
			} else{
				nxt->second -= left;
				break;
			}
		}
	}
}
void sol(){
	map<int,long long> large;
	dfs(1,large);
	long long res = 0;
	for (const pair<int,long long> &p : large)
		res += p.second;
	cout<<res;
}
int main(){
    setup();
    init();
    sol();
}
컴파일 시 표준 에러 (stderr) 메시지
magictree.cpp: In function 'void setup()':
magictree.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen(FNAME".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen(FNAME".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |