Problem p03138 (python)

Fixed submission:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def bit1l(i):
    assert i > 0
    return len(bin(i)) - 3


def count1(i, xs):
    return sum(bool(i & x) for x in xs)


def main():
    n, k = get_int_list()
    xs = get_int_list()

    if k == 0:
        print(sum(xs))
        return
    gains = []
    for i in range(bit1l(k) + 1):
        gains.append((i, (len(xs) - count1(1 << i, xs) * 2) * (1 << i)))
    gains.sort(key=lambda x: x[1], reverse=True)
    best = 0
    for i, gain in gains:
        if gain < 0:
            break
        if k < best + (1 << i):
            continue
        best += (1 << i)
    print(sum((x ^ best) for x in xs))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor, log2

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def bit1l(i):
    assert i > 0
    return len(bin(i)) - 3


def count1(i, xs):
    return sum(bool(i & x) for x in xs)


def main():
    n, k = get_int_list()
    xs = get_int_list()

    if k == 0:
        print(sum(xs))
        return
    gains = []
    for i in range(bit1l(k) + 1):
        gains.append((i, (len(xs) - count1(1 << i, xs) * 2) * (1 << i)))
    gains.sort(key=lambda x: x[1], reverse=True)
    best = 0
    for i, gain in gains:
        if gain < 0:
            break
        if k < best + (1 << i):
            break
        best += (1 << i)
    print(sum((x ^ best) for x in xs))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor, log2

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def main():
    n, k = get_int_list()
    xs = get_int_list()

    if k == 0:
        print(sum(xs))
        return
    best = 0
    for i in range(int(ceil(log2(k)))):
        y = 1 << i
        diff = sum((x ^ y) - x for x in xs)
        if diff > 0:
            best += y
    if best > k:
        raise Exception('Expected error')
    print(sum((x ^ best) for x in xs))


if __name__ == '__main__':
    main()