Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- multiset <int> b;
- multiset <int> a;
- int main()
- {
- int n, k;
- ll sum = 0;
- int m;
- ios::sync_with_stdio(0);
- cin.tie(0);
- cin >> n;
- vector <int> c(n);
- cin >> k;
- char q;
- for (int i = 0; i < n; i++)
- {
- int r;
- cin >> r ;
- c[i] = r;
- int w = c[i];
- b.insert(w);
- }
- cin >> m;
- for (int i = 0 ; i < m ;i ++) {
- cin >> q;
- if ('+' == q)
- {
- int x;
- cin >> x;
- x--;
- int h = c[x] ;
- if (0 >= a.size() || *a.rbegin() < h )
- {
- int u = c[x] + 1;
- u--;
- b.insert(u);
- }
- else
- {
- int u = c[x] + 1;
- u--;
- a.insert(u);
- sum += u;
- }
- } else {
- if (q == '?') {
- while (k < a.size())
- {
- sum -= *a.rbegin();
- b.insert(*a.rbegin());
- a.erase(--a.end());
- }
- while (k > a.size() )
- {
- sum += *b.begin();
- a.insert(*b.begin());
- b.erase(b.begin());
- }
- cout << sum << '\n' ;
- } else {
- int x;
- cin >> x;
- x--;
- if (a.end() != a.find(c[x]) )
- {
- sum -= c[x];
- a.erase(a.find(c[x]));
- }
- else
- {
- b.erase(b.find(c[x]));
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement