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

Fixed submission:
#!/usr/bin/env python3
n = int(input())
p = sorted(list(map(int, input().split())) for _ in range(n))

lmax = n - 1
rmin = 0
for i in range(n):
    if p[i][1] <= p[rmin][1]:
        rmin = i

ans = 0

# same(lmax, rmin)
dmax = 0
for i in range(n):
    if i == lmax or i == rmin:
        continue
    if p[i][1] - p[i][0] > p[dmax][1] - p[dmax][0]:
        dmax = i
d1 = max(0, p[rmin][1] - p[lmax][0] + 1)
d2 = max(0, p[dmax][1] - p[dmax][0] + 1)
ans = max(ans, d1 + d2)

# !same(lmax, rmin)
m = lmax
for i in range(rmin, lmax)[::-1]:
    if p[i + 1][1] < p[m][1]:
        m = i + 1
    d1 = max(0, p[rmin][1] - p[i][0] + 1)
    d2 = max(0, p[m][1] - p[lmax][0] + 1)
    ans = max(ans, d1 + d2)

print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
n = int(input())
p = sorted(list(map(int, input().split())) for _ in range(n))

lmax = n - 1
rmin = 0
for i in range(n):
    if p[i][1] <= p[rmin][1]:
        rmin = i

ans = 0

# same(lmax, rmin)
dmax = 0
for i in range(n):
    if i == lmax or i == rmin:
        continue
    if p[i][1] - p[i][0] > p[dmax][1] - p[dmax][0]:
        dmax = i
    d1 = max(0, p[rmin][1] - p[lmax][0] + 1)
    d2 = max(0, p[dmax][1] - p[dmax][0] + 1)
ans = max(ans, d1 + d2)

# !same(lmax, rmin)
m = lmax
for i in range(rmin, lmax)[::-1]:
    if p[i + 1][1] < p[m][1]:
        m = i + 1
    d1 = max(0, p[rmin][1] - p[i][0] + 1)
    d2 = max(0, p[m][1] - p[lmax][0] + 1)
    ans = max(ans, d1 + d2)

print(ans)
Problem p02900 (python)

Fixed submission:
#!/usr/bin/env python3
from fractions import gcd
a, b = map(int, input().split())
g = gcd(a, b)
ans = {}
for i in range(2, int(g**0.5 + 1)):
    while g % i == 0:
        ans[i] = ans[i] + 1 if i in ans else 1
        g //= i
if g != 1:
    ans[g] = 1
print(len(ans) + 1)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from math import gcd
a, b = map(int, input().split())
g = gcd(a, b)
ans = {}
for i in range(2, int(g**0.5 + 1)):
    while g % i == 0:
        ans[i] = ans[i] + 1 if i in ans else 1
        g //= i
if g != 1:
    ans[g] = 1
print(len(ans) + 1)
Problem p02947 (python)

Fixed submission:
#!/usr/bin/env python3
acc = [0] * (10**5 + 1)
for i in range(10**5):
    acc[i + 1] = acc[i] + (i + 1)
n = int(input())
s = [input() for _ in range(n)]
dic = {}
for i in range(n):
    srt = ''.join(sorted(s[i]))
    dic[srt] = dic[srt] + 1 if srt in dic else 1
ans = 0
for v in dic.values():
    ans += acc[v - 1]
print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
acc = [0] * 11
for i in range(10):
    acc[i + 1] = acc[i] + (i + 1)
n = int(input())
s = [input() for _ in range(n)]
dic = {}
for i in range(n):
    srt = ''.join(sorted(s[i]))
    dic[srt] = dic[srt] + 1 if srt in dic else 1
ans = 0
for v in dic.values():
    ans += acc[v - 1]
print(ans)
Problem p03007 (python)

Fixed submission:
#!/usr/bin/env python3
import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
p = min(max(1, p), n - 1)
print(sum(a[p:]) - sum(a[:p]))
b = a[p - 1]
for i in range(p, n - 1):
    print(b, a[i])
    b -= a[i]
c = a[n - 1]
for i in range(p - 1):
    print(c, a[i])
    c -= a[i]
print(c, b)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())

print(a[-1] - sum(map(abs, A[1:-1])) - A[0])
for i in range(N-2, 0, -1):
	a = A[i]
	if a > 0:
    	print(A[0], a)
    	A[0] -= a
    else:
    	print(A[-1], a)
    	A[-1] -= a
    	print(A[-1], A[0])

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
N = int(input())
A = sorted(inpl())

