Problem:
 Score : 200 points    Problem Statement There are N balls in the xy -plane. The coordinates of the i -th of them is (x_i, i) .
Thus, we have one ball on each of the N lines y = 1 , y = 2 , ... , y = N .  In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i -th type-A robot at coordinates (0, i) , and the i -th type-B robot at coordinates (K, i) .
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1 , y = 2 , ... , y = N .  When activated, each type of robot will operate as follows.    When a type-A robot is activated at coordinates (0, a) , it will move to the position of the ball on the line y = a , collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.    When a type-B robot is activated at coordinates (K, b) , it will move to the position of the ball on the line y = b , collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.    Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.      Constraints  1 \leq N \leq 100  1 \leq K \leq 100  0 < x_i < K  All input values are integers.         Inputs Input is given from Standard Input in the following format:  N  K  x_1  x_2  ...  x_N       Outputs Print the minimum possible total distance covered by robots.        Sample Input 1 1
10
2      Sample Output 1 4  There are just one ball, one type-A robot and one type-B robot.  If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2 , and the distance from the ball to the original position of the robot is also 2 , for a total distance of 4 .  Similarly, if the type-B robot is used, the total distance covered will be 16 .  Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4 .       Sample Input 2 2
9
3 6      Sample Output 2 12  The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.       Sample Input 3 5
20
11 12 9 17 12      Sample Output 3 74

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

goukei = []
for i in range(N):
    goukei.append(min([abs(x[i] - 0), abs(x[i] - K)]))

print(sum(goukei) * 2)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min(2*abs(i-K), 2*abs(i))
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = [int(m) for m in input().split(" ")]

walk_sum = 0
for n_i in range(n):
    walk_sum += min(2*x[n_i], 2*(k-x[n_i]))

print(walk_sum)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))

cnt = 0
for i in range(n):
    if k-x[i] < x[i]:
        cnt += k-x[i]
    else:
        cnt += x[i]
print(cnt*2)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))

ans = 0
for x in X:
    ans += 2*min(x,K-x)
print(ans)

Solution (Python):
def fa(): 
    global ans
    print(ans)
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
N = getN()
K= getN()

xs = getList()

ans = 0
for i in range(N):
    x = xs[i]
    dis= 2*min(x, abs(K-x))
    ans += dis

print(ans)

Solution (Python):
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
k = ni()
x = list(li())

ans = 0
for xi in x:
    ans += 2 * min(xi, k-xi)
    
print(ans)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int,input().split()))
ans = 0
for x in X:
    ans += min(x, K-x)
print(ans*2)

Solution (Python):
n = int(input())
k = int(input())

s = 0 
for x in list(map(int,input().split())):
  s += min(x,k-x)
print(2*s)

Solution (Python):
n = int(input())
k = int(input())
listX = list(map(int, input().split()))

sum=0           
for x in listX:
    sum+=min(abs(k-x)*2, abs(x)*2)

print(sum)

Solution (Python):
n = int(input())
k = int(input())

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

ans = 0

for i in range(len(x)):
    if k - x[i] <= x[i]:
        ans += 2 * (k - x[i])

    else:
        ans += 2 * x[i]

print(ans)

Solution (Python):
n = int(input())
k = int(input())
list_x = list((map(int, input().split())))
ans = 0
for ball_x in list_x:
    ans += 2 * min(ball_x, k-ball_x)
print(ans)

Solution (Python):
n=int(input())
k=int(input())
a=list(map(int,input().split()))
cnt=0
for i in range(n):
    if(a[i]>=k-a[i]):
        cnt+=2*(k-a[i])
    else:
        cnt+=2*a[i]
print(cnt)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in range(n):
    a = x[i]
    b = k - x[i]
    ans += 2*min(a, b)
print(ans)

Solution (Python):
distance = 0
N = int(input())
K = int(input())
xi = list(map(int,input().split()))
for i in xi:
    if abs(i) > abs(K-i):
        distance+=(K-i)
    else:
        distance+=abs(i)

print(distance*2)

Solution (Python):
n = int(input())
k = int(input())
lis = list(map(int,input().split()))
cou = 0
for i in range(n):
  cou += min(lis[i] * 2,abs(k-lis[i]) * 2)
print(cou)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int,input().split()))
answer = 0
for i in range(N) :
    answer += min(X[i],K-X[i])*2
