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:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
x = [int(x) for x in input().split()]

ans = 10**15
for i in range(N-K+1) :
    if x[i]*x[i+K-1] >= 0 :
        temp = max(abs(x[i]), abs(x[i+K-1]))
    elif abs(x[i]) > x[i+K-1] :
        temp = abs(x[i]) + 2*x[i+K-1]
    else :
        temp = x[i+K-1] + 2*abs(x[i])
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

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

ans = float("inf")
for i in range(n-k+1):
    l = a[i]
    r = a[i+k-1]
    time = min(abs(l) + abs(l-r), abs(r) + abs(l-r))
    # print(l, r, time)
    ans = min(ans, time)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

print(time)

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
arr = X[K - 1:] - X[:-K + 1]
arr1 = arr + np.abs(X[:-K + 1])
arr2 = arr + np.abs(X[K - 1:])
if len(arr) == 0:
    print(0)
else:
    ans = min(arr1.min(), arr2.min())
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
X = np.array(list(map(int, input().split())))
idx = np.searchsorted(X, 0)
# print('idx', idx)
ans = 10**10
for i in range(N - K + 1):
    l = i
    r = i + K - 1
    d1 = np.abs(X[l]) + np.abs(X[r] - X[l])
    d2 = np.abs(X[r]) + np.abs(X[r] - X[l])
    d = min(d1, d2)
    ans = min(ans, d)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, k = [int(e) for e in input().split()]
    X = [int(e) for e in input().split()]

    ans = float('inf')
    for i in range(n - k + 1):
        d = None
        add = None
        if abs(X[i]) > abs(X[i + k - 1]):
            d = abs(X[i])
            add = abs(X[i + k - 1])*2
        else:
            d = abs(X[i + k - 1])
            add = abs(X[i])*2

        if X[i] * X[i + k - 1] < 0:
            d += add

        ans = min(ans, d)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

lis = [0]
for i in range(1,n):
    lis.append(lis[i-1] + abs(x[i]-x[i-1]))
#print(lis)
#print(x)
ans = int(1e10)
for i in range(n-k+1):
    val = min(abs(x[i]),abs(x[i+k-1])) + (lis[i+k-1]-lis[i])
    #print(val,ans)
    #print(ans)
    if val < ans:
        ans = val

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    X = LI()
    X = sorted(X)
    pos_q, neg_q = [], []
    cur_i = N - 1
    for x in X:
        if x < 0:
            neg_q.append(x)
        elif x == 0:
            K -= 1
        else:
            pos_q.append(x)
    if K == 0:
        print(0)
        return
    ans = inf
    pos_q = sorted(pos_q)
    neg_q = sorted(neg_q)[::-1]
    
    for ix, p in enumerate(pos_q):
        if K - ix - 1 > 0: # if still remains
            nokori = K - ix - 1
            if 0 <= nokori - 1 < len(neg_q):
                ans = min(abs(neg_q[nokori-1] - p - p), ans)
        else:
            ans = min(p, ans)

    for ix, n in enumerate(neg_q):
        if K - ix - 1 > 0:
            nokori = K - ix -1
            if 0 <= nokori - 1 < len(pos_q):
                ans = min(pos_q[nokori-1] + abs(n) + abs(n), ans)
        else:
            ans = min(abs(n), ans)
    print(ans)

main()

Submission status: Accepted

Solution code:
# 座標上の任意の2点を選んだとき、座標0からそのうちの1点に移動し、
# さらに別の1点に移動すれば、その2点間の座標はすべて通過したことになる
# よって、i番目の座標とi+(k-1)番目の座標を選択し、
# 座標0と近い方の座標との距離と2点間の距離の合計が最小となるようなiを選択すればよい

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_right

def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]
    o = bisect_right(x, 0)
    ans = 10**9
    for i in range(max(o - k, 0), min(o, n - k) + 1):
        a, b = x[i], x[i + k - 1]
        c, d = abs(a), abs(b)
        if a * b > 0:
            ans = min(max(c, d), ans)
        else:
            ans = min(min(c, d) * 2 + max(c, d), ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float("inf")
for left in range(0, N-K+1):
    right = left+K
    minus = 0
    plus = 0
    if X[left] < 0:
        minus = min(minus, X[left])
    if X[right-1] > 0:
        plus = max(plus, X[right-1])
    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)
 
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):
  d=x[i+k-1]-x[i]
  d+=min(abs(x[i]),abs(x[i+k-1]))
  ans=min(ans,d)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

result = []
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if l*r >= 0:
        result.append(max(abs(l), abs(r)))
    else:
        result.append(abs(2*l-r))
        result.append(abs(2*r-l))
