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



Sample input:
1
10
2


Sample output:
4

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
K = int(input())
I = list(map(int,input().split()))
dis = 0

for i in I:
    a = i           #0からiまでの距離
    b = abs(K-i)    #Kからiまでの距離
    if a >= b:
        dis += 2 * b
    else:
        dis += 2 * a


print(dis)

Submission status: Accepted

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

Submission status: Accepted

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

cost=0
for i in range(n):
    cost+=min(abs(a[i]),abs(k-a[i]))

print(cost*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    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*abs(x[i])
        elif abs(x[i]) >= abs(K-x[i]):
            ans += 2*abs(K-x[i])
    print(ans)
resolve()

Submission status: Accepted

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

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

print(calcdistance(N, K, x))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

Solution code:
N=int(input())
K=int(input())
xlis=[int(x) for x in input().split(' ')]
sums=0
for i in range(N):
	ball=xlis[i]
	arob=ball*2
	brob=abs(K-ball)*2
	sums+=min([arob,brob])
print(sums)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# B - Collecting Balls (Easy Version)

N = int(input())
K = int(input())
X = list(int(x) for x in input().split())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    from builtins import int, map, list, print, min
    import sys
    sys.setrecursionlimit(10 ** 6)

    input = (lambda: (sys.stdin.readline()).rstrip())
    input_list = (lambda: input().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

m = 0

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

print(m*2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
k=int(input())
a=list(map(int,input().split()))
sum=0
for i in range(n):
  if a[i]<=k//2:
    sum+=a[i]
  else:
    sum+=k-a[i]
sum*=2
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(ans*2)

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
X = list(map(int, input().split()))
cnt = 0
for e in X:
    if e <= k // 2:
        cnt += e * 2
    else:
        cnt += (k - e) * 2
print(cnt)

Submission status: Accepted

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

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

print(ans*2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10

    N = II()
    K = II()
    X = LMIIS()
    ans = 0
    for x in X:
        ans += min(x,K-x)*2
    print(ans)
    
main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    k = ii()
    ball_place = lmi()
    total_dist = 0
    for x in ball_place:
        total_dist += min(x, abs(k - x)) * 2
    print(total_dist)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
X = list(map(int, input().split()))

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, X: "List[int]"):
    ans = 0
    for x in X:
        robo_a = abs(0 - x) * 2
        robo_b = abs(K - x) * 2
        ans += min(robo_a, robo_b)
    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, x)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[5]:


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


# In[7]:


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


# In[ ]:

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
K = int(input())
X = [int(k) for k in input().split()]
X = [-1] + X
# print(X)
result = 0

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

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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


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

res = 0

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

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

#############
# Functions #
#############

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

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

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

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

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

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

######Factorization######
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

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

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

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

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

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

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

#############
# Main Code #
#############
N = inputI()
K = inputI()
X = inputIL()

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

print(ans)

Submission status: Accepted

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

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

print(l)

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

d = 0

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

print(d)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(moveSum)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans=0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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


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

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

Submission status: Accepted

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

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

print(f(n, k, x))

Submission status: Accepted

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

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

print(s * 2)

Submission status: Accepted

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