print(answer)

Solution (Python):
input();K=int(input())
ans=[min(x-0,K-x) for x in list(map(int,input().split()))]
print(sum(ans)*2)

Solution (Python):
input()
K=int(input())
ans = [min(x-0,K-x) for x in list(map(int,input().split()))]
print(sum(ans)*2)

Solution (Python):
n=int(input())
k=int(input())
x=list(map(int,input().split()))
l=[]
for i in x:
  l.append(min(i,k-i))
print(sum(l)*2)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int,input().split()))
ans = 0
for i in x:
    ans += min(i*2,abs(i-k)*2)
print(ans)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int,input().split()))
L = []
for k in range(N):
  L.append(min(x[k],abs(K-x[k]))*2)
  S = sum(L)
print(S)

Solution (Python):
N=int(input())
K=int(input()) 
S=list(map(int, input().split()))
total=0
for i in range(N):
    if abs(S[i]-K)<S[i]:
        total+=abs(S[i]-K)*2
    else:
        total+=2*S[i]
print(total)

Solution (Python):
N = int(input())
K = int(input())
x_list = list(map(int, input().split()))

sum = 0

for x in x_list:
    A = 2 * x
    B = (K - x) * 2
    sum += B if A >= B else A

print(sum)

Solution (Python):
N=int(input())
K=int(input())
xi=[int(i) for i in input().split()]


ans=0
for i in range(N):
    if xi[i]<abs(xi[i]-K):
        ans+=xi[i]*2
    else:
        ans+=abs(xi[i]-K)*2
    
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += 2*min(x[i], k-x[i])
print(ans)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N = int(input())
    K = int(input())
    X = list(map(int, input().split()))

    ans = 0
    for x in X:
        ans += min(x, abs(K - x))
    print(ans * 2)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
K=int(input())
x=list(map(int, input().split()))
ans=0
for i in range(N):
    ans+=min(x[i]*2, (K-x[i])*2)
print(ans)

Solution (Python):


N = int( input() )

K = int( input() )

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

x = 0
for num in list:
    a = num * 2
    b = (K - num) * 2

    if b <= a:
        x += b
    elif b > a:
        x += a

print( x )

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min([K - i, i]) * 2

print(ans)

Solution (Python):
N = int(input())
K = int(input())

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

ans = 0

for x in A:
  ans += min(x, K-x) * 2
  
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = [int(v) for v in input().split()]

ans = 0
for v in x:
    ans += min(v, k - v) * 2
print(ans)

Solution (Python):
input();k=int(input());s=0
for a in map(int,input().split()):
    s+=2*min(a,abs(k-a))
print(s)

Solution (Python):
n=int(input())
k=int(input())

x = list(map(int,input().split()))	
ass = 0

for i in range(n):
    ass += min(x[i]*2,(k-x[i])*2)
print(ass)

Solution (Python):

n = int(input())
k = int(input())
x = list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=min(2*x[i],2*(k-x[i]))
print(ans)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
ans = 0
for num in x:
    if num <= abs(K-num):
        ans += 2*num
    else:
        ans += 2*abs(K-num)
print(ans)

Solution (Python):
N=int(input())
K=int(input())
X=[int(x) for x in input().split()]

ans=0
for x in X:
  ans+=min(abs(x-K),x)

print(ans*2)

Solution (Python):
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N = int(input())
K = int(input())
xx = inpl()
ans = 0
for x in xx:
	ans += min(x,K-x)*2

print(ans)

Solution (Python):
n, k = [int(input()) for _ in range(2)]
x = list(map(int, input().split()))
a = k / 2
ans = 0
for i in x:
  if i > a:
    ans += (k - i) * 2
  else:
    ans += i * 2
print(int(ans))

Solution (Python):
N=int(input())
K=int(input())
x=[int(N) for N in input().split()]
distance=0
for i in range (N):
    distance+=2*min(abs(x[i]-0),abs(x[i]-K))
print(distance)

Solution (Python):
N, K = [int(input()) for _ in range(2)]
x = list(map(int, input().split()))
ans = 0
for xi in x:
    ans += min(xi, K - xi)
print(ans * 2)

Solution (Python):


def main():
    n = int(input())
    k = int(input())
    x_list = list(map(int, input().split()))

    distance = 0

    for i in x_list:
        distance += 2 * min(k - i, i)
    
    print(distance)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
