#include <set>

using namespace std;

void Bumerang(int n, int q, int* p, int* w, int* s, int* k, long long* ans) {
    multiset<int> skup;
    for(int i = 1; i <= n; i++)
    	skup.insert(w[i]);
    
    for(int i = 1; i <= q; i++) {
    	skup.erase(skup.find(w[s[i]]));
    	w[s[i]] = k[i];
    	skup.insert(w[s[i]]);
    	ans[i] = 1ll * *(prev(skup.end(), 1)) + 1ll * *(prev(skup.end(), 2)) + 1ll * *(prev(skup.end(), 3));
	}
}