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: Wrong Answer

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

N, K = map(int, input_s().split())
xxx = list(map(int, input_s().split()))
if K == 1:
    xxx = [abs(x) for x in xxx]
    print(min(xxx))
    exit()

# Select candles
min_distance = 10000000000
arg_min = 100000000
for i in range(N-K+1):
    left_edge, right_edge = xxx[i], xxx[i+K-1]
    tmp_larger = max(abs(left_edge), abs(right_edge))
    if tmp_larger < min_distance:
        arg_min = i
        min_distance = tmp_larger
    elif tmp_larger == min_distance and left_edge * right_edge > 0:
        # If all elements of an extracted list
        # are smaller than 0 or larger than 0
        arg_min = i
        min_distance = tmp_larger


# Select route
candles = xxx[arg_min:arg_min+K]
left_edge = candles[0]
right_edge = candles[-1]
left_edge_dist = abs(left_edge)
right_edge_dist = abs(right_edge)
if left_edge > 0:
    print(right_edge)
elif right_edge < 0:
    print(abs(left_edge))
elif left_edge_dist > right_edge_dist:
    print(right_edge_dist*2+left_edge_dist)
else:
    print(left_edge_dist*2+right_edge_dist)

Submission status: Wrong Answer

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

N, K = map(int, input_s().split())
xxx = list(map(int, input_s().split()))
if K == 1:
    xxx = [abs(x) for x in xxx]
    print(min(xxx))
    exit()

# Select candles
min_distance = 10000000000
arg_min = 100000000
for i in range(N-K+1):
    tmp_larger = max(abs(xxx[i]), abs(xxx[i+K-1]))
    if tmp_larger < min_distance:
        arg_min = i
        min_distance = tmp_larger

# Select route
candles = xxx[arg_min:arg_min+K]
left_edge = candles[0]
right_edge = candles[-1]
left_edge_dist = abs(left_edge)
right_edge_dist = abs(right_edge)
if left_edge > 0:
    print(right_edge)
elif right_edge < 0:
    print(abs(left_edge))
elif left_edge_dist > right_edge_dist:
    print(right_edge_dist*2+left_edge_dist)
else:
    print(left_edge_dist*2+right_edge_dist)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
seqs=list(map(int,input().split()))
minimum=2*abs(seqs[0])+2*abs(seqs[n-1])
if seqs[0]>0:
    print(seqs[k-1])
    
if seqs[n-1]<0:
    print(-seqs[n-k])
    
if seqs[0]<=0<=seqs[n-1]:
    for j in range(n-k):
        if seqs[j]<0<seqs[j+k-1]:
            minimum=min(minimum,seqs[j+k-1]-2*seqs[j],2*seqs[j+k-1]-seqs[j])
        if seqs[j]>0:
            minimum=min(minimum,seqs[j+k-1])
        
        
        if seqs[j+k-1]<0:
            minimum=min(minimum,-seqs[j])
    print(minimum)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
seqs=list(map(int,input().split()))
minimum=2*abs(seqs[0])+2*abs(seqs[n-1])
if seqs[0]>0:
    print(seqs[k-1])
    
if seqs[n-1]<0:
    print(-seqs[n-k])
    
if seqs[0]<=0<=seqs[n-1]:
    for j in range(n-k):
        if seqs[j]<0<seqs[j+k-1]:
            minimum=min(minimum,seqs[j+k-1]-2*seqs[j],2*seqs[j+k-1]-seqs[j])
        if seqs[j]>=0:
            minimum=min(minimum,seqs[j+k-1])
        
        
        if seqs[j+k-1]<=0:
            minimum=min(minimum,-seqs[j])
    print(minimum)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
seqs=list(map(int,input().split()))
minimum=abs(seqs[0])+abs(seqs[n-1])
if seqs[0]>0:
    print(seqs[k-1])
    
if seqs[n-1]<0:
    print(-seqs[n-k])
    
if seqs[0]<=0<=seqs[n-1]:
    for j in range(n-k):
        if seqs[j]<0<seqs[j+k-1]:
            minimum=min(minimum,seqs[j+k-1]-2*seqs[j],2*seqs[j+k-1]-seqs[j])
        if seqs[j]>=0:
            minimum=min(minimum,seqs[j+k-1])
        
        
        if seqs[j+k-1]<=0:
            minimum=min(minimum,-seqs[j])
    print(minimum)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())  # N本のろうそく、K本に火をつけたい
