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: Runtime Error

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
x_sei = []
x_hu = []
for i in range(n):
    if x[i] < 0:
        x_hu.append(x[i])
    else:
        x_sei.append(x[i])
x_hu.reverse()
if not x_hu:
    print(x_sei[k-1])
    exit()
if not x_sei:
    print(abs(x_hu[k-1]))
    exit()
cnt = 0
sei_i = 0
hu_i = 0
sei_nax = 0
hu_nax = 0
while sei_i + hu_i != k:
    a = x_sei[sei_i]
    b = x_hu[hu_i]
    if a <= abs(b):
        sei_i += 1
        sei_nax = a
    else:
        hu_i += 1
        hu_nax = b
print(min(sei_nax, abs(hu_nax))*2+max(sei_nax, abs(hu_nax)))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = []
num[0] = int(num[0])
num[1] = int(num[1])
for i in zahyou:
    zahyou_int.append(int(i))

rousoku = []
time = 4 * 10 ** 8
for i in range(num[0]-num[1]+1):
    if(i == 0 or (zahyou_int[i] <= 0 and zahyou_int[(i+num[1])] >= 0)):
        rousoku = zahyou_int[i:(i+num[1])]
        # print(rousoku)
        time_p = -min(0, min(rousoku)) * 2 + max(rousoku)
        time_m = max(0, max(rousoku)) * 2 - min(rousoku)
        if(min(time_p, time_m) < time):
            time = min(time_p, time_m)

print(time)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = ((N - 1) + (K - 1 - 1)) // (K - 1)
print(ans)

Submission status: Runtime Error

Solution code:
num = input().split(" ")
zahyou = input().split(" ")

num[1] = int(num[1])
plus = []
minus = []
rousoku = []
for i in range(int(num[0])):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))

# プラス方向で終了する場合

if(num[1] > len(plus)):
    plus_tmp = minus[(len(plus)-num[1]):] + plus
    minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
rousoku = plus_tmp[0:num[1]]
minus_tmp_2 = []
for i in minus_tmp:
    minus_tmp_2.append(i)
while(True):
    if(rousoku[-1] - rousoku[-2] >= (min(min(rousoku), 0) - minus_tmp_2[-1]) * 2):
        rousoku.pop(-1)
        rousoku.insert(0, minus_tmp_2.pop(-1))
    else:
        break
rousoku_move_plus = []
for i in rousoku:
    rousoku_move_plus.append(i - min(rousoku) * 2)

# マイナス方向で終了する場合
if(num[1] > len(minus)):
    minus_tmp = minus + plus[:(num[1]-len(minus))]
    plus_tmp = plus[(num[1]-len(minus)):]
rousoku = minus_tmp[(-(num[1]+1)):]
plus_tmp_2 = []
for i in plus_tmp:
    plus_tmp_2.append(i)
while(True):
    if(rousoku[1] - rousoku[0] >= (plus_tmp_2[0] - max(max(rousoku), 0)) * 2):
        rousoku.pop(0)
        rousoku.append(plus_tmp_2.pop(0))
    else:
        break
rousoku_move_minus = []
for i in rousoku:
    rousoku_move_minus.append(max(rousoku) * 2 - i)

print(min(max(rousoku_move_plus), max(rousoku_move_minus)))

Submission status: Runtime Error

Solution code:
import copy
num = input().split(" ")
zahyou = input().split(" ")

num[1] = int(num[1])
plus = []
minus = []
rousoku = []
for i in range(int(num[0])):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))

# プラス方向で終了する場合

if(num[1] > len(plus)):
    plus_tmp = minus[(len(plus)-num[1]):] + plus
    minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
rousoku = plus_tmp[0:num[1]]
minus_tmp_2 = copy.deepcopy(minus_tmp)
while(True):
    if(rousoku[-1] - rousoku[-2] >= (min(min(rousoku), 0) - minus_tmp_2[-1]) * 2):
        rousoku.pop(-1)
        rousoku.insert(0, minus_tmp_2.pop(-1))
    else:
        break
rousoku_move_plus = []
for i in rousoku:
    rousoku_move_plus.append(i - min(rousoku) * 2)

# マイナス方向で終了する場合
if(num[1] > len(minus)):
    minus_tmp = minus + plus[:(num[1]-len(minus))]
    plus_tmp = plus[(num[1]-len(minus)):]
