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

Fixed submission:
import numpy as np
N, K = map(int, input().split())
M = 10**9+7
xs = []
for _ in range(N):
    xs.append(list(map(int, input().split())))
X = np.array(xs, dtype="object")
S = np.ones(N, dtype="object")
#Xs = [X]
for _ in range(60):
#    Xs.append(np.dot(Xs[-1],Xs[-1])%M)
    if K%2:
        S = np.dot(X, S)%M
    K = K//2
    X = np.dot(X, X)%M
S = np.array(S, dtype="int64")
r = 0
for s in S:
    r = (r + s)%M
print(r)

Buggy submission (verdict: Runtime Error):
import numpy as np
N, K = map(int, input().split())
M = 10**9+7
xs = []
for _ in range(N):
    xs.append(list(map(int, input().split())))
X = np.array(xs, dtype="int64")
S = np.ones(N, dtype="int64")
#Xs = [X]
for _ in range(60):
#    Xs.append(np.dot(Xs[-1],Xs[-1])%M)
    if K%2:
        S = np.dot(X, S)%M
    K = K//2
    X = np.dot(X, X)%M
r = 0
for s in S:
    r = (r + s)%M
print(r)
Problem p03183 (python)

Fixed submission:
N, = map(int, input().split())
Xs = []
for i in range(N):
    X, Y, Z = map(int, input().split())
    Xs.append((X,Y,Z,i))
Xs.sort(key=lambda x : x[0]+x[1])

dp = [0]*(2*(10**4)+1)
for x,y,z,i in Xs:
#    print(i+1,x,y,z)
    for i in range(y+1):
        i = y-i
        dp[i+x] = max(dp[i+x], dp[i]+z)
#    print(dp[:10])
print(max(dp))

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

N0 = 2**((N+1).bit_length())
INF = 10**18
data = [0]*(2*N0)
lazy = [0]*(2*N0)

def gindex(l, r):
    L = l + N0; R = r + N0
    lm = (L // (L & -L)) >> 1
    rm = (R // (R & -R)) >> 1
    while L < R:
        if R <= rm:
            yield R
        if L <= lm:
            yield L
        L >>= 1; R >>= 1
    while L:
        yield L
        L >>= 1

def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if not v:
            continue
        lazy[2*i-1] += v; lazy[2*i] += v
        data[2*i-1] += v; data[2*i] += v
        lazy[i-1] = 0
        
def update(l, r, x):
    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] += x; data[R-1] += x
        if L & 1:
            lazy[L-1] += x; data[L-1] += x
            L += 1
        L >>= 1; R >>= 1

    for i in gindex(l, r):
        data[i-1] = max(data[2*i-1], data[2*i]) + lazy[i-1]

def update2(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])

def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r
    s = -INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])
        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

xs = [0]*(N+1)
ys = [[] for _ in range(N+1)]
for _ in range(M):
    l, r, a = map(int, input().split())
    xs[l] += a
    ys[r].append((l,a))

for i in range(1, N+1):
    update(0,i,xs[i])
    update2(i, query(0, i))
    for l, a in ys[i]:
        update(0, l, -a)
print(query(0,N+1))
Problem p03185 (python)

Fixed submission:
def fi(i, x):
    a, b = lines[i]
    return a*x+b

def find(x):
    if len(lines) == 1:
        return 0
    def f(i):
        # 0<=i<len(lines)-1
        return fi(i+1,x) > fi(i,x)
    mn, mx = 0, len(lines)-2
    idx = (mn+mx)//2
    if f(mn):
        return 0
    if not f(mx):
        return mx+1
    while mx-mn>1:
        if f(idx):
            mx, idx = idx, (mn + idx)//2
            continue
        mn, idx = idx, (mx + idx)//2
    return idx+1

def remove_lines():
    (x, y) = lines.pop()
    a, b  = x, y
    (e, f) = lines[-1]
    while len(lines)-1:
        (c, d), (e, f) = (e, f), lines[-2]
        if (c-e)*(b-d) < (d-f)*(a-c):
            break
        lines.pop()
    lines.append((x, y))

def insert(a, b):
    lines.append((a, b))
    if len(lines) >= 3:
        remove_lines()

N, C = map(int, input().split())
hs = map(int, input().split())
r = 0
lines = []
for i, h in enumerate(hs):
    if i!= 0:
        r = fi(find(h), h) + h**2+C
    insert(-2*h, r+h**2)