X = list(map(int, input().split()))

ans = 1e+9

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())  # N本のろうそく、K本に火をつけたい
X = list(map(int, input().split()))

ans = 1e+9

if K == 1:
    print(0)
    exit()

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

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())  # N本のろうそく、K本に火をつけたい
X = list(map(int, input().split()))

ans = 1e+9

if K == 1:
    print(0)
    exit()

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

print(ans)

Submission status: Wrong Answer

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

ans = 10**8*2

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: Wrong Answer

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

lr = [(x[i], x[i+k-1]) for i in range(n-k+1)]
l, r = min(lr, key=lambda x: abs(x[0] - x[1]))
print(min(abs(l), abs(r)) + abs(l - r))

Submission status: Wrong Answer

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

_, kx = zip(*x[:k+1])

l = abs(min(kx))
r = abs(max(kx))

print(min(l, r) + l + r)

Submission status: Wrong Answer

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: Wrong Answer

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[l]-add[r])))

print(ans)

Submission status: Wrong Answer

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

ans=10**9
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[l]-add[r])))

print(ans)

Submission status: Wrong Answer

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
    a=min(ans,abs(add[l])+abs(add[r]-add[l]),abs(add[r]+abs(add[l]-add[r])))
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
add=list(map(int,input().split()))
add +=[0,10**9,-10**9]
add=sorted(add)
point=add.index(0)
time=0

for i in range(k):
    r=point+1
    l=point-1
    time +=min(abs(add[r]-add[point]),abs(add[l]-add[point]))
    if abs(add[r]-add[point])<abs(add[l]-add[point]):
        del add[point]
        point +=1
    else:
        del add[point]
        point -=1

print(time)

Submission status: Wrong Answer

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):
    # 全部負数なら、片道
    if xm1 <= 0:
        return x0
    # 負数〜0〜正数
    elif x0 < 0:
        # 右から行くケースと、左から行くケースがある
        left = -1*x0*2 + xm1
        right = xm1*2 + abs(x0)
        return min(left, right)
    # 正数だけ
    else:
        return xm1

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: Wrong Answer

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):
    # 全部負数なら、片道
    if xm1 <= 0:
        return x0
    # 負数〜0〜正数
    elif x0 < 0:
        # 右から行くケースと、左から行くケースがある
        left = -1*x0*2 + xm1
        right = xm1*2 + abs(x0)
        return min(left, right)
    # 正数だけ
    else:
        return xm1

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: Wrong Answer

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):
    # 全部負数なら、片道
    if xm1 <= 0:
        return x0
    elif x0 < 0:
        return -1*x0*2 + xm1
    else:
        return xm1

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

print(count)

Submission status: Wrong Answer

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

def get_len(x):
    # 全部負数なら、片道
    if x[-1] <= 0:
        return x[0]
    elif x[0] < 0:
        return -1*x[0]*2 + x[-1]
    else:
        return x[-1]

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

print(count)

Submission status: Wrong Answer

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


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: Wrong Answer

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


def sol(lft, rgt):
    print(X[lft], X[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: Wrong Answer

Solution code:
from bisect import bisect


# Count how many zero appear in array x between i~i+k
def solve(n, k, x, how_many_zero):
    # ans is min x[zero_pos-k:zero_pos ~ zero_pos:zero_pos+k]
    zero_pos = bisect(x, 0)
    k -= how_many_zero
    lx = len(x)
    ans = float('inf')
    sum_of_diff_start_i = 0

    if k < 1 or lx < 1:
        return 0

    for i in range(zero_pos - k, zero_pos+1):
        if i >= 0 and i+k <= lx:
            sum_of_diff_start_i += abs(x[i])
            for j in list(range(i+1, i+k)):
                sum_of_diff_start_i += abs(x[j-1]-x[j])

            ans = min(ans, sum_of_diff_start_i)
            sum_of_diff_start_i = 0

    return ans


if __name__ == '__main__':
    n, k = map(int, input().split())
    x_ = list(map(int, input().split()))
    x = []
    how_many_zero = 0
    for xi in x_:
        if xi == 0:
            how_many_zero += 1
        else:
            x.append(xi)

    print(solve(n, k, x, how_many_zero))

Submission status: Wrong Answer

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

tmp=10**10
for i in range(N-K):
  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: Wrong Answer

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]),  (n[i+b-1]) + abs(n[i +b-1] - n[i]) )) )

