#include <bits/stdc++.h>
//problem from https://usaco.guide/adv/slope-trick?lang=cpp
using namespace std;
vector<vector<int>> adj;
vector<pair<int,long long>> fruits;
map<int,long long> dfs(int v, int p){
map<int,long long> ret;
for(int nei : adj[v]){
if(nei == p) continue;
auto neimap = dfs(nei,v);
if(ret.size() < neimap.size()) swap(ret,neimap);
for(auto [d,w] : neimap){
ret[d] += w;
}
}
if(fruits[v].first != -1){
//add this fruit
long long tot = fruits[v].second;
ret[fruits[v].first] += tot;
int cur = fruits[v].first;
//this will loop O(N) times amortized (every fruit will be removed once)
while(true){
auto it = ret.upper_bound(cur);
if(it == ret.end()) break;
cur = it->first;
if(it->second <= tot){
tot -= it->second;
ret.erase(it);
} else {
it->second -= tot;
break;
}
}
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,D;
cin >> n >> m >> D;
adj = vector<vector<int>>(n+1,vector<int>());
for(int k = 2; k <= n; k++){
int p;
cin >> p;
adj[k].push_back(p);
adj[p].push_back(k);
}
fruits = vector<pair<int,long long>>(n+1,{-1,-1LL});
for(int k = 0; k < m; k++){
int v,d;
long long w;
cin >> v >> d >> w;
fruits[v] = {d,w};
}
auto ret = dfs(1,-1);
long long answer = 0LL;
for(auto [d,w] : ret){
answer += w;
}
cout << answer << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
10512 KB |
Output is correct |
2 |
Correct |
36 ms |
18284 KB |
Output is correct |
3 |
Correct |
101 ms |
15352 KB |
Output is correct |
4 |
Correct |
54 ms |
13516 KB |
Output is correct |
5 |
Correct |
66 ms |
15952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
46 ms |
29640 KB |
Output is correct |
5 |
Correct |
69 ms |
33476 KB |
Output is correct |
6 |
Correct |
62 ms |
29788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
9552 KB |
Output is correct |
2 |
Correct |
52 ms |
9712 KB |
Output is correct |
3 |
Correct |
44 ms |
17744 KB |
Output is correct |
4 |
Correct |
35 ms |
9420 KB |
Output is correct |
5 |
Correct |
47 ms |
29756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
42 ms |
9044 KB |
Output is correct |
11 |
Correct |
41 ms |
9052 KB |
Output is correct |
12 |
Correct |
42 ms |
16988 KB |
Output is correct |
13 |
Correct |
29 ms |
8776 KB |
Output is correct |
14 |
Correct |
42 ms |
29268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1884 KB |
Output is correct |
2 |
Correct |
19 ms |
8028 KB |
Output is correct |
3 |
Correct |
20 ms |
8028 KB |
Output is correct |
4 |
Correct |
20 ms |
8168 KB |
Output is correct |
5 |
Correct |
12 ms |
8148 KB |
Output is correct |
6 |
Correct |
22 ms |
12360 KB |
Output is correct |
7 |
Correct |
28 ms |
16208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
46 ms |
29640 KB |
Output is correct |
14 |
Correct |
69 ms |
33476 KB |
Output is correct |
15 |
Correct |
62 ms |
29788 KB |
Output is correct |
16 |
Correct |
42 ms |
9044 KB |
Output is correct |
17 |
Correct |
41 ms |
9052 KB |
Output is correct |
18 |
Correct |
42 ms |
16988 KB |
Output is correct |
19 |
Correct |
29 ms |
8776 KB |
Output is correct |
20 |
Correct |
42 ms |
29268 KB |
Output is correct |
21 |
Correct |
12 ms |
2396 KB |
Output is correct |
22 |
Correct |
46 ms |
9044 KB |
Output is correct |
23 |
Correct |
68 ms |
11044 KB |
Output is correct |
24 |
Correct |
84 ms |
12540 KB |
Output is correct |
25 |
Correct |
49 ms |
13000 KB |
Output is correct |
26 |
Correct |
67 ms |
17492 KB |
Output is correct |
27 |
Correct |
63 ms |
18908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
61 ms |
10512 KB |
Output is correct |
11 |
Correct |
36 ms |
18284 KB |
Output is correct |
12 |
Correct |
101 ms |
15352 KB |
Output is correct |
13 |
Correct |
54 ms |
13516 KB |
Output is correct |
14 |
Correct |
66 ms |
15952 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 |
46 ms |
29640 KB |
Output is correct |
19 |
Correct |
69 ms |
33476 KB |
Output is correct |
20 |
Correct |
62 ms |
29788 KB |
Output is correct |
21 |
Correct |
37 ms |
9552 KB |
Output is correct |
22 |
Correct |
52 ms |
9712 KB |
Output is correct |
23 |
Correct |
44 ms |
17744 KB |
Output is correct |
24 |
Correct |
35 ms |
9420 KB |
Output is correct |
25 |
Correct |
47 ms |
29756 KB |
Output is correct |
26 |
Correct |
42 ms |
9044 KB |
Output is correct |
27 |
Correct |
41 ms |
9052 KB |
Output is correct |
28 |
Correct |
42 ms |
16988 KB |
Output is correct |
29 |
Correct |
29 ms |
8776 KB |
Output is correct |
30 |
Correct |
42 ms |
29268 KB |
Output is correct |
31 |
Correct |
4 ms |
1884 KB |
Output is correct |
32 |
Correct |
19 ms |
8028 KB |
Output is correct |
33 |
Correct |
20 ms |
8028 KB |
Output is correct |
34 |
Correct |
20 ms |
8168 KB |
Output is correct |
35 |
Correct |
12 ms |
8148 KB |
Output is correct |
36 |
Correct |
22 ms |
12360 KB |
Output is correct |
37 |
Correct |
28 ms |
16208 KB |
Output is correct |
38 |
Correct |
12 ms |
2396 KB |
Output is correct |
39 |
Correct |
46 ms |
9044 KB |
Output is correct |
40 |
Correct |
68 ms |
11044 KB |
Output is correct |
41 |
Correct |
84 ms |
12540 KB |
Output is correct |
42 |
Correct |
49 ms |
13000 KB |
Output is correct |
43 |
Correct |
67 ms |
17492 KB |
Output is correct |
44 |
Correct |
63 ms |
18908 KB |
Output is correct |
45 |
Correct |
13 ms |
2392 KB |
Output is correct |
46 |
Correct |
49 ms |
9276 KB |
Output is correct |
47 |
Correct |
53 ms |
11344 KB |
Output is correct |
48 |
Correct |
89 ms |
13616 KB |
Output is correct |
49 |
Correct |
53 ms |
13516 KB |
Output is correct |
50 |
Correct |
71 ms |
18328 KB |
Output is correct |
51 |
Correct |
79 ms |
19748 KB |
Output is correct |