Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
M = 10**6; dp = [-1]*M; dp[0],dp[1] = 0,0;
def collatz(n):
    if dp[n]!=-1: return dp[n]
    dp[n] = 1+collatz(n/2 if n%2==0 else 3*n+1)
    return dp[n]
while 1:
    n = input()
    if n==0: break
    print collatz(n)

Fixed submission:
def collatz(n):
    if n==1: return 0
    return 1+collatz(n/2 if n%2==0 else 3*n+1)
while 1:
    n = input()
    if n==0: break
    print collatz(n)
Buggy python submission (verdict: Runtime Error):
while 1:
    n = int(input())
    team = []
    for _ in range(n):
        i, *T = map(int, input().split())
        t = 0
        for m, s in zip(T[::2], T[1::2]):
            t += m*60 + s
        team.append((t, i))
    team.sort()
    print(*[team[0][1], team[1][1], team[-2][1]], sep='\n')

Fixed submission:
while 1:
    n = int(input())
    if n == 0:
        break
    team = []
    for _ in range(n):
        i, *T = map(int, input().split())
        t = 0
        for m, s in zip(T[::2], T[1::2]):
            t += m*60 + s
        team.append((t, i))
    team.sort()
    print(*[team[0][1], team[1][1], team[-2][1]], sep='\n')

Buggy python submission (verdict: Runtime Error):
while 1:
    n = int(input())
    team = []
    for _ in range(n):
        i, *T = map(int, input().split())
        t = 0
        for m, s in zip(T[::2], T[1::2]):
            t += m*60 + s
        team.append((t, i))
    team.sort()
    print(team[0][1])
    print(team[1][1])
    print(team[-2][1])

Fixed submission:
while 1:
    n = int(input())
    if n == 0:
        break
    team = []
    for _ in range(n):
        i, *T = map(int, input().split())
        t = 0
        for m, s in zip(T[::2], T[1::2]):
            t += m*60 + s
        team.append((t, i))
    team.sort()
    print(*[team[0][1], team[1][1], team[-2][1]], sep='\n')
Buggy python submission (verdict: Runtime Error):
num = [2,3,5]
M = 10**6; S = [0]*(M+2); S[1] = 1; s = 0;
for i in xrange(1,M+1):
    if S[i]==1:
        for j in xrange(3):
            S[min(M+1,num[j]*i)] = 1;
        s += 1
    S[i] = s
while 1:
    try:
        m,n = map(int, raw_input().split())
        print S[n]-S[m-1]
    except EOFError:
        break

Fixed submission:
while 1:
    ipt = raw_input()
    if ipt=="0": break
    m,n = map(int, ipt.split())
    ans = 0
    for i in xrange(m,n+1):
        p = i
        while p>1:
            if p%2==0:
                p/=2
            elif p%3==0:
                p/=3
            elif p%5==0:
                p/=5
            else: break
        if p==1: ans+=1
    print ans

Buggy python submission (verdict: Runtime Error):
num = [2,3,5]
M = 10**6; S = [0]*(M+2); S[1] = 1; s = 0;
i = 0;
while(i<M+1):
    if S[i]==1:
        for j in xrange(3):
            S[min(M+1,num[j]*i)] = 1;
        s += 1
    S[i] = s
    i+=1
while 1:
    try:
        m,n = map(int, raw_input().split())
        print S[n]-S[m-1]
    except EOFError:
        break

Fixed submission:
while 1:
    ipt = raw_input()
    if ipt=="0": break
    m,n = map(int, ipt.split())
    ans = 0
    for i in xrange(m,n+1):
        p = i
        while p>1:
            if p%2==0:
                p/=2
            elif p%3==0:
                p/=3
            elif p%5==0:
                p/=5
            else: break
        if p==1: ans+=1
    print ans

Buggy python submission (verdict: Runtime Error):
while 1:
    try:
        n,m = map(int, raw_input().split())
        ans = 0
        for i in xrange(n,m+1):
            p = i
            while p>1:
                if p%2==0:
                    p/=2
                elif p%3==0:
                    p/=3
                elif p%5==0:
                    p/=5
                else: break
            if p==1: ans+=1
        print ans
    except EOFError:
        break

