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:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left

#======================================================#

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

N = int(input())
K = int(input())
x = list(multi_input())

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

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

print(answer)

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
K = int(input())
xn = list(map(int,input().split()))
ans = 0
for x in xn: 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()))
result = 0
def A(x):
    disA = 0
    disA = 2 * x
    return disA
def B(k, x):
    disB = 0
    disB = 2 * (k -x)
    return disB

for i in X:
    if A(i) <= B(K, i):
        result += A(i)
    else:
        result += B(K, i)
print(result)

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

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

Submission status: Accepted

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

ans = 0
for x in a:
    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())))
xa = [2*x[j] for j in range(n)]
l = 0
xb = [2*abs(x[i]-k) for i in range(n)]

for k in range(n):
    if xa[k] >= xb[k]:
        l += xb[k]
    else:
        l += xa[k]

print(l)

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

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

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
data = input().split(' ')
x_list = [int(x) for x in data]
ans = 2 * sum([min(x, k-x) for x in x_list])
print(ans)

Submission status: Accepted

Solution code:
# ABC074B

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

Submission status: Accepted

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

ans = 0
for one in x:
    ansOne = min([one, K - one])
    ans += ansOne * 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

r = 0
for x in X:
    if x > K/2:
        r += (K-x)*2
    else:
        r += x*2

print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

Submission status: Accepted

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

total = 0

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

print(total)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#N,K = map(str,input().split())
N = int(input())
K = int(input())
X = list(map(int,input().split()))
c=0
for i in range(len(X)):
    if abs(X[i]-0) < abs(X[i]-K):
        c += X[i]*2
    else:
        c += (K-X[i])*2
print(c)

Submission status: Accepted

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

res = 0
for i in range(len(x_n)):
    if x_n[i] > (k / 2):
        res = res + abs(x_n[i] - k) * 2
    else:
        res = res + 2 * x_n[i]

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

n = int(input())
k = int(input())
x = [int(val) for val in input().split()]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def readlines(n):
    for i in range(n):
        for j, x in enumerate(input().split()):
            yield int(x), (i, j)


def main():
    N = int(input())
    K = int(input())

    for x in (map(int, input().split())):
        yield min(x, K - x) * 2

print(sum(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+=min(abs(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()))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

            

if '__main__' == __name__:
    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(2*x[i], 2*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 i in range(len(x)):
  f = k - x[i]
  if(f>x[i]):
    ans += 2*x[i]
  else:
    ans += 2*f
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 += min(x,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 x in X:
    ans += 2*min(abs(x-k),x)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

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

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
K=int(input())
x=[int(i) for i in input().split()]
cnt=0
for i in x:
    if i<abs(K-i):
        cnt+=(2*i)
    else:
        cnt+=(2*abs(K-i))
print(cnt)

Submission status: Accepted

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

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

distance = []

for i in range(n):
  if abs(x[i] - 0) < abs(x[i] - k):
    distance.append(abs((x[i] - 0) * 2))
  else:
    distance.append(abs((x[i] - k) * 2))

print(sum(distance))

Submission status: Accepted

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

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

ans = 0

for i in range(len(x)):
     ans += min(x[i]*2,abs(x[i]-K)*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 X:
    A = i
    B =abs(i - K)
    if A >= B:
        count = count + 2*B
    else:
        count = count + 2*A
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 += 2 * min(X[i], K - X[i])
print(ans)

Submission status: Accepted

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

ans=0
for i in A:
    if i >=K/2:
        ans+=2*(K-i)
    else:
        ans+=2*i

print(ans)

Submission status: Accepted

Solution code:
N,K,count=input(),int(input()),0
for i in list(map(int,input().split())):
  if i < K-i:
    count+=i
  else:
    count+=K-i
print(count*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(abs(i-k),i)*2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

count = 0
for _, v in enumerate(X):
    count = count + min(v * 2, (K - v) * 2)

print(count)

Submission status: Accepted

Solution code:
ball_n = int(input())
robbotb_x_position = int(input())
ball_x_positions= map(int, input().split())


result = 0
for ball_x_position in ball_x_positions:
    b_distance = abs(ball_x_position - robbotb_x_position)
    a_distance = ball_x_position
    if b_distance > a_distance:
        result += a_distance * 2
    else:
        result += b_distance * 2

print(result)

Submission status: Accepted

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

length = 0

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

print(length)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

print(count)

Submission status: Accepted

Solution code:
import sys

lines = sys.stdin.readlines()

N = int(lines[0])
K = int(lines[1])
X = lines[2].split(' ')
x =[]

y = 0
for i in range(N):
  x.append(int(X[i]))
  if 2*x[i] >= K:
      dist = 2*(K-x[i])
  else:
      dist = 2*x[i]
  y = y + dist

print(y)

Submission status: Accepted

Solution code:
N=int(input())
K=int(input())
L=list(map(int,input().split()))
ans=0
for i in L:
	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()))
ans = 0
for x in X:
    if K - x > x:
        ans += x *2
    else:
        ans += (K-x) *2
print(ans)

Submission status: Accepted

Solution code:
#ABC074-B
n = int(input())
k = int(input())
x = list(map(int,input().split()))
cnt = 0
for i in x:
    if i < k - i:
        cnt += 2*i
    else:
        cnt += 2*(k-i)
        
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(answer *2)

Submission status: Accepted

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

import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

c = 0

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7


def main():
  N = int(readline())
  K = int(readline())
  X = list(map(int, readline().split()))
  ans = 0
  for i in range(N):
    x = X[i]
    ans += 2 * min(x, K-x)
  print(ans)
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

for x in x_l:
    if x <= k-x:
        total += x*2
    else:
        total += (k-x)*2

print(total)

Submission status: Accepted

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


mid = k // 2
ans = 0
for i in range(n):
    d = min(nums[i], k - nums[i])
    ans += d * 2
print(ans)

Submission status: Accepted

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


mid = k // 2
t = 0
for i in range(n):
    if nums[i] < mid:
        t += nums[i] * 2
    else:
        t += (k - nums[i]) * 2

mid = (k + 1) // 2
ans = 0
for i in range(n):
    if nums[i] < mid:
        ans += nums[i] * 2
    else:
        ans += (k - nums[i]) * 2
ans = min(ans, t)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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_L,b_L)
#print(a_dis,b_dis)
print(a_dis+b_dis)

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=x[i]
    b=abs(x[i]-k)
    ans+=min(a,b)
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):
    a=2*(x[i])
    b=2*(abs(x[i]-k))
    ans+=min(a,b)
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]*2, (k-x[i])*2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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):
    #Aの動線
    lenA=abs((x[y]-0))*2 #ボールの位置とロボAの待機位置の差*2
    #Bの動線
    lenB=abs((x[y]-K))*2 #ボールの位置とロボBの待機位置の差*2
    #小さい方を答えとする
    if lenA<=lenB:
        ans+=lenA
    else:
        ans+=lenB

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
x = list(map(int,input().split()))
#ボールの座標 x[i],i
#Aの座標 x,i
#Bの座標 k,i
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)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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:
#!/usr/bin/env python3
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)

Submission status: Accepted

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

Submission status: Accepted

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

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(abs(x), abs(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(1,N+1):
  A = abs(X[i-1])
  B = abs(K-X[i-1])
  ans += 2*(min(A,B))
print(ans)

Submission status: Accepted

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