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 math
from collections import deque
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def combmod(n,r,mod):
	return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod
def printQueue(q):
	r=copyQueue(q)
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): #j:aの個数
			k = i-a*j
			if k%b==0:
				return i
	return -1
def copyQueue(q):
	nq = queue.Queue()
	n = q.qsize()
	for i in range(n):
		x = q.get()
		q.put(x)
		nq.put(x)
	return nq

n = readInt()
k = readInt()
x = readInts()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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 X:
    ans += min(2*i,abs(i-K)*2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())
  
n=i()
k=i()
x=intl()
a=0
for i in range(n):
	a+=min(2*x[i],2*abs(x[i]-k))
print(a)

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

Submission status: Accepted

Solution code:
import numpy as np
def main():
  N = int(input())
  K = int(input())
  x = list(map(int, input().split()))
  ans = 0
  for i in x:
    ans += 2*min(i, K-i)
  print(ans)
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May 14 11:31:25 2020

@author: shinba
"""
n = int(input())
k = int(input())

ans = 0

for x in map(int,input().split()):
    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()))

line = K // 2
ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
K = int(input())
X = list(map(int, sys.stdin.readline().rsplit()))

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

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

print(ans*2)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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:
        ans += (x[i]-k)*2
    elif x[i] < k and k - x[i] < x[i]:
        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 = 0
for x in X:
    ans += min(x*2, (K-x)*2)
print(ans)

Submission status: Accepted

Solution code:
import math
def py():
    print("Yes")
def pn():
    print("No")
def iin():
    x = int(input())
    return x

neko = 0
nya = 0
nuko = 0
n = iin()
k = iin()
x = [int(x) for x in input().split()]

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string,decimal
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def SS(): return map(str,sys.stdin.readline().rstrip().split())
def II(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
K = I()
x = LI()

ans = 0

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

Submission status: Accepted

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

result = 0
for x_ in x:
    result += min(x_, (k - x_))

print(2 * result)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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


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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
#print#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code

#なお、任意の整数 M に対して M の −2 進数表現が一意に定まることが証明できます。
#2桁ごとに見るとよかった（もう実験したよ）
def resolve():
    N,=pin()
    K,=pin()
    X=tupin()
    ans=0
    for x in X:
        ans+=min(abs(x),abs(K-x))*2
    print(ans) 
#%%submit!
resolve()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

A=[]
B=[]

for i in range(len(x)):
    a = min(2*(x[i]), 2*abs(K-x[i]))
    A.append(a)

print(sum(A))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# region header
import sys
import math
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, deque, Counter
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from heapq import heappop, heappush
from itertools import accumulate, groupby, product, permutations, combinations, combinations_with_replacement
from math import ceil, floor, factorial, log, sqrt, sin, cos
from operator import itemgetter
from string import ascii_lowercase, ascii_uppercase, digits
sys.setrecursionlimit(10**7)
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]
INF = float('inf')
MOD = 10 ** 9 + 7
PI = math.pi
# endregion
N = ri()
K = ri()
x = ri_()
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()))
sum=0
for i in range(n):
    sum+=2*min(x[i],abs(k-x[i]))
print(sum)

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())
li = list(map(int,input().split()))	
ans = []
for i in range(N):
    if abs(li[i]-0)>=abs(li[i]-K):
        ans.append(2*abs(li[i]-K))
    elif abs(li[i]-0)<abs(li[i]-K):
        ans.append(2*li[i])
print(sum(ans))

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
 
x = tuple([int(t)for t in input().split()]) 
 
ans = 0
for x_i in x:
  if k-x_i>x_i:
    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 = list(map(int, input().split()))

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

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:
	A = abs(x)
	babs = K - x
	B = abs(babs)
	mindis = min(A,B)
	ans += (mindis * 2)
print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

Solution code:
N=int(input())
K=int(input())
l = list(map(int,input().split()))
ans=0
for i in range(N):
  n=K-l[i]
  nn=abs(n)
  if nn<l[i]:
    ans+=nn*2
  else:
    ans+=2*l[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:
    # print("x: {}, abs(x-K): {}".format(x, abs(x-K)))
    ans += 2 * min(abs(x-K), x)

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(x * 2, 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(n):
    ans += min(x[i] * 2, 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 i in range(len(x)):
    #print(type(x[i]))
    tmp = k - x[i]
    dist = min(x[i], tmp)
    ans += dist * 2

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    ans = []
    for i in x:
        ans.append(i*2 if k/2 > i else (k-i)*2)

    print(sum(ans))

resolve()

Submission status: Accepted

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

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sx = sorted(x)
mn = min(x)

cost = 0
for s in sx:
    cost += min(((K-s)*2), s*2)
print(cost)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N = int(readline().rstrip())
K = int(readline().rstrip())
X = list(map(int, readline().split()))

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

print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

N = int(readline().rstrip())
K = int(readline().rstrip())
X = list(map(int, readline().split()))

half = K / 2
ans = 0
for x in X:
    if half < x:
        ans += K - x
    else:
        ans += x

print(ans * 2)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
midp = k / 2
for x in X:
  if x < midp:
    ans += 2 * x
  else:
    ans += (k - x) * 2

print(ans)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()

n=I()
k=I()
A=LI()
ans=0
for a in A:
    ans+=2*min(abs(a-0),abs(k-a))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

length = 0

for i in range(N):
    if K-X[i] > X[i]:
        length += 2*X[i]
    else:
        length += 2*(K-X[i])

print(length)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
K = int(input())
X = np.array(list(map(int, input().split())))

distance_B = np.abs(X - K)
total_distance = 0
for x, b in zip(X, distance_B):
    if x > b:
        total_distance += 2*b
    else:
        total_distance += 2*x

print(total_distance)

Submission status: Accepted

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

def get_min_total_dist(K, X):
    return sum([2 * min(x, abs(x - K)) for x in X])

def main():
    N = int(input())
    K = int(input())
    X = list(map(int, input().split()))
    print(get_min_total_dist(K, X))

if __name__ == "__main__":
    try:
        assert get_min_total_dist(10, [2]) == 4
        assert get_min_total_dist(9, [3, 6]) == 12
        assert get_min_total_dist(20, [11, 12, 9, 17, 12]) == 74
    except:
        import traceback
        traceback.print_exc()
        exit()
    main()

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:


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

res = []
for i in range(N):
    a_cost = 2 * x[i]
    b_cost = 2 * (K - x[i])
    cost = min([a_cost, b_cost])
    res.append(cost)

print(sum(res))

Submission status: Accepted

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

ans=0
i=0
while i<N:
    ans+=2*min(X[i],K-X[i])
    i+=1

print(ans)

Submission status: Accepted

Solution code:
N,K=int(input()),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:
a=int(input())
b=int(input())
ballist=list(map(int,input().split()))
dist=0
for t in ballist:
    dist+=min(abs(t),abs(t-b))
print(dist*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 += 2*min(i, k-i)
print(ans)

Submission status: Accepted

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


def read():
    N = int(input().strip())
    K = int(input().strip())
    X = map(int, input().strip().split())
    return N, K, X


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


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
K = INT()
x = LIST()

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

print(ans)

Submission status: Accepted

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

d=0
for i in range(N):
  d+=min(x[i],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 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()))
ans = 0

for i in x:
    ans += min(2 * i, 2 * 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)