print(a[-1] - sum(map(abs, A[1:-1])) - A[0])
for i in range(N-2, 0, -1):
  a = A[i]
  if a > 0:
    print(A[0], a)
    A[0] -= a
    else:
      print(A[-1], a)
      A[-1] -= a
      print(A[-1], A[0])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import bisect
n = int(input())
a = sorted(map(int, input().split()))
if n == 2:
    print(a[1] - a[0])
    print(a[1], a[0])
    exit()

p = bisect.bisect_left(a, 0)
if p < 2 and n == 3:
    print(sum(a) - 2 * a[0])
    print(a[0], a[1])
    b = a[0] - a[1]
    print(a[2], b)
    exit()

p = max(2, p)
print(sum(a[p:]) - sum(a[:p]))
b = a[0]
for i in range(p + 1, n):
    print(b, a[i])
    b -= a[i]
c = a[p]
for i in range(1, p):
    print(c, a[i])
    c -= a[i]
print(c, b)
Problem p03067 (python)

Fixed submission:
a,b,c=map(int,input().split())
print(['No','Yes'][a<=c<=b or b<=c<=a])

Buggy submission (verdict: Runtime Error):
a,b,c=map(int,input().split())
print(['No','Yes'][b<=min(a,c) or b=>max(a,c)])
Problem p03128 (python)

Fixed submission:
#!/usr/bin/env python3
N, M = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)

inf = float('inf')
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-inf] * N
for i in range(N + 1)[1::]:
    for x in A:
        if i >= num[x]:
            dp[i] = max(dp[i], dp[i - num[x]] + 1)

ans = ''
while N > 0:
    for x in A:
        if N >= num[x] and dp[N - num[x]] == dp[N] - 1:
            ans += str(x)
            N -= num[x]
            break

print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
A = sorted(map(int, input().split()), reverse=True)

inf = float('inf')
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-inf] * N
for i in range(N + 1)[1::]:
    for x in A:
        if i >= num[x]:
            dp[i] = max(dp[i], dp[i - num[x]] + 1)

ans = ''
while N > 0:
    for x in A:
        if dp[N - num[x]] == dp[N] - 1:
            ans += str(x)
            N -= num[x]
            break

print(ans)
Problem p03142 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(100000)
N, M = map(int, input().split())
g = [[] for _ in range(N)]
rg = [[] for _ in range(N)]
for _ in range(N - 1 + M):
    A, B = (int(x) - 1 for x in input().split())
    g[A].append(B)
    rg[B].append(A)


def dfs(s):
    global ts
    global used
    used[s] = True
    for t in g[s]:
        if not used[t]:
            dfs(t)
    ts.append(s)


def tsort():
    global ts
    for i in range(N):
        dfs(i)
    ts = ts[::-1]


used = [False] * N
ts = []
tsort()

mp = [None] * N
for i, x in enumerate(ts):
    mp[x] = i

ans = [0] * N
for t in ts[1:]:
    if rg[t]:
        ans[t] = ts[max(mp[s] for s in rg[t])] + 1

for x in ans:
    print(x)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
g = [[] for _ in range(N)]
rg = [[] for _ in range(N)]
for _ in range(N - 1 + M):
    A, B = (int(x) - 1 for x in input().split())
    g[A].append(B)
    rg[B].append(A)


def dfs(s):
    global ts
    global used
    used[s] = True
    for t in g[s]:
        if not used[t]:
            dfs(t)
    ts.append(s)


def tsort():
    global ts
    for i in range(N):
        dfs(i)
    ts = ts[::-1]


used = [False] * N
ts = []
tsort()

mp = [None] * N
for i, x in enumerate(ts):
    mp[x] = i

ans = [0] * N
for t in ts[1:]:
    if rg[t]:
        ans[t] = ts[max(mp[s] for s in rg[t])] + 1

for x in ans:
    print(x)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(100000)
N, M = map(int, input().split())
g = [[] for _ in range(N)]
rg = [[] for _ in range(N)]
for _ in range(N - 1 + M):
    A, B = map(lambda x: int(x) - 1, input().split())
    g[A].append(B)
    rg[B].append(A)