print(min(result))

Submission status: Accepted

Solution code:
N,K=map(int,input().split(' '))
x = list(map(int,input().split(' ')))
ans = 2*(10**8)
if N==K:
    if x[-1]<0:
        print(-x[0])
    elif x[0]>0:
        print(x[-1])
    else:
        print(min(x[-1]-2*x[0],2*x[-1]-x[0]))
    exit()
for i in range(N-K+1):
    l,r=x[i],x[i+K-1]
    if l>0:
        ans = min(ans,r)
    elif r<0:
        ans = min(ans,-l)
    else:
        ans = min(ans,r-2*l,2*r-l)
print(ans)

Submission status: Accepted

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

ans=10**10
for i in range(N-K+1):
    if x[i]<=0 and x[i+K-1]<=0:
        ans=min(ans,abs(x[i]))
    elif x[i]<=0 and x[i+K-1]>=0:
        ans=min(ans,x[i+K-1]-x[i]+min(x[i+K-1],abs(x[i])))
    else:
        ans=min(ans,x[i+K-1])
print(ans)

Submission status: Accepted

Solution code:
import math
S = input().split(" ")
N = int(S[0])
K = int(S[1])
arr = [int(s) for s in input().split(" ")]


def calculate(n, k, arr):
    minsArr = []
    plusArr = []
    for ar in arr:
        if ar < 0:
            minsArr.append(ar)
        else:
            plusArr.append(ar)

    # find start index
    startIndex = -10000

    if len(minsArr) > k:
        startIndex = len(minsArr) - k
    else:
        startIndex = 0

    minValues = []
    while startIndex <= len(minsArr):

        if k == N:
            s1 = arr[0]
            s2 = arr[-1]
            minValue = math.fabs(s2 - s1) + min(math.fabs(s2), math.fabs(s1))
            minValues.append(minValue)
            break

        if startIndex + k > len(arr):
            break

        s1 = arr[startIndex]
        s2 = arr[startIndex + k - 1]

        minValue = 0

        if s1 * s2 < 0:
            minValue = math.fabs(s2 - s1) + min(math.fabs(s2), math.fabs(s1))
        else:
            # 全部为正数
            if s1 >= 0:
                minValue = math.fabs(s2)
            # 全部为负数
            else:
                minValue = math.fabs(s1)

        minValues.append(minValue)
        startIndex = startIndex + 1

    return int(min(minValues))


result = calculate(N, K, arr)
print(result)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
INF = float("inf")
cost = INF
for i in range(len(x)-k+1):
  l = x[i]
  r = x[i+k-1]
  tmp = min(abs(l)+abs(l-r),abs(r)+abs(r-l))
  cost = min(cost,tmp)
print(cost)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
answer = float('inf')
for l, r in zip(x, x[K - 1:]):
    # print(l, r)
    cost = r - l + min(abs(l), abs(r))
    answer = cost if cost < answer else answer

print(answer)

Submission status: Accepted

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

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

ans = 1e9

for i in range(len(x)-k+1):
    l = abs(x[i+k-1]-x[i]) + min(abs(x[i]),abs(x[i+k-1]))
    ans = min(ans,l)

print(ans)

Submission status: Accepted

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

    tmp = []
    for i in range(n-k+1):
        l = X[i]
        r = X[i+k-1]
        tmp.append(abs(l)+abs(r-l))
        tmp.append(abs(r)+abs(r-l))
    print(min(tmp))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_right
from bisect import bisect_left

n,k=map(int,input().split())
#n=100000
#k=100000

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

if x[n-1]<0 :
    t=-x[-k]
#    print(1)
    print(t)
elif x[n-1]==0 :
    t=-x[-k+1]
#    print(2)
    print(t)

elif x[0]>0:
    t=x[k-1]
#    print(3)
    print(t)
elif x[0]==0:
    t=x[k-1-1]
#    print(4)
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        k=k-1
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    else:
        tmin=10**9
#        print((iz-(k-1)+1),iz,n-1-(k-1))
        for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
            kt=i
            t1=-x[kt]+x[kt+(k-1)]*2
            t2=-x[kt]*2+x[kt+(k-1)]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
#            print(tmin,t1,t2)
            if t1<tmin:
                tmin=t1
            if t2<tmin:
                tmin=t2
    print(tmin)
    
#  299993628 
# 1000000000        
#   99990701

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
INF = 1001001001

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

ans = INF

for i in range(N-K+1):
    if x[i] < 0:
        neg = -x[i]
    else:
        neg = 0
    if x[i+K-1] > 0:
        pos = x[i+K-1]
    else:
        pos = 0
    tmp = neg + pos + min(neg, pos)
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

