alexjercan/bugnet huggingface dataset:
Problem p00768 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef struct {
  int ch[11];
  int num;
  int time;
  int tnum;
} lg;
int M, T, R, P;
vector<lg> team;

bool asc(lg t1, lg t2);

int main() {
  int i, j, m, t, p;

  for (i = 0; i < 51; i++) {
    lg x;
    team.push_back(x);
  }
  while (1) {
    cin >> M >> T >> P >> R;
    if (M == 0 && T == 0 && P == 0 && R == 0)
      break;
    for (i = 0; i <= T; i++) {
      team[i].num = 0;
      team[i].time = 0;
      team[i].tnum = i + 1;
      for (j = 0; j <= P; j++)
        team[i].ch[j] = 0;
    }
    for (i = 0; i < R; i++) {
      cin >> m >> t >> p >> j;
      if (j != 0)
        team[t - 1].ch[p]++;
      else {
        team[t - 1].time += 20 * team[t - 1].ch[p];
        team[t - 1].num++;
        team[t - 1].time += m;
      }
    }

    sort(team.begin(), team.begin() + T, asc);

    for (i = 0; i < T; i++) {
      if (i == 0)
        cout << team[i].tnum;
      else if (team[i - 1].num == team[i].num &&
               team[i - 1].time == team[i].time)
        cout << "=" << team[i].tnum;
      else
        cout << "," << team[i].tnum;
    }
    cout << endl;
  }
  return 0;
}

