#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
const int maxn=505;
int n,m,root;
int Left[maxn],Right[maxn],w[maxn],f[maxn][maxn];
int dfs(int i,int j)
{
if(i<1||j<1||i>n+1||j>m)return 0;
if(f[i][j]!=0)return f[i][j];
if(i!=n+1)
{
for(int k=0;k<j;k++)
f[i][j]=max(f[i][j],dfs(Right[i],k)+dfs(Left[i],j-k-1)+w[i]);
f[i][j]=max(f[i][j],dfs(Right[i],j));
}
else f[i][j]=max(f[i][j],dfs(Left[i],j));
return f[i][j];
}
int cnt,p[maxn];
void find(int root,int prize,int s)
{
if(root<1||s<1||root>n+1||s>m)return;
if(root!=n+1)
{
if(prize==f[Right[root]][s])
{
find(Right[root],prize,s);
}
else
for(int j=0;j<s;j++)
{
if(prize==f[Right[root]][j]+f[Left[root]][s-j-1]+w[root])
{
p[++cnt]=root;
if(f[Left[root]][s-j-1])find(Left[root],f[Left[root]][s-j-1],s-j-1);
if(f[Right[root]][j])find(Right[root],f[Right[root]][j],j);
}
}
}
else
{
find(Left[root],f[Left[root]][s],s);
}
return;
}
int main()
{
int i;
scanf("%d%d",&n,&m);
root=n+1;
for(i=1;i<=n;i++)
{
int dad;
scanf("%d%d",&dad,&w[i]);
if(!dad)dad=root;
Right[i]=Left[dad];
Left[dad]=i;
}
printf("%d\n",dfs(root,m));
find(root,f[root][m],m);
sort(p+1,p+cnt+1);
for(i=1;i<=cnt;i++)
printf("%d\n",p[i]);
return 0;
}