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

Fixed submission:
n = int(input())
ab = []
g = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
    ab.append([a,b])

k = max([len(i) for i in g])
print(k)

reach = [False]*n
c = dict()
from collections import deque
q = deque([[0,-1]])

while q:
    cur,col = q.popleft()
    reach[cur] = True
    for next_i in g[cur]:
        if not reach[next_i]:
            col = (col+1)%k
            a,b = sorted([cur,next_i])
            c[(a,b)] = col
            q.append([next_i,col])

for a,b in ab:
    print(c[(a,b)]+1)

Buggy submission (verdict: Runtime Error):
n = int(input())
ab = []
g = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
    ab.append([a,b])

k = max([len(i) for i in g])
print(k)

c = [[-1]*n for i in range(n)]
from collections import deque
q = deque([(0,-1)])

while q:
    cur,col = q.popleft()
    for next_i in g[cur]:
        if c[cur][next_i]<0:
            col = (col+1)%k
            c[cur][next_i] = col
            c[next_i][cur] = col
            q.append((next_i,col))


for a,b in ab:
    print(c[a][b]+1)
Problem p02855 (python)

Fixed submission:
h,w,k = map(int,input().split())
s = [input() for i in range(h)]
ans = [[0]*w for i in range(h)]
c = 1
not_cherry = [] #イチゴがない列
ok = False
for i in range(h):
    if '#' in s[i]:
        cherry = 0
        for j in range(w):
            if s[i][j]=='#':
                if cherry==0:
                    ans[i][j] = c
                else:
                    c += 1
                    ans [i][j] = c
                cherry += 1
            else:
                ans[i][j] = c
        c += 1
        ok = False
    else:
        if ok:
            not_cherry[-1] += 1
        else:
            not_cherry.append(1)
        ok = True

i = 0
ind = 0
while i<h:
    if 0 in ans[i]:
        c = not_cherry[ind]
        for l in range(c):
            for j in range(w):
                if i+l<h-1 and i+c<h-1:ans[i+l][j] = ans[i+c][j]
                else:ans[i+l][j] = ans[i+l-1][j]
        i+=c
        ind += 1
    else:
        i+=1

for i in ans:
    print(*i)

Buggy submission (verdict: Runtime Error):
h,w,k = map(int,input().split())
s = [input() for i in range(h)]
ans = [[0]*w for i in range(h)]
c = 1
not_cherry = [] #イチゴがない列
ok = False
for i in range(h):
    if '#' in s[i]:
        cherry = 0
        for j in range(w):
            if s[i][j]=='#':
                if cherry==0:
                    ans[i][j] = c
                else:
                    c += 1
                    ans [i][j] = c
                cherry += 1
            else:
                ans [i][j] = c
        c += 1
        ok = False
    else:
        if ok:
            not_cherry[-1] += 1
        else:
            not_cherry.append(1)
        ok = True

i = 0
ind = 0
while i<h:
    if 0 in ans[i]:
        c = not_cherry[ind]
        for l in range(c):
            for j in range(w):
                if i+l<h-1:ans[i+l][j] = ans[i+c][j]
                else:ans[i][j] = ans[i-1][j]
        i+=c
        ind += 1
    else:
        i+=1

for i in ans:
    print(*i)
Problem p02936 (python)

Fixed submission:
n,q = map(int,input().split())
g = [[] for i in range(n)]
c = [0]*n
reach = [False]*n
for i in range(n-1):
    a,b = map(lambda x: int(x)-1,input().split())
    g[a].append(b)
    g[b].append(a)

for i in range(q):
    p,x = map(int,input().split())
    c[p-1] += x

reach[0] = True
q = [0]
while q:
    cur = q.pop()
    reach[cur] = True
    for i in g[cur]:
        if not reach[i]:
            c[i] += c[cur]
            q.append(i)
print(*c)

Buggy submission (verdict: Runtime Error):
n,q = map(int,input().split())
g = [[] for i in range(n)]
c = [0]*n
for i in range(n-1):
    a,b = map(lambda x: int(x)-1,input().split())
    g[a].append(b)

def dfs(v,x):
    c[v] += x
    for i in g[v]:
        dfs(i,x)

for i in range(q):
    p,x = map(int,input().split())
    dfs(p-1,x)

print(*c)
Problem p02995 (python)

