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())  # N本のろうそく、K本に火をつけたい
X = list(map(int, input().split()))

ans = 1e+9

for i in range(N-K+1):
    l, r = X[i], X[i+K-1]
    ans = min(ans, min(abs(l), abs(r)) + abs(l-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):
    l, r = x[i], x[i+k-1]
    ans = min(ans, min(abs(l), abs(r)) + abs(l - r))

print(ans)

Submission status: Accepted

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

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[l]-add[r]),
            abs(add[r])+abs(add[r]-add[l])
            )

#探索する量が少ないので全探索により答えを求めることができる

print(ans)

Submission status: Accepted

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

ans=float("inf")
for l in range(n-k+1):
    r=l+k-1
    ans=min(ans,
            abs(add[l])+abs(add[r]-add[l]),
            abs(add[r])+abs(add[r]-add[l])
            )

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    n,k=map(int,input().split())
    X=tuple(map(int,input().split()))
    ans=INF
    for i in range(n-k+1):
        a,b=X[i],X[i+k-1]
        if a<0<b:
            ans=min(ans,b-a+min(-a,b))
        else:
            ans=min(ans,max(abs(a),abs(b)))
    print(ans)
resolve()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = sorted([0] + list(map(int, input().split())))

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

print(ans)

Submission status: Accepted

Solution code:


import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

# x[0], x[-1]
def get_len(x0, xm1):
    left = abs(x0) + abs(xm1-x0)
    right = abs(xm1) + abs(xm1-x0)
    return min(left, right)

count = INF
for i in range(N-K+1):
    if X[i] > 0:
        X[i] = 0

    l = get_len(X[i], X[i+K-1])
#    print(X[i], X[i+K-1], l)
    count = min(count, l)

print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

# x[0], x[-1]
def get_len(x0, xm1):
    left = abs(x0) + abs(xm1-x0)
    right = abs(xm1) + abs(xm1-x0)
    return min(left, right)



count = INF
for i in range(N-K+1):
    l = get_len(X[i], X[i+K-1])
#    print(X[i], X[i+K-1], l)
    count = min(count, l)

print(count)

Submission status: Accepted

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

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)
    
if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
if n == k:
  print(min(r[-1], -l[0])*2+max(r[-1], -l[0]))
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)
#print(ts)
print(min(ts))

Submission status: Accepted

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

lft, rgt = 0, K - 1
ans = float('inf')

while True:
    if rgt == N:
        break
    ans = min(ans, min(abs(X[lft]) + abs(X[lft] - X[rgt]),
                       abs(X[rgt]) + abs(X[rgt] - X[lft])))
    lft += 1
    rgt += 1
print(ans)

Submission status: Accepted

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

if N == 1:
    print(abs(X[0]))
    exit()


flg = False

for x in X:
    if x < 0:
        flg = True

if flg is False:
    print(X[K - 1])
    exit()

flag2 = False

for x in X:
    if x > 0:
        flag2 = True

if flag2 is False:
    X.reverse()
    print(abs(X[K - 1]))
    exit()


def sol(lft, rgt):
    if X[lft] < 0 and X[rgt] < 0:
        return float('inf')
    elif X[lft] <= 0:
        return min(2 * abs(X[lft]) + X[rgt], abs(X[lft]) + 2 * X[rgt])
    else:
        return float('inf')


lft, rgt = 0, 0
ans = float('inf')
while True:

    if rgt == N - 1:
        break

    if rgt - lft + 1 < K:
        rgt += 1
        if rgt - lft + 1 == K:
            ans = min(ans, sol(lft, rgt))
            lft += 1
print(ans)

Submission status: Accepted

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

import bisect

N, K = map(int,input().split())
x = list(map(int, input().split()))
zero = bisect.bisect_right(x,0)
x.append(0)
x.sort()
dis = []
for i in range(1,N+1):
    dis.append(x[i]-x[i-1])

left=1
leftdis=0
while left<=K and zero-left>=0:
    leftdis+=dis[zero-left]
    left+=1
