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

x_L = sorted(x_L)

center = int(k/2)

a_L,b_L = [],[]

for i in x_L:
    if i <= center:
        a_L.append(i)
    else:
        b_L.append(i)

a_dis,b_dis = 0,0

for i in a_L:
    a_dis += 2*i
for i in b_L:
    b_dis += 2*(k-i)



print(a_dis+b_dis)

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

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

Solution (Python):
a=int(input())
b=int(input())
ballist=list(map(int,input().split()))
dist=0
for t in ballist:
    dist+=min(abs(t),abs(t-b))
print(dist*2)

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

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

print(ans)

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

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

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

ans=0 


for y in range(N):
    
    lenA=abs((x[y]-0))*2 
    
    lenB=abs((x[y]-K))*2 
    
    if lenA<=lenB:
        ans+=lenA
    else:
        ans+=lenB

print(ans)

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

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

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 io
import math

nim = int(input())
mike = int(input())
data = sorted(map(int,input().split()))
result = 0
for i in data:
    result+=min(2*i,2*abs(mike-i))
print(result)

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

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

Solution (Python):
def main():
    N = int(input())
    K = int(input())
    x_list = list(map(int, input().split()))
    ans = 0
    for x in x_list:
        ans += 2 * min(x, abs(x - K))
    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 :
    ans += min(abs(i-k),i)*2

print(ans)

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

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

print(cnt)

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

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

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

print(sumL)

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

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


if __name__ == '__main__':
    main()

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

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

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



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

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

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

print(ans)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

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

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

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

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

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

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

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

print(d)

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

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

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

Solution (Python):
import math

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

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

  print(ans)
  return

if __name__ == "__main__":
  resolve()

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

import math
ans = []
for i in range(N):
    a = math.sqrt((x[i] - 0) ** 2) * 2
    b = math.sqrt((x[i] - K) ** 2) * 2
    ans.append(min(a, b))
print(round(sum(ans)))

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

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

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,k,*x=map(int,open(0).read().split())

ans=[]
for i in x:
    tmp=2*min(i-0,k-i)
    ans.append(tmp)

print(sum(ans))

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

n=int(input().rstrip())
k=int(input().rstrip())
x=[int(i) for i in input().split()]
ans=0

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

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
k = ni()
x = na()

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

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

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

Solution (Python):




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

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

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

print(ans)

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

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

print(ans)

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


if __name__ == '__main__':
    main()

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

print(ans)

Solution (Python):
import sys
from collections import deque
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
K = I()
a = IL()

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

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

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

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

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

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

Solution (Python):
import sys
def solve():
    input = sys.stdin.readline
    N = int(input())
    K = int(input())
    X = [int(x) for x in input().split()]
    ans = 0
    for x in X: ans += min(x, K - x) * 2
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
K = int(input())
res = 0
a = list(map(int, input().split()))
for x in a:
  res += min(abs(x - K), x) * 2
print(res)

Solution (Python):







from itertools import product,permutations,combinations,combinations_with_replacement






mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

def main():
    n = int(input())
    k = int(input())
    x = readInt()
    ans = 0
    for i in range(n):
        ans += min(x[i],abs(x[i]-k)) * 2
    print(ans)
    return

if __name__=='__main__':
    main()

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

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


if __name__ == '__main__':
    main()

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

sumX = 0
for y in x:
    if K / 2 < y:
        sumX += (K - y) * 2
    else:
        sumX += y * 2
print(sumX)

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

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

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

print(ans)

Solution (Python):
def main():
    N = int(input())
    K = int(input())
    balls = [int(a) for a in input().split()]

    d = 0
    for i in range(len(balls)):
        d += 2*min( balls[i] , K - balls[i])
    print(d)
main()

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

distance = 0

for i in range (0, N):
	if abs(L[i]-K) > L[i]:
		distance+=L[i]
	else:
		distance+=abs(L[i]-K)

print(2*distance)

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

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

    print(ans)

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

ans = 0

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

print(ans)

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

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

distance = 0

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

print(distance)

Solution (Python):

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

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

Solution (Python):












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

anslis = []
for i in arrA:
    num = min(i,K-i)
    anslis.append(num)

ans = sum(anslis)*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(x[i]-K))*2
print(ans)

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

answer = 0

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

print(answer)

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

Solution (Python):
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
MOD = 10**9+7
import itertools
import math

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

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

for i in x:
    a = i*2
    b = (K-i)*2
    if a>=b:
        cnt += b
    else:
        cnt += a
print(cnt)

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):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
k = INT()
x = LIST()
ans = 0
for xx in x:
    ans += min(k - xx, xx) * 2
print(ans)

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

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

print(ans)

Solution (Python):

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

def min_dist(x, K):
    return min(x, abs(K-x))

ans = 0
for v in x:
    ans += min_dist(v, K) * 2

print(ans)

Solution (Python):

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

ans = 0
for i in X:
    if i < center:
        ans += 2*i
    else:
        ans+= 2*(K-i)
print(ans)

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

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

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

Solution (Python):

n = int(input())
k = int(input())
a = list(map(int, input().split()))
ans = 0

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

print(ans)

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

Solution (Python):
def myAnswer(N:int,K:int,X:list):
   ans = 0
   for x in X:
      ans+= min(abs(0 - x)*2, abs(K - x)*2 )
   return ans

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   K = int(input())
   X = list(map(int,input().split()))
   print(myAnswer(N,K,X[:]))


if __name__ == '__main__':
   main()

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

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

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

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

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

Solution (Python):
import numpy as np
N = int(input())
K = int(input())
x = np.array(list(map(int, input().split())))

print(np.sum(np.minimum(x, np.abs(x - K))) * 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])

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

print(ans)

Solution (Python):
ans = 0
n, k = (int(input()) for _ in range(2))
l = list(map(int, input().split()))

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

Solution (Python):

N = int(input())
K = int(input())
x = map(int, input().split())
d = 0
for i in x:
    if i > K/2:
        d += (K - i) *2
    else:
        d += i*2
print(d)

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

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

Solution (Python):
n = int(input())
k = int(input())
list_X = list(map(int, input().split()))
ans = 0
for num in list_X:
    ans += min(abs(num),abs(num-k))

print(ans*2)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	n = II()
	k = II()
	x = LI()
	ans = 0
	for xx in x:
		ans += min(xx, abs(k-xx))*2
	print(ans)


if __name__ == '__main__':
	main()

Solution (Python):

N=int(input())
K=int(input())
def operation(x):
  if x<=K/2:
    return x*2
  else:
    return (K-x)*2
    
x=input().split(" ")
sum=0
for i in range(N):
  sum+=int(operation(int(x[i])))
print(sum)

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline


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 -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 __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


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 lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


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

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

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

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

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

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

print(result)

Solution (Python):

import math



import collections
n = int(input())


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

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

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

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

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

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

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

for xi in X:
    A2x = xi * 2
    B2x = abs(K - xi) * 2
    ans += min(A2x, B2x)

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

Solution (Python):
N = int(input())
K = int(input())
X = [int(x) for x in 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 range(N):
    ans += 2*min(abs(x[i]-K), x[i])

print(ans)

Solution (Python):


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

ans = 0
for x in x_list:
    K_x = K - x

    if K_x >= x:
        ans += x*2

    elif K_x < x:
        ans += K_x*2

print(ans)

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

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

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

answer=0
for val in x:
    if val <= K//2:
        answer+=val*2
    else:
        answer+=(K-val)*2
print(answer)

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

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

print(ans)

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

Solution (Python):












mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

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 += min(abs(i)*2, abs(i-K)*2)

print(ans)

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