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

Fixed submission:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
count = [0] * (N + 1)
ans = [0] * (N + 1)
for a, b in AB:
    G[a].add(b)
    count[b] += 1
for i in range(1, N):
    if count[i] == 0:
        root = collections.deque([i])
        break
while root:
    p = root.popleft()
    for c in G[p]:
        count[c] -= 1
        if count[c] == 0:
            ans[c] = p
            root.append(c)
print(*ans[1:], sep='\n')

Buggy submission (verdict: Runtime Error):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P_cand = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P_cand[b].add(a)
for p in range(1, N):
    if len(P_cand[p]) == 0:
        break
D = [-1] * (N + 1)
P = [0] * (N + 1)


def dfs(p, d):
    for c in G[p]:
        if D[c] < d + 1:
            D[c] = d + 1
            P[c] = p
            dfs(c, d + 1)


dfs(p, 0)
[print(_) for _ in P[1:]]

Buggy submission (verdict: Runtime Error):
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for _ in range(N - 1 + M)]

G = [set() for _ in range(N + 1)]
P_cand = [set() for _ in range(N + 1)]
for a, b in AB:
    G[a].add(b)
    P_cand[b].add(a)
for p in range(1, N):
    if len(P_cand[p]) == 0:
        break
D = [[0, -1] for _ in range(N + 1)]
D[p] = [0, 0]


def dfs(p):
    for _ in range(len(G[p])):
        for c in G[p]:
            if D[c][1] < D[p][1] + 1:
                D[c][1] = D[p][1] + 1
                D[c][0] = p
                dfs(c)


dfs(p)
[print(_[0]) for _ in D[1:]]
Problem p03230 (python)

Fixed submission:
N = int(input())
K = (((8 * N + 1) ** (1/2) + 1) / 2)

