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:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

#WAA TLEEEEEEEEEE
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]

ll=len(l)
rr=len(r)

ans=sum(l)*2+sum(r)*2
if ll==k: ans=min(ans,sum(l))
if rr==k: ans=min(ans,sum(r))
lx=sum(l)*2
rx=sum(r)*2
for i in range(1,k):
    if rr-i>0 and i<=ll and min(i,ll)+(rr-i)>=k:
        lx=lx-(l[-i]*2)+r[i-1]
        ans=min(ans,lx)
    if ll-i>0 and i<=rr and min(i,rr)+(ll-i)>=k:
        rx=rx-(r[-i]*2)+l[i-1]
        ans=min(ans,rx)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)

ans=sum(l)*2+sum(r)*2
if ll==k: ans=min(ans,sum(l))
if rr==k: ans=min(ans,sum(r))
for i in range(1,k+1):
    if rr>=k-(ll-i): ans=min(ans,(sum(l[:-i])*2)+sum(r[:k-(ll-i)]))
    if ll>=k-(rr-i): ans=min(ans,(sum(r[:-i])*2)+sum(l[:k-(rr-i)]))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)

ans=sum(l)*2+sum(r)*2
if ll==k: ans=min(ans,sum(l))
if rr==k: ans=min(ans,sum(r))
for i in range(1,k):
    if rr>=k-(ll-i): ans=min(ans,(sum(l[:-i])*2)+sum(r[:k-(ll-i)]))
    if ll>=k-(rr-i): ans=min(ans,(sum(r[:-i])*2)+sum(l[:k-(rr-i)]))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)

ans=sum(l)*2+sum(r)*2
if ll==k: ans=min(ans,sum(l))
if rr==k: ans=min(ans,sum(r))
for i in range(1,k):
    if rr>i and len(l[:-i])+i==k: ans=min(ans,(sum(r[:i])+sum(l[:-i])*2))
    if ll>i and len(r[:-i])+i==k: ans=min(ans,(sum(l[:i])+sum(r[:-i])*2))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in x:
    if i<0: l.append(-i)
    if i>=0: r.append(i)
l.sort()
r.sort()
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)
lx,rx=[],[]
for i in range(ll):
    if i==0: lx.append(l[i])
    else: lx.append(l[i]-l[i-1])
for i in range(rr):
    if i==0: rx.append(r[i])
    else: rx.append(r[i]-r[i-1])

ans=sum(lx)*2+sum(rx)*2
for i in range(ll):
    if k-1==i: ans=min(ans,sum(lx))
    elif i+rr+1>=k: ans=min(ans,sum(lx[:i+1])*2+sum(rx[:k-i-1]))
    print(lx[:i+1],rx[:k-i-1])