print(r)

Buggy submission (verdict: Runtime Error):
lines = []
def fi(i, x):
    a, b = lines[i]
    return a*x+b

def find(x):
    def f(i):
        return fi(i+1,x) > fi(i,x)
    mn, mx = -1, len(lines)-1
    idx = (mn+mx)//2
    while mx-mn>1:
        if f(idx):
            mx, idx = idx, (mn + idx)//2
            continue
        mn, idx = idx, (mx + idx)//2
    return fi(idx+1, x)

def find2(x):
    # x が単調増加
    I = find2.I
    u, v = lines[I]
    while I+1<len(lines):
        w, y = lines[I+1]
        if u*x+v < w*x+y:
            break
        u, v = w, y
        I += 1
    find2.I = I
    return u*x+v
find2.I = 0

def insert(a, b):
    pass

def insert2(a, b):
    # a が単調減少
    if not lines:
        lines.append((a,b))
        return
    (e, f) = lines[-1]
    while len(lines)-1:
        (c, d), (e, f) = (e, f), lines[-2]
        if (c-e)*(b-d) < (d-f)*(a-c):
            break
        lines.pop()
    lines.append((a, b))

N, C = map(int, input().split())
hs = map(int, input().split())
r = 0
lines = []
for i, h in enumerate(hs):
    if i!= 0:
        r = fi(find(h), h) + h**2+C
    insert(-2*h, r+h**2)
print(r)
Problem p03216 (python)

Fixed submission:
N = int(input())
S = input().strip()
qn = int(input())
qs = list(map(int, input().split()))
dd,mm,cc = [0],[0],[0]
As = [0]*qn
d=m=dm=0
for i, w in enumerate(S):
    i += 1
    if w=="D":
        d+=1
    elif w == "M":
        m+=1
        dm+=d
    elif w == "C":
        for j, q in enumerate(qs):
            ll = max(i-q, 0)
            As[j] += dm - cc[ll] - dd[ll]*(m-mm[ll])
    dd.append(d)
    mm.append(m)
    cc.append(dm)
for a in As:
    print(a)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input().strip()
qn = int(input())
qs = list(map(int, input().split()))
dp = [[0,0,0]]
As = [0]*qn
d=m=dm=0
for i, w in enumerate(S):
    i += 1
    if w=="D":
        d+=1
    elif w == "M":
        m+=1
        dm+=d
    elif w == "C":
        for j, q in enumerate(qs):
            ll = max(i-q, 0)
            dd, mm, dmm = dd[ll]
            As[j] += dm - dmm - dd*(m-mm)
    dp.append([d,m,dm])
for a in As:
    print(a)
Problem p03223 (python)

Fixed submission:
N, = [int(i) for i in input().split(' ')]
import sys
As = []
for line in sys.stdin:
    As.append(int(line.strip()))

As = sorted(As)
r = 0
r2 = 0
if N == 2:
    r = As[1] - As[0]
elif N == 3:
    r = max(2*As[2] - As[1] - As[0], As[2] + As[1] - 2 * As[0])
