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

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=[int(x) for x in input().split()]
ans=0
from math import fabs
for i in x:
    ans+=min(fabs(i-k),fabs(i))
print(int(ans*2))

Submission status: Accepted

Solution code:
# coding: utf-8
def get_ln_inputs():
    return input().split()


def get_ln_int_inputs():
    return list(map(int, get_ln_inputs()))


def main():
    N = get_ln_int_inputs()[0]
    K = get_ln_int_inputs()[0]
    xs = get_ln_int_inputs()

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

    print(2 * ans)
    return


main()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
k = int(input())
summ = 0
for _ in range(n):
  x = int(input())
  summ += min(2*x, 2*(k-x))
 print(summ)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

suml = [0] * 10

for i in range(N):
  delta = abs(K-x[i])
  if x[i] < delta:
  	suml[i] = suml[i] + x[i]*2
  else:
  	suml[i] = suml[i] + delta*2

print(sum(suml))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
k=int(input())
xs=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: Runtime Error

Solution code:
#include<iostream>
using namespace std;
int main(){
	int n, k;
	cin >> n >> k;
	int x; 
	int sum = 0;
	for (int i=0;i<n; i++){
		cin >> x;
		if (x > k-x) sum += (k-x)*2;
		else sum += x*2;
	}
	cout << sum<<endl;
}

Submission status: Runtime Error

Solution code:
N,K,*A=map(int,open(0))
print(sum([min(a,K-a)*2 for a in A]))

Submission status: Runtime Error

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: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
K=int(input())
a=line(map(int,input().split()))
s=0
for i in range(len(a)):
  if a[i]<=K-a[i]:
    s=s+2*a[i]
  else:
    s=s+2*(K-a[i])
print(s)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

readline = sys.stdin.readline

n = int(input())
k = int(input())
x = np.array(readline().split(), dtype=np.int64)

y = k - x
ans = np.sum(np.amin(np.stack([x,y]), axis=0)) * 2
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

readline = sys.stdin.readline

n = int(input())
k = int(input())
x = np.array(readline().split(), dtype=np.int64)

y = k - x
print(np.stack([x,y]))
ans = np.sum(np.amin(np.stack([x,y]), axis=0)) * 2
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
T = list(map(int, input().split()))
sum_T = sum(T)
M = int(input())
for _ in range(M):
    p, x = map(int, input().split())
    v = sum_T - T[p - 1] + x
    print(v)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
typedef long long ll;
#define MOD 1000000007
#define INF -99999999 
using namespace std;
 
int main(){
    int N,K; cin >> N >> K;
    int ans = 0;
    for (int i = 0; i < N; i++)
    {
        int x;
        cin >> x;
        ans += 2*min(x,K-x);
    }
    cout << ans << endl;
}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
k=int(input())
x=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: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:

N, A,B = map(int, input().split())
str = []
if A==0 and B==0:
    print('0')

else:
    from math import floor
    list_ab = []
    list_b = ['b']*A
    list_ab+=(list_b)
    list_a = ['a']*B
    list_ab+=list_a

    count = (floor(N/(A+B))+1)
    str = list_ab*count




    str = str[:N]
    print(str.count('b'))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a, b, c = map(int, input().split())
ans = 0
if a == b == c and a%2 == 0:
  print(-1)
else:
  while (a%2 or b%2 or c%2) == 0 :
    tmp1 = a
    tmp2 = b
    a = b/2 + c/2
    b = tmp1/2 + c/2
    c = tmp1/2 + tmp2/2
    ans += 1
  print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

n = int(input())
k = int(input())
xi  = list(int, input().split())
i = 1
soukyori = 0
while i <= n:
       if k-x[i] >= x[i]:
          soukyori += x[i]
       else :
          soukyori += k-x[i]

       i += 1



print(soukyori)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n, k;  
  cin >> n >> k ;

  int ans = 0;
  int x;
  for (int i=0; i<n; i++) {
    cin >> x;
    ans += min(x*2, (k-x)*2);
  }

  cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
N,X=map(int, input().split())
L=list(map(int, input().split()))
s=[x for x in range(N) if sum(L[:x])<=X]
if N>=len(s):
  print(len(s))
else:
  print(N+1)

Submission status: Runtime Error

Solution code:
import fractions
from functools import reduce
N = int(input())
T = [int(input()) for _ in range(N)]

def gcd(*args):
  return reduce(fractions.gcd, args)

ans = 1
for i in range(1, N):
    ans = ans * T[i] // gcd(ans, T[i])
print(ans)

Submission status: Runtime Error

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

total = 0
for _ in range(n):
  x = int(input())
  total += min(x, k - x)

print(2 * total)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

n = int(input())
k = int(input())
xs = [int(input()) for i in range(n)]

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

Submission status: Runtime Error

Solution code:
import math

n = int(input())
k = int(input())
xs = [int(input()) for i in range(n)]

print(2*sum(min([x, k - x])))

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
ans = 0
for _ in range(N):
  x = int(input())
  if K >= 2*x:
    ans += x*2
  else:
    ans += (K-x)*2
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
l = [int(input()) for i in range(N)]
zero_kyori = 0
K_kyori = 0
kyori_list = []

for i in range(N):
    zero_kyori = l[i] - 0
    K_kyori = abs(l[i] - 0)
    kyori_list.append(min(zero_kyori, K_kyori))

