#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N=1e5;
int n, m, k;
int par[N];
int d[N], w[N];
map<int,ll> mp[N];
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> k;
for(int i=1; i<n; i++){
cin >> par[i]; --par[i];
}
for(int i=0; i<m; i++){
int v; cin >> v;
v--;
cin >> d[v] >> w[v];
}
auto join=[&](int i, int j){
if(mp[i].size()>mp[j].size()) swap(mp[i], mp[j]);
for(auto p:mp[i]){
mp[j][p.first]+=p.second;
}
};
for(int i=n-1; i>=0; i--){
mp[i][d[i]]+=w[i];
auto it=mp[i].upper_bound(d[i]);
while(it!=mp[i].end()){
if(it->second>w[i]){
it->second-=w[i];
break;
}
w[i]-=it->second;
it++;
mp[i].erase(prev(it));
}
// cout << "mp[" << i << "]\n";
// for(auto p:mp[i]) cout << p.first << " " << p.second << "\n";
if(i) join(i, par[i]);
}
ll ans=0;
for(auto p:mp[0]) ans+=p.second;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
18704 KB |
Output is correct |
2 |
Correct |
27 ms |
8588 KB |
Output is correct |
3 |
Correct |
140 ms |
37868 KB |
Output is correct |
4 |
Correct |
73 ms |
16044 KB |
Output is correct |
5 |
Correct |
104 ms |
20444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
33 ms |
6128 KB |
Output is correct |
5 |
Correct |
51 ms |
10100 KB |
Output is correct |
6 |
Correct |
46 ms |
6160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
10760 KB |
Output is correct |
2 |
Correct |
39 ms |
11800 KB |
Output is correct |
3 |
Correct |
34 ms |
9676 KB |
Output is correct |
4 |
Correct |
46 ms |
14172 KB |
Output is correct |
5 |
Correct |
33 ms |
8204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
62 ms |
14460 KB |
Output is correct |
11 |
Correct |
73 ms |
13132 KB |
Output is correct |
12 |
Correct |
31 ms |
7640 KB |
Output is correct |
13 |
Correct |
34 ms |
12340 KB |
Output is correct |
14 |
Correct |
28 ms |
6100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6100 KB |
Output is correct |
2 |
Correct |
20 ms |
9988 KB |
Output is correct |
3 |
Correct |
22 ms |
9996 KB |
Output is correct |
4 |
Correct |
22 ms |
10048 KB |
Output is correct |
5 |
Correct |
16 ms |
12628 KB |
Output is correct |
6 |
Correct |
22 ms |
10628 KB |
Output is correct |
7 |
Correct |
22 ms |
7968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
33 ms |
6128 KB |
Output is correct |
14 |
Correct |
51 ms |
10100 KB |
Output is correct |
15 |
Correct |
46 ms |
6160 KB |
Output is correct |
16 |
Correct |
62 ms |
14460 KB |
Output is correct |
17 |
Correct |
73 ms |
13132 KB |
Output is correct |
18 |
Correct |
31 ms |
7640 KB |
Output is correct |
19 |
Correct |
34 ms |
12340 KB |
Output is correct |
20 |
Correct |
28 ms |
6100 KB |
Output is correct |
21 |
Correct |
17 ms |
8532 KB |
Output is correct |
22 |
Correct |
82 ms |
18084 KB |
Output is correct |
23 |
Correct |
75 ms |
21832 KB |
Output is correct |
24 |
Correct |
129 ms |
30272 KB |
Output is correct |
25 |
Correct |
78 ms |
16136 KB |
Output is correct |
26 |
Correct |
82 ms |
16076 KB |
Output is correct |
27 |
Correct |
56 ms |
9152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
54 ms |
18704 KB |
Output is correct |
11 |
Correct |
27 ms |
8588 KB |
Output is correct |
12 |
Correct |
140 ms |
37868 KB |
Output is correct |
13 |
Correct |
73 ms |
16044 KB |
Output is correct |
14 |
Correct |
104 ms |
20444 KB |
Output is correct |
15 |
Correct |
4 ms |
4948 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
33 ms |
6128 KB |
Output is correct |
19 |
Correct |
51 ms |
10100 KB |
Output is correct |
20 |
Correct |
46 ms |
6160 KB |
Output is correct |
21 |
Correct |
43 ms |
10760 KB |
Output is correct |
22 |
Correct |
39 ms |
11800 KB |
Output is correct |
23 |
Correct |
34 ms |
9676 KB |
Output is correct |
24 |
Correct |
46 ms |
14172 KB |
Output is correct |
25 |
Correct |
33 ms |
8204 KB |
Output is correct |
26 |
Correct |
62 ms |
14460 KB |
Output is correct |
27 |
Correct |
73 ms |
13132 KB |
Output is correct |
28 |
Correct |
31 ms |
7640 KB |
Output is correct |
29 |
Correct |
34 ms |
12340 KB |
Output is correct |
30 |
Correct |
28 ms |
6100 KB |
Output is correct |
31 |
Correct |
7 ms |
6100 KB |
Output is correct |
32 |
Correct |
20 ms |
9988 KB |
Output is correct |
33 |
Correct |
22 ms |
9996 KB |
Output is correct |
34 |
Correct |
22 ms |
10048 KB |
Output is correct |
35 |
Correct |
16 ms |
12628 KB |
Output is correct |
36 |
Correct |
22 ms |
10628 KB |
Output is correct |
37 |
Correct |
22 ms |
7968 KB |
Output is correct |
38 |
Correct |
17 ms |
8532 KB |
Output is correct |
39 |
Correct |
82 ms |
18084 KB |
Output is correct |
40 |
Correct |
75 ms |
21832 KB |
Output is correct |
41 |
Correct |
129 ms |
30272 KB |
Output is correct |
42 |
Correct |
78 ms |
16136 KB |
Output is correct |
43 |
Correct |
82 ms |
16076 KB |
Output is correct |
44 |
Correct |
56 ms |
9152 KB |
Output is correct |
45 |
Correct |
20 ms |
9512 KB |
Output is correct |
46 |
Correct |
73 ms |
20456 KB |
Output is correct |
47 |
Correct |
90 ms |
24688 KB |
Output is correct |
48 |
Correct |
124 ms |
35344 KB |
Output is correct |
49 |
Correct |
80 ms |
18248 KB |
Output is correct |
50 |
Correct |
112 ms |
18856 KB |
Output is correct |
51 |
Correct |
74 ms |
11920 KB |
Output is correct |