Fixed submission:
import fractions
a,b,c,d = map(int,input().split())
e = c*d//fractions.gcd(c,d)
cnt = b-a+1
c_cnt = b//c-(a-1)//c
d_cnt = b//d-(a-1)//d
e_cnt = b//e-(a-1)//e
print(cnt-c_cnt-d_cnt+e_cnt)

Buggy submission (verdict: Runtime Error):
a,b,c,d = map(int,input().split())
a -= 1
from math import gcd
g = c*d//gcd(c,d)
cc = b//c-a//c
dc = b//d-a//d 
gc = b//g-a//b
print(b-a-cc-dc+gc)

Buggy submission (verdict: Runtime Error):
import math
a,b,c,d = map(int,input().split())
e = c*d//math.gcd(c,d)
n = b-a+1
nc = (n+a%c)//c
nd = (n+a%d)//d
ne = (n+a%e)//e
print(n-(nc+nd-ne))
Problem p03000 (python)

Fixed submission:
n,x = map(int,input().split())
l = list(map(int,input().split()))
cur = ans = 0
for i in range(n):
    cur += l[i]
    if cur<=x:
        ans += 1
print(ans+1)

Buggy submission (verdict: Runtime Error):
n,x = map(int,input().split())
l = list(map(int,input().split()))
cur = ans = 0
while cur<=x and ans<=n:
    ans += 1
    cur += l[ans-1]
print(ans)
Problem p03006 (python)

Fixed submission:
n = int(input())
xy = sorted([tuple(map(int,input().split())) for i in range(n)])
if n==1:
    print(1)
    exit()
dif = []
for i in range(n):
    for j in range(i+1,n):
        dif.append(tuple([xy[j][0]-xy[i][0],xy[j][1]-xy[i][1]]))
from collections import Counter
c = Counter(dif)
print(n-max(c.values()))

Buggy submission (verdict: Runtime Error):
n = int(input())
xy = sorted([tuple(map(int,input().split())) for i in range(n)])
dif = []
for i in range(n-1):
    dif.append(tuple([xy[i+1][0]-xy[i][0],xy[i+1][1]-xy[i][1]]))
from collections import Counter
c = Counter(dif)
print(n-c[list(c)[0]])
Problem p03061 (python)

Fixed submission:
import fractions
n = int(input())
a = list(map(int,input().split()))
ans = 0
l = [0]*(n+1)
r = [0]*(n+1)
for i in range(n):
    l[i+1] =  fractions.gcd(l[i],a[i])
for i in range(n-1,0,-1):
    r[i] =  fractions.gcd(r[i+1],a[i])
for i in range(n):
    L = l[i]
    R = r[i+1]
    ans = max(ans,fractions.gcd(L,R))
print(ans)

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

from math import gcd
l,r = [0]*(n+1),[0]*(n+1)
for i in range(n):
    l[i+1] = gcd(l[i],a[i])
    r[n-i-1] = gcd(r[i+1],a[n-i-1])

ans = 1
for i in range(n):
    g = gcd(l[i],r[i+1])
    ans = max(ans,g)
print(ans)
Problem p03074 (python)

Fixed submission:
n,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
b.append(b[-1])
n = len(b)
l = 2*k+1
st=int(s[0])
ans = 0
for i in range(st,n,2):
    ans = max(ans,b[i]-b[max(0,i-l)])
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
s = input()
a = []
c = 1
for i in range(n-1):
    if s[i]==s[i+1]:
        c+=1
    else:
        a.append(c)
        c = 1
a.append(c)
b = [0]
for i in range(len(a)):
    b.append(b[-1]+a[i])
b.append(b[-1])
l = 2*k+1
st=int(s[0])
ans = 0
for i in range(st,n,2):
    ans = max(ans,b[i]-b[max(0,i-l)])
print(ans)
Problem p03148 (python)

Fixed submission:
n,k = map(int,input().split())
td = [list(map(int,input().split())) for i in range(n)]
td.sort(key=lambda x:x[1],reverse=True)
a = 0
ans = []
kind = [-1]*n
c = 0
min_p = []
for i in range(n):
    eat = False
    if i<k:
        a += td[i][1]
        eat = True
    else:
        if min_p:
            if kind[td[i][0]-1]<0:
                a -= (min_p.pop()-td[i][1])
                a += (c+1)**2-c**2
                ans.append(a)
                eat = True
    if eat:
        if kind[td[i][0]-1]<0:
            kind[td[i][0]-1]=1
            c+=1
        else:min_p.append(td[i][1])
    if i==k-1:
        a += c**2
        ans.append(a)
