#include <bits/stdc++.h>

using namespace std;

#define MAXN 200001

int deg[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++) {deg[i]++;deg[p[i]]++;}
    bool subtask2=false;
    for (int node=1;node<=n;node++)
    {
        if (deg[node]==n-1) {subtask2=true;break;}
    }
    if (subtask2)
    {
        multiset<int> weights;for (int node=2;node<=n;node++) weights.insert(w[node]);

        for (int i=1;i<=q;i++)
        {
            weights.erase(weights.find(w[s[i]]));weights.insert(k[i]);w[s[i]]=k[i];
            auto it=weights.end();int answer=0;
            it--;answer+=(*it);it--;answer+=(*it);it--;answer+=(*it);ans[i]=answer;
        }
        return;
    }
};