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

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef long long int ll;
string s, pat = "+-*";

ll parse(int l, int r, vector<int> prio) {
  int ptr = l;

  vector<ll> val;
  vector<char> op;
  while (ptr < r) {
    // 演算子
    if (pat.find(s[ptr]) != string::npos) {
      op.push_back(s[ptr++]);
    }
    // 数字
    else if (isdigit(s[ptr])) {
      ll tmp = 0;
      while (isdigit(s[ptr])) {
        tmp = (tmp * 10) + (s[ptr++] - '0');
      }
      val.push_back(tmp);
    }
    // 括弧
    else if (s[ptr] == '(') {
      int cnt = 0;
      int nl = ptr + 1, nr = -1;
      while (1) {
        if (s[ptr] == '(')
          cnt++;
        else if (s[ptr] == ')') {
          cnt--;
          if (cnt == 0)
            break;
        }
        ptr++;
      }
      nr = ptr++;
      val.push_back(parse(nl, nr, prio));
    }
  }

  /*
  for(int i=0; i<op.size(); i++) {
      printf("%lld %c ", val[i], op[i]);
  }
  printf("%lld\\n", val[op.size()]);
  */

  // マージ
  for (int p = 0; p < 3; p++) {
    for (size_t i = 0; i < op.size(); i++) {
      // printf("op %c (%d)\\n", op[i], prio[pat.find(op[i])]);
      if (prio[pat.find(op[i])] == p) {
        // printf("op %c: %lld, %lld\\n", op[i], val[i], val[i+1]);
        if (op[i] == '+')
          val[i] = val[i] + val[i + 1];
        if (op[i] == '-')
          val[i] = val[i] - val[i + 1];
        if (op[i] == '*')
          val[i] = val[i] * val[i + 1];
        val.erase(val.begin() + i + 1);
        op.erase(op.begin() + i);
        i--;
      }
    }
  }
  return val[0];
}

int main() {
  cin >> s;
  int M = s.length();
  ll ans = parse(0, M, vector<int>{0, 0, 0});

  for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
      for (int z = 0; z < 3; z++) {
        vector<int> v = {x, y, z};
        // printf("parse: %lld\\n", parse(0, M, v));
        ans = max(ans, parse(0, M, v));
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef long long int ll;
string s, pat = "+-*";

ll parse(int l, int r, vector<int> prio) {
  int ptr = l;

  vector<ll> val;
  vector<char> op;
  while (ptr < r) {
    // 演算子
    if (pat.find(s[ptr]) != string::npos) {
      op.push_back(s[ptr++]);
    }
    // 数字
    else if (isdigit(s[ptr])) {
      ll tmp = 0;
      while (isdigit(s[ptr])) {
        tmp = (tmp * 10) + (s[ptr++] - '0');
      }
      val.push_back(tmp);
    }
    // 括弧
    else if (s[ptr] == '(') {
      int nl = ptr + 1, nr = -1;
      while (s[ptr] != ')')
        ptr++;
      nr = ptr++;
      val.push_back(parse(nl, nr, prio));
    }
  }

  /*
  for(int i=0; i<op.size(); i++) {
      printf("%lld %c ", val[i], op[i]);
  }
  printf("%lld\\n", val[op.size()]);
  */

  // マージ
  for (int p = 0; p < 3; p++) {
    for (size_t i = 0; i < op.size(); i++) {
      // printf("op %c (%d)\\n", op[i], prio[pat.find(op[i])]);
      if (prio[pat.find(op[i])] == p) {
        // printf("op %c: %lld, %lld\\n", op[i], val[i], val[i+1]);
        if (op[i] == '+')
          val[i] = val[i] + val[i + 1];
        if (op[i] == '-')
          val[i] = val[i] - val[i + 1];
        if (op[i] == '*')
          val[i] = val[i] * val[i + 1];
        val.erase(val.begin() + i + 1);
        op.erase(op.begin() + i);
        i--;
      }
    }
  }
  return val[0];
}

int main() {
  cin >> s;
  int M = s.length();
  ll ans = parse(0, M, vector<int>{0, 0, 0});

  for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
      for (int z = 0; z < 3; z++) {
        vector<int> v = {x, y, z};
        // printf("parse: %lld\\n", parse(0, M, v));
        ans = max(ans, parse(0, M, v));
      }
    }
  }
  cout << ans << endl;
  return 0;
}