Problem p03138 (python)

Fixed submission:
import numpy as np
import math

_, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

logk = 50
half = len(a) / 2

base = np.array([2**i for i in range(logk + 1)])

a = np.array(a)

ones = a[:,None] & base > 0
zeros = (ones != True)

# print(base[:5])
# print(ones)
# print(zeros)

no = np.sum(ones, axis=0)
nz = np.sum(zeros, axis=0)

ov = no * base
zv = nz * base

# print(no)
# print(nz)

inc = ov < zv

inceq = ov == zv
# vi = np.sum(v * base)


# def largest(fx, kk, i):
#     if i == len(base):
#         return fx

#     kone = kk + base[i]
#     if kone > k:
#         return fx

#     kzero = kk
#     fzero = fx + ov[i]
#     fone = fx + zv[i]
#     return max(largest(fzero, kzero, i + 1), largest(fone, kone, i + 1))

# print(largest(0, 0, 0))

kk = [0]
fx = [0]
for i, b in reversed(list(enumerate(base))):
    for j in range(len(fx)):
        added = kk[j] + b
        if (inc[i] or inceq[i]) and added <= k:
            if inceq[i]:
                fx.append(fx[j] + ov[i])
                kk.append(kk[j])
            fx[j] += zv[i]
            kk[j] += b

        else:
            fx[j] += ov[i]

print(max(fx))

Buggy submission (verdict: Runtime Error):
import numpy as np
import math

_, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

logk = 50
half = len(a) / 2

base = np.array([2**i for i in range(logk + 1)])

a = np.array(a)

ones = a[:,None] & base > 0
zeros = (ones != True)

# print(base[:5])
# print(ones)
# print(zeros)

no = np.sum(ones, axis=0)
nz = np.sum(zeros, axis=0)

ov = no * base
zv = nz * base

# print(no)
# print(nz)

inc = no < half
# vi = np.sum(v * base)


def largest(fx, kk, i):
    if i == len(base):
        return fx

    kone = kk + base[i]
    if kone > k:
        return fx

    kzero = kk
    fzero = fx + ov[i]
    fone = fx + zv[i]
    return max(largest(fzero, kzero, i + 1), largest(fone, kone, i + 1))

print(largest(0, 0, 0))


# print(np.sum(a ^ vi))

Buggy submission (verdict: Runtime Error):

import numpy as np
import math

n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

if k == 0:
    print(0)
    quit()

logk = math.ceil(math.log2(k))
half = n / 2

base = np.array([2**i for i in range(logk)])

a = np.array(a)

ones = a[:,None] & base > 0
# zeros = ones != True

no = np.sum(ones, axis=0)
# nz = np.sum(zeros, axis=0)

# print(no)
# print(nz)

v = no < half
vi = np.sum(v * base)

index = 0
while vi > k:
    flip = not (index & base)
    tmp = v.copy()
    tmp &= flip
    index += 1
    vi = np.sum(tmp * base)

# print(vi)

print(np.sum(a ^ vi))

Buggy submission (verdict: Runtime Error):
import numpy as np
import math

n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

if k == 0:
    print(0)
    quit()

logk = math.ceil(math.log2(k))
half = n / 2

base = np.array([2**i for i in range(logk)])

a = np.array(a)

ones = a[:,None] & base > 0
# zeros = ones != True

no = np.sum(ones, axis=0)
# nz = np.sum(zeros, axis=0)

# print(no)
# print(nz)

v = no < half
vi = np.sum(v * base)

index = 0
while vi > k:
    v[index] != v[index]
    index += 1
    vi = np.sum(v * base)

# print(vi)

print(np.sum(a ^ vi))