Problem:


Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = [int(v) for v in input().split()]
x = [int(v) for v in input().split()]

ans = 10 ** 9
for i in range(n - k + 1):
    left = i
    right = i + k - 1
    if x[right] <= 0:
        ans = min(ans, abs(x[left]))
    elif 0 <= x[left]:
        ans = min(ans, abs(x[right]))
    else:
        diff = abs(x[right] - x[left])
        ans = min(ans, diff + abs(x[right]), diff + abs(x[left])) 

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
          (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

for i in range(1, len(x) - K + 1):
    left = i
    right = i + K - 1

    letf_to_right = abs(x[left]) + abs(x[right] - x[left])
    right_to_left = abs(x[right]) + abs(x[left] - x[right])

    tmp = min(letf_to_right, right_to_left)

    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
def solution(a, k):
    n = len(a)
    p, q = [0], []
    for x in a:
        if x < 0:
            q.append(-x)
        else:
            p.append(x)
    q = [0] + q[::-1]
    foo = min(p[-1], q[-1]) * 2 + max(p[-1], q[-1])
    for x in xrange(max(0, k - len(p) + 1), min(k, len(q) - 1) + 1):
        y = k - x
        foo = min(min(q[x], p[y]) * 2 + max(q[x], p[y]), foo)
    return foo

def main():
    n, k = [int(i) for i in raw_input().strip().split()]
    a = [int(i) for i in raw_input().strip().split()]
    print(solution(a, k))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
T=10**9
for i in range(N-K+1):
    xr=x[i]
    xl=x[i+K-1]
    d1=abs(xr)+xl-xr
    d2=abs(xl)+xl-xr
    if T>d1:
        T=d1
    if T>d2:
        T=d2   
print(T)

Submission status: Accepted

Solution code:
def binarySearch(list_a, key):
    left = 0
    right = len(list_a)
    while left < right:
        mid = (left + right) // 2
        if list_a[mid] == key:
            return mid
            break
        elif key < list_a[mid]:
            right = mid
        else:
            left = mid+1
    else:
        return mid

n,k = map(int, input().split())
list_x = list((map(int, input().split())))
ans = 10**9
key_point = binarySearch(list_x,0)
for i in range(key_point-k,key_point+1):
    if i < 0:
        continue
    try:
        dist = min(abs(list_x[i]),abs(list_x[i+k-1])) + (list_x[i+k-1] - list_x[i])
        if ans > dist:
            ans = dist
    except:
        pass
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if N == 1:
    min_d = abs(x[0])
else:
    for i ,x_i in enumerate(x[:-(K-1)]):
        d = min(abs(x_i), abs(x[i+(K-1)])) + abs(x_i - x[i+(K-1)])
        if d < min_d:
            min_d = d

print(min_d)

Submission status: Accepted

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

Submission status: Accepted

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

l = int(1e9)
idx = 0
for i in range(n-k+1):
    if x[i] < 0 and x[i+k-1] > 0:
        lx = x[i+k-1] - x[i] + min(abs(x[i]), x[i+k-1])
    else:
        lx = max(abs(x[i]), x[i+k-1])
    if l >= lx:
        l = lx
        idx = i

print(l)

Submission status: Accepted

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

# 正解
res = min(min(abs(l), abs(r)) + r - l for l, r in zip(cp, cp[K-1:]))

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = [int(x) for x in input().split()]
D = [X[i + K - 1] - X[i] + min(abs(X[i + K - 1]), abs(X[i])) for i in range(N - K + 1)]
print(min(D))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(a) - K + 1):
  l = a[i]
  r = a[i + K - 1]
  ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
c = list(map(int, input().split()))
ans = float('Inf')
for i in range(n-k+1):
  l, r = c[i], c[i+k-1]
  ans = min(abs(l) + abs(r-l), abs(r) + abs(r-l), ans)

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(a) - K + 1):
  l = a[i]
  r = a[i+K-1]
  ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split(" "))
Xs = [int(a) for a in input().split()]
 
 
min = 3*(10**8)
 