K = int(input())
X = list( map( int, input().split()))
ans = 0
for i in range(N):
    ans += min(abs(X[i]),abs(X[i]-K))*2
print(ans)

Solution (Python):
def f(K):
  return lambda n:min(int(n),K-int(n))*2

N=int(input())
K=int(input())
g=f(K)
x=map(g,input().split())
print(sum(x))

Solution (Python):
n=int(input())
k=int(input())/2
x=list(map(int,input().split()))
res=0
for i in x:
    res += k - abs(k-i)
print(int(res*2))

Solution (Python):
n = int(input())
k = int(input())
x = [int(m) for m in input().split(" ")]

walk_sum = 0
for n_i in range(n):
    walk_sum += min(2*x[n_i], 2*(k-x[n_i]))

print(walk_sum)

Solution (Python):
N = int(input())
K = int(input())

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

sum=0
for i in a:
    if abs(i-0) <= abs(i-K):
        sum+=abs(i-0)*2
    else:
        sum+=abs(i-K)*2
print(sum)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += min(x[i], abs(x[i] - k))
ans *= 2
print(ans)

Solution (Python):
n = input()
k = input()
n = int(n)
k = int(k)
xs = input().split(" ")

l = list()
for x in xs:
	l.append(int(x))

cost = 0
for arg in l:
	if (2 * arg) <= (2 * abs(k - arg)):
		cost += (2 * arg)
	else:
		cost += (2 * abs(k - arg))

print(cost)

Solution (Python):
N = int(input())
K = int(input())
Ns = input().split()
Ns = list(map(int,Ns))

kyori = []
for i in range(len(Ns)):
	a = Ns[i]
	b = K - Ns[i]

	if(a<=b):
		kyori.append(a*2)
	else:
		kyori.append(b*2)

print(sum(kyori))

Solution (Python):
n = int(input())
k = int(input())
x = [int(i) for i in input().split()]
border = k/2
output = 0
for i in x:
    if border > i:
        output += i
    else:
        output += abs(k-i)
print(output*2)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))

total = 0
for y, v in enumerate(x):
    total += min(k-v, v) * 2

print(total)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

result = 0

for i in range(len(x)):
  if x[i] * 2 <= abs((x[i] - K) * 2):
    result += x[i] * 2
  else:
    result += abs((x[i] - K) * 2)

print(result)

Solution (Python):
n = int(input())
k = int(input())

x = tuple(map(int, input().split()))
assert len(x) == n

r = sum([min(k - x[i], x[i]) for i in range(len(x))]) * 2
print(r)

Solution (Python):
n=int(input())
k=int(input())
t=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=min(t[i]*2,(k-t[i])*2)
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in range(n):
  ans += min(x[i], k-x[i])*2
print(ans)

Solution (Python):
N = int(input())
K = int(input())
x = input().split()
ans = 0
for i in range(0,N):
	if int(x[i]) > (K-int(x[i])):
		ans += (K-int(x[i]))*2
	else:
		 ans += int(x[i])*2
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += min(x[i], k-x[i])*2
print(ans)

Solution (Python):
n=int(input())
k=int(input())
x=[int(i) for i in input().split()]
ans=chk=0
for i in x:
    ans+=min(i,abs(i-k))
print(ans*2)

Solution (Python):
n = int(input())
k = int(input())

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

cost = 0
for x in x_list:
    if x <= k/2:
        cost = cost + 2*x
    else:
        cost = cost + 2*(k-x)
print(cost)

Solution (Python):
n = int(input())
k = int(input())
X = list(map(int, input().split()))

SUM = 0
for x in X:
    SUM += min(2*x, 2*abs(x-k))
print(SUM)

Solution (Python):
n = int(input())
k = int(input())

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

cost = 0
for x in x_list:
    if x <= k/2:
        cost = cost + 2*x
    else:
        cost = cost + 2*(k-x)
print(cost)

Solution (Python):
N = int(input())
K = int(input())
A = map(int, input().split())
ans = [2 * min(a, K - a) for a in A]
print(sum(ans))

Solution (Python):

def main():
    N = int(input())
    K = int(input())
    x = list(map(int,input().split()))
    ans = 0

    for i in x:
        ans+= min(i , abs(K -i)) *2

    print(ans)