def dfs1(s):
    global ans
    for t in rg[s]:
        return dfs1(t)
    else:
        ans[s] = 0
        return s


def dfs2(s, dep):
    global ans
    global dist
    if dist[s] < dep:
        return
    for t in g[s]:
        if dist[t] <= dist[s] + 1:
            ans[t] = s + 1
            dist[t] = dist[s] + 1
            dfs2(t, dep + 1)


ans = [None] * N
dist = [0] * N
s = dfs1(0)
dfs2(s, 0)

for x in ans:
    print(x)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, M = map(int, input().split())
g = [[] for _ in range(N)]
rg = [[] for _ in range(N)]
for _ in range(N - 1 + M):
    A, B = map(lambda x: int(x) - 1, input().split())
    g[A].append(B)
    rg[B].append(A)


def dfs1(s):
    global ans
    for t in rg[s]:
        return dfs1(t)
    else:
        ans[s] = 0
        return s


def dfs2(s):
    global ans
    for t in g[s]:
        if ans[t] is None:
            ans[t] = s + 1
            dfs2(t)


ans = [None] * N
s = dfs1(0)
dfs2(s)

for x in ans:
    print(x)
Problem p03157 (python)

Fixed submission:
#!/usr/bin/env python3
from itertools import product
import sys

sys.setrecursionlimit(200000)

H, W = map(int, input().split())
S = [input() for _ in range(H)]
color = [[None] * W for _ in range(H)]
memo = [[0, 0] for _ in range(H * W + 1)]
cnt = 1


def dfs(p, c):
    x, y = p
    color[x][y] = c
    memo[cnt][c] += 1
    for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and color[nx][ny] is None and c != (S[nx][ny] == '#'):
            dfs([nx, ny], not c)


for i, j in product(range(H), range(W)):
    if color[i][j] is None:
        dfs([i, j], S[i][j] == '#')
        cnt += 1

ans = 0
for i in range(H * W):
    ans += memo[i][0] * memo[i][1]
print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from itertools import product

H, W = map(int, input().split())
S = [input() for _ in range(H)]
color = [[None] * W for _ in range(H)]
memo = [[0, 0] for _ in range(H * W + 1)]
cnt = 1


def dfs(p, c):
    x, y = p
    color[x][y] = c
    memo[cnt][c] += 1
    for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and color[nx][ny] is None and c != (S[nx][ny] == '#'):
            dfs([nx, ny], not c)


for i, j in product(range(H), range(W)):
    if color[i][j] is None:
        dfs([i, j], S[i][j] == '#')
        cnt += 1

ans = 0
for i in range(H * W):
    ans += memo[i][0] * memo[i][1]
print(ans)
Problem p03187 (python)

Fixed submission:
#!/usr/bin/env python3
from itertools import accumulate
L, N = map(int, input().split())
X = [int(input()) for _ in range(N)]
Y = [L - x for x in X][::-1]

sx = [0] + list(accumulate(X))
sy = [0] + list(accumulate(Y))

ans = 0
for i in range(N):
    j = N - i - 1
    mn = min(i, j)

    now = X[i]
    now += 2 * (sx[i] - sx[i - mn]) + 2 * (sy[j] - sy[j - mn - (i < j)])
    ans = max(ans, now)

    now = Y[j]
    now += 2 * (sx[i] - sx[i - mn - (i > j)]) + 2 * (sy[j] - sy[j - mn])
    ans = max(ans, now)

print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import numpy as np
L, N = map(int, input().split())
X = np.array([0] + [int(input()) for _ in range(N)])
Y = np.array([0] + [L - x for x in X[1:]][::-1])

if N > 2000:
    print(0)
dp = np.zeros((N + 1, N + 1, 2), dtype=np.int32)
for i in range(N):
    for j in range(N):
        dp[i + 1][j][0] = np.amax(np.array([
                dp[i][j][0] + Y[i + 1] - Y[i],
                dp[i][j][1] + X[j] + Y[i + 1]]))
        dp[i][j + 1][1] = np.amax(np.array([
                dp[i][j][0] + X[j + 1] + Y[i],
                dp[i][j][1] + X[j + 1] - X[j]]))

ans = 0
for i in range(N + 1):
    ans = max(ans, dp[i][N - i][0], dp[i][N - i][1])

