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


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

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

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(N):
    sum += 2*min(abs(x[i]), abs(x[i]-K))

print(sum)

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

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

def ans(num):
    print(num * 2)

def main():
    accumulation = 0
    for x in X_MAP:
        distance = min(x, K - x)
        accumulation += distance
    ans(accumulation)

if __name__ == '__main__':
    main()

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

print(sum)

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

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

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

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

print(sum)

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

Solution (Python):
from sys import stdin

def solve(K, xs):
  return sum([2 * min(x, K - x) for x in xs])

def splitToInt(line):
  words = line.strip().split()
  return [int(w) for w in words]

N = int(stdin.readline())
K = int(stdin.readline())
xs = splitToInt(stdin.readline())
if len(xs) != N: raise
print(solve(K, xs))

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

Solution (Python):
import numpy as np

N = int(input())
K = int(input())
x = [_ for _ in 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()))

HalfK = K/2.0

Ans = 0

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

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

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

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

print (res)

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

res = 0

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

print(res)

Solution (Python):


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

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

print(d)

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

print(d)

Solution (Python):





def main():
    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)

if __name__ == "__main__": main()

Solution (Python):

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

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

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

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

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

print(ans)

Solution (Python):
n, k = [int(input()) for _ in range(2)]
xi = [int(i) for i in input().split()]
dist = 0
for x in xi:
    l, m = [abs(k-x), abs(x)]
    dist += l*2 if l<m else m*2

print(dist)

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

print(ans)

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

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

ans = 0
for i in range(N):
    if(abs(K-x[i]) >= x[i]):
        ans += x[i]
    else:
        ans += abs(K-x[i])

print(ans*2)

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

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

print(sum)

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

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

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    K = int(input())
    xs = [int(s) for s in input().split(' ')]
    res = 0
    for i in range(N):
        res += min(xs[i], abs(K - xs[i])) * 2
    print(res)

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

Solution (Python):
def calc_dist(N, K, x):
    dists = [2 * min(K - x_i, x_i) for x_i in x]
    return sum(dists)

if __name__ == "__main__":
    N = int(raw_input())
    K = int(raw_input())
    x = map(int, raw_input().split())
    print(calc_dist(N, K, x))

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


for i in range(N):
	dist = []
	for Rtype in ['A','B']:
		if Rtype == 'A':
			dist.append(abs(X[i] - 0)*2)
		if Rtype == 'B':
			dist.append(abs(X[i] - K)*2)
	Total_dist += min(dist)

print(Total_dist)

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

x = input().split()

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

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

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

Solution (Python):
N = int(input())
K = int(input())
xstr = input().split()
d = 0
for i in xstr:
    xi = int(i)
    dK = K-xi
    if dK < xi:
        d += dK
    else:
        d += xi
print(d*2)

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

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

print(ret)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


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

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

if __name__ == '__main__':
    main()

Solution (Python):
NUM = int(input())
K = int(input())
POS = list(map(int, input().split()))
sum = 0
for i in POS:
    a = i * 2
    b = abs(i - K) * 2
    sum += min(a, b)
print(sum)

Solution (Python):

N = int(input())

K = int(input())

balls_x = input().split(' ')

sum = 0

for ball_x_str in balls_x:

    ball_x  = int(ball_x_str)

    sum += min(ball_x, K - ball_x) * 2

print(sum)

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

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

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

print(ans)

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

count = 0

for b in balls:
    if (2 * b < K):
        count += 2 * b
    else:
        count += 2 * (K - b)

print(count)

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

res = 0
for t in x:
  res += 2*min(K-t, t)

print(res)

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

Solution (Python):
import math

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

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

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

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

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

sumx = sum(min(k-xx, xx) for xx in x)

print(sumx * 2)

Solution (Python):
n = int(raw_input())
k = int(raw_input())
xl = map(int, raw_input().split())
total = 0
for x in xl:
    if x > k / 2:
        total += (k - x) * 2
    else:
        total += x * 2
print total

Solution (Python):

n=int(raw_input())
k=int(raw_input())
x=map(int,raw_input().split())
res=0
for i in x:
 res+=2*min(i,k-i)
print res

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

Solution (Python):

"""B - Collecting Balls (Easy Version)"""

n = int(raw_input())
k = int(raw_input())
x_list = map(int, raw_input().split())

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

print ret

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

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

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

if __name__ == "__main__":
    main()

Solution (Python):
import sys

def main():
    N = int(input())
    K = int(input())
    l = input().split() 

    result = 0

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

    print(result)


if __name__=='__main__':
    main()

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

