#include <bits/stdc++.h>
using namespace std;

void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans){

    vector<int> deg(n+1, 1);
    for(int i = 2; i <= n; i++){
        deg[p[i]]++;
    }

    bool zvezda = false;
    for(int i = 1; i <= n; i++){
        if(deg[i] == n-1){
            zvezda = true;
            break;
        }
    }
    if(zvezda){
        multiset<int> tez;

        for(int i = 2; i<=n; i++) tez.insert(w[i]);

        for(int i = 1; i <= q; i++){
            tez.extract(w[s[i]]);
            tez.insert(k[i]);
            w[s[i]] = k[i];
            ans[i] = *tez.rbegin() + *next(tez.rbegin()) + *next(next(tez.rbegin()));
        }
    }
}