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

import math
ans = []
for i in range(N):
    a = math.sqrt((x[i] - 0) ** 2) * 2
    b = math.sqrt((x[i] - K) ** 2) * 2
    ans.append(min(a, b))
print(round(sum(ans)))

Submission status: Accepted

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n,k,*x=map(int,open(0).read().split())

ans=[]
for i in x:
    tmp=2*min(i-0,k-i)
    ans.append(tmp)

print(sum(ans))

Submission status: Accepted

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

n=int(input().rstrip())
k=int(input().rstrip())
x=[int(i) for i 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:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
k = ni()
x = na()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 074
# B - Collecting Balls (Easy Version)
# https://atcoder.jp/contests/abc074/tasks/abc074_b

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

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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


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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
K = I()
a = IL()

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
def solve():
    input = sys.stdin.readline
    N = int(input())
    K = int(input())
    X = [int(x) for x in input().split()]
    ans = 0
    for x in X: ans += min(x, K - x) * 2
    print(ans)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
K = int(input())
res = 0
a = list(map(int, input().split()))
for x in a:
  res += min(abs(x - K), x) * 2
print(res)

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt,gcd
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
from itertools import product,permutations,combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

def main():
    n = int(input())
    k = int(input())
    x = readInt()
    ans = 0
    for i in range(n):
        ans += min(x[i],abs(x[i]-k)) * 2
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    K = int(input())
    balls = [int(a) for a in input().split()]

    d = 0
    for i in range(len(balls)):
        d += 2*min( balls[i] , K - balls[i])
    print(d)
main()

Submission status: Accepted

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

distance = 0

for i in range (0, N):
	if abs(L[i]-K) > L[i]:
		distance+=L[i]
	else:
		distance+=abs(L[i]-K)

print(2*distance)

Submission status: Accepted

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

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
K=int(input())
X=list(map(int,input().split()))
s=0
for x in X:
    if 0<=x<=K/2:
        s+=x*2
    else:
        s+=(K-x)*2
    
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(N):
    distance += min(2 * x[i], 2*(abs(K - x[i])))

print(distance)

Submission status: Accepted

Solution code:
# ABC 074: B – Collecting Balls (Easy Version)
n, k = [int(input()) for _ in range(2)]
x = [int(s) for s in input().split()]
print(sum([min(x[i] * 2, abs(k - x[i]) * 2) for i in range(n)]))

Submission status: Accepted

Solution code:
n=int(input())
k=int(input())
x=[0]*n
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:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix

# strlist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
K = int(input())
arrA = list(map(int,input().split()))

anslis = []
for i in arrA:
    num = min(i,K-i)
    anslis.append(num)

ans = sum(anslis)*2
print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(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(x[i]-K))*2
print(ans)

Submission status: Accepted

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

answer = 0

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

print(answer)

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

Submission status: Accepted

Solution code:
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
MOD = 10**9+7
import itertools
import math

n = int(input())
k = int(input())
x = list(map(int,input().split()))
k_d = []
for i in range(len(x)):
    k_d.append(abs(x[i]-k))
ans = 0
for i in range(n):
    diff = min(x[i],k_d[i])
    ans += diff*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())
x = list(map(int, input().split()))
cnt = 0

for i in x:
    a = i*2
    b = (K-i)*2
    if a>=b:
        cnt += b
    else:
        cnt += a
print(cnt)

Submission status: Accepted

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

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

print(ans*2)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
k = INT()
x = LIST()
ans = 0
for xx in x:
    ans += min(k - xx, xx) * 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):
    #0 ---- xi ---- K
    res = min(abs(x[i]-0)*2,abs(k-x[i])*2)
    ans += res
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]-k),x[i])
print(ans*2)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in x:
    if 2 * i < 2 * abs(K-i):
        ans += 2 * i
    else:
        ans += 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 += 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()))

def min_dist(x, K):
    return min(x, abs(K-x))

ans = 0
for v in x:
    ans += min_dist(v, K) * 2

print(ans)

Submission status: Accepted

