Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02750 (python)

Fixed submission:
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
shop = []
shop_0 = []
for i in range(n):
    a, b = map(int, input().split())
    if a == 0:
        shop_0.append((a, b))
        n -= 1
    else:
        shop.append((a, b))
shop.sort(key = lambda x: (x[0])/(1+x[1]), reverse = True)
shop_0.sort()

inf = 10**18
dp = [[inf for i in range(30)] for j in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for j in range(29):
        dp[i+1][j] = min(dp[i][j], dp[i+1][j])
        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + (dp[i][j]+1)*(shop[i][0])+shop[i][1]+1)



ans = 0
for i in range(30):
    tmp = i
    T = dp[n][i]
    if T > t:
        continue
    else:
        for j in range(len(shop_0)):
            if T+shop_0[j][1]+1 <= t:
                T += shop_0[j][1]+1
                tmp += 1
            else:
                break
    ans = max(tmp, ans)

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n, t = map(int, input().split())
shop = []
shop_0 = []
for i in range(n):
    a, b = map(int, input().split())
    if a == 0:
        shop_0.append((a, b))
        n -= 1
    else:
        shop.append((a, b))
shop.sort(key = lambda x: (x[0])/(1+x[1]), reverse = True)
shop0.sort(key = lambda x: x[1])

inf = 10**18
dp = [[inf for i in range(30)] for j in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for j in range(29):
        dp[i+1][j] = min(dp[i][j], dp[i+1][j])
        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + dp[i][j]*(shop[i][0])+shop[i][1]+1)



ans = 0
for i in range(30):
    tmp = i
    T = dp[n][i]
    if T > t:
        continue
    else:
        for j in range(len(shop_0)):
            if T+shop_0[j][1]+1 <= t:
                T += shop_0[j][1]+1
                tmp += 1
            else:
                break
    ans = max(tmp, ans)

print(ans)
Problem p02770 (python)

Fixed submission:
import sys
input = sys.stdin.readline
k, q = map(int, input().split())
d = list(map(int, input().split()))
query = [None]*q
for i in range(q):
    query[i] = list(map(int, input().split()))

def answer_query(n, x, m):
    l = [m if i%m == 0 else i%m for i in d]
    count = n // k
    rem = n % k
    s = sum(l)
    total = s%m * count + x%m
    for i in range(rem):
        total += l[i]

    total //= m
    total += s//m * count

    print(n-total)


for n, x, m in query:
    answer_query(n-1, x, m)

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    k, q = map(int, input().split())
    d = list(map(int, input().split()))
    query = [list(map(int, input().split())) for i in range(q)]
    for n, x, m in query:
        print(answer_query(n, x, m))