rousoku = minus_tmp[(-(num[1]+1)):]
plus_tmp_2 = copy.deepcopy(plus_tmp)
while(True):
    if(rousoku[1] - rousoku[0] >= (plus_tmp_2[0] - max(max(rousoku), 0)) * 2):
        rousoku.pop(0)
        rousoku.append(plus_tmp_2.pop(0))
    else:
        break
rousoku_move_minus = []
for i in rousoku:
    rousoku_move_minus.append(max(rousoku) * 2 - i)

print(min(max(rousoku_move_plus), max(rousoku_move_minus)))

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
x.append(0)
x.sort()
ans = 100
for i in range(n-k+1):
    cnt = 0
    cnt += abs(x[i])
    for j in range(i,k+i):
        cnt += abs(x[j+1] - x[j])    
    if cnt < ans:
        ans = cnt

print(min(ans))

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
x.append(0)
x.sort()
ans = 100000000
for i in range(n-k+1):
    cnt = 0
    cnt += abs(x[i])
    for j in range(i,k+i):
        cnt += abs(x[j+1] - x[j])    
    if cnt < ans:
        ans = cnt

print(min(ans))

Submission status: Runtime Error

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
x.append(0)
x.sort()
ans = 10 ** 18
for i in range(n-k+1):
    cnt = 0
    cnt += abs(x[i])
    for j in range(i,k+i):
        cnt += abs(x[j+1] - x[j])    
    if cnt < ans:
        ans = cnt

print(min(ans))

Submission status: Runtime Error

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

    for i,x in enumerate(X):
        if max(X) < 0:
            array_m = X.copy()
            array_p = []
            break
        if x >= 0:
            array_m = X[:i]
            array_p = X[i:]
            break
    inf = 10**9
    array_m = [inf] + array_m
    array_p += [inf]
    array_m2 = [abs(2*x) for x in array_m]
    array_p2 = [abs(2*x) for x in array_p]
    count = 0
    array_m_tmp = []
    array_p_tmp = []
    for _ in range(k):
        if array_m2[-1] < array_p[0]:
            array_m_tmp.append(array_m2.pop(-1))
        else:
            array_p_tmp.append(array_p.pop(0))
    array_m_tmp.append(0)
    array_p_tmp.append(0)
    ans = max(array_m_tmp) + max(array_p_tmp)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

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

iz=bisect_left(x,0)
if x[iz+1]==0:
    k=k-1
if x[n-1]<=0 :
    t=-x[-k]
    print(t)
elif x[0]>=0:
    t=x[k-1]
    print(t)

else:
    tmin=10**9
    for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
        kt=i
        t1=-x[kt]+x[kt+(k-1)]*2
        t2=-x[kt]*2+x[kt+(k-1)]
        if t1<tmin:
            tmin=t1
        if t2<tmin:
            tmin=t2
    print(tmin)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

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

iz=bisect_left(x,0)
 f x[iz+1]==0:
    k=k-1
if x[n-1]<=0 :
    t=-x[-k]
    print(t)
elif x[0]>=0:
    t=x[k-1]
    print(t)

else:
    tmin=10**9
    for i in range(max(0,iz-(k-1)+1),min(iz,n-1-(k-1)+1)):
        kt=i
        t1=-x[kt]+x[kt+(k-1)]*2
        t2=-x[kt]*2+x[kt+(k-1)]
        if t1<tmin:
            tmin=t1
        if t2<tmin:
            tmin=t2
    print(tmin)

Submission status: Runtime Error

Solution code:
from bisect import bisect_right
from bisect import bisect_left

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

if x[n-1]<0 :
    t=x[-k]
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(t)
elif x[0]==0:
    t=x[k-1-1]
    print(t)

else:
    iz=bisect_left(x,0)
    if x[iz+1]==0:
        for i in range(iz+2,min(iz+k,k)):
            kt=i
            t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
            if t<tmin:
                tmin=t
        for i in range(max(iz+2-k,0),iz):
            kt=i
            t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
            if t<tmin:
                tmin=t
    else:
        tmin=10**9
        for i in range(iz+1,min(iz+k,k)):
            kt=i
            t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
            if t<tmin:
                tmin=t
        for i in range(max(iz+2-k,0),iz):
            kt=i
            t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
            if t<tmin:
                tmin=t
    print(tmin)

Submission status: Runtime Error

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

if x_0 == 0:
    print(x[k])
elif x_0==n:
    print(-x[-k-1])