elif N % 2 == 1:
    r -= sum(As[:(N//2) - 1]) * 2
    r -= As[(N // 2)] + As[(N // 2) - 1]
    r += sum(As[(N // 2)+1:]) * 2
    #print(r)

    r2 -= sum(As[:(N//2)]) * 2
    r2 += As[(N // 2)+1] + As[(N // 2)]
    r2 += sum(As[(N // 2)+2:]) * 2
    #print(r2)
    r = max(r, r2)
else:
    #print(As[:(N//2) - 1])
    r -= sum(As[:(N//2) - 1]) * 2
    r += As[(N // 2)] - As[(N // 2) - 1]
    r += sum(As[(N // 2)+1:]) * 2
    #r2 -= sum(As[:(N//2)]) * 2
    #r2 += As[(N // 2)+1] + As[(N // 2)]
    #r2 += sum(As[(N // 2)+2:]) * 2
    #r = max(r, r2)
print(r)

Buggy submission (verdict: Runtime Error):
import itertools
import sys
H, W = [int(i) for i in input().split(' ')]

cross = [[0] * (H + W -1) for _ in range(H + W - 1)]
for i, line in enumerate(sys.stdin):
    for j, c in enumerate(line.strip()):
        if c == "#":
            cross[i+j][i-j+W-1] = 1

across = [[0] + list(itertools.accumulate(xs)) for xs in cross]
r_cross = [[xs[i] for xs in cross] for i in range(H + W - 1)]
across2 = [[0] + list(itertools.accumulate(xs)) for xs in r_cross]

r = 0
for i in range(H+W-1):
    for j in range(H+W-1):
        if cross[i][j] != 1:
            continue
        for k in range(j+1, H+W-1):
            if cross[i][k] == 1:
                if i - (k-j) >= 0:
                    r += across[i - (k - j)][k+1] - across[i - (k - j)][j]
                if i + (k-j) < H + W - 1:
                    r += across[i + (k - j)][k+1] - across[i + (k - j)][j]

for i in range(H+W-1):
    for j in range(H+W-1):
        if cross[j][i] != 1:
            continue
        for k in range(j+1, H+W-1):
            if cross[k][i] == 1:
                if i - (k-j) >= 0:
                    r += across2[i - (k - j)][k] - across2[i - (k - j)][j+1]
                if i + (k-j) < H + W - 1:
                    r += across2[i + (k - j)][k] - across2[i + (k - j)][j+1]

print(r)
Problem p03246 (python)

Fixed submission:
from collections import Counter
n = int(input().strip())
vs = [int(i) for i in input().split(' ')]
vs1 = vs[::2]
vs2 = vs[1::2]
a = Counter(vs1).most_common()
b = Counter(vs2).most_common()

if a[0][0] != b[0][0]:
    print(n - a[0][1] - b[0][1])
else:
    candidates = []
    if len(a) != 1:
        candidates.append(n - a[1][1] - b[0][1])
    if len(b) != 1:
        candidates.append(n - a[0][1] - b[1][1])
    if len(candidates) != 0:
        print(min(candidates))
    else:
        print(n//2)

Buggy submission (verdict: Runtime Error):
from collections import Counter
n = int(input().strip())
vs = [int(i) for i in input().split(' ')]
vs1 = vs[::2]
vs2 = vs[1::2]
a = Counter(vs1).most_common()
b = Counter(vs2).most_common()

if a[0][0] != b[0][0]:
    print(n - a[0][1] - b[0][1])
else:
    candidates = []
    if len(a) != 1:
        candidates.append(n - a[1][1] - b[0][1])
    if len(b) != 1:
        candidates.append(n - a[0][1] - b[1][1])
    if len(candidates) == 0:
        print(min(candidates))
    else:
        print(n//2)

Buggy submission (verdict: Runtime Error):
from collections import Counter
n = int(input().strip())
vs = [int(i) for i in input().split(' ')]
vs1 = vs[::2]
vs2 = vs[1::2]
a = Counter(vs1).most_common()
b = Counter(vs2).most_common()

if a[0][0] != b[0][0]:
    print(n - a[0][1] - b[0][1])
else:
    candidates = []
#    candidates.append(len(vs2))
    if len(a) != 1:
        candidates.append(n - a[1][1] - b[0][1])
    if len(b) != 1:
        candidates.append(n - a[0][1] - b[1][1])
    print(min(candidates))
Problem p03247 (python)

Fixed submission:
import sys


denum = 39

ds = [pow(2, i) for i in range(denum)]
max_ds = sum(ds)

N = input()
rr = []
for line in sys.stdin:
    x, y = line.strip().split(" ")
    rr.append((int(x) + int(y), int(x) - int(y)))

md = rr[0][0] % 2
if md == 0:
    ds = [1] + ds
Rresult = []
for u, v in rr:
    if u % 2 != md:
        result = "-1"
        break
    result = ""
    if md == 0:
        result = result + "R"
        u -= 1
        v -= 1
    u = (u + max_ds) // 2
    v = (v + max_ds) // 2
    binu = bin(u)[2:][::-1]
    binv = bin(v)[2:][::-1]
    for i in range(denum):
        ui = 0
        if i < len(binu):
            ui = int(binu[i])
        vi = 0
        if i < len(binv):
            vi = int(binv[i])
        #print(ui, vi)
        if ui == 1 and vi == 1:
            result = result + "R"
        if ui == 0 and vi == 0:
            result = result + "L"
        if ui == 1 and vi == 0:
            result = result + "U"
        if ui == 0 and vi == 1:
            result = result + "D"
    Rresult.append(result)

ds = [str(x) for x in ds]
if result == "-1":
    print(result)
else:
    print(len(Rresult[0]))
    print(' '.join(ds))
    print("\n".join(Rresult))

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

rr = []
for line in sys.stdin:
    x, y = line.strip().split(" ")
    rr.append((int(x), int(y)))

mm = max([abs(x)+abs(y) for x, y in rr])

M = mm
ds = ['1'] * mm
#ds = ' '.join([1] * mm)
md = mm % 2
Rresult = []
for x, y in rr:
    if (x+y) % 2 != md:
        result = "-1"
        break
    else:
        result = ""
        result += "R" * x if x > 0 else "L" * abs(x)
        result += "U" * y if y > 0 else "D" * abs(y)
        result += "RL" * ((mm - abs(x) - abs(y))//2)
        Rresult.append(result)
if result == "-1":
    print(result)
else:
    print(M)
    print(' '.join(ds))
    print("\n".join(Rresult))
Problem p03262 (python)

Fixed submission:
import math
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a


a,b = [int(i) for i in input().split(' ')]
xs = [int(i) for i in input().split(' ')]
#a = pow(10, 5)

temp = 0
result = 0
for i, x in enumerate(xs):
    if i == 0:
        result = abs(x - b)
    else:
        result = gcd(abs(x - temp), result)
    temp = x
print(result)

Buggy submission (verdict: Runtime Error):
import math
a,b = [int(i) for i in input().split(' ')]
xs = [int(i) for i in input().split(' ')]

temp = 0
result = 0
for i, x in enumerate(xs):
    if i == 0:
        result = abs(x - b)
    else:
        result = math.gcd(abs(x - temp), result)
    temp = x
print(result)
Problem p03270 (python)

Fixed submission:
import math
import sys
K, N = [int(i) for i in input().split(' ')]
mod = 998244353
sys.setrecursionlimit(100000)

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

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

NNN = 10**4
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

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

def EN(i):
    k2 = int((K - abs(K - int((i-1)/2) * 2) )/2)
    k1 = K - k2*2 - (1 if i % 2 == 0 else 0)
    result = 0
    for j in range(k2+1):
        if N < j:
            break
        result = (result + mul(mul(cmb_f(k2, j, mod) , cmb_f(N+k1-1, j+k1-1, mod)) , pow(2, j) % mod))% mod
    if i % 2 == 1:
        return result
    for j in range(k2+1):
        if N-1 < j:
            break
        result = (result + mul(mul(cmb_f(k2, j, mod), cmb_f(N+k1-2, j+k1-1, mod)), pow(2, j) % mod))% mod
    return result

if K == 1:
    print(0)
else:
    result = []
    for i in range(K):
        result.append(str(EN(i+2)))
    print("\n".join(result + result[:-1][::-1]))

Buggy submission (verdict: Runtime Error):
import math
import sys
K, N = [int(i) for i in input().split(' ')]
mod = 998244353
sys.setrecursionlimit(100000)

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

def power(x, y):
    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y//2)**2 % mod
    else            : return power(x, (y-1)//2)**2 * x % mod

def div(a, b):
    return mul(a, power(b, mod-2))

def C(n, r):
    if r < 0:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

olist = [C(N+K-2, N) % mod]

def EE(i):
    k2 = int((K - abs(K - int((i-1)/2) * 2) )/2)
    k1 = K - k2*2 - (1 if i % 2 == 0 else 0)
    result = 0
    for j in range(min(k2, N+1)):
        if i % 2 == 1:
            olist[j] = div(olist[j] * k2, k2-j)
        olist[j] = div(olist[j] * (k1+j) , N+k1)
        result = (result + olist[j]) % mod
        if i % 2 == 0 and j != N:
            result = (result + div(olist[j] * (N-j), N+k1-1)) % mod

    if N >= k2:
        if i % 2 == 1:
            olist.append((C(k2, k2) * C(N+k1-1, N-k2) * pow(2, k2)) % mod)
        else:
            if i != 2:
                olist[k2] = div(olist[k2] * (k1+k2), N+k1)
            result = (result + div(olist[k2] * (N-k2), N+k1-1)) % mod
        result = (result + olist[k2]) % mod
    return result

result = []
for i in range(K):
    alist = []
    result.append(str(EE(i+2)))
print("\n".join(result + result[:-1][::-1]))
Problem p03274 (python)

Fixed submission:
N, K = map(int, input().split())
X = list(map(int, input().split()))
Y = []
Z = []
zc = 0
for i in range(N):
    if X[i] > 0:
        Y.append(X[i])
    if X[i] == 0:
        zc = 1
for i in range(N-1, -1, -1):
    if X[i] < 0:
        Z.append(-X[i])

cs = []
K -= zc
if K == 0:
    print(0)
else:
    if len(Y) >= K:
        cs.append(Y[K-1])
    if len(Z) >= K:
        cs.append(Z[K-1])
    for i in range(min(K-1, len(Y))):
        if K-i-2 < len(Z):
            cs.append(2*Y[i]+Z[K-i-2])
    for i in range(min(K-1, len(Z))):
        if K-i-2 < len(Y):
            cs.append(2*Z[i]+Y[K-i-2])
    print(min(cs))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
X = list(map(int, input().split()))
Y = []
Z = []
zc = 0
for i in range(N):
    if X[i] > 0:
        Y.append(X[i])
    if X[i] == 0:
        zc = 1
for i in range(N-1, -1, -1):
    if X[i] < 0:
        Z.append(-X[i])

cs = []
K -= zc
if K == 0:
    print(0)
else:
    if len(Y) >= K:
        cs.append(Y[K-1])
    if len(Z) >= K:
        cs.append(Z[K-1])
    for i in range(K-1):
        cs.append(2*Y[i]+Z[K-i-2])
    for i in range(K-1):
        cs.append(2*Z[i]+Y[K-i-2])
print(min(cs))
Problem p03282 (python)

Fixed submission:
s =  int(input())
k =  int(input())
#print(pow(10, 12))
def main(string_s):
    for i, j in enumerate(str(string_s)):
#        print(i, j)
#        print(i, k)
        if j != '1':
            break
        if i+1 == k:
            print('1')
            return
    if str(s).lstrip('1') == '':
         print('1')
         return
    if str(s).lstrip('1')[0] == '2':
        print('2')
    elif str(s).lstrip('1')[0] == '3':
        print('3')
    elif str(s).lstrip('1')[0] == '4':
        print('4')
    elif str(s).lstrip('1')[0] == '5':
        print('5')
    elif str(s).lstrip('1')[0] == '6':
        print('6')
    elif str(s).lstrip('1')[0] == '7':
        print('7')
    elif str(s).lstrip('1')[0] == '8':
        print('8')
    elif str(s).lstrip('1')[0] == '9':
        print('9')
    else:
        print('1')

main(s)

Buggy submission (verdict: Runtime Error):
s =  int(input())
k =  int(input())
#print(pow(10, 12))
def main(string_s):
    if str(s).lstrip('1')[0] == '2':
        print('2')
    elif str(s).lstrip('1')[0] == '3':
        print('3')
    elif str(s).lstrip('1')[0] == '4':
        print('4')
    elif str(s).lstrip('1')[0] == '5':
        print('5')
    elif str(s).lstrip('1')[0] == '6':
        print('6')
    elif str(s).lstrip('1')[0] == '7':
        print('7')
    elif str(s).lstrip('1')[0] == '8':
        print('8')
    elif str(s).lstrip('1')[0] == '9':
        print('9')
    else:
        print('1')

main(s)
Problem p03311 (python)

Fixed submission:
N = int(input())
As = sorted([int(a)-i for i, a in enumerate(input().strip().split(' '))])

temp1 = 0
temp2 = sum(As)
for i, a in enumerate(As):
    temp1 += As[i]
    temp2 -= As[i]
    r = As[i] * (i+1) - temp1 +\
    temp2 - As[i] * (N-1-i)
    result = min(result, r) if i != 0 else r
print(result)

Buggy submission (verdict: Runtime Error):
import random
from itertools import accumulate
N = int(input())
As = [int(i) for i in input().split(' ')]

#N = pow(10, 4)
#As = [random.randint(0,1000) for r in range(N)] 
As = list(accumulate(As))


def bsearch(l, ls, rs):
    n = int(len(l)/2)
    if n == 0:
        return [max(l[0]-ls , rs-l[0]), min(l[0]-ls , rs-l[0])]
    lsum = l[n] - ls
    rsum = rs - l[n]
    ll = l[:n]
    rl = l[n:]
    if lsum > rsum:
        return bsearch(ll, ls, rs)
    else:
        return bsearch(rl, ls, rs)

for i in range(N-3):
    cs = bsearch(As[:i+2], 0, As[i+1]) + bsearch(As[i+2:], As[i+1], As[-1])
    if i == 0:
        result = max(cs) - min(cs)
    else:
        if max(cs) - min(cs) < result:
            result = max(cs) - min(cs)
print(result)
Problem p03343 (python)

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

r = 10**18
for y in X:
    tmp = []
    tmp2 = []
    for x in X:
        if x < y:
            tmp.sort()
            tn = len(tmp)
            if len(tmp) > K-1:
                tmp2 += tmp[:tn-K+1]
            tmp = []
            continue
        tmp.append(x)
    tmp.sort()
    tn = len(tmp)
    if tn-K+1 > 0:
        tmp2 += tmp[:tn-K+1]
    tmp2.sort()
    if len(tmp2) >= Q:
        r = min(r, tmp2[Q-1] - y)
print(r)

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

r = Y[Q-1] - Y[0]
for y in X:
    tmp = []
    tmp2 = []
    for x in X:
        if x < y:
            tmp.sort()
            tn = len(tmp)
            if len(tmp) > K-1:
                tmp2 += tmp[:tn-K+1]
                print(tmp, tmp[:tn-K+1])
            tmp = []
            continue
        tmp.append(x)
    tn = len(tmp)
    if tn-K+1 > 0:
        tmp2 += tmp[:tn-K+1]
    tmp2.sort()
    if len(tmp2) >= Q:
        r = min(r, tmp2[Q-1] - y)
print(r)
Problem p03355 (python)

Fixed submission:
def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            break
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    result = []
    for i in range(K):
        result += sub_k(string, i+1, K-i)
    print(sorted(set(result))[K-1])

string = input()
K = int(input())
main(string, K)

Buggy submission (verdict: Runtime Error):
def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            continue
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    result = []
    for i in range(K):
        result += sub_k(string, i+1, K-i)
    print(sorted(set(result))[K-1])

string = input()
K = int(input())
main(string, K)

Buggy submission (verdict: Runtime Error):
def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            continue
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    for i in range(K):
        print(sub_k(string, i+1, K-i))

string = input()
K = input()
main(string, K)
Problem p03356 (python)

Fixed submission:
import sys

def key2rep_iter(key, key2rep, temp_list):
    if key2rep[key] == key:
        for i in temp_list:
            key2rep[i] = key2rep[key]
        return key
    temp_list.append(key)
    return key2rep_iter(key2rep[key], key2rep, temp_list)

def add_x2y(x, y, key2rep):
    rep = key2rep_iter(y, key2rep, [])
    big_rep = key2rep_iter(x, key2rep, [])
    if rep == big_rep:
        return
    key2rep[big_rep] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), key2rep)

print(len([1 for i in range(N) if key2rep_iter(p_list[i], key2rep, []) == key2rep_iter(i+1, key2rep, [])]))

Buggy submission (verdict: Runtime Error):
import sys

def create_dict(N):
    d = {}
    key2rep = {}
    for i in range(N):
        d[i+1] = set([i+1])
        key2rep[i+1] = i+1
    return d, key2rep

def add_x2y(x, y, d, key2rep):
#    print(y)
    rep = key2rep[y]
    big_rep = key2rep[x]
    for old in d[big_rep]:
        d[rep].add(old)
        key2rep[old] = rep
    del d[big_rep]
#    print(d)

N, M = map(int,input().split(' '))
d, key2rep = create_dict(N)
p_list = list(map(int,input().split(' ')))

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), d, key2rep)

score = 0
for i in d:
    for x in d[i]:
        if p_list[x-1] in d[i]:
            score += 1
print(score)

Buggy submission (verdict: Runtime Error):
import sys

def create_dict(N):
    d = {}
    for i in range(N):
        d[i+1] = set([i+1])
    return d

N, M = map(int,input().split(' '))
d = create_dict(N)
p_list = list(map(int,input().split(' ')))

x_list = []
y_list = []
for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    d[min(x, y)].add(max(x,y))
    del d[max(x,y)]

score = 0
for i in d:
    for x in d[i]:
        if p_list[x-1] in d[i]:
            score += 1
print(score)
Problem p03424 (python)

Fixed submission:
N, = map(int, input().split())
X = set(map(str, input().split()))
if len(X) == 3:
    print("Three")
else:
    print("Four")

Buggy submission (verdict: Runtime Error):
N, = map(int, input().split())
X = set(map(int, input().split()))
if len(X) == 3:
    print("Three")
else:
    print("Four")