#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int N = 1e5 + 5;
vector<int> edges[N];
int is[N], d[N], w[N], mx[N];
map<int, int> sub[N];
void dfs(int u){
mx[u] = u;
for(auto x : edges[u]){
dfs(x);
if(sub[x].size() > sub[mx[u]].size()) mx[u] = x;
}
swap(sub[u], sub[mx[u]]);
for(auto x : edges[u]){
if(x == mx[u]) continue;
for(auto v : sub[x]){
sub[u][v.first] += v.second;
}
sub[x].clear();
}
if(is[u]){
sub[u][d[u]] += w[u];
auto it = sub[u].upper_bound(d[u]);
vector<int> er;
int sum = 0;
while(it != sub[u].end()){
sum += (*it).second;
if(sum >= w[u]){
sub[u][(*it).first] = sum - w[u];
break;
}
er.push_back((*it).first);
it++;
}
for(auto x : er){
sub[u].erase(x);
}
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m, k; cin>>n>>m>>k;
for(int i=2;i<=n;i++){
int x; cin>>x;
edges[x].push_back(i);
}
for(int i=0;i<m;i++){
int v; cin>>v;
cin>>d[v]>>w[v];
is[v] = 1;
}
dfs(1);
int sum = 0;
for(auto x : sub[1]) sum += x.second;
cout<<sum<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7384 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
14164 KB |
Output is correct |
2 |
Correct |
51 ms |
19144 KB |
Output is correct |
3 |
Correct |
121 ms |
17476 KB |
Output is correct |
4 |
Correct |
68 ms |
16880 KB |
Output is correct |
5 |
Correct |
105 ms |
16812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
5 ms |
7608 KB |
Output is correct |
3 |
Correct |
5 ms |
7640 KB |
Output is correct |
4 |
Correct |
70 ms |
31168 KB |
Output is correct |
5 |
Correct |
77 ms |
35068 KB |
Output is correct |
6 |
Correct |
88 ms |
31244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
12344 KB |
Output is correct |
2 |
Correct |
83 ms |
12264 KB |
Output is correct |
3 |
Correct |
61 ms |
19276 KB |
Output is correct |
4 |
Correct |
42 ms |
16780 KB |
Output is correct |
5 |
Correct |
52 ms |
29260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7384 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
77 ms |
12364 KB |
Output is correct |
11 |
Correct |
70 ms |
13748 KB |
Output is correct |
12 |
Correct |
65 ms |
20696 KB |
Output is correct |
13 |
Correct |
47 ms |
17992 KB |
Output is correct |
14 |
Correct |
47 ms |
30752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
8404 KB |
Output is correct |
2 |
Correct |
35 ms |
12872 KB |
Output is correct |
3 |
Correct |
48 ms |
12852 KB |
Output is correct |
4 |
Correct |
39 ms |
12900 KB |
Output is correct |
5 |
Correct |
14 ms |
11600 KB |
Output is correct |
6 |
Correct |
39 ms |
15920 KB |
Output is correct |
7 |
Correct |
29 ms |
19852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7384 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
5 ms |
7608 KB |
Output is correct |
12 |
Correct |
5 ms |
7640 KB |
Output is correct |
13 |
Correct |
70 ms |
31168 KB |
Output is correct |
14 |
Correct |
77 ms |
35068 KB |
Output is correct |
15 |
Correct |
88 ms |
31244 KB |
Output is correct |
16 |
Correct |
77 ms |
12364 KB |
Output is correct |
17 |
Correct |
70 ms |
13748 KB |
Output is correct |
18 |
Correct |
65 ms |
20696 KB |
Output is correct |
19 |
Correct |
47 ms |
17992 KB |
Output is correct |
20 |
Correct |
47 ms |
30752 KB |
Output is correct |
21 |
Correct |
19 ms |
9072 KB |
Output is correct |
22 |
Correct |
74 ms |
13976 KB |
Output is correct |
23 |
Correct |
110 ms |
16360 KB |
Output is correct |
24 |
Correct |
139 ms |
18980 KB |
Output is correct |
25 |
Correct |
60 ms |
18300 KB |
Output is correct |
26 |
Correct |
107 ms |
21904 KB |
Output is correct |
27 |
Correct |
109 ms |
23480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7368 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7384 KB |
Output is correct |
8 |
Correct |
3 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
68 ms |
14164 KB |
Output is correct |
11 |
Correct |
51 ms |
19144 KB |
Output is correct |
12 |
Correct |
121 ms |
17476 KB |
Output is correct |
13 |
Correct |
68 ms |
16880 KB |
Output is correct |
14 |
Correct |
105 ms |
16812 KB |
Output is correct |
15 |
Correct |
4 ms |
7508 KB |
Output is correct |
16 |
Correct |
5 ms |
7608 KB |
Output is correct |
17 |
Correct |
5 ms |
7640 KB |
Output is correct |
18 |
Correct |
70 ms |
31168 KB |
Output is correct |
19 |
Correct |
77 ms |
35068 KB |
Output is correct |
20 |
Correct |
88 ms |
31244 KB |
Output is correct |
21 |
Correct |
67 ms |
12344 KB |
Output is correct |
22 |
Correct |
83 ms |
12264 KB |
Output is correct |
23 |
Correct |
61 ms |
19276 KB |
Output is correct |
24 |
Correct |
42 ms |
16780 KB |
Output is correct |
25 |
Correct |
52 ms |
29260 KB |
Output is correct |
26 |
Correct |
77 ms |
12364 KB |
Output is correct |
27 |
Correct |
70 ms |
13748 KB |
Output is correct |
28 |
Correct |
65 ms |
20696 KB |
Output is correct |
29 |
Correct |
47 ms |
17992 KB |
Output is correct |
30 |
Correct |
47 ms |
30752 KB |
Output is correct |
31 |
Correct |
8 ms |
8404 KB |
Output is correct |
32 |
Correct |
35 ms |
12872 KB |
Output is correct |
33 |
Correct |
48 ms |
12852 KB |
Output is correct |
34 |
Correct |
39 ms |
12900 KB |
Output is correct |
35 |
Correct |
14 ms |
11600 KB |
Output is correct |
36 |
Correct |
39 ms |
15920 KB |
Output is correct |
37 |
Correct |
29 ms |
19852 KB |
Output is correct |
38 |
Correct |
19 ms |
9072 KB |
Output is correct |
39 |
Correct |
74 ms |
13976 KB |
Output is correct |
40 |
Correct |
110 ms |
16360 KB |
Output is correct |
41 |
Correct |
139 ms |
18980 KB |
Output is correct |
42 |
Correct |
60 ms |
18300 KB |
Output is correct |
43 |
Correct |
107 ms |
21904 KB |
Output is correct |
44 |
Correct |
109 ms |
23480 KB |
Output is correct |
45 |
Correct |
20 ms |
9164 KB |
Output is correct |
46 |
Correct |
76 ms |
14288 KB |
Output is correct |
47 |
Correct |
80 ms |
16176 KB |
Output is correct |
48 |
Correct |
137 ms |
18972 KB |
Output is correct |
49 |
Correct |
76 ms |
18948 KB |
Output is correct |
50 |
Correct |
112 ms |
21996 KB |
Output is correct |
51 |
Correct |
106 ms |
23528 KB |
Output is correct |