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

Fixed submission:
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)

Buggy submission (verdict: Runtime Error):
h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]

DP = [[float("inf") for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(1,k+1):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] > DP[x][y]+1):
                break
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))

ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)
Problem p02698 (python)

Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

import sys
sys.setrecursionlimit(10**7)

class RangeMinimumQuery:
    def __init__(self, n):
        self.n0 = 2**(n-1).bit_length()
        self.data = [0]*(2*self.n0)

    def query(self, l,r):
        l += self.n0
        r += self.n0
        res = 0
        while l < r:
            if r&1:
                r -= 1
                res = max(res, self.data[r-1])
            if l&1:
                res = max(res, self.data[l-1])
                l += 1
            l >>=1
            r >>=1
        return res

    def update(self, i, x):
        i += self.n0-1
        self.data[i] = x
        while i:
            i = ~-i//2
            self.data[i] = max(self.data[2*i+1], self.data[2*i+2])


n = int(input())
a = list(map(int, input().split()))
d = {j:i for i,j in enumerate(sorted(set(a)))}
m = len(d)
a = [d[i] for i in a]

G = [[] for i in range(n)]
for i in range(n-1):
    u,v = map(int, input().split())
    u,v = u-1, v-1
    G[u].append(v)
    G[v].append(u)

RMQ = RangeMinimumQuery(m+10)
ans = [0]*n
def dfs(cur, prev):
    ai = a[cur]
    now = RMQ.query(ai, ai+1)
    nxt = max(RMQ.query(0, ai)+1, now)
    RMQ.update(ai, nxt)
    ans[cur] = RMQ.query(0, m+1)
    for to in G[cur]:
        if to != prev:
            dfs(to, cur)
    RMQ.update(ai, now)
dfs(0,-1)

print(*ans, sep="\n")

Buggy submission (verdict: Runtime Error):
n,m = map(int, input().split())
if n %2 == 0:
    k = 2*m+2
    for i in range(m):
        if i == 0:
            print(i+1, k)
        else:
            print(i+1, k-1-i)
else:
    for i in range(m):
        print(i+1, n-1-i)
Problem p02748 (python)

Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__


a,b,m = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
ans = min(aa) + min(bb)

for i in range(m):
    x,y,c = map(int, input().split())
    x,y = x-1, y-1
    tmp = aa[x] + bb[y] - c
    ans = min(ans, tmp)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__


a,b,m = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
ans = min(aa) + min(bb)

for i in range(m):
    x,y,c = map(int, input().split())
    x,y = x-1, y-1
    tmp = aa[x] + aa[y] - c
    ans = min(ans, tmp)
print(ans)
Problem p02775 (python)

Fixed submission:
n = list(map(int, list(input())))
m = len(n)

DP = [[float("inf")]*2 for i in range(m+1)]
DP[0] = [0,1]

for i in range(m):
    # tight
    tmp = float("inf")
    for j in range(10):
        if j == 0:
            tmp = min(tmp, DP[i][1]+9-n[i])
        elif j <= n[i]:
            tmp = min(tmp, DP[i][1]+j+10+j-1-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-1-n[i])
    DP[i+1][1] = tmp

    # not tight
    tmp = float("inf")
    for j in range(10):
        if j < n[i]:
            tmp = min(tmp, DP[i][1]+j+(10+j)-n[i])
        else:
            tmp = min(tmp, DP[i][0]+j+j-n[i])
    DP[i+1][0] = tmp

print(DP[-1][0])

Buggy submission (verdict: Runtime Error):
from functools import lru_cache

n = list(map(int, list(input())))
m = len(n)

@lru_cache(10**10)
def rec(keta, tight):
    if keta == m:
        if tight:
            return 1
        else:
            return 0
    tmp = float("inf")
    for i in range(10):
        if tight:
            if i == 0:
                tmp = min(tmp, rec(keta+1, True)+9-n[keta])

            elif i <= n[keta]:
                tmp = min(tmp, rec(keta+1, True)+i+10+i-1-n[keta])
            else:
                tmp = min(tmp, rec(keta+1, False)+i+i-1-n[keta])
        else:
            if i < n[keta]:
                tmp = min(tmp, rec(keta+1, True)+i+(10+i)-n[keta])
            else:
               tmp = min(tmp, rec(keta+1, False)+i+i-n[keta])
    return tmp

ans = rec(0, False)
print(ans)
Problem p02803 (python)

Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

from heapq import heappop, heappush

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]

h,w = map(int, input().split())
board = [list(input()) for i in range(h)]

