Problem:
Problem E:
The Genome Database of All Space Life
In 2300, the Life Science Division of Federal Republic of Space starts
a very ambitious project to complete the genome sequencing of all
living creatures in the entire universe and develop the genomic
database of all space life.
Thanks to scientific research over many years, it has been known that
the genome of any species consists of at most 26 kinds of
molecules, denoted by English capital letters (
i.e.
A
to
Z
).
What will be stored into the database are plain strings consisting of
English capital letters.
In general, however, the genome sequences of space life include
frequent repetitions and can be awfully long.
So, for efficient utilization of storage, we compress
N
-times
repetitions of a letter sequence
seq
into
N
(
seq
)
, where
N
is a natural 
number greater than or equal to two and the length of
seq
is at
least one.
When
seq
consists of just one letter
c
, we may omit parentheses and write
Nc
.
For example, a fragment of a genome sequence:
ABABABABXYXYXYABABABABXYXYXYCCCCCCCCCC
can be compressed into:
4(AB)XYXYXYABABABABXYXYXYCCCCCCCCCC
by replacing the first occurrence of
ABABABAB
with its compressed form.
Similarly, by replacing the following repetitions of
XY
,
AB
, and
C
, we get:
4(AB)3(XY)4(AB)3(XY)10C
Since
C
is a single letter, parentheses are omitted in this
compressed representation.
Finally, we have:
2(4(AB)3(XY))10C
by compressing the repetitions of
4(AB)3(XY)
.
As you may notice from this example, parentheses can be nested.
Your mission is to write a program that uncompress compressed genome
sequences.
Input
The input consists of multiple lines, each of which contains a
character string
s
and an integer
i
separated by a
single space.
The character string
s
, in the aforementioned manner,
represents a genome sequence.
You may assume that the length of
s
is between 1 and 100,
inclusive.
However, of course, the genome sequence represented by
s
may be
much, much, and much longer than 100.
You may also assume that each natural number in
s
representing the
number of repetitions is at most 1,000.
The integer
i
is at least zero and at most one million.
A line containing two zeros separated by a space follows the last input line and indicates
the end of the input.
Output
For each input line, your program should print a line containing the
i
-th letter in the genome sequence that
s
represents.
If the genome sequence is too short to have the
i
-th element,
it should just print a zero.
No other characters should be printed in the output lines.

Note that in this problem the index number begins from zero rather
than one and therefore the initial letter of a sequence is its zeroth element.
Sample Input
ABC 3
ABC 0
2(4(AB)3(XY))10C 30
1000(1000(1000(1000(1000(1000(NM)))))) 999999
0 0
Output for the Sample Input
0
A
C
M

Submitted solutions:

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

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define int long long
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define all(x) x.begin(), x.end()
using pii = pair<int, int>;
constexpr int MOD = 1e9 + 7;
typedef string::const_iterator State;
int MAX = 1000001;
int R;
int number(State &begin);
string factor(State &begin);
bool fin = false;
void consume(State &begin, char expected) {
    if (fin) begin++;
    if (*begin == expected)
        begin++;
    else {
        begin++;
        // cerr << "expected " << expected << " but got " << *begin << endl;
        // cerr << "rest string is ";
        // while (*begin) {
        //     cerr << *begin++;
        // }
        // cerr << endl;
    }
}
string expression(State &begin) {
    if (fin) return "";
    string ret = factor(begin);
    for (;;) {
        if (ret.size() > R) {
            fin = true;
            return ret;
        }
        if (isdigit(*begin))
            ret += expression(begin);
        else if (isupper(*begin))
            ret += expression(begin);
        else if (*begin == '(')
            ret += expression(begin);
        else
            break;
    }
    return ret;
}
string factor(State &begin) {
    string ret;
    if (fin) return ret;
    int cnt = 1;
    if (*begin == '(') {
        consume(begin, '(');
        ret = expression(begin);
        consume(begin, ')');
        return ret;
    } else if (isdigit(*begin)) {
        cnt = number(begin);
        // begin++;
        // cerr << cnt << endl;
        string res = factor(begin);
        // begin++;
        REP(_, cnt) {
            ret += res;
            if (ret.size() > R) {
                fin = true;
                return ret;
            }
        }
        // ret += factor(begin);
        // cerr << ret << endl;
        return ret;
    } else if (isupper(*begin)) {
        while (isupper(*begin)) {
            ret += *begin;
            begin++;
        }
        // ret += factor(begin);
        return ret;
    } else
        return ret;
}
int number(State &begin) {
    int ret = 0;
    if (fin) return ret;
    while (isdigit(*begin)) {
        ret *= 10;
        ret += *begin - '0';
        begin++;
    }
    // cerr << ret << endl;
    return ret;
}

bool solve() {
    string S;
    fin = false;
    int r;
    cin >> S;
    if (S == "0") return false;
    cin >> r;
    R = r;
    // cerr << S << endl;
    State begin = S.begin();
    string res = expression(begin);
    // cerr << res << endl;
    if (r >= res.size())
        cout << '0' << endl;
    else
        cout << res[r] << endl;
    return true;
}

signed main() {
    while (solve())
        ;
}
accuracy: 1
time_ms: 10
memory_kb: 8644
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
ll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}
ll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
  
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;

typedef string::const_iterator State;


