#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) {
	int bod8 = true, bod82 = -1;
	for (int i = 1; i <= n; i++) {
		if (p[i] == 0) {
			bod82 = i;
			continue;
		}
		if (p[i] != 1) {
			bod8 = false;
		}
	}
	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;
		}
	} else {
		exit(1);
	}
	return;
}