Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, A, B, C = map(int, input().split())
ABC = [A, B, C]
if ABC == [0, 0, 0]:
  print("No")
  quit()

p = [0] * N
for i in range(N):
  p[i] = str(input())
  
s = [0] * N
for i in range(N):
  x = p[i]
  if x == "AB":
    if A > B:
      A, B = A - 1, B + 1
      s[i] = "B"
    elif B > A:
      A, B = A + 1, B - 1
      s[1] = "A"
    else:
      if A == 0:
        A, B = A + 1, B - 1
        s[1] = "A"  
      elif B == 0:
        A, B = A - 1, B + 1
        s[1] = "B"
      else:
        A, B = A - 1, B + 1
        s[1] = "B"
  elif x == "BC":
    if B > C:
      B, C = B - 1, C + 1
      s[i] = "C"
    elif B > A:
      B, C = B + 1, C - 1
      s[1] = "B"
  else:
    if C > A:
      C, A = C - 1, A + 1
      s[i] = "A"
    elif B > A:
      C, A = C + 1, A - 1
      s[1] = "C"    
  
print("Yes")  
for i in range(N):
  print(s[i])
  
    
    
    

Fixed submission:
N, A, B, C = map(int, input().split())
ABC = [A, B, C]

s = [0] * N
for i in range(N):
  s[i] = str(input())
  
d = {"AB":(0, 1, 2), "BC": (2, 1, 0), "AC": (0, 2, 1)}  
dl = ["A", "B", "C"]  

if ABC == [0, 0, 0]:
  print("No")
  quit()

ans = [0] * N  
if A + B + C == 1:
  for n in range(N):
    i, j, k = d[s[n]]
    #print(i, j, k)
    if ABC[i] == 1:
      ABC[i] = 0
      ABC[j] = 1
      ans[n] = dl[j]
    elif ABC[j] == 1:
      ABC[i] = 1
      ABC[j] = 0
      ans[n] = dl[i]
    else:
      print("No")
      quit()
  print("Yes")
  for i in range(N):
    print(ans[i])
    
else:
  for n in range(N):
    i, j, k = d[s[n]]
    #print(i, j, k)
    if ABC[i] == 0:
      if ABC[j] == 0:
        print("No")
        quit()
      else:
        ABC[i] += 1
        ABC[j] -= 1
        ans[n] = dl[i]
    elif ABC[j] == 0:
      ABC[i] -= 1
      ABC[j] += 1
      ans[n] = dl[j]
    else:
      if n < N - 1:
        if (ABC[i] == 1) or (ABC[j] == 1):
          if s[n] != s[n + 1]:
            n1, n2, n3 = d[s[n + 1]]
            if (i == n1) or (i == n2):
              ABC[i] += 1
              ABC[j] -= 1
              ans[n] = dl[i]
            else:
              ABC[i] -= 1
              ABC[j] += 1
              ans[n] = dl[j]
              
          else:
            ABC[i] -= 1
            ABC[j] += 1
            ans[n] = dl[j]
        else:
          ABC[i] -= 1
          ABC[j] += 1
          ans[n] = dl[j]
      else:
        ABC[i] -= 1
        ABC[j] += 1
        ans[n] = dl[j]
        
  print("Yes")
  for i in range(N):
    print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
S = str(input())
N = len(S)
ans1 = [0] * 2019
ans2 = 0

T = [0] *  (N + 1)
T[N] = 0
ans1[0] += 1

for i in range(N - 1, -1, -1):
  #print(i)
  T[i] = (T[i + 1] + 10 ** (N - i - 1) * int(S[i])) % 2019
  ans1[T[i]] += 1

for i in range(2019):
  x = ans1[i]
  ans2 += x * (x - 1) / 2
  
#print(ans1[0])
#print(T)
print(int(ans2))  



Fixed submission:
S = str(input())
N = len(S)
ans1 = [0] * 2019
ans2 = 0

T = [0] *  (N + 1)
T[N] = 0
ans1[0] += 1

P = 1
for i in range(N - 1, -1, -1):
  #print(i)
  T[i] = (T[i + 1] + P * int(S[i])) % 2019
  ans1[T[i]] += 1
  P = 10 * P % 2019

for i in range(2019):
  x = ans1[i]
  ans2 += x * (x - 1) / 2
  
#print(ans1[0])
#print(T)
print(int(ans2))

