Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(                                                          \
    "Ofast,unroll-loops,no-stack-protector,unsafe-math-optimizations")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
#include <assert.h>
#include <emmintrin.h>
#include <immintrin.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
const int MOD = 1000000007;
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF = 1 << 30;
template <typename T> inline bool chkmax(T &aa, T bb) {
  return aa < bb ? aa = bb, true : false;
}
template <typename T> inline bool chkmin(T &aa, T bb) {
  return aa > bb ? aa = bb, true : false;
}
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
#ifdef ONLINE_JUDGE
#define assert(...) /* nothing */
#endif
#define DEBUG_CAT
#ifdef DEBUG_CAT
#define dbg(...) printf(__VA_ARGS__)
#else
#define dbg(...) /****nothing****/
#endif
// int rint();char rch();long long rlong();
const int mn = 2002;
const int W = 0, B = 1;
int pre[2][mn][mn];
int f[mn][mn];
int id2pos[2][mn];
int n;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d\n", &n);
  for (int pos = 0; pos < 2 * n; pos++) {
    char c;
    int id;
    scanf("%c %d\n", &c, &id);
    --id;
    // printf("%c %d\n",c,id);
    int t = (c == 'B') ? B : W;
    id2pos[t][id] = pos;
    for (int x = 0; x <= pos; x++) {
      pre[t][x][id] = 1;
    }
  }
  for (int t = 0; t < 2; t++)
    for (int x = 0; x <= 2 * n; x++) {
      for (int id = 1; id <= n; id++) {
        pre[t][x][id] += pre[t][x][id - 1];
      }
    }

  for (int w = 0; w <= n; w++)
    for (int b = 0; b <= n; b++)
      f[w][b] = INF;
  f[0][0] = 0;
  for (int s = 0; s < 2 * n; s++) {
    for (int w = 0; w <= min(s, n); w++) {
      int b = s - w;
      if (0 <= b && b <= n) {
        int now = f[w][b];
        if (now == INF)
          continue;
        // printf("w:%d b:%d now:%d\n",w,b,now);
        if (b < n) {
          int pos = id2pos[B][b];
          int cand = now + pre[B][pos + 1][b - 1] + pre[W][pos + 1][w - 1];
          // int cand=now+tree[B]->GTE(0,b-1,pos+1)+tree[W]->GTE(0,w-1,pos+1);
          chkmin(f[w][b + 1], cand);
        }
        if (w < n) {
          int pos = id2pos[W][w];
          int cand = now + pre[B][pos + 1][b - 1] + pre[W][pos + 1][w - 1];
          // int cand=now+tree[B]->GTE(0,b-1,pos+1)+tree[W]->GTE(0,w-1,pos+1);
          chkmin(f[w + 1][b], cand);
        }
      }
    }
  }
  printf("%d\n", f[n][n]);
}


Fixed submission:
#pragma GCC optimize(                                                          \
    "Ofast,unroll-loops,no-stack-protector,unsafe-math-optimizations")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
#include <assert.h>
#include <emmintrin.h>
#include <immintrin.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
const int MOD = 1000000007;
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF = 1 << 30;
template <typename T> inline bool chkmax(T &aa, T bb) {
  return aa < bb ? aa = bb, true : false;
}
template <typename T> inline bool chkmin(T &aa, T bb) {
  return aa > bb ? aa = bb, true : false;
}
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
#ifdef ONLINE_JUDGE
#define assert(...) /* nothing */
#endif
#define DEBUG_CAT
#ifdef DEBUG_CAT
#define dbg(...) printf(__VA_ARGS__)
#else
#define dbg(...) /****nothing****/
#endif
// int rint();char rch();long long rlong();
const int mn = 2002;
const int W = 0, B = 1;
int pre[2][2 * mn][mn];
int f[mn][mn];
int id2pos[2][mn];
int n;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  scanf("%d\n", &n);
  for (int pos = 0; pos < 2 * n; pos++) {
    char c;
    int id;
    scanf("%c %d\n", &c, &id);
    --id;
    // printf("%c %d\n",c,id);
    int t = (c == 'B') ? B : W;
    id2pos[t][id] = pos;
    for (int x = 0; x <= pos; x++) {
      pre[t][x][id] = 1;
    }
  }
  for (int t = 0; t < 2; t++)
    for (int x = 0; x <= 2 * n; x++) {
      for (int id = 1; id <= n; id++) {
        pre[t][x][id] += pre[t][x][id - 1];
      }
    }

  for (int w = 0; w <= n; w++)
    for (int b = 0; b <= n; b++)
      f[w][b] = INF;
  f[0][0] = 0;
  for (int s = 0; s < 2 * n; s++) {
    for (int w = 0; w <= min(s, n); w++) {
      int b = s - w;
      if (0 <= b && b <= n) {
        int now = f[w][b];
        if (now == INF)
          continue;
        // printf("w:%d b:%d now:%d\n",w,b,now);
        if (b < n) {
          int pos = id2pos[B][b];
          int cand = now + pre[B][pos + 1][b - 1] + pre[W][pos + 1][w - 1];
          // int cand=now+tree[B]->GTE(0,b-1,pos+1)+tree[W]->GTE(0,w-1,pos+1);
          chkmin(f[w][b + 1], cand);
        }
        if (w < n) {
          int pos = id2pos[W][w];
          int cand = now + pre[B][pos + 1][b - 1] + pre[W][pos + 1][w - 1];
          // int cand=now+tree[B]->GTE(0,b-1,pos+1)+tree[W]->GTE(0,w-1,pos+1);
          chkmin(f[w + 1][b], cand);
        }
      }
    }
  }
  printf("%d\n", f[n][n]);
}