left-=1

right=1
rightdis=0
while left+right<=K:
    rightdis+=dis[zero+right-1]
    right+=1
right-=1

ans = min(rightdis*2+leftdis, rightdis+leftdis*2)
while right+zero<N and left > 0:
    leftdis-=dis[zero-left]
    rightdis+=dis[zero+right]
    ans = min(ans,rightdis*2+leftdis, rightdis+leftdis*2)
    left-=1
    right+=1

print(ans)

Submission status: Accepted

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

ct = [0]*(N-K+1)

for i in range(N-K+1):
  if (X[i]<0)&(X[i+K-1]>0):
    ct[i] = min(abs(X[i]),(X[i+K-1]))*2+max(abs(X[i]),(X[i+K-1]))
  else:
    ct[i] = max(abs(X[i]),(X[i+K-1]))
  
print(min(ct))

Submission status: Accepted

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

tmp=10**10
for i in range(N-K+1):
  if x[i]*x[i+K-1]<0:
    tmp=min(tmp,min(abs(x[i]),abs(x[i+K-1]))*2+max(abs(x[i]),abs(x[i+K-1])))
  else:
    tmp=min(tmp,max(abs(x[i]),abs(x[i+K-1])))
print(tmp)

Submission status: Accepted

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

ans = []
for i, name in enumerate(n):
    if b+i >a:
        break
    ans.append(min( (abs(n[i]) + abs(n[i +b-1] - n[i]),  abs(n[i+b-1]) + abs(n[i +b-1] - n[i]) )) )

print(min(ans))

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    ans = min(r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :]))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[K - 1 :]):
        x = r - l + min(abs(l), abs(r))
        ans = x if x < ans else ans
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[K - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import os, sys, re, math

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

candle = list(map(int,input().split(' ')))
ret = float('inf')

for i in range(N-K+1):
    s = candle[i]
    e = candle[i+K-1]
    if e < 0:
        ret = min(-s, ret)
    elif s > 0:
        ret = min(e, ret)
    else:
        ret = min(-s*2+e, -s+e*2, ret)

print(ret)

Submission status: Accepted

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

min_cost = 10**15
for i in range(n-k+1):
  left =  abs(X[i])    + abs(X[i]-X[i+k-1])
  right = abs(X[i+k-1])+ abs(X[i]-X[i+k-1])  
  min_cost = min(left,right,min_cost)
  
print(min_cost)

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 = x[i]
  r = x[i+K-1]
  temp = min(abs(l)+abs(r-l),abs(r)+abs(l-r))
  ans = min(temp,ans)

print(ans)

Submission status: Accepted

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

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = [int(x) for x in input().split()]
ans = 1e9
for l in range(N-K+1):
    r = l+K-1
    a = min(
        abs(X[l]) + abs(X[r]-X[l]),
        abs(X[r]) + abs(X[l]-X[r])
    )
    if a < ans:
        ans = a
print(ans)

Submission status: Accepted

Solution code:
import os, sys, re, math

(N, K) = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

if 0 in x:
    K -= 1
nx = list(filter(lambda i: i < 0, x))
px = list(filter(lambda i: i > 0, x))
nx.reverse()

result = 0
if K > 0:
    result = sys.maxsize
    if len(nx) > K - 1:
        result = min(result, -nx[K - 1])
    if len(px) > K - 1:
        result = min(result, px[K - 1])
    for i in range(len(px)):
        j = K - i - 2
        if 0 <= j < len(nx):
            result = min(result, px[i] * 2 - nx[j])
    for i in range(len(nx)):
        j = K - i - 2
        if 0 <= j < len(px):
            result = min(result, px[j] - nx[i] * 2)

print (result)

Submission status: Accepted

Solution code:
line = raw_input()
N, K = [int(s) for s in line.split(' ')]
line = raw_input()
Xs = [int(s) for s in line.split(' ')]

ret = []
for i in range(0, len(Xs) - K + 1):
    p = Xs[i + K - 1]
    m = Xs[i]
    if p * m == 0:
        ret.append(abs(p) * abs(m))
        continue
    if p * m > 0:
        ret.append(max(abs(p), abs(m)))
        continue
    ret.append(abs(p) * 2 + abs(m))
    ret.append(abs(p) + abs(m) * 2)

print min(ret)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = sorted(map(int, input().split()))
ret = 1000000000000000000
for s in range(n - k + 1):
    e = s + k - 1
    if x[e] <= 0:
        ret = min(ret, -x[s])
    elif x[s] <= 0:
        ret = min(ret, x[e] - x[s] + min(-x[s], x[e]))
    else:
        ret = min(ret, x[e])
print(ret)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split(' ')))
l = list(map(int, input().split(' ')))
minuss = [0] + [-i for i in l if i < 0][::-1]
pluss = [0] + [i for i in l if i >= 0]
# minuss から k本、plussからK-k本取る(短い方を折り返すので2倍)
ans = 1e16  # ?
if pluss == [0]:  # 4 2 [-4, -3 , -2, -1]
    pluss = minuss
    minuss = [0]
