#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
typedef long long ll;

map<pair<int,int>, int> wts;
ll dfs(int u, int p, vector<vector<int>> g){

    ll ans = 0;
    for(int v : g[u]){
        if(v == p) continue;
        ans = max(ans, 1LL * dfs(v, u, g) + wts[{u, v}]);
    }
    return ans;
}



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

    if(n > 300 || q > 300) return;

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

        wts[{i, p[i]}] = w[i];
        wts[{p[i], i}] = w[i];
    }

    vector<int> bums;
    for(int i = 1; i <= n; i++){
        if(deg[i] >= 3) bums.eb(i);
    }

    auto push = [&](array<ll,3> &kys, ll x){
        if(x > kys[0]){
            kys[0] = x;
            if(kys[0] > kys[1]) swap(kys[1], kys[0]);
            if(kys[1] > kys[2]) swap(kys[1], kys[2]);
        }
        return;
    };
    for(int i = 1; i <= q; i++){

        wts[{s[i], p[s[i]]}] = k[i];
        wts[{p[s[i]], s[i]}] = k[i];

        ll mx = 0;
        for(int u : bums){
            array<ll,3> best = {0, 0, 0};
            for(int v : g[u]){
                push(best, dfs(v, u, g) + wts[{u,v}]);
            }
            mx = max(mx, best[0] + best[1] + best[2]);
        }
        ans[i] = mx;
    }
}