usingnamespacestd; int f[305][305], head[305], tot, scores[305], sz[305];
structedge{ int to, nxt; } edges[305];
voidadd(int u, int v){ edges[++tot] = {v, head[u]}; head[u] = tot; }
voiddfs(int u){ sz[u] = 1; f[u][1] = scores[u]; for (int i = head[u]; i; i = edges[i].nxt) { int v = edges[i].to; dfs(v); sz[u] += sz[v]; for (int j = sz[u]; j >= 1; --j) { for (int k = min(j - 1, sz[v]); k >= 0; --k) { f[u][j] = max(f[u][j], f[u][j - k] + f[v][k]); } } } }
intmain(int argc, char *argv[]){ int n, m; cin >> n >> m; int k, s; for (int i = 1; i <= n; ++i) { cin >> k >> scores[i]; add(k, i); } dfs(0); cout << f[0][++m] << endl; //注意包含了0节点,因此要多选一门 return0; }