if __name__ == "__main__":
        main()

Solution (Python):
n = int(input())
k = int(input())
x = map(int, input().split())

print(sum(map(lambda y: min(y, abs(k - y)) * 2, x)))

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))

re = 0
for x in X:
    if x < K - x:
        re += x
    else:
        re += K-x

print(re*2)

Solution (Python):
N = int(input())
K = int(input())
print(sum([2 * min(int(xi), K-int(xi))
      for xi in input().split()]))

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
res = 0
for i in range(N):
    res += 2 * min(x[i],K-x[i])
print(res)

Solution (Python):
n, k = list(map(int, (input(), input())))
numlist = list(map(int, input().split()))

total = 0

for i in range(n):
  total += (numlist[i] if numlist[i] < abs(numlist[i] - k) else abs(numlist[i] - k)) * 2

print(total)

Solution (Python):
"""
https://abc074.contest.atcoder.jp/tasks/abc074_b
"""
n=int(input())
k=int(input())
x=[int(i) for i in input().split()]
a=0
for i in x:
    a=a+min(abs(i-0),abs(i-k))*2
print(a)

Solution (Python):
N = int(input())
K = int(input())
X = [int(i) for i in input().split()]

total = 0
for i in range(N):
    if abs(X[i] - K) < abs(X[i]):
        total += 2 * abs(X[i] - K)
    else:
        total += 2 * abs(X[i])

print(total)

Solution (Python):
N=int(input())
K=int(input())

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

ans=0

for i in range(N):
    a=x[i]
    b=abs(K-x[i])
    ans=ans+min(a,b)*2

print(ans)

Solution (Python):
input()
K=int(input())
print(sum(min(x,K-x)*2 for x in map(int,input().split())))

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int,input().split()))

cnt = 0
for i in x:
    cnt += 2 * min(i,K - i)

print(cnt)

Solution (Python):
N = int(input())
K = int(input())
src = list(map(int,input().split()))

ans = 0
for a in src:
    tmp = min(a, K-a)
    ans += tmp*2
print(ans)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int,input().split()))

cnt = 0
for i in x:
    cnt += 2 * min(i,K - i)

print(cnt)

Solution (Python):
N=int(input().strip())
K=int(input().strip())
X=list(map(int,input().split()))
print(sum(min(x,abs(x-K))*2 for x in X))

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

total = 0
for i in range(N):
    if x[i] <= K-x[i]:
        total += 2*x[i]
    else:
        total += 2*(K-x[i])

print(total)

Solution (Python):


N = int(input())
K = int(input())
num_list = list(map(int, input().split()))

ans = 0
for i in range(N):
    A = num_list[i]
    B = abs(K - A)
    if A <= B:
        ans += A * 2
    else:
        ans += B * 2
print(ans)

Solution (Python):
N = int(input())
K = int(input())

ans = 0
for x in input().split():
    x = int(x)
    ans += min(x, K - x) * 2
print(ans)

Solution (Python):
N=int(raw_input())
K=int(raw_input())
x=map(int,raw_input().split())
print 2*sum([min(xi,K-xi) for xi in x])

Solution (Python):
N, K = int(input()), int(input())
a = list(map(int, input().split()))
res = 0
for e in a:
    res += min(e * 2, (K - e) * 2)
print(res)

Solution (Python):

a = [input() for i in range(3)]
ave = int(a[1])/2
move = 0

for i in a[2].split():
    if float(i) <= ave:
        move += (2 * float(i))
    else:
        move += (2 * (float(a[1]) - (float(i))))
print(int(move))

Solution (Python):
n=int(input())
k=int(input())
a=list(map(int,input().split()))
ans=0
for x in a:
    ans+=min(abs(2*x), abs(2*(k-x)))
print(ans)

Solution (Python):

N = int(input())
K = int(input())
X = list(map(int, input().split()))

ans = 0

for i in range(N):
    if X[i] >= K - X[i]:
        ans += 2 * (K - X[i])
    else:
        ans += 2 * X[i]

print(ans)

Solution (Python):


n = int(input())
k = int(input())
X = list(map(int, input().split()))
ans = 0

for i in range(n):
    ans += min(2*abs(X[i]), 2*abs(k-X[i]))

print(ans)

Solution (Python):
N = int(input())
K = int(input())
x_list = list(map(int, input().split()))

