/*
IZhO 12-biochips
This
*/
#include<bits/stdc++.h>
using namespace std;
int n, m, root, val[200002];
vector<int>v[200002];
int dp1[200002][502], dp2[200002][502], sz1[200002], sz2[200002];
int qq = 0;
void dfs(int nod)
{
for(int i = 1; i <= m; ++i)
dp1[nod][i] = dp2[nod][i] = -1;
dp1[nod][1] = val[nod];
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 || dp1[vecin][k] != -1)
dp2[nod][j + k] = max(dp2[nod][j + k], dp2[nod][j] + max(dp1[vecin][k], dp2[vecin][k]));
}
}
}
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);
cout << max(dp1[root][m], dp2[root][m]) << '\n';
return 0;
}
Compilation message
biochips.cpp: In function 'void dfs(int)':
biochips.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v[nod].size(); ++i)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5184 KB |
Output is correct |
3 |
Correct |
6 ms |
5560 KB |
Output is correct |
4 |
Correct |
39 ms |
40136 KB |
Output is correct |
5 |
Correct |
47 ms |
44616 KB |
Output is correct |
6 |
Correct |
49 ms |
44616 KB |
Output is correct |
7 |
Execution timed out |
2087 ms |
448876 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |