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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<long double, long double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 61;
const double pi = acos(-1);
const double eps = 1e-10;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n;
vl l, r;
vpll a;

int main() {
  cin >> n;
  l = r = vl(n);
  a = vpll(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    a[i] = {l[i] + r[i], i};
  }
  sort(a.begin(), a.end());
  int N = n / 2;
  vvl dp(n + 1, vl(N + 1)), DP(n + 1, vl(N + 1));
  for (int i = 1; i <= n; i++) {
    ll A = a[i - 1].first, I = a[i - 1].second;
    for (int j = 0; j <= min(i, N); j++)
      if (i - N <= j) {
        ll res = INF;
        if (j)
          res = dp[i - 1][j - 1] + r[I] + A * (N - j);
        if (i != j)
          res = min(res, dp[i - 1][j] + l[I] + A * (N - i + j));
        dp[i][j] = res;
      }
  }
  for (int i = n - 1; i >= 0; i--) {
    ll A = a[i].first, I = a[i].second;
    for (int j = 0; j <= min(n - i, N); j++)
      if (n - i - N <= j) {
        ll res = INF;
        if (j)
          res = DP[i + 1][j - 1] + r[I] + A * (j - 1);
        if (n - i != j)
          res = min(res, DP[i + 1][j] + l[I] + A * (n - i - j - 1));
        DP[i][j] = res;
      }
  }
  if (n % 2 == 0)
    cout << dp[n][N] << endl;
  else {
    ll res = INF;
    for (int i = 0; i < n; i++)
      for (int j = max(0, N - n + i + 1); j <= min(i, N); j++) {
        res = min(res, dp[i][j] + DP[i + 1][N - j] + a[i].first * N);
      }
    cout << res << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<long double, long double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 30;
const ll INF = 1ll << 61;
const double pi = acos(-1);
const double eps = 1e-10;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n;
vl l, r;
vpll a;

int main() {
  cin >> n;
  l = r = vl(n);
  a = vpll(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
    a[i] = {l[i] + r[i], i};
  }
  sort(a.begin(), a.end());
  int N = n / 2;
  vvl dp(n + 1, vl(N + 1)), DP(n + 1, vl(N + 1));
  for (int i = 1; i <= n; i++) {
    ll A = a[i - 1].first, I = a[i - 1].second;
    for (int j = 0; j <= min(i, N); j++)
      if (i - N <= j) {
        ll res = INF;
        if (j)
          res = dp[i - 1][j - 1] + r[I] + A * (N - j);
        if (i != j)
          res = min(res, dp[i - 1][j] + l[I] + A * (N - i + j));
        dp[i][j] = res;
      }
  }
  for (int i = n - 1; i >= 0; i--) {
    ll A = a[i].first, I = a[i].second;
    for (int j = 0; j <= min(n - i, N); j++)
      if (n - i - N <= j) {
        ll res = INF;
        if (j)
          res = DP[i + 1][j - 1] + r[I] + A * (j - 1);
        if (n - i != j)
          res = min(res, DP[i + 1][j] + l[I] + A * (n - i - j - 1));
        DP[i][j] = res;
      }
  }
  assert(n % 2 == 0);
  if (n % 2 == 0)
    cout << dp[n][N] << endl;
  else {
    ll res = INF;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= i; j++) {
        res = min(res, dp[i][j] + DP[i + 1][N - j] + a[i].first * N);
      }
    cout << res << endl;
  }
}