#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > child[300005];
long long n, m, ans;
pair<long long, long long> dfs(long long u)
{
long long sz=child[u].size();
if (!sz)
return {0, 0};
vector<pair<long long, long long> > vec2;
vector<long long> vec;
for (long long i=0; i<sz; i++)
{
long long v=child[u][i].first, w=child[u][i].second;
pair<long long, long long> res=dfs(v);
vec2.push_back({res.first+w, res.second+w});
vec.push_back(res.first+w);
vec.push_back(res.second+w);
}
sort(vec.begin(), vec.end());
for (long long i=0; i<sz; i++)
ans+=max(0LL, max(vec2[i].first-vec[sz], vec[sz]-vec2[i].second));
return {vec[sz-1], vec[sz]};
}
long long main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i=2; i<=n+m; i++)
{
long long p, c;
cin >> p >> c;
child[p].push_back({i, c});
}
dfs(1);
cout << ans;
}
Compilation message
cc1plus: error: '::main' must return 'int'