Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <unordered_set>
// #include <unordered_map>
// #include <bits/stdc++.h>

#define MP(A, B) make_pair(A, B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define fi first
#define se second

using namespace std;

template <typename T> inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod eps = 1e-11;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 998244353;
const int MAXN = 100005;
const int INF = 0x3f3f3f3f; // 1061109567
/*--------------------------------------------------------------------*/
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
inline char getch() {
  char c = getchar();
  while (c < 'A' || c > 'Z')
    c = getchar();
  return c;
}
PR a[4005];
int f[2005][2005], s0[4005][2005], s1[4005][2005], pos[2][4005], n;
int main() {
  n = read();
  for (int i = 1; i <= n * 2; i++) {
    int opt = (getch() == 'B'), x = read();
    a[i] = MP(opt, x);
    pos[opt][x] = i;
  }
  for (int i = 1; i <= n * 2; i++)
    for (int j = 0; j <= n; j++)
      s0[i][j] = s0[i - 1][j] + (a[i - 1].fi == 0 && a[i - 1].se > j),
      s1[i][j] = s1[i - 1][j] + (a[i - 1].fi == 1 && a[i - 1].se > j);
  for (int i = 0; i <= n * 2; i++)
    for (int j = 0; j <= n; j++)
      f[i][j] = INF;
  f[0][0] = 0;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++) {
      int x = pos[0][i], y = pos[1][j];
      if (i)
        upmin(f[i][j], f[i - 1][j] + s0[x][i - 1] + s1[x][j]);
      if (j)
        upmin(f[i][j], f[i][j - 1] + s1[y][j - 1] + s0[y][i]);
    }
  printf("%d\n", f[n][n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <unordered_set>
// #include <unordered_map>
// #include <bits/stdc++.h>

#define MP(A, B) make_pair(A, B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define fi first
#define se second

using namespace std;

template <typename T> inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod eps = 1e-11;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 998244353;
const int MAXN = 100005;
const int INF = 0x3f3f3f3f; // 1061109567
/*--------------------------------------------------------------------*/
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
inline char getch() {
  char c = getchar();
  while (c < 'A' || c > 'Z')
    c = getchar();
  return c;
}
PR a[4005];
int f[2005][2005], s0[4005][2005], s1[4005][2005], pos[2][4005], n;
int main() {
  n = read();
  for (int i = 1; i <= n * 2; i++) {
    int opt = (getch() == 'B'), x = read();
    a[i] = MP(opt, x);
    pos[opt][x] = i;
  }
  for (int i = 1; i <= n * 2; i++)
    for (int j = 0; j <= n; j++)
      s0[i][j] = s0[i - 1][j] + (a[i - 1].fi == 0 && a[i - 1].se > j),
      s1[i][j] = s1[i - 1][j] + (a[i - 1].fi == 1 && a[i - 1].se > j);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      f[i][j] = INF;
  f[0][0] = 0;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++) {
      int x = pos[0][i], y = pos[1][j];
      if (i)
        upmin(f[i][j], f[i - 1][j] + s0[x][i - 1] + s1[x][j]);
      if (j)
        upmin(f[i][j], f[i][j - 1] + s1[y][j - 1] + s0[y][i]);
    }
  printf("%d\n", f[n][n]);
  return 0;
}