void solve(string s,ll n){
  State begin = s.begin();
  stack<pll> st;
  string ret = "";
  while(begin != s.end()){
    if(sz(ret) > n)break;
    if(isdigit(*begin)){
      ll times = 0;
      while(begin != s.end() && isdigit(*begin)){
        times *=10;
        times += *begin - '0';
        begin++;
      }
      st.push({times,sz(ret)});
      continue;
    }
    if(*begin == '('){
      st.push({-1,-1});
      begin++;
    }else if(isupper(*begin)){
      while(begin != s.end() && isupper(*begin)){
        ret += *begin;
        begin++;
      }
      while(!st.empty() && st.top().first != -1){
        //数なので更新
        auto [times,sid] = st.top() ;
        st.pop();
        times--;
        ll eid = sz(ret);
        rep(z,times){
          if(sz(ret) > n)break;
          reps(i,sid,eid){
            ret.push_back(ret[i]);
            if(sz(ret) > n)break;
          }
        }
      }

    }else if(*begin == ')'){
      assert(!st.empty() &&  st.top().first == -1 );
      st.pop();
      while(!st.empty() && st.top().first != -1){
        //数なので更新
        auto [times,sid] = st.top() ;
        st.pop();
        times--;
        ll eid = sz(ret);
        rep(z,times){
          if(sz(ret) > n)break;
          reps(i,sid,eid){
            ret.push_back(ret[i]);
            if(sz(ret) > n)break;
          }
        }
      }
      begin++;
    }
  }
  if(sz(ret) > n){
    cout << ret[n] << endl;
  }else{
    cout << 0 << endl;
  }
}

int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  while(true){
    Str(s);
    LL(n);
    if(s == "0" && n == 0){
      break;
    }
    solve(s,n);
  }
}
accuracy: 1
time_ms: 30
memory_kb: 6252
final_rank: 9

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var) do { cout << #var << " :\n"; view(var); } while(0)
template<typename T>void view(const T& e) {cout << e;}
template<typename T1, typename T2>void view(const pair<T1, T2>& p) {cout << "{" << p.first << ", " << p.second << "}";}
template<typename T>void view(const vc<T>& v) {for (const auto& e : v) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const vv<T>& vv) {for (const auto& v : vv) {view(v);cout << " ";} cout << endl;}
template<typename T>void view(const set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T>void view(const unordered_set<T>& s) {for (const auto& e : s) {view(e);cout << " ";} cout << endl;}
template<typename T1, typename T2>void view(const map<T1, T2>& mp){for (const auto& e : mp) {view(e);cout << " ";} cout << endl;}

string S;

string f(int l, int r){
    bool ok = true;
    srep(i, l, r) if (isdigit(S[i])){
        ok = false;
        break;
    }
    if (ok) return S.substr(l, r - l);
    vc<pair<int, int>> block;
    int c = 0;
    srep(i, l, r){
        if (S[i] == '(') c++;
        if (len(block) == 0 || (!isdigit(S[i - 1]) && isdigit(S[i]) && c == 0) || (S[i - 1] == ')' && c == 0)){
            block.push_back({i, i + 1});
        }else{
            block.back().second++;
        }
        if (S[i] == ')') c--;
    }
    string ret = "";
    for (auto [nl, nr] : block){
        string unit = "";
        int num = 1;
        if (isdigit(S[nl])){
            num = 0;
            while (isdigit(S[nl])){
                num = num * 10 + (S[nl] - '0');
                nl++;
            }
        }
        if (S[nl] == '(') unit = f(nl + 1, nr - 1);
        else unit = f(nl, nr);
        rep(i, num){
            ret += unit;
            if (len(ret) > 1e6) return ret;
        }
    }
    return ret;
}

int main(){
    while (true){
        int k; cin >> S >> k;
        if (S == "0") break;
        string genom = f(0, len(S));
        if (k >= len(genom)) cout << 0 << endl;
        else cout << genom[k] << endl;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 7940
final_rank: 12

code_snippet: //#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for (ll i=0;i<(ll)n;i++)
#define rrep(i,n) for (ll i=n-1;i>=(ll)0;i--)
#define loop(i,m,n) for(ll i=m;i<=(ll)n;i++)
#define rloop(i,m,n) for(ll i=m;i>=(ll)n;i--)
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<" ";}cout<<endl;
#define vpdbg(a) rep(ii,a.size()){cout<<"{"<<a[ii].first<<","<<a[ii].second<<"} ";}cout<<endl;
#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<" ";}cout<<endl;}
#define setdbg(a) for(const auto & ii:a){cout<<ii<<" ";}cout<<endl;
#define inf 1e9
#define mod 998244353LL
//#define mod 1000000007LL
#define eps 0.000000001
random_device rnd;// 非決定的な乱数生成器
mt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード

//#include<boost/multiprecision/cpp_int.hpp>
//#define bbi boost::multiprecision::cpp_int
//#include<atcoder/lazysegtree>


//整数同士の累乗の計算をする。
ll power(ll A, ll B) {
	ll result = 1;
	for (ll i=0;i<B;i++){
		result *= A;
	}
	return result;
}

// nのk乗をmodで割った余りを計算
ll power_mod(ll n, ll k){
	long long result = 1;
	while (k > 0){
		if ((k&1) ==1)result=(result*n)%mod;
		n=n*n%mod;
		k >>= 1;
	}
	return result;
}


//受け取った2次元文字の外側に、文字pをコーティングする。
vector<string> pad(vector<string> &s,char p){
	ll h=s.size();
	ll w=s[0].size();
	vector<string> res(h+2,string(w+2,p));
	rep(i,h)rep(j,w)res[i+1][j+1]=s[i][j];
	return res;
}

// Union-Find
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]);
	}
	// x と y が同じグループに属するかどうか (根が一致するかどうか)
	bool issame(int x, int y) {
		return root(x) == root(y);
	}
	// x を含むグループと 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;
	}
	// x を含むグループのサイズ
	int size(int x) {
		return siz[root(x)];
	}
};


