Problem p03357 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3
#define mod 1000000007

int n;
int widx[2000], bidx[2000];
int wcost[2010][2010], bcost[2010][2010];
int dp[4010][2010];

int main() {
  cin >> n;
  n *= 2;
  rep(i, n) {
    char c;
    int a;
    cin >> c >> a;
    a--;
    if (c == 'W')
      widx[a] = i;
    else
      bidx[a] = i;
  }
  rep(i, n / 2) {
    repl(j, i + 1, n / 2) {
      if (widx[j] < widx[i])
        wcost[i][0]++;
    }
    rep(j, n / 2) {
      if (bidx[j] < widx[i]) {
        wcost[i][0]++;
        wcost[i][j + 1]--;
      }
    }
    rep(j, n / 2) wcost[i][j + 1] += wcost[i][j];
  }
  rep(i, n / 2) {
    repl(j, i + 1, n / 2) {
      if (bidx[j] < bidx[i])
        bcost[i][0]++;
    }
    rep(j, n / 2) {
      if (widx[j] < bidx[i]) {
        bcost[i][0]++;
        bcost[i][j + 1]--;
      }
    }
    rep(j, n / 2) bcost[i][j + 1] += bcost[i][j];
  }
  rep(i, 4010) rep(j, 2010) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, n / 2 + 1) {
      if (dp[i][j] == INF)
        continue;
      int wnxt = i - j;
      int bnxt = j;
      minch(dp[i + 1][j], dp[i][j] + wcost[wnxt][bnxt]);
      if (j < n / 2) {
        minch(dp[i + 1][j + 1], dp[i][j] + bcost[bnxt][wnxt]);
      }
    }
  }
  cout << dp[n][n / 2] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3
#define mod 1000000007

int n;
int widx[2000], bidx[2000];
int wcost[2010][2010], bcost[2010][2010];
int dp[4010][2010];

int main() {
  cin >> n;
  n *= 2;
  rep(i, n) {
    char c;
    int a;
    cin >> c >> a;
    a--;
    if (c == 'W')
      widx[a] = i;
    else
      bidx[a] = i;
  }
  rep(i, n / 2) {
    repl(j, i + 1, n / 2) {
      if (widx[j] < widx[i])
        wcost[i][0]++;
    }
    rep(j, n / 2) {
      if (bidx[j] < widx[i]) {
        wcost[i][0]++;
        wcost[i][j + 1]--;
      }
    }
    rep(j, n / 2) wcost[i][j + 1] += wcost[i][j];
  }
  rep(i, n / 2) {
    repl(j, i + 1, n / 2) {
      if (bidx[j] < bidx[i])
        bcost[i][0]++;
    }
    rep(j, n / 2) {
      if (widx[j] < bidx[i]) {
        bcost[i][0]++;
        bcost[i][j + 1]--;
      }
    }
    rep(j, n / 2) bcost[i][j + 1] += bcost[i][j];
  }
  rep(i, 4010) rep(j, 2010) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, n / 2 + 1) {
      if (dp[i][j] == INF)
        continue;
      int wnxt = i - j;
      int bnxt = j;
      if (wnxt > n / 2 || bnxt > n / 2)
        continue;
      minch(dp[i + 1][j], dp[i][j] + wcost[wnxt][bnxt]);
      if (j < n / 2) {
        minch(dp[i + 1][j + 1], dp[i][j] + bcost[bnxt][wnxt]);
      }
    }
  }
  cout << dp[n][n / 2] << endl;
  return 0;
}