#include <bits/stdc++.h>
using namespace std;
int n, m;
struct node
{
int val, fa;
vector <int> son;
#define add(x) son.push_back(x)
#define sz() son.size()
}a[305];
int f[305][305];
void dfs(int x, int tot)
{
if(tot <= 0)return;
for(int i = 0; i < a[x].sz(); i++)
{
int y = a[x].son[i];
for(int j = 0; j < tot; j++)
f[y][j] = f[x][j] + a[y].val;
dfs(y, tot - 1);
for(int j = 1; j <= tot; j++)
f[x][j] = max(f[x][j], f[y][j - 1]);
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
int x;
cin >> x >> a[i].val;
a[x].add(i);
a[i].fa = x;
}
dfs(0, m + 1);
cout << f[0][m + 1];//<---here
return 0;
}
不是说加一个根节点之后要选的总节点也加1吗?为啥我把m+1改成m就AC了,但是m+1全WA