for i in range(rr):
    if k-1==i: ans=min(ans,sum(rx))
    elif i+ll+1>=k: ans=min(ans,sum(rx[:i+1])*2+sum(lx[:k-i-1]))
    print(rx[:i+1],lx[:k-i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in x:
    if i<0: l.append(-i)
    if i>=0: r.append(i)
l.sort()
r.sort()
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)
lx,rx=[],[]
for i in range(ll):
    if i==0: lx.append(l[i])
    else: lx.append(l[i]-l[i-1])
for i in range(rr):
    if i==0: rx.append(r[i])
    else: rx.append(r[i]-r[i-1])

ans=sum(lx)*2+sum(rx)*2
for i in range(ll):
    if k-1==i: ans=min(ans,sum(lx))
    elif i+rr+1>=k: ans=min(ans,sum(lx[:i+1])*2+sum(rx[:k-i-1]))

for i in range(rr):
    if n-1==i: ans=min(ans,sum(rx))
    elif i+ll+1>=k: ans=min(ans,sum(rx[:i+1])*2+sum(lx[:k-i-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in x:
    if i<0: l.append(-i)
    if i>=0: r.append(i)
l.sort()
r.sort()
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)
lx,rx=[],[]
for i in range(ll):
    if i==0: lx.append(l[i])
    else: lx.append(l[i]-l[i-1])
for i in range(rr):
    if i==0: rx.append(r[i])
    else: rx.append(r[i]-r[i-1])

ans=sum(lx)*2+sum(rx)*2
for i in range(ll):
    if k-1==i: ans=min(ans,sum(lx))
    elif i+rr+1>=k: ans=min(ans,sum(lx[:i+1])*2+sum(rx[:k-i-1]))

for i in range(rr):
    if n-1==i: ans=min(ans,sum(r))
    elif i+ll+1>=k: ans=min(ans,sum(rx[:i+1])*2+sum(lx[:k-i-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

abs_X = [abs(x) for x in X]

sorted_abs_X = sorted(abs_X)[:K]
sorted_index = np.array(abs_X).argsort()[:K]

new = [X[idx] for idx in sorted_index]

x_min = min(new)
x_max = max(new)

if x_min >= 0:
    print(x_max)
    sys.exit(0)
else:
    if -x_min >= x_max:
        print(x_max*2 - x_min)
        sys.exit(0)
    else:
        print(x_max - x_min*2)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
N, K = map(int, input().split())
x = [int(s) for s in input().split()]

izero = bisect_left(x, 0)
zpos = x[izero:]
neg = [-v for v in reversed(x[:izero])]

ans = 1000000000
if len(neg) >= K and ans > neg[K - 1]:
    ans = neg[K - 1]
if len(zpos) >= K and ans > zpos[K - 1]:
    ans = zpos[K - 1]
for lenneg in range(max(1, K - len(zpos)), min(K - 1, len(neg))):
    cand = 2 * neg[lenneg - 1] + zpos[K - lenneg - 1]
    if ans > cand:
        ans = cand
    cand = neg[lenneg - 1] + 2 * zpos[K - lenneg - 1]
    if ans > cand:
        ans = cand
print(ans)

Submission status: Wrong Answer

Solution code:
#n=int(input())
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in x:
    if i<0: l.append(-i)
    if i>0: r.append(i)
l.sort()
r.sort()
l=l[:n]
r=r[:n]

w=n-len(l)
if len(l)==0 or len(r)==0:
    print(sum(l)+sum(r))
    exit()

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
time = 10**8
for i in range(len(x) - k + 1):
    x_k = x[i:i+k]
    #print(x_k)
    if x_k[0] >= 0:
        time = min(x_k[-1], time)
        pass
            
    elif x_k[-1] <= 0:
        time = min(-x_k[0], time)
        pass
    
    elif -x_k[0] <= x_k[-1]:
        t = -x_k[0] * 2 + x_k[-1]
        time = min(t, time)
        pass
    
    else:
        t = -x_k[0] + x_k[-1] *2
        time = min(t, time)
        pass
    
print(time)

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
time = 10**8
for i in range(len(x) - k + 1):
    x_k = x[i:i+k]
    #print(x_k)
    if x_k[0] >= 0:
        time = min(x_k[-1], time)
            
    elif x_k[-1] <= 0:
        time = min(-x_k[0], time)

    elif -x_k[0] <= x_k[-1]:
        t = -x_k[0] * 2 + x_k[-1]
        time = min(t, time)
    else:
        t = -x_k[0] + x_k[-1] *2
        time = min(t, time)

print(time)

Submission status: Wrong Answer

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

result = []
for i in range(N-K+1):
    s,g = i,i+K-1
    result.append(2*abs(x[s]) + abs(x[g]))
    result.append(abs(x[s]) + 2*abs(x[g]))
result.sort()
print(result[0])

Submission status: Wrong Answer

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
time = 10**8
for i in range(len(x) - k + 1):
    x_k = x[i:i+k]
    #print(x_k)
    if x_k[0] >= 0:
        time = min(x_k[-1], time)
            
    elif x_k[-1] <= 0:
        time = min(x_k[0], time)

    elif -x_k[0] <= x_k[-1]:
        t = -x_k[0] * 2 + x_k[-1]
        time = min(t, time)
    else:
        t = -x_k[0] + x_k[-1] *2
        time = min(t, time)

print(time)

Submission status: Wrong Answer

Solution code:
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
x = list(map(int, input().split()))
min_d = 2 * 10 ** 8
for i in range(n - k + 1):
    max_i = x[i + k - 1]
    min_i = x[i]
    if max_i <= 0:
        min_d_i = - min_i
    elif min_i >= 0:
        min_d_i = max_i
    else:
        min_d_i = min(- min_i + 2 * max_i, -2 * min_i + max_i)
    if min_d_i < min_d:
            min_d = min_d_i
print(min_d)

Submission status: Wrong Answer

Solution code:
iN,iK = [int(_) for _ in input().split()]
aX = [int(_) for _ in input().split()]
ans = 10**5
iDiff = iN-iK
for i in range(iDiff + 1):
    iL = aX[i]
    iR = aX[iN-iDiff+i-1]
    ans = min(ans ,abs(iL) + abs(iL-iR),abs(iR) + abs(iR-iL))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
ANS = []
for i in range(len(L)-K+1) :
    p = L[i]
    q = L[i+K-1]
    if p*q == 0 :
        r = abs(p-q)
    elif p*q < 0 :
        r = min(abs(p),q)*2+max(abs(p),q)
    else :
        r = q
    ANS.append(r)
ans = min(ANS)
print(ans)

Submission status: Wrong Answer

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

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


for i in range(n):
    if l[i] >= 0:
        ind = i

left = min(n-1,i+k)

ans = 1e10

while left >= k-1:
    if l[left]*l[left-k+1]>=0:
        d = abs(l[left])
    else:
        a = abs(l[left])
        b = abs(l[left-k+1])
        d = min(2*a+b,a+2*b)
    if d < ans:
        ans=d
    left -=1

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
L = [0]*(N-K+1) 

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

Submission status: Wrong Answer

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

sum = 10 ** 5 * 3 + 1
for i in range(n - k + 1):
    cur = arr[i + k - 1] - arr[i] + min(abs(arr[i + k - 1]), abs(arr[i]))
    if sum > cur:
        sum = cur
print sum

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = [int(i) for i in input().split()]
y = list(map(abs,x))
xl=0
xr=0
loop=n-k+1
ans=1000000

for i in range(loop):
    if(x[i]*x[i+k-1]>=0):
        tmp=max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
    else:
        tmp=2*min(y[i],y[i+k-1])+max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = [int(i) for i in input().split()]
y = list(map(abs,x))
xl=0
xr=0
loop=n-k+1
ans=1000000

for i in range(loop):
    if(x[i]*x[i+k-1]>0):
        tmp=max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
    else:
        tmp=2*min(y[i],y[i+k-1])+max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, k = map(int, input().split())
x = [int(i) for i in input().split()]

mn = 0xFFFFFFFF
for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    t = abs(l) + abs(r - l)
    mn = min(t, mn)
print(mn)

Submission status: Wrong Answer

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

n, k = map(int, input().split())
x = [int(i) for i in input().split()]

mn = 0xFFFFFFFF
for i in range(n):
    if i + k - 1 > n - 1:
        print(mn)
        sys.exit()
    l = x[i]
    r = x[i + k - 1]
    t = abs(l) + abs(l - r)
    mn = min(t, mn)
print(0)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
N, K = map(int, input().split(' '))
x = list(map(int, input().strip().split()))
ans=100000000

for i in range(N-K+1):
#    print(i)
    xl=x[i]
    xr=x[i+K-1]
    ans=min(ans,min(abs(xr)+abs(xr-xl),abs(xl)+abs(xl-xr)))
    

print(ans)

Submission status: Wrong Answer

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

ans = 2 * (10 ** 5) + 1
for i in range(N - K + 1):
    l = x[i]
    r = x[i+K-1]
    ans = min([ans, abs(l) + r - l, abs(r) + r - l])

print(ans)

Submission status: Wrong Answer

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

ans = 2 * (10 ** 5) + 1
for i in range(N - K + 1):
    p = x[i:i+K]
    a = abs(p[0]) + abs(p[0] - p[-1])
    b = abs(p[-1]) + abs(p[0] - p[-1])
    ans = min([ans, a, b])

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect


def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 99999999999
    for i in range(n-k+1):
        s = 0
        if left > i+k:
            s = b[i]
        elif right < i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())

Submission status: Wrong Answer

Solution code:
import bisect


def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    c = [0] * n
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 99999999999
    for i in range(n-k+1):
        s = 0
        if left >= i+k:
            s = b[i]
        elif right <= i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())

Submission status: Wrong Answer

Solution code:
import bisect

def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    c = [0] * n
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 9999999999
    for i in range(n-k+1):
        s = 0
        if left >= i+k:
            s = b[i]
        elif right <= i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())

Submission status: Wrong Answer

Solution code:

N, K = map(int, input().split())
x = list(map(int,input().split()))
sorted_x = [0] * N


sorted_x = sorted(x, key=abs)

copy_x = [0] * K
for i in range(K):
    copy_x[i] = sorted_x[i]


positive_integer = sum(i>=0 for i in copy_x)
negative_integer = sum(i<0 for i in copy_x)

if(max(copy_x) >= abs(min(copy_x))):
    distance_max_positive =  True
else:
    distance_max_positive = False


 # 距離計算
distance_right = 0
distance_left = 0
if negative_integer ==0 :
    distance_right = max(copy_x)

elif positive_integer ==0 :
    distance_left = abs(min(copy_x))

elif distance_max_positive:
     distance_left = abs(min(copy_x)) * 2
     distance_right = max(copy_x)

else:
    distance_left = abs(min(copy_x))
    distance_right = max(copy_x) * 2

print(distance_left + distance_right)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
if x[0] >= 0:
    print(x[k - 1])
elif x[-1] <= 0:
    print(x[-k])
else:
    for i in range(n - 1):
        if x[i] < 0 and x[i + 1] >= 0:
            idx = i
            break
    t = []
    for i in range(idx - (k - 1), idx + 2):
        if i >= 0 and i + k - 1 < n:
            t.append(x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i])))
    print(min(t))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
