Problem:


Score : 200 points

Problem Statement

There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).
Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

Constraints

1 \leq N \leq 100

1 \leq K \leq 100

0 < x_i < K

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
K
x_1 x_2 ... x_N

Outputs

Print the minimum possible total distance covered by robots.

Sample Input 1

1
10
2

Sample Output 1

4

There are just one ball, one type-A robot and one type-B robot.

If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.

Similarly, if the type-B robot is used, the total distance covered will be 16.

Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

Sample Input 2

2
9
3 6

Sample Output 2

12

The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.

Sample Input 3

5
20
11 12 9 17 12

Sample Output 3

74



Sample input:
1
10
2


Sample output:
4

Submission status: Accepted

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

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

print(num)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
k=int(input())
xs=list(map(int,input().split(' ')))
#A (0,i)
#B (K,i)
sum=0
for i in range(n):
  deltak=abs(xs[i]-k)
  if xs[i]<deltak:
    sum+=2*xs[i]
  else:
    sum+=deltak*2
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

    dist = [min(abs(i-0), abs(i-k)) for i in x]
    print(sum(dist)*2)

main()

Submission status: Accepted

Solution code:
N = int(input()) # ボールの数
K = int(input()) # Bの位置
xs = [int(i) for i in input().split()] # ボールの位置

half = K / 2

total = 0
for x in xs:
    if x < half:
        total += x
    else:
        total += (K-x)

# 最後に二倍すればよい
print(2*total)

Submission status: Accepted

Solution code:
N = int(input()) # ボールの数
K = int(input()) # Bの位置
xs = [int(i) for i in input().split()] # ボールの位置

half = K / 2

total = 0
for x in xs:
    if x < half:
        total += 2*x
    else:
        total += 2*(K-x)

print(total)

Submission status: Accepted

Solution code:
import sys
N = int(input())
K = int(input())
coordinate = list(map(int,input().split()))
if N < 0 or N > 100 or K < 0 or K > 100:
    sys.exit()

sum_min = 0
for x in coordinate:
    if x < 0 or x >= K:
        sys.exit()
    if x * 2 <= (K - x) * 2:
        sum_min += x * 2
    else:
        sum_min += (K - x) * 2
print(sum_min)

Submission status: Accepted

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

n = int(input())
k = int(input())
x_li = [int(w) for w in input().split()]

ans = 0
for x in x_li:
    ans += min(x, abs(k - x))

print(ans*2)

Submission status: Accepted

Solution code:
import sys
import math
import numpy as np
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N = i()
K = i()
x = l()
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()))

res = 0
for i in range(N):
    res += min(X[i], abs(X[i]-K))*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):
  temp = min(X[i],K-X[i])
  ans += temp*2
print(ans)

Submission status: Accepted

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
K = int(input())
Xs = list(mapint())

ans = 0
for x in Xs:
    ans += min(abs(x), abs(x-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 += 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()))
cnt = 0
for i in range(N):
    cnt += min(X[i],K-X[i])
print(2*cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque, Counter
import math

# import copy
from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve():
    n = getN()
    k = getN()
    nums = getList()

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


    print(ans)
def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    K = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    ans=0
    for a in A:
        ans+=2*min(a,K-a)
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
k = inp()
a = inpl()
res = 0
for i,x in enumerate(a):
    res += min(abs(k-x)*2, x*2)
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#n , m , l = map(int , input().split())

# list_n = list(map(int , inpuut().split()))<Paste>

#n = input()
# list = [input() for i in range(N)

#list = [[i for i in range(N)] for _ in range(M)]


N = int(input())
K = int(input())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
K = int(input())
x = list(map(int, input().split()))
A = [0] * N
B = [0] * N

ans = 0
for i in range(N):
    x[i] = [x[i], i+1]
    A[i] = [0, i+1]
    B[i] = [K, i+1]

    if x[i][1] == A[i][1] == B[i][1]:
        ans += min(abs(x[i][0] - A[i][0]), abs(x[i][0] - B[i][0]))

print(ans*2)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
K = int(input())
ans = 0

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

s = 0
for i in x:
  if abs(i - k) < i:
    i = abs(i - k)
  s += i * 2

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(ans * 2)

Submission status: Accepted

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

print(result)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

total=0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!python3

LI = lambda: list(map(int, input().split()))

# input
N = int(input())
K = int(input())
X = LI()


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(N):
    A = 2*a[i]
    B = 2*(K-a[i])
    sum_a += min(A,B)
    
    
print(sum_a)

Submission status: Accepted

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

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

print(sum(dp))

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:
N = int(input())
K = int(input())
*A, = map(int, input().split())
print(sum([min(a,K-a)*2 for a in A]))

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from heapq import heapify, heappop, heappush
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        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 -self.parents[self.find(x)]

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

    def members(self, x):
        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()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

# ここから書き始める
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()))
sum = 0
for i in range(n):
    if x[i] <= abs(x[i] - k):
        sum += 2*x[i]
    else:
        sum += 2*abs(x[i] - k)
print(sum)

Submission status: Accepted

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

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

print(idoukyori)

Submission status: Accepted

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