Solution (Python):
n=int(input())
k=int(input())
x=input().split()
l=0

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

Solution (Python):

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

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

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

for a in range(len(x)):
    if k/2 >= x[a]:
        s += x[a]*2
    else:
        s +=  (k-x[a])*2

print(s)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    n = I()
    k = I()
    a = LI()
    r = 0
    for c in a:
        r += min(abs(k-c),c) * 2

    return r


print(main())

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()))
onewaysum=0
for i in range(N):
    if x[i]<=K-x[i]:
        onewaysum+=x[i]
    else:
        onewaysum+=K-x[i]
print(onewaysum*2)

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

Solution (Python):
if __name__ == '__main__':
    N = input()
    K = input()
    x = map(int, raw_input().split())
    sum = 0
    for d in x:
        if (K-d < d):
            sum += 2*(K-d)
        else:
            sum += 2*d

    print sum

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

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

print(ans)

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

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

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

print(ans)

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

total = 0
for each in x:
    A = each
    B = abs(each - K)
    if(A < B):
        total += (A * 2)
    else:
        total += (B * 2)

print(total)

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

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

print(sum_)

Solution (Python):
from sys import stdin
ans = 0
n = int(stdin.readline())
k = int(stdin.readline())
a = map(int,stdin.readline().split())
for i in a:
 ans += min(i,k-i)
print 2*ans

Solution (Python):
import sys

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

z = [2 * (K - i) if i > K/2 else 2 * i for i in x]
print(sum(z))

Solution (Python):
N = int(input())
K = int(input())
Ball = list(map(int,input().split()))
sumA = 0
sumB = 0

def SUMa(i):
    sumA = Ball[i]*2
    return sumA
def SUMb(i):
    sumB = (K-Ball[i]) *2
    return sumB

goukei = 0
for i in range(0,N):
    if SUMa(i) > SUMb(i):
        goukei += SUMb(i)
    else:
        goukei += SUMa(i)

print(goukei)

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

xi = input().split()


if N != len(xi):
    print('err')
    exit()

sum = 0

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

print(sum)

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

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

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

Solution (Python):
import sys




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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    n = read_int()
    k = read_int()
    x = read_int_list()

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


main()

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

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

Solution (Python):
N = int(input())
K = int(input())
X = [(int(j), i+1) for i, j in enumerate(input().split())]
A = [(0, a) for a in range(N+1)]
B = [(K, b) for b in range(N+1)]
cost = 0

for i,x_i in enumerate(X):
	cost += min(abs(x_i[0] - A[i][0])*2, abs(x_i[0] - B[i][0])*2)

print(cost)

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

Solution (Python):


N = int(input())
K = int(input())
ans = 0

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

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

print(ans)

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

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

print(sum)

Solution (Python):
import math

def main():
  N = int(input())
  K = int(input())
  x_i = input().split()
  x_i = [ int(x) for x in x_i ]
  ans = 0

  for i in range(0, N):
    ans += 2 * min(math.fabs(x_i[i] - 0), math.fabs(x_i[i] - K)) 

  print(int(ans))

if __name__ == '__main__':
  main()

Solution (Python):
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, = read_line(int)
k, = read_line(int)
xs = [int(x) for x in input().split()]

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

Solution (Python):
import math
N,K=input(),input()
x = [int(_) for _ in raw_input().split()]
sum = 0

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

Solution (Python):



N = int(raw_input())
K = int(raw_input())
x = raw_input().split()
x = map(int, x)

n = 0

for i in xrange(N):
    if x[i] < K - x[i]:
        n = n + x[i] * 2
    else:
        n = n + (K - x[i]) * 2
print n

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

Solution (Python):
def main():
    """
    ball: N
    ball position: (X, Y) -> (x_i, i)
    robot A: N
    robot B: N
    A position: (X, Y) -> (0, i)
    B position: (X, Y) -> (K, i)

    A pattern: (0, a) if ball pos eq (xx, a)
    B pattern: (K, b) if ball pos eq (xx, b)
    """
    N = int(input())
    K = int(input())
    xs = map(int, input().split())

    mov_dist = 0
    for y, x in enumerate(xs, 1):
        a_dist = x
        b_dist = abs(K - x)
        dist = min(a_dist, b_dist)

        mov_dist += dist * 2

    print(mov_dist)

if __name__ == '__main__':
    main()

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

Solution (Python):




def main():
    n = int(input())
    k = int(input())
    xs = [int(x) for x in input().split()]
    move = 0
    for x in xs:
        move += 2 * min(x, abs(k-x))
    print(move)

if __name__ == '__main__':
    main()

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

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

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