print(min(ans))

Submission status: Wrong Answer

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: Wrong Answer

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

min_cost = 10*10
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: Wrong Answer

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

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 2*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: Wrong Answer

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: Wrong Answer

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 = 1e8 * 1e5  # !!!!
for k in range(K):  # 1e5
    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: Wrong Answer

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 = 1e9
for k in range(K):  # 1e5
    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: Wrong Answer

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, abs(X[l]) + abs(X[l] - X[r]))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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):
    l = ls[i:i + k]
    if all([x <= 0 for x in l]):
        res = min(res, -l[0])
    elif all([x >= 0 for x in l]):
        res = min(res, l[-1])
    else:
        res = min(res, -2 * l[0] + l[-1], -l[0] + 2 * l[-1])
print(res)

Submission status: Wrong Answer

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

for i in range(xi_0):
    if X[i] > 0:
        xi_0 = i

for j in range(K+1):
    if (X[xi_0-j]>0 and X[xi_0+K-1-j]>0) or (X[xi_0-j]<0 and X[xi_0+K-1-j]<0):
        ans=min(ans,max(abs(X[xi_0-j]),abs(X[xi_0+K-1-j])))
    else:
        ans=min(ans,abs(X[xi_0-j])+abs(X[xi_0+K-1-j])+min(abs(X[xi_0-j]),abs(X[xi_0+K-1-j])))

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i)for i in input().split())
    x = [int(i) for i in input().split()]
    ans = float('inf')
    st_val = min(x,key=abs)
    st_idx = x.index(st_val)

    for i in range(K+1):
        dist = float('inf')
        if 0 <= st_idx-K+1+i and st_idx+1+i <= N:
            cur = x[st_idx-K+1+i:st_idx+1+i]
            if i == 0:
                dist = abs(cur[0])
            elif i == K or (cur[0] >= 0 and cur[-1] > 0):
                dist = abs(cur[-1])
            else:
                if abs(cur[0]) > abs(cur[-1]):
                    dist = abs(cur[0]) + abs(cur[-1]) * 2
                else:
                    dist = abs(cur[0]) * 2 + abs(cur[-1])
        ans = min(ans,dist)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i)for i in input().split())
    x = [int(i) for i in input().split()]
    ans = float('inf')
    st_val = min(x,key=abs)
    st_idx = x.index(st_val)

    for i in range(K+1):
        dist = float('inf')
        if 0 <= st_idx-K+1+i and st_idx+1+i <= N:
            cur = x[st_idx-K+1+i:st_idx+1+i]
            if i == 0:
                dist = abs(cur[0])
            elif i == K or (cur[0] >= 0 and cur[-1] > 0):
                dist = abs(cur[-1])
            else:
                if abs(cur[0]) > abs(cur[-1]):
                    dist = abs(cur[0]) + abs(cur[-1]) * 2
                else:
                    dist = abs(cur[0]) * 2 + abs(cur[-1])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

arr = []
for i in range(n-k+1):
    if x[i+k-1] <= 0:
        arr.append(-x[i])
    elif x[i] < 0:
        arr.append(x[i+k-1]-x[i]-x[i])
    else:
        arr.append(x[i+k-1])

print(min(arr))

Submission status: Wrong Answer

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

arr = []
for i in range(n-k+1):
    if x[i+k-1] < 0:
        arr.append(-x[i])
    elif x[i] < 0:
        arr.append(x[i+k-1]-x[i]-x[i])
    else:
        arr.append(x[i+k-1])

print(min(arr))

Submission status: Wrong Answer

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

arr = []
for i in range(n-k+1):
    if x[i+k-1] < 0:
        arr.append(-x[i])
    elif x[i] < 0:
        arr.append(x[i+k-1]-x[i]-x[i])
    else:
        arr.append(x[i+k-1])

print(arr)
print(min(arr))

Submission status: Wrong Answer

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

arr = []
for i in range(n-k+1):
    if x[i] < 0:
        arr.append(x[i+k-1]-x[i]-x[i])
    elif x[i+k-1] < 0:
        arr.append(-x[i])
    else:
        arr.append(x[i+k-1])

print(min(arr))

Submission status: Wrong Answer

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

arr = []
for i in range(n-k+1):
    if x[i] < 0:
        arr.append(x[i+k-1]-x[i]-x[i])
    else:
        arr.append(x[i+k-1])

