Problem p03229 (python)

Fixed submission:
import collections

n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
if n%2==1:
  q=collections.deque()
  q.append(arr[0])
  l=1
  r=n-1
  move=1
  while 1:
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
    if move==n:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
    if move==n:
      break
  q=list(q)
  ans1=0
  for i in range(n-1):
    ans1+=abs(q[i+1]-q[i])
  q=collections.deque()
  q.append(arr[-1])
  l=0
  r=n-2
  move=1
  while 1:
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
    if move==n:
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
    if move==n:
      break
  q=list(q)
  ans2=0
  for i in range(n-1):
    ans2+=abs(q[i+1]-q[i])
  ans=max(ans1,ans2)
  print(ans)
else:
  q=collections.deque()
  q.append(arr[0])
  l=1
  r=n-1
  move=1
  while 1:
    if n-move==1:
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
    if n-move==1:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
  q=list(q)
  ans1=0
  for i in range(n-2):
    ans1+=abs(q[i+1]-q[i])
  ans1+=max(abs(arr[l]-q[0]),abs(arr[l]-q[-1]))
  q=collections.deque()
  q.append(arr[-1])
  l=0
  r=n-2
  move=1
  while 1:
    if n-move==1:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
    if n-move==1:
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
  q=list(q)
  ans2=0
  for i in range(n-2):
    ans2+=abs(q[i+1]-q[i])
  ans2+=max(abs(arr[r]-q[0]),abs(arr[r]-q[-1]))
  ans=max(ans1,ans2)
  print(ans)

Buggy submission (verdict: Runtime Error):
import collections

n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
q=collections.deque()
q.append(arr[0])
l=1
r=n-1
if n%2==1:
  while 1:
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    if n%4==3:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    if abs(l-r)==1:
      break
  q=list(q)
  ans=0
  for i in range(n-1):
    ans+=abs(q[i+1]-q[i])
  print(ans)
else:
  flag=0
  while 1:
    if l==r:
      flag=0
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    if n%4==0:
      flag=1
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
  q=list(q)
  ans=0
  for i in range(n-2):
    ans+=abs(q[i+1]-q[i])
  ans+=max(abs(arr[l]-q[0]),abs(arr[l]-q[-1]))
  print(ans)

Buggy submission (verdict: Runtime Error):
import collections

n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
q=collections.deque()
q.append(arr[0])
l=0
r=0
flag1=0
flag2=0
if len(arr)%2==1:
  while 1:
    if flag1==0:
      r+=1
      if flag2==0:
        q.append(arr[-r])
        flag2=1
      else:
        q.appendleft(arr[-r])
        flag2=0
        flag1=1
    else:
      l+=1
      if flag2==0:
        q.append(arr[l])
        flag2=1
      else:
        q.appendleft(arr[l])
        flag2=0
        flag1=0
    if l+r+1==n:
      break
  q=list(q)
  ans=0
  for i in range(len(q)-1):
    ans+=abs(q[i]-q[i+1])
  print(ans)
else:
  while 1:
    if flag1==0:
      r+=1
      if flag2==0:
        q.append(arr[-r])
        flag2=1
      else:
        q.appendleft(arr[-r])
        flag2=0
        flag1=1
    else:
      l+=1
      if flag2==0:
        q.append(arr[l])
        flag2=1
      else:
        q.appendleft(arr[l])
        flag2=0
        flag1=0
    if l+r+2==n:
      q=list(q)
      lnum=q[0]
      rnum=q[-1]
      if flag1==0:
        r+=1
        if abs(lnum-arr[-r])>abs(rnum-arr[-r]):
          q=[arr[-r]]+q
        else:
          q=q+[arr[-r]]
      else:
        l+=1
        if abs(lnum-arr[l])>abs(rnum-arr[l]):
          q=[arr[l]]+q
        else:
          q=q+[arr[l]]
      break
  q=list(q)
  ans=0
  for i in range(len(q)-1):
    ans+=abs(q[i]-q[i+1])
  print(ans)