border = K // 2
ans = 0
for x in xx:
    if x <= border:
        # A が動く
        ans += x * 2
    else:
        # B が動く
        ans += (K - x) * 2
print(ans)

Submission status: Accepted

Solution code:
# template by 3xC and starkizard.
# contributors: 

#####################################################################################
from __future__ import division, print_function 

import sys
import os
from collections import Counter, deque, defaultdict
import itertools
import math
import io


"""Uncomment modules according to your need"""


# from bisect import bisect_left, bisect_right, insort
# from heapq import heappop, heapify, heappush
# from random import randint as rn
# from Queue import Queue as Q
# from copy import deepcopy
# from decimal import *
# import re
# import operator

#####################################################################################
# this enables you to write python3 code with PyPy2 (Python 2)
if sys.version_info[0] < 3:
    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip

#####################################################################################

"""value of mod"""
MOD = 998244353
mod = 10**9 + 7

"""Uncomment next 4 lines if doing huge recursion"""
# import threading
# threading.stack_size(1<<27)
# sys.setrecursionlimit(10000

def prepare_factorial(mod=mod):
    """ returns two lists, factorial and inverse factorial modulo argument by default 10**9 +7 """
    # Comment code out when you don't need inverse factorial or vice versa
    fact = [1]
    for i in range(1, 200005):
        fact.append((fact[-1] * i) % mod)

    ifact = [0] * 200005
    ifact[200004] = pow(fact[200004], mod - 2, mod)
    for i in range(200004, 0, -1):
        ifact[i - 1] = (i * ifact[i]) % mod

    return fact, ifact

def modinv(n, p):
    """ returns N inverse modulo p """
    return pow(n, p - 2, p)

def ncr(n, r,  fact, ifact):
    """ takes 4 arguments: n , r and factorial and inverse factorial lists"""
    t = (fact[n] * (ifact[r]*ifact[n-r]) % MOD)% MOD
    return t


def get_n(Sum):    
    """this function returns the maximum n for which Summation(n) <= Sum"""
    ans = (-1 + sqrt(1 + 8*Sum))//2
    return ans

def sieve(n):
    """ returns a list of prime numbers till n """
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r



def divs(n, start=1):
    """ returns a list of all divisors till n """
    divisors = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n / i == i:
                divisors.append(i)
            else:
                divisors.extend([i, n // i])
    return divisors



def divn(n, primes):
    """ returns the number of divisors, two arguments n and the sieve till n """
    divs_number = 1
    for i in primes:
        if n == 1:
            return divs_number
        t = 1
        while n % i == 0:
            t += 1
            n //= i
        divs_number *= t



def lrfind(d, x, default=-1):
    """ Takes 2 arguments an iterable and an element. returns a tuple (firstoccurence,lastoccurence) -1 if not found """
    left = right = -1
    for i in range(len(d)):
        if d[i] == x:
            if left == -1: left = i
            right = i
    if left == -1:
        return default, default
    else:
        return left, right

def gcd(x, y): # math.gcd is slower
    """ returns greatest common divisor of x and y """
    while y:
        x, y = y, x % y
    return x

def check_sorted(a):
    ''' returns True/False '''
    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return False
    return True
def ceil(n, k=1): return n // k + (n % k != 0) #returns math.ceil but protecting against floating inconsistencies
def input(): return sys.stdin.readline().strip()
def ii(): return int(input()) #inputs integer
def mi(): return map(int, input().split()) # inputting space seperated variables for example x,y,z
def li(): return list(map(int, input().split())) #inputting a space seperated list of integers
def lw(): return input().split() #inputting a space seperated list of strings
def lcm(a, b): return abs(a * b) // gcd(a, b) #returns LCM of two arguments
def prr(a, sep=' ', end='\n'): print(sep.join(map(str, a)), end=end) #For printing an iterable with seperator sep as optional second argument (default : " "), ending character (default: "\n") as optional third
def dd(): return defaultdict(int) #returns a dictionary with values defaulted to 0
def ddl(): return defaultdict(list) #returns a dictionary with values defaulted to []
def write(s): return sys.stdout.write(s)












###################################################################
def main():
    #CODE GOES HERE:
    n=ii()
    k=ii()
    x=li()
    answer=0
    for i in x:
        answer+=min(i,abs(k-i))
    answer*=2
    print(answer)


























""" -------- Python 2 and 3 footer by Pajenegod and c1729 ---------"""

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192

class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill(): pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill();
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode('ascii'))
            self.read = lambda: self.buffer.read().decode('ascii')
            self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')

""" main function"""

if __name__ == '__main__':
    main()
    # threading.Thread(target=main).start()

Submission status: Accepted

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


def main():
    n = int(input())
    k = int(input())
    l = list(map(int,input().split()))

    tmp = 0
    for x in l:
        if x <= (k/2):
            tmp+=(2*x)
        else:
            tmp+=(2*(k-x))
    print(tmp)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

Solution code:
N,K=int(input()),int(input());print(sum([i*2 if (i*2<(K-i)*2) else ((K-i)*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()))
a = [i*2 for i in x]
b = [(K-i)*2 for i in x]
print(sum([i if i < j else j for i,j in zip(a,b)]))

Submission status: Accepted

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

Submission status: Accepted

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

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