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

X = []
K = 0
def fullsearch():
    for l, r in zip(X, X[K-1:]):
        distance = abs(r - l)
        yield distance + abs(r)
        yield distance + abs(l)

def main():
    global X, K
    _, K = map(int, input().split())
    X = list(map(int, input().split()))

    print(min(fullsearch()))

main()

Submission status: Accepted

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

def fullsearch(x, k):
    for l, r in zip(x, x[k-1:]):
        distance = abs(r - l)
        yield distance + abs(r)
        yield distance + abs(l)

def main():
    _, K = map(int, input().split())
    X = list(map(int, input().split()))



    print(min(fullsearch(X, K)))

main()

Submission status: Accepted

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


def main():
    _, K = map(int, input().split())
    X = list(map(int, input().split()))

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print(min(fullsearch()))

main()

Submission status: Accepted

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


def main():
    _, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for l, r in zip(X, X[K-1:]):
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print(min(fullsearch()))

main()

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = [0] * (N-K+1)
#連続するK本のろうそくの選び方はN-K+1回だから計算量はo(N)
for i in range(N-K+1):
    xr = x[i+K-1]
    xl = x[i]
    lr = abs(xl) + abs(xr-xl)
    rl = abs(xr) + abs(xr-xl)
    ans[i] = min(lr,rl)
a = min(ans)
print(a)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
tmp_x = list(map(int, input().split()))
import bisect

x=[]
for tmp in tmp_x:
    if tmp==0:
        k-=1
    else:
        x.append(tmp)
if k==0:
    print(0)
    exit()

z_ind = bisect.bisect_left(x,0)

mn_res=10**9
for i in range(1,k+2):
    l_ind = z_ind-i+1
    r_ind = z_ind-i+k

    if l_ind < 0:
        continue
    if r_ind >= len(x):
        continue

    l = x[l_ind]
    r = x[r_ind]

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

    if mn_res > result:
        mn_res=result
print(mn_res)

Submission status: Accepted

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


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K+1):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            yield min(distance + abs(r), distance + abs(l))

    print(min(fullsearch()))

main()

Submission status: Accepted

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


def main():
    N, K = map(int, input().split())
    X = [int(c) for c in input().split()]

    def fullsearch():
        for i in range(N-K+1):
            l = X[i]
            r = X[i+K-1]
            distance = abs(r - l)
            yield distance + abs(r)
            yield distance + abs(l)

    print(min(fullsearch()))

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

print(ans)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin

mod = 1000000007
inf = 1 << 60
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n, k = na()
x = na()

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

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc107/tasks/arc101_a

# 累積和を使って区間ごとの時間を求めて、更にスタートからの時間を足す
# 考える区間は0から左にK個〜右K個までK個ずつずらす

import sys
from bisect import bisect_left
read = sys.stdin.readline


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


N, K = read_ints()
X = read_ints()


# すべて正の場合
if X[0] >= 0:
    print(X[K - 1])
    exit()
# 負の場合
if X[-1] <= 0:
    print(abs(X[-K]))
    exit()


zeroidx = bisect_left(X, 0)
if X[zeroidx] == 0:
    K -= 1

l = max(zeroidx - K, 0)
# r_max = min(zeroidx + K, len(X) - 1)

ans = 10**16

for i in range(K):
    ll = l + i
    rr = ll + K - 1
    if rr >= len(X):
        break
    # print(X[ll:ll + K])
    ans = min(ans, 2 * abs(X[ll]) + abs(X[rr]),
              abs(X[ll]) + 2 * abs(X[rr]))


print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
nums = [int(i) for i in input().split()]
# n, k = 5, 3
# nums = [-30, -10, 10, 20, 50]

m = 10 ** 10
# 左から
for i in range(n-k+1):
    m = min(m, abs(nums[i]) + nums[i+k-1] - nums[i])

# 逆で考えるのindex的に大変なので-1をかけて反転する
nums = nums[::-1]
nums = [num * -1 for num in nums]

