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

#include <bits/stdc++.h>
#define INF (1e9)
using namespace std;

int calc(string s) {
  vector<int> v;
  for (int i = 0; i < s.size(); i++) {
    int n = (int)v.size() - 1;
    if (s[i] == '+')
      v.push_back(0);
    if (s[i] == '-')
      v.push_back(1);
    if (s[i] == '(')
      v.push_back(0), v.push_back(0);
    if (s[i] == ')') {
      if (v.size() < 3)
        continue;
      v[n - 2] += v[n - 1] ? -v[n] : v[n];
      v.pop_back(), v.pop_back();
    }

    if (isdigit(s[i])) {
      int num = s[i] - '0';
      if (v.size() >= 2) {
        v[n - 1] += v[n] ? -num : num;
        v.pop_back();
      } else
        v.push_back(num);
    }
  }
  return v[0];
}

bool check(string s) {
  if (s[0] != '(')
    return 0;
  int i = 1, c = 1;
  while (c)
    c += (s[i] == '(') - (s[i++] == ')');
  return i == (int)s.size();
}

map<string, int> mem[2];
int dfs(string s, int f) {
  while (check(s))
    s = s.substr(1, s.size() - 2);
  if (s.size() == 1)
    return s[0] - '0';
  if (mem[f].count(s))
    return mem[f][s];

  int n = s.size(), res = calc(s), d = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '+' || s[i] == '-')
      d++;
  for (int i = 0, c = 0, cnt = 0; i < n; i++, cnt++, d--) {
    while (i < n) {
      while (s[i] == '(' || s[i] == ')')
        c += (s[i] == '(') - (s[i] == ')'), i++;
      if (s[i] == '+' || s[i] == '-')
        break;
      i++;
    }
    if (i >= n)
      break;
    if (cnt && s[i - 2] == '(')
      continue;
    if (d != 1 && s[i + 2] == ')')
      continue;
    string a = s.substr(0, i), b = s.substr(i + 1, n - i - 1);
    int t = c, ch = s[i];
    while (t--)
      a += ')', b = '(' + b;
    int mxa = dfs(a, 0), mna = dfs(a, 1);
    int mxb = dfs(b, 0), mnb = dfs(b, 1);
    if (f == 0 && ch == '+')
      res = max(res, mxa + mxb);
    if (f == 0 && ch == '-')
      res = max(res, mxa - mnb);
    if (f == 1 && ch == '+')
      res = min(res, mna + mnb);
    if (f == 1 && ch == '-')
      res = min(res, mna - mxb);
  }
  return mem[f][s] = res;
}

int main() {
  string S;
  cin >> S;

  cout << dfs(S, 0) << endl;
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define INF (1e9)
using namespace std;

int calc(string s) {
  vector<int> v;
  for (int i = 0; i < s.size(); i++) {
    int n = v.size() - 1;
    if (s[i] == '+')
      v.push_back(0);
    if (s[i] == '-')
      v.push_back(1);
    if (s[i] == '(')
      v.push_back(0), v.push_back(0);
    if (s[i] == ')') {
      v[n - 2] += v[n - 1] ? -v[n] : v[n];
      v.pop_back(), v.pop_back();
    }

    if (isdigit(s[i])) {
      int num = s[i] - '0';
      if (v.size() >= 2) {
        v[n - 1] += v[n] ? -num : num;
        v.pop_back();
      } else
        v.push_back(num);
    }
  }
  return v[0];
}

map<string, int> mem[2];
int dfs(string s, int f) {
  if (s.size() == 1)
    return s[0] - '0';
  if (mem[f].count(s))
    return mem[f][s];
  int n = s.size(), res = calc(s);
  for (int i = 0, c = 0; i < n; i++) {
    while (i < n) {
      while (s[i] == '(' || s[i] == ')')
        c += (s[i] == '(') - (s[i] == ')'), i++;
      if (s[i] == '+' || s[i] == '-')
        break;
      i++;
    }
    if (i >= n)
      break;
    if ((i - 2 >= 0 && s[i - 2] == '('))
      continue;
    string a = s.substr(0, i), b = s.substr(i + 1, n - i - 1);
    int t = c, ch = s[i];
    while (t--)
      a += ')', b = '(' + b;
    int mxa = dfs(a, 0), mna = dfs(a, 1);
    int mxb = dfs(b, 0), mnb = dfs(b, 1);
    if (f == 0 && ch == '+')
      res = max(res, mxa + mxb);
    if (f == 0 && ch == '-')
      res = max(res, mxa - mnb);
    if (f == 1 && ch == '+')
      res = min(res, mna + mnb);
    if (f == 1 && ch == '-')
      res = min(res, mna - mxb);
  }
  return mem[f][s] = res;
}

int main() {
  string S;

  cin >> S;
  cout << dfs(S, 0) << endl;
  return 0;
}