/*
IZhO 12-biochips
This
*/
#include<bits/stdc++.h>
using namespace std;
int n, m, root, val[200002];
vector<int>v[200002];
int dp2[200002][502], sz1[200002], sz2[200002];
int qq = 0;
void dfs(int nod)
{
for(int i = 1; i <= m; ++i)
dp2[nod][i] = -1;
for(int i = 0; i < v[nod].size(); ++i)
{
int vecin = v[nod][i];
dfs(vecin);
for(int j = m; j >= 0; --j)
{
if(dp2[nod][j] != -1)
for(int k = 0; k + j <= m; ++k)
{
if(dp2[vecin][k] != -1)
dp2[nod][j + k] = max(dp2[nod][j + k], dp2[nod][j] + dp2[vecin][k]);
if(k == 1)
dp2[nod][j + k] = max(dp2[nod][j + k], dp2[nod][j] + val[vecin]);
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; ++i)
{
int parent;
cin >> parent;
cin >> val[i];
if(parent == 0)
root = i;
else
v[parent].push_back(i);
}
dfs(root);
int ans = 0;
if(m == 1)
ans = val[root];
cout << max(ans, dp2[root][m]) << '\n';
return 0;
}
Compilation message
biochips.cpp: In function 'void dfs(int)':
biochips.cpp:19:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v[nod].size(); ++i)
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5244 KB |
Output is correct |
3 |
Correct |
6 ms |
5316 KB |
Output is correct |
4 |
Correct |
32 ms |
22912 KB |
Output is correct |
5 |
Correct |
34 ms |
25012 KB |
Output is correct |
6 |
Correct |
38 ms |
25052 KB |
Output is correct |
7 |
Execution timed out |
2071 ms |
218872 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |