#include <bits/stdc++.h>
using namespace std ;
const int MAXN = 2e5 + 5 , MAXM = 505 ;
int arr[MAXN] , dp[MAXN][MAXM] , tmpdp[MAXM] ;
int n , m ;
vector< vector<int> >adj(MAXN) ;
void dfs(int node)
{
dp[node][0] = 0 ;
for(auto &child : adj[node])
{
dfs(child) ;
int Max = 0 ;
for(int k1 = 0 ; k1 <= m ; ++k1)
{
if(!dp[node][k1] && k1)
break ;
for(int k2 = 1 ; k1 + k2 <= m ; ++k2)
{
if(!dp[child][k2] && k2)
break ;
Max = max(Max , k1+k2) ;
tmpdp[k1+k2] = max({tmpdp[k1+k2] , dp[node][k1+k2] , dp[node][k1] + dp[child][k2]}) ;
}
}
for(int i = 1 ; i <= Max ; ++i)
dp[node][i] = tmpdp[i] , tmpdp[i] = 0 ;
}
dp[node][1] = max(dp[node][1] , arr[node]) ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
int root ;
for(int i = 1 ; i <= n ; ++i)
{
int par ;
cin>>par>>arr[i] ;
if(par == 0)
root = i ;
else
adj[par].push_back(i) ;
}
dfs(root) ;
return cout<<dp[root][m]<<"\n" , 0 ;
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:53:28: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
return cout<<dp[root][m]<<"\n" , 0 ;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5120 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5248 KB |
Output is correct |
4 |
Correct |
19 ms |
22784 KB |
Output is correct |
5 |
Correct |
21 ms |
25088 KB |
Output is correct |
6 |
Correct |
22 ms |
25140 KB |
Output is correct |
7 |
Correct |
370 ms |
299768 KB |
Output is correct |
8 |
Correct |
361 ms |
299640 KB |
Output is correct |
9 |
Correct |
542 ms |
364024 KB |
Output is correct |
10 |
Correct |
725 ms |
401784 KB |
Output is correct |