print(min(arr))

Submission status: Wrong Answer

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

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: Wrong Answer

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
    elif r<m:
        m=r
print(m)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=[int(_) for _ in input().split()]
m=10**12
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
    elif r<m:
        m=r
print(m)

Submission status: Wrong Answer

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

for i in range(len(x)):
    x2 += [[abs(x[i]), x[i]]]

x2 = sorted(x2)

import numpy as np

x2 = np.array(x2)

_x2 = x2[:k,:]

_min = min(_x2[:, 1])
_max = max(_x2[:, 1])

if _min >= 0:
    print(_max)
elif _max < 0:
    print(abs(_min))
elif abs(_min) > abs(_max):
    print(abs(_max)*2 + abs(_min))
elif abs(_min) <= abs(_max):
    print(abs(_min)*2 + abs(_max))

Submission status: Wrong Answer

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

for i in range(len(x)):
    x2 += [[abs(x[i]), x[i]]]

x2 = sorted(x2)

import numpy as np

x2 = np.array(x2)

_x2 = x2[:k,:]

_min = min(_x2[:, 1])
_max = max(_x2[:, 1])

if _min >= 0:
    print(_max)
elif _max <= 0:
    print(_min)
elif abs(_min) > abs(_max):
    print(abs(_max)*2 + abs(_min))
elif abs(_min) <= abs(_max):
    print(abs(_min)*2 + abs(_max))

Submission status: Wrong Answer

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

for i in range(len(x)):
    x2 += [[abs(x[i]), x[i]]]

x2 = sorted(x2)

import numpy as np

x2 = np.array(x2)

_x2 = x2[:k,:]

_min = min(_x2[:, 1])
_max = max(_x2[:, 1])

if _min >= 0:
    print(_max)
elif _max <= 0:
    print(_min)
elif abs(_min) > abs(_max):
    print(abs(_max)*2 + abs(_min))
else:
    print(abs(_min)*2 + abs(_max))

Submission status: Wrong Answer

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

loop_cnt = N - K + 1
ans = 10**7

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: Wrong Answer

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

loop_cnt = N - K + 1
ans = 10**5

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: Wrong Answer

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

loop_cnt = N - K + 1
ans = 10**5

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

print(ans)

Submission status: Wrong Answer

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, 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: Wrong Answer

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 = abs(L[i])
    right = abs(L[i + K - 1] - L[i])
    d = min(d, left + right)

print(d)

Submission status: Wrong Answer

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):
    l = L[i:i + K]
    l = [0] + l
    s = 0
    for j in range(K):
        s += abs(l[j] - l[j + 1])
    d = min(s, d)

print(d)

Submission status: Wrong Answer

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

l=0
r=0
ans=10**6

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: Wrong Answer

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
for i in range(n - k + 1):
    if idx < i:
        break
    t = xli[i:i + k]
    if t[0] * t[-1] < 0:
        ans = min(ans, 2 * abs(t[0]) + abs(t[-1]), abs(t[0]) + 2 * abs(t[-1]))
    else:
        if t[0] <= 0:
            ans = min(ans, abs(t[0]))
        else:
            ans = min(ans, abs(t[-1]))
print(ans)

Submission status: Wrong Answer

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
for i in range(n - k + 1):
    if i < idx:
        break
    t = xli[i:i + k]
    if t[0] * t[-1] < 0:
        ans = min(ans, 2 * abs(t[0]) + abs(t[-1]), abs(t[0]) + 2 * abs(t[-1]))
    else:
        if t[0] <= 0:
            ans = min(ans, abs(t[0]))
        else:
            ans = min(ans, abs(t[-1]))
print(ans)

Submission status: Wrong Answer

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
for i in range(n - k + 1):
    if i > idx:
        break
    t = xli[i:i + k]
    if t[0] * t[-1] < 0:
        ans = min(ans, 2 * abs(t[0]) + abs(t[-1]), abs(t[0]) + 2 * abs(t[-1]))
    else:
        if t[0] <= 0:
            ans = min(ans, abs(t[0]))
        else:
            ans = min(ans, abs(t[-1]))
print(ans)

Submission status: Wrong Answer

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

ans = max(abs(x[0]), abs(x[-1])) * 2

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
time=1000000
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: Wrong Answer

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

Submission status: Wrong Answer

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