for k in range(K):
    if k >= len(minuss) or K - k >= len(pluss):
        continue
    ans = min(ans, min(minuss[k] * 2 + pluss[K-k], minuss[k] + pluss[K-k] * 2))
print(ans)

Submission status: Accepted

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

ans=float('inf')
for l in range(N-K+1):
  r=l+K-1
  ans=min(ans,min(abs(X[l]),abs(X[r]))+abs(X[l]-X[r]))
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float('inf')
for l in range(0, N-K+1):
    r = l + K - 1
    ans = min(ans, min(abs(X[l]), abs(X[r])) + abs(X[l] - X[r]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 2 * (abs(ls[0]) + abs(ls[-1]))
for i in range(n - k + 1):
    a = ls[i]
    b = ls[i + k - 1]
    if b <= 0:
        res = min(res, -a)
    elif a >= 0:
        res = min(res, b)
    else:
        res = min(res, -2 * a + b, -a + 2 * b)
print(res)

Submission status: Accepted

Solution code:
def main():
    N, K = (int(i)for i in input().split())
    x = [int(i) for i in input().split()]
    ans = 3 * (10**8)
    
    for i in range(N-K+1):
        dist = min(abs(x[i]),abs(x[i+K-1])) + abs(x[i+K-1] - x[i])
        ans = min(ans,dist)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    N, K = (int(i)for i in input().split())
    x = [int(i) for i in input().split()]
    ans = 3 * (10**8)
    st_val = min(x,key=abs)
    st_idx = x.index(st_val)
    
    for i in range(K+1):
        dist = 3 * (10**8)
        if 0 <= st_idx-K+1+i and st_idx+1+i <= N:
            if (x[st_idx-K+1+i] < 0 and x[st_idx+i] <= 0):
                dist = abs(x[st_idx-K+1+i])
            elif (x[st_idx-K+1+i] >= 0 and x[st_idx+i] > 0):
                dist = abs(x[st_idx+i])
            else:
                if abs(x[st_idx-K+1+i]) > abs(x[st_idx+i]):
                    dist = abs(x[st_idx-K+1+i]) + abs(x[st_idx+i]) * 2
                else:
                    dist = abs(x[st_idx-K+1+i]) * 2 + abs(x[st_idx+i])
        elif N < st_idx+1+i:
            break
        ans = min(ans,dist)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,k,*l=map(int,open(0).read().split());print(min((max(abs(i),abs(j)),min(-i*2+j,-i+2*j))[i*j<0]for i,j in zip(l,l[k-1:])))

Submission status: Accepted

Solution code:
n,k,*l=map(int,open(0).read().split())
m=1e10
for i,j in zip(l,l[k-1:]):
  if i*j<0:
    m=min(m,-i*2+j,-i+j*2)
  else:
    m=min(m,max(abs(i),abs(j)))
print(m)

Submission status: Accepted

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

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

print(min(arr))

Submission status: Accepted

Solution code:
import bisect
import sys

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

zIndex = bisect.bisect_left(x, 0)

if zIndex == 0:
    print(x[K - 1])
    sys.exit()
ans = 10 ** 9
for i in range(N - K + 1):
    if zIndex > i + K - 1:
        ans = min(ans, abs(x[i]))
    else:
        tmp1 = min(abs(x[i]), x[i + K - 1])
        tmp2 = max(abs(x[i]), x[i + K - 1])
        ans = min(ans, (tmp1 * 2) + tmp2)

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
if x.count(0):
    K -= 1
x_p = list(filter(lambda n:n>0,x))
x_n = list(filter(lambda n:n<0,x))
x_n = sorted(list(map(lambda n:n*(-1),x_n)))
ans = float('inf')
if K == 0:
    print(0)
    exit()
for i in range(K+1):
    p = i
    n = K-i
    if len(x_p) < p or len(x_n) < n:
        continue
    else:
        if p == 0:
            result = x_n[n-1]
        elif n == 0:
            result = x_p[p-1]
        else:
            if x_p[p-1] < x_n[n-1]:
                result = x_p[p-1]*2+x_n[n-1]
            else:
                result = x_p[p-1]+x_n[n-1]*2

    ans = min(ans, result)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N, K = map(int, input().split())
rousoku = [int(i) for i in input().split()]
if N == 1:
    print(abs(rousoku[0]))
    sys.exit()
if K == 1:
    rousoku = [abs(i) for i in rousoku]
    print(min(rousoku))
    sys.exit()

dislist = []
for i in range(N - K + 1):
    dislist.append(abs(rousoku[i] - rousoku[i + K - 1]) +
                   min(abs(rousoku[i]), abs(rousoku[i + K - 1])))


print(min(dislist))

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):
    if i + k - 1 > n-1:
        break
    l = x[i]
    r = x[i+k-1]
    temp1 = abs(r) + abs(r - l)
    temp2 = abs(l) + abs(r - l)
    temp = min(temp1, temp2)
    if temp < ans:
        ans = temp

print(ans)

Submission status: Accepted

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

    l = 0
    r = K - 1

    mn = 10e+8
    while r < N:
        ltor = abs(x[l]) + abs(x[r] - x[l])
        rtol = abs(x[r]) + abs(x[l] - x[r])

        mn = min(mn, min(ltor, rtol))
        l += 1
        r += 1

    print(mn)


if __name__ == '__main__':
    func()

Submission status: Accepted

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

print(min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)]))

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy

setrecursionlimit(10**6)
input=stdin.readline


n,k=map(int,input().split())
x=[int(i) for i in input().split()]
ans=10**9
# 7 3
# -20 -10 -5 1 12 20 30
for left in range(n-k+1):
    if x[left+k-1]<=0:
        ans=min(ans,abs(0-x[left+k-1])+abs(x[left+k-1]-x[left]))
    elif x[left]<0<x[left+k-1]:
        dis1=min(abs(x[left]),abs(x[left+k-1]))
        dis2=max(abs(x[left]),abs(x[left+k-1]))
        ans=min(ans,dis1*2+dis2)
    else:
        ans=min(ans,x[left]+abs(x[left+k-1]-x[left]))
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(X[i+k-1]-X[i]+min(abs(X[i+k-1]),abs(X[i])))
print(min(ans))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    N,K = map(int,input().split())
    X = list(map(int,input().split()))
    ans = float("inf")
    for i in range(N-K+1):
        x = X[i]
        y = X[i+K-1]
        if ans > y-x + min(abs(x),abs(y)):
            ans = y-x + min(abs(x),abs(y))
    print(ans)
    
    
    
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=[int(_) for _ in input().split()]
m=10**15
for i in range(N-K+1):
    l=abs(x[i])+abs(x[i]-x[i+K-1])
    r=abs(x[i+K-1])+abs(x[i+K-1]-x[i])
    if l<m:
        m=l
    if r<m:
        m=r
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=[int(s) for s in input().split()]
ls=[int(s) for s in input().split()]
def length(i):
  if ls[i]>=0:
    return ls[K+i-1]
  elif ls[K+i-1]<=0:
    return -ls[i]
  else:
    return min([2*ls[K+i-1]-ls[i],ls[K+i-1]-2*ls[i]])
