#include <bits/stdc++.h>
using namespace std;
int N, M, K;
vector<int> graph[100005];
int day[100005];
long long val[100005];
map<int, long long> dfs(int n) {
map<int, long long> ret;
for(int e : graph[n]) {
auto m = dfs(e);
if(ret.size() < m.size()) {
swap(ret, m);
}
for(auto p : m) {
ret[p.first] += p.second;
}
}
if(val[n]) {
ret[day[n]] += val[n];
long long pre = 0;
while(1) {
auto ptr = ret.upper_bound(day[n]);
if(ptr != ret.end()) {
pre += ptr->second;
if(pre > val[n]) {
ptr->second = pre - val[n];
break;
}
else {
ret.erase(ptr);
}
}
else {
break;
}
}
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> K;
for(int i = 2; i <= N; i++) {
int p;
cin >> p;
graph[p].push_back(i);
}
for(int i = 1; i <= M; i++) {
int n;
cin >> n;
cin >> day[n] >> val[n];
}
auto mp = dfs(1);
long long ans = 0;
for(auto p : mp) {
ans += p.second;
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2672 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2680 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
8212 KB |
Output is correct |
2 |
Correct |
36 ms |
15652 KB |
Output is correct |
3 |
Correct |
106 ms |
11692 KB |
Output is correct |
4 |
Correct |
58 ms |
9992 KB |
Output is correct |
5 |
Correct |
70 ms |
12568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2900 KB |
Output is correct |
2 |
Correct |
2 ms |
2900 KB |
Output is correct |
3 |
Correct |
2 ms |
2900 KB |
Output is correct |
4 |
Correct |
47 ms |
27548 KB |
Output is correct |
5 |
Correct |
78 ms |
31588 KB |
Output is correct |
6 |
Correct |
62 ms |
27584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
7628 KB |
Output is correct |
2 |
Correct |
44 ms |
7480 KB |
Output is correct |
3 |
Correct |
54 ms |
15896 KB |
Output is correct |
4 |
Correct |
35 ms |
6076 KB |
Output is correct |
5 |
Correct |
42 ms |
27844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2672 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2680 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
52 ms |
6928 KB |
Output is correct |
11 |
Correct |
49 ms |
6848 KB |
Output is correct |
12 |
Correct |
45 ms |
15288 KB |
Output is correct |
13 |
Correct |
28 ms |
5384 KB |
Output is correct |
14 |
Correct |
45 ms |
27168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3412 KB |
Output is correct |
2 |
Correct |
19 ms |
6016 KB |
Output is correct |
3 |
Correct |
21 ms |
5964 KB |
Output is correct |
4 |
Correct |
20 ms |
6056 KB |
Output is correct |
5 |
Correct |
8 ms |
4484 KB |
Output is correct |
6 |
Correct |
23 ms |
9600 KB |
Output is correct |
7 |
Correct |
25 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2672 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2680 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
47 ms |
27548 KB |
Output is correct |
14 |
Correct |
78 ms |
31588 KB |
Output is correct |
15 |
Correct |
62 ms |
27584 KB |
Output is correct |
16 |
Correct |
52 ms |
6928 KB |
Output is correct |
17 |
Correct |
49 ms |
6848 KB |
Output is correct |
18 |
Correct |
45 ms |
15288 KB |
Output is correct |
19 |
Correct |
28 ms |
5384 KB |
Output is correct |
20 |
Correct |
45 ms |
27168 KB |
Output is correct |
21 |
Correct |
13 ms |
3796 KB |
Output is correct |
22 |
Correct |
50 ms |
7024 KB |
Output is correct |
23 |
Correct |
63 ms |
8784 KB |
Output is correct |
24 |
Correct |
84 ms |
10436 KB |
Output is correct |
25 |
Correct |
54 ms |
9232 KB |
Output is correct |
26 |
Correct |
76 ms |
14700 KB |
Output is correct |
27 |
Correct |
69 ms |
17168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2672 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2680 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
43 ms |
8212 KB |
Output is correct |
11 |
Correct |
36 ms |
15652 KB |
Output is correct |
12 |
Correct |
106 ms |
11692 KB |
Output is correct |
13 |
Correct |
58 ms |
9992 KB |
Output is correct |
14 |
Correct |
70 ms |
12568 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
2900 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 KB |
Output is correct |
18 |
Correct |
47 ms |
27548 KB |
Output is correct |
19 |
Correct |
78 ms |
31588 KB |
Output is correct |
20 |
Correct |
62 ms |
27584 KB |
Output is correct |
21 |
Correct |
51 ms |
7628 KB |
Output is correct |
22 |
Correct |
44 ms |
7480 KB |
Output is correct |
23 |
Correct |
54 ms |
15896 KB |
Output is correct |
24 |
Correct |
35 ms |
6076 KB |
Output is correct |
25 |
Correct |
42 ms |
27844 KB |
Output is correct |
26 |
Correct |
52 ms |
6928 KB |
Output is correct |
27 |
Correct |
49 ms |
6848 KB |
Output is correct |
28 |
Correct |
45 ms |
15288 KB |
Output is correct |
29 |
Correct |
28 ms |
5384 KB |
Output is correct |
30 |
Correct |
45 ms |
27168 KB |
Output is correct |
31 |
Correct |
6 ms |
3412 KB |
Output is correct |
32 |
Correct |
19 ms |
6016 KB |
Output is correct |
33 |
Correct |
21 ms |
5964 KB |
Output is correct |
34 |
Correct |
20 ms |
6056 KB |
Output is correct |
35 |
Correct |
8 ms |
4484 KB |
Output is correct |
36 |
Correct |
23 ms |
9600 KB |
Output is correct |
37 |
Correct |
25 ms |
14428 KB |
Output is correct |
38 |
Correct |
13 ms |
3796 KB |
Output is correct |
39 |
Correct |
50 ms |
7024 KB |
Output is correct |
40 |
Correct |
63 ms |
8784 KB |
Output is correct |
41 |
Correct |
84 ms |
10436 KB |
Output is correct |
42 |
Correct |
54 ms |
9232 KB |
Output is correct |
43 |
Correct |
76 ms |
14700 KB |
Output is correct |
44 |
Correct |
69 ms |
17168 KB |
Output is correct |
45 |
Correct |
17 ms |
3924 KB |
Output is correct |
46 |
Correct |
51 ms |
7228 KB |
Output is correct |
47 |
Correct |
70 ms |
9268 KB |
Output is correct |
48 |
Correct |
96 ms |
11588 KB |
Output is correct |
49 |
Correct |
54 ms |
9932 KB |
Output is correct |
50 |
Correct |
89 ms |
15660 KB |
Output is correct |
51 |
Correct |
73 ms |
18016 KB |
Output is correct |