p=0
z=0
m=0
Y=[]
for x in Xs:
    if x > 0:
        p += 1
        Y.append(x)
    elif x < 0:
        m += 1
    else:
        z +=1
 
    
K -= z
Z=[]
for i in range(m-1,-1,-1):
    Z.append(-Xs[i])
    
 
if K == 0:
    min = 0
if K-1 < p and K-1 >= 0:
    if Y[K-1] < min:
        min = Y[K-1]
 
if K-1 < m and K-1 >= 0:
    if Z[K-1] < min:
        min = Z[K-1]
 
for i in range(p):
    if i < K-1:
        if K-i-2 < m:
            if Y[i] * 2 + Z[K-i-2] < min:
                min = Y[i] * 2 + Z[K-i-2]
    
for i in range(m):
    if i < K-1:
        if K-i-2 < p:
            if Z[i] * 2 + Y[K-i-2] < min:
                min = Z[i] * 2 + Y[K-i-2]
 
               
print (min)

Submission status: Accepted

Solution code:
n,k = map(int, raw_input().split())
l = map(int, raw_input().split())
mn = 10**9
for i in range(n-k+1):
    if l[i] * l[i+k-1] < 0:
        p = max(abs(l[i]), abs(l[i+k-1])) + 2 * min(abs(l[i]), abs(l[i+k-1]))
    else:
        p = max(abs(l[i]), abs(l[i+k-1]))
    mn = min(mn, p)
print mn

Submission status: Accepted

Solution code:
#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))

# n, k = 5, 3
# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]
if not (0 in ls):
    ls.append(0)
    k+=1
#print (ls)

ls = np.array(ls)
ls.sort()
#print (ls)

idx = np.where(ls==0)[0][0]
#print (idx,k)
#print (idx+k)
start = max(0,idx-k)
goal = min(idx+k,len(ls)-1)

#print (start,goal)
answer = []
for i in range(goal-(k-1)+1):
    #print (i,i+k-1)
    mi = min(abs(ls[i]),abs(ls[i+k-1]))
    ma = max(abs(ls[i]),abs(ls[i+k-1]))
    #print (mi,ma)
    answer.append(2*mi+ma)
print (min(answer))
# def set_max(ls):
#     if len(ls)>0:
#         return set([max(ls)])
#     else:
#         return set()
# def set_min(ls):
#     if len(ls)>0:
#         return set([min(ls)])
#     else:
#         return set()
#
#
# plus = list(filter(lambda n:n>=0, ls))
# minus = list(filter(lambda n:n<=0, ls))
#
# neighbor = set_max(minus) | set_min(plus)
#
# #print (neighbor)
# answer = []
# cnt = 0
# for i in range(n-k+1):
#     if len(set(ls[i:i+k])&neighbor)>0:
#
#         #print (ls[i:i+k])
#         #print (np.sign(ls[i]*ls[i+k-1]))
#         temp = [abs(ls[i]),abs(ls[i+k-1])]
#         if np.sign(ls[i]*ls[i+k-1])<0:
#
#             answer.append(2*min(temp)+max(temp))
#         else:
#             answer.append(max(temp))
#         cnt += 1
#         if cnt == k + 1:
#             break
# print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []
# for i in range(size):
#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))

Submission status: Accepted

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

m = min(b - a + min(abs(a), abs(b)) for a, b in zip(x, x[K-1:]))
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 3*10**8

for i in range(N-K+1):
    ans1 = abs(x[i])+abs(x[i]-x[i+K-1])
    ans2 = abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans = min(ans, ans1, ans2)
print(ans)

Submission status: Accepted

Solution code:
"""
C - Candles
X ろうそく間の距離を出して、K本目まで距離を足せば良い
O 右にいくか　左に行くか　どっちが良いか
"""

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

candles = []
for part in input().split():
    candles.append(int(part))

min_dist = 10 ** 10
for i in range(N - K + 1):
    l = i
    r = i + K - 1

    # 右に行ったとき
    r_dist = abs(candles[r]) + abs(candles[l] - candles[r])
    # 左に行ったとき
    l_dist = abs(candles[l]) + abs(candles[r] - candles[l])

    min_dist = min(min_dist, r_dist, l_dist)

