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

long long dfs(int v, int* p, vector<vector<int>>& c, int* w, vector<bool>& vis){
    vis[v] = true;

    long long m = 0;
    if(v != 1 && !vis[p[v]]) m = max(m, dfs(p[v], p, c, w, vis)+w[v]);

    for(int i = 0; i < c[v].size(); i++){
        if(!vis[c[v][i]]) m = max(m, dfs(c[v][i], p, c, w, vis)+w[c[v][i]]);
    }

    return m;
}

void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans){
    for(int i = 0; i < q; i++){
        ans[i+1] = 0;
    }

    int m = 0, in;
    vector<vector<int>> c(n+1);
    for(int i = 2; i < n+1; i++){
        c[p[i]].push_back(i);
        if(c[p[i]].size() > m){
            m = c[p[i]].size();
            in = p[i];
        }
    }

    if(n <= 200 && q <= 200){
        for(int qt = 0; qt < q; qt++){
            //cout << qt << q << endl;
            w[s[qt+1]] = k[qt+1];

            for(int i = 1; i < n+1; i++){
                if(c[i].size() < 2 || (i == 1 && c[i].size() < 3)) continue;
                //cout << i << endl;

                vector<long long> r;
                vector<bool> vis(n+1, false);
                vis[i] = true;
                if(i != 1) r.push_back(dfs(p[i], p, c, w, vis)+w[i]);

                for(int j = 0; j < c[i].size(); j++){
                    r.push_back(dfs(c[i][j], p, c, w, vis)+w[c[i][j]]);
                }

                for(int i = 0; i < r.size(); i++){
                    //cout << r[i] << " ";
                }
                //cout << endl;

                sort(r.begin(), r.end());

                //cout << (long long)r[r.size()-1] + r[r.size()-2] + r[r.size()-3] << endl;

                ans[qt+1] = max(ans[qt+1], (long long)r[r.size()-1] + r[r.size()-2] + r[r.size()-3]);

                //cout << ans[qt+1] << endl;
            }
        }
    }
    else if((m == n-2 && p[in] == 1) || m == n-1){
        //cout << "yessir" << endl;
        set<long long> r;
        vector<long long> a(n+1);
        for(int i = 2; i < n+1; i++){
            a[i] = w[i];
        }

        for(int i = 0; i < m; i++){
            r.insert(w[c[in][i]]);
        }
        if(m == n-2) r.insert(w[in]);

        for(int qt = 1; qt < q+1; qt++){
            r.erase(a[s[qt]]);
            r.insert(k[qt]);

            a[s[qt]] = k[qt];

            long long v1 = *r.rbegin();
            r.erase(*r.rbegin());
            long long v2 = *r.rbegin();
            r.erase(*r.rbegin());
            long long v3 = *r.rbegin();

            r.insert(v2);
            r.insert(v3);

            ans[qt] = v1+v2+v3;
        }
    }

    return;
}