Fixed submission:
while 1:
    ipt = raw_input()
    if ipt=="0": break
    m,n = map(int, ipt.split())
    ans = 0
    for i in xrange(m,n+1):
        p = i
        while p>1:
            if p%2==0:
                p/=2
            elif p%3==0:
                p/=3
            elif p%5==0:
                p/=5
            else: break
        if p==1: ans+=1
    print ans
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[input()/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])

Fixed submission:
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[int(raw_input())/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(7)]
    for i in range(n):
        age = input()
        s[age/10 if age<60 else 6] += 1
    for i in range(7):
        print s[i]

Fixed submission:
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[int(raw_input())/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(7)]
    for i in range(n):
        age = input()
        s[min(6, age/10)] += 1
    for i in range(7):
        print s[i]

Fixed submission:
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[int(raw_input())/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    n = input()
    if n==0: break
    s = [0 for i in xrange(7)]
    for i in range(n):
        age = input()
        s[min(6, age/10)] += 1
    for i in xrange(7):
        print s[i]

Fixed submission:
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[int(raw_input())/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    n = input()
    if n==0: break
    s = [0 for i in xrange(7)]
    for i in xrange(n):
        age = input()
        s[min(6, age/10)] += 1
    for i in xrange(7):
        print s[i]

Fixed submission:
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[int(raw_input())/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    s = [0 for i in xrange(7)]
    n = input()
    if n==0: break
    for i in xrange(n):
        age = input()
        s[min(6, age/10)] += 1
    for i in xrange(7):
        print s[i]

Fixed submission:
while 1:
    n = input()
    if n==0: break
    s = [0 for i in range(13)]
    for i in range(n):
        s[int(raw_input())/10] += 1
    for i in range(6):
        print s[i]
    print sum(s[6:])
Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n==0: break
    pm = -1; m = 0;
    for i in xrange(n):
        p,d1,d2 = map(int, raw_input().split())
        if m<d1+d2:
            pm = p; m = d1+d2;
    print pm,m

Fixed submission:
while 1:
    try:
        n = input()
        if n==0: break
        l = []
        for i in range(n):
            p,d1,d2 = map(int, raw_input().split())
            l.append([p,d1+d2])
        l.sort(reverse=True, key=lambda x:x[1])
        print l[0][0],l[0][1]
    except SyntaxError:
        pass

Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n==0: break
    pm = -1; m = -1;
    for i in range(n):
        p,d1,d2 = map(int, raw_input().split())
        if m<d1+d2:
            pm = p; m = d1+d2;
    print pm,m

Fixed submission:
while 1:
    try:
        n = input()
        if n==0: break
        l = []
        for i in range(n):
            p,d1,d2 = map(int, raw_input().split())
            l.append([p,d1+d2])
        l.sort(reverse=True, key=lambda x:x[1])
        print l[0][0],l[0][1]
    except SyntaxError:
        pass

Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n==0: break
    l = []
    for i in range(n):
        p,d1,d2 = map(int, raw_input().split())
        l.append([p,d1+d2])
    l.sort(reverse=True, key=lambda x:x[1])
    print l[0][0],l[0][1]

Fixed submission:
while 1:
    try:
        n = input()
        if n==0: break
        l = []
        for i in range(n):
            p,d1,d2 = map(int, raw_input().split())
            l.append([p,d1+d2])
        l.sort(reverse=True, key=lambda x:x[1])
        print l[0][0],l[0][1]
    except SyntaxError:
        pass
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    w = raw_input()
    if w=="0000": break
    if int(w)%1111==0:
        print "NA"
        continue
    ans = 0
    while(w!="6174"):
        w = str(int("".join(sorted(w,reverse=True)))-int("".join(sorted(w))))
        ans += 1
    print ans

Fixed submission:
while 1:
    w = raw_input()
    if w=="0000": break
    if int(w)%1111==0:
        print "NA"
        continue
    ans = 0
    while(w!="6174"):
        w = ("".join(sorted(w))).zfill(4)
        w = str(int(w[::-1])-int(w))
        ans += 1
    print ans
Buggy python submission (verdict: Runtime Error):
input();Q=[0]*101
for p in map(int,input().split()):Q[p]+=1
print(max(i for i in range(101)if sum(Q[i:])>=i))

Fixed submission:
input();Q=[0]*101
for p in map(int,input().split()):Q[p]+=1
print(max(i for i in range(101)if sum(Q[i:])>=i))
Buggy python submission (verdict: Runtime Error):
n = int(input())
*P, = map(int, input().split())
memo = {(n-1, i): i for i in range(P[-1]+1)}
def dfs(pos, t):
    if (pos, t) in memo:
        return memo[pos, t]
    res = memo[pos, t] = dfs(pos+1, max(0, P[pos+1]-t)) + t*2
    return res
print(min(dfs(0, P[0]-i)+i for i in range(P[0]+1)))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n = int(input())
*P, = map(int, input().split())
memo = {(n-1, i): i for i in range(P[-1]+1)}
def dfs(pos, t):
    if (pos, t) in memo:
        return memo[pos, t]
    res = memo[pos, t] = dfs(pos+1, max(0, P[pos+1]-t)) + t*2
    return res
print(min(dfs(0, P[0]-i)+i for i in range(P[0]+1)))
Buggy python submission (verdict: Runtime Error):
n = int(input())
for i in range(n):
    F = set(map(int, input().split()))
    fl = min(F); fr = max(F)
    G = {*range(1, 14)} - F - {7}
    gl = min(G); gr = max(G)

    memo = {}
    def dfs(s, t, u):
        if (s, t, u) in memo:
            return memo[s, t, u]
        T = [G, F][u]
        res = 0
        if s-1 in T:
            if s-1 <= [gl, fl][u] and [gr, fr][u] <= t:
                res = 1
            else:
                res |= dfs(s-1, t, u^1)^1

        if t+1 in T:
            if s <= [gl, fl][u] and [gr, fr][u] <= t+1:
                res = 1
            else:
                res |= dfs(s, t+1, u^1)^1

        if s-1 not in T and t+1 not in T:
            res = dfs(s, t, u^1)^1
        memo[s, t, u] = res
        return res
    print(["no", "yes"][dfs(7, 7, 1)])

Fixed submission:
n = int(input())
for i in range(n):
    F = set(map(int, input().split()))
    fl = min(F); fr = max(F)
    G = {i for i in range(1, 14)} - F - {7}
    gl = min(G); gr = max(G)

    memo = {}
    def dfs(s, t, u):
        if (s, t, u) in memo:
            return memo[s, t, u]
        T = [G, F][u]
        res = 0
        if s-1 in T:
            if s-1 <= [gl, fl][u] and [gr, fr][u] <= t:
                res = 1
            else:
                res |= dfs(s-1, t, u^1)^1

        if t+1 in T:
            if s <= [gl, fl][u] and [gr, fr][u] <= t+1:
                res = 1
            else:
                res |= dfs(s, t+1, u^1)^1

        if s-1 not in T and t+1 not in T:
            res = dfs(s, t, u^1)^1
        memo[s, t, u] = res
        return res
    print(["no", "yes"][dfs(7, 7, 1)])
Buggy python submission (verdict: Runtime Error):
w, h = map(int, input().split())
S = [list(map(int, input().split())) for i in range(h)]
SW = [[0]*w for i in range(h)]
SH = [[0]*w for i in range(h)]
for i in range(h):
    cnt = 0
    for j in range(w-1, -1, -1):
        cnt += S[i][j]
        SW[i][j] = cnt
for j in range(w):
    cnt = 0
    for i in range(h-1, -1, -1):
        cnt += S[i][j]
        SH[i][j] = cnt
memo = {}
def dfs(x, y):
    if (x, y) in memo:
        return memo[x, y]
    if x == w or y == h:
        return 0
    if (x+y) % 2 == 0:
        # first
        res = max(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x])
    else:
        # second
        res = min(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x])
    memo[x, y] = res
    return res
print(abs(dfs(0, 0)))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
w, h = map(int, input().split())
S = [list(map(int, input().split())) for i in range(h)]
SW = [[0]*w for i in range(h)]
SH = [[0]*w for i in range(h)]
for i in range(h):
    cnt = 0
    for j in range(w-1, -1, -1):
        cnt += S[i][j]
        SW[i][j] = cnt
for j in range(w):
    cnt = 0
    for i in range(h-1, -1, -1):
        cnt += S[i][j]
        SH[i][j] = cnt
memo = {}
def dfs(x, y):
    if (x, y) in memo:
        return memo[x, y]
    if x == w or y == h:
        return 0
    if (x+y) % 2 == 0:
        # first
        res = max(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x])
    else:
        # second
        res = min(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x])
    memo[x, y] = res
    return res
