Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M=mi()
    A=list(mi())

    v = [0] * 2*10**5
    seen = set()
    for i in range(N):
        v[i+1] = A[i] + v[i]
        v[i+1] %= M
        seen.add(v[i+1])
    
    counts = {x:0 for x in seen}
    for i in range(N+1):
        counts[v[i]] += 1
    
    ans = 0
    for _,count in counts.items():
        ans += count * (count-1) // 2
    
    print(ans)
    





if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M=mi()
    A=list(mi())

    v = [0] * 2*10**5
    seen = set([0])
    for i in range(N):
        v[i+1] = A[i] + v[i]
        v[i+1] %= M
        seen.add(v[i+1])
    
    counts = {x:0 for x in seen}
    for i in range(N+1):
        counts[v[i]] += 1
    
    ans = 0
    for _,count in counts.items():
        ans += count * (count-1) // 2
    
    print(ans)
    





if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M=mi()
    A=list(mi())

    v = [0] * 2*10**5
    seen = set()
    for i in range(N):
        v[i+1] = A[i] + v[i]
        v[i+1] %= M
        seen.add(v[i+1])
    
    counts = {x:0 for x in seen}
    for i in range(N+1):
        counts[v[i]] += 1
    
    ans = 0
    for _,count in counts.items():
        ans += count * (count-1) // 2
    
    print(ans)
    





if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N,M=mi()
    A=list(mi())

    v = [0] * 2*10**5
    seen = set([0])
    for i in range(N):
        v[i+1] = A[i] + v[i]
        v[i+1] %= M
        seen.add(v[i+1])
    
    counts = {x:0 for x in seen}
    for i in range(N+1):
        counts[v[i]] += 1
    
    ans = 0
    for _,count in counts.items():
        ans += count * (count-1) // 2
    
    print(ans)
    





if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python3
import itertools
import sys
sys.setrecursionlimit(10**9)


INF=10**20
def solve(N: int, C: int, D: "List[List[int]]", c: "List[List[int]]"):

    colormap = [[] for _ in range(3)]
    for i in range(1,N+1):
        for j in range(1,N+1):
            k = i+j
            k %= 3
            colormap[k].append(c[i-1][j-1])

    ans = INF
    for _colors in itertools.permutations(list(range(1,C+1)),3):
        colors = list(_colors)
        res = 0
        for mod,current_colors in enumerate(colormap):
            for current in current_colors:
                color = colors[mod]
                res += D[current-1][color-1]

        ans = min(res,ans)

    
    print(ans)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = [[int(next(tokens)) for _ in range(C)] for _ in range(C)]  # type: "List[List[int]]"
    c = [[int(next(tokens)) for _ in range(N)] for _ in range(N)]  # type: "List[List[int]]"
    solve(N, C, D, c)



if __name__ == "__main__":
    main()


Fixed submission:
import numpy as np
import itertools
import sys
sys.setrecursionlimit(10**9)


INF=10**18
def solve(N,C,D,colormap,new_colormap):
    ans = INF
    for j in range(len(new_colormap)):
        new_colors = new_colormap[j]
        res = 0
        for mod in range(3):
            for current_color in range(1,C+1):
                if current_color == 0: continue
                new_color = new_colors[mod]
                res += D[current_color-1][new_color-1] * colormap[mod][current_color]

        ans = min(res,ans)

    
    print(ans)

    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = [[int(next(tokens)) for _ in range(C)] for _ in range(C)]  # type: "List[List[int]]"
    c = [[int(next(tokens)) for _ in range(N)] for _ in range(N)]  # type: "List[List[int]]"


    colormap = np.zeros((3,C+1),dtype=int)
    for i in range(1,N+1):
        for j in range(1,N+1):
            k = i+j
            k %= 3
            colormap[k][c[i-1][j-1]] += 1
            

    new_color_comb = list(itertools.permutations(list(range(1,C+1)),3))
    solve(N, C, np.array(D), colormap, np.array(new_color_comb))



def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')

    # basic types: https://numba.pydata.org/numba-doc/0.13/types.html
    cc.export('solve', '(i8,i8,i8[:,:],i8[:,:],i8[:,:],)')(solve)
    cc.compile()

if __name__ == '__main__':
    # cc_export()
    try:
        from my_module import solve
    except:
        cc_export()
        exit()

    main()
Buggy python submission (verdict: Runtime Error):
n=int(input())