answer=[]
for i in range(N-K+1):
  answer.append(length(i))
  
print(min(answer))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = map(int, input().split())
a = [0]
b = [0]
for x_ in x:
    if x_ >= 0:
        a.append(x_)
    else:
        b.append(abs(x_))

a.sort()
b.sort()

res = float('inf')
for i in range(k+1):
    try:
        res = min(res, a[i]+2*b[k-i], 2*a[i]+b[k-i])
    except:
        continue

print(res)

Submission status: Accepted

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

ts = [float('inf')] * (N-K+1)

for i in range(N-K+1):
  r = xs[i+K-1]
  l = xs[i]  
  ts[i] = (r - l) + min(abs(r-0), abs(l-0))  

print(min(ts))

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]), abs(x[i+k-1])) + abs(x[i+k-1]-x[i]))
print(min(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):  # iからi+K-1の範囲
    if x[i] >= 0:
        ans1 = 10 ** 9
        ans2 = x[i + K - 1]
    elif x[i + K - 1] <= 0:
        ans1 = abs(x[i])
        ans2 = 10 ** 9
    else:
        ans1 = abs(x[i]) * 2 + x[i + K - 1]
        ans2 = abs(x[i]) + x[i + K - 1] * 2
    ans = min(ans, ans1, ans2)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
list_x = list(map(int,input().split()))
 
loop_cnt = N - K + 1
ans = 10**10
 
for i in range(loop_cnt):
  diff_between_0 = min(abs(list_x[i]), abs(list_x[i+K-1]))
  diff_between_max_min = abs(list_x[i+K-1] - list_x[i])
  _ = diff_between_0 + diff_between_max_min
  ans = min(ans,_)
 
print(ans)

Submission status: Accepted

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

loop_cnt = N - K + 1
ans = []

for i in range(loop_cnt):
  diff_between_0 = min(abs(list_x[i]), abs(list_x[i+K-1]))
  diff_between_max_min = abs(list_x[i+K-1] - list_x[i])
  _ = diff_between_0 + diff_between_max_min
  ans.append(_)

print(min(ans))

Submission status: Accepted

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

l = 0
t_min = 3 * 10 ** 8 + 1

while l + k -1 < n:
    x_l = x[l]
    x_r = x[l + k - 1]
    if x_l < 0 and x_r > 0:
        t = 2 * min(abs(x_l), abs(x_r)) + max(abs(x_l), abs(x_r))
    else:
        t = max(abs(x_l), abs(x_r))
    if t < t_min:
        t_min = t
    l += 1

print(t_min)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(int(i) for i in input().split())
inf = 10**9
max_x = x[-1]
min_x = x[0]
if min_x >= 0:
    print(x[k-1])
elif max_x <= 0:
    print(abs(x[-k]))
else:
    for i in range(n-1):
        if x[i]<0 and x[i+1]>=0:
            key = i+1
            break
    if n-key>=k:
        ans1 = sum(x[key:key+k])
    else:
        ans1 = inf
    if key>=k:
        ans2 = abs(sum(x[key-k:key]))
    else:
        ans2 = inf
    ans3 = inf
    for i in range(max(0, key-k+1), min(n-k+1, key+k-1)):
        a, b = abs(x[i]), abs(x[i+k-1])
        t = max([a, b])+min([a, b])*2
        if t < ans3:
            ans3 = t
    print(min([ans1, ans2, ans3]))

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

# 左端から考えていった場合
# 考えうるK区間を全探索して、最小の時間を出力すれば良い
N, K =  map(int, sys.stdin.readline().split())
L =  list(map(int, sys.stdin.readline().split()))

d = 10 ** 10
for i in range(N - K + 1):
    left_search = abs(L[i]) + abs(L[i + K - 1] - L[i])
    right_search = abs(L[i + K - 1]) + abs(L[i + K - 1] - L[i])
    d = min(d, min(left_search, right_search))

print(d)

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):
    a.append(min(abs(x[i] - 0) + abs(x[i + K - 1] - x[i]), abs(x[i + K - 1] - 0) + abs(x[i] - x[i + K - 1])))