print(min_dist)

Submission status: Accepted

Solution code:
def nl(): return list(map(int, input().split()))

n, k = nl()
x = nl()

ans = float('inf')
for i in range(n - k + 1):
    ans = min(
            ans,
            abs(x[i]) + abs(x[i + k - 1] - x[i]),
            abs(x[i + k - 1]) + abs(x[i + k - 1] - x[i]),
        )
print(ans)

Submission status: Accepted

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

score = []
for i in range(n-k+1):
    l = abs(x[i]) + (x[i+k-1] - x[i])
    r = abs(x[i+k-1]) + (x[i+k-1] - x[i])
    score.append(l)
    score.append(r)
score.sort()
print(score[0])

Submission status: Accepted

Solution code:
N, k = [ int(i) for i in input().split()]
cPos = [ int(i) for i in input().split()]
time = 10**10
for i in range(N-k+1):
    if time > min( abs(cPos[i])+ abs(cPos[i]- cPos[i+k-1]), abs(cPos[i+k-1])+ abs(cPos[i]- cPos[i+k-1])):
        time = min( abs(cPos[i])+ abs(cPos[i]- cPos[i+k-1]), abs(cPos[i+k-1])+ abs(cPos[i]- cPos[i+k-1]))
print(time)

Submission status: Accepted

Solution code:
import bisect

n,k = map(int, input().split())
x = list(map(int, input().split()))
j = bisect.bisect_right(x,0)
if x[-1] > 0:
    j += 1
ans = float("INF")
for i in range(max(j-1-k,0), max(min(j,n-k+1),0)):
    key = x[i+k-1]-x[i]
    ans = min(ans, min(abs(x[i])+key, abs(x[i+k-1])+key))
print(ans)

Submission status: Accepted

Solution code:
import sys

if __name__ == '__main__' :
    
    N,K = map(int, input().split())
    x = list(map(int, input().split()))

    min_dis = sys.maxsize
    for i in range(0,N-K+1):
        l = i
        r = i + K - 1 
        # 左に行った後に右に行くパターン
        dis_l = abs(x[l]) + abs(x[r]-x[l])
        # 右に行った後に左に行くパターン
        dis_r = abs(x[r]) + abs(x[r]-x[l])
        min_dis = min(min_dis,dis_l,dis_r)
    print(min_dis)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
tmin=1000000000
for i in range(N-K+1):
    a=x[i+K-1]
    b=x[i]
    res=a-b+min([abs(a),abs(b)])
    if tmin>res:
        tmin=res
print(tmin)

Submission status: Accepted

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

ans = 999999999

for i in range(n - k + 1):
    a = abs(x[i]) + (x[i + k - 1] - x[i])
    b = abs(x[i + k - 1]) + (x[i + k - 1] - x[i])
    ans = min(ans, a, b)
    
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A = list(map(int, input().split()))
i = 0
ans = 1000000000
while i <= N-K:
  d1 = abs(A[i])+(A[i+K-1]-A[i])
  d2 = abs(A[i+K-1])+(A[i+K-1]-A[i])
  ans = min(ans,d1,d2)
  i += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

x_upper = []

for i in reversed(x):
    if i<0:
        break
    if i>=0:
        x_upper.append(i)
        x.pop()
        
#絶対値昇順へソート
x.reverse()
x_upper.reverse()

lenXu = len(x_upper)
lenX = len(x)

if K>lenXu:
    l = K-lenXu-1
    h = lenXu-1
else:
    l = -1
    h = K-1

S = []
while l<K and l<lenX:
    if h<0:
        S.append(x[l]*(-1))
    elif l<0:
        S.append(x_upper[h])
    else:
        if (x[l]*(-1))<x_upper[h]:
            S.append((x[l]*(-1))*2 + x_upper[h])
        else:
            S.append((x[l]*(-1)) + x_upper[h]*2)
    l += 1
    h -= 1

print(min(S))

Submission status: Accepted

Solution code:
#abc107c

