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

#include <bits/stdc++.h>
using namespace std;
#define INF (1e8)
#define MAX 205
int n;
char s[MAX];

int dpA[MAX][MAX];
int dpB[MAX][MAX];
bool flgA[MAX][MAX];
bool flgB[MAX][MAX];

int solveA(int, int);
int solveB(int, int);

int main() {
  scanf("%s", s);
  n = strlen(s);
  int ans = solveA(0, n);
  assert(abs(ans) < 1e6);
  printf("%d\\n", ans);
  return 0;
}

int solveA(int l, int r) {
  if (flgA[l][r])
    return dpA[l][r];
  flgA[l][r] = true;
  int res = -INF, cnt = 0, num = 0;
  int L = INF, R = -INF;
  for (int i = l; i < r; i++) {
    if (s[i] < '0' || '9' < s[i])
      continue;
    L = min(L, i);
    R = max(R, i);
  }
  for (int i = l; i < r; i++) {
    if (s[i] == '+') {
      if (L <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < R && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = max(res, solveA(l, i) + solveA(i + 1, r));
    } else if (s[i] == '-') {
      if (L <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < R && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = max(res, solveA(l, i) - solveB(i + 1, r));
    } else if ('0' <= s[i] && s[i] <= '9') {
      cnt++;
      num = s[i] - '0';
    }
  }
  if (cnt == 1)
    res = num;

  return dpA[l][r] = res;
}

int solveB(int l, int r) {
  if (flgB[l][r])
    return dpB[l][r];
  flgB[l][r] = true;
  int res = INF, cnt = 0, num = 0;
  int L = INF, R = -INF;
  for (int i = l; i < r; i++) {
    if (s[i] < '0' || '9' < s[i])
      continue;
    L = min(L, i);
    R = max(R, i);
  }

  for (int i = l; i < r; i++) {
    if (s[i] == '+') {
      if (L <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < R && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = min(res, solveB(l, i) + solveB(i + 1, r));
    } else if (s[i] == '-') {
      if (L <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < R && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = min(res, solveB(l, i) - solveA(i + 1, r));
    } else if ('0' <= s[i] && s[i] <= '9') {
      cnt++;
      num = s[i] - '0';
    }
  }
  if (cnt == 1)
    res = num;
  return dpB[l][r] = res;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF (1e8)
#define MAX 205
int n;
char s[MAX];

int dpA[MAX][MAX];
int dpB[MAX][MAX];
bool flgA[MAX][MAX];
bool flgB[MAX][MAX];

int solveA(int, int);
int solveB(int, int);

int main() {
  scanf("%s", s);
  n = strlen(s);
  int ans = solveA(0, n);
  assert(abs(ans) < 1e6);
  printf("%d\\n", ans);
  return 0;
}

int solveA(int l, int r) {
  if (flgA[l][r])
    return dpA[l][r];
  flgA[l][r] = true;
  int res = -INF, cnt = 0, num = 0;
  for (int i = l; i < r; i++) {
    if (s[i] == '+') {
      if (l <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < r && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = max(res, solveA(l, i) + solveA(i + 1, r));
    } else if (s[i] == '-') {
      if (l <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < r && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = max(res, solveA(l, i) - solveB(i + 1, r));
    } else if ('0' <= s[i] && s[i] <= '9') {
      cnt++;
      num = s[i] - '0';
    }
  }
  if (cnt == 1)
    res = num;
  return dpA[l][r] = res;
}

int solveB(int l, int r) {
  if (flgB[l][r])
    return dpB[l][r];
  flgB[l][r] = true;
  int res = INF, cnt = 0, num = 0;
  for (int i = l; i < r; i++) {
    if (s[i] == '+') {
      if (l <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < r && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = min(res, solveB(l, i) + solveB(i + 1, r));
    } else if (s[i] == '-') {
      if (l <= i - 3 && '0' <= s[i - 1] && s[i - 1] <= '9' && s[i - 2] == '(')
        continue;
      if (i + 3 < r && '0' <= s[i + 1] && s[i + 1] <= '9' && s[i + 2] == ')')
        continue;
      res = min(res, solveB(l, i) - solveA(i + 1, r));
    } else if ('0' <= s[i] && s[i] <= '9') {
      cnt++;
      num = s[i] - '0';
    }
  }
  if (cnt == 1)
    res = num;
  return dpB[l][r] = res;
}