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
sys.setrecursionlimit(10 ** 7)
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):
    left = x[i]
    right = x[i+k-1]
    if left <= 0 <= right:
        tmp = -(left) + right
        tmp += min(-left,right)
        ans = min(ans,tmp)
    elif 0 < left:
        ans = min(ans,right)
    elif right < 0:
        ans = min(ans,abs(left))

print(ans)

Submission status: Accepted

Solution code:
import bisect
n,k = map(int, input().split())
x = list(map(int, input().split()))
z = bisect.bisect_left(x,0)

ans=10**12
s=z-k
if s < 0:
  s=0
g=z+1
if g+k >= n:
  g=n-k+1
for i in range(s,g):

  xl=x[i]
  xr=x[i+k-1]
  ans=min(ans,abs(xr)+abs(xr-xl),abs(xl)+abs(xr-xl))
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):
    left = i
    right = i+k-1
    ans = min(ans, min(abs(x[left])+abs(x[right]-x[left]),abs(x[right])+abs(x[left]-x[right])))
print(ans)

Submission status: Accepted

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

if k == 1:
    print(min(abs(max(li)),abs(min(li))))
    exit()

time_li = []
for i in range(n-k+1):
    #print(li[i:i+k]) [-30, -10, 10]
    tmp_time_li = []
    mi = li[i]
    ma = li[i+k-1]
    if mi > 0 and ma >0:
        tmp_time_li.append(ma)
    elif mi < 0 and ma < 0:
        tmp_time_li.append(abs(mi))
    else:
        orikaeshi = min(abs(mi),abs(ma))*2+max(abs(mi),abs(ma))
        tmp_time_li.append(orikaeshi)
    time_li.append(tmp_time_li[0])
print(min(time_li))

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left

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

ans = 4*10**8 + 1
x_0 = bisect_right(x, 0)
x_1 = bisect_left(x, 0) - 1

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

Submission status: Accepted

Solution code:
# Problem C - Candles

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

# initialization
ans = float('INF')
loop_count = N - K + 1

# count
if N-K==0:
    loop_count = 1
for i in range(loop_count):
    f = x_nums[i]
    l = x_nums[i+K-1]
    if f<0:
        if l>=0:
            tmp_time_1 = abs(f)*2 + abs(l)
            tmp_time_2 = abs(l)*2 + abs(f)
            tmp_time = min(tmp_time_1, tmp_time_2)
            ans = min(ans, tmp_time)
        else:
            ans = min(ans, abs(f))
    else:
        ans = min(ans, abs(l))

# output
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
xp=[xi for xi in x if xi>=0]
xm=[xi for xi in x if xi<0]
xm.sort(reverse=True)
ans=3*10**8
for i in range(k+1):
    # xpからi個のろうそくをつける。xmからはk-i個火をつける
    if i>len(xp) or k-i>len(xm):
        continue
    a=xp[i-1] if len(xp)>0 and i>0 else 0
    b=xm[k-i-1] if len(xm)>0 and k-i>0 else 0
    ans=min(ans,2*a-b,a-2*b)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n, k = MI()
    X = LI()


    ans = INF
    for i in range(n + 1 - k):
        xl = X[i]
        xr = X[i+(k-1)]

        d_l = abs(xl)
        d_r = abs(xr)
        d_d = abs(xl - xr)
        ans = min(ans, min(d_l + d_d, d_r + d_d))
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

X = list(map(int, input().split()))
Ne = bisect_left(X, 0)
Po = bisect_right(X, 0)

if Ne != Po:
  K -= 1

ans = 10**9

for i in range(K+1):
  tmp = 10**9
  if i > N - Po:
    continue
  if K - i > Ne:
    continue
  
  if i == 0:
    tmp = abs(X[Ne-K])
  
  elif i == K:
    tmp = abs(X[Po+K-1])
  
  else:
    tmp = X[Po+i-1] - X[Ne-(K-i)] + min(abs(X[Po+i-1]),abs(X[Ne-(K-i)])) 

  ans = min(ans,tmp)

print(ans)

Submission status: Accepted

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

xmin=(x[n-1]-x[0])*2

for i in range(n-k+1):
    x1=abs(x[i])+abs(x[i+k-1]-x[i])
    x2=abs(x[i+k-1])+abs(x[i+k-1]-x[i])
    xmin=min(xmin,x1,x2)

