/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e5 + 10, maxk = 21;
int n, m, k, p[maxn], d[maxn], sub[maxn];
ll w[maxn];
///ll diff[maxn][maxk];
vector < int > adj[maxn];
map < int, ll > dp[maxn];
void dfs(int v)
{
sub[v] = 1;
int heavy = -1;
for (int u : adj[v])
{
dfs(u);
sub[v] += sub[u];
if (heavy == -1 || sub[u] > sub[heavy])
heavy = u;
///for (int j = 0; j <= k; j ++)
///diff[v][j] += diff[u][j];
}
if (heavy != -1)
swap(dp[v], dp[heavy]);
for (int u : adj[v])
{
if (u == heavy)
continue;
for (pair < int, ll > cur : dp[u])
{
dp[v][cur.first] += cur.second;
}
}
if (d[v] != 0)
{
dp[v][d[v] - 1] += w[v];
dp[v][d[v]] -= w[v];
map < int, ll > :: iterator it, hp;
ll cur = d[v];
while(true)
{
it = dp[v].lower_bound(cur);
if (it -> second >= 0)
break;
if (next(it) == dp[v].end())
{
dp[v][it -> first] = 0;
break;
}
hp = next(it);
dp[v][hp -> first] += it -> second;
dp[v].erase(it);
}
/**diff[v][d[v] - 1] += w[v];
diff[v][d[v]] -= w[v];
int pt = d[v];
while(pt < k)
{
if (diff[v][pt] < 0)
{
diff[v][pt + 1] += diff[v][pt];
diff[v][pt] = 0;
pt ++;
}
else
{
break;
}
}*/
}
/**for (int j = 0; j < k; j ++)
{
cout << diff[v][j] << " ";
}
cout << endl;*/
}
void solve()
{
cin >> n >> m >> k;
for (int i = 2; i <= n; i ++)
{
cin >> p[i];
adj[p[i]].push_back(i);
}
for (int i = 1; i <= m; i ++)
{
int v;
cin >> v >> d[v] >> w[v];
}
dfs(1);
ll ans = 0;
for (int j = 0; j < k; j ++)
ans = ans + dp[1][j];
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
/**
6 4 10
1
2
1
4
4
3 3 4
4 2 5
5 5 1
6 3 9
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7292 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
30164 KB |
Output is correct |
2 |
Correct |
75 ms |
27340 KB |
Output is correct |
3 |
Correct |
231 ms |
59252 KB |
Output is correct |
4 |
Correct |
131 ms |
29232 KB |
Output is correct |
5 |
Correct |
167 ms |
31256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7620 KB |
Output is correct |
3 |
Correct |
4 ms |
7632 KB |
Output is correct |
4 |
Correct |
107 ms |
34624 KB |
Output is correct |
5 |
Correct |
138 ms |
34616 KB |
Output is correct |
6 |
Correct |
142 ms |
34652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
17624 KB |
Output is correct |
2 |
Correct |
101 ms |
16792 KB |
Output is correct |
3 |
Correct |
96 ms |
19596 KB |
Output is correct |
4 |
Correct |
82 ms |
20948 KB |
Output is correct |
5 |
Correct |
95 ms |
26508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7292 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
115 ms |
22928 KB |
Output is correct |
11 |
Correct |
105 ms |
21088 KB |
Output is correct |
12 |
Correct |
82 ms |
19604 KB |
Output is correct |
13 |
Correct |
72 ms |
20964 KB |
Output is correct |
14 |
Correct |
80 ms |
26480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12108 KB |
Output is correct |
2 |
Correct |
45 ms |
15560 KB |
Output is correct |
3 |
Correct |
47 ms |
17344 KB |
Output is correct |
4 |
Correct |
47 ms |
17508 KB |
Output is correct |
5 |
Correct |
29 ms |
16176 KB |
Output is correct |
6 |
Correct |
46 ms |
20316 KB |
Output is correct |
7 |
Correct |
48 ms |
24304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7292 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7620 KB |
Output is correct |
12 |
Correct |
4 ms |
7632 KB |
Output is correct |
13 |
Correct |
107 ms |
34624 KB |
Output is correct |
14 |
Correct |
138 ms |
34616 KB |
Output is correct |
15 |
Correct |
142 ms |
34652 KB |
Output is correct |
16 |
Correct |
115 ms |
22928 KB |
Output is correct |
17 |
Correct |
105 ms |
21088 KB |
Output is correct |
18 |
Correct |
82 ms |
19604 KB |
Output is correct |
19 |
Correct |
72 ms |
20964 KB |
Output is correct |
20 |
Correct |
80 ms |
26480 KB |
Output is correct |
21 |
Correct |
39 ms |
13712 KB |
Output is correct |
22 |
Correct |
106 ms |
28060 KB |
Output is correct |
23 |
Correct |
137 ms |
38048 KB |
Output is correct |
24 |
Correct |
218 ms |
53504 KB |
Output is correct |
25 |
Correct |
117 ms |
28624 KB |
Output is correct |
26 |
Correct |
166 ms |
30828 KB |
Output is correct |
27 |
Correct |
186 ms |
28756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7292 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
115 ms |
30164 KB |
Output is correct |
11 |
Correct |
75 ms |
27340 KB |
Output is correct |
12 |
Correct |
231 ms |
59252 KB |
Output is correct |
13 |
Correct |
131 ms |
29232 KB |
Output is correct |
14 |
Correct |
167 ms |
31256 KB |
Output is correct |
15 |
Correct |
4 ms |
7508 KB |
Output is correct |
16 |
Correct |
4 ms |
7620 KB |
Output is correct |
17 |
Correct |
4 ms |
7632 KB |
Output is correct |
18 |
Correct |
107 ms |
34624 KB |
Output is correct |
19 |
Correct |
138 ms |
34616 KB |
Output is correct |
20 |
Correct |
142 ms |
34652 KB |
Output is correct |
21 |
Correct |
111 ms |
17624 KB |
Output is correct |
22 |
Correct |
101 ms |
16792 KB |
Output is correct |
23 |
Correct |
96 ms |
19596 KB |
Output is correct |
24 |
Correct |
82 ms |
20948 KB |
Output is correct |
25 |
Correct |
95 ms |
26508 KB |
Output is correct |
26 |
Correct |
115 ms |
22928 KB |
Output is correct |
27 |
Correct |
105 ms |
21088 KB |
Output is correct |
28 |
Correct |
82 ms |
19604 KB |
Output is correct |
29 |
Correct |
72 ms |
20964 KB |
Output is correct |
30 |
Correct |
80 ms |
26480 KB |
Output is correct |
31 |
Correct |
17 ms |
12108 KB |
Output is correct |
32 |
Correct |
45 ms |
15560 KB |
Output is correct |
33 |
Correct |
47 ms |
17344 KB |
Output is correct |
34 |
Correct |
47 ms |
17508 KB |
Output is correct |
35 |
Correct |
29 ms |
16176 KB |
Output is correct |
36 |
Correct |
46 ms |
20316 KB |
Output is correct |
37 |
Correct |
48 ms |
24304 KB |
Output is correct |
38 |
Correct |
39 ms |
13712 KB |
Output is correct |
39 |
Correct |
106 ms |
28060 KB |
Output is correct |
40 |
Correct |
137 ms |
38048 KB |
Output is correct |
41 |
Correct |
218 ms |
53504 KB |
Output is correct |
42 |
Correct |
117 ms |
28624 KB |
Output is correct |
43 |
Correct |
166 ms |
30828 KB |
Output is correct |
44 |
Correct |
186 ms |
28756 KB |
Output is correct |
45 |
Correct |
33 ms |
13168 KB |
Output is correct |
46 |
Correct |
129 ms |
27356 KB |
Output is correct |
47 |
Correct |
146 ms |
36172 KB |
Output is correct |
48 |
Correct |
223 ms |
49884 KB |
Output is correct |
49 |
Correct |
126 ms |
29252 KB |
Output is correct |
50 |
Correct |
205 ms |
31240 KB |
Output is correct |
51 |
Correct |
162 ms |
29228 KB |
Output is correct |