def recursive(k):
    if k == 2:
        return [[1], [1]]
    else:
        before = recursive(k - 1)
        r = [_ for _ in range(1 + (k - 2) * (k - 1) // 2, 1 + k * (k - 1) // 2)]
        return ([before[_] + [r[_]] for _  in range(len(r))] + [r])

if K.is_integer():
    K = int(K)
    print('Yes')
    print(K)
    [print(K - 1, ' '.join(map(str, ary))) for ary in recursive(K)]
else:
    print('No')

Buggy submission (verdict: Runtime Error):
N = int(input())
K = (((8 * N + 1) ** (1/2) + 1) / 2)

def recursive(k):
    if k == 3:
        return [[1, 2], [1, 3], [2, 3]]
    else:
        before = recursive(k - 1)
        r = [_ for _ in range(1 + (k - 2) * (k - 1) // 2, 1 + k * (k - 1) // 2)]
        return ([before[_] + [r[_]] for _  in range(len(r))] + [r])

if K.is_integer() and K > 1:
    K = int(K)
    print('Yes')
    print(K)
    [print(K - 1, ' '.join(map(str, ary))) for ary in recursive(K)]
else:
    print('No')

Buggy submission (verdict: Runtime Error):
N = int(input())
K = (((8 * N + 1) ** (1/2) - 1) / 2)

def recursive(k):
    if k == 2:
        return [[1, 2], [1, 3], [2, 3]]
    else:
        before = recursive(k - 1)
        r = [_ for _ in range(1 + k * (k - 1) // 2, 1 + k * (k + 1) // 2)]
        return ([before[_] + [r[_]] for _  in range(len(r))] + [r])
if K.is_integer():
    print('Yes')
    K = int(K)
    [print(K, ' '.join(map(str, ary))) for ary in recursive(K)]
else:
    print('No')
Problem p03231 (python)

Fixed submission:
N, M = [int(_) for _ in input().split()]
S = input()
T = input()


def gcd(x, y):
    if x < y:
        return gcd(y, x)
    elif y == 0:
        return x
    else:
        return gcd(y, x % y)


g = gcd(N, M)
if (g == 1 and S[0] == T[0]) or S[::N // g] == T[::M // g]:
    print(N * M // g)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N, M = [int(_) for _ in input().split()]
S = input()
T = input()


def gcd(x, y):
    if x % y:
        return gcd(y, x % y)
    else:
        return y


if N > M:
    L = N * M // gcd(N, M)
else:
    L = N * M // gcd(M, N)

p = L // N
q = L // M

U = ['a'] * L
for i in range(N):
    U[i * p] = S[i]
for i in range(M):
    U[i * q] = T[i]

if all([U[i * p] == S[i] for i in range(N)]):
    print(L)
else:
    print(-1)
Problem p03241 (python)

Fixed submission:
n,m=(int(i) for i in input().split())
p = int(m**(1/2))
divisors = []
for i in range(1,p+1):
    if m%i == 0:
        if n*i<=m:
            divisors.append(i)
        if n<=i:
            divisors.append(int(m/i))
print(max(divisors))

Buggy submission (verdict: Runtime Error):
n,m=(int(i) for i in input().split())
p = int(m**(1/2))
divisors = []
for i in range(1,p+1):
    if m%i == 0:
        if n*i<m:
            divisors.append(i)
        if n*(m/i)<m:
            divisors.append(m/i)
print(max(divisors))
Problem p03244 (python)

Fixed submission:
import numpy as np

n = int(input())
v = [int(_) for _ in input().split()]

m = int(n / 2)
p = max(v)

evens = np.array([0] * p)
odds = np.array([0] * p)

for i in range(0, m):
    evens[v[2 * i] - 1] += 1
    odds[v[2 * i + 1] - 1] += 1

if len(evens) == 1 and len(odds) == 1:
    if evens.argsort()[-1] == odds.argsort()[-1]:
        print(m)
    else:
        print(0)
elif len(evens) ==1:
    mode_e1 = evens.argsort()[-1]
    mode_o2, mode_o1 = odds.argsort()[-2:]
    if mode_e1 == mode_o1:
        print(m - odds[mode_o2])
    else:
        print(m - odds[mode_o1])
elif len(odds) == 1:
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o1 = odds.argsort()[-1]
    if mode_e1 == mode_o1:
        print(m - evens[mode_e2])
    else:
        print(m - evens[mode_e1])
else:
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o2, mode_o1 = odds.argsort()[-2:]

    if mode_e1 == mode_o1:
        print(min(n - evens[mode_e1] - odds[mode_o2], n - evens[mode_e2] - odds[mode_o1]))
    else:
        print(n - evens[mode_e1] - odds[mode_o1])

Buggy submission (verdict: Runtime Error):
import numpy as np

n = int(input())
v = [int(_) for _ in input().split()]

if n == 2:
    if v[0] == v[1]:
        print(1)
    else:
        print(0)
else:
    m = int(n / 2)
    p = max(v)

    evens = np.array([0] * p)
    odds = np.array([0] * p)

    for i in range(0, m):
        evens[v[2 * i] - 1] += 1
        odds[v[2 * i + 1] - 1] += 1
    
    mode_e2, mode_e1 = evens.argsort()[-2:]
    mode_o2, mode_o1 = odds.argsort()[-2:]

    if mode_e1 == mode_o1:
        if evens[mode_e1] > odds[mode_o1]:
            print(n - evens[mode_e1] - odds[mode_o2])
        else:
            print(n - evens[mode_e2] - odds[mode_o1])
    else:
        print(n - evens[mode_e1] - odds[mode_o1])
Problem p03248 (python)

Fixed submission:
S = input()
N = len(S)
S = '0' + S
if S[:2] == '01' and S == S[::-1]:
    before = 1
    for i in range(2, N + 1):
        print(before, i)
        if S[i - 1] == '1' and i < (N + 1) // 2 + 2:
            before = i
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
import itertools
S = input()
N = len(S)
S = '0' + S
A = [[int(i), len(list(v))] for i, v in itertools.groupby(S)]
if A[0][0] == A[-1][0] == 0 and A[1][0] == A[-2][0] == A[0][1] == A[-1][1] == 1:
    if len(A) == 3:
        #一直線
        for i in range(1, N):
            print(i, i + 1)
        exit()
    elif len(A) == 5 and A[1][1] == A[-2][1]:
        #脚の長さの最大値がA[1][1]のstar graph
        M = A[1][1]
        Q, R = N // M, N % M
        #Q本の長さMの脚、1本の長さRの脚
        a = [[N] for _ in range(Q + 1)]
        for i in range(1, N):
            a[i % (Q + 1)] += [i]
        for b in a:
            for c, d in zip(b, b[1:]):
                print(c, d)
        exit()
print(-1)

Buggy submission (verdict: Runtime Error):
import itertools
S = input()
N = len(S)
S = '0' + S
A = [[int(i), len(list(v))] for i, v in itertools.groupby(S)]
if A[0][0] == A[-1][0] == 0 and A[1][0] == A[-2][0] == A[0][1] == A[-1][1] == 1:
    if len(A) == 3:
        #一直線
        for i in range(1, N):
            print(i, i + 1)
        exit()
    elif len(A) == 5 and A[1][1] == A[-2][1]:
        #脚の長さの最大値がA[1][1]+1のstar graph
        M = A[1][1] + 1
        Q, R = N // M, N % M
        #Q本の長さMの脚、1本の長さRの脚
        a = [[N] for _ in range(Q + 1)]
        for i in range(1, N):
            a[i % (Q + 1)] += [i]
        for b in a:
            for c, d in zip(b, b[1:]):
                print(c, d)
        exit()
print(-1)
Problem p03282 (python)

Fixed submission:
S = input()
K = int(input())
for i in range(min(len(S), K)):
    if int(S[i]) > 1:
        print(S[i])
        exit()
print(1)

Buggy submission (verdict: Runtime Error):
S = input()
K = int(input())
for i in range(min(len(S), K)):
    if S[i] > 1:
        print(S[i])
        exit()
print(1)
Problem p03353 (python)

Fixed submission:
import itertools
s = input()
K = int(input())
dp = []

def substr(x):
    table = {}
    for i, j in itertools.combinations(range(len(x) + 1), 2):
        table[x[i:j]] = 1
    return sorted(table.keys())[:5]

dp = []

if len(s) < 6:
    dp = substr(s)
else:
    dp = substr(s[-5:])
    for i in range(-6, -len(s) -1, -1):
        for j in range(1, 6):
            if not s[i : i + j] in dp:
                dp = sorted(dp + [s[i : i + j]])[:5]
print(dp[K - 1])

Buggy submission (verdict: Runtime Error):
import itertools
import bisect
s = input()
K = int(input())
array = []
for i, j in itertools.combinations(range(len(s) + 1), 2):
    index = bisect.bisect_left(array, s[i:j])
    if len(array) <= index:
        array += [s[i:j]]
    elif array[index] != s[i:j]:
        array = array[0 : index] + [s[i:j]] + array[index + 1:len(s)]
print(array[K - 1])
Problem p03356 (python)

Fixed submission:
N, M = [int(_) for _ in input().split()]
P = [0] + [int(_) for _ in input().split()]
XY = [[int(_) for _ in input().split()] for _ in range(M)]

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = find(y)


def same(x, y):
    return find(x) == find(y)


for x, y in XY:
    unite(x, y)

ans = 0
for i in range(1, N + 1):
    if same(i, P[i]):
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(_) for _ in input().split()]
P = [0] + [int(_) for _ in input().split()]
XY = sorted([sorted([int(_) for _ in input().split()]) for _ in range(M)],
            key=lambda xy: -xy[0] - N * xy[1])

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = min(UF[x], find(y))
    UF[UF[y]] = UF[x]


def same(x, y):
    return find(x) == find(y)


for x, y in XY:
    unite(x, y)

ans = 0
for i in range(1, N + 1):
    if same(i, P[i]):
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = [int(_) for _ in input().split()]
P = [0] + [int(_) for _ in input().split()]
XY = [[int(_) for _ in input().split()] for _ in range(M)]

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = min(find(x), find(y))
    UF[find(y)] = UF[x]


def same(x, y):
    return find(x) == find(y)


for x, y in XY:
    unite(x, y)

ans = 0
for i in range(1, N + 1):
    if same(i, P[i]):
        ans += 1
print(ans)
Problem p03386 (python)

Fixed submission:
A, B, K = [int(_) for _ in input().split()]
if 2*K <= B-A:
    print("\n".join([str(_) for _ in list(range(A,A+K)) + list(range(B-K+1,B+1))]))
else:
    print("\n".join([str(_) for _ in list(range(A,B+1))]))

Buggy submission (verdict: Runtime Error):
A, B, K = [int(_) for _ in input().split()]
if 2*K <= B-A:
    print("\n".join([str(_) for _ in list(range(A,A+K)) + list(range(B-K+1,B+1))]))
else:
    print("\n".join([str(_) for _ in list(range(A,B+1))])
Problem p03436 (python)

Fixed submission:
import itertools
H, W = [int(_) for _ in input().split()]
S = []
count_dot = 0
for _ in range(H):
    s = input()
    count_dot += s.count('.')
    S += [list(s)]
S[0][0] = '#'
coord = [[0, 0]]
count = 1
flag = True
while flag:
    count += 1
    coord_new = []
    for i, j in coord:
        for x, y in [i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]:
            if (x, y) == (H - 1, W - 1):
                print(count_dot - count)
                exit()
            elif 0 <= x and x < H and 0 <= y and y < W and S[x][y] == '.':
                S[x][y] = '#'
                coord_new += [[x, y]]
    coord = coord_new
    flag = len(coord)
print(-1)

Buggy submission (verdict: Runtime Error):
import itertools
H, W = [int(_) for _ in input().split()]
S = []
count_dot = 0
for _ in range(H):
    s = input()
    count_dot += s.count('.')
    S += [list(s)]


def parse(i, j):
    for x, y in ([i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]):
        if x < 0 or H <= x or y < 0 or W <= y:
            continue
        elif S[x][y] == '.':
            S[x][y] = S[i][j] + 1
            parse(x, y)


S[0][0] = 1
parse(0, 0)
print(count_dot - S[-1][-1])
Problem p03458 (python)

Fixed submission:
import itertools
import numpy as np
N, K, *XYC = open(0).read().split()
N = int(N)
K = int(K)
board = np.zeros((4 * K + 1, 4 * K + 1), dtype=np.uint16)
for x, y, c in zip(XYC[::3], XYC[1::3], XYC[2::3]):
    x = int(x)
    y = int(y)
    if c == 'W':
        x += K
    x = x % (2 * K)
    y = y % (2 * K)
    board[x, y] += 1
    board[(x + K) % (2 * K), (y + K) % (2 * K)] += 1
board[1 + 2 * K:, 1 + 2 * K:] = board[:2 * K, :2 * K]
board[1:1 + 2 * K, 1 + 2 * K:] = board[1 + 2 * K:, 1 + 2 * K:]
board[1 + 2 * K:, 1:1 + 2 * K] = board[1 + 2 * K:, 1 + 2 * K:]
board[1:1 + 2 * K, 1:1 + 2 * K] = board[1 + 2 * K:, 1 + 2 * K:]
board[0, :] = 0
board[:, 0] = 0
for j in range(1, 1 + 4 * K):
    board[:, j] += board[:, j - 1]
for j in range(1, 1 + 4 * K):
    board[j, :] += board[j - 1, :]
ans = np.max(board[K:3 * K, K:3 * K] + board[:2 * K, :2 * K] -
             board[:2 * K, K:3 * K] - board[K:3 * K, :2 * K])
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import itertools
N, K = [int(_) for _ in input().split()]
XYC = []
b = np.zeros((2 * K, 2 * K), dtype=int)
for _ in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'W':
        y += K
    dy = 2 * K * (y // (2 * K))
    y -= dy
    x -= dy
    x %= 2 * K
    b[x, y] += 1
    b[(x + K) % (2 * K), y + K] += 1
c = np.pad(b, [(1, 0), (1, 0)], 'constant')
acc = c.cumsum(axis=0).cumsum(axis=1)
ans = max(acc[i + K, j + K] - acc[i, j + K] - acc[i + K, j] + acc[i, j]
          for i, j in itertools.product(range(K), range(K)))
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import itertools
N, K = [int(_) for _ in input().split()]
XYC = []
b = np.zeros((2 * K, 2 * K), dtype=int)
for _ in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'W':
        y += K
    dy = K * (y // (2 * K))
    y -= dy
    x -= dy
    x %= 2 * K
    b[x, y] += 1
    b[(x + K) % (2 * K), y + K] += 1
c = np.pad(b, [(1, 0), (1, 0)], 'constant')
acc = c.cumsum(axis=0).cumsum(axis=1)
ans = max(acc[i + K, j + K] - acc[i, j + K] - acc[i + K, j] + acc[i, j]
          for i, j in itertools.product(range(K), range(K)))
print(ans)
Problem p03463 (python)

Fixed submission:
N, A, B = [int(_) for _ in input().split()]
if (B - A) % 2:
    print('Borys')
else:
    print('Alice')

Buggy submission (verdict: Runtime Error):
import math
K = int(input())
A = [int(_) for _ in input().split()]
l = r = 2
'''
if x children form groups consisting of y children each
 then z children are left
=> z=y*(x//y)
=> y*(math.ceil(z/y))<=x<y*(math.floor(z/y)+1)
'''
#necessary conditions
for a in A[::-1]:
    l = a * math.ceil(l / a)
    r = a * (math.floor(r / a) + 1) - 1
#sufficient conditions
for x in [l, r]:
    for a in A:
        x = a * (x // a)
    if x != 2:
        print(-1)
        exit()
print(l, r)
Problem p03550 (python)

Fixed submission:
N, Z, W = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
#Xさんが最初に全部引くか、Xさんが最後から2枚目まで引く
print(max(abs(W - A[-1]), abs(A[N - 2] - A[-1])))

Buggy submission (verdict: Runtime Error):
N, Z, W = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
#[abs(a-A[-1]) for a in A[i:-1]]が広義単調増加になるような最小のiを選ぶと、
#Xさんは最初にA[i]までとるのが最善策の候補
#このときYさんは最後までとるのが最善
#ただしabs(A[i]-A[-1])がWより小さいならXさんは最初に全部とってしまうのが最善
ans = 0
b = A[-1]
j = N - 2
for i in range(N - 3, -1, -1):
    if abs(A[i] - b) <= abs(A[i + 1] - b):
        j -= 1
    else:
        break
print(max(abs(A[i] - b), abs(W - b)))
Problem p03589 (python)

Fixed submission:
N = int(input())
for h in range(N//4 + 1, 3501):
    for n in (N * h // (4 * h - N) + 1, 3501):
        w = N * h * n // (4 * h * n - N * h - N * n)
        if w * (4 * h * n - N * h - N * n) == N * h * n:
            print(h, n, w)
            exit()

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

maxh = int(3 * N / 4)

for h in range(1, min(maxh, 3501)):
    maxn = int(2 / (4 / N - 1 / h))
    for n in range(h, min(maxn, 3501)):
        w_reciprocal = (4 / N) - (1 / h) - (1 / n)
        if w_reciprocal > 0:
            w = 1 / w_reciprocal
            if w < 3501 and int(w) == w:
                w = int(w)
                print(h, n, w, sep = ' ')
                exit()
Problem p03597 (python)

Fixed submission:
N = int(input())
A = int(input())
print(N * N - A)

Buggy submission (verdict: Runtime Error):
A, B, C, D, E, F = [int(_) for _ in input().split()]


def main(A, B, C, D, E, F):
    conc = -1
    ans = (1, 2)
    for na in range(F // (100 * A) + 1):
        for nb in range(F // (100 * B) + 1):
            if na == nb == 0:
                continue
            G = F - na * 100 * A - nb * 100 * B
            for nc in range(G // C + 1):
                for nd in range(G // D + 1):
                    W = na * 100 * A + nb * 100 * B + nc * C + nd * D
                    if 0 < W <= F and nc * C + nd * D <= E * (na * A + nb * B):
                        conc2 = 100 * (nc * C + nd * D) / W
                        if conc < conc2 <= 100 * E / (100 + E):
                            ans = (W, nc * C + nd * D)
                            conc = conc2
    return ans


print(*main(A, B, C, D, E, F))
Problem p03602 (python)

Fixed submission:
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)

Buggy submission (verdict: Runtime Error):
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A)
seen = set()
for k, i, j in itertools.product(range(N), repeat=3):
    if (i, j) in seen or k == i or i == j or j == k:
        continue
    elif A[i][j] > A[i][k] + A[j][k]:
        print(-1)
        exit()
    elif A[i][j] == A[i][k] + A[j][k]:
        ans -= A[i][j]
        seen.add((i, k))
        seen.add((k, i))
        seen.add((j, k))
        seen.add((k, j))
    seen.add(i, j)
    seen.add(j, i)
print(ans // 2)

Buggy submission (verdict: Runtime Error):
import collections
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
B = [_.copy() for _ in A]
d_ij = collections.defaultdict(set)
for i in range(N - 1):
    for j in range(i + 1, N):
        d_ij[A[i][j]].add((i, j))
connected = collections.defaultdict(set)
ans = 0
for k in sorted(d_ij.keys()):
    for i, j in d_ij[k]:
        if i not in connected[j]:
            for p in connected[j]:
                if A[p][i] > A[p][j] + A[j][i]:
                    print(-1)
                    exit()
                elif A[p][i] == A[p][j] + A[j][i]:
                    B[p][i] = B[i][p] = 0
                    connected[p].add(i)
                    connected[i].add(p)
            for q in connected[i]:
                if A[q][j] > A[q][i] + A[i][j]:
                    print(-1)
                    exit()
                elif A[q][j] == A[q][i] + A[i][j]:
                    B[q][j] = B[j][q] = 0
                    connected[q].add(j)
                    connected[j].add(q)
            connected[i].add(j)
            connected[j].add(i)
print(sum(sum(_) for _ in B) // 2)
Problem p03649 (python)

Fixed submission:
N = int(input())
A = sorted([int(_) for _ in input().split()])
#B[i]回の操作をすると全項N-1以下になる
#A[i]-N*B[i]+sum(B)-B[i]<=N-1 が全iで成り立つ
#B[i]=0-(-(a+x+1-N))//(N+1)

def check(x):
    b = [0 - (-(a + x + 1 - N)) // (N + 1) for a in A]
    return sum(b) == x

p = max(0, sum(A) - (N - 1) * N)
while True:
    q = check(p)
    if q:
        break
    p += 1
print(p)

Buggy submission (verdict: Runtime Error):
N, *A = [int(_) for _ in open(0).read().split()]
sumB = max(0, sum(A) - N * (N - 1))
while True:
    B = [0 - (-(a + x + 1 - N)) // (N + 1) for a in A]
    if sumB == sum(B):
        break
    sumB += 1
print(sumB)
Problem p03651 (python)

Fixed submission:
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]


def gcd(x, y):
    if x < y:
        return gcd(y, x)
    elif y == 0:
        return x
    else:
        return gcd(y, x % y)


g = A[0]
for a in A:
    g = gcd(g, a)
if K <= max(A) and K % g == 0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]


def gcd(x, y):
    if x < y:
        return gcd(y, x)
    elif y == 0:
        return x
    else:
        return gcd(y, x % y)


g = A.pop()
for a in A:
    g = gcd(g, a)
if K <= max(A) and K % g == 0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')

Buggy submission (verdict: Runtime Error):
import math
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
g = A.pop()
for a in A:
    g = math.gcd(g, a)

Buggy submission (verdict: Runtime Error):
import math
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
g = A.pop()
for a in A:
    g = math.gcd(g, a)
if K <= max(A) and K % g == 0:
    print('POSSIBLE')
else:
    print('IMPOSSIBLE')
Problem p03659 (python)

Fixed submission:
N = int(input())
A = [int(_) for _ in input().split()]+[0]
sumsub = A[0]
sumall = sum(A)
ans = abs(2*sumsub-sumall)
for i in range(1, N-1):
    sumsub += A[i]
    ans = min(ans, abs(2*sumsub-sumall))
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = sorted([int(_) for _ in input().split()], key=lambda x: abs(x))
ans = 0
pos = False
neg = False
while A:
    a = A.pop()
    if ans * a > 0:
        ans -= a
        neg = True
    else:
        ans += a
        pos = True
if pos & neg:
    print(abs(ans))
else:
    print(abs(A[0]-sum(A[1:])))