else:
    r = min(n-x_0-1, k)
    l = min(x_0, k)
    ans1=float("inf")
    ans2=float("inf")
    #スタートで正に移動
    for i in range(1,r):
        s = 2*x[x_0 + i] - x[x_0 - (k-i)]
        ans1=min(ans1, s)
    for i in range(1,l):
        s = -2*x[x_0 - i] + x[x_0 + (k-i)]
        ans2=min(ans2, s)
    print(min(ans1, ans2))

Submission status: Runtime Error

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

if x_0 == 0:
    print(x[k])
elif x_0==n:
    print(x[-k])
else:
    r = min(n-x_0-1, k)
    l = min(x_0, k)
    ans1=float("inf")
    ans2=float("inf")
    #スタートで正に移動
    for i in range(1,r):
        s = 2*x[x_0 + i] - x[x_0 - (k-i)]
        ans1=min(ans1, s)
    for i in range(1,l):
        s = -2*x[x_0 - i] + x[x_0 + (k-i)]
        ans2=min(ans2, s)
    print(min(ans1, ans2))

Submission status: Runtime Error

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

if x[n-1]<0 :
    t=x[-k]
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(t)
elif x[0]==0:
    t=x[k-1]
    print(t)

else:
    for i in range(n-1):
        if x[i]<0 and x[i+1]>=0 :
            i0=i+1
            break
#    print(i0)
    tmin=10**9
    for i in range(i0,i0+k-1):
        kt=i
        t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
        if t<tmin:
            tmin=t
    for i in range(i0+1-k,i0):
        kt=i
        t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
        if t<tmin:
            tmin=t
    print(tmin)

Submission status: Runtime Error

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

if x[n-1]<0 :
    t=x[-k]
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(t)
elif x[0]==0:
    t=x[k-1]
    print(t)

else:
    for i in range(n-1):
        if x[i]<0 and x[i+1]>=0 :
            i0=i+1
            break
#    print(i0)
    tmin=10**9
    for i in range(i0,k):
        kt=i
        t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
        if t<tmin:
            tmin=t
    for i in range(i0-1,i0+1-k-1,-1):
        kt=i
        t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
        if t<tmin:
            tmin=t
    print(tmin)

Submission status: Runtime Error

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

if x[n-1]<0 :
    t=x[-k]
    print(t)
elif x[n-1]==0 :
    t=x[-k+1]
    print(t)

elif x[0]>0:
    t=x[k-1]
    print(t)
elif x[0]==0:
    t=x[k-1]
    print(t)

else:
    for i in range(n):
        if x[i]<0 and x[i+1]>=0 :
            i0=i+1
            break
#    print(i0)
    tmin=10**9
    for i in range(i0,k):
        kt=i
        t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
        if t<tmin:
            tmin=t
    for i in range(i0-1,i0+1-k-1,-1):
        kt=i
        t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
        if t<tmin:
            tmin=t
    print(tmin)

Submission status: Runtime Error

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

if x[n-1]<=0 :
    t=x[-k+1]
    print(t)

elif x[0]>=0:
    t=x[k-1]
    print(t)

else:
    for i in range(n):
        if x[i]<0 and x[i+1]>=0 :
            i0=i+1
            break
    print(i0)
    tmin=10**9
    for i in range(i0,k):
        kt=i
        t=x[kt]*2-x[kt-k+1]
        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
        if t<tmin:
            tmin=t
    for i in range(i0-1,i0+1-k-1,-1):
        kt=i
        t=-x[kt]*2+x[kt+k-1]
        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
        if t<tmin:
            tmin=t
    print(tmin)

Submission status: Runtime Error

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

if x[n-1]<=0 :
    t=x[-k]
    print(t)

elif x[0]>=0:
    t=x[k]
    print(t)

else:
    for i in range(n):
        if x[i]<0 and x[i+1]>=0 :
            i0=i+1
            break
#    print(i0)
    tmin=10**9
    for i in range(i0,k):
        kt=i
        t=x[kt]*2-x[kt-k+1]
#        print("1",kt,kt-k+1,t,x[kt],x[kt-k+1])
        if t<tmin:
            tmin=t
    for i in range(i0-1,i0+1-k-1,-1):
        kt=i
        t=-x[kt]*2+x[kt+k-1]
#        print("2",kt,kt+k-1,t,x[kt],x[kt+k-1])
        if t<tmin:
            tmin=t
    print(tmin)

