AtCoder Beginner Contest 074 - Collecting Balls (Easy Version)

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
10
2

-----Sample Output-----
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.

Difficulty: EASY

Starter code:

Solutions:
def mapt(fn, *args):
	return tuple(map(fn, *args))

def Input():
	return mapt(int, input().split(' '))

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


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


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


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


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)


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)


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)


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


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


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


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


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)


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


input()
k = int(input())
l = map(int, input().split())
print(sum((min(i, k - i) for i in l)) * 2)


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)


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


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


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


input()
k = int(input())
(*x,) = map(int, input().split())
print(sum((min(i, abs(k - i)) for i in x)) * 2)


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


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


input()
k = int(input())
s = 0
for a in map(int, input().split()):
	s += 2 * min(a, abs(k - a))
print(s)


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)


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


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


(n, k, *x) = map(int, open(0).read().split())
d = 0
for c in x:
	d += min(2 * c, 2 * abs(c - k))
print(d)


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)


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

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


s = 0
input()
k = int(input())
for x in input().split():
	x = int(x)
	s += min(abs(k - x), x) * 2
print(s)


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


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


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)


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


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)

def __starting_point():
	main()
__starting_point()


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


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


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


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


(_, k) = (input(), int(input()))
print(2 * sum((min(x, k - x) for x in map(int, input().split()))))


(n, k, *a) = map(int, open(0).read().split())
ans = 0
for i in a:
	ans += 2 * min(i, k - i)
print(ans)


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

class Solution:

	def __init__(self, n, k, xn):
		self.n = n
		self.k = k
		self.xn = xn

	def printout(self):
		print(n, k, xn)

	def answer(self):
		total = 0
		total = 0
		for xi in xn:
			a = abs(xi - 0)
			b = abs(xi - k)
			if a <= b:
				total += a * 2
			else:
				total += b * 2
		print(total)
conditions = Solution(n, k, xn)
conditions.answer()


n = int(input())
k = int(input())
x = [int(i) for i in input().split()]
print(sum(map(lambda a: min(a, k - a), x)) * 2)


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


N = int(input())
K = int(input())
ans = 0
l_n = list(map(int, input().split()))
for i in l_n:
	ans += min(i, K - i) * 2
print(ans)


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


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)


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

def __starting_point():
	main()
__starting_point()


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)


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)


n = int(input())
k = int(input())
ans = [min(int(i), k - int(i)) for i in input().split()]
print(sum(ans) * 2)


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)


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)


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


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)


n = int(input())
k = int(input())
xs = list(map(int, input().split()))
print(sum((min(x, k - x) * 2 for x in xs)))


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

def __starting_point():
	main()
__starting_point()


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


n = int(input())
k = int(input())
l = list(map(int, input().split()))
ans = 0
for m in l:
	ans += min([k - m, m]) * 2
print(ans)


N = int(input())
K = int(input())
X = input().split()
cnt = 0
for x in X:
	cnt += min(int(x), K - int(x))
print(cnt * 2)


(n, k, *xx) = map(int, open(0).read().split())
print(2 * sum((min(abs(x), abs(k - x)) for x in xx)))


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)


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


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)

def __starting_point():
	main()
__starting_point()


n = int(input())
k = int(input())
xn = map(int, input().split())
print(sum([min(x, k - x) * 2 for x in xn]))


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


N = int(input())
K = int(input())
A = map(int, input().split())
ans = [2 * min(a, K - a) for a in A]
print(sum(ans))


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)


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


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


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


with open(0) as f:
	(N, K, *X) = map(int, f.read().split())
print(2 * sum((min(x, K - x) for x in X)))


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)


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


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


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


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)


a = int(input())
b = int(input())
c = list(map(int, input().split()))
print(sum([min(i * 2, (b - i) * 2) for i in c]))


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


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


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


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)


(N, K, *X) = map(int, open(0).read().split())
print(sum((2 * min(x, K - x) for x in X)))


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)


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)


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)


N = int(input())
K = int(input())
X = [int(i) for i in input().split(' ')]
print(sum([2 * min((x, abs(K - x))) for x in X]))


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

def __starting_point():
	main()
__starting_point()


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)


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)


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)


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)


input()
k = int(input())
total = sum((min(int(tok), k - int(tok)) for tok in input().split()))
print(2 * total)


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


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)


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)


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


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


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)


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


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)


(N, K) = (int(input()), int(input()))
print(sum([i * 2 if i * 2 < (K - i) * 2 else (K - i) * 2 for i in list(map(int, input().split()))]))


input()
K = int(input())
y = list(map(int, input().split()))
g = 0
for x in y:
	g += min(x, abs(x - K))
print(2 * g)


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)


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


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


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


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)


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):

	def assertIO(self, input, output):
		(stdout, stdin) = (sys.stdout, sys.stdin)
		(sys.stdout, sys.stdin) = (StringIO(), StringIO(input))
		resolve()
		sys.stdout.seek(0)
		out = sys.stdout.read()[:-1]
		(sys.stdout, sys.stdin) = (stdout, stdin)
		self.assertEqual(out, output)

	def test_入力例_1(self):
		input = '1\n10\n2'
		output = '4'
		self.assertIO(input, output)

	def test_入力例_2(self):
		input = '2\n9\n3 6'
		output = '12'
		self.assertIO(input, output)

	def test_入力例_3(self):
		input = '5\n20\n11 12 9 17 12'
		output = '74'
		self.assertIO(input, output)

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

def __starting_point():
	resolve()
__starting_point()


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)


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


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


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


n = input()
K = int(input())
xs = map(int, input().split())
res = 0
for p in xs:
	res += min(p, K - p)
print(res * 2)


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


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)


N = int(input())
K = int(input())
xs = list(map(int, input().split()))
r = 0
for x in xs:
	r += 2 * min(x, K - x)
print(r)


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)


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


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)


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


input()
K = int(input())
ans = [min(x - 0, K - x) for x in list(map(int, input().split()))]
print(sum(ans) * 2)


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)

def __starting_point():
	resolve()
__starting_point()


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)


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)


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)


n = int(input())
k = int(input())
x = [min(int(j), k - int(j)) for j in input().split()]
print(2 * sum(x))


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


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)


n = int(input())
k = int(input())
x = map(int, input().split())
print(sum(map(lambda y: min(y, abs(k - y)) * 2, x)))


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


n = int(input())
b = int(input())
print(sum([min(int(i), abs(b - int(i))) for i in input().split()]) * 2)


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)


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)


input()
K = int(input())
print(sum([min(int(i), K - int(i)) * 2 for i in input().split()]))


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


N = int(input())
K = int(input())
x = map(int, input().split(' '))
s = 0
for y in x:
	s += min(y, abs(K - y)) * 2
print(s)


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)


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


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)


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


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


n = int(input())
k = int(input())
xl = list(map(int, input().split()))
l = [min(x * 2, (k - x) * 2) for x in xl]
print(sum(l))


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


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)


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)


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


n = int(input())
k = int(input())
print(sum([min(k - int(v), int(v)) for v in input().split()]) * 2)