# 本当は関数化すべき
for i in range(n-k+1):
    m = min(m, abs(nums[i]) + nums[i+k-1] - nums[i])
print(m)

Submission status: Accepted

Solution code:
def main():
    _, k = map(int, input().split())
    x = tuple(map(int, input().split()))
    print(min(r - l + min(abs(l), abs(r)) for l, r in zip(x, x[k - 1 :])))
 
 
if __name__ == "__main__":
    main()

Submission status: Accepted

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

a = 10**10
for left,x in enumerate(X):
  right = left + K - 1
  if right == N:
    break
  left_to_right = abs(X[left])+abs(X[right]-X[left])
  right_to_left = abs(X[right])+abs(X[right]-X[left])
  m = min(left_to_right, right_to_left)
  if a > m:
    a = m
    
print(a)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left,bisect_right
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    N,K=map(int,input().split())
    x=list(map(int,input().split()))
    ans=INF
    zero=bisect_left(x,0)
    for i in range(K+1):
        if zero<i or N-zero<K-i:
            continue
        if i==K:
            tmp=abs(x[zero-i])
        elif i==0:
            tmp=x[zero+K-i-1]
        else:
            tmp=min(abs(x[zero-i])*2+x[zero+K-i-1],abs(x[zero-i])+x[zero+K-i-1]*2)
        ans=min(ans,tmp)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

res = float('inf')
for i in range(n-k+1):
    l = lst[i]
    r = lst[i+k-1]
    distlr = abs(l) + abs(l-r)
    distrl = abs(r) + abs(r-l)
    min_ = min(distlr, distrl)
    if min_ < res:
        res = min_
print(res)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left

N, K, *X = map(int, open(0).read().split())

# 0 を除外
X = [x for x in X if x != 0]

zero_index = bisect_left(X, 0)
# 0 にロウソクがある場合、Kを減らしてよい
K -= N - len(X)

# 座標 0 にロウソクがあるとき、開始時点でクリアしている場合がある
if K <= 0:
  print(0)
  sys.exit()

ret = float("inf")

# しゃくとり法で解く 
# 0 がどこに入るかでループの開始位置が変わる
if zero_index > K:
  head_start = zero_index - K
  tail_start = zero_index - 1
else:
  head_start = 0
  tail_start = K - 1
  
# 必要な時間は両端のロウソクの近い方を2倍して長い方を足したもの
# ループは O(K) = O(10^5) なので間に合う
for head, tail in zip(X[head_start:], X[tail_start:]):
  if head < 0 and tail < 0:
    ret = min(ret, -head)
  elif head < 0 and tail > 0:
    tmp = min(2 * tail - head, tail - 2 * head) 
    ret = min(ret, tmp)
  else: # head > 0 and tail > 0
    ret = min(ret, tail)

print(ret)

Submission status: Accepted

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

Xp = [0]+[+x for x in X if x>=0]
Xm = [0]+[-x for x in reversed(X) if x< 0]

Np = len(Xp)-1
Nm = len(Xm)-1

ans = 10**10
for np in range(min(Np, K), max(0, K-Nm)-1, -1):
    nm = K-np
    time = min(Xp[np], Xm[nm]) + Xp[np] + Xm[nm]
    ans = min(ans, time)

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

idx = bisect_left(x, 0)

l = x[:idx][::-1]
r = x[idx:]

if K-1 <= len(l):
    s = 0
else:
    s = K-1-len(l)
g = min(len(r), K) 
ans = float("inf")    
for i in range(s, g):
    if i == K-1:
        ans = min(ans, r[i])
    else:
        ans = min(ans, 2 * r[i] + abs(l[K-i-2]), 2 * abs(l[K-i-2]) + r[i])

if len(l) >= K:
    ans = min(ans, abs(l[K-1]))

print(ans)

Submission status: Accepted

Solution code:
"""
0からLまで行き、LからRまで行く = abs(L) + (R-L)
0からRまで行き、RからLまで行く = abs(R) + (R-L)
"""

N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 1<<60

for i in range(0, N-K+1):
  left = A[i]
  right = A[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())
