Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input(),split())
print(a*b)

Fixed submission:
a,b = map(int,input().split())
print(a*b)
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
print(int(a*b))

Fixed submission:
from decimal import Decimal
a,b = input().split()
print(int(Decimal(a)*Decimal(b)))
Buggy python submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 
n = int(readline())
a = list(map(int,readline().split()))

b = [0]*(n+1)
b[-1] = a[-1]
for i in range(n-1,-1,-1):
  b[i] = b[i+1]+a[i]
#print(b)
c = [1]*(n+1)
for i in range(n):
  c[i+1] = (c[i]-a[i])*2
#print(c)
ans = 0
for i in range(n+1):
  m = min(b[i],c[i])
  if m < 0:
    ans = -1
    break
  ans += m
  
if n == 0:
  ans = 1
if a[0] != 0:
  ans = -1
print(ans)

Fixed submission:
a = list(map(int,input().split()))
print(a[2]*60+a[3]-a[0]*60-a[1]-a[4])
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)
from functools import lru_cache
from math import ceil as C
  
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    @lru_cache(None)
    def f(k):
      if k == 1:return d
      if k == 0:return 0
      return min(k*d,f(k//2)+a+k%2*d,f(C(k/2))+a+-k%2*d,f(k//3)+b+k%3*d,f(C(k/3))+b+-k%3*d,f(k//5)+c+k%5*d,f(C(k/5))+c+-k%5*d)
    print(f(n))

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

t = int(input())
  
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    @lru_cache(None)
    def f(k):
      if k == 1:return d
      if k == 0:return 0
      return min(k*d,f(k//5)+c+k%5*d,f(k//3)+b+k%3*d,f(k//2)+a+k%2*d,f((k+4)//5)+c+-k%5*d,f((k+2)//3)+b+-k%3*d,f((k+1)//2)+a+-k%2*d)
    print(f(n))
Buggy python submission (verdict: Runtime Error):
n = int(input())
c = 1
pp = []
mm = []
for i in range(n):
  a,b = map(int,input().split())
  if a*b == 0:
    c += 1
  else:
    pp.append(a/b)
    
c %= mod
mod = 10**9+7
from collections import Counter
p = Counter(pp)
r = 1
s = set()
for i in p.keys():
  if i in s:
    continue
  ans = p[i]
  chk = p[-1/i]
  r *= (pow(2,ans,mod)-1+pow(2,chk,mod))%mod
  r %= mod
  s.add(-1/i)
print((r*c-1)%mod)

Fixed submission:
n = int(input())
m = n%10
  
if m==3:
  print('bon')
elif m==0 or m==1or m==6or m==8:
  print('pon')
else:
  print('hon')
Buggy python submission (verdict: Runtime Error):
from collections import Counter
n = int(input())
pp = []
a0 = 0
b0 = 0
z = 0
for i in range(n):
  a,b = map(int,input().split())
  if a==0 and b==0:
    z += 1
  elif a==0:
    a0 += 1
  elif b==0:
    b0 += 1
  else:
    pp.append(round(a/b,16))
    
mod = 10**9+7
a0 %= mod
b0 %= mod
z %= mod
p = Counter(pp)
r = 1
s = set()

for i in p.keys():
  if i in s:
    continue
  ans = p[i]
  f = round(-1/i,16)
  chk = p[f]
  r *= (pow(2,ans,mod)+pow(2,chk,mod)-1)%mod
  r %= mod
  s.add(f)
r *= (pow(2,a0,mod)+pow(2,b0,mod)-1)%mod
r %= mod
print((r+z-1)%mod)

Fixed submission:
from collections import Counter
import math
n = int(input())
pp = []
a0 = 0
b0 = 0
z = 0

for i in range(n):
  a,b = map(int,input().split())
  if a==0 and b==0:
    z += 1
  elif a==0:
    a0 += 1
  elif b==0:
    b0 += 1
  else:
    gomi = math.gcd(a,b)
    if a < 0:
      a *= -1
      b *= -1
    pp.append((a//gomi,b//gomi))
    
mod = 10**9+7
a0 %= mod
b0 %= mod
z %= mod
p = Counter(pp)
r = 1
s = set()

for i,j in p.keys():
  if (i,j) in s:
    continue
  ans = p[(i,j)]
  if j < 0:
    f = (-j,i)
  else:
    f = (j,-i)
  chk = p[f]
  r *= (pow(2,ans,mod)+pow(2,chk,mod)-1)%mod
  r %= mod
  s.add(f)
r *= (pow(2,a0,mod)+pow(2,b0,mod)-1)%mod
r %= mod
print((r+z-1)%mod)
Buggy python submission (verdict: Runtime Error):
import numpy as np
n,m,x = map(int,input().split())
c = []
for i in range(n):
  a = [int(i) for i in input().split()]
  c.append(a)
  
c = np.array(c)
ans = float('inf')
for i in range(1,2**n):
  chk = [0]*m
  cost = 0
  for j in range(n):
    if (i>>j)&1 == 1:
      cost += c[j][0]
      chk += c[j][1:]
  if min(chk) >= x:
    ans = min(ans,cost)
  
if ans == float('inf'):
  ans = -1
print(ans)


Fixed submission:
s = input()
t = input()
if s == t[:len(t)-1]:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import sys
sys.setrecursionlimit(4100000)

n,m= map(int,readline().split())
a = list(map(int,readline().split()))

if a[0] == 1:
  print(1)
  exit()
  
d = [0]
def dfs(now,cc,d):
    if cc == m:
      return now
    else:
      next = a[now]
      next -= 1
      if not next in d:
        d.append(next)
        return dfs(next,cc+1,d)
      else:
        for i,di in enumerate(d[::-1]):
          if di == next:
            return (i+1,cc,now)

def f(now,cc):
    global mar
    if cc == mar:
      return now
    else:
      next = a[now]
      next -= 1
      return f(next,cc+1)
    
res = dfs(0,0,d)
if type(res) == int:
  print(res+1)
else:
  m -= res[1]
  mar = m % res[0]
  print(f(res[2],0)+1)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import sys
sys.setrecursionlimit(4100000)

n,m= map(int,readline().split())
a = list(map(int,readline().split()))

if a[0] == 1:
  print(1)
  exit()
  
d = set([0])
def dfs(now,cc,d):
    if cc == m:
      return now
    else:
      next = a[now]
      next -= 1
      if not next in d:
        d.add(next)
        return dfs(next,cc+1,d)
      else:
        d = list(d)
        for i,di in enumerate(d[::-1]):
          if di == next:
            return (i+1,cc,now)

def f(now,cc):
    global mar
    if cc == mar:
      return now
    else:
      next = a[now]
      next -= 1
      return f(next,cc+1)
    
res = dfs(0,0,d)
if type(res) == int:
  print(res+1)
else:
  m -= res[1]
  mar = m % res[0]
  print(f(res[2],0)+1)
Buggy python submission (verdict: Runtime Error):
x = int(input())
if x == 1:
  print(0,-1)
  exit()
  
def divisorize(fct):
    b, e = fct.pop()  # base, exponent
    pre_div = divisorize(fct) if fct else [[]]
    suf_div = [[(b, k)] for k in range(e + 1)]
    return [pre + suf for pre in pre_div for suf in suf_div]


def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def num(fct):
    a = 1
    for base, exponent in fct:
        a = a * base**exponent
    return a

c = []
fct = factorize(x)
for div in divisorize(fct):
    c.append(num(div))
    
chk = False
for s in c:
  if chk:
    break
  for i in range(1000):
    if (i+s)**5-i**5 > x:
      break
    if (i+s)**5-i**5 == x:
      ans = (i+s,i)
      chk = True
      break
  if s <1000:    
    for p in range(s,1000):
      j = -1*p
      #print(s,(j+s)**5-j**5)
      if (j+s)**5-j**5 > x:
        break 
      if (j+s)**5-j**5 == x:
        ans = (j+s,j)
        chk = True
        break 
  for ww in range(1,(s+1)//2):
    jj = -1*ww
    if (jj+s)**5-jj**5 > x:
      break    
    if (jj+s)**5-jj**5 == x:
      ans = (jj+s,jj)
      chk = True
      break 
print(*ans)


Fixed submission:
s = input()
if s == 'ABC':
  print('ARC')
else:
  print('ABC')

Buggy python submission (verdict: Runtime Error):
x = int(input())
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

c = factorization(x)
#print(c)
chk = False
for i in c:
  if chk:
    break
  s,t = i
  #print(s)
  for i in range(130):
    if (i+s)**5-i**5 > x:
      break
    if (i+s)**5-i**5 == x:
      ans = (i+s,i)
      chk = True
      break
  for i in range(1,100):
    j = -1*i
    #print(j+s,j)
    if (j+s)**5-j**5 > x:
      break 
    if (j+s)**5-j**5 == x:
      ans = (j+s,j)
      chk = True
      break 
      
print(*ans)


Fixed submission:
s = input()
if s == 'ABC':
  print('ARC')
else:
  print('ABC')
Buggy python submission (verdict: Time Limit Exceeded):
a,b,n = map(int,input().split())
p = (n+1)//b
c = p*b-1
if c < 0:
  print(a*n//b-a*(n//b))
  exit()
ans = 0
for i in range(c,n+1):
  ans = max(ans,a*i//b-a*(i//b))
print(ans)

Fixed submission:
a,b,n = map(int,input().split())
p = (n+1)//b
c = p*b-1
if c < 0:
  print(a*n//b-a*(n//b))
  exit()
ans = max(a*n//b-a*(n//b),a*c//b-a*(c//b))
print(ans)
Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split());mod=10**9+7;print((n*(n+1)*(n+2)//6-k*(k-1)*(3*n-2*k+4)//6+n-k+2)%mod

Fixed submission:
n,k = [int(i) for i in input().split()]
mod = 10**9+7
chk = 0
ans = 0
for i in range(n+1):
  chk += (n + i*n +1 -(i+1)*i)%mod
  chk %= mod
for i in range(k-1):
  ans += (n + i*n +1 -(i+1)*i)%mod
  ans %= mod
print((chk-ans)%mod)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in range(n)]
ab = [(x,y) for x,y in zip(a,b)]
from operator import itemgetter
ab = tuple(sorted(ab,key=itemgetter(0),reverse=True))
dp = [[-10**14]*(n+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
  for j in range(i+2):
    if j >= 1:
      dp[i+1][j] = max(dp[i][j]+ab[i][0]*abs(ab[i][1]-(n-1-(i-j))),dp[i][j-1]+ab[i][0]*abs(ab[i][1]+1-j))
    if j == 0:
      dp[i+1][0] = dp[i][0] + ab[i][0]*abs(ab[i][1]-(n-1-i))
print(max(dp[n]))

Fixed submission:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in range(n)]
ab = [(x,y) for x,y in zip(a,b)]
from operator import itemgetter
ab = tuple(sorted(ab,key=itemgetter(0),reverse=True))
dp = [[-10**14]*(n+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
  for j in range(i+2):
    if j >= 1:
      dp[i+1][j] = max(dp[i][j]+ab[i][0]*abs(ab[i][1]-(n-1-(i-j))),dp[i][j-1]+ab[i][0]*abs(ab[i][1]+1-j))
    if j == 0:
      dp[i+1][0] = dp[i][0] + ab[i][0]*abs(ab[i][1]-(n-1-i))
print(max(dp[n]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in range(n)]
ab = [(x,y) for x,y in zip(a,b)]
from operator import itemgetter
ab = sorted(ab,key=itemgetter(0),reverse=True)
dp = [[-10**14]*(n+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
  for j in range(i+2):
    if j >= 1:
      dp[i+1][j] = max(dp[i][j]+ab[i][0]*abs(ab[i][1]-(n-1-(i-j))),dp[i][j-1]+ab[i][0]*abs(ab[i][1]+1-j))
    if j == 0:
      dp[i+1][0] = dp[i][0] + ab[i][0]*abs(ab[i][1]-(n-1-i))
print(max(dp[n]))

Fixed submission:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in range(n)]
ab = [(x,y) for x,y in zip(a,b)]
from operator import itemgetter
ab = tuple(sorted(ab,key=itemgetter(0),reverse=True))
dp = [[-10**14]*(n+1) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
  for j in range(i+2):
    if j >= 1:
      dp[i+1][j] = max(dp[i][j]+ab[i][0]*abs(ab[i][1]-(n-1-(i-j))),dp[i][j-1]+ab[i][0]*abs(ab[i][1]+1-j))
    if j == 0:
      dp[i+1][0] = dp[i][0] + ab[i][0]*abs(ab[i][1]-(n-1-i))
print(max(dp[n]))
Buggy python submission (verdict: Time Limit Exceeded):
k = int(input())
ans = 0
import math
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      f = math.gcd(i,j)
      ff = math.gcd(f,l)
      ans += ff
print(ans)


Fixed submission:
k = int(input())
ans = 0
import math
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      f = math.gcd(i,j)
      ff = math.gcd(f,l)
      ans += ff
print(ans)

Buggy python submission (verdict: Runtime Error):
k = int(input())
ans = 0
import math
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      f = gcd(i,j)
      ff = gcd(f,l)
      ans += ff
print(ans)

Fixed submission:
k = int(input())
ans = 0
import math
for i in range(1,k+1):
  for j in range(1,k+1):
    for l in range(1,k+1):
      f = math.gcd(i,j)
      ff = math.gcd(f,l)
      ans += ff
print(ans)
Buggy python submission (verdict: Runtime Error):
x,y,a,b,c  = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
p = sorted(p,reverse=True)
q = sorted(q,reverse=True)
r = sorted(r,reverse=True)
chk = p[:x]+q[:y]
chk = sorted(chk,reverse=True)
for i in range(c):
  if r[i] > chk[-1+i]:
    chk[-1+i] = r[i] 
print(sum(chk))

Fixed submission:
x,y,a,b,c  = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]
p = sorted(p,reverse=True)
q = sorted(q,reverse=True)
r = sorted(r,reverse=True)
chk = p[:x]+q[:y]
chk = sorted(chk,reverse=True)
for i in range(c):
  if i < len(chk) and r[i] > chk[-(1+i)]:
    chk[-(1+i)] = r[i]   
print(sum(chk))
Buggy python submission (verdict: Runtime Error):
n,m = [int(i) for i in input().split()]
if n <= 1:
  a = 0
else:
  a = n*(n-1)/2
if m <= 1:
  b = 0
else:
  b = m*(m-1)/2
print(ab)

Fixed submission:
n,m = [int(i) for i in input().split()]
if n <= 1:
  a = 0
else:
  a = n*(n-1)/2
if m <= 1:
  b = 0
else:
  b = m*(m-1)/2
print(int(a+b))
Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  #import collections
  #c = collections.Counter
  j = 0
  for i in range(len(s)):
    j += int(a.count(s[i])*(a.count(s[i])-1)/2)
  #print(j)
  for i in range(n):
    if a.count(a[i]) >= 2:
      print(int(j-(a.count(a[i])-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()

Fixed submission:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  import collections
  c = collections.Counter(a)
  j = 0
  #print(c[a[0]])
  for i in range(len(s)):
    j += int(c[s[i]]*(c[s[i]]-1)/2)
  #print(j)
  for i in range(n):
    if c[a[i]] >= 2:
      print(int(j-(c[a[i]]-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(i) for i in input().split()]
Ss = set(a)
s = list(Ss)
#import collections
#c = collections.Counter
j = 0
for i in range(len(s)):
  j += int(a.count(s[i])*(a.count(s[i])-1)/2)
#print(j)
for i in range(n):
  if a.count(a[i]) >= 2:
    print(int(j-(a.count(a[i])-1)))
  else:
    print(j)

Fixed submission:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  import collections
  c = collections.Counter(a)
  j = 0
  #print(c[a[0]])
  for i in range(len(s)):
    j += int(c[s[i]]*(c[s[i]]-1)/2)
  #print(j)
  for i in range(n):
    if c[a[i]] >= 2:
      print(int(j-(c[a[i]]-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(i) for i in input().split()]
Ss = set(a)
s = list(Ss)
#import collections
#c = collections.Counter
j = 0
for i in range(len(s)):
  j += int(a.count(s[i])*(a.count(s[i])-1)/2)
#print(j)
for i in range(n):
  if a.count(a[i]) >= 2:
    print(int(j-(a.count(a[i])-1)))
  else:
    print(j)

Fixed submission:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = [int(i) for i in input().split()]
  Ss = set(a)
  s = list(Ss)
  import collections
  c = collections.Counter(a)
  j = 0
  #print(c[a[0]])
  for i in range(len(s)):
    j += int(c[s[i]]*(c[s[i]]-1)/2)
  #print(j)
  for i in range(n):
    if c[a[i]] >= 2:
      print(int(j-(c[a[i]]-1)))
    else:
      print(j)
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n-1):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
      #print(b)
    a = copy.deepcopy(b)
    #print(a)

  print(b[0])
if __name__ == '__main__':
    main()

Fixed submission:
h,w = [int(i) for i in input().split()]
s = []
for i in range(h):
  array = list(input())
  s.append(array)
d = [[float("inf")] * (w) for i in range(h)]
d[0][0]=0
if s[0][0]=='#':
  d[0][0]=1
for i in range(1,w):
  if s[0][i]!=s[0][i-1] and s[0][i]!=s[0][0]:
    d[0][i]=d[0][i-1]+1
  else:
    d[0][i]=d[0][i-1]
for i in range(1,h):
  for j in range(w):
    k=0
    u=0
    if j != 0 :
      if s[i][j]=="#" and s[i][j]!=s[i-1][j]:
        k=1
      if s[i][j]=="#" and s[i][j]!=s[i][j-1]:
        u=1
      d[i][j] = min(d[i-1][j]+k,d[i][j-1]+u)
    elif j == 0 :
      if s[i][0]=="#" and s[i][0]!=s[i-1][0]:
        k=1
      d[i][0] = d[i-1][0]+k

print(d[h-1][w-1])
Buggy python submission (verdict: Runtime Error):
k = int(i) for i in input().split()
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(l[k])

Fixed submission:
k = int(input())
l = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
print(l[k-1])
Buggy python submission (verdict: Time Limit Exceeded):
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod) 
  
def comb(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

n,k = [int(i) for i in input().split()]
mod = 10**9+7
ans = 0

for i in range(min(n,k+1)):
  ans += (comb(n,i,mod)*comb(n-1,i,mod)%mod)
  ans %= mod
  
print(ans)

Fixed submission:
def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

n,k = [int(i) for i in input().split()]
mod = 10**9+7
fact, fact_inv = make_array_for_comb(n,mod)
ans = 0

for i in range(min(n,k+1)):
  ans += (comb(n,i,mod)*comb(n-1,i,mod)%mod)
  ans %= mod
  
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
  h,n = [int(i) for i in input().split()]
  ab = []
  for i in range(n):
    a,b = [int(i) for i in input().split()]
    ab.append([a,b])
  dp = [[10**8]*((h+1)+10**4) for i in range(n+1)]
  dp[0][0]=0
  for i in range(n):
    for j in range(h+1+10**4):
      if j>=ab[i][0]:
        dp[i+1][j]=min(dp[i][j],dp[i+1][j-ab[i][0]]+ab[i][1])
      else:
        dp[i+1][j]=dp[i][j]

  ans = 10**8    
  for i in range(n+1):
    for j in range(h,h+1+10**4):
      ans = min(ans,dp[i][j])
  print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
h,n = [int(i) for i in input().split()]
ab = []
for i in range(n):
  a,b = [int(i) for i in input().split()]
  ab.append([a,b])
dp = [[10**8]*((h+1)+10**4) for i in range(n+1)]
dp[0][0]=0
for i in range(n):
  for j in range(h+1+10**4):
    if j>=ab[i][0]:
      dp[i+1][j]=min(dp[i][j],dp[i+1][j-ab[i][0]]+ab[i][1])
    else:
      dp[i+1][j]=dp[i][j]
      
ans = 10**8    
for i in range(n+1):
  for j in range(h,h+1+10**4):
    ans = min(ans,dp[i][j])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

def all_bit_count(n):
    c = (n & 0x5555555555555555) + ((n>>1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c>>2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c>>4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c>>8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c>>16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c>>32) & 0x00000000ffffffff)
    return c

n = int(readline())
g = [[] * n for i in range(n)]
for i in range(n-1):
  u, v = map(int,readline().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)
  
m = int(readline())
c = []
for i in range(m):
  u, v = map(int,readline().split())
  u -= 1
  v -= 1  
  que = deque()
  que.append((u,set()))
  chk = [False]*n
  chk[u] = True
  while que:
      now,L = que.popleft()
      if now == v:
        c.append(L)
        break
      for i in g[now]:
          if not chk[i]:
            chk[i] = True
            mi = min(now,i)
            ma = max(now,i)
            que.append((i,L|{(mi,ma)}))
                     
import itertools
ans = 1<<(n-1)
for i in range(1,m+1):
  for j in itertools.combinations(c,i):
    K = set()
    for k in j:
      K |= k
    l = len(K)
    ans += pow(-1,i)*pow(2,n-1-l)
    
print(ans)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

n = int(readline())
g = [[] * n for i in range(n)]
for i in range(n-1):
  u, v = map(int,readline().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)
  
m = int(readline())
c = []
for i in range(m):
  u, v = map(int,readline().split())
  u -= 1
  v -= 1  
  que = deque()
  que.append((u,set()))
  chk = [False]*n
  chk[u] = True
  while que:
      now,L = que.popleft()
      if now == v:
        c.append(L)
        break
      for i in g[now]:
          if not chk[i]:
            chk[i] = True
            mi = min(now,i)
            ma = max(now,i)
            que.append((i,L|{mi*100+ma}))
                     
import itertools
#print(c)
ans = 1<<(n-1)
for i in range(1,m+1):
  for j in itertools.combinations(c,i):
    K = set()
    for k in j:
      K |= k
    l = len(K)
    ans += pow(-1,i)*pow(2,n-1-l)
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
a = sorted(list(set(a)))
from fractions import gcd
lc = a[0]
if n != 1:
  for num,i in enumerate(a[1:]):
    if lc == 0:
      break
    lc = lc * i // gcd(lc, i)
    if gcd(lc, i) == min(i,lc):
      for j in a[:num+1]:
        z = i//j
        if z == i/j and z%2 == 0:
          lc = 0
          break 
if lc == 0:
  print(0)
else:
  print((m+lc//2)//lc)


Fixed submission:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
a = sorted(list(set(a)))
from fractions import gcd
lc = a[0]
if n != 1:
  for num,i in enumerate(a[1:]):
    if lc == 0:
      break
    lc = lc * i // gcd(lc, i)
    if gcd(lc, i) == min(i,lc):
      for j in a[:num+1]:
        if (i/j)%2 == 0:
          lc = 0
          break 
if lc == 0:
  print(0)
else:
  print((m+lc//2)//lc)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
a = sorted(list(set(a)))
from fractions import gcd
lc = a[0]
if n != 1:
  for num,i in enumerate(a[1:]):
    if lc == 0:
      break
    lc = lc * i // gcd(lc, i)
    if gcd(lc, i) == min(i,lc):
      for j in a[:num+1]:
        z = i//j
        if z == i/j and z%2 == 0:
          lc = 0
          break 
if lc == 0:
  print(0)
else:
  print((m+lc//2)//lc)

Fixed submission:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
a = [int(i) for i in input().split()]
a = sorted(list(set(a)))
from fractions import gcd
lc = a[0]
if n != 1:
  for num,i in enumerate(a[1:]):
    if lc == 0:
      break
    lc = lc * i // gcd(lc, i)
    if gcd(lc, i) == min(i,lc):
      for j in a[:num+1]:
        if (i/j)%2 == 0:
          lc = 0
          break 
if lc == 0:
  print(0)
else:
  print((m+lc//2)//lc)