Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
#B
N, K = [int(x) for x in input().split()]
#print(N, K)#
def count(n):
    if n<=0: return 1
    else:  return (K-1)*count(n-1)
print(K*count(N-1))

Fixed submission:
#B 解2
N, K = [int(x) for x in input().split()]
#print(N, K)#
print(K*(K-1)**(N-1))

Buggy python submission (verdict: Runtime Error):
#B
N, K = [int(x) for x in input().split()]
#print(N, K)#
def count(n):
    if n<=0: return 1
    else:  return (K-1)*count(n-1)
print(K*count(N-1))

Fixed submission:
#B 解2
N, K = [int(x) for x in input().split()]
#print(N, K)#
print(K*(K-1)**(N-1))
Buggy python submission (verdict: Runtime Error):
N = int(input())
transporter = [0]*5
transporter_lag = [0]*5
for i in range(5):
    transporter[i] = int(input())
for i in range(1,5):
    if transporter[i] >= transporter[i-1]:
        transporter_lag[i] = 0
    else :
        transporter_lag[i] = math.ceil(transporter[i-1]/transporter[i]) - 1
if math.ceil(N/transporter[0]) <= 5:
    ans = 5+ sum(transporter_lag)
else :
    ans = math.ceil(N/transporter[0]) + sum(transporter_lag) + 4
print(ans)

Fixed submission:
import math
N = int(input())
transporter = [0]*5
for i in range(5):
    transporter[i] = int(input())
min_trans = min(transporter)
ans = math.ceil(N/min_trans) - 1 + 5
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
in_str = input()
AC_count = []
r = []
for i in range(Q):
    in1, in2 = map(int, input().split())
    AC_count.append(in_str.count("AC", in1-1, in2))

for i in range(Q):
    print(AC_count[i])

Fixed submission:
N, Q = map(int, input().split())
in_str = input()
AC_count = []
r = [0]*(N+1)
for i in range(N):
    r[i+1] = r[i] + (1 if in_str[i:i+2] == "AC" else 0) 
    
    