t,x,y=[],[],[]
for i in range(n):
    l=list(map(int,input().split()))
    t.append(l[0])
    x.append(l[1])
    y.append(l[2])

if n==1:
    if x+y>t or (x+y)%2!=t%2:
        print("No")
    else:
        print("Yes")
    exit()


for i in range(n-1):
    dx=abs(x[i+1]-x[i])
    dy=abs(y[i+1]-y[i])
    dt=t[i+1]-t[i]

    if dx+dy>dt or (dx+dy)%2!=dt%2:
        print("No")
        exit()


print("Yes")


Fixed submission:
a,b = map(int,input().split())

print("Odd" if (a*b)%2!=0 else "Even")
Buggy python submission (verdict: Time Limit Exceeded):
n,Y=map(int,input().split())

flag=False
for y in range(4000):
    for z in range(2300):
        x = n-(y+z)
        if 5*y+9*z == n*10-Y*(10**(-3)) and x >= 0:
            print(x,end="  ")
            print(y,end=" ")
            print(z,end="")
            flag = True
            break
    if flag:
        break

if not flag:
    print("-1 -1 -1")


Fixed submission:
n,Y=map(int,input().split())

for y in range(n+1):
    for z in range(n-y+1):
        x = n-(y+z)
        if 5*y+9*z == n*10-Y*(10**(-3)) and x >= 0:
            print(x,end="  ")
            print(y,end=" ")
            print(z,end="")
            exit()

print("-1 -1 -1")
Buggy python submission (verdict: Runtime Error):
import sys

def comb(n, k):
    if k == 0:
        return 0
    if n < k:
        return 0
    res=1
    for i in range(n - k + 1, n + 1):
        res *= i
    for i in range(1,k + 1):
        res //= i
    return res

n=input()
k = int(input())
l = len(n)

if l == 1:
    if k >= 2:
        print(0)
        sys.exit()
    print(top)
    sys.exit()

top = int(n[0])

if k == 1:
    z = 9
elif k == 2:
    z = 81
else:
    z = 729

ans = comb(l - 1, k) * z
ans += comb(l - 1, k - 1) * (top - 1) * (z // 9)

if k == 1:
    ans+=top    
elif k == 2:
    for i in range(1, l):
        if i==1 or int(n[1:i]) == 0:
            ans += int(n[i])
        else:
            ans += 9
elif k == 3:
    for i in range(1, l):
        for j in range(i + 1, l):
            flag1 = i == 1 or int(n[1:i]) == 0
            flag2 = j == i + 1 or int(n[i + 1 :j]) == 0
            if not(flag1) and not(flag2):
                ans += 81
            elif flag1 and not (flag2):
                ans += 9 * int(n[i])
            elif not (flag1) and flag2:
                ans += 81
            else:
                ans += 9 * (int(n[i]) - 1) + int(n[j])

print(ans)


Fixed submission:
import sys

def comb(n, k):
    if k == 0:
        return 0
    if n < k:
        return 0
    res=1
    for i in range(n - k + 1, n + 1):
        res *= i
    for i in range(1,k + 1):
        res //= i
    return res

n=input()
k = int(input())
l = len(n)

top = int(n[0])

if l == 1:
    if k >= 2:
        print(0)
        sys.exit()
    print(top)
    sys.exit()

if k == 1:
    z = 9
elif k == 2:
    z = 81
else:
    z = 729

ans = comb(l - 1, k) * z
ans += comb(l - 1, k - 1) * (top - 1) * (z // 9)

if k == 1:
    ans+=top
elif k == 2:
    for i in range(1, l):
        if i==1 or int(n[1:i]) == 0:
            ans += int(n[i])
        else:
            ans += 9
elif k == 3:
    for i in range(1, l):
        for j in range(i + 1, l):
            flag1 = i == 1 or int(n[1:i]) == 0
            flag2 = j == i + 1 or int(n[i + 1 :j]) == 0
            if not(flag1) and not(flag2):
                ans += 81
            elif flag1 and not (flag2):
                ans += 9 * int(n[i])
            elif not (flag1) and flag2:
                ans += 81
            else:
                if int(n[i]) == 0:
                    continue
                ans += 9 * (int(n[i]) - 1) + int(n[j])

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000000000)

MOD = 10 ** 9 + 7

@lru_cache(maxsize=None)
def r(k, smaller):
    global s

    if k == len(s):
        return 1

    if smaller:
        if s[k] == "1":
            return (2 * r(k + 1, True) + r(k + 1, False)) % MOD
        else:
            return r(k + 1, True) % MOD
    else:
        return (3 * r(k + 1, False)) % MOD


s = input()
print(r(0, True))


Fixed submission:
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000000)