bool asc(lg t1, lg t2) {
  if (t1.num > t2.num)
    return 1;
  else if (t1.num == t2.num && t1.time < t2.time)
    return 1;
  else if (t1.num == t2.num && t1.time == t2.time && t1.tnum > t2.tnum)
    return 1;
  else
    return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int m;
vector<int> p;
vector<int> s;
int maxn, minn;

int main() {
  int i, j, now = 0, ni = 0;
  while (1) {
    cin >> m >> minn >> maxn;
    now = 0;
    if (m == 0 && maxn == 0 && minn == 0)
      break;
    for (i = 0; i < m; i++) {
      cin >> j;
      p.push_back(j);
    }
    sort(p.begin(), p.end());
    for (i = 0; i < m - 1; i++)
      s.push_back(p[i + 1] - p[i]);
    for (i = m - minn - 1; i >= m - maxn - 1; i--) {
      // cout << " bbb"<<s[i];
      if (s[i] >= now) {
        now = s[i];
        ni = i;
        // cout <<"aaa"<< now << " "<< ni << endl;
      }
    }
    // cout <<endl<< ni << endl;
    cout << m - 1 - ni << endl;
    while (p.size())
      p.pop_back();
    while (s.size())
      s.pop_back();
  }
  return 0;
}
Problem p00768 (C++) — buggy submission, verdict: Runtime Error

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

class Team {
public:
  int num;
  int solve;
  int time;
  vector<int> plist;
  Team() : num(0), solve(0), time(0), plist(vector<int>(11, 0)) {}
};

bool compare_num(const Team &left, const Team &right) {
  return left.num > right.num;
}
bool compare_solve(const Team &left, const Team &right) {
  return left.solve > right.solve;
}
bool compare_time(const Team &left, const Team &right) {
  return left.time < right.time;
}

int main() {
  int M, T, P, R;
  int m, t, p, j;
  vector<Team> team;
  for (;;) {
    scanf("%d %d %d %d", &M, &T, &P, &R);
    if (M == 0 && T == 0 && P == 0 && R == 0)
      return 0;
    for (int i = 0; i < T; i++) {
      Team temp;
      temp.num = i + 1;
      team.push_back(temp);
    }
    for (int i = 0; i < R; i++) {
      scanf("%d %d %d %d", &m, &t, &p, &j);
      if (j != 0)
        team.at(t - 1).plist.at(p) += 1;
      else {
        team.at(t - 1).solve += 1;
        team.at(t - 1).time += m;
        team.at(t - 1).time += team.at(t - 1).plist.at(p) * 20;
      }
    }
    stable_sort(team.begin(), team.end(), compare_num);
    stable_sort(team.begin(), team.end(), compare_time);
    stable_sort(team.begin(), team.end(), compare_solve);
    cout << team.at(0).num;
    for (int i = 1; i < T; i++) {
      if (team.at(i - 1).solve == team.at(i).solve &&
          team.at(i - 1).time == team.at(i).time)
        cout << "=";
      else
        cout << ",";
      cout << team.at(i).num;
    }
    cout << endl;
    team.clear();
  }
}

Error: 0

Fixed submission:

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

class Team {
public:
  int num;
  int solve;
  int time;
  vector<int> plist;
  Team() : num(0), solve(0), time(0), plist(vector<int>(11, 0)) {}
};

bool compare_solve(const Team &left, const Team &right) {
  return left.solve > right.solve;
}
bool compare_time(const Team &left, const Team &right) {
  return left.time <= right.time;
}

int main() {
  int M, T, P, R;
  int m, t, p, j;
  vector<Team> team;
  for (;;) {
    scanf("%d %d %d %d", &M, &T, &P, &R);
    if (M == 0 && T == 0 && P == 0 && R == 0)
      return 0;
    for (int i = 0; i < T; i++) {
      Team temp;
      temp.num = i + 1;
      team.push_back(temp);
    }
    for (int i = 0; i < R; i++) {
      scanf("%d %d %d %d", &m, &t, &p, &j);
      if (j != 0)
        team.at(t - 1).plist.at(p) += 1;
      else {
        team.at(t - 1).solve += 1;
        team.at(t - 1).time += m;
        team.at(t - 1).time += team.at(t - 1).plist.at(p) * 20;
      }
    }
    sort(team.begin(), team.end(), compare_time);
    stable_sort(team.begin(), team.end(), compare_solve);
    cout << team.at(0).num;
    for (int i = 1; i < T; i++) {
      if (team.at(i - 1).solve == team.at(i).solve &&
          team.at(i - 1).time == team.at(i).time)
        cout << "=";
      else
        cout << ",";
      cout << team.at(i).num;
    }
    cout << endl;
    team.clear();
  }
}
Problem p00768 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

class team {
public:
  int num, ans, time;
  team(int n) {
    num = n;
    ans = time = 0;
  }
  bool operator<(const team &t2) const {
    if (ans != t2.ans)
      return ans > t2.ans;
    else if (time != t2.time)
      return time < t2.time;
    else
      return num > t2.num;
  }
};

int main() {
  int M, T, P, R, m[2001], t[2001], p[2001], r[2001], before;
  while (cin >> M >> T >> P >> R, (M || T || P || R)) {
    vector<team> v;
    vector<team>::iterator pv;
    // 入力
    for (int i = 0; i < R; i++) {
      cin >> m[i] >> t[i] >> p[i] >> r[i];
    }

    // 各チームの時間
    for (int i = 1; i <= T; i++) {
      team te = team(i);
      for (int j = 0; j < R; j++) {
        // 正解時間の計算
        if (t[j] == i && r[j] == 0) {
          te.time += m[j];
          te.ans++;
          // 以前のペナルティを計算
          for (int k = 0; k < j; k++) {
            if (t[k] == i && p[k] == p[j]) {
              te.time += 20;
            }
          }
        }
      }
      v.push_back(te);
    }

    // ソート
    sort(v.begin(), v.end());
    pv = v.begin();
    before = -1;
    while (pv != v.end()) {
      if (before == pv->time)
        cout << "=" << pv->num;
      else {
        if (pv == v.begin())
          cout << pv->num;
        else
          cout << "," << pv->num;
        before = pv->time;
      }
      pv++;
    }
    cout << endl;
  }
  return 0;
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

class team {
public:
  int num, ans, time;
  team(int n) {
    num = n;
    ans = time = 0;
  }
  bool operator<(const team &t2) const {
    if (ans != t2.ans)
      return ans > t2.ans;
    else if (time != t2.time)
      return time < t2.time;
    else
      return num > t2.num;
  }
};

int main() {
  int M, T, P, R, m[2001], t[2001], p[2001], r[2001], before;
  while (cin >> M >> T >> P >> R, (M || T || P || R)) {
    vector<team> v;
    vector<team>::iterator pv;
    // 入力
    for (int i = 0; i < R; i++) {
      cin >> m[i] >> t[i] >> p[i] >> r[i];
    }

    // 各チームの時間
    for (int i = 1; i <= T; i++) {
      team te = team(i);
      for (int j = 0; j < R; j++) {
        // 正解時間の計算
        if (t[j] == i && r[j] == 0) {
          te.time += m[j];
          te.ans++;
          // 以前のペナルティを計算
          for (int k = 0; k < j; k++) {
            if (t[k] == i && p[k] == p[j]) {
              te.time += 20;
            }
          }
        }
      }
      v.push_back(te);
    }

    // ソート
    sort(v.begin(), v.end());
    pv = v.begin();
    before = -1;
    while (pv != v.end()) {
      if (before == pv->time)
        cout << "=" << pv->num;
      else {
        if (pv == v.begin())
          cout << pv->num;
        else
          cout << "," << pv->num;
        before = pv->time;
      }
      pv++;
    }
    cout << endl;
    v.~vector();
  }
  return 0;
}
Problem p00768 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int M, T, P, R;

  while (scanf("%d %d %d %d", &M, &T, &P, &R) && M) {
    vector<pair<pair<int, int>, int>> record(T);
    int wa[64][16] = {0};

    for (int i = 0; i < T; i++)
      record[i].second = -(i + 1);

    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      scanf("%d %d %d %d", &m, &t, &p, &j);
      if (j)
        wa[t - 1][p] += 20;
      else {
        record[t - 1].first.first--;
        record[t - 1].first.second += m + wa[t - 1][p];
      }
    }

    sort(record.begin(), record.end());

    for (int i = 0; i < T; i++) {
      printf("%d", -record[i].second);
      if (i + 1 < T && record[i].first == record[i + 1].first)
        printf("=");
      else if (i + 1 < T)
        printf(",");
      else
        printf("\\n");
    }
  }

  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int M, T, P, R;

  while (scanf("%d %d %d %d", &M, &T, &P, &R) && M) {
    vector<pair<pair<int, int>, int>> record(T);
    int wa[16][64] = {0};

    for (int i = 0; i < T; i++)
      record[i].second = -(i + 1);

    for (int i = 0; i < R; i++) {
      int m, t, p, j;
      scanf("%d %d %d %d", &m, &t, &p, &j);
      if (j)
        wa[t - 1][p] += 20;
      else {
        record[t - 1].first.first--;
        record[t - 1].first.second += m + wa[t - 1][p];
      }
    }

    sort(record.begin(), record.end());

    for (int i = 0; i < T; i++) {
      printf("%d", -record[i].second);
      if (i + 1 < T && record[i].first == record[i + 1].first)
        printf("=");
      else if (i + 1 < T)
        printf(",");
      else
        printf("\\n");
    }
  }

  return (0);
}
Problem p00768 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

