Problem p03357 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false);
#define DEBUG_VIS(x) cerr << "vis " << x << endl;
#define DEBUG_REACH cerr << "reach here" << endl;
#define DEBUG_SEQ(x) cerr << x << " ";
#define SET_ZERO(x) memset(x, 0, sizeof(x));
#define SET_NEGONE(x) memset(x, -1, sizeof(x));
#define SET_INF(x) memset(x, 127, sizeof(x));
#define SET_NEGINF(x) memset(x, 128, sizeof(x));
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned int uint;
const int maxn = 2e3 + 10;
template <class T> T mymin(const T a, const T b) { return a < b ? a : b; }
template <class T> T mymax(const T a, const T b) { return a > b ? a : b; }
int N, dp[maxn][maxn], costb[maxn][maxn], costw[maxn][maxn];
int ball[maxn][2], pos[maxn][maxn];

int main() {
  FAST_IO
  cin >> N;
  N *= 2;
  for (int i = 1; i <= N; i++) {
    char c;
    int id;
    cin >> c >> id;
    ball[i][0] = (c == 'B');
    ball[i][1] = id;
    pos[ball[i][0]][ball[i][1]] = i;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= N / 2; j++) {
      costb[i][j] = costb[i - 1][j];
      costw[i][j] = costw[i - 1][j];
      if (ball[i - 1][1] > j)
        ball[i - 1][0] == 0 ? costw[i][j]++ : costb[i][j]++;
    }
  }
  SET_INF(dp);
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      int u = pos[0][i + 1], v = pos[1][j + 1];
      dp[i + 1][j] =
          min(dp[i + 1][j], dp[i][j] + costw[u][i + 1] + costb[u][j]);
      dp[i][j + 1] =
          min(dp[i][j + 1], dp[i][j] + costw[v][i] + costb[v][j + 1]);
    }
  }
  cout << dp[N][N] << endl;
  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false);
#define DEBUG_VIS(x) cerr << "vis " << x << endl;
#define DEBUG_REACH cerr << "reach here" << endl;
#define DEBUG_SEQ(x) cerr << x << " ";
#define SET_ZERO(x) memset(x, 0, sizeof(x));
#define SET_NEGONE(x) memset(x, -1, sizeof(x));
#define SET_INF(x) memset(x, 127, sizeof(x));
#define SET_NEGINF(x) memset(x, 128, sizeof(x));
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned int uint;
const int maxn = 4e3 + 10;
template <class T> T mymin(const T a, const T b) { return a < b ? a : b; }
template <class T> T mymax(const T a, const T b) { return a > b ? a : b; }
int N, dp[maxn][maxn], costb[maxn][maxn], costw[maxn][maxn];
int ball[maxn][2], pos[maxn][maxn];

int main() {
  FAST_IO
  cin >> N;
  N *= 2;
  for (int i = 1; i <= N; i++) {
    char c;
    int id;
    cin >> c >> id;
    ball[i][0] = (c == 'B');
    ball[i][1] = id;
    pos[ball[i][0]][ball[i][1]] = i;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= N / 2; j++) {
      costb[i][j] = costb[i - 1][j];
      costw[i][j] = costw[i - 1][j];
      if (ball[i - 1][1] > j)
        ball[i - 1][0] == 0 ? costw[i][j]++ : costb[i][j]++;
    }
  }
  SET_INF(dp);
  dp[0][0] = 0;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      int u = pos[0][i + 1], v = pos[1][j + 1];
      dp[i + 1][j] =
          min(dp[i + 1][j], dp[i][j] + costw[u][i + 1] + costb[u][j]);
      dp[i][j + 1] =
          min(dp[i][j + 1], dp[i][j] + costw[v][i] + costb[v][j + 1]);
    }
  }
  cout << dp[N][N] << endl;
  return 0;
}