if len(x) == 1:
    print(abs(x[0]))
else:
    ans = 10 ** 9
    for i in range(n-k+1):
        tmp = min(abs(x[i]), abs(x[i+k-1]))
        ans = min(ans,tmp+abs(x[i+k-1]-x[i]))
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n,k=map(int,input().split())
X=list(map(int,input().split()))
X=np.array(X)
m=abs(X[:n-k+1])+abs(X[:n-k+1]-X[k-1:])
p=abs(X[k-1:])+abs(X[k-1:]-X[:n-k+1])
ans=np.min(np.minimum(p,m))
print(ans)

Submission status: Accepted

Solution code:
def main():
    import numpy as np
    n,k=map(int,input().split())
    X=list(map(int,input().split()))
    X=np.array(X)
    Xp,Xz,Xm=X[X>0],X[X==0],X[X<0]
    lp,lm=len(Xp),len(Xm)

    if 0 in Xz:
        k-=1
    if k==0:
      print(0)
      exit()
      
    ans=10**9
    if k<=lp:
        ans=min(ans,Xp[k-1])
    if k<=lm:
        ans=min(ans,-Xm[-k])

    for i in range(lp):
        j=k-i-1
        if 0<j<=lm:
            ans=min(ans,Xp[i]*2-Xm[-j])
        else:
            continue
        
    for i in range(lm,0,-1):
        j=k-i-1
        if 0<=j<lp:
            ans=min(ans,Xm[-i]*(-2)+Xp[j])
        else:
            continue
    print(ans)
   
if __name__=='__main__':
    main()

Submission status: Accepted

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

diffs = []
ranges = []
for i in range(N - K + 1):
  diffs.append(xn[i+K -1] - xn[i])
  ranges.append((i, i + K - 1))
  
sums = []
for diff, range_ in zip(diffs, ranges):
  r = min(abs(xn[range_[0]]), abs(xn[range_[1]]))
  sums.append(r+diff)
print(min(sums))

Submission status: Accepted

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

ans = float('inf')
for i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if l < 0 and r < 0:
        ans = min(ans, max(abs(l), abs(r)))
    elif l <= 0 and r >= 0:
        ans = min(ans, min(abs(l), r)*2 + max(abs(l), r))
    elif l > 0 and r > 0:
        ans = min(ans, max(l, r))
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
x.append(0)
x.sort()
x_0=x.index(0)

if x_0 == 0:
    print(x[k])
elif x_0==n:
    print(-x[-k-1])
else:
    r = min(n-x_0, k)
    l = min(x_0, k)
    ans1=float("inf")
    ans2=float("inf")
    #スタートで正に移動
    for i in range(1,r+1):
        if  k-i <= l:
            s = 2*x[x_0 + i] - x[x_0 - (k-i)]
            ans1=min(ans1, s)
    #スタートで負に移動
    for i in range(1,l+1):
        if k-i <= r:
            s = -2*x[x_0 - i] + x[x_0 + (k-i)]
            ans2=min(ans2, s)
    print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
X = list(map(int, input().split()))
l, r = -1, n
while r - l > 1:
    p = (l + r) // 2
    if X[p] < 0:
        l = p
    else:
        r = p

if l == -1:
    print(X[k - 1])
elif r == n:
    print(abs(X[n - k]))
elif n == k:
    print(min(2 * abs(X[0]) + X[-1], abs(X[0]) + 2 * X[-1]))
else:
    best_score = 10 ** 9
    for i in range(max(l - k + 1, 0), n - k):
        score = 0
        if i > r:
            break
        if X[i + k] < 0:
            score = abs(X[i])
        elif X[i] > 0:
            score = X[i + k - 1]
        elif abs(X[i]) < abs(X[i + k - 1]):
            score = 2 * abs(X[i]) + X[i + k - 1]
        else:
            score = abs(X[i]) + 2 * X[i + k - 1]
        if score < best_score:
            best_score = score
    print(best_score)

Submission status: Accepted

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

#全探索
Ans=10**9
for i in range(N-K+1):
    XL,XR=X[i],X[i+K-1]
    if XL*XR>0:
        Ans=min(Ans,max(abs(XR),abs(XL)))
    else:
        Ans=min(Ans,abs(XR)*2+abs(XL),abs(XL)*2+abs(XR))
print(Ans)

Submission status: Accepted

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

min_time = 10**10
for i in range(n-k+1):
    time = abs(x[i] - x[i+k-1]) + min(abs(x[i]), abs(x[i+k-1]))
    min_time = min(time, min_time)