n,k=map(int,raw_input().split())
x=map(int,raw_input().split())
res=float('inf')
for i in xrange(n-k+1):
 l=x[i]
 r=x[i+k-1]
 res=min(res,abs(l)+abs(r-l),abs(r)+abs(l-r))
print res

Submission status: Accepted

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

sorted_X = sorted(X, key=abs)

centers = []

if sorted_X[0] == 0:
    # 0がある場合
    centers.append(X.index(0))
else:
    for x in sorted_X:
        if x > 0:
            centers.append(X.index(x))
            break

    for x in sorted_X:
        if x < 0:
            centers.append(X.index(x))
            break


#edge_pairs = []
lefts = []
# ひだりエッジ
for center_index in centers:
    left_edge = center_index - (K - 1)
    if left_edge >= 0:
        start_left_edge_index = left_edge
        start_right_edge_index = center_index
    else:
        start_left_edge_index = 0
        start_right_edge_index = K-1

    for move in range(K):
        left_edge = start_left_edge_index + move
        right_edge = start_right_edge_index + move
        if right_edge >= N:
            break
        #edge_pairs.append((left_edge, right_edge))
        lefts.append(left_edge)
lefts = list(set(lefts))

start_left = min(lefts)
search_num = len(lefts)

pos_max = 0
neg_max = 0

propose = []
for i in range(start_left, start_left + K):
    target = X[i]
    if target > 0:
        if pos_max < target:
            pos_max = target

    if target < 0:
        if neg_max < abs(target):
            neg_max = abs(target)

propose.append(pos_max + 2 * neg_max)
propose.append(neg_max + 2 * pos_max)


for slide in range(search_num-1):
    start_left += 1
    left = start_left
    right = start_left + K - 1

    left_target = X[left]
    if left_target < 0:
        neg_max = abs(left_target)
    elif target == 0:
        neg_max = 0

    right_target = X[right]
    if right_target > 0:
        pos_max = right_target
    elif right_target == 0:
        pos_max = 0

    propose.append(pos_max + 2 * neg_max)
    propose.append(neg_max + 2 * pos_max)


print(min(propose))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
mintime = 1e9
for i in range(N-K+1):
    time1 = abs(x[i])+x[i+K-1]-x[i]
    time2 = abs(x[i+K-1])+x[i+K-1]-x[i]
    time = min([time1,time2])
    if time < mintime:
        mintime = time
print(mintime)

Submission status: Accepted

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

print(min([min(abs(x[i]), abs(x[i+k-1])) + (x[i+k-1] - x[i]) for i in range(n-k+1)]))

Submission status: Accepted

Solution code:
import itertools

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

pos = [0]
neg = []
for i in x:
    if i<0:
        neg.append(-i)
    else:
        pos.append(i)
neg = [0] + neg[::-1]

# negを往復する場合
neg_twice = []
for i in range(min(len(neg),k)):
    neg_twice.append(neg[i]*2)
for i in range(len(neg_twice)):
    if k-i<len(pos):
        neg_twice[i] += pos[k-i]
    else:
        neg_twice[i] = float('inf')
# posを往復する場合
pos_twice = []
for i in range(min(len(pos),k)):
    pos_twice.append(pos[i]*2)
for i in range(len(pos_twice)):
    if k-i<len(neg):
        pos_twice[i] += neg[k-i]
    else:
        pos_twice[i] = float('inf')
print(min(pos_twice+neg_twice))

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(N-K+1):
    b.append(min(abs(a[i])+abs(a[i+K-1]-a[i]),abs(a[i+K-1])+abs(a[i]-a[i+K-1])))
print(min(b))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x_coordinate = list(map(int, input().split()))
zero_pos = 0
distance = []

while True:
  try:
    if x_coordinate[zero_pos] >= 0:
      break
    else:
      zero_pos += 1
  except:
    zero_pos = N - 1
    break

