#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>con[200010];
int p,pos[200010],arr[200010],n,m,a[200010],d[200010][510],w=1;
void dfs(int x)
{
int i,j;
arr[x]=w;
for(i=0;i<con[x].size();i++)
{
dfs(con[x][i]);
}
w++;
pos[w]=x;
for(i=1;i<=m;i++) d[w][i]=max(d[w][i],d[w-1][i]);
for(i=1;i<=m;i++) d[w][i]=max(d[w][i],d[arr[x]][i-1]+a[x]);
}
int main()
{
int i,j,x,y;
scanf("%d %d",&n,&m);
for(i=0;i<=n;i++){
for(j=1;j<=m;j++){
d[i][j]=-2147483647;
}
}
for(i=1;i<=n;i++)
{
scanf("%d %d",&x,&y);
if(x==0) p=i;
con[x].push_back(i);
a[i]=y;
}
dfs(p);
printf("%d",d[w][m]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
406704 KB |
Output is correct |
2 |
Correct |
2 ms |
406704 KB |
Output is correct |
3 |
Correct |
0 ms |
406704 KB |
Output is correct |
4 |
Correct |
4 ms |
406836 KB |
Output is correct |
5 |
Correct |
5 ms |
406704 KB |
Output is correct |
6 |
Correct |
5 ms |
406704 KB |
Output is correct |
7 |
Correct |
270 ms |
407760 KB |
Output is correct |
8 |
Correct |
226 ms |
407760 KB |
Output is correct |
9 |
Correct |
325 ms |
407628 KB |
Output is correct |
10 |
Correct |
417 ms |
407628 KB |
Output is correct |