#include <bits/stdc++.h>
#define ll long long
using namespace std;
int N, Q;
ll W[200001];
pair <ll, ll> MX[200001];
ll MX2[200001];
vector <pair <int, int>> VG[200001];
void DFS(int u, int p = 0)
{
  MX[u] = {0, 0};
  for(pair <int, int> sused : VG[u])
  {
    int v = sused.second;
    ll w = W[sused.first];
    if(v == p) continue;
    DFS(v, u);
    if((MX[v].first + w) >= MX[u].first)
    {
      MX[u].second = MX[u].first;
      MX[u].first = (MX[v].first + w);
    }
    else if((MX[v].first + w) >= MX[u].second)
    {
      MX[u].second = (MX[v].first + w);
    }
  }
  return;
}
void DFS2(int u, int p = 0)
{
  for(pair <int, int> sused : VG[u])
  {
    int v = sused.second;
    ll w = W[sused.first];
    if(v == p) continue;
    MX2[v] = MX2[u] + w;
    if(MX[u].first == (MX[v].first + w)) MX2[v] = max(MX[u].second + w, MX2[v]);
    else MX2[v] = max(MX[u].first + w, MX2[v]);
    DFS2(v, u);
  }
  return;
}
ll DIST[200001];
ll DFS3(int u, int p)
{
  ll ret = DIST[u];
  for(pair <int, int> sused : VG[u])
  {
    if(sused.second == p) continue;
    DIST[sused.second] = DIST[u] + W[sused.first];
    ret = max(DFS3(sused.second, u), ret);
  }
  return ret;
}
ll BasBrute()
{
  ll ret = 0, temp;
  for(int i = 1; i <= N; i++)
  {
    if(VG[i].size() < 3) continue;
    vector <ll> V;
    for(pair <int, int> sused : VG[i])
    {
      DIST[sused.second] = 0;
      V.push_back(DFS3(sused.second, i) + W[sused.first]);
    }
    sort(V.begin(), V.end());
    temp = V[0] + V[1] + V[2];
    ret = max(temp, ret);
  }
  return ret;
}
ll Brute()
{
  for(int i = 1; i <= N; i++) MX[i] = {0, 0};
  DFS(1);
  DFS2(1);
  ll ret = 0, temp;
  for(int i = 2; i <= N; i++)
  {
    if(VG[i].size() >= 2)
    {
      temp = MX[i].first + MX[i].second + MX2[i];
      ret = max(temp, ret);
    }
  }
  if(VG[1].size() >= 3)
  {
    vector <ll> V;
    for(pair <int, int> sused : VG[1]) V.push_back(MX[sused.second].first + W[sused.first]);
    sort(V.begin(), V.end());
    temp = V[0] + V[1] + V[2];
    ret = max(temp, ret);
  }
  return ret;
}
void Star()
{
  return;
}
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, i});
    VG[i].push_back({i, p[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];
      if((N <= 200) && (Q != 1))
      {
        assert(BasBrute() <= Brute());
      }
      ans[i] = Brute();
    }
  }
  return;
}