#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n, m, k;
vi G[100100];
vector<pll> fruits;
map<int, ll> dp[100100];
void dfs(int node, int p){
// cout<<"AT: "<<node<<endl;
for(auto next : G[node]){
if(next !=p){
dfs(next, node);
if(dp[node].size() < dp[next].size()) swap(dp[node], dp[next]);
for(auto t : dp[next]) dp[node][t.first] += t.second;
}
}
if(fruits[node].first != -1){
ll hold = fruits[node].second;
while(hold > 0){
auto rem = dp[node].upper_bound(fruits[node].first);
if(rem == dp[node].end()) break;
if(rem->second <= hold){
hold -= rem->second;
dp[node].erase(rem);
}
else{
rem->second -= hold;
break;
}
}
dp[node][fruits[node].first] += fruits[node].second;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
for(int i = 2; i <= n; i++){
int a;
cin>>a;
G[a-1].pb(i-1);
G[i-1].pb(a-1);
}
fruits.resize(n+1);
for(int i = 0; i < n; i++)
fruits[i] = mp(-1, -1);
for(int i = 0; i < m; i++){
int node, day, add;
cin>>node>>day>>add;
node--;
fruits[node] = mp(day, add);
}
ll ans = 0;
dfs(0, -1);
// cout<<dp[0].size()<<endl;
for(auto t : dp[0]) ans += t.second;
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
19916 KB |
Output is correct |
2 |
Correct |
49 ms |
21020 KB |
Output is correct |
3 |
Correct |
133 ms |
40660 KB |
Output is correct |
4 |
Correct |
76 ms |
23876 KB |
Output is correct |
5 |
Correct |
113 ms |
25552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7424 KB |
Output is correct |
3 |
Correct |
3 ms |
7508 KB |
Output is correct |
4 |
Correct |
55 ms |
24524 KB |
Output is correct |
5 |
Correct |
59 ms |
28428 KB |
Output is correct |
6 |
Correct |
55 ms |
24592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
15740 KB |
Output is correct |
2 |
Correct |
69 ms |
15048 KB |
Output is correct |
3 |
Correct |
47 ms |
18132 KB |
Output is correct |
4 |
Correct |
48 ms |
18096 KB |
Output is correct |
5 |
Correct |
40 ms |
24528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
83 ms |
18672 KB |
Output is correct |
11 |
Correct |
72 ms |
17612 KB |
Output is correct |
12 |
Correct |
49 ms |
18100 KB |
Output is correct |
13 |
Correct |
35 ms |
18012 KB |
Output is correct |
14 |
Correct |
41 ms |
24540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8532 KB |
Output is correct |
2 |
Correct |
30 ms |
12272 KB |
Output is correct |
3 |
Correct |
30 ms |
12344 KB |
Output is correct |
4 |
Correct |
30 ms |
12360 KB |
Output is correct |
5 |
Correct |
19 ms |
12492 KB |
Output is correct |
6 |
Correct |
30 ms |
14804 KB |
Output is correct |
7 |
Correct |
27 ms |
17108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
3 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7424 KB |
Output is correct |
12 |
Correct |
3 ms |
7508 KB |
Output is correct |
13 |
Correct |
55 ms |
24524 KB |
Output is correct |
14 |
Correct |
59 ms |
28428 KB |
Output is correct |
15 |
Correct |
55 ms |
24592 KB |
Output is correct |
16 |
Correct |
83 ms |
18672 KB |
Output is correct |
17 |
Correct |
72 ms |
17612 KB |
Output is correct |
18 |
Correct |
49 ms |
18100 KB |
Output is correct |
19 |
Correct |
35 ms |
18012 KB |
Output is correct |
20 |
Correct |
41 ms |
24540 KB |
Output is correct |
21 |
Correct |
17 ms |
10904 KB |
Output is correct |
22 |
Correct |
61 ms |
20040 KB |
Output is correct |
23 |
Correct |
74 ms |
23240 KB |
Output is correct |
24 |
Correct |
136 ms |
30924 KB |
Output is correct |
25 |
Correct |
58 ms |
21812 KB |
Output is correct |
26 |
Correct |
96 ms |
22108 KB |
Output is correct |
27 |
Correct |
78 ms |
19760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7372 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
61 ms |
19916 KB |
Output is correct |
11 |
Correct |
49 ms |
21020 KB |
Output is correct |
12 |
Correct |
133 ms |
40660 KB |
Output is correct |
13 |
Correct |
76 ms |
23876 KB |
Output is correct |
14 |
Correct |
113 ms |
25552 KB |
Output is correct |
15 |
Correct |
3 ms |
7508 KB |
Output is correct |
16 |
Correct |
4 ms |
7424 KB |
Output is correct |
17 |
Correct |
3 ms |
7508 KB |
Output is correct |
18 |
Correct |
55 ms |
24524 KB |
Output is correct |
19 |
Correct |
59 ms |
28428 KB |
Output is correct |
20 |
Correct |
55 ms |
24592 KB |
Output is correct |
21 |
Correct |
62 ms |
15740 KB |
Output is correct |
22 |
Correct |
69 ms |
15048 KB |
Output is correct |
23 |
Correct |
47 ms |
18132 KB |
Output is correct |
24 |
Correct |
48 ms |
18096 KB |
Output is correct |
25 |
Correct |
40 ms |
24528 KB |
Output is correct |
26 |
Correct |
83 ms |
18672 KB |
Output is correct |
27 |
Correct |
72 ms |
17612 KB |
Output is correct |
28 |
Correct |
49 ms |
18100 KB |
Output is correct |
29 |
Correct |
35 ms |
18012 KB |
Output is correct |
30 |
Correct |
41 ms |
24540 KB |
Output is correct |
31 |
Correct |
8 ms |
8532 KB |
Output is correct |
32 |
Correct |
30 ms |
12272 KB |
Output is correct |
33 |
Correct |
30 ms |
12344 KB |
Output is correct |
34 |
Correct |
30 ms |
12360 KB |
Output is correct |
35 |
Correct |
19 ms |
12492 KB |
Output is correct |
36 |
Correct |
30 ms |
14804 KB |
Output is correct |
37 |
Correct |
27 ms |
17108 KB |
Output is correct |
38 |
Correct |
17 ms |
10904 KB |
Output is correct |
39 |
Correct |
61 ms |
20040 KB |
Output is correct |
40 |
Correct |
74 ms |
23240 KB |
Output is correct |
41 |
Correct |
136 ms |
30924 KB |
Output is correct |
42 |
Correct |
58 ms |
21812 KB |
Output is correct |
43 |
Correct |
96 ms |
22108 KB |
Output is correct |
44 |
Correct |
78 ms |
19760 KB |
Output is correct |
45 |
Correct |
19 ms |
11604 KB |
Output is correct |
46 |
Correct |
73 ms |
22180 KB |
Output is correct |
47 |
Correct |
98 ms |
25668 KB |
Output is correct |
48 |
Correct |
130 ms |
35760 KB |
Output is correct |
49 |
Correct |
69 ms |
23892 KB |
Output is correct |
50 |
Correct |
88 ms |
24840 KB |
Output is correct |
51 |
Correct |
76 ms |
22360 KB |
Output is correct |