#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 305;
int n, m, fa[MAXN], val[MAXN], dp[MAXN][MAXN]; //表示i为根的子树中选j个的答案(不包含i)

inline void dfs(int u, int c)
{
for(int v = 1; v <= n; v++) if(fa[v] == u)
{
for(int j = 0; j < c; j++) dp[v][j] = dp[u][j]; //继承其它儿子的答案
dfs(v, c-1);
for(int j = 1; j <= c; j++) dp[u][j] = max(dp[u][j], dp[v][j-1] + val[v]);
}
}

int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d%d", &fa[i], &val[i]);
dfs(0, m);
printf("%d\n", dp[0][m]);
}