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

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