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


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


# 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:
n = int(input())
k = int(input())
L = list(map(int,input().split()))
ans = 0
for l in L:
  a = l
  b = abs(k-l)
  ans += min(a,b)*2
print(ans)

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

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

Submission status: Accepted

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


def solve(N: int, K: int, x: "List[int]"):
    ans = 0
    for i in range(N):
       ans += min(x[i], abs(K - x[i])) * 2
    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:
def resolve():
    N = int(input())
    K = int(input())
    X = list(map(int, input().split()))
    ans = 0
    for x in X:
        ans += min(x, abs(K-x))*2
    print(ans)
    
resolve()

Submission status: Accepted

Solution code:
with open(0) as f:
    N, K, *X = map(int, f.read().split())
print(2*sum(min(x, 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 in X:
  ans += 2 * (x if x <= K - x else K - x)
  
print(ans)

Submission status: Accepted

Solution code:
# abc 074 b


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())
x=list(map(int,input().split()))
print(sum(2*min(y,K-y) for y in x))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(calc)

Submission status: Accepted

Solution code:
import math

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

dis = math.ceil(k/2)
for i in x:
    if i == k:
        continue
    elif i <= dis:
        cnt += i*2
    else:
        cnt += (k-i)*2
print(cnt)

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:
    Y=[]
    Y.append(2*abs(i))
    Y.append(2*abs(i-K))
    ans+=min(Y)
    
print(ans)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

print(sum([min(position, abs(k - position)) * 2 for position in x]))

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K, *X = map(int, open(0).read().split())

ans = 0
for y,x in enumerate(X, start=1):
    a = x * 2
    b = (K - x) * 2
    ans += min(a, b)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
K = int(input())
x_list = list(map(int,input().split()))

sum_distance = 0

for x in x_list:
    if x < K - x:
        sum_distance += 2*x
    else:
        sum_distance += 2*(K-x)

print(sum_distance)

Submission status: Accepted

Solution code:
N = input()
K = int(input())
l = input().split()
         

         
ans =0 
for i in l:
	s = int(i)
	ans += min(s,K-s)
print(ans*2)

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    data=[]
    n = int(input())
    k = int(input())
    data=list(map(int,input().split()))
    length=0

    for ball in data:
        if ball < k-ball:
            length+=2*ball
        else:
            length+=2*(k-ball)
    print(length)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
x_l = list(map(int, input().split()))
th = k/2

ans = 0
for x in x_l:
  if x >= th:
    ans += abs(x-k)
  else:
    ans += x
print(ans*2)

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(x[i], abs(x[i]-k))

print(ans)

Submission status: Accepted

Solution code:
# B - Collecting Balls (Easy Version) 
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:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

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

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 = [int(x.strip()) for x in input().split()]
ans = 0
for i in x:
  if abs(i-k) > i:
    ans = ans + i
  else:
    ans = ans + abs(i-k)
ans += ans
print(ans)

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:
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:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())
    K = int(readline())
    x = list(map(int, readline().split()))

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for p in x:
  a = 2*p
  b = 2*abs(K-p)
  if a >= b:
    sum_min += b
  elif a < b:
    sum_min += a

print(sum_min)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(2 * ans)

Submission status: Accepted

Solution code:
N = int(input(""))
K = int(input(""))
a = input("").split(" ")
a = [int(aa) for aa in a]
ans = 0
for x in a:
    if (K-x) < x:
        ans += (K-x)*2
    elif (K-x) >= x:
        ans += x*2
print(ans)

Submission status: Accepted

Solution code:

# ABC074

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 i in range(N):
  ans += min(abs(x[i] - 0), abs(K - 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 range(n):
  m=min(abs(x[i]),abs(k-x[i]))
  ans+=m

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

dist_sum = 0
for i in range(N):
	y = i + 1
	x = xs[i]
	
	if x > K - x:
		dist_sum += (K - x) * 2
	else:
		dist_sum += x * 2
		
print(dist_sum)

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

Submission status: Accepted

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

s = 0
xs = list(map(int, input().split()))
for x in xs:  
  if x < abs(k-x):
    s += 2 * x
  else:
    s += 2 * abs(k-x)
    
print(s)

Submission status: Accepted

Solution code:
"""Boot-camp-for-Beginners_Easy009_B_Collecting-Balls-(Easy-Version)_28-August-2020.py"""

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

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

Submission status: Accepted

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

distance = 0

for i in range(len(x)):
    if x[i] < k - x[i]:
        distance += x[i] * 2
    else:
        distance += (k - x[i]) * 2

print(distance)

Submission status: Accepted

Solution code:
a=int(input())
b=int(input())
c=list(map(int,input().split()))
total=0
for i in range(a):
    d=2*c[i]
    e=2*abs(b-c[i])
    total+=min(d,e)
print(total)

Submission status: Accepted

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

Submission status: Accepted

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

result = 0

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    ans = 0
    for i,item in enumerate(x):
        a = item
        b = abs(item-k)
        ans += min(a,b)
    ans *= 2
    print(ans)
    return

if __name__=='__main__':
    n,k = I(),I()
    x = list(IL())
    solve()

Submission status: Accepted

Solution code:
n,k = [int(input()) for i in range(2)]
x = list(map(int,input().split()))

distance = 0

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

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

Submission status: Accepted

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

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(t)

Submission status: Accepted

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

res = 0
for y_i, x_i in enumerate(x):
  res += min(x_i*2, (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):
    if X[i] <= (K - X[i]):
        ans += (X[i] * 2)
    else:
        ans += ((K - X[i]) * 2)
print (ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())
k = int(input())
x = [0] + list(map(int, input().split()))

ans = 0
for i in range(1, n+1):
    ans += min(abs(x[i] - 0) * 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 x in x:
    a = 2 * x
    b = abs(K - x) * 2
    ans += min(a, b)

print(ans)

Submission status: Accepted

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

ans=0
for i in l:
  if i>=k/2:
    ans+=abs(k-i)*2
  else:
    ans+=i*2

print(ans)

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    K = I()
    x = LI()

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

if __name__ == '__main__':
    resolve()

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

Submission status: Accepted

Solution code:
_,k=input(),int(input());print(sum(min(i,(k-i))*2 for i in list(map(int,input().split()))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans*2)

Submission status: Accepted

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


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K = (int(input()) for T in range(0,2))
X = [int(T) for T in input().split()]
print(sum(2*min(TX,K-TX) for TX in X))

Submission status: Accepted

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

ans = 0
for x in xs:
  a = x*2
  b = (K-x)*2
  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(abs(0-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()))

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

Submission status: Accepted

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

Submission status: Accepted

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


def diff_ball_K(N, K):
    return [N - K]


ball_K = list(map(lambda x: K - x, X))



diff = [min(a,b) for a,b in zip(X, ball_K)]
print(sum(diff)*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 = x[i] * 2
    b = (k - x[i]) * 2
    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):
    a = x[i] * 2
    b = abs(x[i] - k) * 2
    ans += min(a,b)
print(ans)

Submission status: Accepted

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

print(2*s)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

distance = []
for x in X:
    if x < abs(K-x):
        distance.append(x)
    else:
        distance.append(abs(K-x))

print(2*sum(distance))

Submission status: Accepted

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

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

print(score)

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [list(input()) for _ in range(n)]
#a = [int(input()) for _ in range(n)]


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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

distance = 0
for i in x:#全てのyでの試行
    if abs(i - K) < i:
        distance += 2 * abs(i - K)
    else :
        distance += 2 * i
print(distance)

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

Submission status: Accepted

Solution code:
import math


def resolve():
    import sys
    input = sys.stdin.readline
    # row = [int(x) for x in input().rstrip().split(" ")]
    n = int(input().rstrip())
    k = int(input().rstrip())
    x_list = [int(x) for x in input().rstrip().split(" ")]

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


    print(distance)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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


# Bが回収
# Bが回収した場合の距離
b = []
for i in range(n):
    b.append(abs(k - x[i])*2)


# Aが回収
# Aが回収した場合の距離
a = []
for i in range(n):
    a.append(x[i]*2)

# aとbの少ない値を足していく→最小距離
min_len = 0
for i in range(n):
    min_len += min(a[i], b[i])

print(min_len)

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

Submission status: Accepted

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