#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
struct Tree{
vector<int> adj[MAXN + 1];
int ds[MAXN + 1], js[MAXN + 1];
void insert(map<int, long long>* dp, int v){
(*dp)[ds[v]] += js[v];
auto ptr = dp->find(ds[v]);
int j_left = js[v];
while(j_left > 0 && next(ptr) != dp->end()){
auto nxt_ptr = next(ptr);
if(nxt_ptr->second > j_left){
nxt_ptr->second -= j_left;
break;
}
else{
j_left -= nxt_ptr->second;
dp->erase(nxt_ptr);
}
}
}
map<int, long long>* TreeDp(int v){
auto curr_map = new map<int, long long>();
for(auto u : adj[v]){
auto new_map = TreeDp(u);
if(new_map->size() > curr_map->size()) swap(curr_map, new_map);
for(auto [key, value] : *new_map){
(*curr_map)[key] += value;
}
}
if(ds[v] > 0) insert(curr_map, v);
return curr_map;
}
};
Tree T;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int N, M, K;
cin>> N>> M>> K;
for(int i=2; i<=N; i++){
int p; cin>> p;
T.adj[p].push_back(i);
}
for(int i=0; i<M; i++){
int v; cin>> v;
cin>> T.ds[v]>> T.js[v];
}
auto dp = T.TreeDp(1);
long long res = 0;
for(auto [key, value] : *dp){
res += value;
}
cout<< res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2804 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
20052 KB |
Output is correct |
2 |
Correct |
33 ms |
21332 KB |
Output is correct |
3 |
Correct |
102 ms |
38484 KB |
Output is correct |
4 |
Correct |
58 ms |
21448 KB |
Output is correct |
5 |
Correct |
68 ms |
23432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2820 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
44 ms |
28964 KB |
Output is correct |
5 |
Correct |
62 ms |
32880 KB |
Output is correct |
6 |
Correct |
60 ms |
28984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
17216 KB |
Output is correct |
2 |
Correct |
40 ms |
16328 KB |
Output is correct |
3 |
Correct |
39 ms |
21020 KB |
Output is correct |
4 |
Correct |
31 ms |
17396 KB |
Output is correct |
5 |
Correct |
41 ms |
29016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2804 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
44 ms |
19284 KB |
Output is correct |
11 |
Correct |
40 ms |
18260 KB |
Output is correct |
12 |
Correct |
39 ms |
20388 KB |
Output is correct |
13 |
Correct |
29 ms |
16960 KB |
Output is correct |
14 |
Correct |
39 ms |
28496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4780 KB |
Output is correct |
2 |
Correct |
19 ms |
12124 KB |
Output is correct |
3 |
Correct |
20 ms |
12040 KB |
Output is correct |
4 |
Correct |
19 ms |
12092 KB |
Output is correct |
5 |
Correct |
10 ms |
10460 KB |
Output is correct |
6 |
Correct |
23 ms |
14484 KB |
Output is correct |
7 |
Correct |
22 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2804 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
2 ms |
2820 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
44 ms |
28964 KB |
Output is correct |
14 |
Correct |
62 ms |
32880 KB |
Output is correct |
15 |
Correct |
60 ms |
28984 KB |
Output is correct |
16 |
Correct |
44 ms |
19284 KB |
Output is correct |
17 |
Correct |
40 ms |
18260 KB |
Output is correct |
18 |
Correct |
39 ms |
20388 KB |
Output is correct |
19 |
Correct |
29 ms |
16960 KB |
Output is correct |
20 |
Correct |
39 ms |
28496 KB |
Output is correct |
21 |
Correct |
14 ms |
7260 KB |
Output is correct |
22 |
Correct |
45 ms |
20308 KB |
Output is correct |
23 |
Correct |
53 ms |
23636 KB |
Output is correct |
24 |
Correct |
76 ms |
31940 KB |
Output is correct |
25 |
Correct |
53 ms |
20948 KB |
Output is correct |
26 |
Correct |
66 ms |
23120 KB |
Output is correct |
27 |
Correct |
57 ms |
22216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2804 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2904 KB |
Output is correct |
10 |
Correct |
41 ms |
20052 KB |
Output is correct |
11 |
Correct |
33 ms |
21332 KB |
Output is correct |
12 |
Correct |
102 ms |
38484 KB |
Output is correct |
13 |
Correct |
58 ms |
21448 KB |
Output is correct |
14 |
Correct |
68 ms |
23432 KB |
Output is correct |
15 |
Correct |
2 ms |
2820 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
44 ms |
28964 KB |
Output is correct |
19 |
Correct |
62 ms |
32880 KB |
Output is correct |
20 |
Correct |
60 ms |
28984 KB |
Output is correct |
21 |
Correct |
41 ms |
17216 KB |
Output is correct |
22 |
Correct |
40 ms |
16328 KB |
Output is correct |
23 |
Correct |
39 ms |
21020 KB |
Output is correct |
24 |
Correct |
31 ms |
17396 KB |
Output is correct |
25 |
Correct |
41 ms |
29016 KB |
Output is correct |
26 |
Correct |
44 ms |
19284 KB |
Output is correct |
27 |
Correct |
40 ms |
18260 KB |
Output is correct |
28 |
Correct |
39 ms |
20388 KB |
Output is correct |
29 |
Correct |
29 ms |
16960 KB |
Output is correct |
30 |
Correct |
39 ms |
28496 KB |
Output is correct |
31 |
Correct |
6 ms |
4780 KB |
Output is correct |
32 |
Correct |
19 ms |
12124 KB |
Output is correct |
33 |
Correct |
20 ms |
12040 KB |
Output is correct |
34 |
Correct |
19 ms |
12092 KB |
Output is correct |
35 |
Correct |
10 ms |
10460 KB |
Output is correct |
36 |
Correct |
23 ms |
14484 KB |
Output is correct |
37 |
Correct |
22 ms |
18524 KB |
Output is correct |
38 |
Correct |
14 ms |
7260 KB |
Output is correct |
39 |
Correct |
45 ms |
20308 KB |
Output is correct |
40 |
Correct |
53 ms |
23636 KB |
Output is correct |
41 |
Correct |
76 ms |
31940 KB |
Output is correct |
42 |
Correct |
53 ms |
20948 KB |
Output is correct |
43 |
Correct |
66 ms |
23120 KB |
Output is correct |
44 |
Correct |
57 ms |
22216 KB |
Output is correct |
45 |
Correct |
15 ms |
7772 KB |
Output is correct |
46 |
Correct |
48 ms |
21332 KB |
Output is correct |
47 |
Correct |
57 ms |
24824 KB |
Output is correct |
48 |
Correct |
93 ms |
34900 KB |
Output is correct |
49 |
Correct |
56 ms |
21456 KB |
Output is correct |
50 |
Correct |
72 ms |
23972 KB |
Output is correct |
51 |
Correct |
65 ms |
23088 KB |
Output is correct |