Problem p03244 (python)

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    S = list(map(int, input().split()))
    odd = {}
    even = {}
    for i in range(N):
        if i % 2 == 0:
            if S[i] not in even:
                even[S[i]] = 1
            else:
                even[S[i]] += 1
        else:
            if S[i] not in odd:
                odd[S[i]] = 1
            else:
                odd[S[i]] += 1

    n = 1000000000
    emk = None
    ev1 = None
    ev2 = 0
    for ek, ev in even.items():
        if emk is None:
            emk = ek
            ev1 = ev
        elif ev > ev1:
            emk = ek
            ev2 = ev1
            ev1 = ev
        elif ev > ev2:
            ev2 = ev

    omk = None
    ov1 = None
    ov2 = 0
    for ok, ov in odd.items():
        if omk is None:
            omk = ok
            ov1 = ov
        elif ov > ov1:
            omk = ok
            ov2 = ov1
            ov1 = ov
        elif ov > ov2:
            ov2 = ov

    if emk != omk:
        n = min(n, N - ev1 - ov1)
    else:
        n = min(n, N - ev1 - ov2, N - ev2 - ov1)

    print(n)

main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    S = list(map(int, input().split()))
    odd = {}
    even = {}
    for i in range(N):
        if i % 2 == 0:
            if S[i] not in even:
                even[S[i]] = 1
            else:
                even[S[i]] += 1
        else:
            if S[i] not in odd:
                odd[S[i]] = 1
            else:
                odd[S[i]] += 1

    n = 1000000000
    even_max1 = None
    even_max2 = None
    for ek, ev in even.items():
        if even_max1 is None:
            even_max1 = ek
        elif even_max2 is None:
            even_max2 = ek
        elif ev > even[even_max1]:
            even_max1 = ek
        elif ev > even[even_max2]:
            even_max2 = ek

    odd_max1 = None
    odd_max2 = None
    for ok, ov in odd.items():
        if odd_max1 is None:
            odd_max1 = ok
        elif odd_max2 is None:
            odd_max2 = ok
        elif ov > odd[odd_max1]:
            odd_max1 = ok
        elif ov > odd[odd_max2]:
            odd_max2 = ok

    n = min(n, N - even[even_max1] - odd[odd_max1] if even_max1 != odd_max1 else
            N - max(even[even_max1], odd[odd_max1]))
    if even_max2 is not None:
        n = min(n, N - even[even_max1] - odd[odd_max2] if even_max1 != odd_max2 else
                N - max(even[even_max1], odd[odd_max2]))
        n = min(n, N - even[even_max2] - odd[odd_max1] if even_max2 != odd_max1 else
                N - max(even[even_max2], odd[odd_max1]))
        n = min(n, N - even[even_max2] - odd[odd_max2] if even_max2 != odd_max2 else
                N - max(even[even_max2], odd[odd_max2]))

    print(n)

main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    S = list(map(int, input().split()))
    odd = {}
    even = {}
    for i in range(N):
        if i % 2 == 0:
            if S[i] not in even:
                even[S[i]] = 1
            else:
                even[S[i]] += 1
        else:
            if S[i] not in odd:
                odd[S[i]] = 1
            else:
                odd[S[i]] += 1

    n = 1000000000
    even_max1 = None
    even_max2 = None
    for ek, ev in even.items():
        if even_max1 is None:
            even_max1 = ek
        elif even_max2 is None:
            even_max2 = ek
        elif ev > even[even_max1]:
            even_max1 = ek
        elif ev > even[even_max2]:
            even_max2 = ek

    odd_max1 = None
    odd_max2 = None
    for ok, ov in odd.items():
        if odd_max1 is None:
            odd_max1 = ok
        elif odd_max2 is None:
            odd_max2 = ok
        elif ov > odd[odd_max1]:
            odd_max1 = ok
        elif ov > odd[odd_max2]:
            odd_max2 = ov

    n = min(n, N - even[even_max1] - odd[odd_max1] if even_max1 != odd_max1 else
            N - max(even[even_max1], odd[odd_max1]))
    if even_max2 is not None:
        n = min(n, N - even[even_max1] - odd[odd_max2] if even_max1 != odd_max2 else
                N - max(even[even_max1], odd[odd_max2]))
        n = min(n, N - even[even_max2] - odd[odd_max1] if even_max2 != odd_max1 else
                N - max(even[even_max2], odd[odd_max1]))
        n = min(n, N - even[even_max2] - odd[odd_max2] if even_max2 != odd_max2 else
                N - max(even[even_max2], odd[odd_max2]))

    print(n)

main()