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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define DBG(X) cout << " " << #X << " : " << X << endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input {
  int n;
  char c;
  char str[10024];
  string s;
  ll l;
  double d;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  double nextDouble() {
    scanf("%lf", &d);
    return d;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

string s;

bool isNum(char c) { return c >= '0' && c <= '9'; }

int calc(int l) {
  // cout<<" "<<l<<" "<<s[l]<<endl;
  if (isNum(s[l])) {
    int ret = 0;
    while (l < s.size() && isNum(s[l])) {
      ret *= 10;
      ret += s[l] - '0';
      l++;
    }
    return ret / 2 + 1;
  }
  vector<int> v;
  int lv = 0;
  for (int i = l; i < s.size(); i++) {
    if (s[i] == '[') {
      if (lv == 0)
        v.pb(calc(i + 1));
      lv++;
    } else if (s[i] == ']')
      lv--;
    if (lv < 0)
      break;
  }
  sort(all(v));
  int ret = 0;
  assert(v.size() > 0);
  for (int i = 0; i < v.size() / 2 + 1; i++)
    ret += v[i];
  return ret;
}

int main() {
  int n = in.nextInt();
  rep(i, n) {
    s = in.next();
    // cout<<s<<endl;
    cout << calc(1) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define DBG(X) cout << " " << #X << " : " << X << endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  double d;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  double nextDouble() {
    scanf("%lf", &d);
    return d;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

string s;

bool isNum(char c) { return c >= '0' && c <= '9'; }

int calc(int l) {
  // cout<<" "<<l<<endl;
  if (isNum(s[l])) {
    int ret = 0;
    while (isNum(s[l])) {
      ret *= 10;
      ret += s[l] - '0';
      l++;
    }
    return ret / 2 + 1;
  }
  vector<int> v;
  int lv = 0;
  for (int i = l; i < s.size(); i++) {
    if (s[i] == '[') {
      if (lv == 0)
        v.pb(calc(i + 1));
      lv++;
    } else if (s[i] == ']')
      lv--;
    if (lv < 0)
      break;
  }
  sort(all(v));
  int ret = 0;
  assert(v.size() > 0);
  for (int i = 0; i < v.size() / 2 + 1; i++)
    ret += v[i];
  return ret;
}

int main() {
  int n = in.nextInt();
  rep(i, n) {
    s = in.next();
    cout << calc(1) << endl;
  }
}