MOD = 10 ** 9 + 7

@lru_cache(maxsize=None)
def r(k, smaller):
    global s

    if k == len(s):
        return 1

    if smaller:
        if s[k] == "1":
            return (2 * r(k + 1, True) + r(k + 1, False)) % MOD
        else:
            return r(k + 1, True) % MOD
    else:
        return (3 * r(k + 1, False)) % MOD


s = input()
print(r(0, True))

Buggy python submission (verdict: Runtime Error):
from functools import lru_cache

MOD = 10 ** 9 + 7

@lru_cache(maxsize=None)
def r(k, smaller):
    global s

    if k == len(s):
        return 1

    if smaller:
        if s[k] == "1":
            return (2 * r(k + 1, True) + r(k + 1, False)) % MOD
        else:
            return r(k + 1, True) % MOD
    else:
        return (3 * r(k + 1, False)) % MOD


s = input()
print(r(0, True))


Fixed submission:
import sys
from functools import lru_cache

sys.setrecursionlimit(1000000000)

MOD = 10 ** 9 + 7

@lru_cache(maxsize=None)
def r(k, smaller):
    global s

    if k == len(s):
        return 1

    if smaller:
        if s[k] == "1":
            return (2 * r(k + 1, True) + r(k + 1, False)) % MOD
        else:
            return r(k + 1, True) % MOD
    else:
        return (3 * r(k + 1, False)) % MOD


s = input()
print(r(0, True))
Buggy python submission (verdict: Runtime Error):
a,b=list(map(int,input().split(" ")))
if a>=13:
  pass
elif a>=6:
  b=b//2
else
  b=0
  
print(b)
 

Fixed submission:
a,b=list(map(int,input().split(" ")))
if a>=13:
  pass
elif a>=6:
  b=b//2
else:
  b=0
  
print(b)

Buggy python submission (verdict: Runtime Error):
a,b=list(map(int,input().split(" ")))
if b>=13:
  pass
elif b>=6:
  b=b//2
else
  b=0
  
print(b)
 

Fixed submission:
a,b=list(map(int,input().split(" ")))
if a>=13:
  pass
elif a>=6:
  b=b//2
else:
  b=0
  
print(b)
Buggy python submission (verdict: Runtime Error):
def func(num,pr):
  global l,ans

  if len(l[num])!=0:
    for z in l[num]:
      if z[0]==pr:
        continue

      if pr==-1:
        ans[z[0]]=z[1]
      else:
        ans[z[0]]=ans[pr]+z[1]
      func(z[0],num)

N=int(input())
l=[ [] for z in range(N)]
ans=[0]*N


for z in range(N-1):
  u,v,w=list(map(int,input().split(" ")))
  l[u-1].append((v-1,w))
  l[v-1].append((u-1,w))

func(0,-1)

for z in ans:
  if z%2==0:
    print(1)
  else:
    print(0)

Fixed submission:
import sys
sys.setrecursionlimit(100000)

def func(num,pr):
  global l,ans

  if len(l[num])!=0:
    for z in l[num]:
      if z[0]==pr:
        continue

      if pr==-1:
        ans[z[0]]=z[1]
      else:
        ans[z[0]]=ans[num]+z[1]
      func(z[0],num)

N=int(input())
l=[ [] for z in range(N)]
ans=[0]*N


for z in range(N-1):
  u,v,w=list(map(int,input().split(" ")))
  l[u-1].append((v-1,w))
  l[v-1].append((u-1,w))

func(0,-1)

#print(l)
#print(ans)


for z in ans:
  if z%2==0:
    print(1)
  else:
    print(0)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(100000)

def f(a):
  global R,G,B,N,ans,Rm,Gm,Bm

  for b in range(N-R*a+1):
    cmod=(N-R*a-G*b)%B
    c=(N-R*a-G*b)//B
    if cmod==0 and  c>=0:
      ans+=1
    elif c<0:
      break

  if a>0:
    f(a-1)


R,G,B,N=list(map(int,input().split(" ")))
Rm=N//R
Gm=N//G
Bm=N//B

ans=0

f(Rm)

