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()))
ans=0
for x in X:
  if k-x <= x:
    ans += (k-x)*2
  else:
    ans += x*2
print(ans)

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

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

min_list = []
for x in xs:
    min_ = min(abs(x), abs(x-k))
    min_list.append(min_)

total = 0
for m in min_list:
    total += 2*m
print(total)

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

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

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

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

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

ans = 0
for i in x:
    ans += min(abs(i - 0), abs(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):
    if abs(x[i]) <= abs(x[i] - k):
        ans += x[i] * 2
    else:
        ans += abs(x[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 += min(x[i], K-x[i])*2
print (ans)

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

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

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


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


if __name__ == '__main__':
    main()

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


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


if __name__ == '__main__':
    main()

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

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

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

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=int(input()),int(input())
    X=list(map(int,input().split()))
    ans=0
    for i in range(n):
        if X[i]<=0: ans+=abs(X[i])
        elif X[i]>=k: ans+=X[i]-k
        else: ans+=min(X[i],k-X[i])
    print(ans*2)
resolve()

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

X=list(map(lambda x: min(2*x,2*(K-x)),X))


print(sum(X))

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

result = 0
for i in range(N):
    A_dis = x_list[i]
    B_dis = abs(K - x_list[i])
    min_dif = min(A_dis, B_dis)
    result = result + (min_dif * 2)

print(result)

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

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):
N = int(input())
K = int(input())
x = list(map(int, input().split()))

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

Solution (Python):
import sys
import math
from fractions import gcd





def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""


 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""


N = int(sys.stdin.readline())
K = int(sys.stdin.readline())
L = list(map(int, sys.stdin.readline().split()))

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

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

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):
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):
N = int(input())
K = int(input())
x = list(map(int,input().split()))
ans = 0
for i in range(N):
    ans += (2*min(abs(x[i]),abs(x[i]-K)))
print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
A, = map(int, input().split())
L = [int(v) for v in input().split()]

ans = 0
for i in range(N):
    t = L[i]
    ans += min(t * 2, abs(A - t) * 2)

print(ans)

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

distance = 0

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

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

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

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,abs(K-i))*2
print(ans)

Solution (Python):
N,K,*X=map(int,open(0).read().split());print(sum(2*min(x,K-x)for x in X))

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):
import sys,bisect as bs,collections as cl
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
n = onem()
m = onem()
a = l()
mid = m//2
ans = 0
for i in range(n):
    if mid >= a[i]:
        ans += 2 * a[i]
    else:
        ans += 2 * abs(m - a[i])
print(ans)

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

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

print(sum([min(x, K-x) for x in X])*2)

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))
ans = 0
for i in range(N):
    if X[i]<= abs(X[i]-K):
        ans += 2*X[i]
    else:
        ans += 2*abs(X[i]-K)
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],abs(x[i]-k))
print(ans)

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

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

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


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 += 2 * min(abs(x[i]), 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 range(n):
    if x[i]<k-x[i]:
        ans+=2*x[i]
    else:
        ans+=2*(k-x[i])
print(ans)

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

Solution (Python):

import sys


def ILI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def ISI(): return map(int, sys.stdin.readline().rstrip().split())
def II(): return int(sys.stdin.readline().rstrip())
def ISS(): return sys.stdin.readline().rstrip().split()
def IS(): return sys.stdin.readline().rstrip()

N = II()
K = II()
a = ILI()
ans = 0
for x in a:
    if x < K-x:
        ans += 2 * x
    else:
        ans += 2 * (K - x)
print(ans)

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

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

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

ans = 0

for i in x:
    ans += min(i, abs(k-i))

print(ans*2)

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


ans = 0
for x in X:
    if x <= abs(K-x):
        ans += 2*x
    else:
        ans += abs(K-x)*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):
n = int(input())
k = int(input())
x = list(map(int, input().split()))

ans = 0
for i in range(n):
    ans += 2 * min(x[i], 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 range(n):
  if X[i]<abs(k-X[i]):
    ans+=X[i]*2
  else:
    ans+=abs(k-X[i])*2
print(ans)

Solution (Python):
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    k = int(input().rstrip('\n'))
    x = list(map(int, input().rstrip('\n').split()))
    t = 0
    for v in x:
        t += (min(v, abs(k - v)) * 2)
    print(t)


if __name__ == '__main__':
    slove()

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

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

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):
def test():
    n = int(input())
    k = int(input())
    x = list(map(int,input().split()))
    a = 0; b = k
    count = 0
    for i in range(n):
        if abs(x[i]-a) <= abs(x[i]-b):
            count += 2*abs(x[i]-a)
        else:
            count += 2*abs(x[i]-b)
    print(count)


if __name__ == "__main__":
    test()

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

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

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)) * 2
print(ans)

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):
N = int(input())
K = int(input())
X = list(map(int, input().split()))

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

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

Solution (Python):
n=int(input())
k=int(input())
x=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=[int(i) for i in input().split()]

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

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

array = [int(i) for i  in input().split()]

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

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

Solution (Python):
import numpy as np
n = int(input())
k = int(input())
arr = list(map(int,input().split()))

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

print(ans)

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

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

Solution (Python):

import sys
input = sys.stdin.readline


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

count = 0

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

print(count)

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

if __name__ == '__main__':
    print(main())

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

Solution (Python):
n = int(input())
k = int(input())
x = [int(i) for i in input().split()]
ans = 0
for i in range(n):
    ans += 2 * min(x[i],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, abs(k - i))