print(ans)
Problem p03433 (python)

Fixed submission:
N=int(input())
A=int(input())
print(['No','Yes'][N%500<=A])

Buggy submission (verdict: Runtime Error):
N=input()
A=input()
print(['No','Yes'][N%500<=A])
Problem p03434 (python)

Fixed submission:
N=int(input())
a=sorted(map(int,input().split()),reverse=True)
ans=sum(((-1)**i)*x for i,x in enumerate(a))
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
a=sorted(map(int,input().split()),reversed=True)
ans=sum(((-1)**i)*x for i,x in enumerate(a))
print(ans)
Problem p03439 (python)

Fixed submission:
#!/usr/bin/env python3
N = int(input())

print(0, flush=True)
w0 = input()
if w0 == 'Vacant':
    exit()

l, r = 0, N
while True:
    m = (l + r) // 2
    print(m, flush=True)
    w1 = input()
    if w1 == 'Vacant':
        break
    if m % 2 == (w0 != w1):
        l = m
    else:
        r = m

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N = int(input())

def solve():
    s = []
    print(0)
    s.append(input())
    if s[-1] == 'Vacant':
        return

    l, r = 0, N
    while r - l > 1:
        m = (l + r) // 2
        print(m)
        s.append(input())
        if s[-1] != 'Vacant':
            return

        if s[i - 1] == s[i]:
            l, r = (l, m) if (m - l) % 2 == 1 else (m, r)
        else:
            l, r = (m, r) if (m - l) % 2 == 1 else (l, m)
    print(m + 1)

print(solve())
Problem p03488 (python)

Fixed submission:
#!/usr/bin/env python3
s = input()
x, y = map(int, input().split())

d = [len(s) for s in s.split('T')]
nx = [set([d[0]])]
ny = [set([0])]

for i, l in enumerate(d[2::2]):
    new_n = set()
    for n in nx[i]:
        new_n.add(n + l)
        new_n.add(n - l)
    nx.append(new_n)

for i, l in enumerate(d[1::2]):
    new_n = set()
    for n in ny[i]:
        new_n.add(n + l)
        new_n.add(n - l)
    ny.append(new_n)

print('Yes' if (x in nx[-1] and y in ny[-1]) else 'No')

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
s = list(input())
x, y = map(int, input().split())

X = [0] * 16001
Y = [0] * 16001

X[8000] = Y[8000] = 0
isx = 1
tcnt = 0
fcnt = 0
for i in range(len(s)):
    if s[i] == 'T':
        for j in range(len(X)):
            if isx == 1:
                if tcnt == 0:
                    X[8000 + fcnt] = tcnt + 1
                    break
                if X[j] == tcnt:
                    X[j + fcnt] = X[j - fcnt] = tcnt + 1
            else:
                if X[j] == tcnt:
                    X[j] = tcnt + 1
        for j in range(len(Y)):
            if isx == 0:
                if Y[j] == tcnt:
                    Y[j + fcnt] = Y[j - fcnt] = tcnt + 1
            else:
                if Y[j] == tcnt:
                    Y[j] = tcnt + 1
        isx ^= 1
        tcnt += 1
        fcnt = 0
    else:
        fcnt += 1

if s[-1] == 'F':
    for j in range(len(X)):
        if isx == 1:
            if tcnt == 0:
                X[8000 + fcnt] = tcnt + 1
                break
            if X[j] == tcnt:
                X[j + fcnt] = X[j - fcnt] = tcnt + 1
        else:
            if X[j] == tcnt:
                X[j] = tcnt + 1
    for j in range(len(Y)):
        if isx == 0:
            if Y[j] == tcnt:
                Y[j + fcnt] = Y[j - fcnt] = tcnt + 1
        else:
            if Y[j] == tcnt:
                Y[j] = tcnt + 1
    isx ^= 1
    tcnt += 1
    fcnt = 0

print('Yes' if (X[8000 + x] == tcnt and Y[8000 + y] == tcnt) else 'No')
Problem p03504 (python)

Fixed submission:
#!/usr/bin/env python3
N, C = map(int, input().split())
table = [[0] * C for i in range(10 ** 5 + 1)]
for i in range(N):
    s, t, c = map(int, input().split())
    table[s - 1][c - 1] += 1
    table[t - 1][c - 1] -= 1