print(xmin)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
ans = 10 ** 9
for l in range(n - k + 1):
    dist = abs(x[l] - x[l + k - 1])
    dist += min(abs(x[l]), abs(x[l+k-1]))
    ans = min(ans,dist)
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):
    nowx=(x[i],x[i+k-1])
    if nowx[0]>=0 and nowx[-1]>=0:
        ans=min(ans, nowx[-1])
    elif nowx[0]<=0 and nowx[-1]<=0:
        ans=min(ans, abs(nowx[0]))
    elif nowx[0]<0 and nowx[-1]>=0:
        ans=min(ans, 2*abs(nowx[0])+nowx[-1], abs(nowx[0])+2*nowx[-1])
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, r):
    lv = abs(l)
    rv = abs(r)
    if (l < 0) ^ (r < 0):
        return lv + rv + min(lv, rv)
    else:
        return max(lv, rv)

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))

    ans = 10 ** 9
    for i in range(N - K + 1):
        tmp_ans = solve(x[i], x[i + K - 1])
        if tmp_ans < ans:
            ans = tmp_ans

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def solve(l, m):
    if l < m:
        return l * 2 + m
    else:
        return l + m * 2

def main():
    N, K = map(int, input().split())
    x = tuple(map(int, input().split()))
    right_x = [i for i in x if i >= 0][:K]
    left_x = [-i for i in x if i < 0][-K:]

    left_len = len(left_x)
    right_len = len(right_x)
    if right_len < K:
        right_x += [10 ** 9 + 1] * (K - right_len)
    if left_len < K:
        left_x = [10 ** 9 + 1] * (K - left_len) + left_x

    right_x = [0] + right_x
    left_x += [0]

    ans = 10 ** 9
    for i in range(K + 1):
        tmp_ans = solve(left_x[i], right_x[i])
        if tmp_ans < ans:
            ans = tmp_ans
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

time = 100000000000007
for i in range(0, N-K+1):
  if X[i] < 0 and X[i+K-1] > 0:
    if abs(X[i]) >= abs(X[i+K-1]):
      time = min(abs(X[i+K-1]-X[i]) + abs(X[i+K-1]),time)
    else:
      time = min(abs(X[i+K-1]-X[i]) + abs(X[i]),time)
  if X[i] >= 0:
      time = min(abs(X[i+K-1]-X[i]) + abs(X[i]),time)
  if X[i+K-1] <= 0:
      time = min(abs(X[i+K-1]-X[i]) + abs(X[i+K-1]),time)

print(time)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
#from numpy import sign
#import numpy as np
out = float('Inf')
for i in range(0, n - k + 1):
    ans = abs(a[i] - a[i + k - 1]) + min(abs(a[i]), abs(a[i + k - 1]))
    out = min(out, ans)
print(out)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x_ls = list(map(int, input().split()))
ans = float('inf')
for i in range(n-k+1):
    if x_ls[i+k-1] < 0:
        ans = min(ans, abs(x_ls[i]))
    elif x_ls[i] < 0 and x_ls[i+k-1] >= 0:
        near = min(abs(x_ls[i]),abs(x_ls[i+k-1]))
        far = max(abs(x_ls[i]),abs(x_ls[i+k-1]))
        ans = min(ans, 2 * near + far)
    elif x_ls[i] >= 0:
        ans = min(ans, x_ls[i+k-1])
print(ans)

Submission status: Accepted

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    ans = 10**10
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        l = X[i]
        r = X[i+K-1]
        if l < 0 and r > 0:
            c = min(abs(l) * 2 + r, abs(l) + r * 2)
        else:
            c = max(abs(l), abs(r))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        l = X[i]
        r = X[i+K-1]
        if l < 0 and r > 0:
            c = min(abs(l) * 2 + r, abs(l) + r * 2)
        else:
            c = max(abs(l), abs(r))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s.pop(0)
        s.append(X[i+K-1])
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N, K = map(int, readline().rstrip().split())
    X = list(map(int, readline().rstrip().split()))
    len_m = len([x for x in X if x < 0])
    len_p = len([x for x in X if x > 0])
    ans = 10**10
    idx = max(0, len_m-K)
    s = [0] + X[idx:idx+K-1]
    for i in range(max(0, len_m-K), min(len_m+K, N-K+1)):
        s.pop(0)
        s.append(X[i+K-1])
        if len(s) != K:
            continue
        if s[0] < 0 and s[-1] > 0:
            c = min(abs(s[0]) * 2 + s[-1], abs(s[0]) + s[-1] * 2)
        else:
            c = max(abs(s[0]), abs(s[-1]))
        ans = min(ans, c)

    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 i in range(N - K + 1):
    l = X[i]
    r = X[i + K - 1]

    if l >= 0:
        ans = min(ans, abs(r))
    elif r <= 0:
        ans = min(ans, abs(l))
    else:
        ans = min(ans, 2 * abs(r) + abs(l), 2 * abs(l) + abs(r))