idx = -1
for i in range(n - 1):
    if x[i] < 0 and x[i + 1] >= 0:
        idx = i
        break
t = []
for i in range(idx - (k - 1), idx + 3):
    if i >= 0 and i + k - 1 < n:
        t.append(x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i])))
print(min(t))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
idx = -1
for i in range(n - 1):
    if x[i] < 0 and x[i + 1] >= 0:
        idx = i
        break
t = []
for i in range(idx - (k - 1), idx + 2):
    if i >= 0 and i + k - 1 < n:
        t.append(x[i + k - 1] - x[i] + min(abs(x[i + k - 1]), abs(x[i])))
print(min(t))

Submission status: Wrong Answer

Solution code:
import sys

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

r = 0
for idx, xi in enumerate(x):
    if xi >= 0:
        r = idx
        break

list = []

S = r - K
if x[r] == 0:
    S += 1
S = max(S, 0)

for s in range(S, r+1):
    e = s + K - 1
    if len(x) <= e:
        break
    list.append(abs(x[e] - x[s]) + abs(x[s]))
    list.append(abs(x[e] - x[s]) + abs(x[e]))

print(min(list))

sys.exit(0)

Submission status: Wrong Answer

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

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

ans = float('inf')

for i in range(n - k + 1):
	if arr[i] * arr[k + i -1] >= 0:
		tmp = max(abs(arr[i]), abs(arr[k + i -1]))
		print(tmp)
	else:
		tmp = abs(arr[i]) + abs(arr[k + i -1]) + min(abs(arr[i]), abs(arr[k + i -1]))
	ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

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