def dfs(x, y):
    dist = [[float("inf")]*w for i in range(h)]
    heap = [(0, x, y)]
    while heap:
        c,x,y = heappop(heap)
        if dist[x][y] <= c:
            continue
        dist[x][y] = c
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if 0<=nx<h and 0<=ny<w and board[nx][ny] == "." and dist[nx][ny] > c+1:
                heappush(heap, (c+1, nx, ny))
    ret = 0
    for i in range(h):
        for j in range(w):
            if dist[i][j] != float("inf"):
                ret = max(ret, dist[i][j])
    return ret

ans = 0
for i in range(h):
    for j in range(w):
        if board[i][j] == ".":
            ans = max(ans, dfs(i,j))

print(ans)

Buggy submission (verdict: Runtime Error):
h,w = map(int, input().split())
board = [list(input()) for i in range(h)]

road = []

for i in range(h):
    for j in range(w):
        if board[i][j] == ".":
            road.append([i,j])

from heapq import heappop, heappush

def dfs(start, end):
    dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
    dist = [[float("inf")]*w for i in range(h)]
    heap = [(0, *start)]
    while heap:
        c,x,y = heappop(heap)
        if dist[x][y] <= c:
            continue
        dist[x][y] = c
        for dx, dy in dxdy:
            nx = x + dx
            ny = y + dy
            if 0<=nx<h and 0<=ny<w and board[nx][ny] == ".":
                heappush(heap, (c+1, nx, ny))
    return dist[end[0]][end[1]]


from itertools import combinations

ans = 0
for start, end in combinations(road, 2):
    #print(start, end)
    tmp = dfs(start, end)
    if tmp != float("inf"):
        ans = max(ans, tmp)
print(ans)
Problem p02822 (python)

Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

from collections import deque

mod = 10**9+7

n = int(input())
G = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    G[a].append(b)
    G[b].append(a)

que = deque()
dep = [0]*n
que.append((0,-1, 0))
order = []
while que:
    cur, prev, d = que.popleft()
    dep[cur] = d
    order.append(cur)
    for to in G[cur]:
        if to != prev:
            que.append((to, cur, d+1))

cnt = [1]*n
for i in reversed(order):
    for to in G[i]:
        if dep[to] < dep[i]:
            cnt[to] += cnt[i]

power = [0]*(n)
powe2_n = pow(2, n, mod)
powe2_n = pow(powe2_n, mod-2, mod)
power[n-1] = powe2_n
for i in reversed(range(n-1)):
    powe2_n *= 2
    powe2_n %= mod
    power[i] = powe2_n
ans = 0
que = deque()
que.append((0, -1))

while que:
    cur, prev = que.pop()
    if len(G[cur]) == 1:
        if prev == -1:
            que.append((G[cur][0], cur))
        continue

    prob = 1
    prob -= power[n-2]

    for to in G[cur]:
        if to == prev:
            bubun_cnt = n-cnt[cur]
        else:
            que.append((to, cur))
            bubun_cnt = cnt[to]
        tmp = 1-power[bubun_cnt-1]
        tmp *= power[n-1-bubun_cnt-1]
        prob -= tmp
    prob %= mod
    ans += power[0]*prob
    ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

mod = 10**9+7

n = int(input())
G = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a,b = a-1,b-1
    G[a].append(b)
    G[b].append(a)

cnt = [0]*n
def dfs(cur, prev):
    tmp = 1
    for to in G[cur]:
        if to != prev:
            tmp += dfs(to, cur)
    cnt[cur] = tmp
    return tmp

dfs(0, -1)

power = [0]*(n)
powe2_n = pow(2, n, mod)
powe2_n = pow(powe2_n, mod-2, mod)
power[n-1] = powe2_n
for i in reversed(range(n-1)):
    powe2_n *= 2
    powe2_n %= mod
    power[i] = powe2_n
ans = 0
from collections import deque
que = deque()
que.append((0, -1))

while que:
    cur, prev = que.pop()
    if len(G[cur]) == 1:
        if prev == -1:
            que.append((G[cur][0], cur))
        continue

    prob = 1
    prob -= power[n-2]

    for to in G[cur]:
        if to == prev:
            bubun_cnt = n-cnt[cur]
        else:
            que.append((to, cur))
            bubun_cnt = cnt[to]
        tmp = 1-power[bubun_cnt-1]
        tmp *= power[n-1-bubun_cnt-1]
        prob -= tmp
    prob %= mod
    ans += power[0]*prob
    ans %= mod

print(ans)
Problem p02839 (python)

Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