print(ans)

Fixed submission:
import sys

sys.setrecursionlimit(100000)

def f(a):
  global R,G,B,N,ans,Rm,Gm,Bm

  for b in range(N-R*a+1):
    cmod=(N-R*a-G*b)%B
    c=(N-R*a-G*b)//B
    if cmod==0 and  c>=0:
      ans+=1
    elif c<0:
      break

  if a>0:
    f(a-1)


R,G,B,N=list(map(int,input().split(" ")))

ans=0

f(N//R+1)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(100000)

def f(a):
  global R,G,B,N,ans,Rm,Gm,Bm

  for b in range(N-R*a+1):
    cmod=(N-R*a-G*b)%B
    c=(N-R*a-G*b)//B
    if cmod==0 and  c>=0:
      ans+=1

  if a>0:
    f(a-1)


R,G,B,N=list(map(int,input().split(" ")))
Rm=N//R
Gm=N//G
Bm=N//B

ans=0

f(Rm)

print(ans)

Fixed submission:
import sys

sys.setrecursionlimit(100000)

def f(a):
  global R,G,B,N,ans,Rm,Gm,Bm

  for b in range(N-R*a+1):
    cmod=(N-R*a-G*b)%B
    c=(N-R*a-G*b)//B
    if cmod==0 and  c>=0:
      ans+=1
    elif c<0:
      break

  if a>0:
    f(a-1)


R,G,B,N=list(map(int,input().split(" ")))

ans=0

f(N//R+1)

print(ans)

Buggy python submission (verdict: Runtime Error):
def f(a):
  global R,G,B,N,ans,Rm,Gm,Bm

  for b in range(N-R*a+1):
    cmod=(N-R*a-G*b)%B
    c=(N-R*a-G*b)//B
    if cmod==0 and  c>=0:
      ans+=1

  if a>0:
    f(a-1)


R,G,B,N=list(map(int,input().split(" ")))
Rm=N//R
Gm=N//G
Bm=N//B

ans=0

f(Rm)

print(ans)

Fixed submission:
import sys

sys.setrecursionlimit(100000)

def f(a):
  global R,G,B,N,ans,Rm,Gm,Bm

  for b in range(N-R*a+1):
    cmod=(N-R*a-G*b)%B
    c=(N-R*a-G*b)//B
    if cmod==0 and  c>=0:
      ans+=1
    elif c<0:
      break

  if a>0:
    f(a-1)


R,G,B,N=list(map(int,input().split(" ")))

ans=0

f(N//R+1)

print(ans)
Buggy python submission (verdict: Runtime Error):
def f(a,b,ba):
  if a>0 and b>0:
    return 1 + f(a-1,b-1,ba)
  elif a>0 and ba>0:
    return 1 + f(a-1,b+1,ba-1)
  elif ba>0 and b>0:
    return 1 + f(a,b-1,ba-1)

  return 0

N=int(input())
a=[]
b=[]

stA=0
stB=0
stBA=0

ans=0
for z in range(N):
  a.append(input())

for z in a:
  buf=z.count("AB")
  b.append((z[0],z[-1],buf))
  ans+=buf

for z in b:
  if z[0]=="B" and z[1]=="A":
    stBA+=1
  elif z[0]=="B":
    stB+=1
  elif z[1]=="A":
    stA+=1

ans=ans+f(stA,stB,stBA)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

def f(a,b,ba):
  if a>0 and ba>0:
    return 1 + f(a,b,ba-1)
  elif a>0 and b>0:
    return 1+ f(a-1,b-1,ba)
  elif ba>1:
    return 1 + f(a+1,b,ba-2)
  elif ba>0 and b>0:
    return 1 + f(a,b-1,ba-1)

  return 0

N=int(input())
a=[]
b=[]

stA=0
stB=0
stBA=0

ans=0
for x in range(N):
  z=input()
  buf=z.count("AB")
  b.append((z[0],z[-1],buf))
  ans+=buf

for z in b:
  if z[0]=="B" and z[1]=="A":
    stBA+=1
  elif z[0]=="B":
    stB+=1
  elif z[1]=="A":
    stA+=1

ans=ans+f(stA,stB,stBA)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,Q=list(map(int,input().split(" ")))
s=input()
for z in range(0,Q):
  i=list(map(int,input().split(" ")))
  buf=s[i[0]-1:i[1]]
  print(buf.count("AC"))

Fixed submission:
N,Q=list(map(int,input().split(" ")))
s=input()

arr=[0]*N
for i,z in enumerate(arr):
  if i==0:
    continue
  if s[i-1]=="A" and s[i]=="C":
    arr[i]=arr[i-1]+1
  else:
    arr[i]=arr[i-1]
for z in range(0,Q):
  i=list(map(int,input().split(" ")))
  print(arr[i[1]-1]-arr[i[0]-1])
Buggy python submission (verdict: Time Limit Exceeded):
def f(s):
  r=0
  for i,x in enumerate(s):
    if len(s)==i+1:
      break
    if(x!=s[i+1]):
      del s[i]
      del s[i]
      r+=2
      break
  return r

s=input()
s=list(s)

r=0

while True:
  buf=f(s)
  r+=buf
  if buf==0:
    break

print(r)

Fixed submission:
s=input()

r=0
s0=0
s1=0

for x in s:
  if x=="0":
    if s1!=0:
      s1=s1-1
    else:
      s0=s0+1
  else:
    if s0!=0:
      s0=s0-1
    else:
      s1=s1+1


print(len(s)-s0-s1)
Buggy python submission (verdict: Runtime Error):
def f(k):
  global N,h,update
  if k==N:
    return -1
  if h[k]==0:
    return k
  else:
    h[k]-=1
    update=True
    f(k+1)


update=False
N=int(input())
h=list(map(int,input().split(" ")))
r=0
s=0
while True:
  if h[s]!=0:
    break
  s+=1

while True:
  f(s)
  if update:
    r+=1
    while s!=N-1 and h[s]==0:
      s+=1
    update=False
  else:
    break

print(r)

Fixed submission:
import sys

def f(k):
  global N,h,update
  if k==N:
    return -1
  if h[k]==0:
    return k
  else:
    h[k]-=1
    update=True
    f(k+1)


update=False
N=int(input())
h=list(map(int,input().split(" ")))
r=0
s=0

try:
  while True:
    if h[s]!=0:
      break
    s+=1
except Exception:
  print(0)
  sys.exit(0)

while True:
  f(s)
  if update:
    r+=1
    while s!=N-1 and h[s]==0:
      s+=1
    update=False
  else:
    break

print(r)
Buggy python submission (verdict: Time Limit Exceeded):
N,K=list(map(int,input().split(" ")))
A=list(map(int,input().split(" ")))
dp=[0]*N
for i in range(1,N):
  buf=10**100
  for j in range(1,K+1):
    if i-j<0:
      break
    buf=min((buf,dp[i-j]+abs(A[i-j]-A[i])))
  dp[i]=buf

print(dp[N-1])

Fixed submission:
N,K=list(map(int,input().split(" ")))
A=list(map(int,input().split(" ")))
dp=[0]*N
for i in range(1,N):
  buf=10**100
  for j in range(1,K+1):
    if i-j<0:
      break
    buf=min((buf,dp[i-j]+abs(A[i-j]-A[i])))
  dp[i]=buf
 
print(dp[N-1])
Buggy python submission (verdict: Time Limit Exceeded):
import heapq

mINF=-10**10

N,K=list(map(int,input().split(" ")))

arr=[mINF]*N
for z in range(N):
  heapq.heapreplace(arr,int(input()))

arr2=[mINF]*K
for z in range(K):
  heapq.heapreplace(arr2,heapq.heappop(arr))

r=max(arr2)-min(arr2)

try:
  while True:
    heapq.heapreplace(arr2,heapq.heappop(arr))
    rb=max(arr2)-min(arr2)
    if rb<r:
      r=rb
except Exception:
  pass

print(r)


Fixed submission:
import heapq

N,K=list(map(int,input().split(" ")))

arr=[0]*N
for z in range(N):
  heapq.heapreplace(arr,int(input()))

arr2=[0]*K
for z in range(K):
  arr2[z]=heapq.heappop(arr)

r=arr2[-1]-arr2[0]

z=0
try:
  while True:
    arr2[z]=heapq.heappop(arr)
    if z==K-1:
      zz=0
    else:
      zz=z+1

    rb=arr2[z]-arr2[zz]
    if rb<r:
      r=rb

    z+=1
    if z==K:
      z=0

except Exception:
  pass

print(r)
Buggy python submission (verdict: Runtime Error):
rt re
s=input()
m=re.match(r'A*KIHA*BA*RA*',s)
if m!=None:
    print("YES")
else:
    print("NO")

Fixed submission:
import re
s=input()
m=re.match(r'A?KIHA?BA?RA?$',s)
if m!=None:
    print("YES")
else:
    print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
num = [1 for _ in range(N + 1)]
for i in range(2, N + 1):
    cnt = 1
    while cnt * i <= N:
        num[cnt * i] += 1
        cnt += 1
ans = 0
for i in range(1, N + 1):
    ans += num[i] * i
print(ans)


Fixed submission:
N = int(input())
ans = 0
for i in range(1, N + 1):
    largest = N // i * i
    ans += ((largest + i) * (N // i)) // 2
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
reverse = False
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if not reverse:
        if f == 1:
            s = c + s
        else:
            s = s + c
    else:
        if f == 2:
            s = c + s
        else:
            s = s + c
if reverse:
    print(s[::-1])
else:
    print(s)


Fixed submission:
s = input()
q = int(input())
reverse = False
front = []
back = []
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if not reverse:
        if f == 1:
            front.append(c)
        else:
            back.append(c)
    else:
        if f == 2:
            front.append(c)
        else:
            back.append(c)
s = "".join(front[::-1]) + s + "".join(back)
if reverse:
    print(s[::-1])
else:
    print(s)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        s = s[::-1]
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if f == 1:
        s = c + s
    else:
        s = s + c
print(s)


Fixed submission:
s = input()
q = int(input())
reverse = False
front = []
back = []
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if not reverse:
        if f == 1:
            front.append(c)
        else:
            back.append(c)
    else:
        if f == 2:
            front.append(c)
        else:
            back.append(c)
s = "".join(front[::-1]) + s + "".join(back)
if reverse:
    print(s[::-1])
else:
    print(s)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s_list = {}
already = set()
for _ in range(N):
    s = input()
    if s in already:
        s_list[s] += 1
    else:
        s_list[s] = 0
    already.add(s)


max_kv_list = [kv for kv in s_list.items() if kv[1] == max(s_list.values())]
kv_list = []
for kv in max_kv_list:
    kv_list.append(kv[0])
kv_list.sort()
for kv in kv_list:
    print(kv)

Fixed submission:
N = int(input())
s_list = {}
already = set()
for _ in range(N):
    s = input()
    if s in already:
        s_list[s] += 1
    else:
        s_list[s] = 0
    already.add(s)

max_value = max(s_list.values())
max_kv_list = [kv for kv in s_list.items() if kv[1] == max_value]
kv_list = []
for kv in max_kv_list:
    kv_list.append(kv[0])
kv_list.sort()
for kv in kv_list:
    print(kv)
Buggy python submission (verdict: Runtime Error):
from scipy.misc import comb
N = int(input())
K = int(input())
n = len(str(N))
ans = comb(n - 1, K) * 9 ** K
for i in range(K):
    ans += comb(n - 1 - i, K - 1 - i) * 9 ** (K - 1 - i) * (int(str(N)[i]) - 1)
    ans += comb(n - 1 - i, K - 2 - i) * 9 ** (K - 1 - i) * (int(str(N)[i + 1]) - 1)

 
print(ans + 1)

Fixed submission:
def comb(a, b):
    ans = 1
    for i in range(b):
        ans *= (a - i)
    for i in range(2, b + 1):
        ans //= i
    return ans

def solve_k1(N):
    ans = 0
    N = str(N)
    len_N = len(N)
    ans += 9 * (len_N - 1)
    ans += int(N[0])
    return ans

def solve_k2(N):
    ans = 0
    N = str(N)
    len_N = len(N)
    if len_N < 2:
        return 0
    ans = comb((len_N - 1), 2) * 9 ** 2 + (int(N[0]) - 1) * ((len_N - 1) * 9) + solve_k1(int(N[1:]))
    return ans

def solve_k3(N):
    ans = 0
    N = str(N)
    len_N = len(N)
    if len_N < 3:
        return 0
    ans = comb(len_N - 1, 3) * 9 ** 3 + (int(N[0]) - 1) * comb(len_N - 1, 2) * 9 ** 2 + solve_k2(int(N[1:]))
    return ans

N = int(input())
K = int(input())
ans = 0
if K == 1:
    ans = solve_k1(N)
elif K == 2:
    ans = solve_k2(N)
else:
    ans = solve_k3(N)
print(ans)