Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define db double
#define lowbit(p) (p & (-p))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define A first
#define B second
using namespace std;
void read(int &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void read(ll &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void Min(int &x, int y) {
  if (x > y)
    x = y;
}
void Max(int &x, int y) {
  if (x < y)
    x = y;
}
void Min(ll &x, ll y) {
  if (x > y)
    x = y;
}
void Max(ll &x, ll y) {
  if (x < y)
    x = y;
}
/*
给定一个黑白的,数字序列
要求交换次数最少
使得
黑色升序
白色升序

*/
#define M 4005
#define inf 100000000
int a[M], c[M], pos[2][M], n, m;
int cost0[2005][2005], cost1[2005][2005], dp[2005][2005];
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  //	freopen("1.in","r",stdin);
  read(n);
  m = 2 * n;
  int i, j;
  char s[5];
  for (i = 1; i <= m; i++) {
    scanf("%s%d", s, &a[i]);
    c[i] = (s[0] == 'B');
    pos[c[i]][a[i]] = i;
  }
  for (i = 1; i <= m; i++) {
    for (j = 0; j < n; j++) {
      cost0[i][j] = cost0[i - 1][j] + (a[i - 1] > j && c[i - 1] == 0);
      cost1[i][j] = cost1[i - 1][j] + (a[i - 1] > j && c[i - 1] == 1);
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++)
      if (i || j) {
        dp[i][j] = inf;
        if (i)
          Min(dp[i][j],
              dp[i - 1][j] + cost0[pos[0][i]][i - 1] + cost1[pos[0][i]][j]);
        if (j)
          Min(dp[i][j],
              dp[i][j - 1] + cost1[pos[1][j]][j - 1] + cost0[pos[1][j]][i]);
      }
  }
  printf("%d\n", dp[n][n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define db double
#define lowbit(p) (p & (-p))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define A first
#define B second
using namespace std;
void read(int &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void read(ll &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void Min(int &x, int y) {
  if (x > y)
    x = y;
}
void Max(int &x, int y) {
  if (x < y)
    x = y;
}
void Min(ll &x, ll y) {
  if (x > y)
    x = y;
}
void Max(ll &x, ll y) {
  if (x < y)
    x = y;
}
/*
给定一个黑白的,数字序列
要求交换次数最少
使得
黑色升序
白色升序

*/
#define M 4005
#define inf 100000000
int a[M], c[M], pos[2][M], n, m;
int cost0[M][M], cost1[M][M], dp[2005][2005];
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  //	freopen("1.in","r",stdin);
  read(n);
  m = 2 * n;
  int i, j;
  char s[5];
  for (i = 1; i <= m; i++) {
    scanf("%s%d", s, &a[i]);
    c[i] = (s[0] == 'B');
    pos[c[i]][a[i]] = i;
  }
  for (i = 1; i <= m; i++) {
    for (j = 0; j < n; j++) {
      cost0[i][j] = cost0[i - 1][j] + (a[i - 1] > j && c[i - 1] == 0);
      cost1[i][j] = cost1[i - 1][j] + (a[i - 1] > j && c[i - 1] == 1);
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++)
      if (i || j) {
        dp[i][j] = inf;
        if (i)
          Min(dp[i][j],
              dp[i - 1][j] + cost0[pos[0][i]][i - 1] + cost1[pos[0][i]][j]);
        if (j)
          Min(dp[i][j],
              dp[i][j - 1] + cost1[pos[1][j]][j - 1] + cost0[pos[1][j]][i]);
      }
  }
  printf("%d\n", dp[n][n]);
  return 0;
}