Problem p03244 (python)

Fixed submission:
import collections

n = int(input())
odd = []
even = []
for i, j in enumerate(map(int, input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(), reverse=True, key=lambda x: x[1])
even = sorted(collections.Counter(even).items(), reverse=True, key=lambda x: x[1])

if len(odd) == 1 and len(even) == 1 and odd[0][0] == even[0][0]:
    print(n // 2)
elif odd[0][0] == even[0][0]:
    if odd[0][1] == even[0][1]:
        for i in range(2):
            if odd[i][1] == even[i][1]:
                continue
            elif odd[i][1] > even[i][1]:
                print((n // 2 - odd[1][1]) + (n // 2 - even[0][1]))
                break
            elif odd[i][1] < even[i][1]:
                print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
                break
        else:
            print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
else:
    print((n // 2 - odd[0][1]) + (n // 2 - even[0][1]))

Buggy submission (verdict: Runtime Error):
import collections


n = int(input())
odd = []
even = []
for i,j in enumerate(map(int,input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(),key=lambda x:(x[1]),reverse=True)
even = sorted(collections.Counter(even).items(),key=lambda x:(x[1]),reverse=True)

if len(odd) == 1 and len(even) == 1:
    if odd[0][0] == even[0][0]:
        print(n//2)
    else:
        print(0)
elif len(odd) == 1 or len(even) == 1:
    if len(odd) == 1:
        print((n//2 - odd[1][1]) + (n//2 - even[1][1]))
    elif len(even) == 1:
        print((n//2 - odd[1][1]) + (n//2 - even[0][1]))
else:
    if odd[0][1] + even[1][1] > odd[1][1] + even[0][1]:
        print((n//2 - odd[0][1]) + (n//2 - even[1][1]))
    else:
        print((n//2 - odd[1][1]) + (n//2 - even[0][1]))

Buggy submission (verdict: Runtime Error):
import collections


n = int(input())
odd = []
even = []
for i,j in enumerate(map(int,input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(),key=lambda x:(x[1]),reverse=True)
even = sorted(collections.Counter(even).items(),key=lambda x:(x[1]),reverse=True)

if len(odd) == 1 and len(even) == 1:
    if odd[0][0] == even[0][0]:
        print(n//2)
    else:
        print(0)
elif len(odd) == 1 or len(even) == 1:
    if len(odd) == 1:
        print((n//2 - odd[0][1]) + (n//2 - even[1][1]))
    elif len(even) == 1:
        print((n//2 - odd[1][1]) + (n//2 - even[1][1]))
else:
    if odd[0][1] + even[1][1] > odd[1][1] + even[0][1]:
        print((n//2 - odd[0][1]) + (n//2 - even[1][1]))
    else:
        print((n//2 - odd[1][1]) + (n//2 - even[0][1]))

Buggy submission (verdict: Runtime Error):
import collections

n = int(input())
odd = []
even = []
for i, j in enumerate(map(int, input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(), reverse=True, key=lambda x: x[1])
even = sorted(collections.Counter(even).items(), reverse=True, key=lambda x: x[1])

if len(odd) == 1 and len(even) == 1 and odd[0][0] == even[0][0]:
    print(n // 2)
elif odd[0][0] == even[0][0]:
    if odd[0][1] == even[0][1]:
        for i in range(n//2):
            if odd[i][1] == even[i][1]:
                continue
            elif odd[i][1] > even[i][1]:
                print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
            elif odd[i][1] < even[i][1]:
                print((n // 2 - odd[1][1]) + (n // 2 - even[0][1]))
        else:
            print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
    elif odd[0][1] > even[0][1]:
        print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
    elif odd[0][1] < even[0][1]:
        print((n // 2 - odd[1][1]) + (n // 2 - even[0][1]))
else:
    print((n // 2 - odd[0][1]) + (n // 2 - even[0][1]))

Buggy submission (verdict: Runtime Error):
import collections

n = int(input())
odd = []
even = []
for i, j in enumerate(map(int, input().split())):
    if i % 2 == 0:
        even += [j]
    else:
        odd += [j]
odd = sorted(collections.Counter(odd).items(), reverse=True, key=lambda x: x[1])
even = sorted(collections.Counter(even).items(), reverse=True, key=lambda x: x[1])

if len(odd) == 1 and len(even) == 1 and odd[0][0] == even[0][0]:
    print(n // 2)
elif odd[0][0] == even[0][0]:
    if odd[0][1] == even[0][1]:
        for i in range(n//2):
            if odd[i][1] == even[i][1]:
                continue
            elif odd[i][1] > even[i][1]:
                print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
            elif odd[i][1] < even[i][1]:
                print((n // 2 - odd[1][1]) + (n // 2 - even[0][1]))
        else:
            print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
    elif odd[0][1] > even[0][1]:
        print((n // 2 - odd[0][1]) + (n // 2 - even[1][1]))
    elif odd[0][1] < even[0][1]
        print((n // 2 - odd[1][1]) + (n // 2 - even[0][1]))
else:
    print((n // 2 - odd[0][1]) + (n // 2 - even[0][1]))