for i in range(Q):
    in1, in2 = map(int, input().split())
    print(r[in2-1]-r[in1-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
in_str_root = input()
l = []
r = []
for i in range(Q):
    in1, in2 = map(int, input().split())
    l.append(in1)
    r.append(in2)

for i in range(Q):
    in_str = in_str_root[l[i]-1:r[i]]
    AC = "AC"
    AC_count = in_str.count(AC)
    print(AC_count)

Fixed submission:
N, Q = map(int, input().split())
in_str = input()
AC_count = []
r = [0]*(N+1)
for i in range(N):
    r[i+1] = r[i] + (1 if in_str[i:i+2] == "AC" else 0) 
    
    
for i in range(Q):
    in1, in2 = map(int, input().split())
    print(r[in2-1]-r[in1-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
in_str_root = input()
l = []
r = []
for i in range(Q):
    in1, in2 = map(int, input().split())
    l.append(in1)
    r.append(in2)

for i in range(Q):
    in_str = in_str_root[l[i]-1:r[i]]
    #print(in_str)
    AC_count = 0
    searching_a = 1
    searching_c = 0
    if len(in_str) <= 1:
        print(0)
    else:
        for j in range(len(in_str)-1):
            if in_str[j] == "A":
                if in_str[j+1] == "C":
                    AC_count += 1
        
        print(AC_count)

Fixed submission:
N, Q = map(int, input().split())
in_str = input()
AC_count = []
r = [0]*(N+1)
for i in range(N):
    r[i+1] = r[i] + (1 if in_str[i:i+2] == "AC" else 0) 
    
    
for i in range(Q):
    in1, in2 = map(int, input().split())
    print(r[in2-1]-r[in1-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
in_str_root = input()
l = []
r = []
for i in range(Q):
    in1, in2 = map(int, input().split())
    l.append(in1)
    r.append(in2)

for i in range(Q):
    in_str = in_str_root[l[i]-1:r[i]]
    #print(in_str)
    AC_count = 0
    searching_a = 1
    searching_c = 0
    for j in range(len(in_str)):
        if searching_a == 1 and in_str[j] == "A":
            searching_a =0
            searching_c = 1
        elif searching_c == 1 and in_str[j] == "C":
            AC_count +=1
            searching_a =1
            searching_c = 0
        else :
            searching_a =1
            searching_c = 0
    print(AC_count)

Fixed submission:
N, Q = map(int, input().split())
in_str = input()
AC_count = []
r = [0]*(N+1)
for i in range(N):
    r[i+1] = r[i] + (1 if in_str[i:i+2] == "AC" else 0) 
    
    
for i in range(Q):
    in1, in2 = map(int, input().split())
    print(r[in2-1]-r[in1-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    a_next, b_next = map(int, input().split())
    a.append(a_next)
    b.append(b_next)

for i in range(1000000):
    min_index = a.index(min(a))
    if m - b[min_index] <=0:
        break
    m = m - b[min_index]
    ans += a[min_index]*b[min_index]
    del a[min_index]
    del b[min_index]

ans += m * a[min_index]
print(ans)

Fixed submission:
import numpy as np
n, m = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    a_next, b_next = map(int, input().split())
    a.append(a_next)
    b.append(b_next)

a_sorted_index = np.argsort(a)

for i in range(1000000):
    min_index = a_sorted_index[i]
    if m - b[min_index] <=0:
        break
    m = m - b[min_index]
    ans += a[min_index]*b[min_index]

ans += m * a[min_index]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    a_next, b_next = map(int, input().split())
    a.append(a_next)
    b.append(b_next)

while True:
    min_index = a.index(min(a))
    if m - b[min_index] <=0:
        break
    m = m - b[min_index]
    ans += a[min_index]*b[min_index]
    del a[min_index]
    del b[min_index]

ans += m * a[min_index]
print(ans)

Fixed submission:
import numpy as np
n, m = map(int, input().split())
a = []
b = []
ans = 0
for i in range(n):
    a_next, b_next = map(int, input().split())
    a.append(a_next)
    b.append(b_next)

a_sorted_index = np.argsort(a)

for i in range(1000000):
    min_index = a_sorted_index[i]
    if m - b[min_index] <=0:
        break
    m = m - b[min_index]
    ans += a[min_index]*b[min_index]

ans += m * a[min_index]
print(ans)
Buggy python submission (verdict: Runtime Error):
a, b, k = input().split()
a = int(a)
b = int(b)
k = int(k)
a_list = []
b_list = []
for i in range(1, a):
    i
    if a % i == 0:
        a_list.append(i)

for i in range(1, b):
    i
    if b % i == 0:
        b_list.append(i)

a_list.extend(b_list)
co_divisor = list(set(a_list))
print(co_divisor[k-1])

Fixed submission:
a, b, k = input().split()
a = int(a)
b = int(b)
k = int(k)
a_list = []
b_list = []
for j in range(a):
    i = j + 1
    if a % i == 0:
        a_list.append(i)
for j in range(b):
    i = j + 1
    if b % i == 0:
        b_list.append(i)

#a_list.extend(b_list)
#co_divisor = list(set(a_list))
a_list = set(a_list)
b_list = set(b_list)
co_divisor = list(a_list & b_list)
co_divisor.sort(reverse=True)
print(co_divisor[k-1])
Buggy python submission (verdict: Runtime Error):
s = str(input())
i = 0
score = 0
str_length = len(s)
while True:
    str_length = len(s)
    if str_length == 2:
        if int(s[0]) ^ int(s[1]) == 1:
            score += 2
            print(score)
            exit()
        else:
            print(score)
            exit()
    i = 0
    while True:
        j = i + 1
        cur_num = int(s[j])
        prev_num = int(s[j-1])
        next_num = int(s[j+1])
        if (cur_num ^ prev_num) == 1:
            score += 2
            s = s[:j-1] + s[j+1:]
            break
            
        elif (cur_num ^ prev_num) == 1:
            score += 2
            s = s[:j] + s[j+2:]
            break
        else :
            i += 1
            if i == str_length:
                print(score)
                exit()
        

Fixed submission:
s = input()
zero = s.count("0")
one = s.count("1")
print(min(zero, one)*2)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = []
a.append([0,0])
for i in range(n):
    in1, in2 = map(float, input().split())
    a.append([in1,in2])

ans = 0

for i in range(n):
    if a[i][1] == 'BTC':
        ans += a[i][0]*380000
    else :
        ans += a[i][0]
print(ans)

Fixed submission:
n = int(input())
a = []
a.append([0,0])
for i in range(n):
    in1, in2 = map(str, input().split())
    a.append([float(in1),in2])

ans = 0

for i in range(n):
    j = i + 1
    if a[j][1] == 'BTC':
        ans += a[j][0]*380000
    else :
        ans += a[j][0]
print(ans)
Buggy python submission (verdict: Runtime Error):
import random
N = int(input())
A = list(map(int, input().split()))

while len(A) != 1:
    print(A)
    attack_monster = random.randint(0,10**9) % len(A)
    damage_monster = random.randint(0,10**9) % len(A)
    if attack_monster != damage_monster:
        A[damage_monster] = A[damage_monster] - A[attack_monster]
        if A[damage_monster] <= 0:
            del A[damage_monster]
print(A[0])

Fixed submission:
def euclid_devider(num_devided, num_devider):
    while num_devided % num_devider != 0:
        temp = num_devided % num_devider
        num_devided = num_devider
        num_devider = temp
    return num_devider

N = int(input())
A = list(map(int, input().split()))
ans = A[0]
for i in range(1,N):
    ans = euclid_devider(A[i], ans)
print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])
X_distance.sort(reverse=True)

if N == M:
    print(0)
elif M == 1:
    print(0)
else :
    for i in range(N-1):
        X_distance[i] = 0
    print(sum(X_distance))

Fixed submission:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])
X_distance.sort(reverse=True)

if N >= M:
    print(0)
elif M == 1:
    print(0)
else :
    for i in range(N-1):
        X_distance[i] = 0
    print(sum(X_distance))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])

if N == M:
    print(0)
else :
    for i in range(N-1):
        max_index = X_distance.index(max(X_distance))
        X_distance[max_index] = 0
    print(sum(X_distance))

Fixed submission:
N, M = map(int, input().split())
X = list(map(int, input().split()))
X.sort()
X_distance = [0]*(M-1)
for i in range(M-1):
    X_distance[i] = abs(X[i]-X[i+1])
X_distance.sort(reverse=True)

if N >= M:
    print(0)
elif M == 1:
    print(0)
else :
    for i in range(N-1):
        X_distance[i] = 0
    print(sum(X_distance))
Buggy python submission (verdict: Runtime Error):
N, T = map(int, input().split())
cost = []
time = []
for i in range(N):
    c, t = map(int, input().split())
    if t <= T:
        cost.append(c)
        time.append(t)
min_time_index = time.index(min(time))
print(cost[min_time_index])

Fixed submission:
N, T = map(int, input().split())
cost = []
time = []
for i in range(N):
    c, t = map(int, input().split())
    if t <= T:
        cost.append(c)
        time.append(t)
if len(time) == 0:
    print("TLE")
else:
    #min_time_index = time.index(min(time))
    #print(cost[min_time_index])
    print(min(cost))
Buggy python submission (verdict: Runtime Error):
import math

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])
    
