Problem p01813 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int cal(string &s, int &i) {
  int res = 0;
  while (1) {
    if (isdigit(s[i]))
      res = s[i++] - '0';
    else if (s[i] == '(') {
      res = cal(s, ++i);
    } else if (s[i] == '+')
      res += cal(s, ++i);
    else if (s[i] == '-')
      res -= cal(s, ++i);
    else
      break;
  }
  return res;
}

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];
}

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 j = 0;
  int n = s.size(), res = calc(s), d = -1;
  for (int i = 0; i < n; i++)
    d += (s[i] == '+' || s[i] == '-');

  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 || (cnt && s[i - 2] == '(') || (d && 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: TLE

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 = (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];
}

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), d = -1;
  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 || (cnt && s[i - 2] == '(') || (d && 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;
}