print(abs(dfs(0, 0)))
Buggy python submission (verdict: Runtime Error):
p = [[0 for i in xrange(16)] for j in xrange(16)]
while 1:
    a,b = map(int, raw_input().split())
    if a==b==0: break
    n = input()
    for i in xrange(b):
        for j in xrange(a):
            p[i][j] = 0
    for i in xrange(n):
        x,y = map(int, raw_input().split())
        p[y-1][x-1] = -10**6
    p[0][0] = 1
    for i in xrange(b):
        for j in xrange(a):
            if p[i][j]<0: continue
            p[i+1][j] += p[i][j]; p[i][j+1] += p[i][j];
    print p[b-1][a-1]

Fixed submission:
p = [[0 for i in xrange(17)] for j in xrange(17)]
while 1:
    a,b = map(int, raw_input().split())
    if a==b==0: break
    n = input()
    for i in xrange(b):
        for j in xrange(a):
            p[i][j] = 0
    for i in xrange(n):
        x,y = map(int, raw_input().split())
        p[y-1][x-1] = -10**6
    p[0][0] = 1
    for i in xrange(b):
        for j in xrange(a):
            if p[i][j]<0: continue
            p[i+1][j] += p[i][j]; p[i][j+1] += p[i][j];
    print p[b-1][a-1]
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    INF = 10**9
    N, K = map(int, input().split())
    if N == K == 0:
        break
    E = [[INF]*N for i in range(N)]
    for i in range(N):
        E[i][i] = 0
    ans = []
    for i in range(K):
        q, *c = map(int, input().split())
        if q == 0:
            a, b = c
            cost = E[a-1][b-1]
            if INF <= cost:
                ans.append(-1)
            else:
                ans.append(cost)
        else:
            c, d, e = c
            if e < E[c-1][d-1]:
                E[c-1][d-1] = E[d-1][c-1] = e
                for i in range(N):
                    for j in range(N):
                        E[i][j] = E[j][i] = min(E[i][j], E[i][c-1] + e + E[d-1][j], E[i][d-1] + e + E[c-1][j])
    print(*ans, sep='\n')