ans = X_diff[0]
for i in range(1,N):
    ans = gcd(ans,X_diff[i])

print(ans)

Fixed submission:
def gcd(a,b):   #　ユーグリッドの互除法
    if b == 0:
        return a
    return gcd(b, a % b)

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])
    
ans = X_diff[0]
for i in range(1,N):
    ans = gcd(ans,X_diff[i])

print(ans)

Buggy python submission (verdict: Runtime Error):
import math

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])
    
ans = X_diff[0]
for i in range(1,N):
    ans = math.gcd(ans,X_diff[i])

print(ans)

Fixed submission:
def gcd(a,b):   #　ユーグリッドの互除法
    if b == 0:
        return a
    return gcd(b, a % b)

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])
    
ans = X_diff[0]
for i in range(1,N):
    ans = gcd(ans,X_diff[i])

print(ans)

Buggy python submission (verdict: Runtime Error):
import math

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])

ans = reduce(math.gcd, X_diff)
print(ans)

Fixed submission:
def gcd(a,b):   #　ユーグリッドの互除法
    if b == 0:
        return a
    return gcd(b, a % b)

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])
    
ans = X_diff[0]
for i in range(1,N):
    ans = gcd(ans,X_diff[i])

print(ans)

Buggy python submission (verdict: Runtime Error):
import functools
import math
N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])

ans = functools.reduce(math.gcd, X_diff)
print(ans)

Fixed submission:
def gcd(a,b):   #　ユーグリッドの互除法
    if b == 0:
        return a
    return gcd(b, a % b)

N, X = map(int,input().split())
city = list(map(int,input().split()))
X_diff = [0]*N
for i in range(N):
    X_diff[i] = abs(X-city[i])
    
ans = X_diff[0]
for i in range(1,N):
    ans = gcd(ans,X_diff[i])

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = list(map(int, input().split()))


gcd_l = [0] * n
gcd_r = [0] * n
for i in range(n):
    if i == 0:
        gcd_l[i] = A[i]
        gcd_r[-i-1] = A[-i-1]
    else:
        gcd_l[i] = gcd(gcd_l[i-1], A[i])
        gcd_r[-i-1] = gcd(gcd_r[-i], A[-i-1])

