Problem p03007 (python)

Fixed submission:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import stdin
from collections import deque


def main():
  # input
  N = int(input())
  An = list(map(int, stdin.readline().rstrip().split()))
  An = sorted(An)
  Bn_p,Bn_n = [deque(),deque()]
  for a in An:
    if a >= 0:
      Bn_p.append(a)
    else:
      Bn_n.append(a)
  Bn_n = deque(sorted(Bn_n,reverse=True))

  xy = deque()
  ans = 0
  if len(Bn_p)==0:
    ## 負数のみ
    ans = Bn_n[0]
    for i in range(1,len(Bn_n)):
      x = ans
      y = Bn_n[i]
      xy.append([x,y])
      ans = x-y
  elif len(Bn_n)==0:
    ## 正数のみ
    ans = Bn_p[0]
    for i in range(1,len(Bn_p)-1):
      x = min([Bn_p[i],ans])
      y = max([Bn_p[i],ans])
      xy.append([x,y])
      ans = x-y
    xy.append([Bn_p[-1],ans])
    ans = Bn_p[-1]-ans
  else:
    ## 正負混在
    ans_n = Bn_p.popleft()
    ans_p = Bn_n.popleft()
    ### 正数はminを作り続ける
    for i in range(len(Bn_p)):
      x = ans_p
      y = Bn_p[i]
      xy.append([x,y])
      ans_p = x-y

    Bn_n.append(ans_p)
    ### 負数はmaxを作り続ける
    for i in range(len(Bn_n)):
      x = ans_n
      y = Bn_n[i]
      xy.append([x,y])
      ans_n = x-y
    ans = ans_n

  print(ans)
  for x,y in xy:
    print(x,y)

if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import stdin
from collections import deque


def main():
  # input
  N = int(input())
  An = list(map(int, stdin.readline().rstrip().split()))

  # sort An -> Bn
  An = sorted(An)
  Bn_p,Bn_n = [deque(), deque()]
  for a in An:
    if a >= 0:
      Bn_p.append(a)
    else:
      Bn_n.append(a)

  p,n = [len(Bn_p),len(Bn_n)]      
  xy = []
  while n+p > 2:
    if p==0:
      x = Bn_n.popleft()
      y = Bn_n.popleft()
      Bn_p.append(x-y)      
      xy.append([x,y])
    elif n==0:
      x = Bn_p.popleft()
      y = Bn_p.popleft()
      Bn_n.append(x-y)      
      xy.append([x,y])
    elif p < n:
      x = Bn_p.pop()
      y = Bn_n.pop()
      Bn_p.append(x-y)
      xy.append([x,y])
    else:
      x = Bn_p.pop()
      y = Bn_n.pop()
      Bn_n.append(y-x)
      xy.append([y,x])
    p,n = [len(Bn_p),len(Bn_n)]      

  last = list(Bn_p+Bn_n)
  xy.append([max(last),min(last)])
  ans = max(last)-min(last)


  ## output
  print(ans)
  for x,y in xy:
    print(x,y)

if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from sys import stdin
from collections import deque


def main():
  # input
  N = int(input())
  An = list(map(int, stdin.readline().rstrip().split()))

  # sort An -> Bn
  An = sorted(An)
  Bn_p,Bn_n = [deque(), deque()]
  for a in An:
    if a >= 0:
      Bn_p.append(a)
    else:
      Bn_n.append(a)

  p,n = [len(Bn_p),len(Bn_n)]      
  xy = []
  while n+p > 2:
    if p==0:
      x = Bn_n.popleft()
      y = Bn_n.popleft()
      Bn_p.append(x-y)      
      xy.append([x,y])
    elif n==0:
      x = Bn_p.popleft()
      y = Bn_p.popleft()
      Bn_n.append(x-y)      
      xy.append([x,y])
    elif p < n:
      x = Bn_p.pop()
      y = Bn_n.pop()
      Bn_p.append(x-y)
      xy.append([x,y])
    else:
      x = Bn_p.pop()
      y = Bn_n.pop()
      Bn_n.append(y-x)
      xy.append([y,x])
    p,n = [len(Bn_p),len(Bn_n)]      

  last = list(Bn_p+Bn_n)
  xy.append([max(last),min(last)])
  ans = max(last)-min(last)


  ## output
  print(ans)
  for x,y in xy:
    print(x,y)

if __name__ == "__main__":
  main()