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

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

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

Solution (Python):


import itertools
import collections
import bisect

def main():
    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(K - X[i])) * 2
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


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

length = 0

for i in range(len(x)):
    length += min(x[i]*2,abs(x[i]-k)*2)

print(length)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007


"""
n,a = IR(2)
print(n**2-a)
"""

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

"""
a,b,c,d,e,f = LI()
a *= 100
b *= 100
ans = [a,0]
for i in range(f//a+1):
    for j in range((f-a*i)//b+1):
        for k in range((f-a*i-b*j)//c+1):
            for l in range((f-a*i-b*j-c*k)//d+1):
                if i or j or k or l:
                    s = c*k+d*l
                    w = a*i+b*j
                    q = w*e//100
                    if s <= q:
                        if w+s <= f:
                            if ans[1]/ans[0] < s/(s+w):
                                ans = [w+s,s]
print(*ans)
"""


"""
n = I()
d = LIR(n)
l = [[1 for j in range(n)] for i in range(n)]
for k in range(n):
    for i in range(n):
        for j in range(n):
            if d[i][j] > d[i][k]+d[k][j]:
                print(-1)
                quit()
            if d[i][j] == d[i][k]+d[k][j]:
                l[i][j] = l[i][k]+l[k][j]

ans = 0
for i in range(n):
    for j in range(n):
        if l[i][j] == 4:
            ans += d[i][j]
ans //= 2
print(ans)
"""

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

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

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

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

Solution (Python):
N = int(input())
K = int(input())
x = list(map(int,input().split()))
lists = []
result = 0
for i in range(N):
    min = x[i] * 2
    num = x[i] - K
    B = abs(num) * 2
    if min > B:
        lists.append(B)
    else:
        lists.append(min)
    result += lists[i]
print(str(result))

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
from operator import itemgetter

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)

def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())

def pne(n):
    print(n,end='')

rem = pow(10,9)+7

n = ip()                                     
k = ip()

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










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

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

count = 0

for i in x:
    if 2*i<=2*abs(K-i):
        count += 2*i
    else:
        count += 2*abs(K-i)
print(count)

Solution (Python):

N = int(input())
K = int(input())

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

Solution (Python):
import sys

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

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

    print(total)


if __name__ == '__main__':
    main()

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

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]*2,(k-x[i])*2)
  
print(a)

Solution (Python):
def collecting_balls(N: int, K: int, X: list)->int:
    dist = 0
    for x in X:
        if x < K - x:
            
            
            dist += 2 * x
        else:
            
            
            dist += 2 * (K-x)
    return dist


if __name__ == "__main__":
    N = int(input())
    K = int(input())
    X = [int(s) for s in input().split()]
    ans = collecting_balls(N, 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(x[i]*2,(k-x[i])*2)
print(ans)

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

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

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

print(ans)

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

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

Solution (Python):
n=int(input())
k=int(input())
l=input().split()
m=0
for x in range(len(l)):
    m+=2*min(int(l[x]),k-int(l[x]))
else:
    print(m)

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

l = list(map(int,input().split()))
c = 0

for i in l:
	if i < abs(k-i):
		c += 2*i
	else:
		c += 2*abs(k-i)
print(c)

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

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

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 = [int(_) for _ in input().split()]
ResSum = 0

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

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

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

res = 0
c = k/2
for x_ in x:
    if x_ < c:
        res += x_*2
    else:
        res += (k-x_)*2
print(res)

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

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

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):
def main():
    N = int(input())
    K = int(input())
    ans = 0
    for x in map(int, input().split()):
        ans += min(x, K-x)*2
    print(ans)

if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):
"""AtCoder"""

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

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

Solution (Python):

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

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

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

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

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) * 2

print(ans)

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

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

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

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

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

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

Solution (Python):

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

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

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

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

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

print(ans)

Solution (Python):
n,k=int(input()),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 = map(int, input().split())

ans = 0

for i in x:
    if i < K - i:
        ans += 2 * i
    else:
        ans += 2 * (K - i)

print(ans)

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

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

distance=0
for i in X:
    adis=i*2
    bdis=abs(K-i)*2
    if adis<bdis:
        distance+=adis
    else:
        distance+=bdis
print(distance)

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

Solution (Python):
N=int(input())
K=int(input())
def ans(s):
  n=int(s)
  return 2*min(K-n,n)
x=list(map(ans,input().split()))
print(sum(x))

Solution (Python):
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


N = int(input())
K = int(input())

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

ans = 0

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

print("%d"%(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),abs(k-i))*2
print(ans)

Solution (Python):
int_n = int(input())
int_k = int(input())

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

print(sum([i if i <= abs(i - int_k) else abs(i - int_k) for i in li_x]) * 2)