ans = [0] * n
ans[0] = gcd_r[1]
ans[n-1] = gcd_l[n-2]
for i in range(1, n-1):
    ans[i] = gcd(gcd_l[i-1], gcd_r[i+1])
print(max(ans))


Fixed submission:
from fractions import gcd


n = int(input())
A = list(map(int, input().split()))


gcd_l = [0] * n
gcd_r = [0] * n
for i in range(n):
    if i == 0:
        gcd_l[i] = A[i]
        gcd_r[-i-1] = A[-i-1]
    else:
        gcd_l[i] = gcd(gcd_l[i-1], A[i])
        gcd_r[-i-1] = gcd(gcd_r[-i], A[-i-1])

ans = [0] * n
ans[0] = gcd_r[1]
ans[n-1] = gcd_l[n-2]
for i in range(1, n-1):
    ans[i] = gcd(gcd_l[i-1], gcd_r[i+1])
print(max(ans))
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 998244353

n = int(input())
A = [int(input()) for _ in range(n)]

S = sum(A)
memo0 = [0] * (S+1)
memo1 = [0] * (S+1)
memo0[0] = 1
memo1[0] = 1


# なんで大きい方から小さくしていくのか
def dp0():
    for i in range(n):
        for s in range(S, A[i]-1, -1):
            memo0[s] = (memo0[s] + memo0[s-A[i]]*2) % MOD


# 辺を2グループにわけるDP
def dp1():
    for i in range(n):
        for s in range(S, A[i]-1, -1):
            memo1[s] = (memo1[s] + memo1[s-A[i]]) % MOD


