#include <bits/stdc++.h>
#define N 200005
using namespace std;
int n,m;
vector<int> adj[N];
int par[N];
int arr[N];
int ans = 0;
vector<int> dfs(int v,int c,vector<int> dp){
dp[0] = 0;
vector<int> tmp = dp;
for(auto u:adj[v]){
if(u == c)continue;
dp = dfs(u,-1,dp);
}
for(int i=1;i<=m;i++){
dp[i] = max(dp[i],tmp[i-1]+arr[v]);
}
ans = max(ans,dp[m]);
if(c != -1 && par[v]){
dfs(par[v],v,dp);
}
/*
cout << v << endl;
for(auto u:dp){
cout << u << " ";
}
cout << endl;*/
return dp;
}
void solve(){
cin >> n >> m;
int root = 0;
for(int i=1;i<=n;i++){
cin >> par[i] >> arr[i];
if(par[i] == 0)root = i;
else adj[par[i]].push_back(i);
}
for(int i=1;i<=n;i++){
if(adj[i].empty()){
dfs(i,0,vector<int>(m+1,-1e9));
break;
}
}
cout << ans << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
biochips.cpp: In function 'void solve()':
biochips.cpp:33:9: warning: variable 'root' set but not used [-Wunused-but-set-variable]
33 | int root = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
6 ms |
5324 KB |
Output is correct |
5 |
Correct |
7 ms |
5280 KB |
Output is correct |
6 |
Correct |
7 ms |
5196 KB |
Output is correct |
7 |
Correct |
105 ms |
8752 KB |
Output is correct |
8 |
Correct |
107 ms |
8756 KB |
Output is correct |
9 |
Correct |
133 ms |
11972 KB |
Output is correct |
10 |
Incorrect |
161 ms |
13040 KB |
Output isn't correct |