Fixed submission:
while 1:
    INF = 10**9
    N, K = map(int, input().split())
    if N == K == 0:
        break
    E = [[INF]*N for i in range(N)]
    for i in range(N):
        E[i][i] = 0
    ans = []
    for i in range(K):
        q, *c = map(int, input().split())
        if q == 0:
            a, b = c
            cost = E[a-1][b-1]
            if INF <= cost:
                ans.append(-1)
            else:
                ans.append(cost)
        else:
            c, d, e = c
            if e < E[c-1][d-1]:
                E[c-1][d-1] = E[d-1][c-1] = e
                Ec = E[c-1]; Ed = E[d-1]
                for i in range(N):
                    Ei = E[i]
                    Eic = Ei[c-1] + e
                    Eid = Ei[d-1] + e
                    for j in range(i+1, N):
                        Ei[j] = E[j][i] = min(Ei[j], Eic + Ed[j], Eid + Ec[j])
    print(*ans, sep='\n')
Buggy python submission (verdict: Time Limit Exceeded):
class RollingHash:
    def __init__(self, s, base, MOD):
        self.s = s
        self.l = l = len(s)
        self.base = base
        self.MOD = MOD
        self.h = h = [0]*(l + 1)
        for i in range(l):
            h[i+1] = (h[i] * base + ord(s[i])) % MOD
    def get(self, l, r):
        MOD = self.MOD
        return ((self.h[r] - self.h[l]*pow(self.base, r-l, MOD) + MOD)) % MOD

