Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  multiset<ll> st1, st2;
  ll x = 0, y = 0;
  ll sum = 0;
  int t = 0;
  rep(i, q) {
    int k;
    cin >> k;
    if (k == 1) {
      ll a, b;
      cin >> a >> b;
      sum += b;
      st2.insert(a);
      y += a;
      if (t % 2 == 0) {
        auto itr = st2.end();
        itr--;
        x += *itr;
        y -= *itr;
        st1.insert(*itr);
        st2.erase(itr);
      } else {
        auto itr1 = st1.begin();
        auto itr2 = st2.end();
        itr2--;
        if (*itr1 < *itr2) {
          x += *itr2;
          y -= *itr2;
          x -= *itr1;
          y += *itr1;
          st1.insert(*itr1);
          st2.erase(itr1);
          st2.insert(*itr2);
          st1.erase(itr2);
        }
      }
      t++;
    } else {
      ll num;
      if (st1.size() == st2.size()) {
        num = *(--st2.end());
      } else {
        num = *st1.begin();
      }
      show(num);
      ll a = (ll)st1.size();
      ll b = (ll)st2.size();
      cout << num << " " << abs(x - a * num) + abs(y - b * num) + sum << "\n";
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  multiset<ll> st1, st2;
  ll x = 0, y = 0;
  ll sum = 0;
  int t = 0;
  rep(i, q) {
    int k;
    cin >> k;
    if (k == 1) {
      ll a, b;
      cin >> a >> b;
      sum += b;
      st2.insert(a);
      y += a;
      if (t % 2 == 0) {
        auto itr = st2.end();
        itr--;
        x += *itr;
        y -= *itr;
        st1.insert(*itr);
        st2.erase(itr);
      } else {
        auto itr1 = st1.begin();
        auto itr2 = st2.end();
        itr2--;
        if (*itr1 < *itr2) {
          x += *itr2;
          y -= *itr2;
          x -= *itr1;
          y += *itr1;
          st1.insert(*itr2);
          st2.erase(itr2);
          st2.insert(*itr1);
          st1.erase(itr1);
        }
      }
      t++;
    } else {
      ll num;
      if (st1.size() == st2.size()) {
        num = *(--st2.end());
      } else {
        num = *st1.begin();
      }
      show(num);
      ll a = (ll)st1.size();
      ll b = (ll)st2.size();
      cout << num << " " << abs(x - a * num) + abs(y - b * num) + sum << "\n";
    }
  }
}