Maximum And Queries (easy version) (Codeforces 1903D1)
Tags: Greedy, bitmasks

This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$ and $$$q$$$, the memory and time limits. You can make hacks only if all versions of the problem are solved.Theofanis really likes to play with the bits of numbers. He has an array $$$a$$$ of size $$$n$$$ and an integer $$$k$$$. He can make at most $$$k$$$ operations in the array. In each operation, he picks a single element and increases it by $$$1$$$.He found the maximum bitwise AND that array $$$a$$$ can have after at most $$$k$$$ operations.Theofanis has put a lot of work into finding this value and was very happy with his result. Unfortunately, Adaś, being the evil person that he is, decided to bully him by repeatedly changing the value of $$$k$$$.Help Theofanis by calculating the maximum possible bitwise AND for $$$q$$$ different values of $$$k$$$. Note that queries are independent.

Input: The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 10^5$$$ and $$$n \cdot q \le 10^5$$$) — the size of the array and the number of queries.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^6$$$) — the elements of the array.Next $$$q$$$ lines describe the queries. The $$$i$$$-th line contains one integer $$$k_i$$$ ($$$0 \le k_i \le 10^{18}$$$) — the number of operations that can be done in the $$$i$$$-th query.

Output: For each query, print one integer — the maximum bitwise AND that array $$$a$$$ can have after at most $$$k_i$$$ operations.

Note: In the first test case, in the first query, we add $$$1$$$ in the first and last elements of the array. Thus, the array becomes $$$[2,3,7,6]$$$ with bitwise AND equal to $$$2$$$.In the second test case, in the first query, we add $$$1$$$ in the first element, $$$5$$$ in the second, and $$$3$$$ in the third and now all the elements are equal to $$$5$$$.

Sample Inputs:
4 2
1 3 7 5
2
10

3 5
4 0 2
9
8
17
1
3

1 2
10
5
2318381298321

Sample Outputs:
2
6

5
4
7
0
1

15
2318381298331

Solution (Python):
import sys, math
input = sys.stdin.readline

def f(k, a):
    t = 2 ** 60
    a = [x for x in a]
    ans = 0
    while t:
        b = [x for x in a]
        temp = k
        for i in range(len(a)):
            if a[i] >= t:
                a[i] = a[i] % t
            else:
                k -= t - a[i]
                a[i] = 0
            if k < 0:
                a = [x % t for x in b]
                k = temp
                break
        else:
            ans += t
        t //= 2
    return ans
for _ in range(1):
    (n, q) = map(int, input().split())
    a = list(map(int, input().split()))
    for i in range(q):
        x = int(input())
        print(f(x, a))

Solution (Python):
import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq
import random
from io import BytesIO, IOBase
import os
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
INF = 10 ** 18
(N, Q) = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(Q):
    K = int(input())
    B = list(A)
    for d in reversed(range(65)):
        cost = 0
        for i in range(N):
            if B[i] & 1 << d == 0:
                cost += (1 << d) - (B[i] & (1 << d) - 1)
                if cost > K:
                    break
        if cost <= K:
            K -= cost
            for i in range(N):
                if B[i] & 1 << d == 0:
                    B[i] += (1 << d) - (B[i] & (1 << d) - 1)
    ans = B[0]
    for b in B:
        ans &= b
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
(n, q) = map(int, input().split())
o = list(map(int, input().split()))
rr = 0
for ff in range(65):
    rr += 2 ** ff
for i in range(q):
    k = int(input())
    l = o.copy()
    (p, bit, ans) = ([], 64, 0)
    hh = rr
    while bit > -1:
        p = []
        (num, flag) = (2 ** bit, 1)
        gg = k
        hh -= num
        for ele in l:
            if ele & num:
                p.append(ele)
            else:
                som = hh & ele
                som = num - som
                if gg < som:
                    flag = 0
                    break
                else:
                    gg -= som
                    p.append(ele + som)
        if flag:
            ans += 2 ** bit
            k = gg
            l = p
        bit -= 1
    sys.stdout.write(str(ans) + '\n')