Solution (Python):
n,k = [int(input()) for i in range(2)]
l = [list(map(int,input().split()))]
a = 0

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

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

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

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

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

    ans = 0
    for x in l:
        ans += 2 * min([x, k - x])

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
N = getInt()
K = getInt()
x = getIntList()
d = 0
for i in range(N):
    d += min(x[i], abs(K-x[i]))*2
print(d)

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

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

Solution (Python):


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

count = 0
for i in range(n):
    count += min([x_list[i], k-x_list[i]])*2

print(int(count))

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

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

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

ans = [min(i, K-i) for i in x]
print(sum(ans)*2)

Solution (Python):
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)

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

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

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

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

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

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

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

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

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

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


abc074_b()

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

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

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

Solution (Python):

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

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

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

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

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

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

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

Solution (Python):



def main():
    """Function."""
    n = int(input())
    k = int(input())
    x = list(map(int, input().split()))

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


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    n = int(input())
    k = int(input())
    xs = list(map(int, input().split()))
    res = sum(2 * min(x, abs(x - k)) for x in xs)
    print(res)


if __name__ == '__main__':
    main()

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

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

distance_sum = 0
for x in x_list :
    if (abs(K - x) > x) :
        distance_sum += x*2
    else :
        distance_sum += abs(K-x)*2

print(distance_sum)

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

Solution (Python):
N=int(input())
K=int(input())
count=0
Min=0

a=input().strip().split(" ")
a=[int(i) for i in a]

for i in range(N):
    if(abs(a[i]-0)>abs(a[i]-K)):
        count+=abs(a[i]-K)*2
    else:
        count+=abs(a[i]-0)*2

print(count)

Solution (Python):

N = int(input())
K = int(input())
lst = list(map(int, input().split()))
print(sum(min(x*2, (K-x)*2) for x in lst))

Solution (Python):

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

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

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

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

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

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

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

result = sum(min(x, K - x) * 2 for x in Xs)

print(result)

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


for xi in x_list:
	
    
    x_move += min(xi,K-xi)*2
    

print(x_move)

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

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

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

for num in num_list:
    ans += 2*min(num,k-num)
print(ans)

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

for num in num_list:
    diff = k-num
    if num > diff:
        ans += diff*2
    else:
        ans += num*2
print(ans)

Solution (Python):



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

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


if __name__ == '__main__':
    solve()

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

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()))
print(sum([min(k-xi, xi) for xi in x])*2)

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

Solution (Python):
a = int(input())
k = int(input())
ar = list(map(int,input().split(" ")))
count = 0
for r in ar:
    if abs(0-r) >= abs(r-k):
        count += abs(r-k)
    else:
        count += abs(0-r)
print(count * 2)

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

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

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

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

print(count)

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

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

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

R = 0

for x in X:
    R += min(x, K - x)

print(2 * R)

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

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

for item in li:
        total += 2* min(item , k-item)

print(total)

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

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

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



listy=[]

for i in range(n):
    if int(x[i]) < k-int(x[i]):
        listy.append(int(x[i]))
    else:
        listy.append(k-int(x[i]))
    
print(2*sum(listy))

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

ans = 0
for x in xx:
    if k-x > x:
        ans += x*2
    else:
        ans += (k-x)*2
print(ans)

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

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

total = 0

for i in range(len(xs)):
    bx = xs[i]
    by = i + 1
    v1 = bx * 2
    v2 = abs(K - bx) * 2
    v = v1 if (v1 < v2) else v2 
    total += v
    
print(total)

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

Solution (Python):
input()
k = int(input())
ax = [int(s) for s in input().split()]
s = 0

for a in ax:
  s += min(a, abs(k - a))

print(2 * s)

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

ans = 0
for i in range(n):
    ans += min(abs(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 x in X:
    a = x * 2
    b = (K - x) * 2
    ans += min(a, b)
print(ans)

Solution (Python):


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

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):
from sys import stdin


def main() -> None:
    n, k = [next_int() for _ in range(2)]
    a = b = 0
    for i in range(n):
        xi = next_int()
        if xi <= k // 2:
            a += xi
        else:
            b += k - xi
    print((a + b) * 2)


def next_int() -> int:
    return int(next_str())


def next_str() -> str:
    result = ""
    while True:
        tmp = stdin.read(1)
        if tmp.strip() != "":
            result += tmp
        elif tmp != '\r':
            break
    return result


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(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 xi in x:
    ans += min(2*(xi), 2*(K-xi))
print(ans)

Solution (Python):
n = int(input())
k = int(input())
mid = k/2
date = list(map(int,input().split()))
ans = []
for i in date:
  if i <= mid:
    ans.append(i*2)
  else:
    ans.append(abs(i-k)*2)
print(sum(ans))

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

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

print(ans)

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