print(sum(kyori_list))

Submission status: Runtime Error

Solution code:
# coding: utf-8
import time
import re
import math

def main():
    n=int(input())
    k=int(input())
    X=map(int,input().split())
    total=0
    for a in X:
        total+=min([a,math.abs(k-a)])*2
    print(int(total))
main()

Submission status: Runtime Error

Solution code:
N=int(input())
K=int(input())
list1=list(input())
SUM=0
for i in range(N):
  x=int(list1[i])
  if x>K-x:
    SUM+=2*(K-x)
  else:
    SUM+=2*x
print(SUM)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
cnt = 0
for i in range(N):
  tmp = int(input())
  cnt += min(abs(K-tmp), (tmp))*2
print(cnt)

Submission status: Runtime Error

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
X = [int(_) for _ in stdin.readline().rstrip().split()]
ans = 0
for i in x:
    if i < K - i:
        ans += 2 * i
    else:
        ans += 2 * (K - i)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
X = [int(_) for _ in stdin.readline().rstrip().split()]
ans = 0
for i in x:
    if i < K - i:
        ans += 2 * i
    else:
        ans += 2 * (K - i)
print(route)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

sum = 0

for item in x:
    if 0.5 * k <= int(item):
        sum += 2(k - int(item))
    else:
        sum += 2 * int(item)

print(sum)

Submission status: Runtime Error

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

sum_len = 0
for i in X:
    if X < abs(K-X[i]):
        sum_len += X * 2
    else:
        sum_len += abs(K-X[i]) * 2

print(sum_len)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
k=int(input())
ans=0
for _ in map(int,input()):
  a=int(input())
  ans+=min(a,k-a)
print(ans+ans)

Submission status: Runtime Error

Solution code:
n=int(input())
k=int(input())
ans=0
for _ in range(n):
  a=int(input())
  ans+=min(a,k-a)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
l

Submission status: Runtime Error

Solution code:
n = int(input())
k = int(input())
s = 0
for _ in range(n):
  x = int(input())
  s += min(x * 2, (k - x) * 2)
print(s)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
int N,K,a;
int main(){
    cin>>N>>K;
    while(N--){
        int x;
        cin>>x;
        a+=min(K-x,x);
    }
    cout<<a*2;
    return 0;
}

Submission status: Runtime Error

Solution code:
# input
N_lst = [int(x) for x in list(input())]

if N_lst[0] == 9 or N_lst[1] == 9:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(d)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
ans = 0
for _ in range(N):
    x = int(input())
    if x*2 =< K:
        asn += x*2
    else:
        ans += (K - x)*2
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
K = int(input())
x = []
for i in range(N):
    x.append([int(i) for i in 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)

Submission status: Runtime Error

Solution code:
n = int(input())
k = int(input())
l = []
for i in range(n):
 l.append(int(input()))
ans = 0
for i in l:
 if( i < abs( i - k )  ):
  ans += 2*abs(i-k)
 else:
  ans += 2*i
print(ans)

Submission status: Runtime Error

Solution code:
def calc_dist(N, K, x):
    dists = [2 * min(K - x_i, x_i) for x_i in x]
    return sum(dists)

if __name__ == "__main__":
    run_test(calc_dist, test_pair)
    N = int(raw_input())
    K = int(raw_input())
    x = map(int, raw_input().split())
    print(calc_dist(N, K, x))

Submission status: Runtime Error

Solution code:
Total_dist = 0

#iterate through number of bals
for i in range(N):
	for Rtype in ['A','B']:
		dist = []
		if Rtype == 'A':
			dist.append(abs(X[i] - 0))
		if Rtype == 'B':
			dist.append(abs(X[i] - K))
	Total_dist += min(dist)

print(Total_dist)

Submission status: Runtime Error

Solution code:
#get inputs
N = int(input())
K = int(input())
X = map(int, input().split())
Total_dist = 0

#iterate through number of bals
for i in range(N):
	for Rtype in ['A','B']:
		dist = []
		if Rtype == 'A':
			dist.append(abs(X[i] - 0))
		if Rtype == 'B':
			dist.append(abs(X[i] - K))
	Total_dist += min(dist)

print(Total_dist)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":
    N = int(input())
    K = int(input())
    x = map(int, input().split())
    ans = 0
    for i in range(N):
        r = min(x[i],abs(K-x[i]))
        ans += 2*r
    print(ans)

Submission status: Runtime Error

Solution code:
A,B,C,D,E,F=map(int,input().split())
best_water=0
best_salt=0
best_rate=0
for i in range(30//A+1):#Aの投入数
  for j in range(30//B+1):#Bの投入数
    water=(i*A+j*B)*100
    if water<=F and water:
      max_salt=(i*A+j*B)*E
      for k in range(max_salt//C+1):#Cを何個使うか
        l=(max_salt-C*k)//D#Dは何個つかえるか
        salt=k*C+l*D
        rate=100*salt/(water+salt)
        if rate>best_rate and rate<E and water+salt <=F:
          best_rate=rate
          best_salt=salt
          best_water=water
print(best_water+best_salt,best_salt)

Submission status: Runtime Error

Solution code:
#Collecting.py
N,K=input(),input()
x = (int(_) for _ in raw_input().split()) 

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