posX = []
negX = []
for x in map(int, input().split()):
    if 0 <= x:
        posX.append(x)
    else:
        negX.append(-x)

negX.sort()

len_posX = len(posX)
len_negX = len(negX)

ans = float('inf')
for pos_k in range(len_posX + 1):
    neg_k = K - pos_k
    if len_negX <= neg_k - 1:
        continue

    if neg_k == 0 and len_posX != 0:
        ans = min(ans, posX[pos_k - 1])
    elif pos_k == 0 and len_negX != 0:
        ans = min(ans, negX[neg_k - 1])
    elif len_posX != 0 and len_negX != 0:
        ans = min(ans, posX[pos_k - 1] + 2 * negX[neg_k - 1], 2 * posX[pos_k - 1] + negX[neg_k - 1])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


ans = 10**10

for i in range(n-k+1):
    count = 0
    left = l[i]
    right = l[i+k-1]
    if left <= 0 and right > 0:
        count = min(abs(left),abs(right))*2+max(abs(left),abs(right))
    elif right <= 0:
        count = abs(left)
    else:
        count = right
    ans = min(ans,count)
print(ans)

Submission status: Accepted

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

Ln = [0] * N
for i in range(N-1):
    Ln[i+1] = abs(Xn[i+1] - Xn[i]) + Ln[i]

Rn = [0] * N
for i in range(1, N)[::-1]:
    Rn[i-1] = abs(Xn[i] - Xn[i-1]) + Rn[i]

ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, abs(Xn[i]) + Ln[i+K-1] - Ln[i])

for i in range(K-1, N)[::-1]:
    ans = min(ans, abs(Xn[i]) + Rn[i-K+1] - Rn[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

distans_list = [100000000000] * N
for i in range(N - K + 1):
    if X[i] < 0:
        if X[i+K-1] < 0:
            distans_list[i] = abs(X[i])
        else:
            if abs(X[i]) < abs(X[i+K-1]):
                distans_list[i] = 2 * abs(X[i]) + X[i+K-1]
            else:
                distans_list[i] = abs(X[i]) + 2 * X[i+K-1]
    else:
        distans_list[i] = X[i+K-1]
##print(distans_list)
print(min(distans_list))

Submission status: Accepted

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

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):
    left = X[i]
    right = X[i+k-1]
    if left * right < 0:
        result = min(abs(left), abs(right)) * 2 + max(abs(left), abs(right))
    else:
        result = max(abs(left), abs(right))
    ans.append(result)

print(min(ans))

Submission status: Accepted

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


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    ans = float("inf")
    for i in range(N + 1 - K):
        l = x[i]
        r = x[i + K - 1]
        if l < 0 < r:
            ans = min(ans, abs(l) + abs(r) + min(abs(l), abs(r)))
        else:
            ans = min(ans, max(abs(l), abs(r)))

    print(ans)

    


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    N, K = [int(x) for x in input().split()]
    x = [int(x) for x in input().split()]

    plus = [0]
    j = 0
    for i in range(N):
        if x[i] >= 0:
            plus.append(x[i])
            j += 1

    j = 0
    minus = [0]
    for i in range(N - 1, -1, -1):
        if x[i] < 0:
            minus.append(x[i])
            j += 1

    ans = float("inf")
    for i in range(0, K + 1):
        if len(plus) - 1 < i:
            continue

        if len(minus) - 1 < K - i:
            continue


        rsum = plus[i]
        lsum = abs(minus[K - i])

        if rsum >= lsum:
            ans = min(ans, rsum + lsum * 2)
        else:
            ans = min(ans, rsum * 2 + lsum)

    print(ans)

    


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 並んでるやつから取ったほうがいい + 折返しのぶんは左右どっちにいってから折り返すのがいいかminで判定
# cur, cur + kで調べる
n, k = map(int, input().split())
arr = list(map(int, input().split()))
ans = 10 ** 10
for i in range(n - k + 1):
    if arr[i] < 0 and arr[i + k - 1] < 0:
        ans = min(ans, abs(arr[i]))
    elif arr[i] < 0 and 0 <= arr[i + k - 1]:
        ans = min(
            ans, abs(arr[i]) * 2 + arr[i + k - 1], abs(arr[i]) + arr[i + k - 1] * 2
        )
    elif 0 <= arr[i] <= arr[i + k - 1]:
        ans = min(ans, arr[i + k - 1])

    # print("taken from {}, ans = {}".format(arr[i : i + k], ans))
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):
  f = X[i]
  t = X[i+K-1]
  if f <= 0 <= t:
    ans = min(ans, min(abs(f), abs(t)) + abs(f)+abs(t))
  else:
    ans = min(ans, max(abs(f), abs(t)))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if K==1 and 0 in X:
    print(0)
    exit()
