Buggy python submission (verdict: Time Limit Exceeded):
t = 0
SENTINEL = 1e+9


def sort(a, b):
    global t
    a.append(SENTINEL)
    b.append(SENTINEL)
    i, j = 0, 0
    while i < len(a)-1 or j < len(b)-1:
        t += 1
        if a[i] < b[j]:
            yield a[i]
            i += 1
        else:
            yield b[j]
            j += 1


def merge(a, left, mid, right):
    left_list, right_list = a[left:mid], a[mid:right]
    for index, value in zip(range(left, right), sort(left_list, right_list)):
        a[index] = value


def merge_sort(a, left, right):
    if right-left > 1:
        mid = (left + right) // 2
        merge_sort(a, left, mid)
        merge_sort(a, mid, right)
        merge(a, left, mid, right)


def main():
    global t
    input()
    a = [int(i) for i in input().split()]
    merge_sort(a, 0, len(a))
    print(*a)
    print(t)


if __name__ == '__main__':
    main()



Fixed submission:
SENTINEL = 1e+9
t = 0


def Merge(A, left, mid, right):
    global t
    n1 = mid - left
    n2 = right - mid
    L, R = A[left:left+n1], A[mid:mid+n2]
    L.append(SENTINEL)
    R.append(SENTINEL)
    i, j = 0, 0
    for k in range(left, right):
        t += 1
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


def Merge_Sort(A, left, right):
    if left+1 < right:
        mid = (left + right) // 2
        Merge_Sort(A, left, mid)
        Merge_Sort(A, mid, right)
        Merge(A, left, mid, right)


def main():
    global t
    input()
    A = [int(i) for i in input().split()]
    Merge_Sort(A, 0, len(A))
    print(*A)
    print(t)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
t = 0
SENTINEL = 1e+9


def sort(a, b):
    global t
    a.append(SENTINEL)
    b.append(SENTINEL)
    i, j = 0, 0
    while i < len(a)-1 or j < len(b)-1:
        t += 1
        if a[i] < b[j]:
            yield a[i]
            i += 1
        else:
            yield b[j]
            j += 1


def merge(a, left, mid, right):
    left_list, right_list = a[left:mid], a[mid:right]
    for index, value in zip(range(left, right), sort(left_list, right_list)):
        a[index] = value


def merge_sort(a, left, right):
    global t
    if right-left == 1:
        return
    elif right-left == 2:
        t += 2
        a[left], a[right-1] = min(a[left], a[right-1]), max(a[left], a[right-1])
    else:
        mid = (left + right) // 2
        merge_sort(a, left, mid)
        merge_sort(a, mid, right)
        merge(a, left, mid, right)


def main():
    global t
    input()
    a = [int(i) for i in input().split()]
    merge_sort(a, 0, len(a))
    print(*a)
    print(t)


if __name__ == '__main__':
    main()



Fixed submission:
SENTINEL = 1e+9
t = 0


def Merge(A, left, mid, right):
    global t
    n1 = mid - left
    n2 = right - mid
    L, R = A[left:left+n1], A[mid:mid+n2]
    L.append(SENTINEL)
    R.append(SENTINEL)
    i, j = 0, 0
    for k in range(left, right):
        t += 1
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1


def Merge_Sort(A, left, right):
    if left+1 < right:
        mid = (left + right) // 2
        Merge_Sort(A, left, mid)
        Merge_Sort(A, mid, right)
        Merge(A, left, mid, right)


def main():
    global t
    input()
    A = [int(i) for i in input().split()]
    Merge_Sort(A, 0, len(A))
    print(*A)
    print(t)


if __name__ == '__main__':
    main()