#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);
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 |
201 ms |
400376 KB |
Output is correct |
2 |
Correct |
194 ms |
400376 KB |
Output is correct |
3 |
Correct |
204 ms |
400376 KB |
Output is correct |
4 |
Correct |
224 ms |
400504 KB |
Output is correct |
5 |
Correct |
242 ms |
400556 KB |
Output is correct |
6 |
Correct |
274 ms |
400504 KB |
Output is correct |
7 |
Execution timed out |
2058 ms |
403448 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |