Problem p01909 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 0, 1, 0};
const int vx[] = {0, -1, 0, 1};
const string vv = "DLUR";

const int INF = 1 << 30;

int dp1[2002][2002] = {{}}, dp2[2002][2002] = {{}};
int dp3[2001][2002] = {{}}, dp4[2001][2002] = {{}};

int main() {
  string S;
  int K;

  cin >> S;
  cin >> K;

  string A, B;
  for (int i = 0; i < S.size(); i++) {
    if (vy[vv.find(S[i])] != 0)
      A += S[i];
    else
      B += S[i];
  }

  int latte[2002] = {}, malta[2002] = {};
  for (int i = 0; i < A.size(); i++) {
    int mv = A[i] == 'U' ? 1 : -1;
    latte[i] = mv;
  }
  for (int i = 0; i < B.size(); i++) {
    int mv = B[i] == 'L' ? 1 : -1;
    malta[i] = mv;
  }

  if (!A.empty()) {
    fill_n(*dp1, 2002 * 2002, -INF);
    fill_n(*dp2, 2002 * 2002, INF);
    dp1[0][0] = latte[0];
    dp1[0][1] = -latte[0];
    dp2[0][0] = latte[0];
    dp2[0][1] = -latte[0];
    for (int i = 1; i < A.size(); i++) {
      for (int j = 2000; j >= 0; j--) {
        dp1[i][j + 1] = max(
            dp1[i][j + 1], dp1[i - 1][j] + (j % 2 == 0 ? -latte[i] : latte[i]));
        dp1[i][j] =
            max(dp1[i][j], dp1[i - 1][j] + (j % 2 == 1 ? -latte[i] : latte[i]));
        dp2[i][j + 1] = min(
            dp2[i][j + 1], dp2[i - 1][j] + (j % 2 == 0 ? -latte[i] : latte[i]));
        dp2[i][j] =
            min(dp2[i][j], dp2[i - 1][j] + (j % 2 == 1 ? -latte[i] : latte[i]));
      }
    }

    for (int i = 1; i <= 2000; i++) {
      dp1[A.size() - 1][i] =
          max(dp1[A.size() - 1][i], dp1[A.size() - 1][i - 1]);
      dp2[A.size() - 1][i] =
          min(dp2[A.size() - 1][i], dp2[A.size() - 1][i - 1]);
    }
  }

  if (!B.empty()) {
    fill_n(*dp3, 2001 * 2002, -INF);
    fill_n(*dp4, 2001 * 2002, INF);
    dp3[0][0] = malta[0];
    dp3[0][1] = -malta[0];
    dp4[0][0] = malta[0];
    dp4[0][1] = -malta[0];
    for (int i = 1; i < B.size(); i++) {
      for (int j = 2000; j >= 0; j--) {
        dp3[i][j + 1] = max(
            dp3[i][j + 1], dp3[i - 1][j] + (j % 2 == 0 ? -malta[i] : malta[i]));
        dp3[i][j] =
            max(dp3[i][j], dp3[i - 1][j] + (j % 2 == 1 ? -malta[i] : malta[i]));
        dp4[i][j + 1] = min(
            dp4[i][j + 1], dp4[i - 1][j] + (j % 2 == 0 ? -malta[i] : malta[i]));
        dp4[i][j] =
            min(dp4[i][j], dp4[i - 1][j] + (j % 2 == 1 ? -malta[i] : malta[i]));
      }
    }
    for (int i = 1; i <= 2000; i++) {
      dp3[B.size() - 1][i] =
          max(dp3[B.size() - 1][i], dp3[B.size() - 1][i - 1]);
      dp4[B.size() - 1][i] =
          min(dp4[B.size() - 1][i], dp4[B.size() - 1][i - 1]);
    }
  }

  int ret = 0;

  for (int i = 0; i <= K; i++) {
    int aa = dp1[max(0, (int)A.size() - 1)][i];
    int bb = dp2[max(0, (int)A.size() - 1)][i];
    int cc = dp3[max(0, (int)B.size() - 1)][K - i];
    int dd = dp4[max(0, (int)B.size() - 1)][K - i];

    ret = max(ret, max(aa, -bb) + max(cc, -dd));
  }

  cout << ret << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 0, 1, 0};
const int vx[] = {0, -1, 0, 1};
const string vv = "DLUR";

const int INF = 1 << 30;

int main() {
  string S;
  int K;

  cin >> S;
  cin >> K;

  string A, B;
  for (int i = 0; i < S.size(); i++) {
    if (vy[vv.find(S[i])] != 0)
      A += S[i];
    else
      B += S[i];
  }

  int latte[2002] = {}, malta[2002] = {};
  for (int i = 0; i < A.size(); i++) {
    int mv = A[i] == 'U' ? 1 : -1;
    latte[i] = mv;
  }
  for (int i = 0; i < B.size(); i++) {
    int mv = B[i] == 'L' ? 1 : -1;
    malta[i] = mv;
  }

  int dp1[2002][2002] = {{}}, dp2[2002][2002] = {{}};

  if (!A.empty()) {
    fill_n(*dp1, 2002 * 2002, -INF);
    fill_n(*dp2, 2002 * 2002, INF);
    dp1[0][0] = latte[0];
    dp1[0][1] = -latte[0];
    dp2[0][0] = latte[0];
    dp2[0][1] = -latte[0];
    for (int i = 1; i < A.size(); i++) {
      for (int j = 2000; j >= 0; j--) {
        dp1[i][j + 1] = max(
            dp1[i][j + 1], dp1[i - 1][j] + (j % 2 == 0 ? -latte[i] : latte[i]));
        dp1[i][j] =
            max(dp1[i][j], dp1[i - 1][j] + (j % 2 == 1 ? -latte[i] : latte[i]));
        dp2[i][j + 1] = min(
            dp2[i][j + 1], dp2[i - 1][j] + (j % 2 == 0 ? -latte[i] : latte[i]));
        dp2[i][j] =
            min(dp2[i][j], dp2[i - 1][j] + (j % 2 == 1 ? -latte[i] : latte[i]));
      }
    }

    for (int i = 1; i <= 2000; i++) {
      dp1[A.size() - 1][i] =
          max(dp1[A.size() - 1][i], dp1[A.size() - 1][i - 1]);
      dp2[A.size() - 1][i] =
          min(dp2[A.size() - 1][i], dp2[A.size() - 1][i - 1]);
    }
  }

  int dp3[2001][2002] = {{}}, dp4[2001][2002] = {{}};
  if (!B.empty()) {
    fill_n(*dp3, 2001 * 2002, -INF);
    fill_n(*dp4, 2001 * 2002, INF);
    dp3[0][0] = malta[0];
    dp3[0][1] = -malta[0];
    dp4[0][0] = malta[0];
    dp4[0][1] = -malta[0];
    for (int i = 1; i < B.size(); i++) {
      for (int j = 2000; j >= 0; j--) {
        dp3[i][j + 1] = max(
            dp3[i][j + 1], dp3[i - 1][j] + (j % 2 == 0 ? -malta[i] : malta[i]));
        dp3[i][j] =
            max(dp3[i][j], dp3[i - 1][j] + (j % 2 == 1 ? -malta[i] : malta[i]));
        dp4[i][j + 1] = min(
            dp4[i][j + 1], dp4[i - 1][j] + (j % 2 == 0 ? -malta[i] : malta[i]));
        dp4[i][j] =
            min(dp4[i][j], dp4[i - 1][j] + (j % 2 == 1 ? -malta[i] : malta[i]));
      }
    }
    for (int i = 1; i <= 2000; i++) {
      dp3[B.size() - 1][i] =
          max(dp3[B.size() - 1][i], dp3[B.size() - 1][i - 1]);
      dp4[B.size() - 1][i] =
          min(dp4[B.size() - 1][i], dp4[B.size() - 1][i - 1]);
    }
  }

  int ret = 0;

  for (int i = 0; i <= K; i++) {
    int aa = dp1[max(0, (int)A.size() - 1)][i];
    int bb = dp2[max(0, (int)A.size() - 1)][i];
    int cc = dp3[max(0, (int)B.size() - 1)][K - i];
    int dd = dp4[max(0, (int)B.size() - 1)][K - i];

    ret = max(ret, max(aa, -bb) + max(cc, -dd));
  }

  cout << ret << endl;
}