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

#include <bits/stdc++.h>
using namespace std;

int n, m;
int a[50], b[50];
int A[51], B[51];

int dp[51][51][51][51][2][2];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == -1)
      a[i] = 0;
    A[i + 1] = A[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (b[i] == -1)
      b[i] = 0;
    B[i + 1] = B[i] + b[i];
  }
  memset(dp, -1, sizeof(dp));

  for (int x = n; x >= 0; x--) {
    for (int y = m; y >= 0; y--) {
      for (int s = x; s >= 0; s--) {
        for (int t = y; t >= 0; t--) {
          for (int f = 1; f >= 0; f--) {
            for (int p = 1; p >= 0; p--) {

              int &res = dp[x][y][s][t][f][p];

              if (p == 0) {
                res = -1e9;

                if (f == 1 && x == s && y == t) {
                  res = max(res, 0);
                } else {
                  int nf = (x == s && y == t);
                  res = max(res,
                            dp[x][y][x][y][nf][1] + A[x] - A[s] - B[y] + B[t]);
                }

                if (x < n && a[x] == 0) {
                  res = max(res, dp[x + 1][y][s][y][0][1]);
                }
                if (x < n && a[x] > 0) {
                  res = max(res, dp[x + 1][y][s][t][0][1]);
                }

              } else {
                res = 1e9;

                if (f == 1 && x == s && y == t) {
                  res = min(res, 0);
                } else {
                  int nf = (x == s && y == t);
                  res = min(res,
                            dp[x][y][x][y][nf][0] + A[x] - A[s] - B[y] + B[t]);
                }

                if (y < m && b[y] == 0) {
                  res = min(res, dp[x][y + 1][x][t][0][0]);
                }
                if (y < m && b[y] > 0) {
                  res = min(res, dp[x][y + 1][s][t][0][0]);
                }
              }
            }
          }
        }
      }
    }
  }
  cout << dp[0][0][0][0][0][0] << endl;
  return 0;
}

Error: 127
/tmp/1cd66196-a67f-4766-b298-f1b882a4411f.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, m;
int a[50], b[50];
int A[51], B[51];

int dp[50][50][50][50][2][2];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == -1)
      a[i] = 0;
    A[i + 1] = A[i] + a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
    if (b[i] == -1)
      b[i] = 0;
    B[i + 1] = B[i] + b[i];
  }
  memset(dp, -1, sizeof(dp));

  for (int x = n; x >= 0; x--) {
    for (int y = m; y >= 0; y--) {
      for (int s = x; s >= 0; s--) {
        for (int t = y; t >= 0; t--) {
          for (int f = 1; f >= 0; f--) {
            for (int p = 1; p >= 0; p--) {

              int &res = dp[x][y][s][t][f][p];

              if (p == 0) {
                res = -1e9;

                if (p == 1 && x == s && y == t) {
                  res = max(res, 0);
                } else {
                  int nf = (x == s && y == t);
                  res = max(res,
                            dp[x][y][x][y][nf][1] + A[x] - A[s] - B[y] + B[t]);
                }

                if (x < n && a[x] == 0) {
                  res = max(res, dp[x + 1][y][s][y][0][1]);
                }
                if (x < n && a[x] > 0) {
                  res = max(res, dp[x + 1][y][s][t][0][1]);
                }

              } else {
                res = 1e9;

                if (p == 1 && x == s && y == t) {
                  res = min(res, 0);
                } else {
                  int nf = (x == s && y == t);
                  res = min(res,
                            dp[x][y][x][y][nf][0] + A[x] - A[s] - B[y] + B[t]);
                }

                if (y < m && b[y] == 0) {
                  res = min(res, dp[x][y + 1][x][t][0][0]);
                }
                if (y < m && b[y] > 0) {
                  res = min(res, dp[x][y + 1][s][t][0][0]);
                }
              }
            }
          }
        }
      }
    }
  }
  cout << dp[0][0][0][0][0][0] << endl;
  return 0;
}