#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:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int i = 0; i < v[nod].size(); ++i)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5216 KB |
Output is correct |
4 |
Correct |
13 ms |
22740 KB |
Output is correct |
5 |
Correct |
14 ms |
24968 KB |
Output is correct |
6 |
Correct |
17 ms |
24916 KB |
Output is correct |
7 |
Correct |
233 ms |
298160 KB |
Output is correct |
8 |
Correct |
262 ms |
298160 KB |
Output is correct |
9 |
Correct |
322 ms |
362648 KB |
Output is correct |
10 |
Correct |
373 ms |
400248 KB |
Output is correct |