#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <stack>
#include <iomanip>
using namespace std;
#define int long long
#define endl '\n'
vector<vector<int>>adj(100'001);
vector<pair<int,int>>fruits[100'001];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n - 1; i++){
int x; cin >> x; --x;
adj[x].push_back(i+1);
adj[i+1].push_back(x);
}
for (int i = 0; i < m; i++){
int a,b,c;
cin >> a >> b >> c;
--a;
fruits[a].push_back({b,c});
}
map<int,long long>dp[100'001];
function<void(int,int)>dfs = [&](int u, int p){
for(int& x : adj[u]){
if (x == p)continue;
dfs(x,u);
if (dp[x].size() > dp[u].size())swap(dp[x], dp[u]);
for (auto j : dp[x]){
dp[u][j.first] += j.second;
}
dp[x].clear();
}
//take this one
if (fruits[u].size() > 0){
long long res = fruits[u][0].second;
while(res > 0){
auto it = dp[u].upper_bound(fruits[u][0].first);
if (it == dp[u].end()){
break;
}
if (it->second <= res){
res -= it->second;
dp[u].erase(it);
}else{
it->second -= res;
break;
}
}
dp[u][fruits[u][0].first] += fruits[u][0].second;
}
};
dfs(0,0);
long long ans = 0;
for (auto j : dp[0])ans += j.second;
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9808 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
16780 KB |
Output is correct |
2 |
Correct |
44 ms |
22932 KB |
Output is correct |
3 |
Correct |
110 ms |
23756 KB |
Output is correct |
4 |
Correct |
67 ms |
22208 KB |
Output is correct |
5 |
Correct |
85 ms |
24524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9940 KB |
Output is correct |
2 |
Correct |
5 ms |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
52 ms |
33568 KB |
Output is correct |
5 |
Correct |
71 ms |
37452 KB |
Output is correct |
6 |
Correct |
68 ms |
35148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
16588 KB |
Output is correct |
2 |
Correct |
85 ms |
17080 KB |
Output is correct |
3 |
Correct |
61 ms |
23500 KB |
Output is correct |
4 |
Correct |
45 ms |
17196 KB |
Output is correct |
5 |
Correct |
54 ms |
33612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9808 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
69 ms |
17052 KB |
Output is correct |
11 |
Correct |
68 ms |
16912 KB |
Output is correct |
12 |
Correct |
66 ms |
23652 KB |
Output is correct |
13 |
Correct |
42 ms |
16704 KB |
Output is correct |
14 |
Correct |
50 ms |
33228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
10580 KB |
Output is correct |
2 |
Correct |
29 ms |
13900 KB |
Output is correct |
3 |
Correct |
36 ms |
13872 KB |
Output is correct |
4 |
Correct |
31 ms |
13900 KB |
Output is correct |
5 |
Correct |
15 ms |
13896 KB |
Output is correct |
6 |
Correct |
31 ms |
17380 KB |
Output is correct |
7 |
Correct |
33 ms |
20428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9808 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
6 ms |
9940 KB |
Output is correct |
11 |
Correct |
5 ms |
9804 KB |
Output is correct |
12 |
Correct |
6 ms |
9940 KB |
Output is correct |
13 |
Correct |
52 ms |
33568 KB |
Output is correct |
14 |
Correct |
71 ms |
37452 KB |
Output is correct |
15 |
Correct |
68 ms |
35148 KB |
Output is correct |
16 |
Correct |
69 ms |
17052 KB |
Output is correct |
17 |
Correct |
68 ms |
16912 KB |
Output is correct |
18 |
Correct |
66 ms |
23652 KB |
Output is correct |
19 |
Correct |
42 ms |
16704 KB |
Output is correct |
20 |
Correct |
50 ms |
33228 KB |
Output is correct |
21 |
Correct |
23 ms |
11644 KB |
Output is correct |
22 |
Correct |
61 ms |
16432 KB |
Output is correct |
23 |
Correct |
68 ms |
18280 KB |
Output is correct |
24 |
Correct |
104 ms |
21324 KB |
Output is correct |
25 |
Correct |
69 ms |
21676 KB |
Output is correct |
26 |
Correct |
92 ms |
25548 KB |
Output is correct |
27 |
Correct |
79 ms |
26060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9808 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
56 ms |
16780 KB |
Output is correct |
11 |
Correct |
44 ms |
22932 KB |
Output is correct |
12 |
Correct |
110 ms |
23756 KB |
Output is correct |
13 |
Correct |
67 ms |
22208 KB |
Output is correct |
14 |
Correct |
85 ms |
24524 KB |
Output is correct |
15 |
Correct |
6 ms |
9940 KB |
Output is correct |
16 |
Correct |
5 ms |
9804 KB |
Output is correct |
17 |
Correct |
6 ms |
9940 KB |
Output is correct |
18 |
Correct |
52 ms |
33568 KB |
Output is correct |
19 |
Correct |
71 ms |
37452 KB |
Output is correct |
20 |
Correct |
68 ms |
35148 KB |
Output is correct |
21 |
Correct |
62 ms |
16588 KB |
Output is correct |
22 |
Correct |
85 ms |
17080 KB |
Output is correct |
23 |
Correct |
61 ms |
23500 KB |
Output is correct |
24 |
Correct |
45 ms |
17196 KB |
Output is correct |
25 |
Correct |
54 ms |
33612 KB |
Output is correct |
26 |
Correct |
69 ms |
17052 KB |
Output is correct |
27 |
Correct |
68 ms |
16912 KB |
Output is correct |
28 |
Correct |
66 ms |
23652 KB |
Output is correct |
29 |
Correct |
42 ms |
16704 KB |
Output is correct |
30 |
Correct |
50 ms |
33228 KB |
Output is correct |
31 |
Correct |
10 ms |
10580 KB |
Output is correct |
32 |
Correct |
29 ms |
13900 KB |
Output is correct |
33 |
Correct |
36 ms |
13872 KB |
Output is correct |
34 |
Correct |
31 ms |
13900 KB |
Output is correct |
35 |
Correct |
15 ms |
13896 KB |
Output is correct |
36 |
Correct |
31 ms |
17380 KB |
Output is correct |
37 |
Correct |
33 ms |
20428 KB |
Output is correct |
38 |
Correct |
23 ms |
11644 KB |
Output is correct |
39 |
Correct |
61 ms |
16432 KB |
Output is correct |
40 |
Correct |
68 ms |
18280 KB |
Output is correct |
41 |
Correct |
104 ms |
21324 KB |
Output is correct |
42 |
Correct |
69 ms |
21676 KB |
Output is correct |
43 |
Correct |
92 ms |
25548 KB |
Output is correct |
44 |
Correct |
79 ms |
26060 KB |
Output is correct |
45 |
Correct |
18 ms |
11596 KB |
Output is correct |
46 |
Correct |
59 ms |
16712 KB |
Output is correct |
47 |
Correct |
70 ms |
18636 KB |
Output is correct |
48 |
Correct |
108 ms |
22356 KB |
Output is correct |
49 |
Correct |
66 ms |
22176 KB |
Output is correct |
50 |
Correct |
99 ms |
26444 KB |
Output is correct |
51 |
Correct |
94 ms |
27068 KB |
Output is correct |