if 0 in X:
    K-=1
left=sorted([i for i in X if i<0],reverse=True)
right=[i for i in X if i>0]

lst=set()
for l,L in enumerate(left):
    i=K-(l+1)-1
    if i>=0:
        if i<len(right):
            lst.add(right[i]-L*2)
    else:
        lst.add(-L)

for l,R in enumerate(right):
    i=K-(l+1)-1
    if i>=0:
        if i<len(left):
            lst.add(R*2-left[i])
    else:
        lst.add(R)

print(min(list(lst)))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = 10**9
for x1, x2 in zip(X, X[K-1:]):
    if x2 < 0 or 0 < x1:
        ans = min(ans, max(abs(x1), abs(x2)))
    else:
        xm = min(abs(x1), abs(x2))
        ans = min(ans, xm + abs(x1) + abs(x2))
print(ans)

Submission status: Accepted

Solution code:
import sys

n, k, *x = map(int, sys.stdin.read().split())

def main():
    l = 0; r = k - 1
    ans = float('inf')
    while r < n:
        if x[r] <= 0:
            res = abs(x[l])
        elif x[l] < 0:
            res = min(abs(x[l]), x[r]) * 2 + max(abs(x[l]), x[r])
        else:
            res = x[r]
        ans = min(ans, res)
        l += 1; r += 1
    
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

L=bisect.bisect_left(X,0)
Z=1 if L<N and X[L]==0 else 0
R=N-L-Z

l=max(0,L-(K-Z))
r=l+K-1
ans=10**8*4
while r<N:
    if X[l]<0<X[r]:
        tmp=min(abs(X[l]),abs(X[r]))*2 + max(abs(X[l]),abs(X[r]))
    else:
        tmp=max(abs(X[l]),abs(X[r]))
    ans=min(ans,tmp)
    l+=1
    r+=1

print(ans)

Submission status: Accepted

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


def min_for_interval(left, right):
    if left < 0:
        if right < 0:
            return abs(left)
        else:
            l_r = 2 * abs(left) + abs(right)
            r_l = 2 * abs(right) + abs(left)
            return min(l_r, r_l)
    else:
        return right


min_time = 1e9
for i in range(0, n - k + 1):
    left = xs[i]
    right = xs[i + k - 1]
    min_time = min(min_for_interval(left, right), min_time)

print(min_time)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = [int(i) for i in input().split()]
a = [abs(i) for i in x]
ans = float("inf")

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

print(ans)

Submission status: Accepted

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

answer = []
for i in range(N):
  if i + K > N:
    break
  Ys, Yt = X[i], X[i+K-1]
  if Ys > 0:
    answer.append(Yt)
  elif Yt < 0:
    answer.append(-Ys)
  else:
    answer.append(min(Ys*(-2)+Yt, Yt*2-Ys))

print(min(answer))

Submission status: Accepted

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
K = gi()
xs = [0] * N

for i in range(N):
    xs[i] = gi()

ans = abs(xs[0]) + abs(xs[0] - xs[N - 1])

for i in range(N):
    if i + K - 1 >= N:
        break

    l = xs[i]
    r = xs[i + K - 1]
    ans = min(ans, abs(r - l) + abs(r))
    ans = min(ans, abs(r - l) + abs(l))

