Problem p03233 (C++)

Buggy submission (status: Runtime Error):
/*input
3
1 5
4 2
6 3
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#ifdef leowang
#define debug(...)                                                             \
  do {                                                                         \
    fprintf(stderr, "%s - %d : (%s) = ", __PRETTY_FUNCTION__, __LINE__,        \
            #__VA_ARGS__);                                                     \
    _DO(__VA_ARGS__);                                                          \
  } while (0)
template <typename I> void _DO(I &&x) { cerr << x << endl; }
template <typename I, typename... T> void _DO(I &&x, T &&...tail) {
  cerr << x << ", ";
  _DO(tail...);
}
#else
#define debug(...)
#endif
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}
//}}}
const ll maxn = 300005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const double PI = acos(-1);
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;

    b >>= 1;
  }
  return res;
}
ll a[maxn], b[maxn];
multiset<ll> mn, mx;
int main() {
  IOS;
  int n;
  cin >> n;
  REP(i, n) cin >> a[i] >> b[i];
  REP(i, n) mx.insert(a[i]);
  REP(i, n) mx.insert(b[i]);
  ll ans = INF64;
  {
    ll cur = 0;
    REP(i, n) cur += a[i];
    ans = min(ans, cur);
  }
  {
    ll cur = 0;
    REP(i, n) cur += b[i];
    ans = min(ans, cur);
  }

  REP(i, n - 2) {
    mn.insert(*mx.begin());
    mx.erase(mx.begin());
  }
  ll cur = 0;
  for (ll i : mn)
    cur += i;

  REP(i, n) {
    if (mn.count(a[i]))
      mn.erase(mn.lower_bound(a[i])), cur -= a[i];
    else
      mx.erase(mx.lower_bound(a[i]));

    if (mn.count(b[i]))
      mn.erase(mn.lower_bound(b[i])), cur -= b[i];
    else
      mx.erase(mx.lower_bound(b[i]));

    while (SZ(mn) < n - 2) {
      mn.insert(*mx.begin());
      cur += *mx.begin();
      mx.erase(mx.begin());
    }
    while (SZ(mn) > n - 2) {
      mx.insert(*prev(mn.end()));
      cur -= *prev(mn.end());
      mn.erase(prev(mn.end()));
    }

    ans = min(ans, cur + a[i] + b[i]);
    // cout<<i<<' '<<cur+a[i]+b[i]<<'\n';
    // for(ll i:mn) cout<<i<<' ';
    // cout<<'\n';

    mx.insert(a[i]);
    mx.insert(b[i]);

    while (*prev(mn.end()) > *mx.begin()) {
      ll x = *prev(mn.end()), y = *mx.begin();
      mn.erase(prev(mn.end()));
      mx.erase(mx.begin());
      mn.insert(y);
      mx.insert(x);
      cur = cur - x + y;
    }
  }
  cout << ans << '\n';
}

Fixed submission:
/*input
3
1 5
4 2
6 3
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#ifdef leowang
#define debug(...)                                                             \
  do {                                                                         \
    fprintf(stderr, "%s - %d : (%s) = ", __PRETTY_FUNCTION__, __LINE__,        \
            #__VA_ARGS__);                                                     \
    _DO(__VA_ARGS__);                                                          \
  } while (0)
template <typename I> void _DO(I &&x) { cerr << x << endl; }
template <typename I, typename... T> void _DO(I &&x, T &&...tail) {
  cerr << x << ", ";
  _DO(tail...);
}
#else
#define debug(...)
#endif
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}
//}}}
const ll maxn = 300005;
const ll maxlg = __lg(maxn) + 2;
const ll INF64 = 8000000000000000000LL;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const double PI = acos(-1);
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;

    b >>= 1;
  }
  return res;
}
ll a[maxn], b[maxn];
multiset<ll> mn, mx;
int main() {
  IOS;
  int n;
  cin >> n;
  REP(i, n) cin >> a[i] >> b[i];

  if (n == 2) {
    cout << min(a[0], b[1]) + min(a[1], b[0]) << '\n';
    return 0;
  }

  REP(i, n) mx.insert(a[i]);
  REP(i, n) mx.insert(b[i]);
  ll ans = INF64;
  {
    ll cur = 0;
    REP(i, n) cur += a[i];
    ans = min(ans, cur);
  }
  {
    ll cur = 0;
    REP(i, n) cur += b[i];
    ans = min(ans, cur);
  }

  REP(i, n - 2) {
    mn.insert(*mx.begin());
    mx.erase(mx.begin());
  }
  ll cur = 0;
  for (ll i : mn)
    cur += i;

  REP(i, n) {
    if (mn.count(a[i]))
      mn.erase(mn.lower_bound(a[i])), cur -= a[i];
    else
      mx.erase(mx.lower_bound(a[i]));

    if (mn.count(b[i]))
      mn.erase(mn.lower_bound(b[i])), cur -= b[i];
    else
      mx.erase(mx.lower_bound(b[i]));

    while (SZ(mn) < n - 2) {
      mn.insert(*mx.begin());
      cur += *mx.begin();
      mx.erase(mx.begin());
    }
    while (SZ(mn) > n - 2) {
      mx.insert(*prev(mn.end()));
      cur -= *prev(mn.end());
      mn.erase(prev(mn.end()));
    }

    ans = min(ans, cur + a[i] + b[i]);
    // cout<<i<<' '<<cur+a[i]+b[i]<<'\n';
    // for(ll i:mn) cout<<i<<' ';
    // cout<<'\n';

    mx.insert(a[i]);
    mx.insert(b[i]);

    while (*prev(mn.end()) > *mx.begin()) {
      ll x = *prev(mn.end()), y = *mx.begin();
      mn.erase(prev(mn.end()));
      mx.erase(mx.begin());
      mn.insert(y);
      mx.insert(x);
      cur = cur - x + y;
    }
  }
  cout << ans << '\n';
}