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()))
Y = [i for i in range(1, N+1)]
ans = 0

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

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
K = int(input())
X = list(map(int,input().split()))
distance = 0
for n in range(N):
  if X[n] < K/2:
    distance += X[n]
  else:
    distance += K - X[n]
print(distance*2)

Solution (Python):
def int_list(sepalate=" "): return list(map(int, input().split(sepalate)))


def solve1(n, k, x):
    ans = 0

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

    return ans


n = int(input())
k = int(input())
x = int_list()
print(solve1(n, k, x))

Solution (Python):

import sys


def solve(N: int, K: int, x: "List[int]"):
    return sum(min(xx, abs(xx-K)) for xx in x) * 2



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)]  
    print(solve(N, K, x))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

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

Solution (Python):
from typing import List


def answer(n: int, k: int, xs: List[int]) -> int:
    moving_distance = 0
    reference_value = k / 2
    for x in xs:
        if x <= reference_value:
            moving_distance += x * 2
        else:
            moving_distance += (k - x) * 2

    return moving_distance


def main():
    n = int(input())
    k = int(input())
    xs = list(map(int, input().split()))
    print(answer(n, k, xs))


if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
k = I()
x = LI()







mov = 0

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

print(mov)

Solution (Python):
N = int(input())
K = int(input())
lst = input().split()

ans = 0

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

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

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

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

Solution (Python):
n,k=[int(input()) for i in range(2)]
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())
X = list(map(int, input().split()))

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

Solution (Python):

import sys


def solve(N: int, K: int, x: "List[int]"):
    res = 0
    for x_ in x:
        res += 2*min(x_, abs(K-x_))
    print(res)
    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())
L = list(map(int,input().split()))
ans = 0
for l in L:
  a = l
  b = abs(k-l)
  ans += min(a,b)*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]-0, K-x[i])
print(ans*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],abs(x[i]-k))
print(ans*2)

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

import sys


def solve(N: int, K: int, x: "List[int]"):
    ans = 0
    for i in range(N):
       ans += min(x[i], abs(K - x[i])) * 2
    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):
def resolve():
    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)
    
resolve()

Solution (Python):
with open(0) as f:
    N, K, *X = map(int, f.read().split())
print(2*sum(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 x in X:
  ans += 2 * (x if x <= K - x else 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(map(int,input().split()))
print(sum(2*min(y,K-y) for y in x))

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

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

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

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

print(calc)

Solution (Python):
import math

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

dis = math.ceil(k/2)
for i in x:
    if i == k:
        continue
    elif i <= dis:
        cnt += i*2
    else:
        cnt += (k-i)*2
print(cnt)

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:
    Y=[]
    Y.append(2*abs(i))
    Y.append(2*abs(i-K))
    ans+=min(Y)
    
print(ans)

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

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

print(cnt)

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

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

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, K, *X = map(int, open(0).read().split())

ans = 0
for y,x in enumerate(X, start=1):
    a = x * 2
    b = (K - x) * 2
    ans += min(a, b)

print(ans)

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

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

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

Solution (Python):

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

sum_distance = 0

for x in x_list:
    if x < K - x:
        sum_distance += 2*x
    else:
        sum_distance += 2*(K-x)

print(sum_distance)

Solution (Python):
N = input()
K = int(input())
l = input().split()
         

         
ans =0 
for i in l:
	s = int(i)
	ans += min(s,K-s)
print(ans*2)

Solution (Python):

import sys
input=sys.stdin.readline

def main():
    data=[]
    n = int(input())
    k = int(input())
    data=list(map(int,input().split()))
    length=0

    for ball in data:
        if ball < k-ball:
            length+=2*ball
        else:
            length+=2*(k-ball)
    print(length)

if __name__=="__main__":
    main()

Solution (Python):
n = int(input())
k = int(input())
x_l = list(map(int, input().split()))
th = k/2

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

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], abs(x[i]-k))

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):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

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

Solution (Python):
n = int(input())
k = int(input())
x = [int(x.strip()) for x in input().split()]
ans = 0
for i in x:
  if abs(i-k) > i:
    ans = ans + i
  else:
    ans = ans + abs(i-k)
ans += ans
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())
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):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

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

for p in x:
  a = 2*p
  b = 2*abs(K-p)
  if a >= b:
    sum_min += b
  elif a < b:
    sum_min += a

