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

Fixed submission:
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

result = [[None] * W for _ in range(H)]
d = 1
x, y = 0, 0
for i in range(N):
    t = a[i]
    while t > 0:
        result[y][x] = i + 1
        x += d
        if x == -1 or x == W:
            x -= d
            d = -d
            y += 1
        t -= 1

for i in range(H):
    print(*result[i])

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

result = [[None] * W for _ in range(H)]
d = 1
x, y = 0, 0
for i in range(N):
    t = a[i]
    while t > 0:
        result[y][x] = i + 1
        if x == 0 or x == W - 1:
            d = -d
            y += 1
        else:
            x += d
        t -= 1

for i in range(H):
    print(*result[i])
Problem p03658 (python)

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

l.sort(reverse=True)
print(sum(l[:K]))

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

l.sort()
print(sum(l[-K:])
Problem p03712 (python)

Fixed submission:
H, W = map(int, input().split())

print('#' * (W + 2))
for i in range(H):
    print('#%s#' % (input()))
print('#' * (W + 2))

Buggy submission (verdict: Runtime Error):
H, W = map(int, input())

print('#' * (W + 2))
for i in range(H):
    print('#%s#' % (input()))
print('#' * (W + 2))
Problem p03732 (python)

Fixed submission:
from sys import stdin
from itertools import accumulate
readline = stdin.readline

N, W = map(int, input().split())
vs = [[] for _ in range(4)]
w, v = map(int, input().split())
w1 = w
vs[0].append(v)
for _ in range(N - 1):
    w, v = map(int, input().split())
    vs[w - w1].append(v)

for i in range(4):
    vs[i].sort(reverse=True)
    vs[i] = list(accumulate(vs[i]))

result = 0
for i in range(len(vs[0]) + 1):
    a = W - w1 * i
    if a < 0:
        break
    for j in range(len(vs[1]) + 1):
        b = a - (w1 + 1) * j
        if b < 0:
            break
        for k in range(len(vs[2]) + 1):
            c = b - (w1 + 2) * k
            if c < 0:
                break
            t = 0
            if i != 0:
              t += vs[0][i - 1]
            if j != 0:
              t += vs[1][j - 1]
            if k != 0:
              t += vs[2][k - 1]
            for l in range(len(vs[3]) + 1):
                d = c - (w1 + 3) * l
                if d < 0:
                    break
                if l == 0:
                    result = max(result, t)
                else:
                    result = max(result, t + vs[3][l -1])
print(result)

Buggy submission (verdict: Runtime Error):
from sys import stdin
from itertools import accumulate
readline = stdin.readline

N, W = map(int, input().split())
vs = [[] for _ in range(4)]
w, v = map(int, input().split())
w1 = w
vs[0].append(v)
for _ in range(N - 1):
    w, v = map(int, input().split())
    vs[w - w1].append(v)

for i in range(4):
    vs[i].sort(reverse=True)
    vs[i] = [0] + list(accumulate(vs[i]))

result = 0
for i in range(len(vs[0]) + 1):
    a = W - w1 * i
    if a < 0:
        break
    for j in range(len(vs[1]) + 1):
        b = a - (w1 + 1) * j
        if b < 0:
            break
        for k in range(len(vs[2]) + 1):
            c = b - (w1 + 2) * k
            if c < 0:
                break
            t = vs[0][i] + vs[1][j] + vs[2][k]
            for l in range(len(vs[3]) + 1):
                d = c - (w1 + 3) * l
                if d < 0:
                    break
                result = max(result, t + vs[3][l])
print(result)
Problem p03854 (python)

Fixed submission:
import sys
s = raw_input()
ts = ['']
while True:
  nts= []
  for t in ts:
    for w in ['dreamer', 'eraser', 'dream', 'erase']:
      if s == t + w:
        print 'YES'
        sys.exit()
      if s.startswith(t + w):
        nts.append(t + w)
  if len(nts) == 0:
    print 'NO'
    sys.exit()
  ts = nts

Buggy submission (verdict: Runtime Error):
package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string
	fmt.Scanln(&s)
	r := []rune(s)
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	words := [][]rune{[]rune("maerd"), []rune("remaerd"), []rune("esare"), []rune("resare")}
	for {
		if len(r) == 0 {
			fmt.Println("YES")
			return
		}
		fail := 0
		for _, w := range words {
			if strings.HasPrefix(r, w) {
				r = r[len(w):]
				break
			} else {
				fail++
			}
		}
		if fail == len(words) {
			fmt.Println("NO")
			return
		}
	}
}
Problem p03998 (python)