print(min_time)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
arr = list(map(int,input().split()))
ans = float('inf')
for i in range(N-K+1):
  if (arr[i+K-1]<=0)&(arr[i]<=0):
    path = abs(arr[i])
  elif (arr[i+K-1]>0)&(arr[i]<=0):
    path  = arr[i+K-1]-arr[i] + min(abs(arr[i+K-1]),abs(arr[i]))
  else:
    path  = abs(arr[i+K-1])
  
  ans = min(path,ans)

print(ans)

Submission status: Accepted

Solution code:
f = lambda:map(int,input().split())
n,k = f()
x = list(f())
d = 10**9
for i in range(n-k+1):
  l,r = x[i],x[i+k-1]
  d = [min(d,max(-l,r)),min(d,(r-l)+min(-l,r))][l<0<r]
print(d)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 1000000000

for i in range(n - k + 1):
  l = i
  r = i + k - 1
  cost = min(abs(x[r]) + abs(x[r] - x[l]), abs(x[l]) + abs(x[r] - x[l]))
  ans = min(ans, cost)

print(ans)

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):
    ans.append(min(abs(x[i])+x[i+k-1]-x[i] , abs(x[i+k-1])+(x[i+k-1]-x[i])))
    
print(min(ans))

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    s = 5 * (10 ** 8)
    for i in range(N-K+1):
        if x[i] * x[K - 1 + i] < 0:
            s = min(s, min(x[K - 1 + i], abs(x[i])) * 2 + max(x[K - 1 + i], abs(x[i])))
        else:
            s = min(s, max(abs(x[K - 1 + i]), abs(x[i])))
    print(s)
main()

Submission status: Accepted

Solution code:
def examA():
    N, i = LI()
    ans = N - i + 1
    print(ans)
    return

def examB():
    H, W = LI()
    A = [SI() for _ in range(H)]
    h = [[]for _ in range(H)]
    for i in range(W):
        flag = False
        for j in range(H):
            if A[j][i]=="#":
                flag = True
                break
        if flag:
            for j in range(H):
                h[j].append(A[j][i])
    #print(h)
    ans = []
    for i,a in enumerate(h):
        if "#" in a:
            ans.append(i)
    for v in ans:
        print("".join(map(str,h[v])))
    return

def examC():
    N, K = LI()
    X = LI()
    ans = inf
    for i in range(N-K+1):
        if X[i]<0 and X[i+K-1]>0:
            cur = (-X[i] + X[i+K-1]) + min(-X[i], X[i+K-1])
        else:
            cur = max(-X[i], X[i+K-1])
        ans = min(ans,cur)
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

"""

"""

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):
    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:
def main():
    INF = 10**18
    N,K = map(int,input().split())
    x = list(map(int,input().split()))
    ans = INF
    for i in range(K-1,N):
        ans = min(ans,abs(x[i]-x[i-K+1])+min(abs(0-x[i]),abs(0-x[i-K+1])))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  N, K = map(int, input().split(" "))
  X = list(map(int, input().split(" ")))
  answer = 10 ** 9 + 7
  for i in range(0, N - K + 1):
    time = X[i + K - 1] - X[i] + min(abs(X[i]), abs(X[i + K - 1]))
    answer = min(answer, time)
  print(answer)


if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a
 
show_flg=False
show_flg=True
 
ans=0

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

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float("inf")
for i in range(N-K+1):
    l,r = x[i],x[i+K-1]
    if l*r>=0:
        ans = min(ans,max(abs(l),abs(r)))
    else:
        ans = min(ans,min(abs(l)*2+r,abs(l)+r*2))
print(ans)

Submission status: Accepted

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

xp = []
xm = []
for i in range(N):
  if x[i] > 0:
    xp.append(x[i])
  elif x[i] < 0:
    xm.append(-x[i])
  else:
    K -= 1
xm.reverse()

ans = 1000000000
n = max(0, K - len(xp) )
for i in range(n, min(len(xm)+1,min(len(xp),K))):
  if i == 0:  m = 0
  else:       m = xm[i-1]
  ans = min(ans, m * 2 + xp[K - i - 1] )
#  print("p ",i,m,xp[K - i - 1],ans)

n = max(0, K - len(xm) )
for i in range(n, min(len(xp)+1,min(len(xm),K))):
  if i == 0:  p = 0
  else:       p = xp[i-1]
  ans = min(ans, p * 2 + xm[K - i - 1] )
#  print("m ",i,ans)
if ans == 1000000000: ans = 0
print(ans)

Submission status: Accepted