cnt = 0
for x in x_list:
    cnt += min(x, K - x) * 2
print(cnt)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))
ans = 0
for x in X:
    if x < K / 2:
        ans += 2 * x
    else:
        ans += 2 * (K - x)
print(ans)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))
res = 0

for i in range(N):
    res += min(abs(K-X[i]), abs(X[i]))*2
    
print(res)

Solution (Python):
N = int(input())
K = int(input())
X = [int(i) for i in input().split()]
ans = 0
for x in X:
  ans += 2* min(x, K-x)
print(ans)

Solution (Python):
N = int(input())
K = int(input())
X = list(int(i) for i in input().split())

distance = 0

for i in X:
    distance += min(i*2, (K-i)*2)
    
print(distance)

Solution (Python):
n = int(input())
k = int(input())
x = [int(i) for i in input().split()]

cost = 0

for dis in x:
    cost += min(dis, k-dis)

print(cost*2)

Solution (Python):
N = int(input())
K = int(input())
xchar = input().split()
x = []
for i in range(N):
    x.append(int(xchar[i]))

distance = 0

for i in range(N):
    distance = distance + 2*min(abs(K-x[i]),x[i])

print(distance)

Solution (Python):
s=0;input();k=int(input())
for x in input().split():
    x=int(x);s+=min(abs(k-x),x)*2
print(s)

Solution (Python):
n, k = int(input()), int(input())
ans = 0
for x in input().split():
    x = int(x)
    ans += min(x, k-x)*2
print(ans)

Solution (Python):
n=int(input())
k=int(input())
x=list(map(int,input().split()))
cnt=0
for i in range(n):
    ans=min(x[i],k-x[i])
    cnt+=ans*2
print(cnt)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int,input().split()))
ans = 0
for i in range(n):
    ans += min(x[i]*2,abs((k-x[i])*2))
print(ans)

Solution (Python):
N = int(input())
K = int(input())
balls = list(map(int, input().split(' ')))
dist = 0
for i in balls:
    if i >= abs(K - i):
        dist += abs(K - i) * 2
    else:
        dist += i * 2
print(dist)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int,input().split()))
counter = 0

for d in x:
    counter += min(d*2,(k-d)*2)
    
print(counter)

Solution (Python):
n = int(input())
k = int(input())
x = map(int, input().split())
dis = 0

for i in x:
    if abs(k-i) >= i:
        dis += 2 * i
    else:
        dis += 2 * abs(k-i)

print(dis)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
tot = 0
for i in range (N):
    tot += 2*min(abs(K-x[i]), x[i])
print(tot)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int,input().split()))
Ans = 0
for i in range(N):
    Ans += min(X[i]*2,(K-X[i])*2)
print(Ans)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int,input().split()))[:N]

SUM=0
for x in X:
    if x < K-x:
        SUM += x*2
    else:
        SUM += (K-x)*2
        
print(SUM)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))
ans = 0
for k in range(N):
    ans += min(2*X[k],2*(K-X[k]))
print(ans)

Solution (Python):
N = int(input())
K = int(input())
x = [int(i) for i in input().split()]
d = 0
for i in range(N):
    if x[i] < abs(x[i]-K):
        d += x[i]*2
    else:
        d += abs(x[i]-K)*2
print(d)

Solution (Python):
n=int(input())
k=int(input())
x=list(map(int,input().split()))
ans=0

for p in x:
    ans+=min(p,abs(k-p))*2

print(ans)

Solution (Python):
a=int(input())
b=int(input())
c=list(map(int, input().split()))

print(sum([min(i*2,(b-i)*2) for i in c]))

Solution (Python):
n=int(input())
k=int(input())
x=list(map(int,input().split()))
total=0
for i in range(n):
    total+=min(x[i],k-x[i])
print(total*2)

Solution (Python):
n=int(input())
k=int(input())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]<abs(x[i]-k):
        s += x[i]
    else:
        s += abs(x[i]-k)
print(s*2)

Solution (Python):
def distance(balls, k):
    total = 0
    for x in balls:
        total += 2 * min(x, abs(x - k))
    return total


if __name__ == '__main__':
    _ = input()
    k = int(input())
    balls = map(int, input().split())
    print(distance(balls, k))

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
result = 0
for i in range(n):
    result += min(x[i], abs(k-x[i]))