Buggy python submission (verdict: Time Limit Exceeded):
S = str(input())
N = len(S)
ans1 = [0] * 2019
ans2 = 0

T = [0] *  (N + 1)
T[N] = 0
ans1[0] += 1

for i in range(N - 1, -1, -1):
  #print(i)
  T[i] = (T[i + 1] + 10 ** (N - i - 1) * int(S[i])) % 2019
  ans1[T[i]] += 1

for i in range(2019):
  x = ans1[i]
  ans2 += x * (x - 1) / 2
  
#print(ans1[0])
#print(T)
print(int(ans2))  



Fixed submission:
S = str(input())
N = len(S)
ans1 = [0] * 2019
ans2 = 0

T = [0] *  (N + 1)
T[N] = 0
ans1[0] += 1

P = 1
for i in range(N - 1, -1, -1):
  #print(i)
  T[i] = (T[i + 1] + P * int(S[i])) % 2019
  ans1[T[i]] += 1
  P = 10 * P % 2019

for i in range(2019):
  x = ans1[i]
  ans2 += x * (x - 1) / 2
  
#print(ans1[0])
#print(T)
print(int(ans2))
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
K = int(input())

def gcd3(a, b, c):
  x = gcd(a, b)
  y = gcd(x, c)
  return y

ans = 0
#count = 0
for i in range(1, K + 1):
  for j in range(i, K + 1):
    for k in range(j, K + 1):
      #count += 1
      #print(count, gcd3(i, j, k))
      x = gcd3(i, j, k)
      if i == j:
        if j == k:
          ans += x
        else:
          ans += 3 * x
      else:
        if i == k:
          ans += 3 * x
        elif j == k:
          ans += 3 * x
        else:
          ans += 6 * x
          

print(ans)  


Fixed submission:
import math
from functools import reduce
K = int(input())

def gcd(*numbers):
  return reduce(math.gcd, numbers)


ans = 0
#count = 0
for i in range(1, K + 1):
  for j in range(i, K + 1):
    for k in range(j, K + 1):
      #count += 1
      #print(count, gcd3(i, j, k))
      x = gcd(i, j, k)
      if i == j:
        if j == k:
          ans += x
        else:
          ans += 3 * x
      else:
        if i == k:
          ans += 3 * x
        elif j == k:
          ans += 3 * x
        else:
          ans += 6 * x
          

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
K = int(input())

def gcd3(a, b, c):
  x = gcd(a, b)
  y = gcd(x, c)
  return y

ans = 0
#count = 0
for i in range(1, K + 1):
  for j in range(1, K + 1):
    for k in range(1, K + 1):
      #count += 1
      #print(count, gcd3(i, j, k))
      ans += gcd3(i, j, k)

print(ans)  

Fixed submission:
import math
from functools import reduce
K = int(input())

def gcd(*numbers):
  return reduce(math.gcd, numbers)


ans = 0
#count = 0
for i in range(1, K + 1):
  for j in range(i, K + 1):
    for k in range(j, K + 1):
      #count += 1
      #print(count, gcd3(i, j, k))
      x = gcd(i, j, k)
      if i == j:
        if j == k:
          ans += x
        else:
          ans += 3 * x
      else:
        if i == k:
          ans += 3 * x
        elif j == k:
          ans += 3 * x
        else:
          ans += 6 * x
          

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = str(input())
RGB = [0] * 3
for i in range(N):
  if S[i] == "R":
    RGB[0] += 1
  elif S[i] == "G":
    RGB[1] += 1
  else:
    RGB[2] += 1
    
ans = RGB[0] * RGB[1] * RGB[2]

for i in range(1, 2000):
  for j in range(1, N + 1):
    if (j + 2 * i) <= N:
      if S[j - 1] != S[j + i - 1]:
        if S[j + 2 * i - 1] != S[j + i - 1]:
          if S[j - 1] != S[j + 2 * i - 1]:
            ans -= 1
            
print(max(ans, 0))
  


Fixed submission:
N = int(input())
S = str(input())
R, G, B = 0, 0, 0
for i in range(N):
  if S[i] == "R":
    R += 1
  elif S[i] == "G":
    G += 1
  else:
    B += 1
    
ans = R * G * B 

for i in range(1, 2000):
  for j in range(1, N - 2 * i + 1):
    if S[j - 1] != S[j + i - 1]:
      if S[j + 2 * i - 1] != S[j + i - 1]:
        if S[j - 1] != S[j + 2 * i - 1]:
          ans -= 1
            
