#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define v vector
#define lp(i,s,e)for(ll i =s;i<e;++i)
struct st {
ll req, profit;
bool operator<(const st& other) const {
return req > other.req;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,s;
cin>>n>>s;
v<v<ll>> g(n);
v<ll> bef(n), pro(n);
lp(i,0,n) {
cin>>pro[i]>>bef[i];
bef[i]--;
}
lp(i,0,n) {
if (bef[i]!=-1)g[bef[i]].push_back(i);
}
v<st> after(n);
for (ll i = n-1;i>=0;--i) {
after[i].req= (pro[i]>0 ? 0: -pro[i]);
after[i].profit = pro[i];
priority_queue<st, v<st>> pq;
for (auto it : g[i]) {
pq.push({after[it].req, after[it].profit});
}
while (!pq.empty()) {
auto [req,prof] = pq.top();
pq.pop();
if (after[i].profit >req && prof>0) {// we can and want to merge
after[i].req = max(after[i].req, req-after[i].profit);
after[i].profit+=prof;
}
}
}
ll ans = 0;
lp(i,0,n)ans = max(ans, after[i].profit);
cout<<ans<<'\n';
}