Fixed submission:
S = {}
S['A'] = input()
S['B'] = input()
S['C'] = input()

turn = 'A'
while True:
    if S[turn] == '':
        print(turn)
        break
    t = S[turn][0].upper()
    S[turn] = S[turn][1:]
    turn = t

Buggy submission (verdict: Runtime Error):
S = {}
S['A'] = input()
S['B'] = input()
S['C'] = input()

turn = 'A'
while True:
    if S[turn] == '':
        print(turn)
        break
    turn = S[turn].pop(0).upper()
Problem p03125 (python)

Fixed submission:
a,b = (int(x) for x in input().split())
if b % a == 0:
  print(a+b)
else:
  print(b-a)

Buggy submission (verdict: Runtime Error):
a,b = (int(x) for x in input().split())
if b % a == 0:
  print(a+b)
 else:
  print(a-b)
Problem p02579 (python)

Fixed submission:
# https://atcoder.jp/contests/abc176/submissions/16164430?lang=ja
import heapq

h, w = map(int, input().split())
sh, sw = map(lambda x: int(x) - 1, input().split())
gh, gw = map(lambda x: int(x) - 1, input().split())
S = [input() for i in range(h)]

inf = 10 ** 10
D = [[inf] * w for i in range(h)]

D[sh][sw] = 0
Q = [(0, (sh, sw))]
heapq.heapify(Q)
while Q:
  _, (curr_h, curr_w) = heapq.heappop(Q)
  for di in range(-2, 3):
    for dj in range(-2, 3):
      next_h, next_w = curr_h + di, curr_w + dj
      if 0 <= next_h < h and 0 <= next_w < w and S[next_h][next_w] == ".":
        nex = int(abs(di) + abs(dj) != 1)
        if D[next_h][next_w] > D[curr_h][curr_w] + nex:
          D[next_h][next_w] = D[curr_h][curr_w] + nex
          heapq.heappush(Q, (D[next_h][next_w], (next_h, next_w)))
if D[gh][gw] >= inf:
  print(-1)
else:
  print(D[gh][gw])

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc176/submissions/16164430?lang=ja
import heapq

h, w = map(int, input().split())
sh, sw = map(lambda x: int(x) - 1, input().split())
gh, gw = map(lambda x: int(x) - 1, input().split())
S = [input() for i in range(h)]

inf = 10 ** 10
D = [[inf] * w for i in range(h)]

D[sh][sw] = 0
Q = [(0, (sh, sw))]
heapq.heapify(Q)
while Q:
  curr_h, curr_w = heapq.heappop(Q)
  for di in range(-2, 3):
    for dj in range(-2, 3):
      next_h, next_w = curr_h + di, curr_w + dj
      if 0 <= next_h < h and 0 <= next_w < w and S[next_h][next_w] == ".":
        nex = int(abs(di) + abs(dj) != 1)
        if D[next_h][next_w] > D[curr_h][curr_w] + nex:
          D[next_h][next_w] = D[curr_h][curr_w] + nex
          heapq.heappush(Q, (next_h, next_w))
if D[gh][gw] >= inf:
  print(-1)
else:
  print(D[gh][gw])

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/abc176/submissions/16164430?lang=ja
import heapq

h, w = map(int, input().split())
sh, sw = map(lambda x: int(x) - 1, input().split())
gh, gw = map(lambda x: int(x) - 1, input().split())
S = [input() for i in range(h)]

inf = 10 ** 10
D = [[inf] * w for i in range(h)]

D[sh][sw] = 0
Q = [(0, (sh, sw))]
heapq.heapify(Q)
while Q:
  curr_h, curr_w = heapq.heappop(Q)
  for di in range(-2, 3):
    for dj in range(-2, 3):
      next_h, next_w = curr_h + di, curr_w + dj
      if 0 <= next_h < h and 0 <= next_w < w and S[next_h][next_w] == ".":
        nex = int(abs(di) + abs(dj) != 1)
        if D[next_h][next_w] > D[curr_h][curr_w] + nex:
          D[next_h][next_w] = D[curr_h][curr_w] + nex
          heapq.heappush(Q, (next_h, next_w))
