Problem:
Hierarchical Democracy
The presidential election in Republic of Democratia is carried out through multiple stages as follows.
There are exactly two presidential candidates.
At the first stage, eligible voters go to the polls of his/her electoral district.
The winner of the district is the candidate who takes a majority of the votes.
Voters cast their ballots only at this first stage.
A district of the
k
-th stage (
k
> 1) consists of multiple districts of the (
k
− 1)-th stage. In contrast, a district of the (
k
− 1)-th stage is a sub-district of one and only one district of the
k
-th stage. The winner of a district of the
k
-th stage is the candidate who wins in a majority of its sub-districts of the (
k
− 1)-th stage.
The final stage has just one nation-wide district. The winner of the final stage is chosen as the president.
You can assume the following about the presidential election of this country.
Every eligible voter casts a vote.
The number of the eligible voters of each electoral district of the first stage is odd.
The number of the sub-districts of the (
k
− 1)-th stage that constitute a district of the
k
-th stage (
k
> 1) is also odd.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes.
Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively.
The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third.
In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. 
Although you might consider this election system unfair, you should accept it as a reality.
Input
The entire input looks like:
the number of datasets (=n)
1st dataset
2nd dataset
…
n-th dataset
The number of datasets,
n
, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
An electoral district of the first stage is denoted as [
c
], where
c
is the number of the eligible voters of the district.
A district of the
k
-th stage (
k
> 1) is denoted as [
d
1
d
2
…
d
m
], where
d
1
,
d
2
, …,
d
m
denote its sub-districts of the (
k
− 1)-th stage in this notation.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123].
A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation.
You can assume the following.
The character string in each dataset does not include any characters except digits (
...(truncated)

Submitted solutions:

code_snippet: #include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <numeric>
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define phb push_back
using namespace std;

int Z;
string str;

void solve();

int dfs(int, int);

int main() {
  cin >> Z;
  for (int zi = 1; zi <= Z; ++zi) {
    cin >> str;
    solve();
  }

  return 0;
}

void solve() {
  cout << dfs(0, sz(str)) << "\n";
}

int dfs(int l, int r) {
  ++l, --r;

  if (str[l] != '[') {
    istringstream iss(str.substr(l, r - l));
    int k;

    iss >> k;
    return (k + 1) / 2;
  }
  else {
    vector< int > vi;
    int cnt = 0, pre = l;

    for (int i = l; i < r; ++i) {
      if (str[i] == '[')
        ++cnt;
      else if (str[i] == ']') {
        --cnt;
        if (cnt == 0)
          vi.phb(dfs(pre, i + 1)), pre = i + 1;
      }
    }

    stable_sort(all(vi));
    return accumulate(vi.begin(), vi.begin() + (sz(vi) + 1) / 2, 0);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 8

code_snippet: #include <utility>

namespace atcoder {

namespace internal {

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

struct barrett {
    unsigned int _m;
    unsigned long long im;

    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    unsigned int umod() const { return _m; }

    unsigned int mul(unsigned int a, unsigned int b) const {

        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b


        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
#include <functional>
#include<unordered_map>
#include <random>
#include<bitset>
using namespace std;  
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
//#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define arr(x) (x).rbegin(),(x).rend()
typedef long long ll;
typedef unsigned long long ull;
const ll inf=4e18;  
using graph = vector<vector<int> > ;
using vi=vector<int>;
using P= pair<ll,ll>;  
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vll=vector<ll>; 
using vvll=vector<vll>;
using vp=vector<P>;
using vvp=vector<vp>;
using vd=vector<double>;
using vvd =vector<vd>;
//using vs=vector<string>;
//string T="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string S="abcdefghijklmnopqrstuvwxyz";
//g++ main.cpp -std=c++17 -I .  
//cout <<setprecision(20);
//cout << fixed << setprecision(10);
//cin.tie(0); ios::sync_with_stdio(false);

const double PI = acos(-1);
int vx[]={0,1,0,-1,-1,1,1,-1},vy[]={1,0,-1,0,1,1,-1,-1};
void putsYes(bool f){cout << (f?"Yes":"No") << endl;}
void putsYES(bool f){cout << (f?"YES":"NO") << endl;}
void putsFirst(bool f){cout << (f?"First":"Second") << endl;}
void debug(int test){cout << "TEST" << " " << test << endl;}
ll pow_pow(ll x,ll n,ll mod){
    if(n==0) return 1; 
    x%=mod;
    ll res=pow_pow(x*x%mod,n/2,mod);
    if(n&1)res=res*x%mod;
    return res;
}
ll gcdll(ll x,ll y){
    if(y==0)return x;
    return gcdll(y,x%y);
}
ll INFF=1000100100100100100;
ll lcmll(ll x,ll y){
    ll X=(x/gcdll(x,y)),Y=y;
    if(X<=INFF/Y)return X*Y;
    else return INFF;
}
template<class T> bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    else return false;
}
template<class T> bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    else return false;
}
struct edge{
  int to; ll cost;int t;
  edge(int to,ll cost,int t) : to(to),cost(cost),t(t){}
};
using ve=vector<edge>;
using vve=vector<ve>;
using mint = modint;
//using mint = modint1000000007;
using vm=vector<mint>;
using vvm=vector<vm>;
using vvvm=vector<vvm>;
int mod = 1e9+9;
//int mod=1e9+7;
constexpr int MAX =10;
ll fact[MAX],finv[MAX],inv[MAX];
void initcomb(){
  fact[0]=fact[1]=1;
  finv[0]=finv[1]=1;
  inv[1]=1;
  for(int i=2;i<MAX;i++){
    fact[i]=fact[i-1]*i%mod;
    inv[i]=mod-inv[mod%i]*(mod/i)%mod;
    finv[i]=finv[i-1]*inv[i]%mod;
  }
}
ll comb(ll n,ll k){
  if(n<k) return 0;
  if(n<0||k<0) return 0;
  return fact[n]*(finv[k]*finv[n-k]%mod)%mod;
}

struct UnionFind { 
    vector<int> par, siz;
    UnionFind(int n) : par(n, -1) , siz(n, 1) { } 
    int root(int x) { 
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false; 
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};
struct Compress{
   vll a;
   int cnt;
   Compress() :cnt(0) {}
   void add(ll x){a.push_back(x);}
   void init(){
       sort(all(a));
       a.erase(unique(all(a)),a.end());
       cnt=a.size();
   }
   ll to(ll x){
       int index=lower_bound(all(a),x)-a.begin();
       return index;
   } //変換先
   ll from(int x){return a[x];}//逆引き
   int size(){return cnt;}
};
vll anss;
int EXIT=0;
void solve(int test){
    string s; cin >> s;
    int n=s.size();
    vi st;
    vi rs(n,-1);
    rep(i,n){
        if(s[i]=='[')st.push_back(i);
        if(s[i]==']'){
            int l=st.back();
            st.pop_back();
            rs[l]=i;
        }
    }
    vvi g(n);
    rep(i,n)if(rs[i]!=-1){
        for(int j=i-1; j>=0; j--){
            if(rs[j]>rs[i]){
                g[j].push_back(i);
                break;
            }
        }
    }
    vvll dp(n,vll(2,inf));
    auto dfs=[&](auto dfs,int v)->void{
        if(g[v].size()==0){
            int l=v+1,r=rs[v]-1;
            ll num=0;
            repi(j,l,r+1){
                num=num*10+s[j]-'0';
            }
            dp[v][0]=0;
            dp[v][1]=(num+1)/2;
        }
        else {
            int si=g[v].size();
            vll vals;
            for(auto &u:g[v]){
                dfs(dfs,u);
                vals.push_back(dp[u][1]);
            }
            sort(all(vals));
            dp[v][0]=0;
            dp[v][1]=0;
            rep(j,(si+1)/2)dp[v][1]+=vals[j];
        }
    };
    dfs(dfs,0);
    cout << dp[0][1] << endl;
}
//g++ cf.cpp -std=c++17 -I .
int main(){cin.tie(0);ios::sync_with_stdio(false);
    {
        mod=998244353;
        initcomb();
        modint::set_mod(mod);
    }
    int t; cin >> t;
    rep(test,t)solve(test);
    /*
    for(int test=0;;test++){
        solve(test);
        if(EXIT)break;
    }
    */
    for(auto ans:anss){
        cout<< ans << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4156
final_rank: 13

code_snippet: #define _GLIBCXX_DEBUG /* これをするとvectorの配列外参照などがチェックされる */
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const bool DEBUG = true;
#define debug(x) \
    if (DEBUG) cerr << __FILE__ << ':' << __LINE__ << ") " << #x << " : " << (x) << endl;

// [[[3][5][7]][[[5][2][3]]]

// [ 読む
// 中身評価
// ] 読む
// 中身 := 中のやつで過半数取るのに必要な

/*
    ][ 以前のやつを
    calc(][ 以前のやつ)
    calc の結果を vector に入れて 最後にソート
*/

// sを評価
ll calc(string s) {
    


    string t = "";
    vector<ll> vec;
    s.pop_back();
    s.erase(s.begin());
    
    
    if (s.back() != ']')return (stoi(s) + 1) / 2;
    
    ll count = 0;
    for (int ind = 0; ind < s.size(); ind++) {
        if (s[ind] == '[')
            count++;
        else if (s[ind] == ']') {
            count--;
        }

        t += s[ind];
        if (count == 0) {
            vec.push_back(calc(t));
            t = "";
        } else {
            
        }
    }

    sort(vec.begin(), vec.end());
    int sz = int(vec.size());
    sz = (sz + 1) / 2;
    ll res = 0;
    for (int i = 0; i < sz; i++) {
        res += vec[i];
    }
    
    return res;
}

int main() {
    int n;
    cin >> n;
    while (n-- > 0) {
        string s;
        cin >> s;
        cout << calc(s) << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3504
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)
int co;
int func(string s) {
  vector<int> vv;
  if (s[co] != '[' && s[co] != ']') {
    int val = s[co++] - '0';
    while (s[co] != ']')
      val = 10 * val + (s[co++] - '0');
    vv.push_back(val / 2 + 1);
  } else {
    while (s[co++] == '[') {
      vv.push_back(func(s));
      if (s[co + 1] != ']')
        ++co;
    }
  }
  sort(vv.begin(), vv.end());
  int sum = 0;
  REP(i, vv.size() / 2 + 1) { sum += vv[i]; }
  return sum;
}
int main() {
  int n;
  vector<int> v;
  cin >> n;
  REP(i, n) {
    string s;
    cin >> s;
    co = 0;
    v.push_back(func(s));
  }
  REP(i, n) cout << v[i] << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3272
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
int toint(string s) {
  stringstream sst;
  sst << s;
  int a;
  sst >> a;
  return a;
}
int rec(string s) {
  int kk = 0;
  rep(i, s.size()) {
    if (s[i] == '[' || s[i] == ']')
      kk++;
  }
  if (kk == 2) {
    return (toint(s.substr(1, s.size() - 2)) + 1) / 2;
  }
  int cnt = 0;
  int st = 0;
  vector<int> cand;
  rep(i, s.size()) {
    if (s[i] == '[') {
      if (cnt == 1)
        st = i;
      cnt++;
    }
    if (s[i] == ']') {
      cnt--;
      if (cnt == 1)
        cand.push_back(rec(s.substr(st, i - st + 1)));
    }
  }
  sort(cand.begin(), cand.end());
  int sum = 0;
  rep(i, (cand.size() + 1) / 2) { sum += cand[i]; }
  return sum;
}
int solve(string s) { return rec(s); }
int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;
    printf("%d\n", solve(s));
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3204
final_rank: 15

code_snippet: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)
int solve(string s) {
  s.erase(s.begin());
  s.erase(s.begin() + s.size() - 1);
  int cnt = 0;
  rep(i, s.size()) {
    if (s[i] == '[' || s[i] == ']')
      cnt++;
  }
  if (cnt == 0) {
    stringstream sst;
    sst << s;
    int ret;
    sst >> ret;
    return ret / 2 + 1;
  }
  vector<int> cand;
  int st = 0;
  int ct = 0;
  rep(i, s.size()) {
    if (s[i] == '[')
      ct++;
    if (s[i] == ']')
      ct--;
    if (ct == 0) {
      cand.push_back(solve(s.substr(st, i - st + 1)));
      st = i + 1;
    }
  }
  sort(cand.begin(), cand.end());
  int ans = 0;
  rep(i, cand.size() / 2 + 1) { ans += cand[i]; }
  return ans;
}
int main() {
  int n;
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;
    cout << solve(s) << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3236
final_rank: 16

code_snippet: #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
string str;
string::iterator it;
int s2i(string s) {
  stringstream ss(s);
  int x;
  ss >> x;
  return x / 2 + 1;
}
int calc() {
  int ans = 0;
  ++it;
  if (*it == '[') {
    vector<int> v;
    while (*it == '[') {
      v.push_back(calc());
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size() / 2 + 1; i++) {
      ans += v[i];
    }
  } else if (*it != ']') {
    string ss = "";
    while ('0' <= *it && *it <= '9') {
      ss += *it;
      ++it;
    }
    ans = s2i(ss);
  }
  ++it;
  return ans;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> str;
    it = str.begin();
    cout << calc() << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3128
final_rank: 1

code_snippet: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
int foo(string s) {
  s = s.substr(1, s.length() - 2);
  if (s[0] != '[') {
    stringstream ss(s);
    int x;
    ss >> x;
    return x / 2 + 1;
  }
  int y = 0, _i = 0;
  vector<int> v;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '[')
      y++;
    else if (s[i] == ']')
      y--;
    if (y == 0) {
      string sub = s.substr(_i, i - _i + 1);
      v.push_back(foo(sub));
      _i = i + 1;
    }
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < v.size() / 2 + 1; i++)
    sum += v[i];
  return sum;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    string s;
    cin >> s;
    cout << foo(s) << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3240
final_rank: 17

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int StoI(string s) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    ans += (s[i] - '0') * (int)pow(10, s.length() - 1 - i);
  }
  return ans;
}
long long int solve(string s) {
  bool flag = false;
  string a = "";
  int leftnum = 0;
  vector<int> result;
  for (int i = 1; i < s.length() - 1; i++) {
    a += s[i];
    if (s[i] == '[')
      leftnum++;
    else if (s[i] == ']')
      leftnum--;
    if (leftnum > 1)
      flag = true;
    if (leftnum == 0) {
      if (flag) {
        result.push_back(solve(a));
      } else {
        a = a.substr(1, a.length() - 2);
        result.push_back(StoI(a) / 2 + 1);
      }
      a = "";
      flag = false;
    }
  }
  sort(result.begin(), result.end());
  long long int ans = 0;
  for (int i = 0; i < result.size() / 2 + 1; i++) {
    ans += result[i];
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    cout << solve(s) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3388
final_rank: 7

code_snippet: #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#define all(u) (u).begin(), (u).end()
#define PB push_back
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef vector<int> VI;
int stoi(string num) {
  int ret;
  stringstream s;
  s << num;
  s >> ret;
  return ret;
}
string str;
int cur;
int rec() {
  cur++;
  if (str[cur] != '[' && str[cur] != ']') {
    string t;
    while (str[cur] != '[' && str[cur] != ']') {
      t += str[cur];
      cur++;
    }
    cur++;
    return stoi(t) / 2 + 1;
  }
  VI vote;
  while (str[cur] != ']') {
    vote.PB(rec());
  }
  sort(all(vote));
  int ret = 0;
  REP(i, vote.size() / 2 + 1) { ret += vote[i]; }
  cur++;
  return ret;
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> str;
    cur = 0;
    cout << rec() << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3196
final_rank: 14

code_snippet: #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef int number;
int c;
string str;
void debug(string str) {}
int minimum(vector<number> v, int n) {
  sort(v.begin(), v.end());
  int sum = 0;
  REP(i, n) { sum += v[i]; }
  return sum;
}
number A();
number Num();
number A() {
  debug("A:" + str.substr(c));
  if (str[c] == '[') {
    c++;
    int p;
    vector<number> children;
    while (p = c, (str[p] == '[' || isdigit(str[p]))) {
      number a = A();
      children.push_back(a);
    }
    c++;
    int len = children.size();
    return minimum(children, (1 + len) / 2);
  } else {
    number num = Num();
    return num;
  }
}
number Num() {
  debug("Num:" + str.substr(c));
  int val = 0;
  while (isdigit(str[c])) {
    val = val * 10 + (str[c] - '0');
    c++;
  }
  return (1 + val) / 2;
}
int main() {
  int n;
  cin >> n;
  REP(i, n) {
    cin >> str;
    c = 0;
    cout << A() << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3240
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
int solve(string s, int &i) {
  vector<int> v;
  if (isdigit(s[i])) {
    int num = 0;
    while (isdigit(s[i])) {
      num = num * 10 + s[i] - '0';
      i++;
    }
    return num + 1;
  }
  while (s[i] == '[') {
    i++;
    v.push_back(solve(s, i));
    i++;
  }
  sort(all(v));
  int n = v.size(), sum = 0;
  rep(k, (n + 1) / 2) { sum += v[k]; }
  return sum;
}
int main(void) {
  int T;
  cin >> T;
  rep(_, T) {
    string s;
    cin >> s;
    int index = 0;
    cout << (solve(s, index) + 1) / 2 << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3200
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repp(i, m, n) for(int i = (int)m; i < (int)n; i++)

#define ALL(x) x.begin(),x.end()

#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define keta(x) cout << std::fixed << std::setprecision(15) << (double)x << endl;

const int MAXN = 7368792;
int n;
string S;
int N;

using Graph = vector<vector<int>>;

//BFSを用いて始点sからの距離を求めるアルゴリズム
vector<int> BFS(const Graph &G, int s){

    vector<int> dist(N, -1);
    queue<int> todo;
    dist[s] = 0; //開始の頂点の距離を0にする．

    //キューに追加
    todo.push(s);

    while(!todo.empty()){
        int v = todo.front();
        todo.pop();
        for(auto next_v : G[v]){
            if(dist[next_v] != -1) continue;
            dist[next_v] = dist[v] + 1;
            todo.push(next_v);
        }
    }
    return dist;
}

int solve(){
    int cnt = 0; //左括弧の数(頂点数)
    rep(i, S.size()){
        if(S[i] == '[') cnt++;
    }
    Graph G(cnt);
    N = cnt;

    //入力から木を構築
    map<int, int> V;
    rep(i, cnt){
        V[i] = -1; //何も書かれていない
    }
    stack<int> st;
    string num_st = "";
    int v_now = 0;
    rep(i, S.size()){
        if(i == 0){
            st.push(v_now);
            v_now++;
        }
        else{
            //左括弧がでてきたらstのtopと今入れようとしている頂点
            //を辺で結ぶ
            if(S[i] == '['){
                G[st.top()].push_back(v_now);
                G[v_now].push_back(st.top());
                st.push(v_now);
                v_now++;
            }else if(S[i] == ']'){
                //もし，num_stに数値が描き込まれていたら
                //st.top()の頂点にその数字を書き込む
                if(num_st != ""){
                    V[st.top()] = stoi(num_st);
                    num_st = "";
                }
                st.pop();
            }else{
                num_st += S[i];
            }
        }
    }

    vector<int> depth = BFS(G, 0);
    //木の深さを調べる
    int max_depth = 0;
    rep(i, N) max_depth = max(depth[i], max_depth);

    //深さと頂点の関係
    //最下層には数字が書かれている．
    vector<vector<int>> dp_V(max_depth+1);
    rep(i, N){
        dp_V[depth[i]].push_back(i);
    }

    //最下層の一つ上から頂点に対して計算を行う
    for(int i = max_depth-1; i >= 0; i--){
        for(auto v : dp_V[i]){
            vector<int> tmp;
            int tmp_ans = 0;
            for(auto v_chi : G[v]){
                //自分より一つ下ならtmpに追加
                if(depth[v_chi] == i+1){
                   if(i+1 == max_depth) tmp.push_back(V[v_chi]/2+1);
                   else tmp.push_back(V[v_chi]);
                }
            }
            sort(ALL(tmp));
            rep(j, tmp.size()/2+1){
                tmp_ans += tmp[j];
            }
            //頂点の更新
            V[v] = tmp_ans;
        }
    }
    return V[0];
}

int main(){
        cin >> n;
        vector<int> ans;
        rep(i, n){
            cin >> S;
            ans.push_back(solve());
        }
        for(auto p : ans){
            cout << p << endl;
        }
}
accuracy: 1
time_ms: 10
memory_kb: 3892
final_rank: 12

code_snippet: #include <bits/stdc++.h>

const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl
#define dump(x)  cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(10)<<youso_<<",";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,V,greater<long long>>


typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;


#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
  cin>>head;
  CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
  x %= MOD;
  x += MOD;
  x %= MOD;
}

ll GCD(ll a, ll b) {
  if(b == 0) return a;
  else return GCD(b, a%b);
}

struct COMB{
  vl fact, fact_inv, inv;
  void init_nCk(long long SIZE) {
    fact.resize(SIZE + 5);
    fact_inv.resize(SIZE + 5);
    inv.resize(SIZE + 5);
    fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
    for(long long i = 2; i < SIZE + 5; i++) {
      fact.at(i) = fact.at(i - 1)*i%MOD;
      inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
      fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
    }
  }
  long long nCk (long long n, long long k) {
    assert(!(n < k));
    assert(!(n < 0 || k < 0));
    return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
  }
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
  if(b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = extGCD(b, a%b, y, x);
  y -= a/b*x;
  return d;
}

vector<string> sol(string S) {
  vector<string> res;
  S = S.substr(1, S.size() - 2);
  int count = 0;
  int s = 0;
  for(int i = 0; i < S.size(); i++) {
    if(S.at(i) == '[') {
      count++;
      if(count == 1) {
        s = i;
      }
    }
    if(S.at(i) == ']') {
      count--;
      if(count == 0) {
        res.push_back(S.substr(s, i - s + 1));
      }
    }
  }
  if(res.size() == 0) res.push_back(S);
  return res;
}


int solve(string S) {
  int sum = 0;

  vector<string> sl = sol(S);
  // VECCOUT(sl);

  // 処理終了
  if(sl.size() == 1) {
    istringstream s(sl.at(0));
    int ret = 0;
    s >> ret;
    return (ret + 1)/2;
  }
  vl res(sl.size(), 0);
  for(int i = 0; i < sl.size(); i++) {
    // 一段階バラバラにした文字列を撮るのに必要な票数
    res.at(i) = solve(sl.at(i));
  }
  sort(res.begin(),res.end());
  for(int i = 0; i < (res.size() + 1)/2; i++) {
    sum += res.at(i);
  }
  return sum;
}

void Main() {
  LCIN(N);
  for(int i = 0; i < N; i++) {
    SCIN(S);
    cout << solve(S) << endl;
  }
}

int main() {
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3376
final_rank: 6

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

int main() {
  
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  for (int i=0;i<n;i++) {
    string s;
    cin >> s;
    int ns = s.size();
    stack<string> st;
    int tmp = 0;
    for (int j=0;j<ns;j++) {
      if (s[j] == '[') st.push("[");
      else if (s[j] == ']') {
        vector<string> numss;
        st.push(to_string(tmp));
        tmp = 0;
        while (st.top() != "[") {
          numss.emplace_back(st.top());
          st.pop();
        }
        st.pop();
        vector<int> nums;
        int nt = numss.size();
        for (int k=0;k<nt;k++) {
          nums.emplace_back(stoi(numss[k]));
        }
        sort(nums.begin(),nums.end());
        int ned = 0;
        for (int k=0;k<=nt/2;k++) {
          if (nt == 1) ned += (nums[k]+1)/2;
          else ned += nums[k];
        }
        st.push(to_string(ned));
      }
      else {
        tmp *= 10;
        tmp += s[j]-'0';
      }
      /*stack<string> kst;
      while (!st.empty()) {
        kst.push(st.top());
        st.pop();
      }
      while (!kst.empty()) {
        cout << kst.top() << ' ';
        st.push(kst.top());
        kst.pop();
      }
      cout << endl;*/
    }
    cout << st.top() << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3556
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vpll = vector<pair<ll, ll>>;
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1<<30;
const ll INF = 1ll<<60;
//const ll mod = 1000000007;
const ll mod = 998244353;
//const ll dx[4] = {1, 0, -1, 0};
//const ll dy[4] = {0, 1, 0, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

template<class T> void disp1(vector<T> &array, bool blank=1) {
    rep(i, array.size()) cout << ((i&&blank) ? " " : "") << array[i];
    cout << endl;
    //cout << " ";
}
template<class T> void disp2(vector<vector<T>> &array, bool blank=1) {
    rep(i, array.size()) disp1(array[i], blank);
}

ll rem(ll a, ll b) { return (a % b + b) % b; }      // 余りを0~b-1に丸める
// 小数の入力が文字で与えられたとき、10^n倍したものを返す
ll x10(string str_f, ll n) {
    if (str_f[0] == '-') return -x10(str_f.substr(1), n);
    ll times = 1; rep(i,n) times *= 10;     // times = 10^n
    int id = -1; rep(i,str_f.size()) if (str_f[i] == '.') id = i;
    if (id == -1) { return stoll(str_f)*times; }
    string str_int = str_f.substr(0, id), str_dec = str_f.substr(id+1);
    while (str_dec.size() < n) str_dec += '0';
    return stoll(str_int)*times + stoll(str_dec);
}
// A^N (mod)
ll modpow(ll A, ll N, ll mod) {
    ll RES = 1;
    while (N) {
        if (N & 1) RES = RES * A % mod;
        A = A * A % mod;
        N >>= 1;
    }
    return RES;
}

//using mint = modint998244353;

ll dfs(string s) {
    // [ABC]みたいなやつがくるので、AとBとCに分解する
    vector<string> vs;
    int n = sz(s);
    // [A]みたいなやつが来た時の対処
    bool leaf = 1;
    for (int i = 1; i < n-1; i++) {
        if ((s[i] == '[') || (s[i] == ']')) leaf = 0;
    }
    //cout << leaf << endl;
    if (leaf) {
        string t = s.substr(1, n-2);
        //cout << t << endl;
        ll ret = stoll(t);
        return (ret+1)/2;
    }
    // [ABC]みたいなやつの処理
    int c = 0, id = 1;
    for (int i = 1; i < n-1; i++) {
        if (s[i] == '[') c++;
        if (s[i] == ']') c--;
        if (c == 0) {
            vs.pb(s.substr(id, i+1-id));
            id = i+1;
        }
    }
    vll ave;
    /*
    for (string si : vs) {
        puts("vs");
        cout << si << endl;
    }
    */
    for (string si : vs) {
        ave.pb(dfs(si));
    }
    sort(all(ave));
    ll ret = 0;
    rep(i,sz(ave)/2+1) ret += ave[i];
    return ret;
}

int main() {
    ll n; cin >> n;
    rep(i,n) {
        string s; cin >> s;
        cout << dfs(s) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3312
final_rank: 5

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

int process()
{
    string str;
    cin >> str;
    array<vector<int>, 10000> vec = {};
    array<int,10000> level_size = {};
    string start = "[";
    string end = "]";
    int level = 0;
    int current = 0;
    for(int i=0;i<str.length();i++){
        if(str.compare(i,1,start)==0){
            level ++;
        }else if(str.compare(i,1,end)==0){
            if(current == 0){
                sort(vec[level].begin(),vec[level].end());
                int sum = 0;
                for(int i=0;i<(level_size[level]+1)/2;i++){
                    sum += vec[level][i];
                }
                for(int i=0;i<vec[level].size();i++){
                    vec[level][i] = 1e9;
                } 
                level_size[level] = 0;
                level --;
                level_size[level] ++;
                vec[level].push_back(sum);
            }else{
                level --;
                vec[level].push_back((current+1)/2);
                current = 0;
                level_size[level] ++;
            }
        }else{
            current *= 10;
            current +=  stoi(str.substr(i,1));
        }
    }
    return vec[0][0];
}

int main()
{
    int num;
    cin >> num;
    vector<int> ans;
    for(int i=0;i<num;i++){
        ans.push_back(process());
    }
    for(int i=0;i<num;i++){
        cout << ans[i] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4028
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define reps(i,s,n) for(int i=s;i<(int)n;++i)

using namespace std;
using ll = long long;
constexpr ll LINF = 1LL<<60;

template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;

bool solve(){
    string s;
    cin>>s;
    V<ll> d;
    rep(i,s.size()){
        if(s[i]=='[')d.push_back(-1);
        else if(s[i]==']'){
            if(d.size()!=0 && d.back()>=0){
                d.back()=ceil((double)d.back()/2);
            }
            d.push_back(-2);
        }else {
            if(d.size()==0 || d.back()<0){
                d.push_back(s[i]-'0');
            }else{
                d.back()=d.back()*10+s[i]-'0';
            }
        }
    }
    rep(i,d.size()){
        if(d[i]!=-2)continue;
        V<ll> tmp;
        ll r=i;
        for(i--;d[i]>=0;i--){
            tmp.push_back(d[i]);
        }
        sort(tmp.begin(),tmp.end());
        ll ans=0;
        rep(j,ceil((double)tmp.size()/2)){
            ans+=tmp[j];
        }
        d[i]=ans;
        d.erase(d.begin()+i+1,d.begin()+r+1);
        
        /*rep(j,d.size()){
            if(d[j]>=0)cout<<d[j]<<" ";
            else cout<<"|";
        }
        cout<<endl;*/
    }
    cout<<d[0]<<endl;
    return true;
}

int main(){
    ll n;
    cin>>n;
    rep(i,n)solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3596
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n,cnt,tot,q,Q;
string s;
vector<int> v;
stack<int> sta[10000];
int main(void){
    cin>>Q;
    while(Q--){
    for(int i=0;i<10000;i++)sta[i]={};
    v={};
    cnt=tot=0;
    cin>>s;
    for(int i=0;i<s.size();i++){
        //cout<<s[i]<<" "<<cnt<<endl;
        if(s[i]=='[')cnt++;
        else if(s[i]==']'){
            if(tot!=0){
                cnt--;
                //cout<<cnt<<" "<<tot<<endl;
                sta[cnt].push((tot+1)/2);
                tot=0;
            }
            else{
                v={};
                while(sta[cnt].size()){
                    q=sta[cnt].top();
                    sta[cnt].pop();
                    v.push_back(q);
                    //cout<<q<<endl;
                }
                sort(v.begin(),v.end());
                q=0;
                for(int j=0;j<=v.size()/2;j++)q+=v[j];
                cnt--;
                sta[cnt].push(q);
            }
        }
        else{
            tot*=10;
            tot+=(s[i]-'0');
        }
    }
    q=sta[0].top();
    cout<<q<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 10224
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};


int main() {
    ll N;
    cin >>N;
    rep(i,N){
        string S;
        cin>>S;
        ll M=S.size();
        ll K=0;
        rep(j,M){
            if(S[j]=='['){
                K++;
            }else{
                break;
            }
        }
        ll mode=0;
        vector<ll>cnt(K);
        vector<vector<ll>>sq(K);
        vector<ll>num;
        ll ct=0;
        ll dl=0;
        queue<ll>que;
        rep(j,M){
            //cout<<"OK"<<endl;
            if(S[j]==']'){
                ct++;
                if(j==M-1){
                    if(ct>1){
                        rep(k,ct-1){
                            sq[k].pb(cnt[k]+1);
                        }
                    }
                }
                if(mode==0){
                    //cout<<"OK"<<endl;
                    num.pb(stoi(S.substr(j-dl,dl)));
                    que.push((stoi(S.substr(j-dl,dl))+1)/2);
                    dl=0;
                }
                
                mode=1;
            }else{
                if(S[j]=='['){
                    if(ct==1){
                        cnt[0]++;
                    }else if(ct>1){
                        rep(k,ct-1){
                            sq[k].pb(cnt[k]+1);
                        }
                        
                        
                        rep(k,ct-1){
                            cnt[k]=0;
                        }
                        cnt[ct-1]++;
                    }
                    ct=0;
                }else{
                    dl++;
                    mode=0;
                }
            }
        }
        /*rep(j,K){
            rep(k,sq[j].size()){
                cout<<sq[j][k]<<" ";
            }
            cout<<endl;
        }*/

        /*rep(j,num.size()){
            cout<<num[j]<<endl;
        }*/
        vector<ll>ele(K);
        rep(j,K){
            ll now=0;
            rep(k,sq[j].size()){
                vector<ll>tmp;
                rep(m,sq[j][k]){
                    tmp.pb(que.front());
                    que.pop();
                }
                sort(all(tmp));
                ll sum=0;
                rep(m,(sq[j][k]+1)/2){
                    sum+=tmp[m];
                }
                que.push(sum);
            }
            
        }
        cout<<que.front()<<endl;


    }

    
    


    return 0;
    

}
accuracy: 1
time_ms: 10
memory_kb: 3292
final_rank: 4