ans = float('inf')

for i in range(n - k):
	if arr[i] * arr[k + i -1] >= 0:
		tmp = max(abs(arr[i]), abs(arr[k + i -1]))
	else:
		tmp = abs(arr[i]) + abs(arr[k + i -1]) + min(abs(arr[i]), abs(arr[k + i -1]))
	ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
ans = 1000000000
for i in range(n - k + 1):
    ans = min(ans,abs(x[i]) + abs(x[i] - x[i + k - 1]))
for i in range(n-1,k-1,-1):
    ans = min(ans,abs(x[i]) + 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 = 1000000000
for i in range(n - k + 1):
   ans = min(ans,abs(x[i]) + 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()))
for i in range(n-k+1):
    if i==0:
        a=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
    else:
        a=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
print(a)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return [int(i) for i in input().split()]

n,k = inpl()
C =inpl()

ans = 10**9
for i in range(n-k+1):
    ans = min(ans,abs(C[i]-C[k+i-1])+abs(min(C[i],C[k+i-1])))

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
  a.remove(0)
  n -= 1
  k -= 1
if k == 0:
  print(0)
  exit()
m = 300000000
for i in range(n+1-k):
  if a[i+k-1] < 0:
    x = -a[i]
  if a[i] > 0:
    x = a[i+k-1]
  else:
    x = (a[i+k-1]-a[i]) + min(-a[i],a[i+k-1])
  m = min(m,x)