if D[gh][gw] >= inf:
  print(-1)
else:
  print(D[gh][gw])
Problem p02609 (python)

Fixed submission:
n = int(input())
x = input()
x10 = int(x, 2)
lenx = len(x)

maxbit = x.count("1")
xc0 = x10 % (maxbit + 1)
xc1 = x10 % (maxbit - 1) if maxbit > 1 else 0

def f(ii):
  cnt = 0
  while ii > 0:
    ii %= bin(ii).count("1")
    cnt += 1
  return cnt

for i in range(lenx):
  if x[i] == "0":
    t = xc0 + pow(2, (lenx - 1 - i), maxbit + 1)
    ans = f(t % (maxbit + 1)) + 1
  else:
    if maxbit - 1 == 0:
      ans = 0
    else:
      t = xc1 - pow(2, (lenx - 1 - i), maxbit - 1)
      ans = f(t % (maxbit - 1)) + 1
  print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = input()
x10 = int(x, 2)
lenx = len(x)

maxbit = x.count("1")

box = [0] * (maxbit + 1)

def f(i):
  cnt = 0
  while i > 0:
    if box[i] != 0:
      return box[i] + cnt
    i %= bin(i).count("1")
    cnt += 1
  return cnt

for i in range(1, min(maxbit + 1, 200000)):
  box[i] = f(i)

for i in range(lenx):
  if x[i] == "0":
    mb = maxbit + 1
    t = x10 + pow(2, (lenx - 1 - i), mb)
  else:
    mb = maxbit - 1
    t = x10 - pow(2, (lenx - 1 - i), mb)
  # print(t, mb)
  if t == 0:
    print(0)
  else:
    t %= mb 
    if t == 0:
      print(1)
    else:
      ans = f(t) + 1 
      box[t] = ans
      print(ans)

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

n = int(input())
x = input()
x10 = int(x, 2)
lenx = len(x)

maxbit = x.count("1")

box = [0] * (maxbit + 1)

@lru_cache(maxsize=None)
def f(i):
  cnt = 1
  while i > 0:
    if box[i] != 0:
      return box[i] + cnt
    i %= bin(i).count("1")
    cnt += 1
  return cnt

xlist = []

for i in range(lenx):
  if x[i] == "0":
    t = x10 + 2 ** (lenx - 1 - i)
  else:
    t = x10 - 2 ** (lenx - 1 - i)
  t %= (maxbit - 1) 
  ans = f(t)
  box[t] = ans
  print(ans)
Problem p02616 (python)

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

MOD = 10 ** 9 + 7

AA = sorted(A, key=lambda x: -abs(x))

nega1 = []
posi1 = []
for a in AA[:k]:
  if a < 0:
    nega1.append(a)
  else:
    posi1.append(a)

nega2 = []
posi2 = []
for a in AA[k:]:
  if a < 0:
    nega2.append(a)
  else:
    posi2.append(a)

if all(a < 0 for a in A) and k % 2 == 1:
  AA = sorted(A, key=lambda x: abs(x))
  nega1 = []
  posi1 = []
  for a in AA[:k]:
    nega1.append(a)

elif n == k:
  pass

elif len(nega1) % 2 != 0:
  if posi1 and nega2 and posi2:
    if abs(nega1[-1] * nega2[0]) < abs(posi1[-1] * posi2[0]):
      del nega1[-1]
      nega1.append(posi2[0])
    else:
      del posi1[-1]
      posi1.append(nega2[0])
  elif not posi1 or not nega2:
    del nega1[-1]
    nega1.append(posi2[0])
  elif not posi2:
    del posi1[-1]
    posi1.append(nega2[0])
  
a = 1
for m in posi1:
  a = (a * m) % MOD
for p in nega1:
  a = (a * p) % MOD
 
print(a)

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

MOD = 10 ** 9 + 7

AA = sorted(A, key=lambda x: -abs(x))

ans = []
mns = []
pls = []
itr = 0
while itr < k:
  ans.append(AA[itr])
  if AA[itr] < 0:
    mns.append(AA[itr])
  else:
    pls.append(AA[itr])
  itr += 1