print(ans)

Submission status: Accepted

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

def f(min1,max1):
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<=0 and max1 <=0:
    return abs(min1)
  else:
    return min([-2*min1 + max1,-min1 + 2*max1])

ans = 2*10**9
for i in range(N-K+1):
  min1 = xlist[i]
  max1 = xlist[i+K-1]
  dist =f (min1,max1) 
  if dist < ans :
    ans = dist

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

Submission status: Accepted

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

    ans = 3 * 10 ** 8
    for left, right in zip(x, x[K - 1:]):
        if left * right < 0:
            ans = min(ans, right - left + min(-left, right))
        else:
            ans = min(ans, -left if left < 0 else right)
    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
x=list(map(int,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()))
A.sort()

cur = 10**9
i = 0

while(i+k <= n):
    if(A[i+k-1] <= 0):
        tmp = -A[i]
    elif(A[i] <= 0):
        B = [-A[i], A[i+k-1]]
        B.sort()
        tmp = B[1] + 2 * B[0]
    else:
        tmp = A[i+k-1]
    cur = min(cur, tmp)
    i = i + 1

print(cur)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
X=list(map(int,input().split()))
for i in range(n):
    if X[i]>=0:
        break
else:
    i=n

if i==0:
    M=[]
else:
    M=X[i-1::-1]
if i==n:
    P=[]
else:
    P=X[i:]
    
ans=10**10
for j in range(k+1):
    if len(M)<j or len(P)<k-j:
        continue
    if j==0:
        ans=min(P[k-1],ans)
    elif j==k:
        ans=min(-M[k-1],ans)
    else:
        l=max(-M[j-1],P[k-j-1])
        s=min(-M[j-1],P[k-j-1])
        ans=min(s*2+l,ans)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N,K=map(int, input().split())
X=list(map(int, input().split()))
ans=np.inf
for i in range(N-K+1):
  l=X[i]
  r=X[i+K-1]
  ans=min(ans, r-l+min(abs(l), abs(r)))
print(ans)

Submission status: Accepted

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

ans = 10**18
for i in range(N - K + 1):
    x1 = X[i]
    x2 = X[i + K - 1]
    if x1 * x2 >= 0:
        dist = max(abs(x1), abs(x2))
    else:
        dist = min(abs(x1), abs(x2)) + abs(x1 - x2)
    ans = min(ans, dist)
print(ans)

Submission status: Accepted

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

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):
    left = X[i]
    right = X[i + k - 1]
    if left < 0 and right > 0:
        time = right - left + min(abs(left), abs(right))
    else:
        time = max(abs(right), abs(left))
    ans = min(ans, time)