h,w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
b = [list(map(int, input().split())) for i in range(h)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        elif i == 0:
            up = min((i+j+1)*80+1, ma-k)
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k]
        elif j == 0:
            for l in range(up):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Buggy submission (verdict: Runtime Error):
h,w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
b = [list(map(int, input().split())) for i in range(h)]
m = 2*80*80
DP = [[[0]*m for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
        else:
            for l in range((i+j+1)*80+1):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
print(DP[-1][-1].index(1))
Problem p02840 (python)

Fixed submission:
n,x,d = map(int, input().split())

if d == 0:
    if x == 0:
        print(1)
    else:
        print(n+1)
    exit()

node = []
for i in range(n+1):
    j = n-i
    base = x*(i-j)
    base += 2*d*(i-1)*i//2
    right = (n-i)*i
    if d<0:
        base += 2*d*right
    node.append((base, right))

node.sort()
d = abs(d)
di = {}
mod = 2*d
ans = 0
for base, r in node:
    i = base%mod
    if i in di:
        right_most = di[i]
        if right_most < base:
            ans += r+1
            di[i] = base+2*d*r
        elif base+2*d*r <= right_most:
            continue
        else:
            nxt = base+2*d*r
            ans += (nxt-right_most)//mod
            di[i] = nxt
    else:
        ans += r+1
        di[i] = base+2*d*r

print(ans)

Buggy submission (verdict: Runtime Error):
n,x,d = map(int, input().split())

node = []
for i in range(n+1):
    j = n-i
    base = x*(i-j)
    base += 2*d*(i-1)*i//2
    r = (n-i)*i
    node.append((base, r))

node.sort()
di = {}
mod = 2*d
ans = 0
for base, r in node:
    i = base%mod
    if i in di:
        right_most = di[i]
        if right_most < base:
            ans += r+1
            di[i] = base+2*d*r
        elif base+2*d*r <= right_most:
            continue
        else:
            nxt = base+2*d*r
            ans += (nxt-right_most)//mod
            di[i] = nxt
    else:
        ans += r+1
        di[i] = base+2*d*r

print(ans)
Problem p02845 (python)

Fixed submission:
mod = 10**9+7

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

remain = 3
s = [0,0,0]
ans = 1
for i in a:
    k = s.count(i)
    ans *= s.count(i)
    ans %= mod
    if k == 0:
        break
    s[s.index(i)] += 1

print(ans)

Buggy submission (verdict: Runtime Error):
mod = 10**9+7

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

remain = 3
s = [0,0,0]
ans = 1
for i in a:
    ans *= s.count(i)
    ans %= mod
    s[s.index(i)] += 1

print(ans)
Problem p02889 (python)

Fixed submission:
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
import sys
input = sys.stdin.readline

def main():
    INF = float("Inf")
    n,m,l = map(int, input().split())
    G = [[INF]*n for i in range(n)]
    for i in range(n):
        G[i][i] = 0
    for i in range(m):
        a,b,c = map(int, input().split())
        a,b = a-1,b-1
        if c <= l:
            G[a][b] = G[b][a] = c
    G = floyd_warshall(G, directed=False)
    G = np.where(G <=l, 1, INF)
    for i in range(n):
        G[i][i] = 0
    GG = floyd_warshall(G, directed=False)

    q = int(input())
    ans = []
    for i in range(q):
        s,t = map(int, input().split())
        a = GG[s-1][t-1]
        if a == INF:
            a = 0
        else:
            a = int(a)
        ans.append(a-1)
    print(*ans, sep="\n")

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
import sys
input = sys.stdin.readline

def main():
    INF = float("Inf")
    n,m,l = map(int, input().split())
    G = [[INF]*n for i in range(n)]
    for i in range(n):
        G[i][i] = 0
    for i in range(m):
        a,b,c = map(int, input().split())
        a,b = a-1,b-1
        if c <= l:
            G[a][b] = G[b][a] = c
    G = floyd_warshall(G, directed=False)
    G = np.where(G <=l, 1, INF)
    for i in range(n):
        G[i][i] = 0
    GG = floyd_warshall(G, directed=False)

    q = int(input())
    ans = []
    for i in range(q):
        s,t = map(int, input().split())
        a = int(GG[s-1][t-1])
        if a == INF:
            a = 0
        ans.append(a-1)
    print(*ans, sep="\n")

if __name__ == "__main__":
    main()
Problem p02985 (python)

Fixed submission:
from collections import deque

mod = 10**9+7
n, k = map(int, input().split())
edges = [[] for i in range(n)]
if n == 1:
    print(k)
    exit()
for i in range(n-1):
    a, b = map(int, input().split())
    a-=1
    b-=1
    edges[a].append(b)
    edges[b].append(a)

start = None
for i in range(n):
    if len(edges[i]) == 1:
        start = i
        break

ans = 1
kumi = [0]*n
q = deque()
q.append((start,-1, 1, 0, 0))
while q:
    now, par, par_cnt,par2,child_cnt = q.popleft()
    a = par_cnt
    a *= k - par2 - child_cnt
    a %= mod
    cnt = 0
    for to in edges[now]:
        if to != par:
            if cnt == 0:
                q.append((to, now, a, min(par2+1, 2), 0))
            else:
                q.append((to, now, 1, min(par2+1, 2), cnt))
            cnt += 1
    if cnt == 0:
        ans *= a
        ans %= mod
print(ans)

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

mod = 10**9+7
n, k = map(int, input().split())
edges = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a-=1
    b-=1
    edges[a].append(b)
    edges[b].append(a)

start = None
for i in range(n):
    if len(edges[i]) == 1:
        start = i
        break

ans = 1
kumi = [0]*n
q = deque()
q.append((start,-1, 1, 0, 0))
while q:
    now, par, par_cnt,par2,child_cnt = q.popleft()
    a = par_cnt
    a *= k - par2 - child_cnt
    a %= mod
    cnt = 0
    for to in edges[now]:
        if to != par:
            if cnt == 0:
                q.append((to, now, a, min(par2+1, 2), 0))
            else:
                q.append((to, now, 1, min(par2+1, 2), cnt))
            cnt += 1
    if cnt == 0:
        ans *= a
        ans %= mod
print(ans)
Problem p03007 (python)

Fixed submission:
n = int(input())
b = list(map(int, input().split()))
b.sort()
if n == 2:
    print(b[-1]-b[0])
    print(b[-1], b[0])
    exit()
if b[0] == 0 and b[-1] == 0:
    print(0)
    for i in range(n-1):
        print(0,0)
elif b[0] > 0 and b[-1] > 0:
    ans = sum(b) - 2*b[0]
    print(ans)
    print(b[1], b[0])
    left = b[1] - b[0]
    right = b[-1]
    for i in range(2, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)
elif b[0] < 0 and b[-1] < 0:
    ans = -sum(b) + 2*b[-1]
    print(ans)
    b = b[::-1]
    print(b[0], b[1])
    left = 0
    right = b[0] - b[1]
    for i in range(2, n):
        x = right
        y = b[i]
        print(x, y)
        right = x - y
else:
    ans = abs(sum(abs(i) for i in b))
    print(ans)
    now = 1
    left = b[0]
    right = b[-1]
    while b[now] < 0:
        x = right
        y = b[now]
        print(x, y)
        right = x - y
        now += 1
    for i in range(now, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)

Buggy submission (verdict: Runtime Error):
n = int(input())
b = list(map(int, input().split()))
b.sort()
if n == 2:
    print(b[-1]-b[0])
    print(b[-1], b[0])
    exit()
# 同符号
if b[0] > 0 and b[-1] > 0:
    ans = sum(b) - 2*b[0]
    print(ans)
    print(b[1], b[0])
    left = b[1] - b[0]
    right = b[-1]
    for i in range(2, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)
elif b[0] < 0 and b[-1] < 0:
    ans = -sum(b) + 2*b[-1]
    print(ans)
    b = b[::-1]
    print(b[0], b[1])
    left = 0
    right = b[0] - b[1]
    for i in range(2, n):
        x = right
        y = b[i]
        print(x, y)
        right = x - y
# 異符号
else:
    ans = abs(sum(abs(i) for i in b))
    print(ans)
    # 正,負がそれぞれ一つ以上ある
    # まず、正 - 負をして正を作って、負に引く
    now = 1
    left = b[0]
    right = b[-1]
    # 二個目以上の負を全部消化
    while b[now] <= 0:
        x = right
        y = b[now]
        print(x, y)
        right = x - y
        now += 1
    for i in range(now, n-1):
        x = left
        y = b[i]
        print(x, y)
        left = x - y
    print(right, left)
Problem p03043 (python)

Fixed submission:
from math import log2, floor
n, k = map(int, input().split())
a = 1/n
ans = 0
for i in range(1, n+1):
    if i >= k:
        ans += a
        continue
    j = int(log2(k/i)) + 1
    if i*2**(j-1) == k:
        j -= 1
    ans += a/2**j
print(ans)

Buggy submission (verdict: Runtime Error):
from math import log2, floor
n, k = map(int, input().split())
a = 1/n
ans = 0
for i in range(1, n+1):
    if i >= k:
        ans += a
        continue
    j = int(log2(k/i)) + 1
    if i*2**(j-1) == k:
        j -= 1
    ans += a/2**j
print(ans)
Problem p03062 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))