print(m)

Submission status: Wrong Answer

Solution code:
import copy
 
n,k = map(int,input().split())
x = list(map(int,input().split()))
if 0 in x:
    x.remove(0)
    k -= 1

ans = 10**14
for i in range(2**(k)):
    blis=list(map(int,list(bin(i)[2::].zfill(k))))
    xs = copy.deepcopy(x)
    s = 0
    aans = 0
    print(blis)
    for bli in blis:
        if bli == 1:
            for i in range(len(xs))[::-1]:
                if xs[i] < s:
                    if i == len(xs)-1:
                        s1 = xs.pop(i)
                        #print(" ch1-1",s1)
                        aans += abs(s-s1)
                        #print("  aans",aans)
                        s = s1
                        break
                    else:
                        s1 = xs.pop(i+1)
                        #print(" ch1-1",s1)
                        aans += abs(s-s1)
                        #print("  aans",aans)
                        s = s1
                        break
                if i == 0:
                    s1 = xs.pop(0)
                    #print(" ch1-3",s1)
                    aans += abs(s-s1)
                    s = s1
                    #print("  aans",aans)
                    break
        else:
            for i in range(len(xs)):
                if xs[i] > s:
                    if i == 0:
                        s1 = xs.pop(i)
                        #print(" ch0-1",s1)
                        aans += abs(s-s1)
                        #print("  aans",aans)
                        s = s1
                        break
                    else:
                        s1 = xs.pop(i-1)
                        #print(" ch0-2",s1)
                        aans += abs(s-s1)
                        #print("  aans",aans)
                        s = s1
                        break
                if i == len(xs)-1:
                    s1 = xs.pop(i)
                    #print(" ch0-3",s1)
                    aans += abs(s-s1)
                    #print("  aans",aans)
                    s = s1
                    break
        #print(" ",xs)
        if aans >= ans:
            break
    ans = min(ans,aans)
    #print(ans)
print(ans)

Submission status: Wrong Answer

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

ans = max(X)*4
for i in range(N - K + 1):
    l = i
    r = i + K - 1

    Xl, Xr = X[l], X[r]
    ans = min(abs(Xl) + abs(Xl - Xr), abs(Xr) + abs(Xr - Xl) , ans)


print(ans)

Submission status: Wrong Answer

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

ans = max(X)*4
for i in range(N - K + 1):
    l = i
    r = i + K - 1
    Xl, Xr = X[l], X[r]
    ans = min(min(abs(Xl), abs(Xr)) + abs(Xl - Xr) , ans)


print(ans)

Submission status: Wrong Answer

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

ans = max(X)*4
for i in range(N - K + 1):
    l = i
    r = i + K - 1
    Xl, Xr = X[l], X[r]
    ans = min(abs(Xl) + abs(Xl - Xr), ans)


print(ans)

Submission status: Wrong Answer

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