print(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 = 10 ** 18
    for i in range(K-1, N):
        a = X[i-K+1]
        b = X[i]
        if a < 0 and b > 0:
            ans = min(ans, (-a)*2+b, (-a)+b*2)
        elif b <= 0:
            ans = min(ans, -a)
        elif a >= 0:
            ans = min(ans, b)
    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 i in range(n-k+1):
    l, r = x[i], x[i+k-1]
    if r <= 0:
        a = abs(l)
    elif l >= 0:
        a = r
    else:
        a = min(abs(l), abs(r)) * 2 + max(abs(l), abs(r))
    if ans > a: ans = a
print(ans)

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 = 10**12
    for i in range(N-K+1):
        u = X[i]
        v = X[i+K-1]
        if u*v >= 0:
            ans = min(ans, max(abs(u), abs(v)))
        else:
            ans = min(ans, min(abs(u), v)*2+max(abs(u), v))
    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 = 10**12
    for i in range(N-K+1):
        u = X[i]
        v = X[i+K-1]
        if u*v >= 0:
            ans = min(ans, max(abs(u), abs(v)))
        else:
            u = -u
            ans = min(ans, min(u, v)*2+max(u, v))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = map(int,readline().split())

lst1 = list(map(int,readline().split()))
plus = []
minus = []
for i in lst1:
    if i < 0:
        minus.append(-i)
    else:
        plus.append(i)
minus.reverse() #[1,2,3]
ans = 10**18
if len(minus) >= k:
    ans = min(ans,minus[k-1])
if len(plus) >= k:
    ans = min(ans,plus[k-1])

if len(plus) == 0 or len(minus) == 0:
    print(ans)
    exit()
for i in range(1,k):
    if i > len(plus) or k-i > len(minus):
        continue
    p = plus[i-1]
    m = minus[k-i-1]
    res = min(p*2+m,p+m*2)
    ans = min(ans,res)

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]))
    ans = min(ans, 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=10**18
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  ans=min(ans,min(abs(l)+abs(r-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**18
for l, r in zip(X, X[K - 1:]):
    if r <= 0:
        ans = min(ans, abs(l))
    if l >= 0:
        ans = min(ans, abs(r))
    if l < 0 < r:
        ans = min(ans, r - l + min(abs(r), abs(l)))
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    import bisect
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    ans = 10**9
    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]))+(min(abs(x[i]), abs(x[i+k-1])))*2)
        else:
            ans = min(ans, max(abs(x[i]), abs(x[i+k-1])))

    print(ans)

resolve()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
INF = float("inf")