mflg = False
pflg = False
zflg = False
if all(a < 0 for a in A) and k % 2 == 1:
  AA = sorted(A, key=lambda x: abs(x))
  mns = []
  for itr in range(k):
    mns.append(AA[itr])

elif len(mns) % 2 != 0:
  mm = mns[-1]
  pp = pls[-1]
  for i in range(itr, n):
    nn = AA[i]
    if nn > 0 and not pflg:
      pp *= nn
      pflg = True
      pv = nn
    elif nn < 0 and not mflg:
      mm *= nn
      mflg = True
      mv = nn
    elif nn == 0:
      zflg = True
  if mflg and not pflg:
    if pls:
      del pls[-1]
      mns.append(mv)
      
  elif pflg and not mflg:
    del mns[-1]
    pls.append(pv)

a = 1
for m in mns:
  a = (a * m) % MOD
for p in pls:
  a = (a * p) % MOD

print(a)

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

MOD = 10 ** 9 + 7

AA = sorted(A, key=lambda x: -abs(x))

ans = []
mns = []
pls = []
itr = 0
while itr < k:
  ans.append(AA[itr])
  if AA[itr] < 0:
    mns.append(AA[itr])
  else:
    pls.append(AA[itr])
  itr += 1

mflg = False
pflg = False
zflg = False
if all(a < 0 for a in A) and k % 2 == 1:
  AA = sorted(A, key=lambda x: abs(x))
  mns = []
  for itr in range(k):
    mns.append(AA[itr])

elif len(mns) % 2 != 0:
  mm = mns[-1]
  pp = pls[-1]
  for i in range(itr, n):
    nn = AA[i]
    if nn > 0 and not pflg:
      pp *= nn
      pflg = True
      pv = nn
    elif nn < 0 and not mflg:
      mm *= nn
      mflg = True
      mv = nn
    elif nn == 0:
      zflg = True
  if mflg and not pflg:
    del pls[-1]
    mns.append(mv)
  elif pflg and not mflg:
    del mns[-1]
    pls.append(pv)

a = 1
for m in mns:
  a = (a * m) % MOD
for p in pls:
  a = (a * p) % MOD

print(a)
Problem p02641 (python)

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

koho = [i for i in range(-100, 200) if i not in P]

koho.sort(key=lambda a: (abs(a - x), a))

print(koho[0])

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

koho = [i for i in range(1, 101) if i not in P]

koho.sort(key=lambda a: (abs(a - x), a))

print(koho[0])
Problem p02691 (python)

Fixed submission:
from collections import Counter

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

L = [i + a for i, a in enumerate(A)]
R = [i - a for i, a in enumerate(A)]

cL = Counter(L)
cR = Counter(R)

ans = 0
for i in cL.keys():
  ans += cL[i] * cR[i]

print(ans)

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

# A[0]との和 - idxの差の数
box = [0] * (n - 1)

for i in range(n - 1):
  box[A[0] + A[i + 1] - i - 1] += 1 

ans = 0
for a in A:
  ans += sum(i if itr == (A[0] - a) else 0 for itr, i in enumerate(box))

print(ans)
Problem p02741 (python)

Fixed submission:
li = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k = int(input())
print(li[k-1])

Buggy submission (verdict: Runtime Error):
li = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
k = input()
print(li[k-1])
Problem p02850 (python)

Fixed submission:
from collections import deque
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for itr, (a, b) in enumerate(AB):
  e[a].append((b, itr))
  e[b].append((a, itr))
  
cidx = max([len(i) for i in e])

ans = [-1] * (n - 1)

q = deque()
q.append((0, 0, -1))
# 色は１から順に塗っていく
while q:
  v, p, pcol = q.popleft()
  col = 1
  for u, i in e[v]:
    if u != p:
      # 親と色がかぶる時は＋１
      if col == pcol:
        col += 1
      if ans[i] == -1: 
        ans[i] = col
      q.append((u, v, col))
      col += 1 

print(cidx)
for i in ans:
  print(i)

Buggy submission (verdict: Runtime Error):
n = int(input())
e = [[] for _ in range(n)]
AB = [list(map(int, input().split())) for _ in range(n - 1)]
AB = [(a -1, b - 1) for a, b in AB]