C = open(0).read().split()
MOD = 563980774085007485
for t in range(len(C)//2):
    S = C[2*t]; T = C[2*t+1]
    rhs = RollingHash(S, 31, MOD)
    rht = RollingHash(T, 31, MOD)
    L = len(S)
    hs = [set() for i in range(L+1)]
    for l in range(1, L+1):
        hsl = hs[l]
        for i in range(L-l+1):
            hsl.add(rhs.get(i, i+l))
    ans = 0
    M = len(T)
    for l in range(L, 0, -1):
        hsl = hs[l]
        for i in range(M-l+1):
            if rht.get(i, i+l) in hsl:
                break
        else:
            continue
        ans = l
        break
    print(ans)


Fixed submission:
def rolling_hash(S, base, MOD):
    l = len(S)
    h = [0]*(l + 1)
    for i in range(l):
        h[i+1] = (h[i] * base + ord(S[i])) % MOD
    return h


C = open(0).read().split()
MOD = 358976445361682909
base = 31
for t in range(len(C)//2):
    S = C[2*t]; T = C[2*t+1]
    rhs = rolling_hash(S, base, MOD)
    rht = rolling_hash(T, base, MOD)
    L = len(S); M = len(T)
    def solve(l):
        v = pow(base, l, MOD)
        hs = set()
        for i in range(L-l+1):
            hs.add((rhs[i+l] - rhs[i]*v) % MOD)
        for i in range(M-l+1):
            if (rht[i+l] - rht[i]*v) % MOD in hs:
                return 1
        return 0
    left = 0; right = min(L, M)+1
    while left+1 < right:
        mid = (left + right) // 2
        if solve(mid):
            left = mid
        else:
            right = mid
    print(left)
Buggy python submission (verdict: Runtime Error):
while True:
    n, m = map(int, raw_input().split())
    if n==0 and m==0:
        break
    b = [input() for i in xrange(n)]
    p = 0; ans = 0;
    for i in xrange(m):
        d = input()
        p += d
        if p>=n-1 or p+b[p]>=n-1:
            ans = i+1
            break
        p += b[p]
    print ans

Fixed submission:
while True:
    n, m = map(int, raw_input().split())
    if n==0 and m==0:
        break
    b = [input() for i in xrange(n)]
    d = [input() for i in xrange(m)]
    p = 0; ans = 0;
    for i in xrange(m):
        p += d[i]
        if p>=n-1 or p+b[p]>=n-1:
            ans = i+1
            break
        p += b[p]
    print ans
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
D = [int(input()) for i in range(N)]
C = [int(input()) for i in range(M)]
memo = {}
def dfs(i, j):
    if i == N:
        return 0
    if j == M:
        return 10**9
    if (i, j) in memo:
        return memo[i, j]
    memo[i, j] = res = min(dfs(i+1, j+1) + D[i]*C[j], dfs(i, j+1))
    return res
print(dfs(0, 0))

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
N, M = map(int, input().split())
D = [int(input()) for i in range(N)]
C = [int(input()) for i in range(M)]
memo = {}
def dfs(i, j):
    if i == N:
        return 0
    if j == M:
        return 10**9
    if (i, j) in memo:
        return memo[i, j]
    memo[i, j] = res = min(dfs(i+1, j+1) + D[i]*C[j], dfs(i, j+1))
    return res
print(dfs(0, 0))
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = []
for i in range(H):
    tmp = []
    s = input()
    for j in range(W):
        if s[j] == '.':
            tmp.append(-1)
        else:
            tmp.append(int(s[j]))
    S.append(tmp)
D = [0, 1, -1, -1, 0, -1, 1, 1, 0]
update = 1; ans = 0
while update:
    update = 0; ans += 1
    d = set()
    for i in range(H):
        for j in range(W):
            if S[i][j] > -1:
                cnt = 0
                for k in range(8):
                    ni = i + D[k]; nj = j + D[k+1]
                    if S[ni][nj] == -1:
                        cnt += 1
                if S[i][j] <= cnt:
                    d.add((i, j))
                    update = 1
    for i, j in d:
        S[i][j] = -1
print(ans-1)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
S = [list(map(lambda x: int(x) if x != '.' else -1, input())) for i in range(H)]
dd = ((-1, 0), (-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1))
que = []
D = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        v = S[i][j]
        if v == -1:
            continue
        x = sum(S[i+dy][j+dx] == -1 for dx, dy in dd)
        if v <= x:
            S[i][j] = 0
            que.append((i, j))
        else:
            S[i][j] -= x
ans = 0
while que:
    que2 = []
    for y, x in que:
        if S[y][x] == -1:
            continue
        S[y][x] = -1
        for dx, dy in dd:
            nx = x + dx; ny = y + dy
            if S[ny][nx] > 0:
                S[ny][nx] -= 1
                if not S[ny][nx]:
                    que2.append((ny, nx))
    que = que2
    ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
N2 = 2**N.bit_length()
D = [[0]*(N2+1) for i in range(M)]
def get(i, k):
    data = D[i]
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def add(i, k, x):
    data = D[i]
    while k <= N2:
        data[k] += x
        k += k & -k
cnts = [0]*M
for i in range(N):
    v = int(input())
    cnts[v-1] += 1
    add(v-1, i+1, 1)
memo = {2**M-1: 0}
def dfs(state, idx):
    if state in memo:
        return memo[state]
    res = N
    for i in range(M):
        if state & (1 << i) == 0:
            need = cnts[i] - (get(i, cnts[i] + idx) - get(i, idx))
            res = min(res, need + dfs(state | (1 << i), idx + cnts[i]))
    memo[state] = res
    return res
print(dfs(0, 0))

Fixed submission:
N, M = map(int, input().split())
D = [[0]*(N+1) for i in range(M)]
cnts = [0]*M
for i in range(N):
    v = int(input())
    cnts[v-1] += 1
    D[v-1][i+1] = 1
for i in range(M):
    d = D[i]
    for j in range(1, N+1):
        d[j] += d[j-1]
memo = [None]*(2**M)
memo[2**M-1] = 0
def dfs(state, idx):
    if memo[state] is not None:
        return memo[state]
    res = N
    for i in range(M):
        if state & (1 << i) == 0:
            need = cnts[i] - (D[i][cnts[i] + idx] - D[i][idx])
            res = min(res, need + dfs(state | (1 << i), idx + cnts[i]))
    memo[state] = res
    return res
print(dfs(0, 0))
Buggy python submission (verdict: Runtime Error):
def s(p,A,B):b=lambda i:(A[i]and B[i])and s(p,A[i],B[i])or p==u and(A[i]or B[i])or 0;return(b(0),b(1))
try:
 while 1:g=_,i,u=raw_input().split();print`s(*map(lambda s:eval(s.replace("(,","(0,").replace(",)",",0)")),g))`.replace(" ","").replace("0","")
except:pass

Fixed submission:
def convert(s):
    cur = [0]
    def dfs():
        result = [0, 0]
        cur[0] += 1 # '('
        if s[cur[0]] == '(':
            result[0] = dfs()
        cur[0] += 1 # ','
        if s[cur[0]] == '(':
            result[1] = dfs()
        cur[0] += 1 # ')'
        return result
    return dfs()
def rconvert(C):
    def dfs(node):
        res = ["", ""]
        if node[0]:
            res[0] = rconvert(node[0])
        if node[1]:
            res[1] = rconvert(node[1])
        return "(%s,%s)" % tuple(res)
    return dfs(C)
def solve(A, B, t):
    def get(i):
        if A[i] and B[i]:
            return solve(A[i], B[i], t)
        elif t is 'u':
            return A[i] or B[i]
    return [get(0), get(1)]
try:
    while 1:
        ipt = raw_input().split()
        print rconvert(solve(convert(ipt[1]), convert(ipt[2]), ipt[0]))
except:pass