Solution code:
def main():
    # editorial読んだ．
    N, K = map(int, input().split(" "))
    coordinates = list(map(int, input().split(" ")))
    result = 10 ** 9 + 7
    for i in range(N - K + 1):
        result = min(result, abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i]),
                     abs(coordinates[i + K - 1] - coordinates[i]) + abs(coordinates[i + K - 1]))
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

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

x0=bisect.bisect_right(x,0)

ans=10**9
for i in range(K):
    if x0+i>=N:
        break
    r=x[x0+i]
    if x0-K+i+1>=0 and x0-K+i+1 <x0:
        l=-1*x[x0-K+i+1]
        ans=min(ans,r*2+l,l*2+r)
    
if x0+K-1<N:
    ans=min(ans,x[x0+K-1])

if x0-K>=0:
    ans=min(ans,-1*x[x0-K])
    
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
xxx = list(map(int, input().split()))
print(min(min(abs(l), abs(r)) + r - l for l, r in zip(xxx, xxx[k - 1:])))

Submission status: Accepted

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

ans = 10**10

for i in range(N - K + 1):  
  left = li[i]
  right = li[i + K - 1]
  ans = min(ans, min(abs(left), abs(right)) + right - left)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=[int(i) for i in input().split()]
X=[int(i) for i in input().split()]
m=10**9
for i in range(N-K+1):
    m=min(m,X[K-1+i]-X[i]+min(abs(X[K-1+i]),abs(X[i])))

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):
    l = x[i]
    r = x[i+k-1]
    ans.append(abs(r-l)+min(abs(l),abs(r)))
print(min(ans))

Submission status: Accepted

Solution code:
import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
x=[-10**10]*k+x+[10**10]*k
s=bisect.bisect_left(x,0)
if x[s]==0:
    m=min(-x[s-(k-1)],x[s+(k-1)])
    for j in range(k):
        m=min(m,2*(x[s+j]-x[s-(k-1)+j])-max(-x[s-(k-1)+j],x[s+j]))
else:
    m = min(-x[s-1 - (k - 1)], x[s + (k - 1)])
    for j in range(k-1):
        m = min(m, 2 * (x[s + j] - x[s - (k - 1) + j]) - max(-x[s - (k - 1) + j], x[s + j]))
print(m)

Submission status: Accepted

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

ans = 10 ** 18
for r in range(k-1, len(x)):
    ans = min(ans, abs(x[l]) + abs(x[r] - x[l]))
    ans = min(ans, abs(x[r]) + abs(x[r] - x[l]))
    l += 1
print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)


cnt = 10000000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    #リストがK以下の場合不具合が起きる
    #for i in range(1,K):   
    #len(P)=4 len(M)=3 K=5の場合、IndexErrorとなる。rangeをうまく設定する必要があるが実装できない。
    #正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
    s = max(0,K-len(M)+1)
    e = min(K+1, len(P))
    for i in range(s, e):
        cnt =min(cnt,P[i]*2 + M[K-i])
    #負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
    s1 = max(0,K-len(P)+1)
    e1 = min(K+1, len(M))
    for i in range(s1, e1):
        cnt =min(cnt,M[i]*2 + P[K-i])

print(cnt)

Submission status: Accepted

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

min_value = 10**9

for i in range(N-K+1):
    left = abs(candle[i])+abs(candle[i]-candle[i+K-1])
    right = abs(candle[i+K-1])+abs(candle[i]-candle[i+K-1])
    if min_value > min(left,right):
        min_value = min(left,right)
        
        
print(min_value)

Submission status: Accepted

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

s = 10**9
for i in range(n-k+1) :
  m = min(abs(x[i]) +abs(x[i+k-1] - x[i]) , abs(x[i+k-1]) + abs(x[i+k-1] - x[i]))
  if s >m :
    s = m
print(s)

Submission status: Accepted

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

s = 10**9
for i in range(n-k+1) :
  f1 = x[i]
  f2 = x[i+k-1]
  m = min(abs(f1) +abs(f2 - f1) , abs(f2) + abs(f2 - f1))
  if s >m :
    s = m
print(s)

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):
  l = X[i]
  r = X[i+k-1]
  
  ans.append(abs(r-l)+min(abs(l), abs(r)))
  
print(min(ans))

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb
 
N,K = map(int, input().split())
 
# 複数個の数値を、intの配列として取得
X = list(map(int, input().split()))
 
ans = 10**18
for i in range(N-K+1):
    if X[i]*X[i+K-1] >= 0:
        ans = min(ans,max(abs(X[i]),abs(X[i+K-1])))
    else:
        ans = min(ans, 2*abs(X[i])+abs(X[i+K-1]), abs(X[i])+2*abs(X[i+K-1]))
 
print(ans)