#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, k; cin >> n >> m >> k;
vector<vector<int>> g(n);
array<int, 2> base = {-1, -1};
vector<array<int, 2>> val(n, base);
for(int i=1; i<n; i++){
int x; cin >> x; x--;
g[x].push_back(i);
}
for(int i=0; i<m; i++){
int v, d, w; cin >> v >> d >> w;
v--;
val[v] = {d, w};
}
auto dfs=[&](int v, auto&&dfs)->set<array<int, 2>>{
set<array<int, 2>> res;
res.insert({0, 0});
for(auto i: g[v]){
auto x = dfs(i, dfs);
if(x.size() > res.size()) swap(res, x);
for(auto i: x){
auto a = res.lower_bound({i[0], 0});
if(a==res.end()||(*a)[0] > i[0]) a=prev(a);
if((*a)[0] == i[0]){
i[1]+=(*a)[1];
res.erase(a);
res.insert(i);
}
else{
res.insert(i);
}
}
}
if(val[v][0]!=-1){
auto a = res.lower_bound({val[v][0], 0});
auto i=val[v];
if(a==res.end()||(*a)[0] > i[0]) a=prev(a);
if((*a)[0] == i[0]){
i[1]+=(*a)[1];
res.erase(a);
res.insert(i);
}
else{
res.insert(i);
}
auto it = res.upper_bound({val[v][0], (int)1e18});
while(it!=res.end()){
if((*it)[1]>val[v][1]){
array<int, 2> x=*it;
res.erase(it);
x[1]-=val[v][1];
if(x[1]) res.insert(x);
break;
}
else{
val[v][1]-=(*it)[1];
it = res.erase(it);
}
}
}
return res;
};
auto x = dfs(0, dfs);
int ans=0;
for(auto i: x)ans+=i[1];
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
8016 KB |
Output is correct |
2 |
Correct |
44 ms |
18824 KB |
Output is correct |
3 |
Correct |
108 ms |
10096 KB |
Output is correct |
4 |
Correct |
77 ms |
8860 KB |
Output is correct |
5 |
Correct |
80 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
63 ms |
35652 KB |
Output is correct |
5 |
Correct |
77 ms |
35520 KB |
Output is correct |
6 |
Correct |
89 ms |
35548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
6236 KB |
Output is correct |
2 |
Correct |
48 ms |
6236 KB |
Output is correct |
3 |
Correct |
52 ms |
18004 KB |
Output is correct |
4 |
Correct |
36 ms |
5340 KB |
Output is correct |
5 |
Correct |
62 ms |
35676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
55 ms |
6236 KB |
Output is correct |
11 |
Correct |
50 ms |
6228 KB |
Output is correct |
12 |
Correct |
62 ms |
18096 KB |
Output is correct |
13 |
Correct |
38 ms |
5340 KB |
Output is correct |
14 |
Correct |
65 ms |
35412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
22 ms |
6280 KB |
Output is correct |
3 |
Correct |
24 ms |
6236 KB |
Output is correct |
4 |
Correct |
30 ms |
6236 KB |
Output is correct |
5 |
Correct |
16 ms |
5336 KB |
Output is correct |
6 |
Correct |
27 ms |
11608 KB |
Output is correct |
7 |
Correct |
32 ms |
18008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
63 ms |
35652 KB |
Output is correct |
14 |
Correct |
77 ms |
35520 KB |
Output is correct |
15 |
Correct |
89 ms |
35548 KB |
Output is correct |
16 |
Correct |
55 ms |
6236 KB |
Output is correct |
17 |
Correct |
50 ms |
6228 KB |
Output is correct |
18 |
Correct |
62 ms |
18096 KB |
Output is correct |
19 |
Correct |
38 ms |
5340 KB |
Output is correct |
20 |
Correct |
65 ms |
35412 KB |
Output is correct |
21 |
Correct |
15 ms |
1884 KB |
Output is correct |
22 |
Correct |
56 ms |
6648 KB |
Output is correct |
23 |
Correct |
67 ms |
8276 KB |
Output is correct |
24 |
Correct |
98 ms |
9572 KB |
Output is correct |
25 |
Correct |
69 ms |
8632 KB |
Output is correct |
26 |
Correct |
84 ms |
14416 KB |
Output is correct |
27 |
Correct |
85 ms |
18516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
47 ms |
8016 KB |
Output is correct |
11 |
Correct |
44 ms |
18824 KB |
Output is correct |
12 |
Correct |
108 ms |
10096 KB |
Output is correct |
13 |
Correct |
77 ms |
8860 KB |
Output is correct |
14 |
Correct |
80 ms |
11356 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
63 ms |
35652 KB |
Output is correct |
19 |
Correct |
77 ms |
35520 KB |
Output is correct |
20 |
Correct |
89 ms |
35548 KB |
Output is correct |
21 |
Correct |
53 ms |
6236 KB |
Output is correct |
22 |
Correct |
48 ms |
6236 KB |
Output is correct |
23 |
Correct |
52 ms |
18004 KB |
Output is correct |
24 |
Correct |
36 ms |
5340 KB |
Output is correct |
25 |
Correct |
62 ms |
35676 KB |
Output is correct |
26 |
Correct |
55 ms |
6236 KB |
Output is correct |
27 |
Correct |
50 ms |
6228 KB |
Output is correct |
28 |
Correct |
62 ms |
18096 KB |
Output is correct |
29 |
Correct |
38 ms |
5340 KB |
Output is correct |
30 |
Correct |
65 ms |
35412 KB |
Output is correct |
31 |
Correct |
5 ms |
1624 KB |
Output is correct |
32 |
Correct |
22 ms |
6280 KB |
Output is correct |
33 |
Correct |
24 ms |
6236 KB |
Output is correct |
34 |
Correct |
30 ms |
6236 KB |
Output is correct |
35 |
Correct |
16 ms |
5336 KB |
Output is correct |
36 |
Correct |
27 ms |
11608 KB |
Output is correct |
37 |
Correct |
32 ms |
18008 KB |
Output is correct |
38 |
Correct |
15 ms |
1884 KB |
Output is correct |
39 |
Correct |
56 ms |
6648 KB |
Output is correct |
40 |
Correct |
67 ms |
8276 KB |
Output is correct |
41 |
Correct |
98 ms |
9572 KB |
Output is correct |
42 |
Correct |
69 ms |
8632 KB |
Output is correct |
43 |
Correct |
84 ms |
14416 KB |
Output is correct |
44 |
Correct |
85 ms |
18516 KB |
Output is correct |
45 |
Correct |
18 ms |
1884 KB |
Output is correct |
46 |
Correct |
60 ms |
6624 KB |
Output is correct |
47 |
Correct |
66 ms |
8276 KB |
Output is correct |
48 |
Correct |
129 ms |
9808 KB |
Output is correct |
49 |
Correct |
69 ms |
8852 KB |
Output is correct |
50 |
Correct |
90 ms |
14676 KB |
Output is correct |
51 |
Correct |
91 ms |
18772 KB |
Output is correct |