for i in range(zero_pos - K, zero_pos + 1):
  if (i >= 0) and (i + K - 1 < N):
    start_pos = x_coordinate[i]
    end_pos = x_coordinate[i + K - 1]
    if start_pos*end_pos >= 0:
      if abs(start_pos) > abs(end_pos):
        distance.append(abs(start_pos))
      else:
        distance.append(abs(end_pos))
    else:
      if abs(start_pos) >= abs(end_pos):
        total = abs(start_pos) + 2*abs(end_pos)
        distance.append(total)
      else:
        total = 2*abs(start_pos) + abs(end_pos)
        distance.append(total)
      
print(min(distance))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def make_time(l, k):
	r = []
	for i in range(k-1, len(l)):
		r.append(l[i] - l[i-k+1])
	return r

if __name__ == '__main__':
	N, K  = list(map(int, input().split()))
	x = list(map(int, input().split()))
	x_time = make_time(x, K)
	times1 = [(x_time[i] + abs(x[i])) for i in range(N - K + 1)]
	x.reverse()
	x_time.reverse()
	times2 = [(x_time[i] + abs(x[i])) for i in range(N - K + 1)]
	times = times1 + times2
	print(min(times))

Submission status: Accepted

Solution code:
# coding: utf-8

def getMin(X, K):
    minVal = float('inf')
    for l in range(len(X)-K+1):        
        minVal = min(minVal, abs(X[l]) + abs(X[K + l - 1] - X[l]))
        minVal = min(minVal, abs(X[K + l - 1]) + abs(X[l] - X[K + l - 1]))
    return minVal

if __name__ == "__main__":
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    print(getMin(X, K))

Submission status: Accepted

Solution code:
str = input()
n,k = [int(x) for x in str.split()]
str = input()
list = [int(x) for x in str.split()]
ans = 1000000000
for i in range(0,n - k + 1):
	a = list[i]
	b = list[i + k - 1]
	if (a < 0) & (b > 0): #优先级需要加括号
	    ans = min(ans,min(b - a * 2,b * 2 - a))
	else:
		ans = min(ans,max(abs(a),abs(b)))
print(ans)

Submission status: Accepted

Solution code:
import math
N,K = map(int, input().split())
a = list(map(int,input().split()))
l = 0
r = K - 1
Min = float("inf")
for i in range(N - K + 1):
    if(a[l]*a[r]<=0):#原点を跨ぐとき
        Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    elif(l == 0 and r == N-1):
        Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))        
    elif(l==0):#左端の時
        if(a[r]*a[r+1]<0 or a[0]>0 or (a[N-1]<0 and r == N-1)):
            Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    elif(r==N-1):#右端の時
        if(a[l-1]*a[l]<0 or (a[l]>0 and l == 0) or a[r] <= 0):
            Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    else:
        if(a[r]*a[r+1]<0 or a[l-1]*a[l]<0):
            Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    l = l + 1
    r = r + 1
print(Min)

Submission status: Accepted

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

for i in range(N-K+1):
    l = x[i]
    r = x[i+K-1]
    ans = min(ans, abs(l) + abs(r-l), abs(r)+abs(l-r))
    
print(ans)

Submission status: Accepted

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


def main():
    n, k = list(map(int, input().split()))
    x = list(map(int, input().split()))
    ans = float('inf')

    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]
        abs_left = abs(left)
        abs_right = abs(right)

        if left * right >= 0:
            ans = min(ans, max(abs_left, abs_right))
        else:
            ans = min(ans, 2 * abs_left + abs_right, abs_left + 2 * abs_right)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    n, k = list(map(int, input().split()))
    x = list(map(int, input().split()))
    ans = float('inf')

    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]

        if left * right >= 0:
            ans = min(ans, max(abs(left), abs(right)))
        else:
            ans = min(ans, 2 * abs(left) + abs(right), abs(left) + 2 * abs(right))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if 0 in a:
  K = K - 1
  if K == 0:
    print('0')
    exit()

aLow = list(filter(lambda x: x < 0, a))
aLow = list(map(lambda x: abs(x), aLow))
aLow.sort()

aHigh = list(filter(lambda x: x > 0, a))

least = 99999999999999999

#print(aLow)
#print(aHigh)