print(max(ans, 0))

Buggy python submission (verdict: Runtime Error):
N = int(input())
S = str(input())
RGB = [0] * 3
for i in range(N):
  if S[i] == "R":
    RGB[0] += 1
  elif S[i] == "G":
    RGB[1] += 1:
  else:
    RGB[2] += 1
    
ans = RGB[0] * RGB[1] * RGB[2]

for i in range(1, 2000):
  for j in range(1, N + 1):
    if (j + 2 * i) <= N:
      if S[j - 1] != S[j + i - 1]:
        if S[j + 2 * i - 1] != S[j + i - 1]:
          if S[j - 1] != S[j + 2 * i - 1]:
            ans -= 1
            
print(max(ans, 0))
  

Fixed submission:
N = int(input())
S = str(input())
R, G, B = 0, 0, 0
for i in range(N):
  if S[i] == "R":
    R += 1
  elif S[i] == "G":
    G += 1
  else:
    B += 1
    
ans = R * G * B 

for i in range(1, 2000):
  for j in range(1, N - 2 * i + 1):
    if S[j - 1] != S[j + i - 1]:
      if S[j + 2 * i - 1] != S[j + i - 1]:
        if S[j - 1] != S[j + 2 * i - 1]:
          ans -= 1
            
print(max(ans, 0))
Buggy python submission (verdict: Runtime Error):
N, K, C = map(int, input().split())
S = str(input())

#前から貪欲に
x = 0
L = [-1] * N
p = 0
while x < N:
  if S[x] == "o":
    L[p] = x
    p += 1
    x += C + 1
  else:
    x += 1

#後ろから貪欲に    
y = N - 1
R = [-1] * N
q = 0
while y > -1:
  if S[y] == "o":
    R[q] = y
    q += 1
    y -= C + 1
  else:
    y -= 1
    
#print(L, R)    
L_1 = L.index(-1)
L = L[:L_1]
R = R[:L_1]
N = len(L)

#LとRの被っている数に1足したものを出力
for i in range(N):
  if L[i] == R[N - i - 1]:
    print(L[i] + 1)




    
  
  


Fixed submission:
N, K, C = map(int, input().split())
S = str(input())

#前から貪欲に
x = 0
L = [-1] * K
p = 0
while (x < N) and (p != K):
  if S[x] == "o":
    L[p] = x
    p += 1
    x += C + 1
  else:
    x += 1

#後ろから貪欲に    
y = N - 1
R = [-1] * K
q = 0
while (y > -1) and(q != K):
  if S[y] == "o":
    R[q] = y
    q += 1
    y -= C + 1
  else:
    y -= 1
    
#print(L, R)

#LとRの被っている数に1足したものを出力
for i in range(K):
  if L[i] == R[K - i - 1]:
    print(L[i] + 1)
Buggy python submission (verdict: Time Limit Exceeded):
X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

PR = sorted(P + R, reverse = True)
QR = sorted(Q + R, reverse = True)

ans = 0
for i in range(X):
  ans += PR[i]
  if PR[i] in R:
    if PR[i] in QR:
      QR.remove(PR[i])
#print(ans)
for i in range(Y):
  ans += QR[i]
  
print(ans)


Fixed submission:
X, Y, A, B, C = map(int, input().split())
P = sorted(list(map(int, input().split())), reverse = True)
Q = sorted(list(map(int, input().split())), reverse = True)
R = list(map(int, input().split()))

P = P[:X]
Q = Q[:Y]

D = P + Q + R
D = sorted(D, reverse = True)

print(sum(D[:(X + Y)]))
Buggy python submission (verdict: Time Limit Exceeded):
H, W, K = map(int, input().split())
HW = [[0] * W for i in range(H)]
S = [0] * H
for i in range(H):
  x = str(input())
  for j in range(len(x)):
    HW[i][j] = int(x[j])
#print(HW) 

for i in range(H):
  S[i] = sum(HW[i])
#print(S)  