dp0()
dp1()
# 実装方法を変える
ans = pow(3, n, MOD)
# 解から三角形が存在しない通りを引く
for l in range(0, S//2+1):
    ans = (ans - memo0[l]*3) % MOD
# 引きすぎた分を引く
if S % 2 == 0:
    ans = (ans + memo1[S//2]*3) % MOD


print(ans)


Fixed submission:
MOD = 998244353

n = int(input())
A = [int(input()) for _ in range(n)]

S = sum(A)
memo0 = [0] * (S+1)
memo1 = [0] * (S+1)
memo0[0] = 1
memo1[0] = 1


# なんで大きい方から小さくしていくのか
def dp0():
    for i in range(n):
        for s in range(S, A[i]-1, -1):
            memo0[s] = (memo0[s] + memo0[s-A[i]]*2) % MOD


# 辺を2グループにわけるDP
def dp1():
    for i in range(n):
        for s in range(S, A[i]-1, -1):
            memo1[s] = (memo1[s] + memo1[s-A[i]]) % MOD


dp0()
dp1()
# 実装方法を変える
ans = pow(3, n, MOD)
# 解から三角形が存在しない通りを引く
for l in range(0, S//2+1):
    ans = (ans - memo0[l]*3) % MOD
# 引きすぎた分を引く
if S % 2 == 0:
    ans = (ans + memo1[S//2]*3) % MOD


print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
B = list(map(int, input().split()))

def dfs(stack, B):
    if len(B)==1:
        if B[0]==1:
            stack.append(1)
            return stack
        else: return None
    else:
        tmp = []
        flag = False
        for i, b in enumerate(B):
            stack_ = stack + [b]
            B_ = B[:i] + B[i+1:]
            if b==i+1: tmp.append(dfs(stack_, B_))
        for tp in tmp:
            if tp is not None:
                flag = True
                ans = tp
        if flag:
            return ans
        else:
            return None
ans = dfs([], B)
if ans is None:
    print('-1')
else:
    ans = ans[::-1]
    for i in range(N):
        print(ans[i])

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

stack = []
for i in range(N):
    tmp = []
    flag = True
    for j in range(len(B)):
        if B[j]==j+1:
            tmp.append(j)
    if tmp:
        tmp.sort(reverse=True)
        ans = tmp[0]
        B = B[:ans] + B[ans+1:]
        stack.append(ans+1)
    else:
        flag = False
        break
if flag:
    stack = stack[::-1]
    for i in stack:
        print(i)
else:
    print(-1)
Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce
N, o = list(map(int, input().split()))
X = list(map(int, input().split()))
X = sorted(X)
X = [abs(x-o) for x in X]
# 最大公約数が答え
print(reduce(math.gcd, X))

Fixed submission:
import fractions
from functools import reduce
N, o = list(map(int, input().split()))
X = list(map(int, input().split()))
X = sorted(X)
X = [abs(x-o) for x in X]
# 最大公約数が答え
print(reduce(fractions.gcd, X))
Buggy python submission (verdict: Runtime Error):
from scipy.special import comb
N, K = list(map(int, input().split()))
if K % 2 == 0:
    num = (N//K)**3 + (N//int(K/2)-N//K)**3
else:
    k = int(K)
    num = (N//k)**3

print(num)

Fixed submission:
N, K = list(map(int, input().split()))
if K % 2 == 0:
    num = (N//K)**3 + (N//int(K/2)-N//K)**3
else:
    num = (N//K)**3

print(num)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
X = list(map(int, input().split()))
cost = []
for i in range(0, N-K+1):
    window = X[i:i+K]
    cost.append(window[-1]-window[0] + min(abs(window[-1]), abs(window[0])))
print(min(cost))

Fixed submission:
N, K = map(int, input().split())
X = list(map(int, input().split()))
cost = []
for i in range(0, N-K+1):
    #window = X[i:i+K]
    #cost.append(window[-1]-window[0] + min(abs(window[-1]), abs(window[0])))
    cost.append(X[i+K-1]-X[i] + min(abs(X[i+K-1]), abs(X[i])))
print(min(cost))
Buggy python submission (verdict: Runtime Error):
def dfs(cost, N):
    if N<6 :
        return cost+N
    else:
        ret0 = 6 ** math.floor(math.log(N, 6))
        ret1 = 9 ** math.floor(math.log(N, 9))
        if (ret0>1) and (ret1>1):
            return min(dfs(cost+1, N-ret0), dfs(cost+1, N-ret1))
        else:
            ret = max(ret0, ret1)
            return dfs(cost+1, N-ret)

N = int(input())
print(dfs(0, N))

Fixed submission:
N = int(input())
if N<=999:
    print('ABC')
else:
    print('ABD')
Buggy python submission (verdict: Runtime Error):
def dfs(cost, N):
    if N<6 :
        return cost+N
    else:
        ret0 = 6 ** math.floor(math.log(N, 6))
        ret1 = 9 ** math.floor(math.log(N, 9))
        if (ret0>1) and (ret1>1):
            return min(dfs(cost+1, N-ret0), dfs(cost+1, N-ret1))
        else:
            ret = max(ret0, ret1)
            return dfs(cost+1, N-ret)

N = int(input())
print(dfs(0, N))

Fixed submission:
import sys
sys.setrecursionlimit(10**6) 

def memorize(f):
    table = {}
    def func(*args):
        if not args in table:
            table[args] = f(*args)
        return table[args]
    return func

@memorize
def memDFS(n):
    if n==0:
        return 0
    else:
        res = n
        pow6 = 1
        while(pow6<=n):
            res = min(res, memDFS(n-pow6)+1)
            pow6 *= 6
        pow9 = 1
        while(pow9<=n):
            res = min(res, memDFS(n-pow9)+1)
            pow9 *= 9
        return res

N = int(input())
print(memDFS(N))
Buggy python submission (verdict: Time Limit Exceeded):
N, S = int(input()), str(input())
S = S.translate(str.maketrans({'E':'0', 'W':'1'}))
cost = N
for i in range(N):
    res = '0'* (i-1) + '1' * (N-1-i)
    S_test = S[:i-1]+ S[i+1:]
    bit = int(res, 2)^int(S_test, 2)
    cost = min(cost, bin(bit).count('1'))
print(cost)

Fixed submission:
N, S = int(input()), str(input())
for i in range(N):
    if i == 0:
        res = S[1:].count('E')
        cost = res
    else:
        if S[i-1] == 'W': res += 1
        if S[i] == 'E': res -= 1
    cost = min(cost, res)
print(cost)
Buggy python submission (verdict: Time Limit Exceeded):
s = str(input())
K = int(input())
stack = []
for i in range(len(s)):
    for j in range(i+1, min(i+1+K, len(s))+1):
        sub = s[i:j]
        if sub not in stack:
            stack.append(sub)
stack.sort()
print(stack[K-1])

Fixed submission:
s = str(input())
K = int(input())
stack = []
for i in range(len(s)):
    for j in range(i+1, min(i+K, len(s))+1):
        sub = s[i:j]
        if sub not in stack:
            stack.append(sub)
stack.sort()
print(stack[K-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = str(input())
K = int(input())
stack = []
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        sub = s[i:j]
        if sub not in stack:
            stack.append(sub)
stack.sort()
print(stack[K-1])

Fixed submission:
s = str(input())
K = int(input())
stack = []
for i in range(len(s)):
    for j in range(i+1, min(i+K, len(s))+1):
        sub = s[i:j]
        if sub not in stack:
            stack.append(sub)
stack.sort()
print(stack[K-1])