minus = []
plus = []

for i in a:
    if i < 0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort()
minus.sort(reverse=True)

if len(plus)==0:
    if len(minus)%2==0:
        print(-sum(minus))
    else:
        print(-sum(minus)+2*minus[0])
    exit()

if len(minus)==0:
    print(sum(a))
    exit()

if len(minus) % 2==0:
    print(sum(plus) - sum(minus))
    exit()

if - minus[0] < plus[0]:
    print(sum(plus) - sum(minus) +2*minus[0])
else:
    print(sum(plus) - sum(minus) -2*plus[0])

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

minus = []
plus = []

for i in a:
    if i < 0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort()
minus.sort(reverse=True)

if len(plus)*len(minus) == 0:
    plus[133],minus[133]
    print(sum(plus)-sum(minus))
    exit()

if len(minus) % 2==0:
    print(sum(plus) - sum(minus))
    exit()

if - minus[0] < plus[0]:
    print(sum(plus) - sum(minus) +2*minus[0])
else:
    print(sum(plus) - sum(minus) -2*plus[0])

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

minus = []
plus = []

for i in a:
    elif i < 0:
        minus.append(i)
    else:
        plus.append(i)
plus.sort()
minus.sort(reverse=True)

if len(plus)*len(minus) == 0:
    plus[133],minus[133]
    print(sum(plus)-sum(minus))
    exit()