ans = H + W
for i in range(2 ** (H - 1)):#521 * 9
  time = 0
  now = 0
  cut = []
  for j in range(H - 1):
    if (i >> j) & 1:#j番目とj+1番目の間で割る
      cut.append(j)
  #print(cut)
  cut2 = [[] for _ in range(len(cut) + 1)]
  for j in range(len(cut)):
    for p in range(now, cut[j] + 1):
      cut2[j].append(p)
      now = cut[j] + 1
  for p in range(now, H):
    cut2[-1].append(p)
  #print(cut2)    
    
  time = len(cut2) - 1  
  choco = [0] * len(cut2)
  for l in range(W):
    for k in range(len(cut2)):
      for p in cut2[k]:
        choco[k] += HW[p][l]
    if max(choco) > K:
      time += 1
      for k in range(len(cut2)):
        choco[k] = 0
      for k in range(len(cut2)):
        for p in cut2[k]:
          choco[k] += HW[p][l]
        
        
  ans = min(ans, time)
  #print(i, time)
  
print(ans)     
      
    

Fixed submission:
H, W, K = map(int, input().split())
HW = [[0] * W for i in range(H)]
S = [0] * H
for i in range(H):
  x = str(input())
  for j in range(len(x)):
    HW[i][j] = int(x[j])
#print(HW) 

for i in range(H):
  S[i] = sum(HW[i])
#print(S)  

ans = H + W
for i in range(2 ** (H - 1)):#521 * 9
  time = 0
  now = 0
  cut = []
  for j in range(H - 1):
    if (i >> j) & 1:#j番目とj+1番目の間で割る
      cut.append(j)
  #print(cut)
  cut2 = [[] for _ in range(len(cut) + 1)]
  for j in range(len(cut)):
    for p in range(now, cut[j] + 1):
      cut2[j].append(p)
      now = cut[j] + 1
  for p in range(now, H):
    cut2[-1].append(p)
  #print(cut2)    
    
  time = len(cut2) - 1  
  choco = [0] * len(cut2)
  for l in range(W):
    for k in range(len(cut2)):
      nn = 0
      for p in cut2[k]:
        nn += HW[p][l]
      if nn > K:  
        time += 10000
      else:
        choco[k] += nn
      

    if max(choco) > K: 
      time += 1
      for k in range(len(cut2)):
        nn = 0
        for p in cut2[k]:
          nn += HW[p][l]
        choco[k] = nn  

  ans = min(ans, time)
  #print(i, time)
  
print(ans)
Buggy python submission (verdict: Runtime Error):
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    s_h += 1
  else:
    if int(q[2]) == 1:#先頭
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

print(S)

Fixed submission:
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
s_h = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    s_h += 1
  else:
    if int(q[2]) == 1:#先頭
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

odd = ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 1) and (str(st[a]) != "0")]
odd = "".join(odd)
even =  ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 0) and (str(st[a]) != "0")]
even = "".join(even)[::-1]
if s_h % 2 == 1:
  S = S[::-1]

b = [even, S, odd]
#print(S,odd,even)
print("".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
S = str(input())
Q = int(input())
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    S = S[::-1]
  else:
    if int(q[2]) == 1:#先頭
      t = [q[4], S]
      S = "".join(t)
    else:
      t = [S, q[4]]
      S = "".join(t)
print(S)

Fixed submission:
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
s_h = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    s_h += 1
  else:
    if int(q[2]) == 1:#先頭
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

odd = ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 1) and (str(st[a]) != "0")]
odd = "".join(odd)
even =  ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 0) and (str(st[a]) != "0")]
even = "".join(even)[::-1]
if s_h % 2 == 1:
  S = S[::-1]

b = [even, S, odd]
#print(S,odd,even)
print("".join(b))

Buggy python submission (verdict: Time Limit Exceeded):
S = str(input())
Q = int(input())
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    S = S[::-1]
  else:
    if int(q[2]) == 1:#先頭
      S = q[4] + S
    else:
      S = S + q[4]
      
print(S)

Fixed submission:
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
s_h = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    s_h += 1
  else:
    if int(q[2]) == 1:#先頭
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

odd = ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 1) and (str(st[a]) != "0")]
odd = "".join(odd)
even =  ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 0) and (str(st[a]) != "0")]
even = "".join(even)[::-1]
if s_h % 2 == 1:
  S = S[::-1]

b = [even, S, odd]
#print(S,odd,even)
print("".join(b))
Buggy python submission (verdict: Time Limit Exceeded):
import math
n, a, b = map(int, input().split())
p = 10 ** 9 + 7
#ans = 2 ** n - 1 - N C a - N C b