ans = max(X)*4
for i in range(N - K + 1):
    l = i
    r = i + K - 1
    Xl, Xr = X[l], X[r]
    if Xl * Xr < 0:
        ans = min(min(abs(Xl),abs(Xr)) * 2 + max(abs(Xl),abs(Xr)), ans)
    else:
        ans = min(abs(max(abs(Xl),abs(Xr))), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

if N == 1:
    print(abs(x[0]))
else:
    ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
              (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

    for i in range(1, len(x) - K):
        left = i
        right = i + K - 1

        letf_to_right = abs(x[left]) + abs(x[right] - x[left])
        right_to_left = abs(x[right]) + abs(x[left] - x[right])

        tmp = min(letf_to_right, right_to_left)

        if tmp < ans:
            ans = tmp

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

if N == 1:
    print(abs(x[0]))
else:
    ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
              (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

    for i in range(1, len(x) - K):
        tmp = min((abs(x[i]) + abs(x[i + K - 1] - x[i])),
                  (abs(x[i + K - 1]) + abs(x[i] - x[i + K - 1])))

        if tmp < ans:
            ans = tmp

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

if N == 1:
    print(x[0])
else:
    ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
              (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

    for i in range(1, len(x) - K):
        tmp = min((abs(x[i]) + abs(x[i + K - 1] - x[i])),
                  (abs(x[i + K - 1]) + abs(x[i] - x[i + K - 1])))

        if tmp < ans:
            ans = tmp

    print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
          (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

for i in range(1, len(x) - K):
    tmp = min((abs(x[i]) + abs(x[i + K - 1] - x[i])),
              (abs(x[i + K - 1]) + abs(x[i] - x[i + K - 1])))
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
T=10**8
for i in range(N-K+1):
    xr=x[i]
    xl=x[i+K-1]
    d1=abs(xr)+xl-xr
    d2=abs(xl)+xl-xr
    if T>d1:
        T=d1
    if T>d2:
        T=d2   
print(T)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
xl = [int(i) for i in input().split()]
inf = float('inf')
ans = inf
for i in range(n-k):
	ans = min(abs(xl[i])+xl[i+k-1]-xl[i], \
						abs(xl[i+k-1])+xl[i+k-1]-xl[i], ans)
print(ans if ans != inf else 0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
xl = [int(i) for i in input().split()]
inf = float('inf')
ans = inf
for i in range(n-k):
	ans = min(abs(xl[i])+xl[i+k-1]-xl[i], \
						abs(xl[i+k])+xl[i+k-1]-xl[i], ans)
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):
    temp=abs(0-x[i])+abs(x[i]-x[k+i-1])
    ans.append(temp)
print(min(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N == 1:
    min_d = abs(x[0])
else:
    for i ,x_i in enumerate(x[:-(K-1)]):
        if min(abs(x_i), abs(x[i+(K-1)])) + abs(x_i - x[i+(K-1)]) < min_d:
            min_d = abs(x_i) + abs(x_i - x[i+(K-1)])

print(min_d)

Submission status: Wrong Answer

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

if N == 1:
    min_d = abs(x[0])
else:
    for i ,x_i in enumerate(x[:-(K-1)]):
        if abs(x_i) + abs(x_i - x[i+(K-1)]) < min_d:
            min_d = abs(x_i) + abs(x_i - x[i+(K-1)])

print(min_d)

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):
    light = x[i:i+K]
    l = light[0]
    r = light[-1]
    if l*r >= 0:
        time = max(abs(l), abs(r))
    else:
        time = min(abs(l)*2+abs(r), abs(l)+abs(r)*2)
    if time < ans:
        ans = time
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
x = list(map(int, input().split()))
dist = sorted([[abs(x),i,x] for i, x in enumerate(x)])
light = sorted([tmp[2] for tmp in dist[:K]])
if light[0] * light[-1] > 0:
    time = max(abs(light[0]), abs(light[-1]))
else:
    time = min(abs(light[0])*2+abs(light[-1]),
               abs(light[0])+abs(light[-1])*2)
print(time)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
x = list(map(int, input().split()))
dist = sorted([[abs(x),i,x] for i, x in enumerate(x)])
light = sorted([tmp[2] for tmp in dist[:K]])
if light[0] * light[-1] > 0:
    time = max(abs(light[0]), abs(light[-1]))
else:
    time = min(abs(light[0])*2+abs(light[-1]),
               abs(light[0])+abs(light[-1]*2))
print(time)

Submission status: Wrong Answer

Solution code:
inp1 = [int(x) for x in input().split()]
inp2 = [int(x) for x in input().split()]

ans = 999999
for i in range(inp1[0] - inp1[1] + 1):
    l = i
    r = i + inp1[1] - 1
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
for i in range(inp1[0]-1, inp1[1]-1, -1):
    l = i - inp1[1] + 1
    r = i
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
print(ans)