Solution code:
# abc074_b.py
N = int(input())
K = int(input())
X = list(map(int,input().split()))
center = K/2

ans = 0
for i in X:
    if i < center:
        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()))
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=[int(s) for s in input().split(" ")]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8
n = int(input())
k = int(input())
a = list(map(int, input().split()))
ans = 0

for i in range(n):
    dist = min(2*a[i], 2*(k-a[i]))
    ans += dist

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def myAnswer(N:int,K:int,X:list):
   ans = 0
   for x in X:
      ans+= min(abs(0 - x)*2, abs(K - x)*2 )
   return ans

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   K = int(input())
   X = list(map(int,input().split()))
   print(myAnswer(N,K,X[:]))


if __name__ == '__main__':
   main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

print(np.sum(np.minimum(x, np.abs(x - K))) * 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], 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 K - x[i] > x[i]:
        ans += x[i] * 2
    else:
        ans += (K - x[i]) * 2

print(ans)

Submission status: Accepted

Solution code:
ans = 0
n, k = (int(input()) for _ in range(2))
l = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:

N = int(input())
K = int(input())
x = map(int, input().split())
d = 0
for i in x:
    if i > K/2:
        d += (K - i) *2
    else:
        d += 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 abs(X[i]) <= abs(K - X[i]):
    ans += 2 * X[i]
  else:
    ans += 2 * (K - X[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
k = int(input())
list_X = list(map(int, input().split()))
ans = 0
for num in list_X:
    ans += min(abs(num),abs(num-k))

print(ans*2)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

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


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
# coding: utf-8
N=int(input())
K=int(input())
def operation(x):
  if x<=K/2:
    return x*2
  else:
    return (K-x)*2
    
x=input().split(" ")
sum=0
for i in range(N):
  sum+=int(operation(int(x[i])))
print(sum)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
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
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

# ここから書き始める
n = int(input())
k = int(input())
x = list(map(int, input().split()))
ans = 0
for i in x:
    ans += min(abs(i), 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 x in X:
    ans += min(x, abs(x-k))*2
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):
    ans += min(A[i],abs(K-A[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):
    dist = x[i] * 2
    tmp =  abs(k - x[i]) * 2
    if dist > tmp :
        dist = tmp
    result += dist

print(result)

Submission status: Accepted

Solution code:

import math
#import itertools
#import statistics
#import numpy as np
import collections
n = int(input())
#a = list(map(int, input().split()))
#a = list(map(int, input().split()))
k = int(input())
x = list(map(int, input().split()))
total = 0
for i in range(n):
    if k-x[i] >= x[i]:
        total += x[i]*2
    else:
        total += (k-x[i])*2
print(total)

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

Submission status: Accepted

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

# 間違えてAに提出した

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_, k, *x = map(int, open(0).read().split())
print(sum(i if k/2>i else k-i for i in x)*2)

Submission status: Accepted

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

for xi in X:
    A2x = xi * 2
    B2x = abs(K - xi) * 2
    ans += min(A2x, B2x)

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc074/tasks/abc074_b

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

ans = 0
for x in x_list:
    K_x = K - x

    if K_x >= x:
        ans += x*2

    elif K_x < x:
        ans += K_x*2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# import itertools
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    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)) * 2
    print(ans)
    return

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(x)):
  dist_a = x[i] * 2
  dist_b = abs(K - x[i]) * 2
  if dist_a < dist_b:
    sum += dist_a
  else:
    sum += dist_b

print(sum)

Submission status: Accepted

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

a = 0 # aはタイプBからボールまでの距離

ans = 0

for i in range(N):

    if K>x[i]:
        a = K - x[i]
    else:
        a = x[i] - K
    
    if x[i]<a:
        ans += x[i]
    else:
        ans += a

print(str(ans * 2))

Submission status: Accepted

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

tmp = []
for i in x:
    tmp.append((min(abs(K-i), i))*2)
print(sum(tmp))

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

print(ans)

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], abs(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(x, K - x) for x in X]))

Submission status: Accepted

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

x_list = [int(i) for i in input().split()]

th = k // 2

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

print(dis)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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