Problem p03229 (python)

Fixed submission:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
l,l_1,l_2 = [],[],[]
if len(A) % 2 == 0:  
  for i in range(len(A)):
    if i <= len(A)/2 - 2:
      l.append(-2)
    elif i == len(A)/2 -1:
      l.append(-1)
    elif i == len(A)/2:
      l.append(1)
    else:
      l.append(2)
  ans_d = [A[i]*l[i] for i in range(len(A))]
  print(sum(ans_d))
if len(A) % 2 == 1:
  for i in range(len(A)):
    if i <= int(len(A)/2) -2:
      l_1.append(2)
      l_2.append(-2)
    elif int(len(A)/2) -1 <= i <= int(len(A)/2):
      l_1.append(1)
      l_2.append(-1)
    else:
      l_1.append(-2)
      l_2.append(2)
  l_1.sort()
  l_2.sort()
  ans_d1 = [A[i]*l_1[i] for i in range(len(A))]
  ans_d2 = [A[i]*l_2[i] for i in range(len(A))]
  print(max(sum(ans_d1),sum(ans_d2)))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
l = []
if len(A) % 2 == 0:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2):]
  l.append(A_s[-1])
  l.append(A_l[0])
  for i in range(1,int(len(A)/2) + 1):
    if i % 2 == 1:
      l.insert(i,A_l[-i])
      l.insert(-i-1,A_s[i-1])
    elif i % 2 == 0:
      l.insert(i,A_s[i-1])
      l.insert(-i-1,A_l[-i])
elif len(A) % 2 == 1:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2)+1:]
  A_m = A[int(len(A)/2)]
  d = abs(A_m - A_s[-1]) - abs (A_m - A_l[0])
  if d >= 0:
    l.append(A_m)
    l.append(A_l[0])
    for i in range(1,int(len(A_s)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
      elif i % 2 == 0:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
    l.insert(int(len(A_s)/2)+1,A_l[1])
  else:
    l.append(A_s[-1])
    l.append(A_m)
    for i in range(1,int(len(A_l)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
      elif i % 2 == 0:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
    l.insert(int(len(A_l)/2)+1,A_s[-2])
l_d = [abs(l[i]-l[i+1] ) for i in range(len(l)-1)]
print(sum(l_d))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
l = []
if len(A) % 2 == 0:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2):]
  l.append(A_s[-1])
  l.append(A_l[0])
  for i in range(1,int(len(A)/2) + 1):
    if i % 2 == 1:
      l.insert(i,A_l[-i])
      l.insert(-i-1,A_s[i-1])
    elif i % 2 == 0:
      l.insert(i,A_s[i-1])
      l.insert(-i-1,A_l[-i])
elif len(A) % 2 == 1:
  A_s = A[:int(len(A)/2)]
  A_l = A[int(len(A)/2)+1:]
  A_m = A[int(len(A)/2)]
  d = abs(A_m - A_s[-1]) - abs (A_m - A_l[0])
  if d > 0:
    l.append(A_m)
    l.append(A_l[0])
    for i in range(1,int(len(A_s)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
      elif i % 2 == 0:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
    l.insert(int(len(A_s/2))+1,A_l[1])
  else:
    l.append(A_s[-1])
    l.append(A_m)
    for i in range(1,int(len(A_l)/2)+1):
      if i % 2 == 1:
        l.insert(i,A_l[-i+1])
        l.insert(-i-1,A_l[-i])
      elif i % 2 == 0:
        l.insert(i,A_s[i-1])
        l.insert(-i-1,A_s[i])
    l.insert(int(len(A_l)/2)+1,A_s[-2])
l_d = [abs(l[i]-l[i+1] ) for i in range(len(l)-1)]
print(sum(l_d))