def modpow(a, n, MOD):
  res = 1
  while (n > 0):
    if (n & 1):
      res = res * a % MOD
    a = a * a % MOD
    n >>= 1
  return res

def cmb(n, r, p):
  A = 1
  for i in range(r):
    A *= (n - i)
    A *= modpow(i + 1, p - 2, p)
    A %= p
  return A

ans = (modpow(2, n, p) - 1) % p
ans -= cmb(n, a, p) + cmb(n, b, p)

print(ans % p)







Fixed submission:
import math
n, a, b = map(int, input().split())
p = 10 ** 9 + 7
#ans = 2 ** n - 1 - N C a - N C b

def modpow(a, n, MOD):
  res = 1
  while (n > 0):
    if (n & 1):
      res = res * a % MOD
    a = a * a % MOD
    n >>= 1
  return res

def cmb(n, r, p):
  A = 1
  for i in range(r):
    A *= (n - i)
    A *= modpow(i + 1, p - 2, p)
    A %= p
  return A

ans = (modpow(2, n, p) - 1) % p
ans -= cmb(n, a, p) + cmb(n, b, p)

print(ans % p)

Buggy python submission (verdict: Runtime Error):
import math
N, a, b = map(int, input().split())
MOD = 10 ** 9 + 7

#def combinations_count(n, r):
#    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
#print(combinations_count(4, 2))
# 6
ans = 0
X = 1
for i in range(1, N + 1):
  X = int(X * (N + 1 - i) / i)
  if i != a:
    if i != b:
      ans += X
      
print(ans)

Fixed submission:
import math
n, a, b = map(int, input().split())
p = 10 ** 9 + 7
#ans = 2 ** n - 1 - N C a - N C b

def modpow(a, n, MOD):
  res = 1
  while (n > 0):
    if (n & 1):
      res = res * a % MOD
    a = a * a % MOD
    n >>= 1
  return res

def cmb(n, r, p):
  A = 1
  for i in range(r):
    A *= (n - i)
    A *= modpow(i + 1, p - 2, p)
    A %= p
  return A

ans = (modpow(2, n, p) - 1) % p
ans -= cmb(n, a, p) + cmb(n, b, p)

print(ans % p)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = [0] * N
time = [1] * N
j = 0
for i in range(N):
  inp = str(input())
  if inp in s:
    x = s.index(inp)
    time[x] += 1
  else:
    s[j] = inp
    j += 1
    
ans = max(time)
#print(s, time)
ans_list = []
for i in range(N):
  if time[i] == ans:
    if s[i] != 0:
      ans_list.append(s[i])
      
ans_list = sorted(ans_list)
for i in range(len(ans_list)):
  print(ans_list[i])

Fixed submission:
import collections
N = int(input())
s = [0] * N
for i in range(N):
  s[i] = str(input())
  
#print(s)
t = collections.Counter(s)
u = list(t.items())
#print(u)
x = max(list(t.values()))
ans = []
#print(x)
#print(u)
for i in range(len(u)):
  #print(u[i][1], x)
  if u[i][1] == x:
    ans.append(u[i][0])
    
    
ans = sorted(ans)

for i in range(len(ans)):
  print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
p = list(map(int, input().split()))

ans = 0
for i in range(N - K + 1):
  x = (sum(p[i : K + i]) + K) / 2
  #print(x)
  ans = max(x, ans)
  
print(ans)

Fixed submission:
N, K = map(int, input().split())
p = list(map(int, input().split()))

ans = 0
for i in range(N - K + 1):
  if i == 0:
    xe = sum(p[0 : K])
  else:
    xe = xe - p[i - 1] + p[i + K -1]
  x = (xe + K) / 2
  #print(x)
  ans = max(x, ans)
  
print(ans)
Buggy python submission (verdict: Runtime Error):
H = int(input())

bin_h = str(bin(H)[2:])
len_h = len(bin_h)
ans = 2 ** len_h - 1

if bin_h[1] == 1:
  ans += 2 ** (len_h - 1)
  
print(ans)
  

Fixed submission:
H = int(input())

if H == 1:
  print("1")
  quit()

bin_h = str(bin(H)[2:])
len_h = len(bin_h)
ans = 2 ** len_h - 1

if bin_h[1] == 1:
  ans += 2 ** (len_h - 1)
  
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(N):
  a, b = list(map(int, input().split()))
  A[i] = a
  B[i] = b
  
