Problem p03873 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  int N;
  cin >> N;
  int _N = N;
  vector<ll> a(N), b(N);
  rep(i, N) cin >> a[i] >> b[i];
  if (N % 2 == 0) {
    N++;
    a.pb(0);
    b.pb(0);
  }
  vector<pair<ll, ll_ll>> p(N);
  rep(i, N) p[i] = make_pair(a[i] + b[i], i_i(a[i], b[i]));
  sort(p.begin(), p.end());
  rep(i, N) a[i] = p[i].second.first, b[i] = p[i].second.second;
  int n = N / 2;
  if (_N % 2 == 0 || _N <= 10) {
    ll ans = n * (a[0] + b[0]);
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, LLONG_MAX));
    dp[0][0] = ans;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) {
        ll l = a[i + j + 1], r = b[i + j + 1];
        if (i < n) {
          ll x = dp[i][j] + r + (n - i - 1) * (l + r);
          dp[i + 1][j] = min(dp[i + 1][j], x);
        }
        if (j < n) {
          ll x = dp[i][j] + l + (n - j - 1) * (l + r);
          dp[i][j + 1] = min(dp[i][j + 1], x);
        }
      }
    cout << dp[n][n] << endl;
    return 0;
  }
  ll unko = LLONG_MAX;
  rep(t, 10) {
    ll ans = n * (a[0] + b[0]);
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, LLONG_MAX));
    dp[0][0] = ans;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) {
        ll l = a[i + j + 1], r = b[i + j + 1];
        if (i < n) {
          ll x = dp[i][j] + r + (n - i - 1) * (l + r);
          dp[i + 1][j] = min(dp[i + 1][j], x);
        }
        if (j < n) {
          ll x = dp[i][j] + l + (n - j - 1) * (l + r);
          dp[i][j + 1] = min(dp[i][j + 1], x);
        }
      }
    unko = min(unko, dp[n][n]);
    vector<pair<ll, ll_ll>> _p = p;
    swap(_p[0], _p[t + 1]);
    sort(_p.begin() + 1, _p.end());
    rep(i, N) a[i] = _p[i].second.first, b[i] = _p[i].second.second;
  }
  cout << unko << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  int N;
  cin >> N;
  int _N = N;
  vector<ll> a(N), b(N);
  rep(i, N) cin >> a[i] >> b[i];
  if (N % 2 == 0) {
    N++;
    a.pb(0);
    b.pb(0);
  }
  vector<pair<ll, ll_ll>> p(N);
  rep(i, N) p[i] = make_pair(a[i] + b[i], i_i(a[i], b[i]));
  sort(p.begin(), p.end());
  rep(i, N) a[i] = p[i].second.first, b[i] = p[i].second.second;
  int n = N / 2;
  if (_N % 2 == 0 || _N <= 10) {
    ll ans = n * (a[0] + b[0]);
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, LLONG_MAX));
    dp[0][0] = ans;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) {
        ll l = a[i + j + 1], r = b[i + j + 1];
        if (i < n) {
          ll x = dp[i][j] + r + (n - i - 1) * (l + r);
          dp[i + 1][j] = min(dp[i + 1][j], x);
        }
        if (j < n) {
          ll x = dp[i][j] + l + (n - j - 1) * (l + r);
          dp[i][j + 1] = min(dp[i][j + 1], x);
        }
      }
    cout << dp[n][n] << endl;
    return 0;
  }
  return -1;
  ll unko = LLONG_MAX;
  rep(t, 10) {
    ll ans = n * (a[0] + b[0]);
    vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, LLONG_MAX));
    dp[0][0] = ans;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) {
        ll l = a[i + j + 1], r = b[i + j + 1];
        if (i < n) {
          ll x = dp[i][j] + r + (n - i - 1) * (l + r);
          dp[i + 1][j] = min(dp[i + 1][j], x);
        }
        if (j < n) {
          ll x = dp[i][j] + l + (n - j - 1) * (l + r);
          dp[i][j + 1] = min(dp[i][j + 1], x);
        }
      }
    unko = min(unko, dp[n][n]);
    swap(a[0], a[t]);
    swap(b[0], b[t]);
    sort(a.begin() + 1, a.end());
    sort(b.begin() + 1, b.end());
  }
  cout << unko << endl;
}