print(max(ans))

Buggy submission (verdict: Runtime Error):
from collections import deque
n,k = map(int,input().split())
td = [list(map(int,input().split())) for i in range(n)]
td.sort(key=lambda x:x[1],reverse=True)
a = 0
ans = []
kind = [-1]*n
c = 0
min_p = deque()
for i in range(n):
    eat = False
    if i<k:
        a += td[i][1]
        eat = True
    else:
        if min_p!=[]:
            if kind[td[i][0]-1]<0:
                a -= (min_p[-1]-td[i][1])
                a += (c+1)**2-c**2
                ans.append(a)
                min_p.popleft()
                eat = True
    if eat:
        if kind[td[i][0]-1]<0:
            kind[td[i][0]-1]=1
            c+=1
        else:min_p.append(td[i][1])
    if i==k-1:
        a += c**2
        ans.append(a)
print(max(ans))
Problem p03150 (python)

Fixed submission:
s = input()
ans = 'NO'
for i in range(len(s)):
    for j in range(i,len(s)+1):
        if s[:i]+s[j:]=='keyence':
            ans = 'YES'
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
k = 'keyence'
i = 0
inds = []
ans = 'NO'
for a,x in enumerate(s):
    if x==k[i]:
        i+=1
        inds.append(a)
    if i==6:
        c = 0
        for z in range(i-1):
            if inds[z+1]-inds[z]>1:
                c+=1
        if c<=1:ans='YES'
print(ans)
Problem p03309 (python)

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

for i in range(n):
    a[i] = a[i]-(i+1)