imos = [0] * (10 ** 5 + 1)
for i in range(10 ** 5 + 1):
    for j in range(C):
        if table[i][j] == 1:
            imos[i] += 1
        if table[i][j] == -1:
            imos[i + 1] -= 1

ans = 0
for i in range(1, 10 ** 5 + 1):
    imos[i] += imos[i - 1]
    ans = max(ans, imos[i])

print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, C = map(int, input().split())
table = [0] * (10 ** 5 + 1)
for i in range(N):
    s, t, c = map(int, input().split())
    table[s] += 1
    table[t + 1] -= 1

ans = 0
for i in range(1, 10 ** 5 + 1):
    table[i] += table[i - 1]
    ans = max(ans, table[i])

print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N, C = map(int, input().split())
table = [[0] * C for i in range(10 ** 5)]
for i in range(N):
    s, t, c = map(lambda x: int(x) - 1, input().split())
    table[s][c] += 1
    table[t + 1][c] -= 1


ans = 0
for i in range(1, 10 ** 5):
    for j in range(C):
        table[i][j] += table[i - 1][j]
    ans = max(ans, sum(table[i]))

print(ans)
Problem p03534 (python)

Fixed submission:
#!/usr/bin/env python3
S = input()
n = [S.count('a'), S.count('b'), S.count('c')]

ans = False
if len(S) == 1:
    ans = True
elif len(S) == 2 and len([x for x in n if x != 0]) == 2:
    ans = True
else:
    n.sort()
    if n[0] != 0 and n[2] - n[0] <= 1:
        ans = True
print(('NO', 'YES')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
A, B = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [[0] * (B + 1) for _ in range(A + 1)]

for i in range(A)[::-1]:
    dp[i][B] = dp[i + 1][B] + a[i] if (i + B) % 2 == 0 else dp[i + 1][B]
for j in range(B)[::-1]:
    dp[A][j] = dp[A][j + 1] + b[j] if (A + j) % 2 == 0 else dp[A][j + 1]

for i in range(A)[::-1]:
    for j in range(B)[::-1]:
        dp[i][j] = \
        max(dp[i + 1][j] + a[i], dp[i][j + 1] + b[j]) if (i + j) % 2 == 0 else \
        min(dp[i + 1][j], dp[i][j + 1])

print(dp[0][0])
Problem p03734 (python)

Fixed submission:
#!/usr/bin/env python3

def rec(i, j):
    if (i, j) in dp:
        return dp[(i, j)]

    if i == N:
        res = 0
    elif j < w[i]:
        res = rec(i + 1, j)
    else:
        res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i])

    dp[(i, j)] = res
    return res


N, W = map(int, input().split())
w = [0] * N
v = [0] * N
for i in range(N):
    w[i], v[i] = map(int, input().split())

dp = {}
print(rec(0, W))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

N, W = map(int, input().split())
w = [0] * N
v = [0] * N
for i in range(N):
    w[i], v[i] = map(int, input().split())

dp = [[0] * (W + 1) for i in range(N + 1)]
for i in range(N):
    for j in range(W + 1):
        if j < w[i]:
            dp[i + 1][j] = dp[i][j]
        else:
            dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i])

print(dp[N][W])
Problem p03921 (python)

Fixed submission:
#!/usr/bin/env python3
class UnionFind():
    def __init__(self, size):
        self.table = [-1] * size

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s2] < self.table[s1]: s1, s2 = s2, s1
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        return s1 != s2


N, M = map(int, input().split())
uf = UnionFind(N + M)
for i in range(N):
    L = [int(x) - 1 for x in input().split()]
    L[0] += 1
    for j in range(L[0]):
        uf.union(L[j + 1], M + i)

ans = True
for i in range(N):
    if uf.find(M + i) != uf.find(M):
        ans = False