for i in range(0,K+1):
  j = K - i
  if i > len(aLow) or j > len(aHigh):
    continue

  if i == 0:
    dist = aHigh[j-1]
  elif j == 0:
    dist = aLow[i-1]
  else:
    dist = min(2*aLow[i-1]+aHigh[j-1], aLow[i-1]+2*aHigh[j-1])

  if least > dist:
    least = dist

print(least)

Submission status: Accepted

Solution code:
import sys

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

ans = sys.maxsize

for i in range(K, N + 1):
    a = x[i - K]
    b = x[i - 1]

    ans = min(ans, max(abs(a), abs(b)) + min(abs(a), abs(b)) * 2 * (a * b < 0))

print(ans)

Submission status: Accepted

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

minus = 0
while minus < N and array[minus]<0:
    minus += 1

first,last = max(minus-K,0),min(minus,N-K)

ans = 2*(array[N-1]- array[0])

for i in range(first,last+1):
    m_dist, p_dist = array[i], array[K-1+i]
    if m_dist*p_dist <=0:
        dist_i = min(abs(m_dist),abs(p_dist))*2+max(abs(m_dist),p_dist)
    else:
        dist_i = max(abs(m_dist),abs(p_dist))
    ans = min(ans,dist_i)

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1e9
for i in range(n-k+1):
    l, r = i, i+k-1
    d = min(abs(x[l]) + abs(x[l]-x[r]), abs(x[r]) + abs(x[l]-x[r]))
    ans = min(ans, d)
print(ans)

Submission status: Accepted

Solution code:
n, k = input().split()
n = int(n)
k = int(k)
xs = input().split()
xs = [int(x) for x in xs]
ans = 1e9
for i in range(0, n-k+1):
    l = xs[i]
    r = xs[i + k - 1]
    if l < 0 and r > 0:
        ans = min(ans, abs(l) + 2 * r)
        ans = min(ans, abs(l) * 2 + r)
    else:
        ans = min(ans, max(abs(l), abs(r)))
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int, input().split()))
a=[]
for i in range(n-k+1):
    j=i+k-1
    if x[i]<=0 and x[j]<=0:
        a.append(abs(min(x[i],x[j])))
    elif x[i]<=0 and x[j]>=0:
        a.append(2*min(abs(x[i]),abs(x[j]))+abs(max(abs(x[i]),abs(x[j]))))
    elif x[i]>=0 and x[j]>=0:
        a.append(max(x[i], x[j]))
