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

#define MAXN 200003

int d[MAXN];

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

	for (int i = n; i >= 1; i--) {
		d[i] = 0;
	}

	for (int i = n; i >= 1; i--) {
		d[p[i]] = max(d[p[i]], d[i] + w[i]);
	}

	multiset<long long> grane;
	for (int i = 2; p[i] == 1; i++) {
		grane.insert(w[i] + d[i]);
	}
	for (int i = 1; i <= q; i++) {
		w[s[i]] = k[i];
		int j = s[i];
		while (j>1) {
			d[p[j]] = max(d[p[j]], d[j] + w[j]);
			j=p[j];
		}
		grane.clear();
		for (int i = 2; p[i] == 1; i++) {
			grane.insert(w[i] + d[i]);
		}
		auto ender = grane.end();
		ans[i] = 0;
		ans[i] += *(--ender);
		ans[i] += *(--ender);
		ans[i] += *(--ender);
	}


	for (int i = 1; i <= n; i++) {
		//cout << d[i] << ' ';
	}
//	cout << endl;

	return;
}