Problem p03227 (python)

Fixed submission:
def main(string):
    if len(string) == 2:
        return string
    elif len(string) == 3:
        return string[::-1]

if __name__ == '__main__':
    s = input()
    print(main(s))

Buggy submission (verdict: Runtime Error):
from itertools import accumulate


class CoeffTable:
    def __init__(self, n):
        self.n = n
        if n == 2:
            (self.p2, self.p1, self.m1, self.m2) = (0, 1, 1, 0)
        if n % 2 == 0:
            self.p1 = self.m1 = 1
            self.p2 = self.m2 = (n - 1) // 2
        else:
            self.p1 = 0
            self.m1 = 2
            self.p2 = (n - 1) // 2
            self.m2 = (n - 2) // 2
    def reverse(self):
        (self.p2, self.p1, self.m1, self.m2) = (
            self.m2,
            self.m1,
            self.p1,
            self.p2,
        )

    def cumsum(self):
        return list(accumulate([self.p2, self.p1, self.m1, self.m2]))



def main(n, seq):
    seq = sorted(seq, reverse=True)
    coeff = CoeffTable(n)
    cm = coeff.cumsum()
    ans1 = (
        2 * sum(seq[0 : cm[0]])
        + 1 * sum(seq[cm[0] : cm[1]])
        - 1 * sum(seq[cm[1] : cm[2]])
        - 2 * sum(seq[cm[2] : cm[3]])
    )
    coeff.reverse()
    rcm = coeff.cumsum()
    ans2 = (
        2 * sum(seq[0 : rcm[0]])
        + 1 * sum(seq[rcm[0] : rcm[1]])
        - 1 * sum(seq[rcm[1] : rcm[2]])
        - 2 * sum(seq[rcm[2] : rcm[3]])
    )
    ans = max(ans1, ans2)
    print(ans)
    return


if __name__ == "__main__":
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    main(n, sample)

Buggy submission (verdict: Runtime Error):
from collections import deque

def main(sorted_list):
    if len(sorted_list) == 2:
        print(sorted_list.pop()- sorted_list.popleft())
        return
    if len(sorted_list) == 3:
        print(max(sorted_list[1]+sorted_list[2]-sorted_list[0]*2, -sorted_list[1]-sorted_list[0]+sorted_list[2]*2))
        return
    max_num = sorted_list.pop()
    left = sorted_list.popleft()
    right = sorted_list.popleft()
    ans = 2*max_num - left - right
    status = 'max'

    while sorted_list:
        if status == 'max':
            num = sorted_list.pop()
            status = 'min'
            if left < right:
                ans += num - left
                left = num
            else:
                ans += num - right
                right = num
        else:
            num = sorted_list.popleft()
            status = 'max'
            if left < right:
                ans += right - num
                right = num
            else:
                ans += left - num
                left = num
    print(ans)


if __name__ == '__main__':
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    sorted_list = deque(sorted(sample))
    main(sorted_list)

Buggy submission (verdict: Runtime Error):
from collections import deque

def main(sorted_list):
    if len(sorted_list) == 2:
        print(sorted_list.pop()- sorted_list.popleft())
        return
    if len(sorted_list) == 3:
        print(max(sorted_list[1]+sorted_list[2]-sorted_list[0]*2, -sorted_list[1]-sorted_list[0]+sorted_list[2]*2))
        return
    max_num = sorted_list.pop()
    left = sorted_list.popleft()
    right = sorted_list.popleft()
    ans = 2*max_num - left - right
    status = 'max'

    while sorted_list:
        if status == 'max':
            num = sorted_list.pop()
            status = 'min'
            if left < right:
                ans += num - left
                left = num
            else:
                ans += num - right
                right = num
        else:
            num = sorted_list.popleft()
            status = 'max'
            if left < right:
                ans += right - num
                right = num
            else:
                ans += left - num
                left = num
    print(ans)


if __name__ == '__main__':
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    sorted_list = deque(sorted(sample))
    main(sorted_list)