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

#include <cmath>
#include <iostream>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF 100000000
using namespace std;
typedef long long ll;

string S;
ll dp[300][300][2];

ll number(int l, int r) {
  ll ret = 0;
  REP(i, l, r) {
    if ('0' <= S[i] && S[i] <= '9')
      ret = ret * 10 + (S[i] - '0');
    else
      return -1;
  }
  return ret;
}

ll dfs(int l, int r, int mode, int depth) {

  if (dp[l][r][mode] > -INF * 2)
    return dp[l][r][mode];

  ll n = number(l, r);
  if (n >= 0)
    return n;

  ll ret = mode == 0 ? -INF : INF;
  REP(i, l, r) if (S[i] == '+' || S[i] == '-') {
    int nl = l, nr = r;
    while (1) {
      ll n1 = dfs(nl, i, mode, depth + 1);
      ll n2 = dfs(i + 1, nr, S[i] == '-' ? 1 - mode : mode, depth + 1);
      if (abs(n1) != INF && abs(n2) != INF) {
        if (mode == 0) {
          if (S[i] == '+')
            ret = max(ret, n1 + n2);
          if (S[i] == '-')
            ret = max(ret, n1 - n2);
        } else {
          if (S[i] == '+')
            ret = min(ret, n1 + n2);
          if (S[i] == '-')
            ret = min(ret, n1 - n2);
        }
      }
      if (S[nl] != '(' && S[nr - 1] != ')')
        break;
      if (S[nl] == '(')
        nl++;
      if (S[nr - 1] == ')')
        nr--;
    }
  }
  return dp[l][r][mode] = ret;
}

int main(void) {
  cin >> S;

  REP(i, 0, 201) REP(j, 0, 201) REP(k, 0, 2) dp[i][j][k] = -INF * 2;
  cout << dfs(0, S.size(), 0, 0) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF 100000000
using namespace std;
typedef long long ll;

string S;
ll dp[300][300][2];

ll number(int l, int r) {
  ll ret = 0;
  REP(i, l, r) {
    if ('0' <= S[i] && S[i] <= '9')
      ret = ret * 10 + (S[i] - '0');
    else
      return -1;
  }
  return ret;
}

ll dfs(int l, int r, int mode, int depth) {
  ll ret;

  if (dp[l][r][mode] != -INF)
    ret = dp[l][r][mode];
  else {
    ll n = number(l, r);
    if (n >= 0)
      ret = n;
    else {
      ret = mode == 0 ? -INF : INF;
      REP(i, l, r) {
        if (S[i] != '+' && S[i] != '-')
          continue;

        int nl = l, nr = r;
        while (1) {
          ll n1 = dfs(nl, i, mode, depth + 1);
          ll n2 = dfs(i + 1, nr, S[i] == '-' ? 1 - mode : mode, depth + 1);
          if (abs(n1) != INF && abs(n2) != INF) {
            if (mode == 0) {
              if (S[i] == '+')
                ret = max(ret, n1 + n2);
              if (S[i] == '-')
                ret = max(ret, n1 - n2);
            } else {
              if (S[i] == '+')
                ret = min(ret, n1 + n2);
              if (S[i] == '-')
                ret = min(ret, n1 - n2);
            }
          }
          if (S[nl] != '(' && S[nr - 1] != ')')
            break;
          if (S[nl] == '(')
            nl++;
          if (S[nr - 1] == ')')
            nr--;
        }
      }
    }
  }

  return dp[l][r][mode] = ret;
}

int main(void) {
  cin >> S;

  REP(i, 0, 201) REP(j, 0, 201) REP(k, 0, 2) dp[i][j][k] = -INF;
  cout << dfs(0, S.size(), 0, 0) << endl;

  return 0;
}