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

Fixed submission:
from collections import deque

N, M = map(int, input().split())

X = [0 for _ in range(N+1)]
edges = [[] for _ in range(N+1)]

contradiction = False

for i in range(M):
    l, r, d = map(int, input().split())
    edges[l].append((r, d))
    edges[r].append((l, -d))

searched = set()
q = deque()

# assign x coordinate for each vertex
for i in range(N+1):
    if i in searched:
        continue
    q.append(i)
    searched.add(i)
    X[i] = 0
    while len(q) != 0:
        a = q.pop()
        for b, d in edges[a]:
            if b not in searched:
                q.append(b)
                searched.add(b)
                X[b] = X[a] + d
            else:
                if X[b] != X[a] + d:
                    contradiction = True
                    print("No")
                    break
        if contradiction:
            break
    if contradiction:
        break    
else:
    print("Yes")

Buggy submission (verdict: Runtime Error):
from collections import deque

N, M = map(int, input().split())

X = [0 for _ in range(N+1)]
edges = [[] for _ in range(M+1)]

contradiction = False

for i in range(M):
    l, r, d = map(int, input().split())
    edges[l].append((r, d))
    edges[r].append((l, -d))

searched = set()
q = deque()

# assign x coordinate for each vertex
for i in range(N+1):
    if i in searched:
        continue
    q.append(i)
    searched.add(i)
    X[i] = 0
    while len(q) != 0:
        a = q.pop()
        for b, d in edges[a]:
            if b not in searched:
                q.append(b)
                searched.add(b)
                X[b] = X[a] + d
            else:
                if X[b] != X[a] + d:
                    contradiction = True
                    print("No")
                    break
        if contradiction:
            break
    if contradiction:
        break    
else:
    print("Yes")

Buggy submission (verdict: Runtime Error):
from collections import deque

N, M = map(int, input().split())

X = [0 for _ in range(N+1)]
edges = [[] for _ in range(M+1)]

contradiction = False

for i in range(M):
    l, r, d = map(int, input().split())
    edges[l].append((r, d))
    edges[r].append((l, -d))

searched = set()
q = deque()

# assign x coordinate for each vertex
for i in range(N+1):
    if i in searched:
        continue
    q.append(i)
    searched.add(i)
    X[i] = 0
    while len(q) != 0:
        a = q.pop()
        for b, d in edges[a]:
            if b not in searched:
                q.append(b)
                searched.add(b)
                X[b] = X[a] + d
            else:
                if X[b] != X[a] + d:
                    contradiction = True
                    print("No")
                    break
        if contradiction:
            break
    if contradiction:
        break    
else:
    print("Yes")
Problem p03456 (python)

Fixed submission:
import math

a, b = input().split()
n = int(a+b)

m = int(math.sqrt(n))

if abs(m**2 - n) < 1e-6:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
import math

a, b = input().split()
n = int(a+b)

if math.abs(math.sqrt(n)**2 - n) < 1e-6:
    print("Yes")
else:
    print("No")
Problem p03472 (python)

Fixed submission:
import math

N, H = map(int, input().split())
A = []
B = []

for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

a_max = max(A)

B = sorted([x for x in B if x > a_max], reverse = True)

attack = 0

for damage in B:
    H -= damage
    attack += 1
    if H <= 0:
        break

if H > 0:
    attack += math.ceil(H / a_max)

print(attack)

Buggy submission (verdict: Runtime Error):
import math

N, H = map(int, input().split())
A = []
B = []

for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

A.sort()
A = A[::-1]
a_max = A[0]

B.sort()
B = B[::-1]

attack = 0
idx = 0

while B[idx] > a_max:
    H -= B[idx]
    attack += 1
    idx += 1
    if H <= 0:
        print(attack)
        break

if H > 0:
    attack += math.ceil(H / a_max)
    print(attack)

Buggy submission (verdict: Runtime Error):
N, H = map(int, input().split())
A = []
B = []

for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

A.sort()
A = A[::-1]
a_max = A[0]

B.sort()
B = B[::-1]

attack = 0
idx = 0