print(sum_min)

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

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

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

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

ans = 0
for i in x:
    if i < k - i:
        ans += i
    else:
        ans += k - i

print(2 * ans)

Solution (Python):
N = int(input(""))
K = int(input(""))
a = input("").split(" ")
a = [int(aa) for aa in a]
ans = 0
for x in a:
    if (K-x) < x:
        ans += (K-x)*2
    elif (K-x) >= x:
        ans += x*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)*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(abs(x[i] - 0), abs(K - x[i]))
print(ans * 2)

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]),abs(k-x[i]))
  ans+=m

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

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

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

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

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

print(ans)

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

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

dist_sum = 0
for i in range(N):
	y = i + 1
	x = xs[i]
	
	if x > K - x:
		dist_sum += (K - x) * 2
	else:
		dist_sum += x * 2
		
print(dist_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]-0), abs(x[i]-k))
print(ans*2)

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

s = 0
xs = list(map(int, input().split()))
for x in xs:  
  if x < abs(k-x):
    s += 2 * x
  else:
    s += 2 * abs(k-x)
    
print(s)

Solution (Python):
"""Boot-camp-for-Beginners_Easy009_B_Collecting-Balls-(Easy-Version)_28-August-2020.py"""

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

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

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

distance = 0

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

print(distance)

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

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

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

result = 0

for pos in X:
    if pos <= abs(pos - K):
        result += 2 * pos
    else:
        result += 2 * abs(pos - K)

print(result)

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

Solution (Python):
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    ans = 0
    for i,item in enumerate(x):
        a = item
        b = abs(item-k)
        ans += min(a,b)
    ans *= 2
    print(ans)
    return

if __name__=='__main__':
    n,k = I(),I()
    x = list(IL())
    solve()

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

distance = 0

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

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

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

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

sum = 0

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

print(sum)

Solution (Python):
n,k,*x=map(int,open(0).read().split())
print(sum((2*(k-i),2*i)[k-i>i] for i in x))

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

ans = 0

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

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

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

print(sum)

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

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

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

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

for i in range(1,n+1):
    k1 = 0
    k2 = 0
    k1 = abs(x[i-1]-0)*2
    k2 = abs(k-x[i-1])*2
    t += min(k1, k2)

print(t)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())
k = int(input())
x = [0] + list(map(int, input().split()))

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

print(ans)

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

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

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    K = I()
    x = LI()

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

if __name__ == '__main__':
    resolve()

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

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

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

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

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

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

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

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

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

print(ans*2)

Solution (Python):
def main():
    _ = int(input())
    k = int(input())
    x = map(int, input().split())
    print(sum([min([abs(xi - point) for point in [0, k]]) * 2 for xi in x]))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    _ = int(input())
    k = int(input())
    x = map(int, input().split())
    print(sum([min([abs(xi - point) for point in [0, k]]) * 2 for xi in 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(1, n + 1):
    ans += min(2 * abs(X[i - 1]), 2 * abs(X[i - 1] - k))
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],abs(K-L[i]))
print(ans*2)

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

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

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

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

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

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

Solution (Python):
N,K = (int(input()) for T in range(0,2))
X = [int(T) for T in input().split()]
print(sum(2*min(TX,K-TX) for TX in X))

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

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

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

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

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

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

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


def diff_ball_K(N, K):
    return [N - K]


ball_K = list(map(lambda x: K - x, X))



diff = [min(a,b) for a,b in zip(X, ball_K)]
print(sum(diff)*2)

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

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

Solution (Python):
n = int(input())
k = int(input())
l = list(map(int, input().split()))
s = 0
for i in l:
  s += min(i, k - i)

print(2*s)

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

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

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

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] - 0 , abs(x[i]-k))
print(ans)

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

distance = []
for x in X:
    if x < abs(K-x):
        distance.append(x)
    else:
        distance.append(abs(K-x))

print(2*sum(distance))

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

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

print(score)

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

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

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

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

Solution (Python):

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

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

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

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

Solution (Python):
import math


def resolve():
    import sys
    input = sys.stdin.readline
    
    n = int(input().rstrip())
    k = int(input().rstrip())
    x_list = [int(x) for x in input().rstrip().split(" ")]

    distance = 0
    for x in x_list:
        distance += (x if x < abs(k-x) else abs(k-x)) * 2


    print(distance)

if __name__ == "__main__":
    resolve()