#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 2e9;
int dp[10005][105];
int newdp[105];
vector<int> adj[200005];
int val[200005];
int n, k;
void dfs(int nod, int parent)
{
for(auto it : adj[nod])
{
if(it != parent)
{
dfs(it, nod);
for(int i = 0; i <= k; i++)
newdp[i] = dp[nod][i];
for(int i = 0; i <= k; i++)
{
if(dp[it][i] == 0)
continue;
for(int j = k - i; j >= 0; j--)
if(dp[nod][j] != -INF)
newdp[i + j] = max(newdp[i + j], dp[nod][j] + dp[it][i]);
}
for(int i = 0; i <= k; i++)
dp[nod][i] = newdp[i];
}
}
dp[nod][1] = max(dp[nod][1], val[nod]);
}
signed main()
{
cin>>n>>k;
int root;
for(int i = 1; i <= n; i++)
{
int a, b;
cin>>a>>b;
if(a != 0)
{
adj[i].push_back(a);
adj[a].push_back(i);
}
else
root = i;
val[i] = b;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= k; j++)
dp[i][j] = -INF;
dfs(root, root);
cout<<dp[root][k];
return 0;
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:65:21: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | cout<<dp[root][k];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6596 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
28 ms |
14300 KB |
Output is correct |
5 |
Correct |
37 ms |
15268 KB |
Output is correct |
6 |
Correct |
52 ms |
15272 KB |
Output is correct |
7 |
Runtime error |
76 ms |
44624 KB |
Execution killed with signal 11 |
8 |
Runtime error |
78 ms |
44584 KB |
Execution killed with signal 11 |
9 |
Runtime error |
79 ms |
46636 KB |
Execution killed with signal 11 |
10 |
Runtime error |
86 ms |
48044 KB |
Execution killed with signal 11 |