a.sort()
b = (a[n//2]+a[(n-1)//2])//2

ans = 0
for i in range(n):
    ans += abs(a[i]-b)
print(ans)

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

for i in range(n):
    a[i] = a[i]-(i+1)

a.sort()
if i%2==1:
    b = a[i//2]
else:
    b = (a[i//2]+a[i//2+1])//2

ans = 0
for i in range(n):
    ans += abs(a[i]-b)
print(ans)
Problem p03474 (python)

Fixed submission:
a,b = map(int,input().split())
s = input()
if s[a]=='-' and s.count('-')==1:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
import re
a,b = map(int,input().split())
s = input()
if s[a]=='-' and re.fullmatch(r'\d+',s[:a]) and re.fullmatch(r'\d+',s[a+1:]):
    print('Yes')
else:
    print('No')
Problem p03546 (python)

Fixed submission:
import itertools
import collections
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]
a = list(itertools.chain.from_iterable(a))
b = collections.Counter(a)
ans = 0

def bfs(x):
    if x[-1]==1:
        global s
        buf = 0
        for i in range(len(x)-1):
            buf += c[x[i]][x[i+1]]
        s = min(s,buf)
    else:
        for i in range(10):
            if not i in x:
                bfs(x+[i])

for i in b:
    if not i in [1,-1]:
        s = 10**9
        bfs([i])
        ans += s*b[i]
print(ans)

Buggy submission (verdict: Runtime Error):
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]
n = 10
from scipy.sparse.csgraph import floyd_warshall
c = floyd_warshall(c)
ans = 0
for i in a:
    for j in i:
        if j!=-1: ans += c[j][1]
print(int(ans))
Problem p03607 (python)

Fixed submission:
import collections
n = int(input())
a = [int(input()) for _ in range(n)]
c = collections.Counter(a)
ans = 0
for i in c:
    if c[i]%2==1:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
d = [0]*(10**5+1)
for i in range(n):
    a = int(input())
    d[a] += 1
ans = 0
for i in d:
    if i%2==1:
        ans += 1
print(ans)
Problem p03633 (python)

Fixed submission:
from fractions import gcd
n = int(input())
ans = 1
for i in range(n):
    t = int(input())
    if ans%t!=0:
        ans = ans*t//gcd(ans,t)
print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd
n = int(input())
ans = 1
for i in range(n):
    t = int(input())
    if ans%t!=0:
        ans = ans*t//gcd(ans,t)
print(ans)
Problem p03646 (python)

Fixed submission:
k = int(input())

n = 50
a = k//n
m = k%n
ans = list(range(a,a+n))
for i in range(m):
    ans[n-i-1] += 1

print(50)
print(*ans)

Buggy submission (verdict: Runtime Error):
k = int(input())

tasu = k // 2
lst = list(range(tasu, tasu+2))
amari = k % 2
for i in range(amari):
    idx = 2 - i
    lst[idx] += 1

print(2)
print(' '.join(map(str, lst)))
Problem p03651 (python)

Fixed submission:
n,k = map(int,input().split())
a = list(map(int,input().split()))
from fractions import gcd
g = a[0]
for i in a[1:]:
    g = gcd(g,i)
if k<=max(a) and k%g==0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))
from math import gcd
g = a[0]
for i in a[1:]:
    g = gcd(g,i)
if k<=max(a) and k%g==0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
Problem p03779 (python)

Fixed submission:
x = int(input())
ans = i = 0
while i<x:
    i += ans
    ans += 1
print(ans-1)

Buggy submission (verdict: Runtime Error):
x = int(input())
ans = 10**9
def bfs(i,a):
    if a == x:
        global ans
        ans = min(ans,i-1)
    elif i<=x:
        bfs(i+1,a+i)
        bfs(i+1,a)
        bfs(i+1,a-i)
bfs(0,0)
print(ans)
Problem p03976 (python)

Fixed submission:
n,k = map(int,input().split())
p = [input()[0] for i in range(n)]
from collections import Counter
c = Counter(p)
v = sorted(list(c.values()),reverse=True)
ans = 0
while len(v)>=k:
    for i in range(k):
        v[i] -= 1
        if v[i]<0:
            print(ans)
            exit()
    ans += 1
    v = sorted(v,reverse=True)
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
p = [input()[0] for i in range(n)]
from collections import Counter
c = Counter(p)
v = list(c.values())
ans = 0
while len(v)>=k:
    c = 0
    while c<k and len(v)!=0:
        if v[c]==0:
            v.pop(c)
        else:
            v[c] -= 1
            c+=1
    if c==k:ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
p = [input()[0] for i in range(n)]
from collections import Counter
c = Counter(p)
v = list(c.values())
ans = 0
while len(v)>=k:
    c = 0
    while c<3 and len(v)!=0:
        if v[c]==0:
            v.pop(c)
        else:
            v[c] -= 1
            c+=1
    if c==3:ans += 1
print(ans)
Problem p03994 (python)

Fixed submission:
s = list(input())
k = int(input())
n = len(s)
for i in range(n-1):
    d = (ord('z')-ord(s[i])+1)%26
    if d<=k:
        s[i] = 'a'
        k -= d

x = ord(s[-1])+k%26
if x>ord('z'):x-=26
s[-1] = chr(x)


print(''.join(s))

Buggy submission (verdict: Runtime Error):
s = list(input())
k = int(input())
n = len(s)
for i in range(n):
    d = ord('z')-ord(s[i])+1
    if d<=k:
        s[i] = 'a'
        k -= d

s[-1] = chr(ord(s[-1])+k)

print(''.join(s))
Problem p02983 (python)

Fixed submission:
l, r = map(int, input().split())
ans = 2020
if l <= (l // 2019 + 1) * 2019 <= r:
    ans = 0
else:
    for i in range(l, r):
        for j in range(i + 1, r + 1):
            tmp = i * j % 2019
            ans = tmp if tmp < ans else ans
            if ans == 0:
                break
print(ans)

Buggy submission (verdict: Runtime Error):
from math import inf

l, r = map(int, input().split())
ans = inf
for i in range(l, r):
    for j in range(i+1, r+1):
        tmp = i * j % 2019
        ans = tmp if tmp < ans else ans
        if ans == 0:
            break

print(ans)
Problem p03030 (python)

Fixed submission:
def make_guide():
    num = int(input())
    lst = [(i + 1, input().split()) for i in range(num)]
    # TODO fix
    # 同じものが存在したら、っていう条件のもとでないとダメ
    # あと数字は降順
    lst = sorted(lst, key=lambda t: int(t[1][1]), reverse=True)
    lst = sorted(lst, key=lambda t: t[1][0])
    return lst


def test_display():
    lst = make_guide()
    for i, guides in lst:
        print(str(i) + " " + guides[0] + " " + guides[1])


def display():
    lst = make_guide()
    for i, guides in lst:
        print(i)


if __name__ == "__main__":
    display()

Buggy submission (verdict: Runtime Error):
def make_guide():
    num: int = int(input())
    lst = [(i + 1, *input().split()) for i in range(num)]
    lst = sorted(sorted(lst, key=lambda t: t[2]), key=lambda t: t[1])
    return lst


def test_display():
    lst = make_guide()
    for i, lst in lst:
        print(str(i) + " " + lst[0] + " " + lst[1])


def display():
    lst = make_guide()
    for i, city, point in lst:
        print(i)


if __name__ == "__main__":
    display()

Buggy submission (verdict: Runtime Error):
def make_guide():
    num: int = int(input())
    lst = [(i, input().split()) for i in range(num)]
    print(lst[0])
    lst = sorted(sorted(lst, key=lambda t: t[1][1]), key=lambda t: t[1][0])
    return lst
 
if __name__ == "__main__":
    display()
Problem p03035 (python)

Fixed submission:
A, B = [int(x) for x in input().split()]
if A <= 5:
    print(0)
elif A <= 12:
    print(int(B / 2))
else:
    print(B)

Buggy submission (verdict: Runtime Error):
n, g = map(int, input().split())
cards = [[int(x) for x in input().split()] for _ in range(g)]

cards.sort(key=lambda x: x[0])
lmx = cards[g - 1][0]
cards.sort(key=lambda x: x[1])
rmn = min(cards[0][1], n)

print(max(0, rmn - lmx + 1))
Problem p03050 (python)

Fixed submission:
N = int(input())
lim = int(N ** 0.5)
ans = 0
for i in range(1, lim + 1):
    m = N // i
    quotient = N // m
    while m != 0:
        remainder = N % m
        if N // m != quotient or remainder > i:
            break
        if quotient == remainder:
            ans += m
            break
        m -= 1
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
lim = int(N ** 0.5) + 1
ans = 0
for i in range(1, lim + 1):
    m = N // i
    quotient = N // m
    while m != 0:
        remainder = N % m
        if N // m != quotient or remainder > i:
            break
        if quotient == remainder:
            ans += m
            break
        m -= 1
print(ans)
Problem p03087 (python)

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

cc = []

acc = 0

for idx in range(N):
    if S[idx - 1:idx + 1] == "AC":
        acc = acc + 1
    cc.append(acc)

for i in range(Q):
    l, r = map(int, input().split())
    print(cc[r - 1] - cc[l - 1])

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

cc = []

for idx in range(len(S) - 1):
    if S[idx:idx + 2] == "AC":
        cc.append(1)
    else:
        cc.append(0)

for _ in range(Q):
    l, r = map(int, input().split())
    print(sum(cc[l - 1:r]) - cc[r - 1])
Problem p03109 (python)

Fixed submission:
y, m, d = map(int, input().split('/'))
if y < 2019:
    print('Heisei')
elif y == 2019 and m < 5:
    print('Heisei')
else:
    print('TBD')

Buggy submission (verdict: Runtime Error):
from datetime import date
dt = date.fromisoformat(input().replace('/', '-'))
th = date.fromisoformat('2019-04-30')
if dt <= th:
    print('Heisei')
else:
    print('TBD')
Problem p03262 (python)

Fixed submission:
from functools import reduce
from fractions import gcd

X = int(input().split()[1])
x = [abs(int(x) - X) for x in input().split()]

print(reduce(gcd, x))

Buggy submission (verdict: Runtime Error):
def gcd(a, b):
    if a > b:
        a, b = b, a
    if b % a == 0:
        return a
    a, b = b / a, a


def reduce(f, l):
    if len(l) == 1:
        return l[0]
    c = l[0]
    for i in l[1:]:
        c = f(c, i)
    return c


X = int(input().split()[1])
x = [abs(int(x) - X) for x in input().split()]

print(reduce(gcd, x))

Buggy submission (verdict: Runtime Error):
from functools import reduce
from math import gcd

X = int(input().split()[1])
x = [abs(int(x) - X) for x in input().split()]

print(reduce(gcd, x))

Buggy submission (verdict: Runtime Error):
from math import sqrt, floor
from functools import reduce


def divisors(a):
    return {i for i in range(1, floor(sqrt(a)) + 1) if a % i == 0}\
        .union({a // i for i in range(1, floor(sqrt(a)) + 1) if a % i == 0})


def greatest_common_divisor(a, b):
    return max(divisors(a).intersection(divisors(b)))


def greatest_common_divisor_of(lst):
    return reduce(greatest_common_divisor, lst)


X = int(input().split()[1])
xi = list(map(int, input().split()))
x = [x - X for x in xi]
print(greatest_common_divisor_of(x))
Problem p03472 (python)

Fixed submission:
from math import ceil

n, hp = map(int, input().split())
A = []
B = []
for _ in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
MAX_A = max(A)
B.sort(reverse=True)

cnt = 0
for b in B:
    if b > MAX_A:
        hp -= b
        cnt += 1
    else:
        break
    if hp <= 0:
        break
if hp > 0:
    cnt += ceil(hp / MAX_A)
print(cnt)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right
from math import ceil

n, h = map(int, input().split())
A = []
B = []
for _ in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
B_sorted = sorted(B)
B_cumulative_sum = list(B_sorted)
for i in range(n - 2, -1, -1):
    B_cumulative_sum[i] += B_cumulative_sum[i + 1]
    if B_cumulative_sum[i] > h:
        print(n - i)
        exit()
cnt = int(1e9)
for a, b in zip(A, B):
    target_idx = bisect_right(B_sorted, a)
    throws = n - target_idx
    hp_remains_after_throw = h - B_cumulative_sum[target_idx]
    attacks = ceil(hp_remains_after_throw / a)
    cnd = throws + attacks
    cnt = min(cnt, cnd)
print(cnt)
Problem p02744 (python)

Fixed submission:
from collections import deque
N = int(input())

alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
unvisited = deque(alphabet[0 : N])
visited = deque()
st = deque([["a", visited, unvisited]]) # stack

while len(st) > 0:
    str, visited, unvisited = st.popleft()

    if str[-1] not in visited:
        visited.append(str[-1])
        unvisited.popleft()

    if len(str) == N:
        print(str)
    else:
        # すでに使った文字を使う
        for x in visited:
            st.append([str + x, deque(visited), deque(unvisited)])
        # まだ使っていない文字を使う
        if len(unvisited) > 0:
            x = unvisited[0]
            st.append([str + x, deque(visited), deque(unvisited)])

Buggy submission (verdict: Runtime Error):
from collections import deque
N = int(input())

alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
unvisited = deque(alphabet[0 : N])
visited = deque()
st = deque([["a", visited, unvisited]]) # stack

while len(st) > 0:
    str, visited, unvisited = st.popleft()

    if str[-1] not in visited:
        visited.append(str[-1])
        unvisited.popleft()

    if len(str) == N:
        print(str)
    else:
        # すでに使った文字を使う
        for x in visited:
            st.append([str + x, visited.copy(), unvisited.copy()])
        # まだ使っていない文字を使う
        if len(unvisited) > 0:
            x = unvisited[0]
            st.append([str + x, visited.copy(), unvisited.copy()])
Problem p02775 (python)

Fixed submission:
n = "0" + input()

ans = 0
n_dig = len(n) # Nの桁数

carry = 0
d_next = int(n[n_dig - 1])

for i in range(n_dig - 1, 0, -1):  # n[0]はダミーなのでn[1]で止める
    d = d_next
    d_next = int(n[i - 1])

    if carry + d <= 4 or (carry + d == 5 and d_next <= 4):
        ans += d + carry
        carry = 0  # 繰り上がり
    else:
        ans += 10 - d - carry
        carry = 1

print(ans + carry)

Buggy submission (verdict: Runtime Error):
import math
N = int(input())

n = N
ans = 0
n_dig = math.floor(math.log10(N)) + 1 # Nの桁数
mai = [0] * n_dig # 枚数

for i in range(n_dig):
    d = n % 10
    n //= 10
    if d < 1 + 10 - d:
        mai[i] = d
    else:
        mai[i] = d - 10 # おつり
        mai[i+1] = 1

    ans += min(d, 1 + 10 - d)
    if n == 0: break

print(sum([abs(m) for m in mai]))