program nearest_card_game
  implicit none
  integer(8)              :: n, q
  integer(8), allocatable :: a(:), x(:)
  integer(8)              :: i, tl, th
  read(*,*) n, q
  allocate(a(n),x(q))
  read(*,*) a(1:n)
  do i = 1, q
     read(*,*) x(i)
  end do
  if (q .le. 12) then
     do i = 1, q
        write(*,'(i0)') total(a,x(i))
     end do
     stop
  end if
  
  tl = total(a,1_8)
  th = total(a,a(n))
  do i = 1, q
     if (x(i) .lt. a((n+3)/4)) then
        write(*,'(i0)') tl
     else if (x(i) .ge. a(n-1)) then
        write(*,'(i0)') th
     else
        write(*,'(i0)') total(a,x(i))
     end if
  end do
  deallocate(a,x)
  stop
contains
  function total(a,x) result(m)
    implicit none
    integer(8), intent(in) :: a(:), x
    integer(8)             :: m, n, i, ih, ib
    integer(8)             :: h(size(a),2), b(size(a),2)
    n = size(a)
    h(:,1) = a(n:1:-1)
    b(:,1) = abs(h(:,1) - x)
    do i = 1, n
       b(i,2) = i
    end do
    call merge_sort(n,b)
    do i = 1, n
       h(b(i,2),2) = i
    end do
    m = 0
    i = 1
    ih = 0
    ib = 0
    do
       ih = ih + 1
       do while (h(ih,1) .lt. 0 .and. ih .le. n)
          ih = ih + 1
       end do
       if (ih .gt. n) exit
       m = m + h(ih,1)
       b(h(ih,2),1) = -1
       i = i + 1
       if (i .gt. n) exit
       
       ib = ib + 1
       do while (b(ib,1) .lt. 0 .and. ib .le. n)
          ib = ib + 1
       end do
       if (ib .gt. n) exit
       h(b(ib,2),1) = -1
       i = i + 1
       if (i .gt. n) exit
    end do
    return
  end function total

  subroutine merge_sort(n,c)
    implicit none
    integer(8), intent(in)    :: n
    integer(8), intent(inout) :: c(n,2)
    integer(8)                :: number, length, i, ubound
    number = n
    length = 1
    do while (number .gt. 1)
       do i = 1, number/2
          ubound = min(2*i*length,n)
          call merge(c(2*(i-1)*length+1:(2*i-1)*length,:),c((2*i-1)*length+1:ubound,:))
       end do
       length = 2*length
       number = (number+1)/2
    end do
    return
  end subroutine merge_sort
  
  subroutine merge(c1,c2)
    implicit none
    integer(8), intent(inout) :: c1(:,:), c2(:,:)
    integer(8)                :: c(size(c1(:,1))+size(c2(:,1)),size(c1(1,:)))
    integer(8)                :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while (i1 .le. n1 .and. i2 .le. n2)
       if (c1(i1,1) .lt. c2(i2,1)) then
          c(i1+i2-1,:) = c1(i1,:)
          i1 = i1+1
       else if (c1(i1,1) .gt. c2(i2,1)) then
          c(i1+i2-1,:) = c2(i2,:)
          i2 = i2+1
       else if (c1(i1,2) .gt. c2(i2,2)) then
          c(i1+i2-1,:) = c1(i1,:)
          i1 = i1+1
       else
          c(i1+i2-1,:) = c2(i2,:)
          i2 = i2+1
       end if
    end do
    if (i1 .le. n1) then
       c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 .le. n2) then
       c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merge
end program nearest_card_game