print(min(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
tmp = []
for i in range(n-k+1):
    tmp.append(abs(x[i])+abs(x[i+k-1]-x[i]))
x_2 = x
x_2.reverse()

for j in range(n-k+1):
    tmp.append(abs(x_2[j])+abs(x_2[j+k-1]-x_2[j]))
print(min(tmp))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = pow(10, 10)
for i in range(N-K+1):
    temp_l = abs(x[i]) + abs(x[i]-x[i+K-1])
    temp_r = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans, temp_l, temp_r)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
ans=10**9
for i in range(N-K+1):
    tmp1=abs(x[i])+abs(x[i]-x[i+K-1])
    tmp2=abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans=min([ans,tmp1,tmp2])
print(ans)

Submission status: Accepted

Solution code:
# Q3
N, K = map(int, input().split())
x = [int(_) for _ in input().split()]

def distance(left, right):
    if right < 0:
        return abs(left)
    elif left > 0:
        return right
    else:
        return -left + right + min(-left, right)

min_dist = float('inf')
for left in range(N):
    right = left + K - 1
    if right >= N:
        break
    dist = distance(x[left], x[right])
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
ans = []
for i in range(n):
    if (i + k -1) >= n:
        break
    x1 = x[i]
    x2 = x[i+k-1]
    ans1 = abs(x2) + abs(x2-x1)
    ans2 = abs(x1) + abs(x1-x2)
    ans.append(ans1)
    ans.append(ans2)
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K =list(map(int, input().split()))
x=list(map(int, input().split()))
ans=10000000000000
for i in range(N-K+1):
    if min(abs(x[i])+abs(x[i+K-1]-x[i]), abs(x[i+K-1])+abs(x[i+K-1]-x[i]))<ans:
        ans=min(abs(x[i])+abs(x[i+K-1]-x[i]), abs(x[i+K-1])+abs(x[i+K-1]-x[i]))
print(ans)

Submission status: Accepted

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

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

ans = 10000000000010

for i in range(n):
    if i+k-1 < n:
        if x[i] >= 0:
            ans = min(ans, x[i+k-1])
        elif x[i+k-1] <= 0:
            ans = min(ans, -x[i])
        else:
            ans = min(ans, -x[i]*2+x[i+k-1])
            ans = min(ans, x[i+k-1]*2-x[i])

print(ans)

Submission status: Accepted

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

dist = []
for i in range(N-K+1):
    dist.append(abs(X[i]) + abs(X[K+i-1]-X[i]))
    dist.append(abs(X[K+i-1]) + abs(X[i]-X[K+i-1]))

if not len(dist) == 0:
    print(min(dist))
else:
    print(0)

Submission status: Accepted

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

dist = float('inf')
for i in range(n-(k-1)):
    l = x[i]
    r = x[i+k-1]
    temp = min(abs(l), abs(r)) + abs(l-r)
    dist = min(dist, temp)

print(dist)

Submission status: Accepted

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

for center in range(n):
    if x[center] >= 0:
        break

if center == n - 1:
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - i]))

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,raw_input().split())
a = map(int,raw_input().split())
b = []
for i in range(N-K+1):
  k_1 = a[i+K-1]-a[i]+abs(a[i])
  k_2 = a[i+K-1]-a[i]+abs(a[i+K-1])
  if k_1<k_2:
    b.append(k_1)
  else:
    b.append(k_2)
print(min(b))

Submission status: Accepted

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

# 頭の体操

# 一次元部分探索: N-K+1 通り
# 右から辿る：|xr| + |xl - xr|
# 左から辿る：|xl| + |xr - xl|

distance = []
for i in range(n-k+1):
    distance.append(abs(x[i]) + abs(x[k+i-1]-x[i]))
    distance.append(abs(x[k+i-1]) + abs(x[i]-x[k+i-1]))

if not len(distance) == 0:
    print(min(distance))
else:
    print(0)

Submission status: Accepted

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

tmin = float('inf')

for i in range(N - K + 1):
    d = x[i + K - 1] - x[i]
    tmin = min(tmin, d + abs(x[i]), d + abs(x[i + K - 1]))

print(tmin)

Submission status: Accepted

Solution code:
def main():
    """
    1 <= N <= 10^5
    1 <= K <= N
    |xi| <= 10^8
        -10^8 <= xi <= 10^8
    xs: sorted
    """
    N, K = map(int, input().split())
    *x, = map(int, input().split())
    # ans = WA(N, K, x)
    ans = editorial(N, K, x)
    print(ans)


def editorial(N, K, x):
    from bisect import bisect_left
    try:
        # N=7,K=3
        # [-3,-2,-1,0,1,2,3]
        #      |----*---|
        idx = x.index(0)
        left = max(idx - K + 1, 0)
        right = min(idx + 1, N - K)
    except:
        # N=6,K=3
        # [-3,-2,-1,1,2,3]
        #   |-----|*|---|
        idx = bisect_left(x, 0)
        left = max(idx - K, 0)
        right = min(idx + 1, N - K)

    _range = range(left, right + 1)
    # print(idx, _range)
    ans = float("inf")
    for left in _range:
        right = left + K - 1
        # print(left, right)
        a1 = abs(x[left]) + abs(x[right] - x[left])
        a2 = abs(x[right]) + abs(x[left] - x[right])
        ans = min([ans, a1, a2])

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = (int(v) for v in input().split())
x = [int(v) for v in input().split()]

m = 99999999999999999999
for l in range(n-k+1):
    r = l + k - 1
    m = min(m, abs(x[l]) + abs(x[r] - x[l]))
    m = min(m, abs(x[r]) + abs(x[l] - x[r]))
print(m)