Problem:


Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60



Sample input:
5 5
3 5 7 11 13
1
4
9
10
13


Sample output:
31
31
27
23
23

Submission status: Runtime Error

Solution code:
def nibun(v, k):
  n = len(k)
  if n == 2:
    return 1
  elif v < k[n//2]:
    return nibun(v, k[:n//2+1])
  else:
    return n//2 + nibun(v, k[n//2:])

n, q = map(int, input().split())
a = list(map(int, input().split()))

s = [0] * (n //2 + n%2)
x = [0] * (n // 2 + n % 2 - 1)
s[0] = sum(a[n // 2:])
if n % 2 == 0:
  for i in range(1, len(s)):
    s[i] = s[i-1] + a[2 * i -1] - a[n // 2 + i - 1]
    x[i-1] = (a[2*i-1] + a[n//2+i-1]) // 2
else:
  for i in range(1, len(s)):
    s[i] = s[i-1] - a[n//2+i-1] + a[2*(i-1)]
    x[i-1] = (a[2*(i-1)]+a[n//2+i-1]) // 2

for j in range(q):
  v = int(input())
  if v > x[-1]:
    print(s[-1])
  elif v <= x[0]:
    print(s[0])
  else:
    i = nibun(v, x)
    print(s[i])

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())
a = list(map(int,input().split()))
x =[]
for n in range(q):
    x.append(int(input()))

for i in range(q):
    ans = 0
    atemp = a.copy()
    for j in range(n+1):
        if (j % 2) == 0:
            ans += max(atemp)
            atemp.remove(max(atemp))
        else:
            atemprmv = max(atemp)
            deltmin =abs(max(atemp)-x[i])
            for k in range(len(atemp)):
                deltemp = abs(atemp[k]-x[i])
                if deltemp <= deltmin:
                    atemprmv = min(atemprmv,atemp[k])
            atemp.remove(atemprmv)
    print(int(ans))

Submission status: Runtime Error

Solution code:
import bisect
n,q=map(int,input().split())
a=list(map(int,input().split()))
x=[0]*q
ans=[0]*q
for i in range(q):
    x[i]=int(input())

aa=0
k=a[n//2:]
aa=sum(k)
    
bb=0
c=list(a)
while len(c)>0:
    bb+=c[len(c)-1]
    c.pop()
    if len(c)>0:
        c.pop():




for i in range(q):
    b=list(a)
    xx=x[i]
    sum=0
    ll=bisect.bisect_left(b, xx)-1
    if ll<=len(b)//4:
        ans[i]=aa
    elif ll>=len(b)//2+len(b)//4:
        ans[i]=bb
    else:
        while len(b)>0:
            sum+=b[len(b)-1]
            b.pop()
            if len(b)>0:
                ll=bisect.bisect_left(b, xx)-1
                rr=ll+1
                if ll>=len(b)-1:
                    b.pop()
                elif abs(b[rr]-xx)<abs(b[ll]-xx):
                    b.pop(rr)    
                else:
                    b.pop(ll)
        ans[i]=sum
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import bisect
n,q=map(int,input().split())
a=list(map(int,input().split()))
x=[0]*q
ans=[0]*q
for i in range(q):
    x[i]=int(input())

aa=0
k=a[n//2:]
aa=sum(k)
    
bb=0
c=list(a)
while len(c)>0
    bb+=c[len(c)-1]
    c.pop()
    if len(c)>0:
        c.pop():




for i in range(q):
    b=list(a)
    xx=x[i]
    sum=0
    ll=bisect.bisect_left(b, xx)-1
    if ll<=len(b)//4:
        ans[i]=aa
    elif ll>=len(b)//2+len(b)//4:
        ans[i]=bb
    else:
        while len(b)>0:
            sum+=b[len(b)-1]
            b.pop()
            if len(b)>0:
                ll=bisect.bisect_left(b, xx)-1
                rr=ll+1
                if ll>=len(b)-1:
                    b.pop()
                elif abs(b[rr]-xx)<abs(b[ll]-xx):
                    b.pop(rr)    
                else:
                    b.pop(ll)
        ans[i]=sum
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import copy

N, Q = map(int, input().split())
Alist = list(map(int, input().split()))

Xlist = []
sumT = 0
Acount = 0

for i in range(Q):
    Xtmp = int(input())
    Xlist.append(Xtmp)


for i in range(Q):
    Tlist = []
    A = copy.deepcopy(Alist)
    Adifflist = []


    if Xlist[i] <= A[0]:
        a = len(A)//2
        print(sum(A[a:]))

    elif A[-1] <= Xlist[i]:
        for s in range(len(A)):
            if s % 2 == 0:
                sumT += A[len(A)-1-s]
        print(sumT)

    
    else:
        for j in range(len(A)):
            Adifflist.append([abs(A[j]-Xlist[i]), A[j]])

        Adifflist.sort(reverse=False)
            
        while 1:
            Ttmp = A.pop(-1)
            Tlist.append(Ttmp)

            Adifflist.remove([abs(Ttmp-Xlist[i]), Ttmp])

            if A == []:
                print(sum(Tlist))
                break

            Atmp = Adifflist[Acount][1]
            Acount += 1
            A.remove(Atmp)

            if A == []:
                print(sum(Tlist))
                break

Submission status: Runtime Error

Solution code:
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

import sys
import bisect

N, Q = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
X = []
for _ in range(Q):
    X.append(int(input()))

A.sort()
A_COPY = A.copy()

T = []


def takahashi():
    a = A[-1]
    T.append(a)
    del A[-1]


def aoki(x):
    l = bisect.bisect_left(A, x)
    r = bisect.bisect_right(A, x)
    idx = l
    if len(A) > r and len(A) > l:
        if abs(A[l] - x) > abs(A[r] - x):
            idx = r
    if idx == len(A):
        idx -= 1
    del A[idx]


for x in X:
    A = A_COPY.copy()
    T = []
    while True:
        if len(A) == 0:
            break
        takahashi()
        if len(A) == 0:
            break
        aoki(x)
    print(sum(T))

Submission status: Runtime Error

Solution code:
import sys

import numpy as np


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


N, Q = il()
A_ = il()
X = imi(Q)
for x in X:
    takahashi = 0
    A = A_[:]
    while A:
        takahashi += A.pop()
        if A:
            aoki = [abs(a - x) for a in A]
            A.remove(A[np.argmin(aoki)])
    print(takahashi)

Submission status: Runtime Error

Solution code:
n,q = (int(i) for i in input().split())
a = list(int(i) for i in input().split())
x = []
for i in range(q):
    x.append(int(input()))

i = 0
res = list(0 for i in range(n))
count = 0
for i in range(n):
    b = sorted(a)
    for j in range(n):
        if j%2==0:
            res[i] += max(b)
            b.remove(max(b))
        else:
            b_sub = []
            for num in b:
                b_sub.append(abs(x[i]-num))
            b.pop(b_sub.index(min(b_sub)))
for r in res:
    print(r)

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
card_num = list(map(int,input().split()))
card.sort()
x_i = []
for i in range(q):
    x_i.append(int(input()))
for i in range(q):
    card = []
    for num in range(n):
        card.append(card_num[num])
    taka = []
    for j in range(n//2):
        temp = max(card)
        card.remove(temp)
        taka.append(temp)
        up = [x for x in card if x >= x_i[i]]
        down = [y for y in card if y < x_i[i]]
        if up == []:
            card.remove(down[-1])
        elif down == []:
            card.remove(up[0])
        elif up[0]-x_i[i]<x_i[i]-down[-1]:
            card.remove(up[0])
        else:
            card.remove(down[-1])
    if n%2==1:
        taka.append(card[0])
    print(sum(taka))

Submission status: Runtime Error

Solution code:
import copy

N, Q = map(int, input().split())
Alist = list(map(int, input().split()))

Xlist = []
sumT = 0

for i in range(Q):
    Xtmp = int(input())
    Xlist.append(Xtmp)




for i in range(len(Xlist)):
    Tlist = []
    A = copy.deepcopy(Alist)
    Adifflist = []


    if Xlist[i] <= A[0]:
        if len(A) % 2 == 0:
            print(sum(A[len(A)/2:]))
        else:
            print(sum(A[A//2:))
    
    elif A[-1] <= Xlist[i]:
        for i in range(len(A)):
            if i % 2 == 0:
                sumT += A[len(A)-1-i]
                print(sumT)
    
    else:
        for j in range(len(A)):
            Adifflist.append([abs(A[j]-Xlist[i]), A[j]])

        Adifflist.sort(reverse=False)
            
        while 1:
            Ttmp = A.pop(-1)
            Tlist.append(Ttmp)

            Adifflist.remove([abs(Ttmp-Xlist[i]), Ttmp])

            if A == []:
                print(sum(Tlist))
                break

            Atmp = Adifflist[0][1]
            del Adifflist[0]
            A.remove(Atmp)

            if A == []:
                print(sum(Tlist))
                break

Submission status: Runtime Error

Solution code:
import bisect
n,q=map(int,input().split())
a=list(map(int,input().split()))
x=[0]*q
for i in range(q):
    x[q]=int(input())

for i in range(q):
    b=list(a)
    xx=x[i]
    sum=0
    while len(b)>0:
        sum+=b[len(b)-1]
        b.pop()
        if len(b)>0:
            ll=bisect.bisect_left(b, xx)-1
            rr=ll+1
            if ll>=len(b)-1:
                b.pop()
            elif abs(b[rr]-xx)<abs(b[ll]-xx):
                b.pop(rr)    
            else:
                b.pop(ll)
    print(sum)

Submission status: Runtime Error

Solution code:
# D
N, Q = map(int, input().split())
A_list = list(map(int, input().split()))
q_list = [0]*Q
for i in range(Q):
    q_list[i] = int(input())

# precompute

# res
M = (N+1)//2
S1 = [0]*M
S2 = [0]*M

s1 = 0
s2 = 0
for i in range(M):
    s1 += A_list[N-1-i]
    S1[i] = s1
    s2 += A_list[N-1-2*i]
    S2[i] = s2

# bins
bins = [0]*(M-1)
for i in range(M-1):
    bins[i] = (A_list[N - (i+2)] + A_list[N - (2*i+3)]) / 2

# function for binary search
def bisearch(q):
    
    if q > bins[0]:
        return 0
    elif q <= bins[M-2]:
        return M - 1
    
    l = 0
    r = M - 2
    while l < r - 1:
        m = (l + r) // 2
        if q > bins[m]:
            r = m
        else:
            l = m
    return l+1
    
    

# first conflict
for q in q_list:
    r = bisearch(q)
    print(S1[r]+s2-S2[r])

Submission status: Runtime Error

Solution code:
import sys

n, q = list(map(int, input().split()))
aaa = list(map(int, input().split()))
xxx = sorted(((int(line), i) for i, line in enumerate(sys.stdin.readlines())), reverse=True)
total = sum(aaa)
tmp = sum(aaa[n // 2:])
ans = [sum(aaa[-1::-2])] * q
if n % 2 == 0:
    l, r = 1, n // 2
else:
    l, r = 0, n // 2
while l < r:
    al, ar = aaa[l], aaa[r]
    m = (al + ar) // 2
    while xxx[-1][0] <= m:
        x, i = xxx.pop()
        ans[i] = tmp
    tmp += al - ar
    l += 2
    r += 1
print('\n'.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())
A_list = sorted(list(map(int, input().split())))
X_list = [int(input()) for _ in range(Q)]
import numpy as np

def aoki(cards_, x):
    res = [abs(x-card) for card in cards]
    min_res = min(res)
    for idx, r in enumerate(res):
        if r == min_res:
            return cards[idx]
        
def game(cards, x):
    taka_sum = 0
    while(len(cards)>0):
        taka_card = cards.pop(np.argmax(cards))
        taka_sum += taka_card
        if len(cards) == 0:
            break
        aoki_card = aoki(cards, x)
        cards.remove(aoki_card)
    return taka_sum

import copy
for X in X_list:
    A_list_copy = copy.copy(A_list) 
    print(game(A_list_copy, X))

Submission status: Runtime Error

Solution code:
from copy import deepcopy

N,Q = map(int,input().split())
A = list(map(int,input().split()))
X = []
for i in range(Q):
    X.append(int(input()))
for j in range(Q):
    Y = deepcopy(A)
    a = 0
    b = 0
    for k in range(Q):
        if k%2 == 0:
            a+=Y.pop()
        else:
            Y.append(A[j])
            Y.sort()
            c = Y.index(A[j])
            d = Y.pop(c)
            if c == 0:
                ind = c
            elif len(Y) == c:
                ind = c-1
            elif abs(A[j]-Y[c-1]) <= abs(A[j]-Y[c]):
                ind = c-1
            else:
                ind = c
            b+=Y.pop(ind)
    print(a)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

n, q = map(int, input().split())

a = list(map(int, input().split()))

x = [int(input()) for _ in range(n)]


a.sort()

def getNearestValue(l, num):
    idx = np.abs(np.asarray(l) - num).argmin()
    return l[idx]

def game(xi, card):
    tkhs = 0
    aok = 0

    while len(card) > 0:
        tkhs += card[-1]
        del card[-1]
        if len(card) == 0:
            break
        ind = getNearestValue(card, xi)
        card.remove(ind)

    return tkhs

for i in range(len(x)):
    thistimecard = copy.copy(a)
    print(game(x[i], thistimecard))