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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
#define MOD 1000000007
#define ARRAY_MAX 10005

const int INF = 1e9 + 7;

string s;
int now = 0;

int solve() {

  if (isdigit(s[now])) {
    int num = 0;
    while (now < s.size() && isdigit(s[now])) {
      num = num * 10 + s[now] - '0';
      now++;
    }
    return (num + 1) / 2;
  } else {

    vector<int> vec;
    int cnt = 0;
    while (s[now] == '[') {
      now++;
      vec.push_back(solve());
      now++;
    }
    sort(vec.begin(), vec.end());
    for (int i = 0; i < (vec.size() + 1) / 2; i++) {
      cnt += vec[i];
    }
    return cnt;
  }
}

int main() {

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    now = 0;
    cin >> s;
    cout << solve() << endl;
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
#define MOD 1000000007
#define ARRAY_MAX 10005

const int INF = 1e9 + 7;

vector<vector<int>> G(ARRAY_MAX);
int maxi = 0;

void dfs(int &now, int dpt, string &s) {

  int cc = 0;
  while (now < s.length() && s[now] != ']') {
    if (s[now] == '[') {
      now++;
      dfs(now, dpt + 1, s);
    } else {
      cc = cc * 10 + s[now] - '0';
      now++;
    }
  }
  // cout << cc << " " << dpt << endl;
  // ここの時点で']'が来ている
  if (cc != 0) {
    G[dpt].push_back(cc);
    maxi = max(maxi, dpt);
  }
  if (now + 1 <= s.length() - 1) {
    if (s[now] == ']' && s[now + 1] == ']') {
      sort(G[dpt].begin(), G[dpt].end());
      int cnt = 0;
      if (dpt == maxi) {
        /*for(int i = 0;i < (G[dpt].size()+1)/2;i++){
            cout << G[dpt][i] << " ";
        }
        cout << endl;*/
        for (int i = 0; i < (G[dpt].size() + 1) / 2; i++) {
          cnt += (G[dpt][i] + 1) / 2;
        }
      } else {
        for (int i = 0; i < (G[dpt].size() + 1) / 2; i++) {
          cnt += G[dpt][i];
        }
      }
      // cout << "cnt = " << cnt << " dpt = " << dpt << endl;
      G[dpt - 1].push_back(cnt);
      G[dpt].clear();
    }
  }
  now++;
}

int main() {

  int n;
  cin >> n;
  int st = 0;
  for (int rep = 0; rep < n; rep++) {

    string s;
    cin >> s;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '[') {
        maxi++;
      } else {
        break;
      }
    }
    dfs(st, 0, s);
    cout << G[1][0] << endl;

    for (int i = 0; i < G[i].size(); i++) {
      G[i].clear();
    }
  }

  return 0;
}