if len(minus) % 2==0:
    print(sum(plus) - sum(minus))
    exit()

if - minus[0] < plus[0]:
    print(sum(plus) - sum(minus) +2*minus[0])
else:
    print(sum(plus) - sum(minus) -2*plus[0])
Problem p03089 (python)

Fixed submission:
n = int(input())
b = list(map(int, input().split()))

def check(b):
    for i in range(len(b))[::-1]:
        if i+1 == b[i]:
            return i
    return -1

ans = []
for i in range(n):
    ind = check(b)
    if ind >= 0:
        ans.append(ind+1)
        b = b[:ind] + b[ind+1:]
    else:
        print(-1)
        exit()
for i in ans[::-1]:
    print(i)

Buggy submission (verdict: Runtime Error):
from collections import deque
def main():
    n = int(input())
    b = np.array(list(map(int, input().split())))
    s = set()
    q = deque()

    q.append([tuple(b), [0]*n, 0])
    ans = None
    while q:
        now, history, depth = q.popleft()
        if now in s:
            continue
        s.add(now)
        if len(now) == 0:
            ans = history
            break
        for i, j in enumerate(now):
            if i+1 == j:
                tmp = history[::]
                tmp[depth] = i+1
                q.appendleft([tuple(now[:i] + now[i+1:]), tmp, depth+1])
    if ans is None:
        print(-1)
    else:
        for i in ans[::-1]:
            if i == 0:
                break
            print(i)
if __name__ == "__main__":
    main()
Problem p03142 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

n,m = map(int, input().split())
G = [[] for i in range(n)]
rG = [[] for i in range(n)]
used = [0]*n
for i in range(n+m-1):
    a,b = map(int, input().split())
    a,b = a-1, b-1
    G[a].append(b)
    rG[b].append(a)
    used[b] = 1
root = used.index(0)

par = [-1]*n
depth = [-1]*n

def rec(cur):
    if depth[cur] != -1:
        return depth[cur]
    m = 0
    i = -1
    for p in rG[cur]:
        if rec(p)+1 > m:
            i = p
            m = rec(p)+1
    par[cur] = i
    depth[cur] = m
    return m

for i in range(n):
    rec(i)
par = [i+1 for i in par]
print(*par, sep="\n")

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n,m = map(int, input().split())
G = [[] for i in range(n)]
used = [0]*n
for i in range(n+m-1):
    a,b = map(int, input().split())
    a,b = a-1, b-1
    G[a].append(b)
    used[b] = 1
root = used.index(0)

dist = [0]*n
def rec(cur, depth):
    dist[cur] = max(dist[cur], depth)
    for to in G[cur]:
        rec(to, depth+1)

rec(root, 0)

par = [0]*n
for from_ in range(n):
    for to in G[from_]:
        if dist[from_] + 1 == dist[to]:
            par[to] = from_+1
print(*par, sep="\n")