Buggy python submission (verdict: Time Limit Exceeded):
import copy
 
N,Q = map(int,input().split())
A = list(map(int,input().split()))
X = [0]*Q
 
for i in range(Q):
  X[i] = int(input())
  #初期値
  j= -1
  Takahashi = 0
  Aoki = 0
  B = copy.deepcopy(A)
  flag =0
  
  for k in range(N):
    if flag == 0: #高橋くんの行動
      Takahashi += B[-1]
      del B[-1]
      flag = 1
    
    elif flag ==1: #青木くんの行動
      
      #j-1番目のカード値B[j-1] <= 青木くんが決めた整数X[i] < j番目のカード値B[j]
      #となるようにjの値を決める
      #ただしX[i]がいずれのBの要素よりも大きい場合はi=N-k-1とする
      if j == -1:
        for l in range(N-k):
          if X[i] < B[l]:
            j = l 
            break
        if j == -1:
          j = N-k-1
          
      if len(B) == j:
         j = len(B)-1

          
      #j=0（青木くんが決めた整数は残りカードのいずれよりも小さい）の場合、B[0]を取る
      if j == 0:
        Aoki +=B[0]
        del B[0]
        flag =0
      #j=0（青木くんが決めた整数は残りカードのいずれよりも小さい）の場合、B[0]を取る
      #j番目のカードB[j]を取る     
      elif abs(X[i]-B[j]) < abs(X[i]-B[j-1]):
        Aoki += B[j]
        del B[j]
        flag =0
        if len(B) == j:
           j=j-1
      #j番目のカードB[j-1]を取る   
      else:
        Aoki += B[j-1]
        del B[j-1]
        flag = 0
        j = j-1
  print(Takahashi)

Fixed submission:
n,q = map(int,input().split())
a = list(map(int,input().split()))
query = [int(input()) for i in range(q)]

a = sorted(a)
ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]
    
ruiseki2 = [0]*(n+1)
if n % 2 == 1:
    for i in range(n):
        if i % 2 == 0:
            ruiseki2[i+1] = ruiseki2[i] + a[i]
        else:
            ruiseki2[i+1] = ruiseki2[i]

if n % 2 == 0:
    for i in range(n):
        if i % 2 == 1:
            ruiseki2[i+1] = ruiseki2[i] + a[i]
        else:
            ruiseki2[i+1] = ruiseki2[i]

def solve(mid, num):
    r = -((-mid) // 2)
    l = mid
    r_pos = n - r
    l_pos = n - l
    if abs(a[l_pos] - num) <= abs(a[r_pos] - num):
        return True
    else:
        return False

for i in range(q):
    num = query[i]
    ok = 1
    ng = n + 1 
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid, num):
            ok = mid
        else:
            ng = mid
    ans = 0
    ans += ruiseki[n] - ruiseki[n-(ok+1)//2]
    nokori = n - ok
    ans += ruiseki2[nokori]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

N,Q = map(int,input().split())
A = list(map(int,input().split()))
X = [0]*Q

for i in range(Q):
  X[i] = int(input())
  #初期値
  Takahashi = 0
  Aoki = 0
  B = copy.deepcopy(A)
  flag =0
  
  for k in range(N):
    if flag == 0: #高橋くんの行動
      Takahashi += B[-1]
      del B[-1]
      flag = 1
    
    elif flag ==1: #青木くんの行動
      j=-1
      for l in range(N-k):
        if X[i] < B[l]:
          j = l 
          break
      if j==-1:
        j = N-k-1
        
      if j == 0:
        Aoki +=B[0]
        del B[0]
        flag =0
      elif abs(X[i]-B[j]) < abs(X[i]-B[j-1]):
        Aoki += B[j]
        del B[j]
        flag =0
      else:
        Aoki += B[j-1]
        del B[j-1]
        flag = 0
  print(Takahashi)

Fixed submission:
n,q = map(int,input().split())
a = list(map(int,input().split()))
query = [int(input()) for i in range(q)]

a = sorted(a)
ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]
    
ruiseki2 = [0]*(n+1)
if n % 2 == 1:
    for i in range(n):
        if i % 2 == 0:
            ruiseki2[i+1] = ruiseki2[i] + a[i]
        else:
            ruiseki2[i+1] = ruiseki2[i]

if n % 2 == 0:
    for i in range(n):
        if i % 2 == 1:
            ruiseki2[i+1] = ruiseki2[i] + a[i]
        else:
            ruiseki2[i+1] = ruiseki2[i]

def solve(mid, num):
    r = -((-mid) // 2)
    l = mid
    r_pos = n - r
    l_pos = n - l
    if abs(a[l_pos] - num) <= abs(a[r_pos] - num):
        return True
    else:
        return False

for i in range(q):
    num = query[i]
    ok = 1
    ng = n + 1 
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if solve(mid, num):
            ok = mid
        else:
            ng = mid
    ans = 0
    ans += ruiseki[n] - ruiseki[n-(ok+1)//2]
    nokori = n - ok
    ans += ruiseki2[nokori]
    print(ans)