#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define sc second
#define pb push_back

using namespace std;

const int MAXN = 2e5+5;

vector<vector<pii>> graf(MAXN);




void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans){
    for(int i=2; i<=n; i++)
    {
        graf[i].pb({p[i],w[i]});
        graf[p[i]].pb({i,w[i]});
    }
    int mxadj = 0;
    int node = -1;
    for(int i=1; i<=n; i++)
    {
        if((int)graf[i].size() > mxadj)
        {
            mxadj = (int)graf[i].size();
            node = i;
        }
    }
    if(mxadj == n-1)
    {
        multiset<int> setic;
        for(auto x: graf[node])setic.insert(x.sc);
        //for(int i=2; i<=n; i++)setic.insert(w[i]);
        //for(auto x: setic)cout << x << endl;
        for(int j=1; j<=q; j++)
        {
            int x = w[s[j]];
            setic.erase(setic.find(w[s[j]]));
            w[s[j]] = k[j];
            //cout << k[j] << endl;
            setic.insert(k[j]);
            //for(auto x: setic)cout << x << endl;
            long long sum = *prev(setic.end()) + *prev(prev(setic.end())) + *prev(prev(prev(setic.end())));
            ans[j] = sum;
        }
    }
}

/*
5 3
0 1 2 2 2
0 3 2 6 1
2 5 5
4 10 1
*/