print(ans)

Solution (Python):


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

if __name__ == '__main__':
    N = int(input())
    K = int(input())
    X = [int(x) for x in input().split()]
    main(N,K,X)

Solution (Python):
n = int(input())
k = int(input())
x = list(map(int, input().split())) 
a = 0
l = 0
while n>a:
    if (k/2)>=x[a]:
        l+=2*x[a]
        a+=1
    else:
        l+=2*(k-x[a])
        a+=1
else:
    print(l)

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 = list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=min(k-x[i],x[i])*2

print(ans)

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

total = 0

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

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

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

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

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

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

Solution (Python):
n=int(input())
k=int(input())
x=input().split()
c=0
for i in range(n):
    if 2*int(x[i])>k:
        c+=2*(k-int(x[i]))
    else:
        c+=2*int(x[i])

print(c)

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/2:
    ans += 2*x[i]
  else:
    ans += (k-x[i])*2
print(ans)

Solution (Python):

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


res = sum([min([abs(e - 0), abs(e - K)])*2 for e in x])


print(res)

Solution (Python):

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

Solution (Python):
N = int(input())
K = int(input())
X = list(map(int, input().split()))
ans = 0
for i, x in enumerate(X):
    ans += 2*min(x, 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):
    ans += min(k - x[i], 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(2*x[i], 2*(k-x[i]))
print(ans)

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

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

print(ans)

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

x=list(map(int, input().split()))
count=0
for i in range(n):
    a_dis=2*x[i]
    b_dis=abs(k-x[i])*2
    if a_dis>=b_dis:
        count+=b_dis
    else:
        count+=a_dis
print(count)

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

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

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

ans = 0

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

print(ans)

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

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

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

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

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

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

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

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

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

Solution (Python):

import sys


def solve(N: int, K: int, X: "List[int]"):
    
    res = 0
    for x in X:
        res += min(x, K-x)

    print(res*2)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    x = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, K, x)

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):
  now = x[i]
  ans += 2 * min( abs(now) , abs(k-now) )
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())
xs = list(map(int, input().split()))

move = []
for x in xs:
    move.append(min(2*x,2*(k-x)))
print(sum(move))

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

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

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

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

ans = 0
for i in range(n):
    if a[i] - 0 > k - a[i]:
        ans += k - a[i]
    else:
        ans += a[i] - 0
print(ans * 2)

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

ret=0
for i in range(N):
    if XN[i] < (K/2):
        ret+= XN[i]*2
    else:
        ret+= 2*(K-XN[i])
print(ret)

Solution (Python):
n = int(input())
k = int(input())
li = list(map(int, input().split()))
center = k//2
sum = 0
for i in li:
    if i <= center:
        sum += i*2
    else:
        sum += (k-i)*2
print(sum)

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

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

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

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

for i in nums:
    if i > abs(K - i):
        total += 2* (abs(K - i))
    else:
        total += 2 * i

print(total)

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

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

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

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

Solution (Python):
if __name__ == '__main__':
    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):
N = int(input())
K = int(input())
x = list(map(int,input().split()))

ans = 0

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

print(ans)

Solution (Python):
import math

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

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

Solution (Python):
n = int(input())
k = int(input())
thr = k // 2
x = list(map(int,input().split()))
ans = 0
for i in range(n):
    if x[i] > thr:
        ans += 2 * (k - x[i])
    else:
        ans += 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 += 2 * min(x[i],k-x[i])
print(ans)

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

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, K: int, x: "List[int]"):
    ans = 0
    for i in range(N):
        ans += min(x[i], K-x[i])
    print(ans*2)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    x = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, x)


if __name__ == '__main__':
    main()

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

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

ans = 0
for i in xList:
    xtmp = 2 * i
    ytmp = abs(i - K) * 2
    ans += min(xtmp, ytmp)
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(abs(x[i]),abs(x[i]-k))
print(2*ans)

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

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

print(sum(min(abs(K-x), abs(0-x)) * 2 for x in X))

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

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

print(sum)

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

robot_1 = 0
robot_2 = K
for i in range(N):
    distance_1 = abs(robot_1 - x[i])*2
    distance_2 = abs(robot_2 - x[i])*2

    ans += min(distance_1, distance_2)
print(ans)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math






def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    K = inputInt()
    x = inputList()
    
    ans = 0
    for i, val in enumerate(x):
        tmp = min(val-0, abs(K-val))
        ans += tmp * 2
        
    print(ans)
    
            	
if __name__ == "__main__":
	main()

Solution (Python):
def main48():
    n = int(input())
    k = int(input())
    data = input().split()
    x = []
    for i in range(n):
        x.append(int(data[i]))
    result = 0
    for i in range(n):
        if x[i] < k-x[i]:
            result = result + x[i] * 2
        else:
            result = result + (k-x[i]) * 2
    print(result)

if __name__ == "__main__":
    main48()

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

ans = 0

for x in X:
  if K >= 2*x:
    ans += x*2
  else:
    ans += (K-x)*2
print(ans)

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

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int, input().split()))
ans_list = []
for i in range(N):
    tmp = min(x[i], K-x[i])
    ans_list.append(2*tmp)

ans = sum(ans_list)
print(ans)

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

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

print(a)

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

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

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

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

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

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

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

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

ans = 0

for x in xs:
  if k - x > x:
    ans += x
  else:
    ans += k - x

print(ans * 2)

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