//グリッド問題等用
vl dx={1,0,-1,0};
vl dy={0,1,0,-1};

string s;
ll k;
char ans='0';

//
ll dfs(ll i){
	if(k==0)return 0;
	while(i!=s.size()&&s[i]!=')'){
		if('A'<=s[i]&&s[i]<='Z'){
			k--;
			if(k==0){
				ans=s[i];
				return 0;
			}
		}else if('0'<=s[i]&&s[i]<='9'){
			ll num=0;
			while('0'<=s[i]&&s[i]<='9'){
				num*=10;
				num+=s[i]-'0';
				i++;
			}
			if(s[i]=='('){
				ll tmp;
				rep(j,num)tmp=dfs(i+1);
				i=tmp;
			}else{
				rep(j,num){
					k--;
					if(k==0){
						ans=s[i];
						return 0;
					}
				}
			}
			if(k==0)return 0;
		}
		i++;
	}
	return i;
}

ll solve(){
	cin>>s>>k;
	if(s=="0"){return 1;}
	k++;
	ans='0';
	dfs(0);

	cout<<ans<<endl;
	return 0;
}

//メイン
int main(){
	while(solve()==0);
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3336
final_rank: 5

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

int solve() {
    string s;
    int k;
    cin >> s >> k;
    if (s == "0") return 1;
    string ans;

    auto add = [&](char c) -> bool {
        ans.push_back(c);
        if (ans.size() > k) return true;
        return false;
    };

    using Iter = string::const_iterator&;

    auto num = [&](Iter it) {
        int v = 0;
        while (isdigit(*it)) {
            v = 10 * v + (*it - '0');
            it++;
        }
        return v;
    };

    auto dfs = [&](auto self, Iter it) -> pair<bool, string> {
        string cur;
        while (it != s.end() && *it != ')') {
            if (isdigit(*it)) {
                int rp = num(it);
                if (isalpha(*it)) {
                    char c = *it;
                    it++;
                    for (int i = 0; i < rp; i++) {
                        if (add(c)) {
                            return {true, ""};
                        }
                        cur.push_back(c);
                    }
                } else {
                    assert(*it == '(');
                    it++;
                    auto [f, t] = self(self, it);
                    if (f) return {f, t};
                    assert(*it == ')');
                    it++;
                    for (auto c: t) {
                        cur.push_back(c);
                    }
                    for (int i = 1; i < rp; i++) {
                        for (auto c: t) {
                            if (add(c)) {
                                return {true, ""};
                            }
                            cur.push_back(c);
                        }
                    }
                }
            } else {
                char c = *it;
                it++;
                if (add(c)) return {true, ""};
                cur.push_back(c);
            }
        }
        return {false, cur};
    };
    auto it = s.cbegin();
    dfs(dfs, it);
    if (k < ans.size()) cout << ans[k] << '\n';
    else cout << 0 << '\n';
    return 0;
}
int main(){
    while (!solve());
}
accuracy: 1
time_ms: 140
memory_kb: 8380
final_rank: 17

code_snippet: #include<bits/stdc++.h>
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#ifdef LOCAL
#include <debug.hpp>
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (static_cast<void>(0))
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll,ll>;
using pii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vul = vector<ull>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vs = vector<string>;
template<class T> using pq = priority_queue<T,vector<T>, greater<T>>;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(a,b,c,name,...) name
#define rep1(n) for (ll UNUSED_NUMBER = 0; UNUSED_NUMBER < (n); ++UNUSED_NUMBER)
#define rep2(i, n) for (ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i = (n) - 1;i >= 0;i--)
#define rrep2(i,n) for(ll i = (n) - 1;i >= 0;i--)
#define rrep3(i,a,b) for(ll i = (b) - 1;i >= (a);i--)
#define rrep4(i,a,b,c) for(ll i = (a) + ((b)-(a)-1) / (c) * (c);i >= (a);i -= c)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define all1(i) begin(i) , end(i)
#define all2(i,a) begin(i) , begin(i) + a
#define all3(i,a,b) begin(i) + a , begin(i) + b
#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__),0LL)
template<class T> bool chmin(T &a, const T &b){ if(a > b){ a = b; return 1; } else return 0; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } else return 0; }
template<class T> auto min(const T& a){return *min_element(all(a));}
template<class T> auto max(const T& a){return *max_element(all(a));}
template<class... Ts> void in(Ts&... t);
#define INT(...) int __VA_ARGS__; in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__; in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__; in(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; in(__VA_ARGS__)
#define VEC(type, name, size) vector<type> name(size); in(name)
#define VV(type, name, h, w) vector<vector<type>> name(h, vector<type>(w)); in(name)
ll intpow(ll a, ll b){ ll ans = 1; while(b){if(b & 1) ans *= a; a *= a; b /= 2;} return ans;}
ll modpow(ll a, ll b, ll p){ ll ans = 1; a %= p;if(a < 0) a += p;while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
bool is_clamp(ll val,ll low,ll high) {return low <= val && val < high;}
void Yes() {cout << "Yes\n";return;}
void No() {cout << "No\n";return;}
void YES() {cout << "YES\n";return;}
void NO() {cout << "NO\n";return;}
template <typename T>
T floor(T a, T b) {return a / b - (a % b && (a ^ b) < 0);}
template <typename T>
T ceil(T x, T y) {return floor(x + y - 1, y);}
template <typename T>
T bmod(T x, T y) {return x - y * floor(x, y);}
template <typename T>
pair<T, T> divmod(T x, T y) {T q = floor(x, y);return {q, x - q * y};}
namespace IO{
#define VOID(a) decltype(void(a))
struct setting{ setting(){cin.tie(nullptr); ios::sync_with_stdio(false);fixed(cout); cout.precision(12);}} setting;
template<int I> struct P : P<I-1>{};
template<> struct P<0>{};
template<class T> void i(T& t){ i(t, P<3>{}); }
void i(vector<bool>::reference t, P<3>){ int a; i(a); t = a; }
template<class T> auto i(T& t, P<2>) -> VOID(cin >> t){ cin >> t; }
template<class T> auto i(T& t, P<1>) -> VOID(begin(t)){ for(auto&& x : t) i(x); }
template<class T, size_t... idx> void ituple(T& t, index_sequence<idx...>){in(get<idx>(t)...);}
template<class T> auto i(T& t, P<0>) -> VOID(tuple_size<T>{}){ituple(t, make_index_sequence<tuple_size<T>::value>{});} 
#undef VOID
}
#define unpack(a) (void)initializer_list<int>{(a, 0)...}
template<class... Ts> void in(Ts&... t){ unpack(IO :: i(t)); }
#undef unpack
constexpr long double PI = 3.141592653589793238462643383279L;
template <class F> struct REC {
    F f;
    REC(F &&f_) : f(forward<F>(f_)) {}
    template <class... Args> auto operator()(Args &&...args) const { return f(*this, forward<Args>(args)...); }};

//constexpr int mod = 998244353;
constexpr int mod = 1000000007;
template <typename T> 
struct Edge {
	int from, to;
	T cost;
	Edge() = default;
	Edge(int _to, T _cost) : from(-1), to(_to), cost(_cost) {}
	Edge(int _from, int _to, T _cost) : from(_from), to(_to), cost(_cost) {}
	bool operator < (const Edge &a) const { return cost < a.cost; }
	bool operator > (const Edge &a) const { return cost > a.cost; }
    Edge &operator = (const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
    friend ostream operator<<(ostream &os, Edge &edge) { return os << edge.to; }
};
template <typename T>
using Edges = vector<Edge<T>>;
template <typename T>
using Wgraph = vector<Edges<T>>;
using Ugraph = vector<vector<int>>;
Ugraph uinput(int N, int M = -1, bool is_directed = false, int origin = 1) {
    Ugraph g(N);
    if (M == -1) M = N - 1;
    while(M--) {
        int a,b;
        cin >> a >> b;
        a -= origin, b -= origin;
        g[a].push_back(b);
        if(!is_directed) g[b].push_back(a);
    }
    return g;
}
template <typename T>
Wgraph<T> winput(int N, int M = -1, bool is_directed = false,int origin = 1) {
    Wgraph<T> g(N);
    if (M == -1) M = N - 1;
    while(M--) {
        int a,b;
        T c;
        cin >> a >> b >> c;
        a -= origin, b -= origin;
        g[a].emplace_back(b,c);
        if(!is_directed) g[b].emplace_back(a,c);
    }
    return g;
}
int K;
string term(string &s,int &id);
string expression(string &s,int &id);
string factor(string &s,int &id);
int number(string &s,int &id) {
    int ret = 0;
    while(isdigit(s[id])) {
        ret *= 10;
        ret += s[id++] - '0';
    }
    return ret;
}
string factor(string &s,int &id) {
	int co = 1;
    if(isdigit(s[id])) co = number(s,id);
	//debug(co);
    if(s[id] == '(') {
        id++;
        string ret = expression(s,id);
        assert(s[id] == ')');
        id++;
		string ret2;
		rep(i,co) {
			rep(j,ret.size()) {
				ret2 += ret[j];
				if(ret2.size() == K) break; 
			}
			if(ret2.size() == K) break;
		}
        return ret2;
    }
	string ret;
	rep(i,co) {
		ret += s[id];
		if(ret.size() == K) break;
	}
	id++;
	return ret;
}
// string term(string &s,int &id) {
//     int ret = factor(s,id);
//     while(1) {
//         if(s[id] == '*') {
//             id++;
//             ret *= factor(s,id);
//         }
//         else if(s[id] == '/') {
//             id++;
//             ret /= factor(s,id);
//         }
//         else break;
//     }
//     return ret;
// }
string expression(string &s,int &id) {
    string ret = factor(s,id);
    while(id < s.size()) {
        if(s[id] == ')') {
			break;
        }
        else {
			auto ret2 = factor(s,id);
			//debug(ret2);
			if(ret.size() < K) {
				int c = K - (int)ret.size();
				rep(i,min(c,(int)ret2.size())) {
					ret += ret2[i];
				}
			}
		}
    }
    return ret;
}

int main() {
	while(1) {
		STR(s);
		cin >> K;
		K++;
		if(s == "0") break;
		int id = 0;
		string X = expression(s,id);
		debug(X);
		if(X.size() < K) cout << 0 << '\n';
		else cout << X[K-1] << '\n';
	}
}
accuracy: 1
time_ms: 320
memory_kb: 7780
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;

int M;

class Result {
  public:
    string s;

    Result() : s("") {}

    Result &operator+=(const Result &rhs) {
        rep(i, rhs.s.size()) {
            if (s.size() <= M) s += rhs.s[i];
            else break;
        }
        return *this;
    }
};

vector<string> OPS = {"+"};
Result parse(const string &s, size_t &i, size_t p = 0) {
    if (p == OPS.size()) {
        int coef = 0;
        if (isdigit(s[i])) {
            while (isdigit(s[i])) {
                coef *= 10;
                coef += s[i++] - '0';
            }
        }
        if (coef == 0) coef = 1;
        if (s[i] == '(') {
            Result res = parse(s, ++i, 0);
            assert(s[i] == ')');
            ++i;

            string s = res.s;
            rep(i, coef - 1) {
                if (res.s.size() <= M) res.s += s;
                else break;
            }
            return res;
        }
        if (isupper(s[i])) {
            Result res;
            while (isupper(s[i])) {
                res.s += s[i++];
            }

            string s = res.s;
            rep(i, coef - 1) {
                if (res.s.size() <= M) res.s += s;
                else break;
            }
            return res;
        }
        assert(false);
    }

    Result res = parse(s, i, p + 1);
    while (i < s.length()) {
        char op = s[i];
        if (OPS[p].find(op) == string::npos) break;
        Result rhs = parse(s, ++i, p + 1);
        if (op == '+') res += rhs;
    }
    return res;
}

void solve(string S, int idx) {
    M = idx;

    string T;
    for (char c : S) {
        if (!T.empty()) {
            if (T.back() == ')' && (isdigit(c) || isupper(c))) {
                T += "+";
            }
            if (isupper(T.back()) && isdigit(c)) {
                T += "+";
            }
        }
        T += c;
    }

    size_t i = 0;
    Result res = parse(T, i);

    if (res.s.size() > idx) cout << res.s[idx];
    else cout << 0;
    cout << endl;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    string S;
    int idx;
    while (cin >> S >> idx) {
        if (S == "0" && idx == 0) break;
        solve(S, idx);
    }

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7636
final_rank: 11

code_snippet: // 11:28
#include<bits/stdc++.h>
using namespace std;
using state = string::iterator;
int K;
string S;
string T;
state it0;
bool done = 0;

string expr(state&);

int num(state& it){
    int res = 0;
    while('0' <= *it && *it <= '9'){
        res *= 10;
        res += int(*it - '0');
        // cerr << *it;
        it++;
    }
    return res;
}

string alps(state& it){
    string res = "";
    //cerr << *it;
    if(*it == '('){
        it++;
        res = expr(it);
        if(done) return "";
        if(*it != ')'){
            cerr << "( \'" << *it << "\' ) found (at " << it - it0 << ") in alps0\n";
            cerr << S << endl;
            exit(1);
        }
        //else cerr << "?";
        it++;
    }
    else if('A' <= *it && *it <= 'Z'){
        T += *it;
        //cout << *it;
        it++;
    }
    else{
        //cerr << "( \'" << *it << "\' ) found (at " << it - it0 << ") in alps\n";
        //cerr << S << endl;
        exit(1);
    }
    if(K < (int)T.size()){
        done = 1; return "";
    }
    return res;
}

// <expr>
string expr(state& it){
    static int depth = 0;
    depth++;
    // cout << depth << " ";
    //cerr << "!";

    string res = "";
    int resI = 0;

    if(*it == '\0' || *it == ')'){
        //cerr << "?";
        depth--; return "";
    }
    //cerr << *it;
    int n = 1;
    //cerr << "### " << *it << " ###";
    if('0' <= *it && *it <= '9'){
        n = num(it);
        if(done){
            depth--; return "";
        }
    }
    if(done){
        depth--; return "";
    }
    while(n--){
        state it2 = it;
        alps(it2);
        if(K < (int)(res.size() + T.size())){
            T += res;
            done = 1;
            depth--; return "";
        }
        if(n == 0) it = it2;
    }
    if(done) return "";

    if(*it != ')' && *it != '\0'){
        depth--;
        res += expr(it);
        depth++;
    }
    
    if(K < (int)T.size()){
        done = 1;
        depth--; return "";
    }
    depth--; return "";
    return res;
}

int main(){
    while(1){
        cin >> S >> K;
        S = S + ")";
        T = "";
        if(S == "0)" && K == 0) break;
        //cerr << S << " !!! " ;

        it0 = S.begin();
        done = 0;
        expr(it0);

        //cout << T << " ";
        if((int)T.size() <= K) cout << 0 << endl;
        else cout << T[K] << endl;
    }
}
accuracy: 1
time_ms: 910
memory_kb: 4944
final_rank: 10

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

string s, k_in;
int i, k, n;
const int len = 1'000'001;
string expr() {
    string res = "";
    while(i < n) {
        if(isalpha(s[i])) {
            res += s[i];
            i++;
            if(len < res.size()) res.resize(len);
        } else if(isdigit(s[i])) {
            int number = 0;
            while(isdigit(s[i])) {
                number = number * 10 + (s[i] - '0');
                i++;
            }

            if(s[i] == '(') {
                i++;
                string t = expr();
                for(int _ : rep(number)) {
                    res += t;
                    if(len < res.size()) res.resize(len);
                }
                assert(s[i] == ')');
                i++;
            } else {
                assert(isalpha(s[i]));
                for(int _ : rep(number)) {
                    res += s[i];
                    if(len < res.size()) res.resize(len);
                }
                i++;
            }
        } else break;
    }
    return res;
}

int main() {
    while(true) {
        cin >> s >> k_in;
        if(s == "0" and k_in == "0") return 0;
        i = 0;
        k = stoi(k_in);
        n = s.size();

        string t = expr();
        debug(t);
        if(k < t.size()) {
            print(t[k]);
        } else {
            print(0);
        }
    }
}
accuracy: 1
time_ms: 3930
memory_kb: 13868
final_rank: 20

code_snippet: #include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int N;

string dfs(const string &s, int start, int end) {
    string r;
    long long n = -1;
    for (int i = start; i < end;) {
        if ('0' <= s[i] && s[i] <= '9') {
            if (n < 0)
                n = s[i] - '0';
            else
                n = n * 10 + s[i] - '0';
            i++;
        } else if (s[i] == '(') {
            if (n < 0)
                n = 1;
            int cnt = 1, j = i + 1;
            for (; cnt; j++) {
                if (s[j] == '(')
                    cnt++;
                else if (s[j] == ')')
                    cnt--;
            }
            string r0 = dfs(s, i + 1, j - 1);
            for (int _ = 0; _ < n && r.size() <= N; _++) {
                r += r0;
            }
            n = -1;
            i = j;
        } else {
            if (n < 0)
                n = 1;
            for (int _ = 0; _ < n && r.size() <= N; _++) {
                r += s[i];
            }
            n = -1;
            i++;
        }
        if (r.size() > N)
            break;
    }
    return r;
}

int main() {
    for (;;) {
        string s;
        cin >> s >> N;
        if (s == "0")
            break;
        string r = dfs(s, 0, s.size());
        printf("%c\n", r.size() > N ? r[N] : '0');
    }
}
accuracy: 1
time_ms: 10
memory_kb: 8712
final_rank: 16

code_snippet: #include <iostream>
#include <string>
#include <vector>
using namespace std;

int N;

void appendChar(vector<char>& result, char c, long long n) {
    for (int i = 0; i < n && result.size() <= N; ++i) {
        result.push_back(c);
    }
}

void parseString(const string& s, int start, int end, vector<char>& result) {
    long long n = -1;
    for (int i = start; i < end && result.size() <= N;) {
        if ('0' <= s[i] && s[i] <= '9') {
            n = (n < 0 ? 0 : n * 10) + (s[i] - '0');
            ++i;
        } else if (s[i] == '(') {
            if (n < 0)
                n = 1;
            int cnt = 1, j = i + 1;
            for (; cnt;) {
                if (s[j] == '(')
                    ++cnt;
                else if (s[j] == ')')
                    --cnt;
                ++j;
            }
            for (int _ = 0; _ < n && result.size() <= N; _++) {
                parseString(s, i + 1, j - 1, result);
            }
            n = -1;
            i = j;
        } else {
            if (n < 0)
                n = 1;
            appendChar(result, s[i], n);
            n = -1;
            ++i;
        }
    }
}

int main() {
    for (;;) {
        string s;
        cin >> s >> N;
        if (s == "0")
            break;
        vector<char> result;
        parseString(s, 0, s.size(), result);
        cout << ((result.size() > N) ? result[N] : '0') << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 5048
final_rank: 7

code_snippet: #include <cstdlib>
#include <iostream>
#include <string>

int cket(const std::string &str, int i) {
  int n = 1;
  while (n != 0) {
    ++i;
    if (str[i] == '(') ++n;
    if (str[i] == ')') --n;
  }
  return i;
}

int n;

char expand(const std::string &str) {
  std::string res;
  for (int i = 0; i < str.size(); ++i) {
    if (isdigit(str[i])) {
      int j = 1;
      while (isdigit(str[i + j])) ++j;
      int num = std::atoi(str.substr(i, j).c_str());
      int s, e;
      if (str[i + j] == '(') {
        int k = cket(str, i + j + 1);
        s = i + j + 1;
        e = k - (i + j + 1);
        i = k;
      } else {
        s = i + j;
        e = 1;
        i = i + j;
      }
      for (int l = 0; l < num; ++l) {
        char a = expand(str.substr(s, e));
        if (a != '0') return a;
      }
    } else {
      if (n == 0) {
        return str[i];
      }
      n--;
    }
  }
  return '0';
}

int main(void) {
  while (true) {
    std::string str;
    std::cin >> str >> n;
    if (str == "0" && n == 0) break;
    std::cout << expand(str) << std::endl;
  }
  return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 3060
final_rank: 8

code_snippet: #include <cstdlib>
#include <iostream>
#include <string>

int n;

char expand(const std::string &str, int s, int e) {
  for (int i = s; i < e; ++i) {
    if (isdigit(str[i])) {
      int num = 0;
      while (isdigit(str[i])) {
        num = num * 10 + (str[i] - '0');
        i++;
      }
      int s_new, e_new;
      if (str[i] == '(') {
        int k = i + 1;
        int count = 1;
        while (count != 0) {
          if (str[k] == '(')
            count++;
          if (str[k] == ')')
            count--;
          k++;
        }
        s_new = i + 1;
        e_new = k - 1;
        i = k - 1;
      } else {
        s_new = i;
        e_new = i + 1;
      }
      for (int l = 0; l < num; ++l) {
        char a = expand(str, s_new, e_new);
        if (a != '0')
          return a;
      }
    } else {
      if (n == 0) {
        return str[i];
      }
      n--;
    }
  }
  return '0';
}

int main(void) {
  while (true) {
    std::string str;
    std::cin >> str >> n;
    if (str == "0" && n == 0)
      break;
    std::cout << expand(str, 0, str.size()) << std::endl;
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3172
final_rank: 2

code_snippet: #include <cstdlib>
#include <iostream>
#include <string>

int cket(const std::string &str, int i) {
  int n = 1;
  for (; i < str.size(); ++i) {
    if (str[i] == '(')
      n++;
    if (str[i] == ')')
      n--;
    if (n == 0)
      break;
  }
  return i;
}

int n;
char expand(const std::string &str, int s, int e) {
  std::string res;
  for (int i = s; i < s + e; ++i) {
    if (isdigit(str[i])) {
      int j = 1;
      while (isdigit(str[i + j]))
        j++;
      int num = std::atoi(str.substr(i, j).c_str());
      int se, ee;
      if (str[i + j] == '(') {
        int k;
        k = cket(str, i + j + 1);
        se = i + j + 1;
        ee = k - (i + j + 1);
        i = k;
      } else {
        se = i + j;
        ee = 1;
        i = i + j;
      }
      char a = '0';
      for (int l = 0; l < num; ++l) {
        a = expand(str, se, ee);
        if (a != '0')
          return a;
      }
    } else {
      if (n == 0) {
        return str[i];
      }
      n--;
    }
  }
  return '0';
}

int main(void) {
  while (true) {
    std::string str;
    std::cin >> str >> n;
    if (str == "0" && n == 0)
      break;
    std::cout << expand(str, 0, str.size()) << std::endl;
  }
  return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3056
final_rank: 4

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>

int cket(const std::string &str, int i) {
  int n = 1;
  for (; i < str.size(); ++i) {
    if (str[i] == '(')
      n++;
    else if (str[i] == ')')
      n--;
    if (n == 0)
      break;
  }
  return i;
}

int n;
char expand(const std::string &str, int s, int e) {
  for (int i = s; i < s+e; ++i) {
    if (isdigit(str[i])) {
      int num = str[i] - '0';
      int j = i + 1;
      while (isdigit(str[j])) {
        num = num * 10 + (str[j] - '0');
        ++j;
      }
      i = j - 1;
      if (str[i + 1] == '(') {
        int k = cket(str, i + 2);
        for (int l = 0; l < num; ++l) {
          char a = expand(str, i + 2, k - (i + 2));
          if (a != '0')
            return a;
        }
        i = k;
      } else {
        for (int l = 0; l < num; ++l) {
          if (n == 0) {
            return str[i + 1];
          }
          n--;
        }
        i++;
      }
    } else {
      if (n == 0) {
        return str[i];
      }
      n--;
    }
  }
  return '0';
}

int main() {
  while (true) {
    std::string str;
    std::cin >> str >> n;
    if (str == "0" && n == 0)
      break;
    std::cout << expand(str, 0, str.size()) << std::endl;
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3132
final_rank: 3

code_snippet: #include <cstdlib>
#include <iostream>
#include <string>

int cket(const std::string &str, int i) {
  int n = 1;
  for (; i < str.size(); ++i) {
    if (str[i] == '(')
      n++;
    if (str[i] == ')')
      n--;
    if (n == 0)
      break;
  }
  return i;
}

int n;

char expand(const std::string &str, int s, int e) {
  std::string res;
  for (int i = s; i < s + e; ++i) {
    bool isDigit = isdigit(str[i]);
    if (isDigit) {
      int num = 0;
      while (isDigit) {
        num = num * 10 + (str[i] - '0');
        i++;
        isDigit = isdigit(str[i]);
      }
      int start, end;
      if (str[i] == '(') {
        int k = cket(str, i + 1);
        start = i + 1;
        end = k - start;
        i = k;
      } else {
        start = i;
        end = 1;
      }
      for (int l = 0; l < num; ++l) {
        char a = expand(str, start, end);
        if (a != '0')
          return a;
      }
    } else {
      if (n == 0) {
        return str[i];
      }
      n--;
    }
  }
  return '0';
}

int main(void) {
  while (true) {
    std::string str;
    std::cin >> str >> n;
    if (str == "0" && n == 0)
      break;
    std::cout << expand(str, 0, str.size()) << std::endl;
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3168
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <sstream>

int cket(const std::string &str, int i) {
  int n = 1;
  for (; i < str.size(); ++i) {
    if (str[i] == '(')
      n++;
    if (str[i] == ')')
      n--;
    if (n == 0)
      break;
  }
  return i;
}

int n;
char expand(const std::string &str, int start, int len) {
  for (int i = start; i < start + len; ++i) {
    if (isdigit(str[i])) {
      int j = 1;
      while (isdigit(str[i + j]))
        j++;
      std::stringstream ss(str.substr(i, j));
      int num;
      ss >> num;
      int s, e, idx = i + j;
      if (str[idx] == '(') {
        int k;
        k = cket(str, idx + 1);
        s = idx + 1;
        e = k - s;
        i = k;
      } else {
        s = idx;
        e = 1;
        i = idx;
      }
      for (int l = 0; l < num; ++l) {
        char a = expand(str, s, e);
        if (a != '0')
          return a;
      }
    } else {
      if (n == 0) {
        return str[i];
      }
      n--;
    }
  }
  return '0';
}

int main(void) {
  while (true) {
    std::string str;
    std::cin >> str >> n;
    if (str == "0" && n == 0)
      break;
    std::cout << expand(str, 0, str.size()) << std::endl;
  }
  return 0;
}
accuracy: 1
time_ms: 700
memory_kb: 3168
final_rank: 6

code_snippet: #include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int N;

string dfs(const string& s, int start, int end) {
  string r;
  r.reserve(N); // reserve memory for r
  long long n = -1;
  for (int i = start; i < end;) {
    if ('0' <= s[i] && s[i] <= '9') {
      if (n < 0)
        n = s[i] - '0';
      else
        n = n * 10 + s[i] - '0';
      i++;
    } else if (s[i] == '(') {
      if (n < 0)
        n = 1;
      int cnt = 1, j = i + 1;
      for (; cnt;) {
        if (s[j] == '(')
          cnt++;
        else if (s[j] == ')')
          cnt--;
        j++;
      }
      string r0 = dfs(s, i + 1, j - 1);
      for (int _ = 0; _ < n; _++) {
        r += r0;
        if (r.size() > N)
          return r;
      }
      n = -1;
      i = j;
    } else {
      if (n < 0)
        n = 1;
      r += string(n, s[i]);
      n = -1;
      i++;
      if (r.size() > N) // break if size of r is greater than N
        break;
    }
  }
  return r;
}

int main() {
  for (;;) {
    string s;
    cin >> s >> N;
    if (s == "0")
      break;
    string r = dfs(s, 0, s.size());
    printf("%c\n", r.size() > N ? r[N] : '0');
  }
}
accuracy: 1
time_ms: 40
memory_kb: 10648
final_rank: 19

code_snippet: #include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int N;
vector<char> dfs(const vector<char>& s, int i, int j) {
  vector<char> r;
  long long n = -1;
  while (i < j) {
    if ('0' <= s[i] && s[i] <= '9') {
      if (n < 0)
        n = s[i] - '0';
      else
        n = n * 10 + s[i] - '0';
      i++;
    } else if (s[i] == '(') {
      if (n < 0)
        n = 1;
      int cnt = 1, k = i + 1;
      while (cnt) {
        if (s[k] == '(')
          cnt++;
        else if (s[k] == ')')
          cnt--;
        k++;
      }
      vector<char> r0 = dfs(s, i + 1, k - 1);
      for (int _ = 0; _ < n; _++) {
        r.insert(r.end(), r0.begin(), r0.end());
        if (r.size() > N)
          return r;
      }
      n = -1;
      i = k;
    } else {
      if (n < 0)
        n = 1;
      r.insert(r.end(), n, s[i]);
      n = -1;
      i++;
    }
  }
  return r;
}
int main() {
  for (;;) {
    string s;
    cin >> s >> N;
    if (s == "0")
      break;
    vector<char> r = dfs(vector<char>(s.begin(), s.end()), 0, s.size());
    printf("%c\n", r.size() > N ? r[N] : '0');
  }
}
accuracy: 1
time_ms: 10
memory_kb: 8380
final_rank: 13

code_snippet: #include <iostream>
#include <string>

std::string dfs(const std::string& s, int N) {
  std::string r;
  int i = 0;
  long long n = -1;
  for (; i < s.size();) {
    if ('0' <= s[i] && s[i] <= '9') {
      if (n < 0)
        n = s[i] - '0';
      else
        n = n * 10 + s[i] - '0';
      i++;
    } else if (s[i] == '(') {
      if (n < 0)
        n = 1;
      int cnt = 1, j = i + 1;
      for (; cnt;) {
        if (s[j] == '(')
          cnt++;
        else if (s[j] == ')')
          cnt--;
        j++;
      }
      std::string r0 = dfs(s.substr(i + 1, j - 1 - i - 1), N);
      for (int _ = 0; _ < n; _++) {
        r += r0;
        if (r.size() > N)
          return r;
      }
      n = -1;
      i = j;
    } else {
      if (n < 0)
        n = 1;
      r.append(n, s[i]);
      n = -1;
      i++;
    }
  }
  return r;
}

int main() {
  for (;;) {
    std::string s;
    int N;
    std::cin >> s >> N;
    if (s == "0")
      break;
    std::string r;
    r.reserve(N + 1);
    r = dfs(s, N);
    std::cout << (r.size() > N ? r[N] : '0') << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8748
final_rank: 18