#include <bits/stdc++.h>
using namespace std;
int w[100100], d[100100], sz[100100];
vector<int> a[100100];
map<int, long long> *f[100100];
void visit(int x)
{
f[x] = new map<int, long long>();
for (int y : a[x])
{
visit(y);
if (f[y]->size() > f[x]->size())
swap(f[x], f[y]);
for (auto u : *f[y])
(*f[x])[u.first] += u.second;
}
if (!d[x])
return;
int currentW = w[x];
while (currentW)
{
auto u = f[x]->upper_bound(d[x]);
if (u == f[x]->end())
break;
if (u->second >= currentW)
{
u->second -= currentW;
break;
}
currentW -= u->second;
f[x]->erase(u);
}
(*f[x])[d[x]] += w[x];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k, par;
cin >> n >> m >> k;
for (int i = 2; i <= n; i++)
{
cin >> par;
a[par].push_back(i);
}
int x;
while (m--)
{
cin >> x;
cin >> d[x] >> w[x];
}
visit(1);
long long ans = 0;
for (auto u : *f[1])
ans += u.second;
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
20780 KB |
Output is correct |
2 |
Correct |
60 ms |
21316 KB |
Output is correct |
3 |
Correct |
134 ms |
39324 KB |
Output is correct |
4 |
Correct |
90 ms |
22340 KB |
Output is correct |
5 |
Correct |
117 ms |
24104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2892 KB |
Output is correct |
2 |
Correct |
3 ms |
2892 KB |
Output is correct |
3 |
Correct |
2 ms |
2892 KB |
Output is correct |
4 |
Correct |
66 ms |
28028 KB |
Output is correct |
5 |
Correct |
86 ms |
31936 KB |
Output is correct |
6 |
Correct |
87 ms |
28072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
17900 KB |
Output is correct |
2 |
Correct |
73 ms |
17212 KB |
Output is correct |
3 |
Correct |
66 ms |
21160 KB |
Output is correct |
4 |
Correct |
49 ms |
18184 KB |
Output is correct |
5 |
Correct |
60 ms |
28232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
77 ms |
21308 KB |
Output is correct |
11 |
Correct |
77 ms |
19904 KB |
Output is correct |
12 |
Correct |
64 ms |
20608 KB |
Output is correct |
13 |
Correct |
47 ms |
17544 KB |
Output is correct |
14 |
Correct |
55 ms |
27600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4876 KB |
Output is correct |
2 |
Correct |
35 ms |
12844 KB |
Output is correct |
3 |
Correct |
36 ms |
12736 KB |
Output is correct |
4 |
Correct |
36 ms |
12868 KB |
Output is correct |
5 |
Correct |
16 ms |
11212 KB |
Output is correct |
6 |
Correct |
33 ms |
14980 KB |
Output is correct |
7 |
Correct |
36 ms |
18592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2892 KB |
Output is correct |
11 |
Correct |
3 ms |
2892 KB |
Output is correct |
12 |
Correct |
2 ms |
2892 KB |
Output is correct |
13 |
Correct |
66 ms |
28028 KB |
Output is correct |
14 |
Correct |
86 ms |
31936 KB |
Output is correct |
15 |
Correct |
87 ms |
28072 KB |
Output is correct |
16 |
Correct |
77 ms |
21308 KB |
Output is correct |
17 |
Correct |
77 ms |
19904 KB |
Output is correct |
18 |
Correct |
64 ms |
20608 KB |
Output is correct |
19 |
Correct |
47 ms |
17544 KB |
Output is correct |
20 |
Correct |
55 ms |
27600 KB |
Output is correct |
21 |
Correct |
23 ms |
8128 KB |
Output is correct |
22 |
Correct |
105 ms |
22676 KB |
Output is correct |
23 |
Correct |
105 ms |
26932 KB |
Output is correct |
24 |
Correct |
132 ms |
38588 KB |
Output is correct |
25 |
Correct |
105 ms |
21640 KB |
Output is correct |
26 |
Correct |
129 ms |
24364 KB |
Output is correct |
27 |
Correct |
96 ms |
23264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
75 ms |
20780 KB |
Output is correct |
11 |
Correct |
60 ms |
21316 KB |
Output is correct |
12 |
Correct |
134 ms |
39324 KB |
Output is correct |
13 |
Correct |
90 ms |
22340 KB |
Output is correct |
14 |
Correct |
117 ms |
24104 KB |
Output is correct |
15 |
Correct |
2 ms |
2892 KB |
Output is correct |
16 |
Correct |
3 ms |
2892 KB |
Output is correct |
17 |
Correct |
2 ms |
2892 KB |
Output is correct |
18 |
Correct |
66 ms |
28028 KB |
Output is correct |
19 |
Correct |
86 ms |
31936 KB |
Output is correct |
20 |
Correct |
87 ms |
28072 KB |
Output is correct |
21 |
Correct |
74 ms |
17900 KB |
Output is correct |
22 |
Correct |
73 ms |
17212 KB |
Output is correct |
23 |
Correct |
66 ms |
21160 KB |
Output is correct |
24 |
Correct |
49 ms |
18184 KB |
Output is correct |
25 |
Correct |
60 ms |
28232 KB |
Output is correct |
26 |
Correct |
77 ms |
21308 KB |
Output is correct |
27 |
Correct |
77 ms |
19904 KB |
Output is correct |
28 |
Correct |
64 ms |
20608 KB |
Output is correct |
29 |
Correct |
47 ms |
17544 KB |
Output is correct |
30 |
Correct |
55 ms |
27600 KB |
Output is correct |
31 |
Correct |
9 ms |
4876 KB |
Output is correct |
32 |
Correct |
35 ms |
12844 KB |
Output is correct |
33 |
Correct |
36 ms |
12736 KB |
Output is correct |
34 |
Correct |
36 ms |
12868 KB |
Output is correct |
35 |
Correct |
16 ms |
11212 KB |
Output is correct |
36 |
Correct |
33 ms |
14980 KB |
Output is correct |
37 |
Correct |
36 ms |
18592 KB |
Output is correct |
38 |
Correct |
23 ms |
8128 KB |
Output is correct |
39 |
Correct |
105 ms |
22676 KB |
Output is correct |
40 |
Correct |
105 ms |
26932 KB |
Output is correct |
41 |
Correct |
132 ms |
38588 KB |
Output is correct |
42 |
Correct |
105 ms |
21640 KB |
Output is correct |
43 |
Correct |
129 ms |
24364 KB |
Output is correct |
44 |
Correct |
96 ms |
23264 KB |
Output is correct |
45 |
Correct |
22 ms |
7884 KB |
Output is correct |
46 |
Correct |
105 ms |
22168 KB |
Output is correct |
47 |
Correct |
96 ms |
25536 KB |
Output is correct |
48 |
Correct |
136 ms |
35612 KB |
Output is correct |
49 |
Correct |
98 ms |
22340 KB |
Output is correct |
50 |
Correct |
118 ms |
24372 KB |
Output is correct |
51 |
Correct |
101 ms |
23288 KB |
Output is correct |