#モンスターの体力を i 減らすため消耗する魔力の最小値  
DP = [float("inf")] * (H + max(A) + 1)
DP[0] = 0  
for i in range(len(DP)):
  for j in range(N):
    if i - A[j] >= 0:
      DP[i] = min(DP[i], DP[i - A[j]] + B[j])
#print(DP) 
print(min(DP[H:H + max(A) + 1]))
  

Fixed submission:
H, N = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(N):
  a, b = list(map(int, input().split()))
  A[i] = a
  B[i] = b
  
#モンスターの体力を i 減らすため消耗する魔力の最小値  
DP = [float("inf")] * (H + max(A) + 1)
DP[0] = 0  
for i in range(len(DP)):
  for j in range(N):
    if i - A[j] >= 0:
      DP[i] = min(DP[i], DP[i - A[j]] + B[j])
#print(DP) 
print(min(DP[H:H + max(A) + 1]))

Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
AB = [0] * N
damage = [0] * N
for i in range(N):
  AB[i] = list(map(int, input().split()))
  damage[i] = AB[i][0]
  
x = min(AB, key = lambda x:x[1])
x = x[1]
  
DP = [10 ** 10] * (H + 1)#ダメージを与える最小魔力
DP[0] = 0

for i in range(1, H + 1):
  for j in range(i):
    if j in damage:
      p = damage.index(j)
      DP[i] = min(DP[i], DP[i - j] + AB[p][1])
  if DP[i] == 10 ** 10:
    DP[i] = x
  
print(DP[H])    


Fixed submission:
H, N = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(N):
  a, b = list(map(int, input().split()))
  A[i] = a
  B[i] = b
  
#モンスターの体力を i 減らすため消耗する魔力の最小値  
DP = [float("inf")] * (H + max(A) + 1)
DP[0] = 0  
for i in range(len(DP)):
  for j in range(N):
    if i - A[j] >= 0:
      DP[i] = min(DP[i], DP[i - A[j]] + B[j])
#print(DP) 
print(min(DP[H:H + max(A) + 1]))

Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
AB = [0] * N
damage = [0] * N
for i in range(N):
  AB[i] = list(map(int, input().split()))
  damage[i] = AB[i][0]
  
x = min(AB, key = lambda x:x[1])
x = x[1]
  
DP = [10 ** 10] * (10 ** 4 + 1)#ダメージを与える最小魔力
DP[0] = 0

for i in range(1, 10 ** 4 + 1):
  for j in range(i):
    if j in damage:
      p = damage.index(j)
      DP[i] = min(DP[i], DP[i - j] + AB[p][1])
  if DP[i] == 10 ** 10:
    DP[i] = x
  
print(DP[H])    


Fixed submission:
H, N = map(int, input().split())
A = [0] * N
B = [0] * N
for i in range(N):
  a, b = list(map(int, input().split()))
  A[i] = a
  B[i] = b
  
#モンスターの体力を i 減らすため消耗する魔力の最小値  
DP = [float("inf")] * (H + max(A) + 1)
DP[0] = 0  
for i in range(len(DP)):
  for j in range(N):
    if i - A[j] >= 0:
      DP[i] = min(DP[i], DP[i - A[j]] + B[j])
#print(DP) 
print(min(DP[H:H + max(A) + 1]))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
len_n = len(str(N))
top_n = N // (10 ** (len_n - 1))
bot_n = N % 10

count = 0

for i in range(1, N + 1):
  p = len(str(i))
  a = i // (10 ** (p - 1))
  b = i % 10
  if a == b:
    count += 1
  s = 2
  while len_n > s:
    count += 10 ** (s - 2)
    s + 1
  if i > 9:
    if top_n > b:
      count += 10 ** (s - 2)
    elif top_n == b:
      if bot_n > a:
        count += 1
      if i > 99:
        count += N // 10 - top_n * 10 ** (len_n - 1)
    
print(count)

Fixed submission:
N = int(input())
C = [[0] * 10 for i in range(10)]

for i in range(1, N + 1):
  p = len(str(i))
  a = i // (10 ** (p - 1))
  b = i % 10
  C[a][b] += 1
  
count = 0
for i in range(10):
  for j in range(10):
    count += C[i][j] * C[j][i]
    #if C[i][j] * C[j][i] > 0:
      #print(i, j)
print(count)