print(min(a))

Submission status: Accepted

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

# xiにいってからxi+(K-1)に行く
a = []
b = []
for i in range(N - K + 1):
    a.append(abs(x[i] - 0) + abs(x[i + K - 1] - x[i]))
    b.append(abs(x[i + K - 1] - 0) + abs(x[i] - x[i + K - 1]))

print(min(min(a), min(b)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC107 C - Candles
N,K = map(int,input().split())
C = list(map(int,input().split()))

l=0
r=0
ans=10**9

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
xli = na()
li = []
ans = 1000000000
for i in range(n - k + 1):
    l = xli[i]
    r = xli[i + k - 1]
    ans = min(ans, abs(l) + abs(r-l), abs(r) + abs(r - l))
print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
xli = na()
li = []
ans = 1000000000
idx = -1
for i in range(n):
    if xli[i] > 0:
        idx = i
        break
if idx == -1:
    print(abs(xli[-k]))
elif idx == 0:
    print(abs(xli[k-1]))
else:
    for i in range(max(idx - k, 0), min(idx+1, n-k+1)):
        if xli[i] * xli[i + k -1] < 0:
            ans = min(ans, 2 * abs(xli[i]) + xli[i + k -1], abs(xli[i]) + 2 * xli[i + k -1])
        else:
            if xli[i + k -1] <= 0:
                ans = min(ans, abs(xli[i]))
            else:
                ans = min(ans, abs(xli[i + k -1]))
    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):
  l = X[i]
  r = X[i+k-1]
  tmp = min(abs(l)+abs(l-r), abs(r)+abs(l-r))
  ans = min(tmp, ans)
print(ans)

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################


'''
https://img.atcoder.jp/arc101/editorial.pdf
https://atcoder.jp/contests/abc107/submissions/5375441

l → r の順：|xl| + |xr-xl|
r → l の順：|xr| + |xr-xl|
'''


N, K = 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]
    ans = min(ans, abs(left)+abs(right-left), abs(right)+abs(left-right))

print(ans)

Submission status: Accepted

Solution code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if index < len(xs) and xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
if len(a) >= k - cost:
    ret = min(ret, cost_a[k - cost])
if len(b) >= k - cost:
    ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if 0 <= k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if 0 <= k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)

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):
    a, b = x[i], x[i + k - 1]
    d = b - a + min(abs(a), abs(b))
    ans = min(ans, d)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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] + 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()))
p = n
for i in range(n):
  if x[i] > 0:
    p = i
    break
result = float('inf')
for i in range(k + 1):
  if p - i < 0 or p + k - i > n:
    continue
  if i == 0:
    result = min(result, x[p + k - 1])
  elif i == k:
    result = min(result, -x[p - k])
  else:
    l = x[p - i]
    r = x[p + (k - i) - 1]
    result = min(result, r - 2 * l, r * 2 - l)
print(result)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
p = n
for i in range(n):
  if x[i] > 0:
    p = i
    break
result = float('inf')
for i in range(k + 1):
  if p - i < 0 or p + k - i > n:
    continue
  if i == 0:
    t = min(result, x[p + k - 1])
  elif i == k:
    t = min(result, abs(x[p - k]))
  else:
    t = min(result, abs(x[p - i]) * 2 + x[p + (k - i) - 1], abs(x[p - i]) + x[p + (k - i) - 1] * 2)
  result = min(result, t)
print(result)