#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, k;
int root;
int dp[N][505];
int a[N];
vector<int> adj[N];
void dfs(int x)
{
int ret = 0;
dp[x][0] = 0;
for (auto u : adj[x])
{
dfs(u);
int mxx = 0 ;
vector<int> auxdp (k+1 , 0) ;
for (int j = 0; j <= k ; j++)
{
if(dp[j] < 0) break;
for (int k1 = 0; k1 + j <=k ; k1++)
{
if(dp[u][k1] < 0) break ;
mxx = max(mxx , k1 + j) ;
auxdp[j+k1] = max(auxdp[j+k1] , dp[x][j] + dp[u][k1]) ;
}
}
for(int j = 0 ;j <= k; j++){
dp[x][j] = max(dp[x][j] , auxdp[j]) ;
}
}
dp[x][1] = max(dp[x][1] , a[x]) ;
return ;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("in.in", "r", stdin);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
int u ;
cin >> u >> a[i];
if (u)
adj[u].push_back(i);
else
root = i;
}
memset(dp, -1, sizeof dp);
dfs(root) ;
cout<< dp[root][k] ;
return 0;
}
Compilation message
biochips.cpp: In function 'void dfs(int)':
biochips.cpp:15:13: warning: unused variable 'ret' [-Wunused-variable]
int ret = 0;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
400376 KB |
Output is correct |
2 |
Correct |
200 ms |
400376 KB |
Output is correct |
3 |
Correct |
196 ms |
400376 KB |
Output is correct |
4 |
Correct |
215 ms |
400504 KB |
Output is correct |
5 |
Correct |
225 ms |
400504 KB |
Output is correct |
6 |
Correct |
226 ms |
400504 KB |
Output is correct |
7 |
Correct |
546 ms |
402040 KB |
Output is correct |
8 |
Correct |
531 ms |
403576 KB |
Output is correct |
9 |
Correct |
734 ms |
403576 KB |
Output is correct |
10 |
Correct |
887 ms |
403704 KB |
Output is correct |