print(result*2)

Solution (Python):
N = int(input())
K = int(input())

temp_list = list(map(int, input().split()))
total_len = 0

for i in temp_list:
    if K-i <= i:
        total_len +=((K-i)*2)
    else:
        total_len += i*2
     
print(total_len)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

ans = 0

for x_i in x:
    ans += min(K-x_i, x_i)*2

print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
num = 0
for i in x:
    num += min(2 * i, abs(i - k) * 2)

print(num)

Solution (Python):
N = int(input())
K = int(input())

X = [int(s) for s in input().split()]

sumdist = 0
for x in X:
    typeA = x * 2
    typeB = abs(x- K) * 2
    dist = min(typeA, typeB)
    sumdist += dist

print(sumdist)

Solution (Python):
N=int(input())
K=int(input())
data=list(map(int,input().split()))
count=0
for i in range(N):
    count+= 2*data[i] if data[i]<abs(data[i]-K) else 2*abs(data[i]-K)
print(count)

Solution (Python):
n = int(input())
k = int(input())
x = [int(i) for i in input().split()]
answer = 0
for i in x:
	answer += 2*(min(i,k-i))
print(answer)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
m = 0
for i in x:
    m += min(2*i,2*(K-i))
print(m)

Solution (Python):
N = int(input())
K = int(input())
sum = 0
num = input().split()
for i in range(N):
  work = int(num[i])
  sum += min(2*work,abs(K-work)*2)
print(sum)

Solution (Python):
p=input;p();k=int(p());print(sum([2*min(i,abs(k-i))for i in map(int,p().split())]))

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

ans = sum(map(lambda a: min(abs(a-0), abs(K-a)), x))
print(ans * 2)

Solution (Python):





if __name__ == '__main__':
    ball_count = int(input())
    robotB_position = int(input())
    positions = list(map(int, input().split()))

    total_length = 0

    for i in range(ball_count):
        if positions[i] < abs(robotB_position - positions[i]):
            total_length += 2 * positions[i]
        else:
            total_length += 2 * abs(robotB_position - positions[i])

    print(total_length)

Solution (Python):
n = int(input())
k = int(input())
ans = 0
lis = list(map(int,input().split()))
for i in range(n):
    ans += min(2 * (lis[i]),2 * (k - lis[i]))
print(ans)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(10000)


def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))


def to_bin(x):
    return bin(x)[2:]


def dim2input(N):
    li = []
    for _ in range(N):
        li.append(input().split())
    return li



dp = None

def main():
    N = int(input())
    K = int(input())
    X = list(map(int, input().split()))

    move = 0

    for x in X:
        move += min(2*x, 2*abs(x-K))

    print(move)

main()

Solution (Python):
n = int(input())
k = int(input())
x = [int(x) for x in input().split()]
ans = 0
for i in x:
    ans += min(i*2, (k-i)*2)
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
print(sum([ min( k-x[i], x[i] ) * 2 for i in range(n) ]))

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
print(sum([ min( abs(x[i]-k), x[i] ) * 2 for i in range(n) ]))

Solution (Python):
n=int(input())
k=int(input())
x=list(map(int,input().split()))
sum=0
for i in range(n):
    sum+=min(abs(x[i]),abs(k-x[i]))*2
print(sum)

Solution (Python):
N=int(input())
K=int(input())
x=list(map(int,input().split()))
dist=0
for i in range(N):
    if(x[i]<abs(K-x[i])):
        dist+=2*x[i]
    else:
        dist+=2*abs(K-x[i])
print(dist)

Solution (Python):
N = int(input())
K = int(input())
x= list(map(int,input().split(" ")))
ans = 0
for i in range(N):
    temp_x = x[i]
    if temp_x < abs(K-temp_x):
       ans += 2*temp_x 
    else:
        ans+=2*abs(K-temp_x)

print(ans)

Solution (Python):
N = int(input())
K = int(input())
sum = 0
x = list(map(int, input().split()))
for i in range(N):
    sum += 2 * min(x[i], abs(K - x[i]))
print(sum)

Solution (Python):
N = int(input())
K = int(input())
S = 0
st = input().split()
for i in range(N):
  xi = int(st[i])
  S += 2*min([xi,K-xi])
 
print(S)