print(('NO', 'YES')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from unionfind import unionfind
N, M = map(int, input().split())
u = unionfind(N + M)
for i in range(N):
    L = [int(x) - 1 for x in input().split()]
    L[0] += 1
    for j in range(L[0]):
        u.unite(L[j + 1], M + i)

ans = True
for i in range(N):
    if not u.issame(M, M + i):
        ans = False
print(('NO', 'YES')[ans])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N = int(input())

sum = 0
m = 0
for i in range(1, 10**3 + 1):
    sum += i
    if sum >= N:
        m = i
        break

sum = N
for i in range(1, m + 1)[::-1]:
    if sum - i >= 0:
        sum -= i
        print(i)
Problem p03992 (python)

Fixed submission:
#!/usr/bin/env python3
s = input()
print(s[:4], s[4:])

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
N = int(input())
a = list(map(int, input().split()))

print(sum([a[a[i] - 1] - 1 == i for i in range(N)]) // 2)
Problem p04016 (python)

Fixed submission:
#!/usr/bin/env python3
import math

def f(b, n):
    return n if (n < b) else f(b, n // b) + n % b

n = int(input())
s = int(input())

ans = -1
if n == s:
    ans = n + 1
else:
    for b in range(2, math.floor(n ** 0.5) + 1):
        if f(b, n) == s:
            ans = b
            break
    else:
        for p in range(1, math.ceil(n ** 0.5)):
            if (n - s) % p != 0:
                continue
            b = (n - s) // p + 1
            if math.floor(n ** 0.5) < b <= n and f(b, n) == s:
                ans = b
print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math

def f(b, n):
    return n if (n < b) else f(b, n // b) + n % b

n = int(input())
s = int(input())

ans = -1
if n == s:
    ans = n + 1
else:
    for b in range(2, math.floor(n ** 0.5) + 1):
        if f(b, n) == s:
            ans = b
            break
    else:
        for p in range(1, math.ceil(n ** 0.5))[::-1]:
            b = (n - s) // p + 1
            if b != int(b):
                continue
            if f(b, n) == s:
                ans = b
                break
print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

def f(b, n):
    return n if (n < b) else f(b, n // b) + n % b

n = int(input())
s = int(input())

sqrt_n = int(n ** 0.5)
ans = -1
if n == s:
    ans = n + 1
else:
    for b in range(2, sqrt_n + 1):
        if f(b, n) == s:
            ans = b
            break
    else:
        for p in range(1, sqrt_n):
            if (n - s) % p != 0:
                continue
            b = (n - s) // p + 1
            if f(b, n) == s:
                ans = b
                break
print(ans)
Problem p04044 (python)

Fixed submission:
N = input().split()
list = []

N = int(N[0])
for i in range(1,N+1,1):
    list.append(input())

list.sort(reverse = False)

ans = ""
for i in range(1,N+1,1):
    ans = ans + list[0]
    list.pop(0)

print(ans)

Buggy submission (verdict: Runtime Error):
N = input().split()
list = []

for i in range(1,int(N[0]),1):
    list.append(input())

list.sort(reverse = False)

ans = ""
for i in range(1,N+1,1):
    ans = ans + list[0]
    list.pop(0)

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
L_num = int(input())
list = []

for i in range(1,N+1,1):
    list.append(input())

list.sort(reverse = False)

ans = ""
for i in range(1,N+1,1):
    ans = ans + list[0]
    list.pop(0)

print(ans)
Problem p03999 (python)

Fixed submission:
import ast

def pattern(li, el, space, acc, xs):
  if len(li) != 1:
    acc.append(el)
    acc.append(space)
    al = li.pop(0)
    pattern(list(li), al, '+', list(acc), xs)
    pattern(list(li), al, '', list(acc), xs)
  else:
    acc.append(el)
    acc.append(space)
    al = li.pop(0)
    acc.append(al)
    xs.append(ast.literal_eval(''.join(acc)))

li = list(input())
el = li.pop(0)
xs = []

if len(li) != 0:
  pattern(list(li), el, '+', [], xs)
  pattern(list(li), el, '', [], xs)

if len(li) != 0:
  print(sum(xs))
else:
  print(int(el))

Buggy submission (verdict: Runtime Error):
import ast

def pattern(li, el, space, acc, xs):
  if len(li) != 1:
    acc.append(el)
    acc.append(space)
    al = li.pop(0)
    pattern(list(li), al, '+', list(acc), xs)
    pattern(list(li), al, '', list(acc), xs)
  else:
    acc.append(el)
    acc.append(space)
    al = li.pop(0)
    acc.append(al)
    xs.append(ast.literal_eval(''.join(acc)))

li = list(input())
el = li.pop(0)
xs = []

pattern(list(li), el, '+', [], xs)
pattern(list(li), el, '', [], xs)

print(sum(xs))