#include<bits/stdc++.h>#defineintlonglong#defineyescout <<"YES"<< endl;#definenocout <<"NO"<< endl;#defineIOScin.tie(0), cout.tie(0), ios::sync_with_stdio(false);#definecxk1#definedebug(s, x)if(cxk) cout <<"#debug:("<< s <<")="<< x << endl;usingnamespace std;constint N =5e5+10;int n, k, q, res;int w[N];
multiset<int> a, b;//a维护前k大,小根堆 b维护比第k个数小的,大根堆voidsolve(){
cin >> n >> k >> q;for(int i =1; i <= k; i++)a.insert(0);for(int i =1; i <= n - k; i++) b.insert(0);while(q--){int x, y;
cin >> x >> y;if(a.find(w[x])!= a.end()) a.erase(a.find(w[x])), res -= w[x];else b.erase(b.find(w[x]));
w[x]= y;if(!b.empty()&& y >=*b.rbegin()) a.insert(y), res += y;else b.insert(y);while(a.size()> k){
b.insert(*a.begin());
res -=*a.begin();
a.erase(a.begin());}while(a.size()< k){
a.insert(*b.rbegin());
res +=*b.rbegin();
b.erase(b.find(*b.rbegin()));}
cout << res << endl;}}signedmain(){
IOS
#ifndefONLINE_JUDGEfreopen("../test.in","r",stdin);freopen("../test.out","w",stdout);#endifint _ =1;while(_--)solve();return0;}