Submission status: Runtime Error

Solution code:
def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    s = N * (10 ** 8)
    for i in range(N-K):
        if x[i] * x[K - 1 + i] < 0:
            s = min(s, min(x[K - 1 + i], abs(x[i])) * 2 + max(x[K - 1 + i], abs(x[i])))
        else:
            s = min(s, max(abs(x[K - 1 + i], x[i])))
    print(s)
main()

Submission status: Runtime Error

Solution code:
N,K=[int(i) for i in input().split()]
X=[int(i) for i in input().split()]
m=10**5
for i in range(N-K+1):
    m=min(m,x[K-1+i]-x[i]+min(abs(x[K-1+i]),abs(x[i])))

print(m)

Submission status: Runtime Error

Solution code:
N,K=[int(i) for i in input().split()]
X=[int(i) for i in input().split()]
m=10**5
for i in range(n-k+1):
    m=min(m,x[k-1+i]-x[i]+min(abs(x[k-1+i]),abs(x[i])))

print(m)

Submission status: Runtime Error

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

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

xm=[]
xp=[]
x0=[]
ans=10**5+1

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if K==１ and x0!=[]:
    ans=0

print(ans)

Submission status: Runtime Error

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

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

xm=[]
xp=[]
x0=[]
ans=10**5+1

for i in range(N):
    xi=x[i]
    if xi<0:
        xm.append(xi)
    elif xi==0:
        x0.append(xi)
    else:
        xp.append(xi)

xm.sort(reverse=True)

if x0==[]:
    ajst=0
else:
    ajst=1

K=K-ajst