print(ans)

Submission status: Accepted

Solution code:
def main():
    N, K, *xn = map(int, open(0).read().split())
    ans = int(1e9 + 7)
    for xl, xr in zip(xn, xn[K - 1:]):
        t = xr - xl + min(abs(xl), abs(xr))
        ans = t if t < ans else ans
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, K, *xn = map(int, open(0).read().split())
    ans = int(1e9 + 7)
    for xl, xr in zip(xn[:], xn[K - 1:]):
        t = min(ans, xr - xl + min(abs(xr), abs(xl)))
        ans = t if t < ans else ans
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, K, *xn = map(int, open(0).read().split())
    ans = int(1e9+7)
    for xl, xr in zip(xn[:], xn[K - 1:]):
        ans = min(ans, xr - xl + min(abs(xr), abs(xl)))
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
MAX_D = 300000001


def main():
    N, K, *xn = map(int, open(0).read().split())
    if 0 < xn[0]:
        print(xn[K - 1])
        return
    if xn[N - 1] < 0:
        print(abs(xn[N - K]))
        return
    ans = MAX_D
    for i in range(N - K + 1):
        if xn[i + K - 1] < 0 and i + K <= N - 1 and 0 < xn[i + K]:
            ans = min(ans, abs(xn[i]))
        elif 0 < xn[i]:
            if 0 != i:
                if xn[i - 1] < 0:
                    ans = min(ans, abs(xn[i + K - 1]))
                else:
                    break
        elif 0 <= xn[i + K - 1]:
            ans = min(ans,
                      2 * abs(xn[i]) + abs(xn[i + K - 1]),
                      abs(xn[i]) + 2 * abs(xn[i + K - 1]))
    print(ans)
    return


main()

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):
    xl = X[i]
    xr = X[i+K-1]
    if xl*xr < 0: tmp = min(abs(xl),abs(xr))*2 + max(abs(xl),abs(xr))
    elif xr <= 0: tmp = abs(xl)
    else: tmp = xr
    if tmp < ans: ans = tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N, K = list(map(int, sys.stdin.buffer.readline().split()))
X = list(map(int, sys.stdin.buffer.readline().split()))

li = 0
ri = K - 1
ans = INF
while ri < N:
    l = X[li]
    r = X[ri]
    if r <= 0:
        a = abs(l)
        ans = min(a, ans)
    elif l >= 0:
        a = abs(r)
        ans = min(a, ans)
    else:
        a = max(abs(l), abs(r)) + min(abs(l), abs(r)) * 2
        ans = min(a, ans)
    li += 1
    ri += 1
print(ans)

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+1-K):
    if arr[i]<= 0 and arr[i+K-1]>0:
        preans = min(abs(arr[i]*2) + arr[i+K-1], abs(arr[i]) + arr[i+K-1]*2)
    elif arr[i]<=0 and arr[i+K-1]<=0:
        preans = abs(arr[i])
    elif arr[i]>=0 and arr[i+K-1]>=0:
        preans = arr[i+K-1]
    ans = min(ans,preans)

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):
    if X[i] * X[K + i - 1] >= 0:
        ans1 = abs(X[i])
        ans2 = abs(X[K + i - 1])
        buf_ans = max(ans1, ans2)
        ans = min(ans, buf_ans)
    else:
        ans1 = abs(X[i])*2 + abs(X[K + i - 1])
        ans2 = abs(X[i]) + abs(X[K + i - 1])*2
        ans = min(ans, ans1, ans2)

print(ans)

Submission status: Accepted

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

ans = 3*10**8 + 1
for i in range(N-K+1):
  l = A[i]
  r = A[i+K-1]
  a = abs(l) + r-l
  b = abs(r) + r-l
  ans = min(ans, min(a,b))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
H,K = map(int,input().split())
x=list(map(int,input().split()))
m = sum([1 for i in x if i < 0])
xm = [-x[i] for i in range(m)]
xm.reverse()

if 0 in x:
  K = K-1
  xp = x[m+1:]
