#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) {

	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();
		long long last = *(--ender);
		long long last2 = *(--ender);
		long long last3 = *(--ender);
		ans[i] = last + last2 + last3;
	}

	return;
}