for itr, (a, b) in enumerate(AB):
  e[a].append((b, itr))
  e[b].append((a, itr))
  
cidx = max([len(i) for i in e])

ans = [-1] * (n - 1)

def dfs(v = 0, p = 0, p_col = -1):
  # 色は１から順に塗っていく
  col = 1
  for u, i in e[v]:
    if u != p:
      # 親と色がかぶる時は＋１
      if col == p_col:
        col += 1
      if ans[i] == -1: 
        ans[i] = col
      dfs(u, v, col)
      col += 1 

dfs()

print(cidx)
for i in ans:
  print(i)
Problem p02957 (python)

Fixed submission:
A, B = map(int,input().split())
if (A + B) % 2 == 0: 
  print(int((A+B)/2))
else:
  print("IMPOSSIBLE")

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

S = input()
N = len(S)

dp = [[0] * 13 for _ in range(N + 1)]
dp[0][0] = 1

mul = 1

for i, x in enumerate(S[::-1]):
  if x == "?":
    for k in range(10):
      for j in range(13):
        dp[i + 1][(mul * k + j) % 13] += dp[i][j]
        dp[i + 1][(mul * k + j) % 13] %= MOD
  else:
    k = int(x)
    for j in range(13):
      dp[i + 1][(mul * k + j) % 13] += dp[i][j]
      dp[i + 1][(mul * k + j) % 13] %= MOD

  mul = mul * 10

print(dp[N][5])
Problem p02963 (python)

Fixed submission:
inf = 10**9
 
S = int(input())
 
if S == 10 ** 18:
    print(0, 0, inf, 0, inf, inf)
else:
    S1 = S // inf + 1
    S2 = inf - (S % inf)
 
    print(0, 0, S1, 1, S2, inf)

Buggy submission (verdict: Runtime Error):
inf = 10**9
 
S = int(input())
 
if S == 10 ** 18:
    print(0, 0, inf, 0, M, inf)
else:
    S1 = S // inf + 1
    S2 = inf - (S % inf)
 
    print(0, 0, S1, 1, S2, inf)
Problem p02975 (python)

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

if len(aset) == 1:
  if all(i == 0 for i in a):
    print("Yes")
  else:
    print("No")
elif len(aset) == 2:
  a1, a2 = sorted(aset)
  if a1 == 0:
    if a.count(a1) * 3 == n:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
elif len(aset) == 3:
  a1, a2, a3 = aset
  if a.count(a1) == a.count(a2) == a.count(a3):
    if a1 ^ a2 == a3:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  print("No")

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

if len(aset) == 1:
  if all(i == 0 for i in a):
    print("Yes")
  else:
    print("No")
elif len(aset) == 2:
  a1, a2 = sorted(aset)
  if a1 == 0:
    if a.count(a1) * 3 == N:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
elif len(aset) == 3:
  a1, a2, a3 = aset
  if a.count(a1) == a.count(a2) == a.count(a3):
    if a1 ^ a2 == a3:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  print("No")

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

if len(aset) == 1:
  if all(i == 0 for i in a):
    print("Yes")
  else:
    print("No")
elif len(aset) == 2:
  a1, a2 = sorted(aset):
  if a1 == 0:
    if a.count(a1) * 3 == N:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
elif len(aset) == 3:
  a1, a2, a3 = aset
  if a.count(a1) == a.count(a2) == a.count(a3):
    if a1 ^ a2 = a3:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  print("No")
Problem p02996 (python)

Fixed submission:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])
timeSum = 0
for i in AB:
  timeSum += i[0]
  if timeSum > i[1]:
    print("No")
    exit()
print("Yes")

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

n, k = map(int, input().split())

def combinations_count_mod(n, r):
  ret = 1
  for i in range(r):
    ret *= (n-i)/(r-i) % c
  return ret

for i in range(1, k+1):
  a = combinations_count_mod(k-1, i-1) 
  if n-k > i-1:
#    if n-k-i >= 1:
    b = (combinations_count_mod(n-k-1, i-1) * ((n-k-1-(i-1))/i + 2)) 
#    else:
#      b = (n-k-1) * 3
  elif n-k == i-1:
    b = 1
  else:
    b = 0
  print(int((a*b) % c))
