#include<bits/stdc++.h>
//define endl '\n'
using namespace std;
const int MAXN = 2e5 + 10, MAXK = 1003;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int n, m, k;
int p[MAXN];
long long v[MAXN], d[MAXN], w[MAXN];
vector < int > g[MAXN];
pair < int, int > f[MAXN];
map < int, int > decode;
long long leaf[MAXN], total = 0;
int maxindex = 0;
void read()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; ++ i)
leaf[i] = 1;
for (int i = 2; i <= n; ++ i)
{
cin >> p[i];
leaf[p[i]] = 0;
g[p[i]].push_back(i);
}
bool no = 0;
long long total = 0;
vector < int > diffw;
for (int i = 1; i <= m; ++ i)
{
cin >> v[i] >> d[i] >> w[i];
diffw.push_back(d[i]);
if(!leaf[v[i]])no = 1;
total += w[i];
f[v[i]] = make_pair(d[i], w[i]);
}
sort(diffw.begin(), diffw.end());
int newid = 0, last = -1;
for(auto x: diffw)
{
if(last == x)continue;
newid ++;
decode[x] = newid;
last = x;
}
if(!no)
{
cout << total << endl;
exit(0);
}
for (int i = 1; i <= n; ++ i)
{
f[i].first = decode[f[i].first];
// cout << f[i].first << endl;
}
k = newid;
// cout << k << endl;
}
long long sub[MAXN][MAXK], dp[MAXN][MAXK], pref[MAXN][MAXK];
void dfs(int beg, int par)
{
int dd = f[beg].first;
if(dd)sub[beg][dd] += f[beg].second;
long long sum = 0;
for (auto nb: g[beg])
{
if(nb == par)continue;
dfs(nb, beg);
for (int i = 1; i <= k; ++ i)
sub[beg][i] += sub[nb][i];
}
for (int day = 1; day <= k; ++ day)
{
long long cost = 0;
if(day == dd)cost += f[beg].second;
long long sum = 0;
for (auto nb: g[beg])
cost += max(pref[nb][day], sub[nb][day]);
dp[beg][day] = max(dp[beg][day], cost);
pref[beg][day] = max(dp[beg][day], pref[beg][day-1]);
}
}
int main()
{
speed();
read();
if(k <= 1000)
{
long long ans = 0;
for (auto x: g[1])
{
dfs(x, 1);
ans = ans + pref[x][k];
}
cout << ans << endl;
return 0;
}
return 0;
}
/***
7 5 2
1 2 2 1 4 4
2 2 1
3 2 1
4 1 1
6 1 1
5 1 10
6 5 10
1 2 2 1 1
2 2 8
3 5 6
4 5 7
5 10 1
6 10 1
*/
Compilation message
magictree.cpp: In function 'void dfs(int, int)':
magictree.cpp:81:19: warning: unused variable 'sum' [-Wunused-variable]
81 | long long sum = 0;
| ^~~
magictree.cpp:69:15: warning: unused variable 'sum' [-Wunused-variable]
69 | long long sum = 0;
| ^~~
/tmp/ccPR3pHj.o: in function `__tcf_0':
magictree.cpp:(.text+0x389): relocation truncated to fit: R_X86_64_PC32 against symbol `g' defined in .bss section in /tmp/ccPR3pHj.o
/tmp/ccPR3pHj.o: in function `speed()':
magictree.cpp:(.text+0x3e2): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cin' defined in .bss._ZSt3cin section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
magictree.cpp:(.text+0x3ed): relocation truncated to fit: R_X86_64_PC32 against symbol `std::cout' defined in .bss._ZSt4cout section in /usr/lib/gcc/x86_64-linux-gnu/10/libstdc++.a(globals_io.o)
/tmp/ccPR3pHj.o: in function `dfs(int, int)':
magictree.cpp:(.text+0x419): relocation truncated to fit: R_X86_64_PC32 against symbol `f' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x437): relocation truncated to fit: R_X86_64_PC32 against symbol `sub' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x44e): relocation truncated to fit: R_X86_64_PC32 against symbol `g' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x46c): relocation truncated to fit: R_X86_64_PC32 against symbol `sub' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x4cf): relocation truncated to fit: R_X86_64_PC32 against symbol `k' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x504): relocation truncated to fit: R_X86_64_PC32 against symbol `sub' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x522): relocation truncated to fit: R_X86_64_PC32 against symbol `f' defined in .bss section in /tmp/ccPR3pHj.o
magictree.cpp:(.text+0x5ad): additional relocation overflows omitted from the output
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status