Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << x << '\n'
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(i, v.size()) cout << ' ' << v[i];                                        \
  cout << '\n'
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

const ll MOD = 1000000007ll;
// const ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-11;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll a[114514], b[114514];
  pair<ll, int> ab[114514];
  cin >> n;
  REP(i, n) {
    cin >> a[i] >> b[i];
    ab[i] = make_pair(a[i], i);
    ab[n + i] = make_pair(b[i], n + i);
  }
  sort(ab, ab + n * 2);
  ll ans = 0, sum1 = 0, sum2 = 0;
  REP(i, n) {
    ans += ab[i].first;
    sum1 += a[i];
    sum2 += b[i];
  }
  if (sum1 != ans && sum2 != ans) {
    bool ok = false;
    REP(i, n) {
      if ((a[i] < ab[n - 1].first && b[i] < ab[n - 1].first) ||
          (a[i] == ab[n - 1].first && b[i] == ab[n - 1].first &&
           ab[n - 2].first == ab[n - 1].first)) {
        ok = true;
        break;
      }
    }
    if (!ok) {
      if (abs(ab[n - 1].second - ab[n].second) == n) {
        ans = ans + min(ab[n + 1].first - ab[n - 1].first,
                        ab[n].first - ab[n - 2].first);
      } else {
        ans = ans + ab[n].first - ab[n - 1].first;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << x << '\n'
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(i, v.size()) cout << ' ' << v[i];                                        \
  cout << '\n'
#define ALL(a) (a).begin(), (a).end()

#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

const ll MOD = 1000000007ll;
// const ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-11;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  ll a[114514], b[114514];
  pair<ll, int> ab[214514];
  cin >> n;
  REP(i, n) {
    cin >> a[i] >> b[i];
    ab[i] = make_pair(a[i], i);
    ab[n + i] = make_pair(b[i], n + i);
  }
  sort(ab, ab + n * 2);
  ll ans = 0, sum1 = 0, sum2 = 0;
  REP(i, n) {
    ans += ab[i].first;
    sum1 += a[i];
    sum2 += b[i];
  }
  if (sum1 != ans && sum2 != ans) {
    bool ok = false;
    REP(i, n) {
      if ((a[i] < ab[n - 1].first && b[i] < ab[n - 1].first) ||
          (a[i] == ab[n - 1].first && b[i] == ab[n - 1].first &&
           ab[n - 2].first == ab[n - 1].first)) {
        ok = true;
        break;
      }
    }
    if (!ok) {
      if (abs(ab[n - 1].second - ab[n].second) == n) {
        ans = ans + min(ab[n + 1].first - ab[n - 1].first,
                        ab[n].first - ab[n - 2].first);
      } else {
        ans = ans + ab[n].first - ab[n - 1].first;
      }
    }
  }
  cout << ans << '\n';
  return 0;
}