#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));

    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));
    }

    return m+w[v];
}

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;
    }

    vector<vector<int>> c(n+1);
    for(int i = 2; i < n+1; i++){
        c[p[i]].push_back(i);
    }

    if(q == 1 || (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));

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

                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;
            }
        }
    }

    return;
}