Problem p03244 (python)

Fixed submission:
n = int(input())
v = list(map(int,input().split()))
dict1 = {}
dict2 = {}

for i,key in enumerate(v):
    if i%2 == 0:
        if key not in dict1:
            dict1[key] = 1
        else:
            dict1[key] += 1
    else:
        if key not in dict2:
            dict2[key] = 1
        else:
            dict2[key] += 1

max1 = 0
max2 = 0
if len(dict1) == 1 and len(dict2) == 1:
    if v[0] == v[1]:
        ans = n//2
    else:
        ans = 0
elif len(dict1) == 1:
    dict2[v[0]] = 0
    for key in dict2:
        max2 = max(max2,dict2[key])
    else:
        ans = n//2 - max2
elif len(dict2) == 1:
        dict2[v[1]] = 0
        for key in dict1:
            max1 = max(max1,dict1[key])
        else:
            ans = n//2 - max1
else:
    second1 = 0
    second2 = 0
    maxkey1 = 0
    maxkey2 = 0
    secondkey1 = 0
    secondkey2 = 0
    for key in dict1:
        if dict1[key] > max1:
            max1,second1, = dict1[key],max1
            maxkey1,secondkey1 = key,maxkey1
        elif dict1[key] > second1:
            second1 = dict1[key]
            secondkey1 = key
    for key in dict2:
        if dict2[key] > max2:
            max2,second2, = dict2[key],max2
            maxkey2,secondkey2 = key,maxkey2
        elif dict2[key] > second2:
            second2 = dict2[key]
            secondkey2 = key
    if maxkey1 == maxkey2:
        if dict1[secondkey1] < dict2[secondkey2]:
            ans = n - dict1[maxkey1] - dict2[secondkey2]
        else:
            ans = n - dict2[maxkey2] - dict1[secondkey1]
    else:
        ans = n - dict1[maxkey1] - dict2[maxkey2]

print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
v = list(map(int,input().split()))
dict1 = {}
dict2 = {}

for i,key in enumerate(v):
    if i%2 == 0:
        if key not in dict1:
            dict1[key] = 1
        else:
            dict1[key] += 1
    else:
        if key not in dict2:
            dict2[key] = 1
        else:
            dict2[key] += 1

max1 = 0
max2 = 0
if len(dict1) == 1 and len(dict2) == 1:
    if v[0] == v[1]:
        ans = n//2
    else:
        ans = 0
elif len(dict1) == 1:
    dict2[v[0]] = 0
    for key in dict2:
        max2 = max(max2,dict2[key])
    else:
        ans = n//2 - max2
elif len(dict2) == 1:
        dict2[v[1]] = 0
        for key in dict1:
            max1 = max(max1,dict1[key])
        else:
            ans = n//2 - max1
else:
    second1 = 0
    second2 = 0
    maxkey1 = 0
    maxkey2 = 0
    secondkey1 = 0
    secondkey2 = 0
    for key in dict1:
        if dict1[key] > max1:
            max1,second1, = dict1[key],max1
            maxkey1,secondkey1 = key,maxkey1
        elif dict1[key] > second1:
            second1 = dict1[key]
    for key in dict2:
        if dict2[key] > max2:
            max2,second2, = dict2[key],max2
            maxkey2,secondkey2 = key,maxkey2
        elif dict2[key] > second2:
            second2 = dict2[key]
    if maxkey1 == maxkey2:
        if dict1[secondkey1] < dict2[secondkey2]:
            ans = n - dict1[maxkey1] - dict2[secondkey2]
        else:
            ans = n - dict2[maxkey2] - dict1[secondkey1]
    else:
        ans = n - dict1[maxkey1] - dict2[maxkey2]

print(ans)