#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 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);
for (int j = k; j >= 0 ; j--)
{
for (int k1 = 0; k1 + j <=k ; k1++)
{
dp[x][j+k1] = max(dp[x][j+k1], dp[u][k1] + dp[x][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;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
40320 KB |
Output is correct |
2 |
Correct |
27 ms |
40312 KB |
Output is correct |
3 |
Correct |
25 ms |
40320 KB |
Output is correct |
4 |
Correct |
47 ms |
40576 KB |
Output is correct |
5 |
Correct |
62 ms |
40448 KB |
Output is correct |
6 |
Correct |
88 ms |
40580 KB |
Output is correct |
7 |
Runtime error |
11 ms |
3584 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |