Buggy python submission (verdict: Time Limit Exceeded):
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))
for i in range(Q):
    m = -1
    spam = int(input())
    for n in C:
        ham =n%spam 
        m =  max(m,ham) 
    print(m)

Fixed submission:
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))

M = max(C)+1
t1 = [0]*(M)
for v in C:
    t1[v] = 1

t2 = [0]*(M)
v = 0
for i in range(M):
    if t1[i]:
        v = i
    t2[i] = v

for i in range(Q):
    t = int(input())
    n_v = M-1
    m = 0
    while n_v != 0:
        amr = n_v % t
        m = max(m, amr) 
        tmp = n_v-m-1
        if tmp <= 0: break
        n_v = t2[tmp]

    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
def next_max(lst,mi,v):
    l, r = 0, mi-1
    while l <= r:
        mid = int((l+r)/2)
        if lst[mid] == v: return mid -1
        if lst[mid] > v:
            r = mid - 1
        else:
            l = mid + 1
    return r


N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))
C.sort()
for i in range(Q):
    t = int(input())
    idx = N-1
    m = C[-1]% t
    idx = next_max(C,idx,C[idx]-m)
    while idx >= 0:
        amr = C[idx]%t
        m = max(m,amr) 
        idx = next_max(C,idx,C[idx]-amr)
    print(m)

Fixed submission:
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))

M = max(C)+1
t1 = [0]*(M)
for v in C:
    t1[v] = 1

t2 = [0]*(M)
v = 0
for i in range(M):
    if t1[i]:
        v = i
    t2[i] = v

for i in range(Q):
    t = int(input())
    n_v = M-1
    m = 0
    while n_v != 0:
        amr = n_v % t
        m = max(m, amr) 
        tmp = n_v-m-1
        if tmp <= 0: break
        n_v = t2[tmp]

    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))
C.sort()

def next_max(mi,v):
    l, r = 0, mi-1
    while l <= r:
        mid = int((l+r)/2)
        if C[mid] == v: return mid - 1
        if C[mid] > v:
            r = mid - 1
        else:
            l = mid + 1
    return r

for i in range(Q):
    t = int(input())
    idx = N-1
    m = C[-1]% t
    idx = next_max(idx,C[idx]-m)
    while idx >= 0:
        amr = C[idx]%t
        m = max(m,amr) 
        idx = next_max(idx,C[idx]-amr)
    print(m)

Fixed submission:
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))

M = max(C)+1
t1 = [0]*(M)
for v in C:
    t1[v] = 1

t2 = [0]*(M)
v = 0
for i in range(M):
    if t1[i]:
        v = i
    t2[i] = v

for i in range(Q):
    t = int(input())
    n_v = M-1
    m = 0
    while n_v != 0:
        amr = n_v % t
        m = max(m, amr) 
        tmp = n_v-m-1
        if tmp <= 0: break
        n_v = t2[tmp]

    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))

M = max(C)+1
t1 = [0]*(M)
for v in C:
    t1[v] = 1

t2 = [0]*(M)
v = 0
for i in range(M):
    if t1[i]:
        v = i
    t2[i] = v

for i in range(Q):
    t = int(input())
    n_v = M-1
    m = 0
    while n_v != 0:
        amr = n_v % t
        m = max(m,amr) 
        n_v = t2[n_v-1]

    print(m)

Fixed submission:
N,Q = list(map(int,input().split()))
C = list(map(int,input().split()))

M = max(C)+1
t1 = [0]*(M)
for v in C:
    t1[v] = 1

t2 = [0]*(M)
v = 0
for i in range(M):
    if t1[i]:
        v = i
    t2[i] = v

for i in range(Q):
    t = int(input())
    n_v = M-1
    m = 0
    while n_v != 0:
        amr = n_v % t
        m = max(m, amr) 
        tmp = n_v-m-1
        if tmp <= 0: break
        n_v = t2[tmp]

    print(m)