Solution (Python):
N = int(input())
K = int(input())
S = 0
st = input().split()
for i in range(N):
  xi = int(st[i])
  S += 2*min([xi,K-xi])

print(S)

Solution (Python):
N=int(input())
K=int(input())
S=list(map(int, input().split()))

t=0
for i in range(N):
    if K-S[i]<S[i]:
        t+=(K-S[i])*2
    else:
        t+=S[i]*2

print(t)

Solution (Python):
n, k = int(input()), int(input())
x = [int(i) for i in input().split()]
ans = 0

for i in range(n):
    ans += 2*min((k-x[i], x[i]))

print(ans)

Solution (Python):
n = int(input())
k = int(input())
xs = list(map(int, input().split()))

print(sum((min(x, k-x) * 2 for x in xs)))

Solution (Python):
n = int(input())
k = int(input())
a = 0
x = list(map(int, input().split()))
ans = 0
for i in range(n):
   if abs(x[i]-a) <= abs(k-x[i]):
      ans += 2*abs(x[i]-a)
   else:
      ans += 2*abs(k-x[i])
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int,input().split()))

ans = 0
for i in x:
    ans += 2 * min(i,k-i)
print(ans)

Solution (Python):
n = int(input())
k = int(input())
xl = [int(x) for x in input().split()]
s=0
for x in xl:
    s+= x*2 if abs(k-x)>x else abs(k-x)*2
print(s)

Solution (Python):
n=int(input())
k=int(input())
x=[int(i) for i in input().split()]
ans=0
for y in x:
        ans+=min(y,abs(k-y))
print(ans*2)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min(i*2, (k-i)*2)
print(ans)

Solution (Python):

n = input()
K = int(input())

xs = map(int, input().split())

res = 0
for p in xs:
    res += min(p, K - p)

print(res*2)

Solution (Python):
n = int(input())
k = int(input())
arr = map(int, input().split())
ans = []
for ar in arr:
  if (ar < k - ar):
    ans.append(ar)
  else:
    ans.append(k-ar)



print(sum(ans)*2)

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

a = 0

for i in range(N):
    a += min(x[i], K-x[i])

print(a*2)

Solution (Python):
import math

n=int(input())
k=int(input())
x=list(map(int,input().split()))

l=0
for i in range(n):
    if(abs(x[i]-0) < abs(x[i]-k)):
        l+=2*abs(x[i]-0)
    else:
        l+=2*abs(x[i]-k)

print(l)

Solution (Python):
a=int(input())
b=int(input())


def ab(x):
  return min(abs(x),abs(x-b))*2

print(sum([ab(int(i)) for i in input().split(' ')]))

Solution (Python):
n = int(input())
k = int(input())
x = list(input().split())
x = [ int(i) for i in x ]

sum = 0
for i in range(n):
	a = x[i] * 2
	b = (k - x[i]) * 2
	if a < b:
		sum += a
	else:
		sum += b

print(sum)

Solution (Python):
input()
k=int(input())
print(sum(2*min(x,k-x) for x in map(int,input().split())))

Solution (Python):



n = int(input())
k = int(input())
li = list(map(int, input().split()))


count = 0
for p in li:
    if p < k - p:
        count += p * 2
    else:
        count += (k-p) * 2


print(count)

Solution (Python):
N=input();K=input();x=input().split();Ans=int(0)
x.sort(key=int)
for i in range(0,len(x)):
    if(int(x[i])>(int(K)/2)):
        Ans += 2*(int(K)-int(x[i]))
    else:
        Ans += 2*int(x[i])
print(int(Ans))

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int,input().split())) 

Ans = 0

for i in range(N):
	p = abs(x[i])
	q = abs(K - x[i])
	if p >= q :
		Ans += 2 * q
	if p < q:
		Ans += 2 * p

print(Ans)

Solution (Python):
N = int(input())
K = int(input())
p = (list(map(int,input().split())))
ans = 0
for i in range(N):
    if abs(p[i] - 0) < abs(p[i] - K):
        ans += abs((p[i]-0) * 2)
        
    else:
        ans += abs((p[i]-K) * 2)
        
print(ans)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split(' ')))

dsum = 0

for i in x:
    if i <= k/2:
        dsum += i*2
    else:
        dsum += (k-i)*2

print(dsum)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in range(n):
    ans += min(x[i], k - x[i]) * 2
print(ans)