Problem p03233 (C++)

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

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back
#define DEBUG(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  T sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

const int Maxn = 1e5 + 100;

int N, A[Maxn], B[Maxn];
vector<pii> C;
vector<int> Pos[Maxn];
LL Sum[Maxn];

inline void Solve() {
  LL sum = 0, ans = LLONG_MAX;

  for (int i = 1; i <= N; ++i)
    sum += A[i];
  Chkmin(ans, sum);
  sum = 0;
  for (int i = 1; i <= N; ++i)
    sum += B[i];
  Chkmin(ans, sum);

  C.pb(mp(0, 0));
  for (int i = 1; i <= N; ++i)
    C.pb(mp(A[i], i)), C.pb(mp(B[i], i));
  sort(C.begin(), C.end());
  for (int i = 1; i <= 2 * N; ++i) {
    Pos[C[i].y].pb(i);
    Sum[i] = Sum[i - 1] + C[i].x;
  }

  for (int i = 1; i <= N; ++i) {
    sort(Pos[i].begin(), Pos[i].end());
    if (Pos[i][1] <= N) {
      Chkmin(ans, Sum[N]);
      cout << ans << endl;
      return;
    }
  }

  for (int i = 1; i <= N; ++i) {
    if (Pos[i][1] == N + 1)
      Chkmin(ans, Sum[N + 2] - A[i] - B[i]);
    else
      Chkmin(ans, Sum[N + 1] - C[Pos[i][0]].x);
  }

  cout << ans << endl;
}

inline void Input() {
  N = read<int>();
  for (int i = 1; i <= N; ++i)
    A[i] = read<int>(), B[i] = read<int>();
}

int main() {

#ifdef hk_cnyali
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif

  Input();
  Solve();

  return 0;
}


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

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back
#define DEBUG(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  T sum = 0, fl = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

const int Maxn = 2e5 + 100;

int N, A[Maxn], B[Maxn];
vector<pii> C;
vector<int> Pos[Maxn];
LL Sum[Maxn];

inline void Solve() {
  LL sum = 0, ans = LLONG_MAX;

  for (int i = 1; i <= N; ++i)
    sum += A[i];
  Chkmin(ans, sum);
  sum = 0;
  for (int i = 1; i <= N; ++i)
    sum += B[i];
  Chkmin(ans, sum);

  C.pb(mp(0, 0));
  for (int i = 1; i <= N; ++i)
    C.pb(mp(A[i], i)), C.pb(mp(B[i], i));
  sort(C.begin(), C.end());
  for (int i = 1; i <= 2 * N; ++i) {
    Pos[C[i].y].pb(i);
    Sum[i] = Sum[i - 1] + C[i].x;
  }

  for (int i = 1; i <= N; ++i) {
    sort(Pos[i].begin(), Pos[i].end());
    if (Pos[i][1] <= N) {
      Chkmin(ans, Sum[N]);
      cout << ans << endl;
      return;
    }
  }

  for (int i = 1; i <= N; ++i) {
    if (Pos[i][1] == N + 1)
      Chkmin(ans, Sum[N + 2] - A[i] - B[i]);
    else
      Chkmin(ans, Sum[N + 1] - C[Pos[i][0]].x);
  }

  cout << ans << endl;
}

inline void Input() {
  N = read<int>();
  for (int i = 1; i <= N; ++i)
    A[i] = read<int>(), B[i] = read<int>();
}

int main() {

#ifdef hk_cnyali
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif

  Input();
  Solve();

  return 0;
}