Problem p03233 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read() {
  LL f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }
template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
const int MAXN = 100005;
int n, a[MAXN], b[MAXN];
int L[MAXN], R[MAXN];
vector<pii> vec;
LL pres[MAXN];
int main() {
#ifdef Rose
  double BeginJudgeTime = clock();
#endif
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    vec.emplace_back(mp(a[i], i));
    vec.emplace_back(mp(b[i], i));
  }
  sort(vec.begin(), vec.end());
  LL ans = 1e18, sum = 0;
  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);
  sum = 0;
  int t = 0;
  for (auto p : vec) {
    if (L[p.second])
      R[p.second] = ++t;
    else
      L[p.second] = ++t;
    pres[t] = pres[t - 1] + p.first;
  }
  for (int i = 1; i <= n; i++) {
    if (L[i] > n - 2)
      chkmin(ans, a[i] + b[i] + pres[n - 2]);
    else if (R[i] > n - 1)
      chkmin(ans, pres[n - 1] + pres[R[i]] - pres[R[i] - 1]);
    else
      chkmin(ans, pres[n]);
  }
  pr2(ans);
#ifdef Rose
  double EndJudgeTime = clock();
  cerr << "JudgeTime is"
       << " ";
  cerr << (EndJudgeTime - BeginJudgeTime) / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read() {
  LL f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
template <typename T> inline void chkmin(T &x, T y) { x = x < y ? x : y; }
template <typename T> inline void chkmax(T &x, T y) { x = x > y ? x : y; }
const int MAXN = 200005;
int n, a[MAXN], b[MAXN];
int L[MAXN], R[MAXN];
vector<pii> vec;
LL pres[MAXN];
int main() {
#ifdef Rose
  double BeginJudgeTime = clock();
#endif
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read(), b[i] = read();
    vec.emplace_back(mp(a[i], i));
    vec.emplace_back(mp(b[i], i));
  }
  sort(vec.begin(), vec.end());
  LL ans = 1e18, sum = 0;
  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);
  sum = 0;
  int t = 0;
  for (auto p : vec) {
    if (L[p.second])
      R[p.second] = ++t;
    else
      L[p.second] = ++t;
    pres[t] = pres[t - 1] + p.first;
  }
  for (int i = 1; i <= n; i++) {
    if (L[i] > n - 2)
      chkmin(ans, a[i] + b[i] + pres[n - 2]);
    else if (R[i] > n - 1)
      chkmin(ans, pres[n - 1] + pres[R[i]] - pres[R[i] - 1]);
    else
      chkmin(ans, pres[n]);
  }
  pr2(ans);
#ifdef Rose
  double EndJudgeTime = clock();
  cerr << "JudgeTime is"
       << " ";
  cerr << (EndJudgeTime - BeginJudgeTime) / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}