#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e2 + 5;
const int INF = 1e18;
int Value[N];
vector<int> Graph[N];
int Need[N];
int To[N];
int Profit[N];
priority_queue<pair<int, int>> PQ;
void DFS(int u, int from, int sum, int mini)
{
sum += Value[u];
mini = min(mini, sum);
if(sum >= 0) {
To[from] = u;
Need[from] = mini;
from = -1;
}
Profit[u] = Value[u];
if(Graph[u].size() > 0) {
int v = Graph[u][0];
if(from == -1)
DFS(v, v, 0, 0);
else
DFS(v, from, sum, mini);
if(Profit[v] > 0)
Profit[u] += Profit[v];
}
}
int Solve(int s)
{
int starter = s;
while(!PQ.empty()) {
if(s + PQ.top().first < 0)
break;
auto [d, u] = PQ.top();
PQ.pop();
int lim = To[u];
do {
s += Value[u];
if(u == lim)
break;
u = Graph[u][0];
}while(true);
if(Graph[u].size() > 0) {
if(Profit[Graph[u][0]] > 0)
PQ.push({Need[Graph[u][0]], Graph[u][0]});
}
}
return (s - starter);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, s, x, p;
cin >> n >> s;
for(int i = 0; i < n; i++) {
cin >> x >> p;
Value[i+1] = x;
Graph[p].push_back(i+1);
}
for(auto e : Graph[0]) {
DFS(e, e, 0, 0);
if(Profit[e] > 0)
PQ.push({Need[e], e});
}
cout << Solve(s) << "\n";
return 0;
}