Problem p03006 (python)

Fixed submission:
from itertools import combinations
from collections import Counter

N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]

cXY = combinations(XY, 2)

edge = []
for a, b in cXY:
  edge.append((a[0]-b[0], a[1]-b[1]))
  edge.append((-a[0]+b[0], -a[1]+b[1]))

ce = Counter(edge)

if N == 1:
  ans = 1
else:
  dup = max(ce.values())
  ans = N - dup
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
 
import itertools
hoges = {}
for i in itertools.combinations(xy, 2):
    if i[0][0] - i[1][0] >= 0:
        hoge = (i[0][0] - i[1][0], i[0][1] - i[1][1])
    else:
        hoge = (i[1][0] - i[0][0], i[1][1] - i[0][1])
    hoges.setdefault(hoge, 0)
    hoges[hoge] += 1
# print(hoges)
max_v = max(hoges.values())
print(N - max_v)
Problem p03007 (python)

Fixed submission:
from collections import deque
plusQ = deque([]) 
minsQ = deque([])
 
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
 
for itr, val in enumerate(A):
  if itr == 0:
    plusQ.append(val)
  elif itr != len(A) - 1 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)
    
p1 = plusQ.popleft()
 
ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])

Buggy submission (verdict: Runtime Error):
from collections import deque
plusQ = deque([]) 
minsQ = deque([])
 
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
 
for itr, val in enumerate(A):
  if itr != len(A) - 1 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)
    
p1 = plusQ.popleft()
 
ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])

Buggy submission (verdict: Runtime Error):
from collections import deque
plusQ = deque([]) 
minsQ = deque([])
 
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
 
for itr, val in enumerate(A):
  if itr <= (len(A) - 1) // 2 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)
    
p1 = plusQ.popleft()

ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])

Buggy submission (verdict: Runtime Error):
from collections import deque
plusQ = deque([]) 
minsQ = deque([])

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

for itr, val in enumerate(A):
  if itr + 1 <= (len(A) + 1) // 2 and val > 0:
    plusQ.append(val)
  else:
    minsQ.append(val)

# print(plusQ, minsQ)
p1 = plusQ.popleft()

ans = deque([])
while len(minsQ) > 0:
  if len(plusQ) > 0:
    p, m = plusQ.popleft(), minsQ.popleft()
    ans.append([m, p])
    minsQ.append(m - p)    
  else:
    m = minsQ.popleft()
    ans.append([p1, m])
    p1 = p1 - m

print(p1)
while len(ans) > 0:
  hoge = ans.popleft()
  print(hoge[0],hoge[1])
Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(20000000)

def dfs(node, distance):
    for next_node, edge_size in G[node]:
        if ans[next_node] == -1:
            ans[next_node] = ((distance + edge_size) % 2)
            dfs(next_node, distance + edge_size)

N = int(input())

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

ans = [-1] * N
dfs(0, 0)
print(*ans, sep='\n')

Buggy submission (verdict: Runtime Error):
def dfs(i):
    if all([v != -1 for v in visited]):
        return
    color = visited[i]
    for nextIdx, wgt in graph[i]:
        if visited[nextIdx] == -1:
            if wgt % 2 == 0:
                visited[nextIdx] = color
            else:
                visited[nextIdx] = int(not color)
            dfs(nextIdx)

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

visited = [-1] * n
visited[0] = 0
dfs(0)
for v in visited:
    print(v)

Buggy submission (verdict: Runtime Error):
def dfs(node, distance):
    for next_node, edge_size in G[node]:
        if ans[next_node] == -1:
            ans[next_node] = ((distance + edge_size) % 2)
            dfs(next_node, distance + edge_size)

N = int(input())

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

ans = [-1] * N
dfs(0, 0)
print(*ans, sep='\n')
Problem p03047 (python)

Fixed submission:
N, K = map(int, input().split())
print(N-K+1)

Buggy submission (verdict: Runtime Error):
R, G, B ,N = map(int, input().split())

cnt = 0
for r in range(N//R + 1):
  for g in range((N-r*R)//G + 1):
    val = N - (r*R + g*G)
    if val % B == 0 and  val >= 0:
      cnt += 1
print(cnt)