#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int mxn=1e5+5;
int d[mxn]{0};ll w[mxn];
map<int,ll>dp[mxn];
vector<int>g[mxn];
void dfs(int u){
for(auto v:g[u]){
dfs(v);
if(dp[v].size()>dp[u].size())swap(dp[u],dp[v]);
for(auto it:dp[v]){
if(dp[u].find(it.f)!=dp[u].end())dp[u][it.f]+=it.s;
else dp[u][it.f]=it.s;
}
}
if(d[u]==0)return;
if(dp[u].find(d[u])==dp[u].end())dp[u][d[u]]=w[u];
else dp[u][d[u]]+=w[u];
ll cur=w[u];
auto it = dp[u].upper_bound(d[u]);
vector<int>mem;
for(;it!=dp[u].end();it++){
if(cur>=it->s){
cur-=it->s;
dp[u][it->f]=0;mem.pb(it->f);
}
else {
dp[u][it->f]-=cur;
break;
}
}for(auto it : mem)dp[u].erase(it);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m,k;cin>>n>>m>>k;
for(int i=2;i<=n;i++){
int p;cin>>p;g[p].pb(i);
}
for(int i=1;i<=m;i++){
int v;cin>>v;cin>>d[v]>>w[v];
}dfs(1);ll ans=0;
for(auto it : dp[1])ans+=it.s;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7444 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
17732 KB |
Output is correct |
2 |
Correct |
33 ms |
18772 KB |
Output is correct |
3 |
Correct |
109 ms |
34896 KB |
Output is correct |
4 |
Correct |
55 ms |
18384 KB |
Output is correct |
5 |
Correct |
72 ms |
19912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7512 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
35 ms |
25752 KB |
Output is correct |
5 |
Correct |
62 ms |
29780 KB |
Output is correct |
6 |
Correct |
60 ms |
25680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
13904 KB |
Output is correct |
2 |
Correct |
36 ms |
13140 KB |
Output is correct |
3 |
Correct |
34 ms |
17748 KB |
Output is correct |
4 |
Correct |
24 ms |
14660 KB |
Output is correct |
5 |
Correct |
35 ms |
25684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7444 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
44 ms |
16720 KB |
Output is correct |
11 |
Correct |
39 ms |
15700 KB |
Output is correct |
12 |
Correct |
31 ms |
17756 KB |
Output is correct |
13 |
Correct |
24 ms |
14656 KB |
Output is correct |
14 |
Correct |
34 ms |
25684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8028 KB |
Output is correct |
2 |
Correct |
14 ms |
10336 KB |
Output is correct |
3 |
Correct |
15 ms |
10332 KB |
Output is correct |
4 |
Correct |
16 ms |
10328 KB |
Output is correct |
5 |
Correct |
6 ms |
9176 KB |
Output is correct |
6 |
Correct |
15 ms |
12892 KB |
Output is correct |
7 |
Correct |
16 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7444 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
2 ms |
7512 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
35 ms |
25752 KB |
Output is correct |
14 |
Correct |
62 ms |
29780 KB |
Output is correct |
15 |
Correct |
60 ms |
25680 KB |
Output is correct |
16 |
Correct |
44 ms |
16720 KB |
Output is correct |
17 |
Correct |
39 ms |
15700 KB |
Output is correct |
18 |
Correct |
31 ms |
17756 KB |
Output is correct |
19 |
Correct |
24 ms |
14656 KB |
Output is correct |
20 |
Correct |
34 ms |
25684 KB |
Output is correct |
21 |
Correct |
14 ms |
10588 KB |
Output is correct |
22 |
Correct |
46 ms |
18004 KB |
Output is correct |
23 |
Correct |
51 ms |
21332 KB |
Output is correct |
24 |
Correct |
82 ms |
29012 KB |
Output is correct |
25 |
Correct |
58 ms |
18200 KB |
Output is correct |
26 |
Correct |
72 ms |
20052 KB |
Output is correct |
27 |
Correct |
66 ms |
19476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7256 KB |
Output is correct |
2 |
Correct |
2 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7444 KB |
Output is correct |
5 |
Correct |
2 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7260 KB |
Output is correct |
9 |
Correct |
2 ms |
7260 KB |
Output is correct |
10 |
Correct |
38 ms |
17732 KB |
Output is correct |
11 |
Correct |
33 ms |
18772 KB |
Output is correct |
12 |
Correct |
109 ms |
34896 KB |
Output is correct |
13 |
Correct |
55 ms |
18384 KB |
Output is correct |
14 |
Correct |
72 ms |
19912 KB |
Output is correct |
15 |
Correct |
2 ms |
7512 KB |
Output is correct |
16 |
Correct |
2 ms |
7512 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
35 ms |
25752 KB |
Output is correct |
19 |
Correct |
62 ms |
29780 KB |
Output is correct |
20 |
Correct |
60 ms |
25680 KB |
Output is correct |
21 |
Correct |
36 ms |
13904 KB |
Output is correct |
22 |
Correct |
36 ms |
13140 KB |
Output is correct |
23 |
Correct |
34 ms |
17748 KB |
Output is correct |
24 |
Correct |
24 ms |
14660 KB |
Output is correct |
25 |
Correct |
35 ms |
25684 KB |
Output is correct |
26 |
Correct |
44 ms |
16720 KB |
Output is correct |
27 |
Correct |
39 ms |
15700 KB |
Output is correct |
28 |
Correct |
31 ms |
17756 KB |
Output is correct |
29 |
Correct |
24 ms |
14656 KB |
Output is correct |
30 |
Correct |
34 ms |
25684 KB |
Output is correct |
31 |
Correct |
4 ms |
8028 KB |
Output is correct |
32 |
Correct |
14 ms |
10336 KB |
Output is correct |
33 |
Correct |
15 ms |
10332 KB |
Output is correct |
34 |
Correct |
16 ms |
10328 KB |
Output is correct |
35 |
Correct |
6 ms |
9176 KB |
Output is correct |
36 |
Correct |
15 ms |
12892 KB |
Output is correct |
37 |
Correct |
16 ms |
16732 KB |
Output is correct |
38 |
Correct |
14 ms |
10588 KB |
Output is correct |
39 |
Correct |
46 ms |
18004 KB |
Output is correct |
40 |
Correct |
51 ms |
21332 KB |
Output is correct |
41 |
Correct |
82 ms |
29012 KB |
Output is correct |
42 |
Correct |
58 ms |
18200 KB |
Output is correct |
43 |
Correct |
72 ms |
20052 KB |
Output is correct |
44 |
Correct |
66 ms |
19476 KB |
Output is correct |
45 |
Correct |
15 ms |
11096 KB |
Output is correct |
46 |
Correct |
47 ms |
18772 KB |
Output is correct |
47 |
Correct |
58 ms |
22096 KB |
Output is correct |
48 |
Correct |
94 ms |
31276 KB |
Output is correct |
49 |
Correct |
55 ms |
18388 KB |
Output is correct |
50 |
Correct |
74 ms |
20308 KB |
Output is correct |
51 |
Correct |
71 ms |
19596 KB |
Output is correct |