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

void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans) {
	bool bod8 = true;
	for (int i = 2; i <= n; i++) {
		if (p[i] != 1) {
			bod8 = true;
		}
	}
	if (bod8) {
		multiset<int> tezine;
		for (int i = 1; i <= n; i++) {
			tezine.insert(w[i]);
		}
		cout << endl;
		for (int i = 1; i <= q; i++) {
			tezine.erase(w[s[i]]);
			tezine.insert(k[i]);
			w[s[i]] = k[i];

			auto ender = tezine.end();
			int last = *(--ender);
			int last2 = *(--ender);
			int last3 = *(--ender);
			ans[i] = last + last2 + last3;
		
		}
	}
	return;
}