def answer_query(n, x, m):
    l = []
    for i in range(k):
        l.append(d[i]%m)
    zero = l.count(0)
    total = sum(l)*((n-1)//k)
    total += x%m
    zero_total = zero*((n-1)//k)

    s = n
    n -= 1
    n -= n//k * k
    t = 0
    for i in range(n):
        t += l[i]
    total += t

    for i in range(n):
        if l[i] == 0:
            zero_total += 1

    return s-1-total//m-zero_total

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
k, q = map(int, input().split())
d = list(map(int, input().split()))
query = [list(map(int, input().split())) for i in range(q)]
ans = []
for n, x, m in query:
    l = [d[i]%m for i in range(k)]
    n -= 1
    zero = l.count(0)
    count = n // k
    zero_total = zero*count
    total = sum(l)*count + x%m
    rem = n % k
    total += sum(l[:rem])
    zero_total += l[:rem].count(0)
    ans.append(n-total//m-zero_total)
print("\n".join(ans))
Problem p02806 (python)

Fixed submission:
n = int(input())
ans = 0
music = []
for i in range(n):
    s, t = input().split()
    music.append([s,t])

x = input()
flag = 0
for i in range(n):
    if flag == 1:
        ans += int(music[i][1])
    if music[i][0] == x:
        flag = 1

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = list(map(int, input().split()))

x_diff = []
for i in range(n-1):
    x_diff.append(x[i+1] - x[i])



def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

base = 1
for i in range(1, n):
    base *= i
    base %= 10**9+7

frac = []
for i in range(1, n):
    frac.append(base*modinv(i) % (10**9+7))

import itertools
frac = list(itertools.accumulate(frac))

ans = 0
for i in range(n-1):
    ans += frac[i]*x_diff[i]
    ans %= 10**9+7

print(ans)
Problem p02853 (python)

Fixed submission:
x, y = map(int,input().split())
ans = 0
if x == 1:
  ans += 300000
elif x == 2:
  ans += 200000
elif x == 3:
  ans += 100000

if y == 1:
  ans += 300000
elif y == 2:
  ans += 200000
elif y == 3:
  ans += 100000
if x == 1 and y == 1:
  ans += 400000
  
print(ans)

Buggy submission (verdict: Runtime Error):
x, y = map(int(input().split()))
ans = 0
if x == 1:
  ans += 300000
elif x == 2:
  ans += 200000
elif x == 3:
  ans += 100000

if y == 1:
  ans += 300000
elif y == 2:
  ans += 200000
elif y == 3:
  ans += 100000
if x == 1 and y == 1:
  ans += 400000
  
print(ans)
Problem p02899 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
  b.append([a[i], i+1])
b.sort()
ans = []
for i in range(n):
  ans.append(b[i][1])
print(' '.join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
  b.append(a[i], i+1)
b.sort()
ans = []
for i in range(n):
  ans.append(b[i][1])
print(''.join(map(str, ans)))
Problem p02900 (python)

Fixed submission:
A, B = map(int, input().split())
a = []
b = []
import random

def is_prime(n):
    if n == 2: return True
    if n == 1 or n & 1 == 0: return False

    d = (n - 1) >> 1
    while d & 1 == 0:
        d >>= 1

    for k in range(100):
        a = random.randint(1, n - 1)
        t = d
        y = pow(a, t, n)

        while t != n - 1 and y != 1 and y != n - 1:
            y = (y * y) % n
            t <<= 1

        if y != n - 1 and t & 1 == 0:
            return False

    return True


for i in range(1, int(A**(1/2))+1):
    if A % i == 0:
        a.append(i)
        a.append(A//i)
for i in range(1, int(B**(1/2))+1):
    if B % i == 0:
        b.append(i)
        b.append(B//i)

a = set(a)
b = set(b)
a = a & b

ans = 0
for i in a:
    if is_prime(i):
        ans += 1
print(ans+1)

Buggy submission (verdict: Runtime Error):
from fractions import gcd
A, B = map(int, input().split())
a = []
b = []
for i in range(1, A**(1/2)+1):
    if A % i == 0:
        a.append(i)
        a.append(A//i)
for i in range(1, B**(1/2)+1):
    if B % i == 0:
        a.append(i)
        a.append(B//i)

ans = 0
for i in a:
    for j in b:
        if gcd(i, j) == 1:
            ans += 1
print(ans)
Problem p02913 (python)

Fixed submission:
N = int(input())
S = input()


r = 1
ans = 0
for l in range(N):

    while True:
        #print(S.rfind(S[l:r+1]), S[l:r+1], l, r)

        if S.rfind(S[l:r+1]) >= r+1:
            r += 1
            ans = max(ans, r-l)
        else:
            break

print(ans)

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

import random
base1 = 1007
mod1 = 10**9+7
base2 = 2009
mod2 = 10**9+9
modTank1 = [3000012541,3000012553,3000012563,3000012649,3000012683,3000012709]
mod1 = modTank1[random.randint(0,5)]
modTank2 = [3000014753,3000014783,3000014833,3000014839,3000014891,3000015433]
mod2 = modTank2[random.randint(0,5)]
hash1 = [0]*(n+1)
power1 = [1]*(n+1)
hash2 = [0]*(n+1)
power2 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1
    hash2[i+1] = (hash2[i]*base2 + ord(e))%mod2
    power2[i+1] = (power2[i]*base2)%mod2

def rolling_hash(i, j):
    return ((hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1)*(10**10) + (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2



hash = set()
ans = 0
for i in range(n//2, 0, -1):#length
    for j in range(1, n-i+2):
        h = rolling_hash(j, j+i)
        if h not in hash:
            hash.add(h)
        else:
            ans = i
            break
    if ans != 0:
        break
print(ans)
Problem p02926 (python)

Fixed submission:
from math import *
n = int(input())
engine = [list(map(int, input().split())) for i in range(n)]

args = []
for i in range(n):
    x, y = engine[i]
    if x == 0:
        if y < 0:
            args.append((-pi/2, x, y))
        elif y > 0:
            args.append((pi/2, x, y))

    elif y == 0:
        if x > 0:
            args.append((0, x, y))
        elif x < 0:
            args.append((pi, x, y))

    else:
        args.append((atan2(y, x), x, y))

args.sort()
l = len(args)
args = args[:] + args[:]
ans = 0
for i in range(1, l+1):
    for j in range(l):
        x, y = 0, 0
        for k in range(j, j+i):
            x += args[k][1]
            y += args[k][2]
        ans = max(ans, hypot(x, y))

print(ans)

Buggy submission (verdict: Runtime Error):
from math import *
n = int(input())
engine = [list(map(int, input().split())) for i in range(n)]

args = []
for i in range(n):
    x, y = engine[i]
    if x == 0:
        if y < 0:
            args.append((-pi/2, x, y))
        elif y > 0:
            args.append((pi/2, x, y))

    elif y == 0:
        if x > 0:
            args.append((0, x, y))
        elif x < 0:
            args.append((pi, x, y))

    else:
        args.append((atan(y, x), x, y))

args.sort()
l = len(args)
args = args[:] + args[:]
ans = 0
for i in range(1, l+1):
    for j in range(l):
        x, y = 0, 0
        for k in range(j, j+i):
            x += args[k][1]
            y += args[k][2]
        ans = max(ans, hypot(x, y))

print(ans)

Buggy submission (verdict: Runtime Error):
import random
N = int(input())
engine = [list(map(int, input().split())) for i in range(N)]

used = [False]*N
x = 0
y = 0

if N <= 50:

  for _ in range(5000000):
      i = random.randint(0, N-1)
      if used[i]:
          if x*x+y*y <= (x-engine[i][0])**2+(y-engine[i][1])**2:
              x -= engine[i][0]
              y -= engine[i][1]
              used[i] = False
      else:
          if x*x+y*y <= (x+engine[i][0])**2+(y+engine[i][1])**2:
              x += engine[i][0]
              y += engine[i][1]
              used[i] = True

  print((x*x+y*y)**(1/2))

else:
  print(ans)

Buggy submission (verdict: Runtime Error):
import random
N = int(input())
engine = [list(map(int, input().split())) for i in range(N)]

used = [True]*N

x = 0
y = 0
for i in range(N):
    x += engine[i][0]
    y += engine[i][1]

for _ in range(2500000):
    i = random.randint(0, N-2)
    xtemp = x
    ytemp = y
    if used[i]:
        xtemp -= engine[i][0]
        ytemp -= engine[i][1]
    else:
        xtemp += engine[i][0]
        ytemp += engine[i][1]
    j = random.randint(i+1, N-1)
    if used[j]:
        xtemp -= engine[j][0]
        ytemp -= engine[j][1]
    else:
        xtemp += engine[j][0]
        ytemp += engine[j][1]


    if x*x+y*y <= xtemp**2+ytemp**2:
        used[i] = not used[i]
        used[j] = not used[j]
        x = xtemp
        y = ytemp
print((x*x+y*y)**(1/2))
Problem p02929 (python)

Fixed submission:
from bisect import *
N = int(input())
S = input()

swapleft = []
swapright = []

for i in range(N):
    if S[i] == "B":
        if (i+1) % 2 == 0:
            swapright.append(i+1)
    else:
        if (i+1) % 2 == 1:
            swapright.append(i+1)

for i in range(N, 2*N):
    if S[i] == "B":
        if (i+1) % 2 == 1:
            swapleft.append(i+1)
    else:
        if (i+1) % 2 == 0:
            swapleft.append(i+1)


swapleft = set(swapleft)
swapright = set(swapright)

if len(swapleft) > len(swapright):
    swapleft |= set([i for i in range(1, N+1)]) - swapright
    swapright = set([i for i in range(1, 2*N+1)]) - swapleft

else:
    swapright |= set([i for i in range(N+1, 2*N+1)]) - swapleft
    swapleft = set([i for i in range(1, 2*N+1)]) - swapright

if len(swapright) != len(swapleft):
    print(0)
else:

    swapleft = list(swapleft)

    swapleft.sort(reverse = True)
    swapright = list(swapright)
    swapright.sort()

    ans = 1

    for i in range(N):
        ans *= (N - bisect_left(swapright, swapleft[i]) - i)
        ans %= 1000000007


    for i in range(1, N+1):
        ans *= i
        ans %= 1000000007

    print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import *
N = int(input())
S = input()

swapleft = []
swapright = []

for i in range(N):
    if S[i] == "B":
        if (i+1) % 2 == 0:
            swapright.append(i+1)
    else:
        if (i+1) % 2 == 1:
            swapright.append(i+1)

for i in range(N, 2*N):
    if S[i] == "B":
        if (i+1) % 2 == 1:
            swapleft.append(i+1)
    else:
        if (i+1) % 2 == 0:
            swapleft.append(i+1)

swapleft = set(swapleft)
swapright = set(swapright)

swapleft |= set([i for i in range(1, N+1)]) - swapright
swapright = set([i for i in range(1, 2*N+1)]) - swapleft
swapleft = list(swapleft)
swapleft.sort(reverse = True)
lmax = max(swapleft)
swapright = list(swapright)
swapright.sort()

ans = 1

for i in range(N):
    ans *= (N - bisect_left(swapright, swapleft[i]) - i)
    ans %= 1000000007


for i in range(1, N+1):
    ans *= i
    ans %= 1000000007

print(ans)
Problem p02965 (python)

Fixed submission:
N, M = map(int, input().split())

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353
n = 10**6*3
g1 = [1, 1]
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, n + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

ans = cmb(M*3+N-1, N-1, mod)
for i in range(M*2+1, M*3+1):
    ans -= cmb(M*3-i+(N-1)-1, N-2, mod) * N - mod*mod
    ans = (ans+mod)%mod

for i in range(M+1, min(N, 3*M)+1):
    if (3*M - i) % 2 == 0:
        two = (3*M - i) // 2
        ans -= cmb(two+N-1, N-1, mod) * cmb(N, i, mod) - mod*mod
        ans = (ans+mod)%mod
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353
n = 10**6
g1 = [1, 1]
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, n + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

ans = cmb(M*3+N-1, N-1, mod)
for i in range(M*2+1, M*3+1):
    ans -= cmb(M*3-i+(N-1)-1, N-2, mod) * N - mod*mod
    ans = (ans+mod)%mod

for i in range(M+1, min(N, 3*M)+1):
    if (3*M - i) % 2 == 0:
        two = (3*M - i) // 2
        ans -= cmb(two+N-1, N-1, mod) * cmb(N, i, mod) - mod*mod
        ans = (ans+mod)%mod
print(ans)
Problem p03005 (python)

Fixed submission:
n,k=map(int,input().split())
if k == 1:
  print(0)
else:
  print(n-k)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
anslist = []

for i in range(1,n-1):
  if a[i] < 0:
    anslist.append([a[-1], a[i]])
    a[-1] -= a[i]
  else:
    anslist.append([a[0], a[i]])
    a[0] -= a[i]
anslist.append([a[-1], a[0]])
ans = a[-1]-a[0]
print(ans)
for i in range(len(anslist)):
  print(anslist[i][0], anslist[i][1])
Problem p03061 (python)

Fixed submission:
from fractions import gcd
N = int(input())
a = list(map(int, input().split()))
N0 = 2**(N-1).bit_length()
INF = 2**31-1
data = [INF]*(2*N0+1)
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        if data[2*k+1] == INF:
            data[k] = data[2*k+2]
        elif data[2*k+2] == INF:
            data[k] = data[2*k+1]
        else:
            data[k] = gcd(data[2*k+1], data[2*k+2])

for i in range(N):
    update(i, a[i])

ans = 0
for i in range(N):
    update(i, INF)
    ans = max(ans, data[0])
    update(i, a[i])

print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd
N = int(input())
a = list(map(int, input().split()))

if N > 2:
    a.sort()
    diff = []
    for i in range(N-1):
        diff.append(a[i+1]-a[i])
    index = diff.index(min(diff))
    diff_ = diff[:]
    if index > 0:
        plus = diff[index-1] + diff[index]
        diff_[index-1] = plus
        diff_[index] = plus
    GCD = diff_[0]

    for i in range(N-1):
        GCD = gcd(diff_[i], GCD)

    if index < N-1:
        plus = diff[index] + diff[index+1]
        diff[index] = plus
        diff[index+1] = plus
    GCD2 = diff[0]
    for i in range(N-1):
        GCD2 = gcd(diff[i], GCD2)
    print(max(GCD, GCD2))
else:
    print(max(a))
Problem p03086 (python)

Fixed submission:
S = input()
atcg = ['A', 'C', 'T', 'G']
ans = 0
count = 0
for i in range(len(S)):
  if S[i] in atcg:
    count += 1
    ans = max(count, ans)
  else:
    count = 0
print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
atcg = ['A', 'C', 'T', 'G']
ans = 0
count = 0
for i in range(S):
  if S[i] in atcg:
    count += 1
    ans = max(count, ans)
  else:
    count = 0
print(ans)
Problem p03087 (python)

Fixed submission:
n, q = map(int, input().split())
s = input()
ac = [0]
for i in range(len(s)-1):
  if s[i] == 'A' and s[i+1] == 'C':
    ac.append(ac[i]+1)
  else:
    ac.append(ac[i])
for i in range(q):
  l, r = map(int, input().split())
  print(ac[r-1]-ac[l-1])

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
s = input()
ac = [0]
for i in range(len(s)):
  if s[i] == 'A' and s[i+1] == 'C':
    ac.append(ac[i]+1)
  else:
    ac.append(ac[i])
for i in range(q):
  l, r = map(int, input().split())
  print(ac[r-1]-ac[l-1])
Problem p03096 (python)

Fixed submission:
n = int(input())
c = [-1] + [int(input()) for i in range(n)]

count = [0]*(200005)
dp = [0]*(n+1)
dp[0] = 1
mod = 10**9+7
#dp[i]: i番目までの場合の数
for i in range(n):
    dp[i+1] = dp[i]
    if c[i+1] != c[i]:
        dp[i+1] += count[c[i+1]]
        dp[i+1] %= mod
        count[c[i+1]] += dp[i]
        count[c[i+1]] %= mod

print(dp[n]%mod)

Buggy submission (verdict: Runtime Error):
n = int(input())
c = [-1] + [int(input()) for i in range(n)]

count = [0]*(n+2)
dp = [0]*(n+1)
dp[0] = 1
mod = 10**9+7
#dp[i]: i番目までの場合の数
for i in range(n):
    dp[i+1] = dp[i]
    if c[i+1] != c[i]:
        dp[i+1] += count[c[i+1]]
        dp[i+1] %= mod
    count[c[i+1]] += dp[i]
    count[c[i+1]] %= mod

print(dp[n])
Problem p03102 (python)

Fixed submission:
n, m, c = map(int, input().split())
b = list(map(int, input().split()))
ans = 0
for i in range(n):
  a = list(map(int, input().split()))
  point = c
  for i in range(m):
    point += a[i]*b[i]
  if point > 0:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n, m, c = map(int, input().split())
b = list(map(int, input().split()))
ans = 0
for i in range(N):
  a = list(map(int, input().split()))
  point = c
  for i in range(m):
    point += a[i]*b[i]
  if point > 0:
    ans += 1
print(ans)
Problem p03110 (python)

Fixed submission:
N = int(input())
ans = 0.0
for i in range(N):
  a, b = input().split()
  if b == 'JPY':
    ans += float(a)
  else:
    ans += float(a)*380000
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
ans = 0
for i in range(N):
  a, b = input().split()
  if b == 'JPY':
    ans += int(a)
  else:
    ans += int(a)*380000
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
ans = 0
for i in range(N):
  a, b = input().split()
  if b = 'JPY':
    ans += int(a)
  else:
    ans += int(a)*380000
print(ans)
Problem p03128 (python)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
matchlist = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
numberlist = []
for i in range(m):
    numberlist.append(matchlist[a[i]])
numberlist = set(numberlist)
#dp[i]:i本でのmax桁
dp = [-10000000]*(n+1)
dp[0] = 0
for i in range(n):
    for j in numberlist:
        if i+j <= n:
            dp[i+j] = max(dp[i+j], dp[i]+1)


ans = []
d = dp[n]
a.sort(reverse = True)
for i in range(d):
    for j in a:
        if n - matchlist[j] >= 0:
            if dp[n] == dp[n-matchlist[j]] + 1:
                n -= matchlist[j]
                ans.append(j)
                break
print("".join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))
matchlist = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
numberlist = []
for i in range(m):
    numberlist.append(matchlist[a[i]])
numberlist = set(numberlist)
#dp[i]:i本でのmax桁
dp = [-10000000]*(n+1)
dp[0] = 0
for i in range(n):
    for j in numberlist:
        if i+j <= n:
            dp[i+j] = max(dp[i+j], dp[i]+1)


ans = []
d = dp[n]
a.sort(reverse = True)
for i in range(d):
    for j in a:
        if dp[n] == dp[n-matchlist[j]] + 1:
            n -= matchlist[j]
            ans.append(j)
            break
print("".join(map(str,ans)))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
matchlist = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
numberlist = []
for i in range(M):
    numberlist.append([matchlist[A[i]], A[i]])
numberlist.sort(key = lambda x: (x[0], -x[1]))

ans = []
while N > 0:
    ans.append(numberlist[0])
    N -= ans[-1][0]
    if N < 0:
        N += ans.pop()[0]
        N += ans.pop()[0]
        numberlist.pop(0)

ans = [i[1] for i in ans]
ans.sort(reverse = True)
print("".join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
matchlist = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
numberlist = []
for i in range(M):
    numberlist.append([matchlist[A[i]], A[i]])
numberlist.sort(key = lambda x: (x[0], -x[1]))

ans = []
while N > 0:
    print(N)
    ans.append(numberlist[0])
    N -= ans[-1][0]
    if N < 0:
        N += ans.pop()[0]
        N += ans.pop()[0]
        numberlist.pop(0)

ans = [i[1] for i in ans]
ans.sort(reverse = True)
print("".join(map(str, ans)))
Problem p03155 (python)

Fixed submission:
N = int(input())
H = int(input())
W = int(input())
print((N - H + 1)*(N - W + 1))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())

#white = ".", black = "#"
def search(x, y, color):
    global ans
    if color == ".":
        for m in [1, -1]:
            if 0 <= x+m <= H-1:
                if S[x+m][y] == "#" and not dp[x+m][y]:
                    dp[x+m][y] = True
                    color = "#"
                    search(x+m, y, color)
        for n in [1, -1]:
            if 0 <= y+n <= W-1:
                if S[x][y+n] == "#" and not dp[x][y+n]:
                    dp[x][y+n] = True
                    color = "#"
                    search(x, y+n, color)

    else:
        for m in [1, -1]:
            if 0 <= x+m <= H-1:
                if S[x+m][y] == "." and not dp[x+m][y]:
                    ans += 1
                    dp[x+m][y] = True
                    color = "."
                    search(x+m, y, color)

        for n in [1, -1]:
            if 0 <= y+n <= W-1:
                if S[x][y+n] == "." and not dp[x][y+n]:
                    ans += 1
                    dp[x][y+n] = True
                    color = "."
                    search(x, y+n, color)


ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            dp = [[False] * W for a in range(H)]
            dp[i][j] = True
            search(i, j, "#")

print(ans)
Problem p03194 (python)

Fixed submission:
N, P = map(int, input().split())
ans = 0
if N == 1:
    print(P)
elif N >= P or N > 50:
    print(1)

else:
    for i in range(1, int(P**(1/N)+2)):
        if P % i**N == 0:
            ans = i
    print(ans)

Buggy submission (verdict: Runtime Error):
N, P = map(int, input().split())
ans = 0
if N == 1:
    print(P)
else:
    for i in range(1, int(P**(1/N)+5)):
        if P % i**N == 0:
            ans = i
    print(ans)