else:
  xp = x[m:]
xm = [0]+xm
xp = [0]+xp

if K == 0:
  ans = 0
else:
  ans = 3 * 10**8
  for i in range(min(K+1,len(xm))):
    if K-i < len(xp):
      x = xm[i]*2+xp[K-i]
      ans = min(x,ans) 
  for i in range(min(K+1,len(xp))):
    if K-i < len(xm):
      x = xp[i]*2+xm[K-i]
      ans = min(x,ans) 
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(0,N-K+1):
    ans = min(ans,abs(A[i])+abs(A[i]-A[i+K-1]),abs(A[i+K-1])+abs(A[i]-A[i+K-1]))

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
import numpy
input = sys.stdin.readline
N,K = [int(i) for i in input().split()]
li = [int(i) for i in input().split()]
mina = 10000000001
for i in range(N-K+1) :
    ll = abs(li[i])
    lr = abs(li[i+K-1])
    if li[i] * li[i+K-1] > 0 :
        l = max(ll,lr)
    else :
        if ll >= lr :
            l = lr * 2 + ll
        else :
            l = ll * 2 + lr
    mina = min(mina,l)
print(mina)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi==0:
        k-=1
    else:
        x.append(xi)
x.sort()
ans=float("INF")
zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

Submission status: Accepted

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

# print(X)

time = float('inf')
for n_i in range(n-k+1):
  r_side = X[n_i+k-1]
  l_side = X[n_i]
  time = min(time, min(abs(r_side), abs(l_side)) + abs(r_side-l_side))
  
print(time)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate

def solve(N: int, K: int, x: "List[int]"):
    minus = []
    plus = []
    answer = 10**9
    for i in range(N):
        if x[i] >= 0:
            plus.append(x[i])
        else:
            minus.append(abs(x[i]))
    minus = minus[::-1]

    for plus_count in range(0,K+1):
        minus_count = K- plus_count

    
        if 0 <= plus_count <= len(plus) and 0 <= minus_count <= len(minus):
            if plus_count == 0:
                answer = min(answer,minus[minus_count-1])
            elif minus_count ==0:
                answer = min(answer,plus[plus_count-1])
            else:
                answer = min(answer,plus[plus_count-1]+minus[minus_count-1]*2,plus[plus_count-1]*2+minus[minus_count-1])   

    print(answer)    
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, x)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
x = np.asarray(list(map(int,input().split())))
ans = 10**9
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]-x[i+K-1]))
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
# print(X)
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())
xlist = list(map(int, input().split()))
ans = []
for i in range(N - K + 1):
    left = xlist[i]
    right = xlist[i + K - 1]
    if left < 0 and right > 0 or left > 0 and right < 0:
        ans.append(min(abs(left), abs(right)) * 2 + max(abs(left), abs(right)))
    elif left == 0 or right == 0:
        ans.append(abs(left) + abs(right))
    else:
        ans.append(max(abs(left), abs(right)))
print(min(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(0, n-(k-1)):
    lr = abs(x[i]) + abs(x[i+k-1]-x[i])
    rl = abs(x[i+k-1]) + abs(x[i+k-1]-x[i])
    ans = min(lr, rl, ans)
print(ans)

Submission status: Accepted

Solution code:
##境界の処理が場当たり的すぎるが他を見てもコードの発想が理解できない
import bisect
n,k = map(int, input().split( ))
x = list(map(int, input().split( )))
inf = 10**15
x.append(inf)
start = bisect.bisect_left(x,0)

if x[start] == 0:
    k -= 1
    n -= 1
    x = [-inf]*k + x + [inf]*k###
else:
    x = [-inf]*k + x[:start] + [0] + x[start:] + [inf]*k###
#print(x)
ans = inf
start = bisect.bisect_left(x,0)
for i in range(k+1):###
    tmp1 = -x[start-i]
    tmp2 = x[start+k-i]
    #print(tmp1,tmp2)
    ans = min(ans, tmp1*2 + tmp2, tmp1 + tmp2*2)

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