for i in range(N):
    A = abs(data[i])
    B = abs(K - data[i])
    if A < B:
        count += A*2
    else:
        count += B*2

print(count)

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

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

A=0
B=0
C=0

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

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

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

a = 0
for i in x:
    if float(i) <= (K / 2):
        a += i * 2
    elif float(i) > (K / 2):
        a += (K - i) * 2

print(a)

Solution (Python):
N, K, A = int(input()), int(input()), [int(v) for v in input().split()]
R = 0
for a in A:
  Na = abs(0-a)
  Ka = abs(K-a)
  r = Na if Na <= Ka else Ka
  R += r * 2

print(R)

Solution (Python):
import numpy as np
N = int(input())
K = int(input())
x = np.fromiter(map(int, input().split()), dtype=np.int)
A = x
B = np.abs(x - K*np.ones(N))
C = np.minimum(A,B)
print(int(C.sum()*2))

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

print(ans*2)

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

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

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

Solution (Python):
N=int(raw_input())
K=int(raw_input())
x=map(int,raw_input().split(' '))

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

print s

Solution (Python):
r = lambda : map(int, raw_input().split())

n = r()[0]
k = r()[0]
a = r()

tot = 0 

for x in a :
    val = min(x*2, abs(k-x)*2)
    tot += val
    
print tot

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

x=map(int, raw_input().split())

l=0
for i in range(len(x)):

	if x[i] < abs(K-x[i]):
		l+=x[i]*2
	else:
		l+=abs(K-x[i])*2

print l

Solution (Python):


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

sum = 0

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

print(sum*2)

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

ans = 0

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

print ans

Solution (Python):


N=int(input().rstrip())
K=int(input().rstrip())
xi=map(int,input().rstrip().split())

idoSum=0

for x in xi:
    roboA=x*2
    roboB=abs(K-x)*2
    idoSum+=( roboB if( roboB < roboA) else roboA )
print(idoSum,flush=True)

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

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

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

print(ans)

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

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

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

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

    print(sum)
    
    
solve()

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

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

Solution (Python):


n = input()
k = input()

x = map(int, raw_input().split())
t = 0

for d in x:
    d_z = abs(d)
    d_k = abs(k-d)
    if d_z > d_k:
        t += d_k * 2
    else:
        t += d_z * 2

print t

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

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

print(ans)

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

Solution (Python):
def main():
    N = int(input())
    K = int(input())
    
    x = [int(i) for i in input().split()]
         
    
    
    
    
    sum_dis = 0
    for pos in x:
        min_dis = min(pos, K - pos) * 2
        sum_dis += min_dis
        
    print(sum_dis)
    
if __name__=="__main__":
    main()

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

for i in range(0, len(int_split_x)):
    if abs(K - int_split_x[i]) > abs(0 - int_split_x[i]):
        result += (abs(0 - int_split_x[i]))*2
    else:
        result += (abs(K - int_split_x[i]))*2

print(result)

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

if __name__=='__main__':
    main()

Solution (Python):


import sys

def main(argv):
    line = sys.stdin.readline()

    while line:
        N = int(line)
        K = int(sys.stdin.readline())
        X = map(int, sys.stdin.readline().rstrip("\n").split(" ", N))

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

        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Solution (Python):
def main():
    N = int(input())
    K = int(input())
    X = tuple(map(int, input().split()))

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

main()

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

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

x = []
for i in range(N):
	x.append(int(data[i]))

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

print(sum)

Solution (Python):

"""
https://beta.atcoder.jp/contests/abc074/tasks/abc074_b


"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    N = int(input())
    K = int(input())
    balls = [int(x) for x in input().split()]

    total_step = 0
    for b in balls:
        step_a = 2 * b
        step_b = 2 * abs(K - b)
        step = min(step_a, step_b)
        total_step += step

    print(total_step)


if __name__ == '__main__':
    main(sys.argv[1:])

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

Solution (Python):
def read_int_list():
    return list(int(i) for i in input().split())

n = int(input())
k = int(input())
x = read_int_list()

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

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

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

print(S)

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

def Minl(x, K):
  l = abs(K - x)
  if l < x:
    return 2 * l
  else:
    return 2 * x

ans = 0
for x in xlist:
  ans = ans + Minl(x, K)

print(str(ans))

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

s = 0

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

print(s)

Solution (Python):

import math

N = int(input())
K = int(input())
X = list(map(int, input().split(' ')))
ans = 0
for i in range(N):
    dis1 = X[i]
    dis2 = X[i]-K
    if dis2 < 0:
        dis2 = -dis2
    ans += min(dis1, dis2)*2
print(ans)