Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (register ll i = (a); i <= (b); ++i)
#define per(i, a, b) for (register ll i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int maxn = 5e3 + 5;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const ll mod = 1e9 + 7;
const double eps = 1e-7;
template <class T = int> T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}

ll n, ans;

ll a[maxn], b[maxn], suma, sumb;

pair<ll, pair<ll, ll>> c[maxn];

ll pre[maxn];

ll rnka[maxn], rnkb[maxn];

signed main() {
  scanf("%lld", &n);
  rep(i, 1, n) scanf("%lld %lld", &a[i], &b[i]), suma += a[i], sumb += b[i],
      c[i] = mp(a[i], mp(i, 0)), c[n + i] = mp(b[i], mp(i, 1));
  ans = min(suma, sumb);
  sort(c + 1, c + 2 * n + 1);
  rep(i, 1, 2 * n) pre[i] = pre[i - 1] + c[i].fi;
  rep(i, 1, n) {
    rnka[i] = lb(c + 1, c + n + n + 1, mp(a[i], mp(i, 0ll))) - c;
    rnkb[i] = lb(c + 1, c + n + n + 1, mp(b[i], mp(i, 1ll))) - c;
  }
  rep(i, 1, n) {
    ll x = rnka[i], y = rnkb[i];
    if (x > y)
      swap(x, y);
    ll sum = pre[n - 2] + a[i] + b[i];
    if (x <= n - 2) {
      sum += c[n - 1].fi;
      sum -= c[x].fi;
      if (y <= n - 1)
        sum += c[n].fi, sum -= c[y].fi;
    }
    ans = min(ans, sum);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (register ll i = (a); i <= (b); ++i)
#define per(i, a, b) for (register ll i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int maxn = 2e6 + 5;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const ll mod = 1e9 + 7;
const double eps = 1e-7;
template <class T = int> T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}

ll n, ans;

ll a[maxn], b[maxn], suma, sumb;

pair<ll, pair<ll, ll>> c[maxn];

ll pre[maxn];

ll rnka[maxn], rnkb[maxn];

signed main() {
  scanf("%lld", &n);
  rep(i, 1, n) scanf("%lld %lld", &a[i], &b[i]), suma += a[i], sumb += b[i],
      c[i] = mp(a[i], mp(i, 0)), c[n + i] = mp(b[i], mp(i, 1));
  ans = min(suma, sumb);
  sort(c + 1, c + 2 * n + 1);
  rep(i, 1, 2 * n) pre[i] = pre[i - 1] + c[i].fi;
  rep(i, 1, n) {
    rnka[i] = lb(c + 1, c + n + n + 1, mp(a[i], mp(i, 0ll))) - c;
    rnkb[i] = lb(c + 1, c + n + n + 1, mp(b[i], mp(i, 1ll))) - c;
  }
  rep(i, 1, n) {
    ll x = rnka[i], y = rnkb[i];
    if (x > y)
      swap(x, y);
    ll sum = pre[n - 2] + a[i] + b[i];
    if (x <= n - 2) {
      sum += c[n - 1].fi;
      sum -= c[x].fi;
      if (y <= n - 1)
        sum += c[n].fi, sum -= c[y].fi;
    }
    ans = min(ans, sum);
  }
  printf("%lld\n", ans);
  return 0;
}