for i in range(min(len(xp),K)):
    if i<K-1:
        if len(xm)>=K-(i+1):
            ans=min(ans,2*xp[i]-xm[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,xp[i])

for i in range(min(len(xm),K)):
    if i<K-1:
        if len(xp)-1>=K-(i+1):
            ans=min(ans,-2*xm[i]+xp[K-ajst-(i+1)-1])
    
    else:#i==K-1
        ans=min(ans,-xm[i])

if K==１ and x0!=0:
    ans=0

print(ans)

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

cnt = 10000000

if len(P) == len(M) == 1:
    cnt = 0
elif len(P) == 1:
    cnt = M[K]
elif len(M) == 1:
    cnt = P[K]
else:
    #正の部分、負の部分の長さが0の場合エラーが起こる
    #正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
    for i in range(1,K):
        cnt =min(cnt,P[i]*2 + M[K-i])
        cnt =min(cnt,M[i]*2 + P[K-i])
        #print(cnt)

print(cnt)

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

cnt = 10000000



#正の部分、負の部分の長さが0の場合エラーが起こる
#正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
for i in range(1,K):
    cnt =min(cnt,P[i]*2 + M[K-i])
    cnt =min(cnt,M[i]*2 + P[K-i])
    #print(cnt)

print(cnt)

Submission status: Runtime Error

Solution code:

N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

cnt = 10000000
#正の部分、負の部分の長さが0の場合エラーが起こる
#正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
for i in range(1,K):
    cnt =min(cnt,P[i]*2 + M[K-i])
    #print(cnt)
#負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
for i in range(1,K):
    cnt =min(cnt,M[i]*2 + P[K-i])
    #print(cnt)

print(cnt)

Submission status: Runtime Error

Solution code:
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
P = [0] + [abs(i) for i in A if i> 0 ]
M = [abs(i) for i in A if i< 0 ] +[0]
M = sorted(M,reverse =False)

PP = []
for i in range(len(P)-1):
    PP.append(P[i+1] - P[i])

MM = []
for i in range(len(M)-1):
    MM.append(M[i+1] - M[i])

cnt = 10000000
#正の部分、負の部分の長さが0の場合エラーが起こる
#正の方向でi個ろうそくを通って、負の方向にK-i個ろうそくを通る
for i in range(1,K):
    cnt =min(cnt,P[i]*2 + sum(MM[0:K-i]))
    #print(cnt)
#負の方向でi個ろうそくを通って、正の方向にK-i個ろうそくを通る
for i in range(1,K):
    cnt =min(cnt,M[i]*2 + sum(PP[0:K-i]))
    #print(cnt)

print(cnt)

Submission status: Runtime Error

Solution code:
import bisect

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

init_pos = bisect.bisect_left(X, 0)
ans = 10**8

for pos_i in [init_pos-1, init_pos]:
  temp_ans = abs(X[pos_i])
  left_case = 10**8
  right_case = 10**8
  if pos_i-k+1>=0:
    left_case = abs(X[pos_i-k+1]-X[pos_i])
  if pos_i+k-1<=n-1:
    right_case = abs(X[pos_i+k-1]-X[pos_i])
  temp_ans += min(left_case, right_case)
  
  ans = min(ans, temp_ans)

print(ans)

Submission status: Runtime Error

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

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


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


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

ans = 10**16

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


print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=[]
C=[]
for i in A:
  if i<=0:    
    B.append(i)
  else:
    C.append(i)

b=len(B)
c=len(C)
a=min(b,c,K)

l=10**9
for i in range(a):
  la=C[i]+B[i+1-K]*(-2)
  lb=C[i]*2-B[i+1-K]
  l=min(l,la,lb)

BB=10**9
CC=10**9
if b>=K:
  BB=(B[-K])*(-1)
if c>=K:
  CC=(C[K-1])
print(min(BB,CC,l))

Submission status: Runtime Error

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

count_0 = X.count(0)

if k <= count_0:
    print(0)
    exit()

k -= count_0

neg = [x for x in X if x < 0]
pos = [x for x in X if x > 0]

neg_count = len(neg)
pos_count = len(pos)

if not neg:
    print(pos[k-1])
    exit()

if not pos:
    print(neg[-k])
    exit()

res = []
for left in range(1, min(neg_count, k)):
    time1 = neg[-left]
    right = k - left - 1
    if right >= pos_count:
        continue
    elif right < 0:
        res.append(abs(time1))
    else:
        time2 = pos[right]
        res.append(time2-time1*2)

for right in range(min(pos_count, k)):
    time1 = pos[right]
    left = k - right - 1
    if left >= neg_count:
        continue
    elif left <= 0:
        res.append(abs(time1))
    else:
        time2 = neg[-left]
        res.append(time1*2-time2)

print(min(res))

Submission status: Runtime Error

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

count_0 = X.count(0)

if k <= count_0:
    print(0)
    exit()

k -= count_0

neg = [x for x in X if x < 0]
pos = [x for x in X if x > 0]

neg_count = len(neg)
pos_count = len(pos)

if not neg:
    print(pos[k-1])
    exit()
if not pos:
  	print(neg[-k])
    exit()

res = []
for left in range(1, min(neg_count, k)):
    time1 = neg[-left]
    right = k - left - 1
    if right >= pos_count:
        continue
    elif right < 0:
        res.append(abs(time1))
    else:
        time2 = pos[right]
        res.append(time2-time1*2)

for right in range(min(pos_count, k)):
    time1 = pos[right]
    left = k - right - 1
    if left >= neg_count:
        continue
    elif left <= 0:
        res.append(abs(time1))
    else:
        time2 = neg[-left]
        res.append(time1*2-time2)


print(min(res))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if 0 in X:
    K-=1
left=[i for i in X if i<0]
right=[i for i in X if i>0]
lst=set()
for l,L in enumerate(left):
    i=K-(l+1)
    if i>=0:
        if i>=len(right):
            continue
        R=right[i]
        lst.add(R-L*2)
    else:
        lst.add(-L)

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

print(min(list(lst)))

Submission status: Runtime Error

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
  Y = X[i:i+K]
  if Y[0] > 0:
    answer.append(Y[-1])
  elif Y[-1] < 0:
    answer.append(-Y[0])
  else:
    answer.append(min(Y[0]*(-2)+Y[-1], Y[-1]*2-Y[0]))

print(min(answer)

Submission status: Runtime Error

Solution code:
import bisect

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

l = []
r = []
for x in X:
  if x<0:
    l.append(-x)
  else:
    r.append(x)
l = l[::-1]


sl = 2*10**8
sr = 2*10**8
sa = 2*10**8

if len(l)>=K:
  sl = l[K-1]
if len(r)>=K:
  sr = r[K-1]

if len(l)!=0 and len(r)!=0:
  il = 0
  ir = 0
  while not(il+ir+2>=K):
    if il>=len(l)-1:
      ir += 1
    elif ir>=len(r)-1:
      il += 1
    else:
      if l[il+1]<l[ir+1]:
        il+=1
      else:
        ir+=1
  sa = l[il] + r[ir] +min(l[il],r[ir])

print(min([sl, sr, sa]))

Submission status: Runtime Error

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

DIS = []

for i in range(n-k+1):
    A = X[i:i+k]
    B = []
    C = []
    for i in A:
        if i<0:
            B.append(abs(i))
        else:
            C.append(i)
    if len(B) != 0:
        max_b = max(B)
    else:
        max_b = 0
    max_c = max(C)
    distance = max_b + max_c + min(max_b, max_c)
    DIS.append(distance)
print(min(DIS))

Submission status: Runtime Error

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(x)
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: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0+1:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k==0:
    print(0)
    exit()
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k==0:
    print(0)
    exit()
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k-1<len(a):
    ans=min(ans,a[k-1])
if k-1<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
from bisect import bisect_left as bl

x0=bl(x,0)
if x[x0]==0:
    k-=1
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
else:
    a=x[:x0]
    a=a[::-1]
    a=list(map(lambda x:-x,a))
    b=x[x0:]
#print(a,b)
ans=float("INF")
if k<len(a):
    ans=min(ans,a[k-1])
if k<len(b):
    ans=min(ans,b[k-1])
for i in range(k):
    j=k-i-2
    if i<len(a) and 0<=j and j<len(b):
        ans2=2*a[i]+b[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
for i in range(k):
    j=k-i-2
    if i<len(b) and 0<=j and j<len(a):
        ans2=b[i]+2*a[j]
        ans=min(ans,ans2)
        #print(i,j,ans)
print(ans)

Submission status: Runtime Error

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

    # print(X[left:right])

    # print(X[left:right], X[left], X[left-1])

    if right-1 < N and X[right-1] < 0 and X[right] < 0:
        continue

    if left-1 >= 0 and X[left] > 0 and X[left-1] > 0:
        break

    # print(X[left:right])
    minus = 0
    plus = 0
    for x in X[left:right]:
        if x < 0:
            minus = min(minus, x)
        elif x > 0:
            plus = max(plus, x)

    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)

print(ans)

Submission status: Runtime Error

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

start = bisect.bisect_left(x,0)
inf = 10**15
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: Runtime Error

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

start = bisect.bisect_left(x,0)

if x[start] == 0:
    k -= 1
    n -= 1
    #x = x[:start] + [0] + x[start+1:]
else:
    x = x[:start] + [0] + x[start:]
#print(x)
ans = 10**15

for i in range(start,start + k - n ,-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: Runtime Error

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

start = bisect.bisect_left(x,0)

if x[start] == 0:
    k -= 1
    n -= 1
    #x = x[:start] + [0] + x[start+1:]
else:
    x = x[:start] + [0] + x[start:]
#print(x)
ans = 10**15

for i in range(start,start + k - n - 1 ,-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: Runtime Error

Solution code:
gsfd

Submission status: Runtime Error

Solution code:
import sys

N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = abs(x[0])
if N == 1:
    print(abs(x[0]))
    sys.exit()
for i in range(N - 1):
    if x[i] < 0 and x[i + 1] >= 0:
        c = i
        break
    elif i == N - 2 and x[0] >= 0:
        for l in range(K - 1):
            ans += (x[i + 1] - x[i])
        print(ans)
        sys.exit()
    elif i == N - 2 and x[0] < 0:
        for l in range(K - 1):
            ans += abs(x[i + 1] - x[i])
        print(ans)
        sys.exit()
ans = abs(x[c])
for i in range(c - K + 1, c):
    ans += abs(x[i] - x[i + 1])
ANS = ans
for i in range(K - 1):
    ans -= abs(x[c - K + 1 + i] - x[c - K + 2 + i])
    ans += abs(x[c + i] - x[c + i + 1])
    if ANS > ans:
        ANS = ans
print(ANS)

Submission status: Runtime Error

Solution code:
def dist(a, b):
  if a * b < 0:
    return 2 * min(abs(a), abs(b)) + max(abs(a), abs(b))
  else:
    return max(abs(a), abs(b))

def solve(A, K):
  ans = dist(A[0], A[K])
  for i in range(len(A)-K+1):
    ans = min(ans, dist(A[i], A[i+K-1]))
  return ans
               
N, K, *A = map(int, open(0).read().split())
print(solve(A, K))

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(xn[K-1])
if len(xp) >= K:
    ans.append(xp[K-1])

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-1] + abs(xn[-1]) + min(xp[-1],abs(xn[-1])))
print(min(ans))