#include <bits/stdc++.h>
#define ll long long
using namespace std;
int N, Q;
ll W[200000], DIST[200001];
int t, IN[200001];
pair <ll, ll> MX[200001];
vector <int> VG[200001];
void DFS(int u)
{
  IN[u] = ++t;
  MX[u] = {0, 0};
  for(int v : VG[u])
  {
    DIST[v] = DIST[u] + W[v];
    DFS(v);
    if((MX[v].first + W[v]) > MX[u].first)
    {
      MX[u].second = MX[u].first;
      MX[u].first = (MX[v].first + W[v]);
    }
    else if((MX[v].first + W[v]) > MX[u].second)
    {
      MX[u].second = (MX[v].first + W[v]);
    }
  }
  return;
}
ll Brute()
{
  t = 0;
  for(int i = 1; i <= N; i++) MX[i] = {0, 0};
  DFS(1);
  ll ret = 0, temp;
  for(int i = 2; i <= N; i++)
  {
    if(VG[i].size() >= 2)
    {
      temp = MX[i].first + MX[i].second + DIST[i];
      int l = 0, r = VG[1].size(), s, index = -1;
      while(l <= r)
      {
        s = (l + r) >> 1;
        if(IN[VG[1][s]] <= IN[i]) {l = s + 1; index = s;}
        else r = s - 1;
      }
      assert(s != -1);
      if((MX[VG[1][s]].first + W[VG[1][s]]) == MX[1].first) temp += MX[1].second;
      else temp += MX[1].first;
      ret = max(temp, ret);
    }
  }
  if(VG[1].size() >= 3)
  {
    vector <ll> V;
    for(int v : VG[1]) V.push_back(MX[v].first + W[v]);
    sort(V.begin(), V.end());
    temp = V[0] + V[1] + V[2];
    ret = max(temp, ret);
  }
  return ret;
}
void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans)
{
  N = n;
  Q = q;
  for(int i = 2; i <= N; i++) VG[p[i]].push_back(i);
  for(int i = 2; i <= N; i++) W[i] = w[i];
  if((Q == 1) || (max(N, Q) <= 5000))
  {
    for(int i = 1; i <= Q; i++)
    {
      W[s[i]] = k[i];
      ans[i] = Brute();
    }
  }
  return;
}