Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint ungigned
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define vi vector<int>
#define vpi vector<pii>
#define IT iterator

#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define y1 wzpakking
#define fi first
#define se second
#define BG begin
#define ED end

#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define UPD(x, y) (((x) += (y)) >= mo ? (x) -= mo : 233)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define sqr(x) (1ll * x * x)

#define LS3 k * 2, l, mid
#define RS3 k * 2 + 1, mid + 1, r
#define LS5 k * 2, l, mid, x, y
#define RS5 k * 2 + 1, mid + 1, r, x, y
#define GET                                                                    \
  pushdown(k);                                                                 \
  int mid = (l + r) / 2
#define INF (1 << 29)
using namespace std;
const int N = 100005;
int a[N], b[N], n, tot;
int sum[N], val[N];
ll ans1, ans2, ans3;
pii q[N * 2];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  ans1 = 0, ans2 = 0, ans3 = 0;
  for (int i = 1; i <= n; i++)
    ans1 += a[i], ans2 += b[i];
  for (int i = 1; i <= n; i++)
    q[++tot] = pii(a[i], i), q[++tot] = pii(b[i], -i);
  sort(q + 1, q + tot + 1);
  int ok = 0;
  for (int i = 1; i <= n; i++) {
    ok += (sum[abs(q[i].se)] != 0);
    ans3 += q[i].fi;
    sum[abs(q[i].se)] += q[i].se;
    val[abs(q[i].se)] = q[i].fi;
  }
  if (!ok) {
    int del = 1 << 30;
    for (int i = n + 1; i <= tot; i++)
      del = min(del, q[i].fi - (val[abs(q[i].fi)] == q[n].fi ? q[n - 1].fi
                                                             : q[n].fi));
    ans3 += del;
  }
  printf("%lld\n", min(min(ans1, ans2), ans3));
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint ungigned
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define vi vector<int>
#define vpi vector<pii>
#define IT iterator

#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define y1 wzpakking
#define fi first
#define se second
#define BG begin
#define ED end

#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define UPD(x, y) (((x) += (y)) >= mo ? (x) -= mo : 233)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define sqr(x) (1ll * x * x)

#define LS3 k * 2, l, mid
#define RS3 k * 2 + 1, mid + 1, r
#define LS5 k * 2, l, mid, x, y
#define RS5 k * 2 + 1, mid + 1, r, x, y
#define GET                                                                    \
  pushdown(k);                                                                 \
  int mid = (l + r) / 2
#define INF (1 << 29)
using namespace std;
const int N = 100005;
int a[N], b[N], n, tot;
int sum[N], val[N];
ll ans1, ans2, ans3;
pii q[N * 2];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]);
  ans1 = 0, ans2 = 0, ans3 = 0;
  for (int i = 1; i <= n; i++)
    ans1 += a[i], ans2 += b[i];
  for (int i = 1; i <= n; i++)
    q[++tot] = pii(a[i], i), q[++tot] = pii(b[i], -i);
  sort(q + 1, q + tot + 1);
  int ok = 0;
  for (int i = 1; i <= n; i++) {
    ok += (sum[abs(q[i].se)] != 0);
    ans3 += q[i].fi;
    sum[abs(q[i].se)] += q[i].se;
    val[abs(q[i].se)] = q[i].fi;
  }
  if (!ok) {
    int del = 1 << 30;
    for (int i = n + 1; i <= tot; i++)
      del = min(del, q[i].fi - (val[abs(q[i].se)] == q[n].fi ? q[n - 1].fi
                                                             : q[n].fi));
    ans3 += del;
  }
  printf("%lld\n", min(min(ans1, ans2), ans3));
}