#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(long long (i)=0;(i)<(n);++(i))
#define ref(i,a,b) for (long long (i)=(a); (i)<=(b); ++(i))
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define mp make_pair
const int mx=2e5+5;
int n,m;
vector <int > v[mx];
vector <int> k;
int root,dp[mx][505],a[mx],res;
void git(int sir,int sum,int kac){
if(kac>m)return;
if(sir==k.size()){
dp[res][kac]=max(dp[res][kac],sum);
return;
}
dp[res][kac]=max(sum,dp[res][kac]);
for(int i=0;i<=m;++i)git(sir+1,dp[k[sir]][i]+sum,kac+i);
return;
}
void dfs(int x){
k.clear();
for(int i=0;i<v[x].size();++i){
dfs(v[x][i]);
}
for(int i=0;i<v[x].size();++i){
k.pb(v[x][i]);
}
res=x;
git(0,0,0);
dp[x][1]=max(dp[x][1],a[x]);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i=1;i<=n;++i){
int x,y;cin >> x >> y;
if(x==0)root=i;
v[x].pb(i);
a[i]=y;
}
dfs(root);
//cout << dp[2][2]<<endl;
cout << dp[root][m] <<endl;
}
Compilation message
biochips.cpp: In function 'void git(int, int, int)':
biochips.cpp:18:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(sir==k.size()){
~~~^~~~~~~~~~
biochips.cpp: In function 'void dfs(int)':
biochips.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();++i){
~^~~~~~~~~~~~
biochips.cpp:31:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();++i){
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |