#include <bits/stdc++.h>
// mrrrow meeow :3
// go play vivid/stasis now! it's free on steam
#define fo(i, a, b) for (auto i = (a); i < (b); i++)
#define of(i, a, b) for (auto i = (b); i-- > (a);)
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define be(a) a.begin(), a.end()
using namespace std;
int ____init = [] {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    return 0;
}();
vector<int> adj[100000];
pair<int, int> fruit[100000];
map<int, long long> dfs(int i) {
    map<int, long long> res = {{0, 0}};
    for (int j : adj[i]) {
        map<int, long long> b = dfs(j);
        if (res.size() < b.size()) swap(res, b);
        for (auto [d, v] : b) res[d] += v;
    }
    res[fruit[i].f] += fruit[i].s;
    long long curr = fruit[i].s;
    for (auto it = res.ub(fruit[i].f); it != res.end(); res.erase(it++)) {
        curr -= it->s;
        if (curr < 0) {
            it->s = -curr;
            break;
        }
    }
    return res;
}
int main() {
    int n, m, k;
    cin >> n >> m >> k;
    fo(i, 1, n) {
        int p;
        cin >> p;
        adj[--p].pb(i);
    }
    fo(_, 0, m) {
        int v, d, w;
        cin >> v >> d >> w;
        fruit[--v] = {d, w};
    }
    map<int, long long> res_map = dfs(0);
    long long res = 0;
    for (auto [_, v] : res_map) res += v;
    cout << res % 998244353 << endl;
}
| # | 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... |