while B[idx] > a_max:
    H -= B[idx]
    attack += 1
    idx += 1
    if H <= 0:
        print(attack)
        break

if H > 0:
    attack += (H // a_max) + 1
    print(attack)
Problem p03475 (python)

Fixed submission:
# スペース区切りの整数の入力
N = int(input())
C = []
S = []
F = []

for i in range(N-1):
    c, s, f= map(int, input().split())
    C.append(c)
    S.append(s)
    F.append(f)

for i in range(N-1):
    time = 0
    for j in range(i, N-1):
        if time < S[j]:
            time = S[j]
        else:
            if time % F[j] != 0:
                time += F[j] - (time % F[j])
        time += C[j]
    print(time)
    
print(0)

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数の入力
N = int(input())
C = []
S = []
F = []

for i in range(N-1):
    c, s, f= map(int, input().split())
    C.append(c)
    S.append(s)
    F.append(f)

for i in range(N-1):
    time = 0
    for j in range(i, N-1):
        if time < S[j]:
            time = S[j]
        else:
            if time % F[j] != 0:
                time += F[j] - (time % F[j])
        time += C[j]
    print(t)
    
print(0)
Problem p03477 (python)

Fixed submission:
# スペース区切りの整数の入力
a, b, c, d= map(int, input().split())

left = a + b
right = c + d

if left == right:
    print("Balanced")
elif left > right:
    print("Left")
else:
    print("Right")

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数の入力
x, y= map(int, input().split())

lst = []

num = x

while num <= y:
    lst.append(num)
    num = 2*num

print(len(lst))
Problem p03480 (python)

Fixed submission:
S = input()
n = len(S)

prev = None
cand = []
for i in range(n):
    if i != 0:
        if S[i] != prev:
            cand.append(max(i, n-i))
    prev = S[i]    
if cand:
    print(min(cand))
else:
    print(n)

Buggy submission (verdict: Runtime Error):
S = input()
n = len(S)
if n == 1:
    print(1)
else:
    prev = None
    cand = []
    for i in range(n):
        if i != 0:
            if S[i] != prev:
                cand.append(max(i, n-i))
        prev = S[i]    

    print(min(cand))

Buggy submission (verdict: Runtime Error):
S = input()
n = len(S)
prev = None
cand = []
for i in range(n):
    if i != 0:
        if S[i] != prev:
            cand.append(max(i, n-i))
    prev = S[i]    

print(min(cand))

Buggy submission (verdict: Runtime Error):
# スペース区切りの整数の入力
bit = map(str, input().split())
length = len(bit)

num0 = 0
num1 = 0

for i in range(length):
    if bit[i] == '0':
        num0 += 1
    else:
        num1 += 1

K = max(num0, num1)
        
print(K)
Problem p03488 (python)

Fixed submission:
from collections import  defaultdict

s = input()
x, y = map(int, input().split())

s_splitted = s.split('T')
x -= len(s_splitted[0])

dist = {0: [], 1: []} # 0: x-direction, 1: y-direction
for i, Fs in enumerate(s_splitted[1:]):
    dist[(i+1) % 2].append(len(Fs))

reachable = {0: set([0]), 1: set([0])}

for i in range(2):
    for d in dist[i]:
        temp = []
        for r in reachable[i]:
            temp.append(r+d)
            temp.append(r-d)
        reachable[i] = set(temp)

if x in reachable[0] and y in reachable[1]:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import  defaultdict

s = input()
x, y = map(int, input().split())
N = len(s)

# 0: x-direction, 1: y-direction
dist = {0: [], 1: []}
current = 0
temp = 0
for move in s:
    if move == 'F':
        temp += 1
    else:
        dist[current].append(temp)
        temp = 0
        current ^= 1
if temp > 0:
    dist[current].append(temp)

xdp = {i: defaultdict(bool) for i in range(len(dist[0]))}
ydp = {i: defaultdict(bool) for i in range(len(dist[1]))}

xdp[0][dist[0][0]] = True
for i in range(1, len(dist[0])):
    d = dist[0][i]
    for j in range(-N, N+1):
        xdp[i][j] = xdp[i-1][j-d] | xdp[i-1][j+d] 

ydp[0][dist[1][0]] = True
ydp[0][-dist[1][0]] = True
for i in range(1, len(dist[1])):
    d = dist[1][i]
    for j in range(-N, N+1):
        ydp[i][j] = ydp[i-1][j-d] | ydp[i-1][j+d] 

if xdp[len(dist[0])-1][x] & ydp[len(dist[1])-1][y]:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
from collections import  defaultdict

s = input()
x, y = map(int, input().split())
N = len(s)

# 0: x-direction, 1: y-direction
dist = {0: [], 1: []}
current = 0
temp = 0
for move in s:
    if move == 'F':
        temp += 1
    else:
        dist[current].append(temp)
        n_move[current] += 1
        temp = 0
        current ^= 1
if temp > 0:
    dist[current].append(temp)
    n_move[current] += 1
        
xdp = {i: defaultdict(bool) for i in range(len(dist[0]))}
ydp = {i: defaultdict(bool) for i in range(len(dist[1]))}

xdp[0][dist[0][0]] = True
for i in range(1, len(dist[0])):
    d = dist[0][i]
    for j in range(-N, N+1):
        xdp[i][j] = xdp[i-1][j-d] | xdp[i-1][j+d] 

ydp[0][dist[1][0]] = True
ydp[0][-dist[1][0]] = True
for i in range(1, len(dist[1])):
    d = dist[1][i]
    for j in range(-N, N+1):
        ydp[i][j] = ydp[i-1][j-d] | ydp[i-1][j+d] 

if xdp[len(dist[0])-1][x] & ydp[len(dist[1])-1][y]:
    print('Yes')
else:
    print('No')
Problem p03502 (python)

Fixed submission:
X = input()
print('Yes' if int(X) % sum(map(int, X)) == 0 else 'No')

Buggy submission (verdict: Runtime Error):
X = input()
print('Yes' if X % sum(map(int, X)) == 0 else 'No')
Problem p03609 (python)

Fixed submission:
X, t = map(int, input().split())
print(max(X - t, 0))

Buggy submission (verdict: Runtime Error):
X, t = map(int, input(),split())
print(max(X - t, 0))
Problem p03611 (python)

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

maximum = max(A)
count = [0]*(maximum + 2)

for i in range(N):
    if A[i] == 0:
        count[0] += 1
        count[1] += 1
        
    elif A[i] == maximum:
        count[A[i]] += 1
        count[A[i] - 1] += 1
    
    else:
        count[A[i] + 1] += 1
        count[A[i]] += 1
        count[A[i] - 1] += 1

print(max(count))

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

maximum = max(A)
count = [0]*(maximum + 1)

for i in range(N):
    if A[i] == 0:
        count[0] += 1
        count[1] += 1
        
    elif A[i] == maximum:
        count[A[i]] += 1
        count[A[i] - 1] += 1
    
    else:
        count[A[i] + 1] += 1
        count[A[i]] += 1
        count[A[i] - 1] += 1

print(max(count))

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

count = [0]*(max(A) + 1)

for i in range(N):
    if A[i] == 0:
        count[0] += 1
        count[1] += 1
        
    elif A[i] == max(A):
        count[A[i]] += 1
        count[A[i] - 1] += 1
    
    else:
        count[A[i] + 1] += 1
        count[A[i]] += 1
        count[A[i] - 1] += 1

print(max(count))

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

count = [0 for i in range(max(A) + 1)]
for i in range(N):
    if A[i] == 0:
        count[A[i]] += 1
        count[A[i] + 1] += 1
    elif A[i] == max(A):
        count[A[i]] += 1
        count[A[i] - 1] += 1
    else:
        count[A[i] + 1] += 1
        count[A[i]] += 1
        count[A[i] - 1] += 1
print(max(count))
Problem p03612 (python)

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

dist = [P[i-1] - i for i in range(1, N+1)]

count = 0

for i in range(N):
    if dist[i] == 0:
        count += 1
        if i < N-1:
            dist[i] -= 1
            dist[i+1] += 1
        else: # i == N-1
            dist[i] += 1
            dist[i-1] -= 1
    
print(count)

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

dist = [P[i-1] - i for i in range(1, N+1)]

count = 0

for i in range(N):
    if dist[i] == 0:
        count += 1
        dist[i] -= 1
        dist[i+1] += 1
    elif i == N-1 and dist[i] == 0:
        count += 1
        dist[i] += 1
        dist[i-1] -= 1
        
print(count)

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

dist = [P[i-1] - i for i in range(1, N+1)]

count = 0

for i in range(N):
    if dist[i] == 0:
        count += 1
        dist[i] -= 1
        dist[i+1] += 1
    elif i == N-1 and dist[i+1] == 0:
        count += 1
        dist[i] -= 1
        dist[i+1] += 1
        
print(count)
Problem p03625 (python)

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

bar_length_count = dict()
for a in A:
    bar_length_count[a] = bar_length_count.get(a, 0) + 1

for val in bar_length_count.values():
    pass

count_at_least_2 = 0
for val in bar_length_count.values():
    if val >= 2:
        if val >= 4:
            count_at_least_2 += 2
        else:
            count_at_least_2 += 1

if count_at_least_2 < 2:
    import sys
    print(0)
    sys.exit(0)

else:
    bar_length_count = sorted({k:v for k, v in bar_length_count.items() if v >= 2}.items(), key=lambda x: x[0], reverse=True)
    if bar_length_count[0][1] >= 4:
        print(bar_length_count[0][0]**2)
    else:
        print(bar_length_count[0][0]*bar_length_count[1][0])

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

bar_length_count = dict()
for a in A:
    bar_length_count[a] = bar_length_count.get(a, 0) + 1

for val in bar_length_count.values():
    pass

count_at_least_2 = 0
for val in bar_length_count.values():
    if val >= 2:
        if val >= 4:
            count_at_least_2 += 2
        else:
            count_at_least_2 += 1

if count_at_least_2 < 2:
    import sys
    print(0)
    sys.exit(0)

else:
    bar_length_count = sorted({k:v for bar_length_count.items() if v >= 2}.items(), key=lambda x: x[0], reverse=True)
    if bar_length_count[0][1] >= 4:
        print(bar_length_count[0][0]**2)
    else:
        print(bar_length_count[0][0]*bar_length_count[1][0])

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

bar_length_count = dict()
for a in A:
    bar_length_count[a] = bar_length_count.get(a, 0) + 1

for val in bar_length_count.values():
    pass

count_at_least_2 = 0
for val in bar_length_count.values():
    if val >= 2:
        if val >= 4:
            count_at_least_2 += 2
        else:
            count_at_least_2 += 1

if count_at_least_2 < 2:
    import sys
    print(0)
    sys.exit(0)

else:
    bar_length_count = sorted({k:v for bar_length_count.items() if v >= 2}, reverse=True)
    if bar_length_count[0][1] >= 4:
        print(bar_length_count[0][0]**2)
    else:
        print(bar_length_count[0][0]*bar_length_count[1][0])

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

bar_length_count = dict()
for a in A:
    bar_length_count[a] = bar_length_count.get(a, 0) + 1

for val in bar_length_count.values():
    pass

count_at_least_2 = 0
for val in bar_length_count.values():
    if val >= 2:
        if val >= 4:
            count_at_least_2 += 2
        else:
            count_at_least_2 += 1

if count_at_least_2 < 2:
    import sys
    print(0)
    sys.exit(0)

else:
    bar_length_count = sorted([(k, v) for bar_length_count.items() if v >= 2], reverse=True)
    if bar_length_count[0][1] >= 4:
        print(bar_length_count[0][0]**2)
    else:
        print(bar_length_count[0][0]*bar_length_count[1][0])
Problem p03651 (python)

Fixed submission:
N, K = map(int, input().split())
*A, = map(int, input().split())
A = sorted(A)
if K in A:
    print('POSSIBLE')
else:
    temp = [A[i+1] - A[i] for i in range(N-1) if A[i+1] - A[i] > 0]
    diff = min(temp) if temp else -1
    if diff == -1:
        print('IMPOSSIBLE')
    else:
        for a in [a for a in A if a >= K]:
            if (a - K) % diff == 0:
                print('POSSIBLE')
                break
        else:
            print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
*A, = map(int, input().split())
A = sorted(A)
diff = min([A[i+1] - A[i] for i in range(N-1) if A[i+1] - A[i] > 0])
for a in [a for a in A if a >= K]:
    if (a - K) % diff == 0:
        print('POSSIBLE')
        break
else:
    print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
*A, = map(int, input().split())
A = sorted(A)
diff = min([A[i+1] - A[i] for i in range(N-1)])
for a in [a for a in A if a >= K]:
    if (a - K) % diff == 0:
        print('POSSIBLE')
        break
else:
    print('IMPOSSIBLE')
Problem p03676 (python)

Fixed submission:
MOD = 10**9 + 7
n = int(input())
A = list(map(int, input().split()))

fact = {i: None for i in range(n+2)}                 # n!
inverse = {i: None for i in range(1, n+2)}       # inverse of n in the field Z/(MOD)Z
fact_inverse = {i: None for i in range(n+2)}  # inverse of n! in the field Z/(MOD)Z

fact[0] = fact[1] = 1
fact_inverse[0] = fact_inverse[1] = 1
inverse[1] = 1
for i in range(2, n+2):
    fact[i] = i * fact[i-1] % MOD
    inverse[i] = - inverse[MOD % i] * (MOD // i) % MOD
    fact_inverse[i] = inverse[i] * fact_inverse[i-1] % MOD

def combination(n, r):
    if n < r or n < 0 or r < 0:
        return 0
    else:
        return fact[n] * (fact_inverse[r] * fact_inverse[n-r] % MOD) % MOD

dup_num = sum(A) - n*(n+1)//2
dup_idx = []
for i, a in enumerate(A):
    if a == dup_num:
        dup_idx.append(i)
        
left, right = dup_idx[0], dup_idx[1]
        
for k in range(1, n+2):
    print((combination(n+1, k) - combination(n + 1 - (right - left + 1), k - 1)) % MOD)

Buggy submission (verdict: Runtime Error):
# ARC 77 D
MOD = 10**9 + 7
n = int(input())
A = list(map(int, input().split()))

fact = {i: None for i in range(n+1)}                 # n!
inverse = {i: None for i in range(1, n+1)}       # inverse of n in the field Z/(MOD)Z
fact_inverse = {i: None for i in range(n+1)}  # inverse of n! in the field Z/(MOD)Z

fact[0] = fact[1] = 1
fact_inverse[0] = fact_inverse[1] = 1
inverse[1] = 1
for i in range(2, n+1):
    fact[i] = i * fact[i-1] % MOD
    inverse[i] = - inverse[MOD % i] * (MOD // i) % MOD
    fact_inverse[i] = inverse[i] * fact_inverse[i-1] % MOD

def combination(n, r):
    if n < r or n < 0 or r < 0:
        return 0
    else:
        return fact[n] * (fact_inverse[r] * fact_inverse[n-r] % MOD) % MOD

dup_num = sum(A) - n*(n+1)//2
dup_idx = []
for i, a in enumerate(A):
    if a == dup_num:
        dup_idx.append(i)
        
left, right = dup_idx[0], dup_idx[1]
        
for k in range(1, n+2):
    print((combination(n+1, k) - combination(n + 1 - (right - left + 1), k - 1)) % MOD)
Problem p03693 (python)

Fixed submission:
s = input().split()
print('YES' if int(''.join(s))%4 == 0 else 'NO')

Buggy submission (verdict: Runtime Error):
s = input().split()
print('YES' if str(''.join(s))%4 == 0 else 'NO')
Problem p03722 (python)

Fixed submission:
from collections import defaultdict

def bellman_ford(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = {i: defaultdict(int) for i in range(N)}
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    INF = float('inf')
    N = len(V)
    dist = [INF]*N
    dist[s] = 0
    for n_iterations in range(N):
        updated = [False]*N
        for v in V:
            d = dist[v]
            if d != INF:
                for u, c in E[v].items():
                    temp = d + c
                    if dist[u] > temp:
                        updated[u] = True
                        dist[u] = temp
        if sum(updated) == 0: # the minimum costs are already stored in dist
            break
    else: # the above loop is not broken if and only if there exists a negative cycle
        if updated[N-1]: return None
        else: return dist
    return dist

N, M = map(int, input().split())
E = {i: defaultdict(int) for i in range(N)}
for _ in range(M):
    a, b, c = map(int, input().split())
    E[a-1][b-1] = -c
V = list(range(N))
dist = bellman_ford(V, E, 0)
if dist is None:
    print('inf')
else:
    print(-dist[N-1])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

def bellman_ford(V, E, s):
    '''
    Input:
        V = list(range(N))
        E = {i: defaultdict(int) for i in range(N)}
        s (in V) : start vertex
    Output:
        dist: the list of the minimum cost from s to arbitrary vertex v in the given graph
    '''
    INF = float('inf')
    N = len(V)
    dist = [INF]*N
    dist[s] = 0
    for n_iterations in range(N):
        updated = [False]*N
        for v in V:
            d = dist[v]
            if d != INF:
                for u, c in E[v].items():
                    temp = d + c
                    if dist[u] > temp:
                        updated[u] = True
                        dist[u] = temp
                        prev[u] = v
        if sum(updated) == 0: # the minimum costs are already stored in dist
            break
    else: # the above loop is not broken if and only if there exists a negative cycle
        if updated[N-1]: return None
        else: return dist
    return dist

N, M = map(int, input().split())
E = {i: defaultdict(int) for i in range(N)}
for _ in range(M):
    a, b, c = map(int, input().split())
    E[a-1][b-1] = -c
V = list(range(N))
dist = bellman_ford(V, E, 0)
if dist is None:
    print('inf')
else:
    print(-dist[N-1])
Problem p03919 (python)

Fixed submission:
from itertools import product

H, W = map(int, input().split())
table = [input().split() for _ in range(H)]
for i, j in product(range(H), range(W)):
    if table[i][j] == 'snuke': print('ABCDEFGHIJKLMNOPQRSTUVWXYZ'[j] + str(i+1)); break

Buggy submission (verdict: Runtime Error):
from itertools import product

H, W = map(int, input().split())
table = [input().split() for _ in range(H)]
for i, j in product(range(H), range(W)):
    if table[i][j] == 'snuke': print('ABCDEFGHIJ'[j] + str(i+1)); break
Problem p03994 (python)

Fixed submission:
S = input()
K = int(input())
length = len(S)
alpha, n = 'abcdefghijklmnopqrstuvwxyz', 26
i = 0
ans = ''
while i < length:
    diff = (n - (ord(S[i]) - ord('a'))) % 26
    
    if 0 <= diff <= K:
        ans += 'a'
        K -= diff
    else:
        ans += S[i]
    i += 1
if K: ans = ans[:-1] + chr(ord('a') + (ord(ans[-1]) - ord('a') + K) % 26)
print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
K = int(input())
length = len(S)
alpha, n = 'abcdefghijklmnopqrstuvwxyz', 26
i = 0
ans = ''
while i < length:
    diff = (n - (ord(S[i]) - ord('a'))) % 26
    
    if 0 <= diff <= K:
        ans += 'a'
        K -= diff
    else:
        ans += S[i]
        
    i += 1
    if K == 0:
        ans += S[i:]
else:
    ans = ans[:-1] + chr(ord(ans[-1]) + K % 26)
print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
K = int(input())
length = len(S)
alpha, n = 'abcdefghijklmnopqrstuvwxyz', 26
i = 0
ans = ''
while i < length:
    diff = (n - (ord(S[i]) - ord('a'))) % 26
    
    if 0 <= diff <= K:
        ans += 'a'
        K -= diff
    else:
        ans += S[i]
        
    i += 1
    if K == 0:
        ans += S[i:]
else:
    print(K)
    ans = ans[:-1] + chr(ord(ans[-1]) + K % 26)
print(ans)