int main() {
  int M, T, P, R;
  while (true) {
    cin >> M >> T >> P >> R;
    if (M == 0)
      return 0;
    vector<int> m(R), t(R), p(R), j(R);
    REP(i, R) {
      cin >> m[i] >> t[i] >> p[i] >> j[i];
      t[i]--;
      p[i]--;
    }
    vector<vector<int>> pena(T, vector<int>(P));
    vector<vector<int>> score(T, vector<int>(2)); // seikai,jikan

    REP(i, R) {
      if (j[i] == 0) {
        score[t[i]][0]++;
        score[t[i]][1] += pena[t[i]][p[i]] + m[i];
      }
      pena[t[i]][p[i]] += 20;
    }

    vector<tuple<int, int, int>> tupleScore(T);
    REP(i, T) { tupleScore[i] = make_tuple(-score[i][0], score[i][1], -1 - i); }
    sort(ALL(tupleScore));

    int top;
    tie(ignore, ignore, top) = tupleScore[0];
    cout << -top;
    REP(i, T - 1) {
      int ans1, tim1, ans2, tim2;
      tie(ans1, tim1, ignore) = tupleScore[i];
      tie(ans2, tim2, top) = tupleScore[i + 1];
      if (ans1 == ans2 && tim1 == tim2) {
        cout << "=" << -top;
      } else {
        cout << "," << -top;
      }
    }

    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

int main() {
  int M, T, P, R;
  while (true) {
    cin >> M >> T >> P >> R;
    if (M == 0)
      return 0;
    vector<int> m(R), t(R), p(R), j(R);
    REP(i, R) {
      cin >> m[i] >> t[i] >> p[i] >> j[i];
      t[i]--;
    }
    vector<vector<int>> pena(T, vector<int>(P));
    vector<vector<int>> score(T, vector<int>(2)); // seikai,jikan

    REP(i, R) {
      if (j[i] == 0) {
        score[t[i]][0]++;
        score[t[i]][1] += pena[t[i]][p[i]] + m[i];
      }
      pena[t[i]][p[i]] += 20;
    }

    vector<tuple<int, int, int>> tupleScore(T);
    REP(i, T) { tupleScore[i] = make_tuple(-score[i][0], score[i][1], -1 - i); }
    sort(ALL(tupleScore));

    int top;
    tie(ignore, ignore, top) = tupleScore[0];
    cout << -top;
    REP(i, T - 1) {
      int ans1, tim1, ans2, tim2;
      tie(ans1, tim1, ignore) = tupleScore[i];
      tie(ans2, tim2, top) = tupleScore[i + 1];
      if (ans1 == ans2 && tim1 == tim2) {
        cout << "=" << -top;
      } else {
        cout << "," << -top;
      }
    }

    cout << endl;
  }
  return 0;
}
Problem p00769 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <list>

using namespace std;

class D {
public:
  int sets;
  int n;
  list<D *> child;
  D() : child() {
    n = 0;
    sets = 0;
  };
  ~D() {}
};

char c[10002];

int get(D *d, int &l) {
  int sum = 0;
  if (c[l] == '[') {
    while (c[l] == '[') {
      D *dd = new D();
      ++l; //'['
      sum += get(dd, l);
      d->child.push_back(dd);
      d->sets++;
      l++; //']'
    }
    d->n = sum;
  } else {
    int num = c[l] - '0';
    for (l++; isdigit(c[l]); l++) {
      num = num * 10 + (c[l] - '0');
    }
    d->n = num;
    return num;
  }
  return sum;
}

int ans(D *d) {
  // cout << "in" << endl;
  if (d->sets == 0) {
    // cout << "okk" << endl;
    return d->n / 2 + 1;
  }
  // else
  auto func = [](D *d1, D *d2) { return d1->n < d2->n; };
  d->child.sort(func);
  // cout << "sets::" << d->sets << endl;
  list<int> ilist;
  auto it = d->child.begin();
  for (int i = 0; i < d->sets; i++) {
    D *cur = *it;
    // cout << i << ":::" << cur->n << endl;
    ilist.push_back(ans(cur));
    it++;
  }
  ilist.sort([](int &i1, int &i2) { return i1 < i2; });
  int sum = 0;
  auto iit = ilist.begin();
  for (int i = 0; i < d->sets / 2 + 1; i++) {
    sum += *iit;
    iit++;
  }
  return sum;
}

void solve() {
  int n;
  int l;
  cin >> n;
  // cout << n << endl;
  for (int i = 0; i < n; i++) {
    cin >> c;
    // cout << c << endl;
    D *d = new D();
    l = 1;
    get(d, l);
    cout << ans(d) << endl;
    // cout << "hoge" << endl;
    delete d;
  }
}

int main(void) { solve(); }

Error: WA

Fixed submission:

#include <cctype>
#include <iostream>
#include <list>

using namespace std;

class D {
public:
  int sets;
  int n;
  list<D *> child;
  D() : child() {
    n = 0;
    sets = 0;
  };
  ~D() {}
};

char c[BUFSIZ];

int get(D *d, int &l) {
  int sum = 0;
  if (c[l] == '[') {
    while (c[l] == '[') {
      D *dd = new D();
      ++l; //'['
      sum += get(dd, l);
      d->child.push_back(dd);
      d->sets++;
      l++; //']'
    }
    d->n = sum;
  } else {
    int num = c[l] - '0';
    for (l++; isdigit(c[l]); l++) {
      num = num * 10 + (c[l] - '0');
    }
    d->n = num;
    return num;
  }
  return sum;
}

int ans(D *d) {
  // cout << "in" << endl;
  if (d->sets == 0) {
    // cout << "okk" << endl;
    return d->n / 2 + 1;
  }
  // else
  int sum = 0;
  auto func = [](D *d1, D *d2) { return d1->n < d2->n; };
  d->child.sort(func);
  // cout << "sets::" << d->sets << endl;
  auto it = d->child.begin();
  for (int i = 0; i < d->sets / 2 + 1; i++) {
    D *cur = *it;
    // cout << i << ":::" << cur->n << endl;
    sum += ans(cur);
    it++;
  }
  return sum;
}

void solve() {
  int n;
  int l;
  cin >> n;
  cout << n << endl;
  for (int i = 0; i < n; i++) {
    cin >> c;
    // cout << c << endl;
    D *d = new D();
    l = 1;
    get(d, l);
    cout << ans(d) << endl;
    // cout << "hoge" << endl;
    delete d;
  }
}

int main(void) { solve(); }
Problem p00769 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int compute(string s) {
  int n = 0;
  int nest = 0;
  vector<int> vote[1000];
  for (int i = 0; i < (int)s.length(); i++) {
    if (s[i] == '[') {
      nest++;
    } else if (s[i] == ']') {
      if (vote[nest].size() == 0) {
        n = n / 2 + 1;
      } else {
        sort(vote[nest].begin(), vote[nest].end());
        n = 0;
        for (int i = 0; i <= (int)vote[nest].size() / 2; i++) {
          n += vote[nest][i];
        }
      }
      vote[nest].clear();
      nest--;
      vote[nest].push_back(n);
      n = 0;
    } else {
      n *= 10;
      n += s[i] - '0';
    }
  }
  return vote[0][0];
}

int main() {
  int n;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    cout << compute(s) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

class Record {
public:
  int t, n, m, s;
};

int compute_time(int s, int e, vector<Record> records) {
  int count = 0;
  int time = 0;
  int table[1261];
  for (int i = 0; i <= 1260; i++) {
    table[i] = 0;
  }
  for (int i = 0; i < (int)records.size(); i++) {
    table[records[i].t] += 2 * records[i].s - 1;
  }
  for (int i = 0; i <= 1260; i++) {
    count += table[i];
    if (count > 0 && s <= i && i < e) {
      time++;
    }
  }
  return time;
}

int main() {
  int n, m, r, q;
  while (true) {
    vector<Record> records[10001];
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    for (int i = 0; i <= 10000; i++) {
      records[i].clear();
    }
    cin >> r;
    for (int i = 0; i < r; i++) {
      Record rec;
      cin >> rec.t >> rec.n >> rec.m >> rec.s;
      records[rec.m].push_back(rec);
    }
    cin >> q;
    for (int i = 0; i < q; i++) {
      int s, e, m;
      cin >> s >> e >> m;
      cout << compute_time(s, e, records[m]) << endl;
    }
  }
}
Problem p00769 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int mincost[5001];
vector<pii> tx(5001);
vector<vector<int>> way(5001);
int main() {
  int n;
  cin >> n;
  while (n--) {
    string buf;
    cin >> buf;
    vector<pair<int, vector<int>>> g;
    int s = 0, n = 1;
    bool f = 0;
    vector<int> stack;
    g.push_back(make_pair(-1, vector<int>()));
    stack.push_back(0);
    for (int i = 1; i < buf.size(); i++) {
      switch (buf[i]) {
      case '[':
        f = 0;
        g.push_back(make_pair(-1, vector<int>()));
        g[stack.back()].second.push_back(n);
        stack.push_back(n++);
        break;
      case ']':
        stack.pop_back();
        break;
      default:
        if (f)
          break;
        g.back() =
            make_pair(stoi(string(buf.c_str() + i)) / 2 + 1, vector<int>());
        g[stack.back()].second.push_back(n);
        f = 1;
      }
    }
    while (g[0].first == -1) {
      for (int i = 0; i < g.size(); i++) {
        if (g[i].first != -1 || g[g[i].second[0]].first == -1)
          continue;
        vector<int> buf;
        for (int j = 0; j < g[i].second.size(); j++) {
          buf.push_back(g[g[i].second[j]].first);
        }
        sort(buf.begin(), buf.end());
        g[i].first =
            accumulate(buf.begin(), buf.begin() + (buf.size() + 2) / 2, 0);
      }
    }
    cout << g[0].first << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int mincost[5001];
vector<pii> tx(5001);
vector<vector<int>> way(5001);
int main() {
  int h, w;
  while (cin >> h >> w, h) {
    int m = h * h + w * w;
    int m1, h1;
    for (m1 = m;; m1++) {
      for (h1 = (m1 == m ? h + 1 : 1); h1 < 100 && h1 * h1 * 2 < m1; h1++) {
        if ((int)sqrt(m1 - h1 * h1) * (int)sqrt(m1 - h1 * h1) == m1 - h1 * h1)
          goto a;
      }
    }
  a:
    cout << h1 << " " << (int)sqrt(m1 - h1 * h1) << endl;
  }
}
Problem p00769 (C++) — buggy submission, verdict: Runtime Error

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int cases;
string s;

int dfs(int l, int r) {
  bool num = true;
  repl(i, l + 1, r - 1) {
    if (!isdigit(s[i])) {
      num = false;
      break;
    }
  }
  if (num) {
    return stoi(s.substr(l + 1, r - l - 2)) / 2 + 1;
  } else {
    int level = -1;
    int ll;
    vector<int> ns;
    repl(i, l, r) {
      if (s[i] == '[') {
        if (level == 0) {
          ll = i;
        }
        level++;
      } else if (s[i] == ']') {
        level--;
        if (level == 0) {
          ns.pb(dfs(ll, i + 1));
        }
      }
    }
    sort(all(ns));
    int res = 0;
    rep(i, ns.size() / 2 + 1) { res += ns[i]; }
    return res;
  }
}

int main() {
  cin.sync_with_stdio(false);
  cin >> cases;
  while (cases--) {
    cin >> s;
    cout << dfs(0, s.length()) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <string.h>

#define MAX_N 100

using namespace std;

int N;
char str[10000];

int get_num(int l) {
  int res = 0;
  for (int i = l; str[i] != ']'; i++) {
    res += (str[i] - '0');
    res *= 10;
  }
  res /= 10;
  return res;
}

int dfs(int l,
        int r) { // "[....]"??¨?????£???????????????(...???)????????°?????????
  if (str[l + 1] >= '0' && str[l + 1] <= '9')
    return get_num(l + 1) / 2 + 1;
  int a[1000];
  int count = 0; // ??????????????°
  int k = 0, nl = l + 1;
  for (int i = l + 1; i < r; i++) {
    if (str[i] == '[') {
      k++;
    } else if (str[i] == ']') {
      k--;
    }
    if (k == 0) {
      a[count] = dfs(nl, i);
      nl = i + 1;
      count++;
    }
  }
  sort(a, a + count);
  int sum = 0;
  for (int i = 0; i < count / 2 + 1; i++) {
    sum += a[i];
  }
  return sum;
}

void solve() {
  int res = dfs(0, strlen(str) - 1);
  cout << res << endl;
  return;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> str;
    solve();
  }
  return 0;
}