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:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())

K = int(input())

balls_x = input().split(' ')

sum = 0

for ball_x_str in balls_x:

    ball_x  = int(ball_x_str)

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

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

print(count)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sumx * 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""B - Collecting Balls (Easy Version)"""

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

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

print ret

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

def main():
    N = int(input())
    K = int(input())
    l = input().split() #N個のstrのリスト

    result = 0

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

    print(result)


if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(s)

Submission status: Accepted

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

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

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


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

    return r


print(main())

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

Submission status: Accepted

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

Submission status: Accepted

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

    print sum

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

print(total)

Submission status: Accepted

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

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

print(sum_)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

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

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

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

print(goukei)

Submission status: Accepted

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

xi = input().split()


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

sum = 0

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

print(sum)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('b1.in')


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


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


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


def read_str():
    return input()


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

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


main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
N = int(input())
K = int(input())
ans = 0

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

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

print(ans)

Submission status: Accepted

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

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

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

print(sum)

Submission status: Accepted

Solution code:
import math

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

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

  print(int(ans))

if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/user/bin/python
# coding: UTF-8

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

n = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

        mov_dist += dist * 2

    print(mov_dist)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(count)

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

Submission status: Accepted

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

A=0
B=0
C=0

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

Submission status: Accepted

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

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

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

print(a)

Submission status: Accepted

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

print(R)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
k = int(input())
x = [int(i) for i in input().split()]
ans = 0
for i in range(N):
    l = k - x[i]
    if x[i] < l:
        ans += x[i]*2
    else:
        ans += l*2

print(ans)

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] <= abs(x[i] - k):
        ans += x[i]
    else:
        ans += abs(x[i] -k)

print(ans*2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print s

Submission status: Accepted

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

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

tot = 0 

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

Submission status: Accepted

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

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

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

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

print l

Submission status: Accepted

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

n = int(input())
k = int(input())
x = list(map(int, input().split())) #x[0->(n-1)]

sum = 0

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

print(sum*2)

Submission status: Accepted

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

ans = 0

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

print ans

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
N=int(input().rstrip())
K=int(input().rstrip())
xi=map(int,input().rstrip().split())

idoSum=0

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve(): # B - Collecting Balls (Easy Version)
    N = int(input())
    K = int(input())
    x = list(map(int, input().split(" ")))

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

    print(sum)
    
    
solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8

n = input()
k = input()

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

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

print t

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 = [(int(xi), i + 1) for i, xi in enumerate(input().split())]
A = [(0, i) for i in range(1, N+1)]
B = [(K, i) for i in range(1, N+1)]
print(sum([2*min(abs(x[i][0]-A[i][0]), abs(x[i][0]-B[i][0])) for i in range(N)]))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

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

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python

import sys

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

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

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

        line = sys.stdin.readline()

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

Submission status: Accepted

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

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

main()

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())
data = input().split()

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

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

print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc074/tasks/abc074_b


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


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

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

    print(total_step)


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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(S)

Submission status: Accepted

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

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

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

print(str(ans))

Submission status: Accepted

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

s = 0

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

print(s)

Submission status: Accepted

Solution code:

import math

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

Submission status: Accepted

Solution code:
import sys

# utils
def read_numbers():
    return [int(num) for num in sys.stdin.readline().split()]
def read_string():
    return sys.stdin.readline().rstrip('\r\n')
def list2str(*array):
    return ' '.join([str(i) for i in array])

def main(N, K, xs):
    result = 0
    for x in xs:
        result += 2*min(K-x, x)
    return result

def parse_stdin():
    # N = read_numbers()
    # data = []
    # for i in range(N):
    #    data.append(read_numbers())
    N = read_numbers()[0]
    K = read_numbers()[0]
    xs = read_numbers()
    return N, K, xs

def test_main():
    """run pytest stdin.py to test"""
    assert main(2, 9, [3, 6]) == 12

if __name__ == '__main__':
    N, K, xs = parse_stdin()
    result = main(N, K, xs)
    print(result)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    K = int(input())
    x = list(map(int, input().split()))
    sum_half = 0
    for tmp_x in x:
        sum_half += min(tmp_x, K - tmp_x)

    print(sum_half * 2)

Submission status: Accepted

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

n=i1()
k=i1()
x=i2()
c=0
for i in x:
    if i>abs(k-i):
       c+=abs(k-i)
    else:
       c+=i
print(c*2)

Submission status: Accepted

Solution code:
N=(int)(input())
K=(int)(input())
x=(input()).split(' ')
result=0

for i in x:
    result+=2*min((int)(i),abs((int)(i)-K))

print(result)

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

print(ans)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

goukei = 0
for x in x_list:
    if abs(0-x) > abs(k-x):
        goukei += 2*abs(k-x)
    else:
        goukei += 2*abs(x)

print(goukei)

Submission status: Accepted

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

for i in range(N):
    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()))

dist = 0

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

Submission status: Accepted

Solution code:
import sys

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())


def main():
    n = I()
    k = I()
    xs = LI()

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

    return r


print(main())

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

counter = 0

for _ in listx:
    if abs(K - _) > _:
        counter += _*2
    else:
        counter += abs(K - _)*2

print(counter)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(d)

Submission status: Accepted

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

from collections import defaultdict
from math import factorial as f
from fractions import gcd as g

N = int (raw_input ())
K = int (raw_input ())
l = [int (i) for i in raw_input ().split ()]
print sum (2 * min (i, abs (K - i)) for i in l)

Submission status: Accepted

Solution code:
#coding:utf-8

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

print(sum(min(abs(k - i), i) * 2 for i in a))

Submission status: Accepted

Solution code:
def min(a,b):
	if(a<b):
		return a
	else:
		return b
n=int(input())
k=int(input())
l=list(map(int,input().split()))
ans=0
for val in l:
	ans+=2*abs(min(val,abs(k-val)))
print(ans)

Submission status: Accepted

Solution code:
n = int(input().strip())
k = int(input().strip())
xs = [int(x) for x in input().strip().split()]

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