#include<bits/stdc++.h>
using namespace std;
int n, m, root, val[200002];
vector<int>v[200002];
int dp[200002][502], val2[200002], st[200002], sf[200002], poz;
int qq = 0;
void dfs(int nod)
{
++poz;
st[nod] = poz;
val2[poz] = val[nod];
dp[poz][1] = val[nod];
for(int i = 0; i < v[nod].size(); ++i)
{
int vecin = v[nod][i];
dfs(vecin);
}
sf[st[nod]] = poz;
}
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;
for(int i = poz; i >= 1; --i)
for(int j = 1; j <= m; ++j)
{
if(i + 1 <= poz)
dp[i][j] = max(dp[i][j], dp[i+1][j]);
if(sf[i] + 1 <= poz)
dp[i][j] = max(dp[i][j] , dp[sf[i] + 1][j-1] + val2[i]);
if(j == m)
ans = max(ans, dp[i][j]);
}
cout << ans;
return 0;
}
Compilation message
biochips.cpp: In function 'void dfs(int)':
biochips.cpp:14:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v[nod].size(); ++i)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
5240 KB |
Output is correct |
4 |
Correct |
20 ms |
22904 KB |
Output is correct |
5 |
Correct |
22 ms |
25080 KB |
Output is correct |
6 |
Correct |
26 ms |
25080 KB |
Output is correct |
7 |
Correct |
440 ms |
299756 KB |
Output is correct |
8 |
Correct |
368 ms |
299896 KB |
Output is correct |
9 |
Correct |
553 ms |
364012 KB |
Output is correct |
10 |
Correct |
603 ms |
401912 KB |
Output is correct |