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):


import sys






























def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    k = ii()
    ball_place = lmi()
    total_dist = 0
    for x in ball_place:
        total_dist += min(x, abs(k - x)) * 2
    print(total_dist)


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

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

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

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

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

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

print(sum)

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

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

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

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

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

Solution (Python):
N=int(input())
K=int(input())
l = input().split()
l=[int(s) for s in l]
sum=0

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

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

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

print(d)

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

Solution (Python):
n,k,*x = map(int, open(0).read().split())
print(sum(k-i if k/2 < i else i for i in x)*2)

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

sum = 0
for i in range(N):
	dist = min(X[i], abs(X[i] - K))
	sum += dist*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(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:
    d = min(i, K - i)
    ans += 2 * d

print(ans)

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

Solution (Python):

import sys


def solve(N: int, K: int, X: "List[int]"):
    ans = 0
    for x in X:
        robo_a = abs(0 - x) * 2
        robo_b = abs(K - x) * 2
        ans += min(robo_a, robo_b)
    print(ans)
    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 = sum([min(p, K-p)*2 for p in 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(2*x[i], 2*abs(k-x[i]))
print(ans)

Solution (Python):
N = int(input())
K = int(input())
X = [int(k) for k in input().split()]
X = [-1] + X

result = 0

for i in range(len(X)):
    if (i != 0):
        
        
        
        A = X[i]
        B = abs(K - X[i])
        
        
        
        if (A > B):
            result += B*2
        else:
            result += A*2

print(result)

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

all_length = 0
for pos in x:
    length = min(pos, K - pos)
    all_length += 2 * length
print(all_length)

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

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

print(ans)

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

ans = 0

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

print(ans)

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

Solution (Python):
def li():
    return list(map(int, input().split()))


def mi():
    return map(int, input().split())


N = int(input())
K = int(input())
x = li()

res = 0

for i in range(len(x)):
    res += min(x[i], K - x[i]) * 2

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

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

Solution (Python):

N = int(input())
K = int(input())
balls = list(map(int, input().split()))
mv = 0
border = (K-1) // 2
for ball in balls:
  if ball <= border:
    mv += ball * 2
  else:
    mv += abs((K - ball) * 2)
print(mv)

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, k = [int(readline()) for _ in range(2)]
x = [min(abs(int(x)-0), abs(int(x)-k))*2 for x in readline().split()]
print(sum(x))

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

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

ans = 0
for ball in balls:
    if (ball) < abs(K - ball):
        ans += ball * 2
    else:
        ans += abs(K-ball) *2

print(ans)

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

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

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def Yes(): print("Yes"); return
def No(): print("No"); return


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


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


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def Base_10_to_n(X, n):
  if X//n:
    return Base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def Base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N = inputI()
K = inputI()
X = inputIL()

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

print(ans)

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

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

print(l)

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

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

print(sum)

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

d = 0

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

print(d)

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

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

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

moveSum = 0
for x2 in x:
    move = x2 * 2
    if move > (K - x2) * 2:
        move = (K - x2) * 2
    moveSum += move

print(moveSum)

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

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

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

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

ans=0

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

print(ans)

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

Solution (Python):
def inp():
    return(int(input()))


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


n = inp()
K = inp()
X = inlt()

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

def f(n, k, x):
    rtn = 0
    for i in range(n):
        rtn += min(x[i], k-x[i]) * 2
    return rtn

print(f(n, k, x))

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

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

print(s * 2)

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):
import math
from collections import deque
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def combmod(n,r,mod):
	return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod
def printQueue(q):
	r=copyQueue(q)
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): 
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): 
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} 
def bitArr(n):
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): 
			k = i-a*j
			if k%b==0:
				return i
	return -1
def copyQueue(q):
	nq = queue.Queue()
	n = q.qsize()
	for i in range(n):
		x = q.get()
		q.put(x)
		nq.put(x)
	return nq

n = readInt()
k = readInt()
x = readInts()

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

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

Solution (Python):
import math

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

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

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

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

Solution (Python):
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())
  
n=i()
k=i()
x=intl()
a=0
for i in range(n):
	a+=min(2*x[i],2*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(N):
  ans += 2*min(abs(x[i]),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+=min(abs(x[i]-0)*2,abs(x[i]-k)*2)
print(ans)

Solution (Python):
import numpy as np
def main():
  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)
if __name__ == "__main__":
  main()

Solution (Python):

"""
Created on Thu May 14 11:31:25 2020

@author: shinba
"""
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())
X = list(map(int, input().split()))

line = K // 2
ans = 0

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

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

print(ans)

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

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

Solution (Python):
import sys

N = int(input())
K = int(input())
X = list(map(int, sys.stdin.readline().rsplit()))

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

print(res)

Solution (Python):


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

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

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

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

Solution (Python):
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0
n = iin()
k = iin()
x = [int(x) for x in input().split()]

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

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string,decimal
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def SS(): return map(str,sys.stdin.readline().rstrip().split())
def II(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
K = I()
x = LI()

ans = 0

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

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

result = 0
for x_ in x:
    result += min(x_, (k - x_))

print(2 * result)

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

ans = 0

for ball in ball_list:
    if abs(ball - 0) <= abs(ball - K):
        ans += abs(ball - 0) * 2
    else:
        ans += abs(ball - K) * 2

print(ans)

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

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

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

sum = 0

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

print(sum)

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

main()

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))




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

resolve()

Solution (Python):
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

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

A=[]
B=[]

for i in range(len(x)):
    a = min(2*(x[i]), 2*abs(K-x[i]))
    A.append(a)

print(sum(A))

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

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

print(ans)

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

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

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

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

Solution (Python):

import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**7)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]
INF = float('inf')
MOD = 10 ** 9 + 7
PI = math.pi

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

Solution (Python):
N = int(input())
K  = int(input())
li = list(map(int,input().split()))	
ans = []
for i in range(N):
    if abs(li[i]-0)>=abs(li[i]-K):
        ans.append(2*abs(li[i]-K))
    elif abs(li[i]-0)<abs(li[i]-K):
        ans.append(2*li[i])
print(sum(ans))

Solution (Python):
n = int(input())
k = int(input())
 
x = tuple([int(t)for t in input().split()]) 
 
ans = 0
for x_i in x:
  if k-x_i>x_i:
    ans+=x_i*2
  else:
    ans+=(k-x_i)*2
    
print(ans)

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

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

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

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

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

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

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

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

ans = 0

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

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

print(ans)

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, abs(k - x) * 2)

print(ans)