def main():
    n, k = map(int, input().split())
    X = list(map(int, input().split()))
    if n == 1 and k == 1:
        print(0)
        exit()
    ans = INF
    for i in range(n - k + 1):
        l, r = X[i], X[i + k - 1]
        tmp = r - l
        if l * r < 0:
            tmp += min(abs(l), abs(r))
        else:
            tmp += min(abs(l), abs(r))
        ans = min(tmp, ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
minus = []
plus = []
ans = 10**100
for x in a:
    if x <0:minus.append(-x)
    else:plus.append(x)
minus = minus[::-1]
for i in range(min(k+1,len(minus)+1)):#負のものをi個通る
    if k-i > len(plus):continue
    res = 0
    if i-1 >=0:res += minus[i-1]
    res*=2
    if k-i-1 >=0 and k-i <= len(plus):res += plus[k-i-1]
    ans = min(ans,res)
    res = 0
    if k-i-1 >=0 and  k-i <= len(plus):res += plus[k-i-1]
    res *=2
    if i-1 >=0:res += minus[i-1]
    ans = min(ans,res)
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
minus = []
plus = []
ans = 10**100
for x in a:
    if x <0:minus.append(-x)
    else:plus.append(x)
minus = minus[::-1]
for i in range(min(k+1,len(minus)+1)):#負のものをi個通る
    if k-i > len(plus):continue
    res = 0
    if i-1 >=0:res += minus[i-1]
    res*=2
    if k-i-1 >=0 and k-i <= len(plus):res += plus[k-i-1]
    ans = min(ans,res)
    res = 0
    if k-i-1 >=0 and  k-i <= len(plus):res += plus[k-i-1]
    res *=2
    if i-1 >=0:res += minus[i-1]
    ans = min(ans,res)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc107/tasks/arc101_a
N, K = read_ints()
X = read_ints()

# 右だけにK本とるとき
# 右から左に折り返してK本とるとき
# 反転させて二通りやるときの場合分けをする


def solve(X):
    ret = 10**13
    for i in range(N):
        j = i + K - 1
        if j >= N:
            continue
        if X[i] <= 0 <= X[j]:
            ret = min(ret, -2 * X[i] + X[j])  # 右スタートか左スタートかの違い
            ret = min(ret, 2 * X[j] - X[i])
    return ret


ans = solve(X)
# 右直進だけのほうがいいとき
i = bisect_left(X, 0) + K - 1
if i < N:
    ans = min(ans, X[i])
# 左直進だけのほうがいいとき
i = bisect_right(X, 0) - K
if -1 < i:
    ans = min(ans, -X[i])
# print(i, X[i])
print(ans)

Submission status: Accepted

Solution code:
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,K=pin()
    X=tupin()
    Y=[]
    temp=X[0]
    ans=10000000000
    for i in range(N-K+1):
        temp=abs(X[i+K-1]-X[i])+min(abs(X[i+K-1]),abs(X[i]))
        #print((X[i+K-1],X[i]))
        ans=min(ans,temp)
    print(ans)    
#%%submit!
resolve()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
X = LIST()

ans = INF
for i in range(N-K+1):
    a, b = X[i], X[i+K-1]
    # if a <= 0 and 0 < b:
    if b <= 0:
        ans = min(abs(a), ans)
    elif 0 <= a:
        ans = min(abs(b), ans)
    else:
        tmp = min(abs(a), abs(b)) + (b - a)
        ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = ((N+1)//2)/N
    print(ans)
    return

def examB():
    N, K = LI()
    H = [I()for _ in range(N)]
    H.sort()
    ans = inf
    for i in range(N-K+1):
        cur = H[i+K-1]-H[i]
        if ans>cur:
            ans = cur
    print(ans)
    return

def examC():
    N = I()
    A = LI()
    ans = [0]*N
    C = Counter(A)
    base = 0
    for c in C.values():
        base += c*(c-1)//2
    #print(C)
    #print(base)
    for i in range(N):
        ans[i] = base - (C[A[i]]-1)
    for v in ans:
        print(v)
    return

def examD():
    N, K = LI()
    X = LI()
    L = []; R = [0]
    for x in X:
        if x<0:
            L.append(-x)
        else:
            R.append(x)
    L.append(0)
    L = L[::-1]
    ans = inf
    for i in range(len(L)):
        if len(R)<=K-i or K-i<0:
            continue
        cur = min(L[i] * 2 + R[K - i], L[i] + R[K - i] * 2)
        # print(cur)
        if ans > cur:
            ans = cur
    print(ans)
    return

def examE():
    N, M = LI()
    X = [LI()for _ in range(N)]
    ans = 0
    for i1 in [-1,1]:
        for i2 in [-1,1]:
            for i3 in [-1,1]:
                curX = [0]*N
                for i in range(N):
                    curX[i] = X[i][0]*i1+X[i][1]*i2+X[i][2]*i3
                curX.sort(reverse=True)
                cur = sum(curX[:M])
                if ans<cur:
                    ans = cur
    print(ans)
    return

def examF():

    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,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)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

ans = 10**18
for i in range(N - K + 1):
    x1 = X[i]
    x2 = X[i + K - 1]
    if x1 * x2 >= 0:
        dist = max(abs(x1), abs(x2))
    else:
        dist = min(abs(x1), abs(x2)) + abs(x1 - x2)
    ans = min(ans, dist)
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):
  x_max=x[i+k-1]
  x_min=x[i]

  t_ans=x_max-x_min
  t_ans+=min(abs(x_max),abs(x_min))

  ans=min(ans,t_ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]
    x.sort()
    answer = float("inf")
    if n == k:
        if n == 1:
            answer = abs(x[0])
        if 0 <= x[0]:
            answer = x[-1]
        elif x[-1] <= 0:
            answer = abs(x[0])
        else:
            answer = x[-1] - x[0] + min(abs(x[-1]), abs(x[0]))
    else:
        for i in range(n - k + 1):
            left = x[i]
            right = x[i + k - 1]
            if left * right >= 0:
                answer = min(answer, max(abs(left), abs(right)))
            else:
                answer = min(answer, right - left + min(abs(left), abs(right)))
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
X = list(map(int,input().split()))
Xp = [x for x in X if x>=0]
Xm = [-x for x in X if x<0]
Xm.sort()
Xp.insert(0,0)
Xm.insert(0,0)
lp = len(Xp)
lm = len(Xm)
ans = 10**9
for p in range(k+1):
    m = k-p
    if p>lp-1 or m>lm-1:continue
    ans= min(ans,2*Xm[m] +Xp[p],Xm[m] +2*Xp[p] )
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):
  dista = abs(x[i]) + abs(x[i+K-1] - x[i])
  distb = abs(x[i+K-1]) + abs(x[i+K-1] - x[i])
  dist = min(dista, distb)
  if(dist < ans):
    ans = dist
    
print(ans)

Submission status: Accepted

Solution code:
import sys

N, K, *X = map(int, sys.stdin.read().split())

ans = 10**20
for l, r in zip(X, X[K-1:]):
    if l*r < 0:
        dist = min(-2*l + r, 2*r - l)
    else:
        dist = max(abs(l), abs(r))
    if ans > dist:
        ans = dist
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):
    left = x[i]
    right = x[i + k - 1]
    ans = min(ans, abs(left) + abs(right - left), abs(right) + abs(right - left))

print(ans)