Problem p03227 (python)

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
s=input()
if len(s)==2:
  print(s)
else:
  print(s[2]+s[1]+s[0])

Buggy submission (verdict: Runtime Error):
!/usr/bin/env python3
# -*- coding: utf-8 -*-
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

b = [a[0]]
l = a[0]
r = a[0]
a=a[1:]
res=0

while len(a)>0:
  al = a.pop()
  lx = abs(l-al)
  rx = abs(r-al)

  if lx > rx:
    res = res + lx
    l = al
    if len(a)>0:
      ta = a.pop()
      res = res + abs(r-ta)
      r=ta
  else:
    res = res + rx
    r = al
    if len(a)>0:
      ta = a.pop()
      res = res + abs(l-ta)
      l = ta
  #
  if len(a)>0:
    af = a[0]
    a = a[1:]
    ln = abs(l-af)
    rn = abs(r-af)
    if ln > rn:
      res = res + ln
      l = af
      if len(a)>0:
        ta=a[0]
        res=res+abs(r-ta)
        r=ta
        a=a[1:]
    else:
      res = res + rn
      r = af
      if len(a)>0:
        ta=a[0]
        res=res+abs(l-ta)
        l=ta
        a=a[1:]


print(res)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import deque
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

d = [a[0]]
a=a[1:]
b = deque(d)

f=True
while len(a)>0:
  la=len(a)-1
  lb=len(b)-1
  ln = abs(b[0]-a[0])
  lx = abs(b[0]-a[la])
  rn = abs(b[lb]-a[0])
  rx = abs(b[lb]-a[la])

  x = max(ln, max(lx, max(rn, rx)))
  
  if x == ln:
    b.insert(0,a[0])
    a=a[1:]
  elif x == lx:
    b.insert(0,a.pop())
  elif x == rn:
    b.append(a[0])
    a=a[1:]
  else:
    b.append(a.pop())
#print(b)

res=0
for i in range(0,len(b)-1):
  res=res+abs(b[i]-b[i+1])
print(res)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

b = [a[0]]
a=a[1:]

f=True
while len(a)>0:
  la=len(a)-1
  lb=len(b)-1
  ln = abs(b[0]-a[0])
  lx = abs(b[0]-a[la])
  rn = abs(b[lb]-a[0])
  rx = abs(b[lb]-a[la])

  x = max(ln, max(lx, max(rn, rx)))
  
  if x == ln:
    b.insert(0,a[0])
    a=a[1:]
  elif x == lx:
    b.insert(0,a.pop())
  elif x == rn:
    b.append(a[0])
    a=a[1:]
  else:
    b.append(a.pop())

res=0
for i in range(0,len(b)-1):
  res=res+abs(b[i]-b[i+1])
print(res)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import deque
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

d = [a[0]]
a=a[1:]
b = deque(d)

f=True
while len(a)>0:
  la=len(a)-1
  lb=len(b)-1
  ln = abs(b[0]-a[0])
  lx = abs(b[0]-a[la])
  rn = abs(b[lb]-a[0])
  rx = abs(b[lb]-a[la])

  x = max(ln, max(lx, max(rn, rx)))

  if x == ln:
    b.insert(0,a[0])
    a=a[1:]
  elif x == lx:
    b.insert(0,a.pop())
  elif x == rn:
    b.append(a[0])
    a=a[1:]
  else:
    b.append(a.pop())
#print(b)

res=0
for i in range(0,len(b)-1):
  res=res+abs(b[i]-b[i+1])
print(res)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import deque
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

d = [a[0]]
a=a[1:]
b = deque(d)

f=True
while len(a)>0:
  la=len(a)-1
  lb=len(b)-1
  ln = abs(b[0]-a[0])
  lx = abs(b[0]-a[la])
  rn = abs(b[lb]-a[0])
  rx = abs(b[lb]-a[la])

  x = max(ln, max(lx, max(rn, rx)))

  if x == ln:
    b.insert(0,a[0])
    a=a[1:]
  elif x == lx:
    b.insert(0,a.pop())
  elif x == rn:
    b.append(a[0])
    a=a[1:]
  else:
    b.append(a.pop())

res=0
for i in range(0,len(b)-1):
  res=res+abs(b[i]-b[i+1])
print(res)