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:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())


#n = ip()                              #入力整数1つ
x,y= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
s = []
if x == 1:
    print(a[0])
    exit(0)
for i in range(x-y):
    c = a[i]
    d = a[i+y-1]

    if c*d < 0:
        s.append(d-c + min(abs(c),abs(d)))
    elif  d < 0:
        s.append(abs(c))
    else: s.append(d)
print(min(s))

Submission status: Runtime Error

Solution code:
N, K   = map(int, input().split())
L = list(map(int, input().split()))
if L[-1] < 0:
    n = -1
elif L[0] >0:
    n = 0
for i in range(len(L)-1):
    if L[i] <= 0 and L[i+1] >= 0:
        n = i+1
L.insert(n,0)


dis = []
for i in range(N-K+1):
    if 0 in L[i:i+K+1]:
        l = L[i:i+K+1]
        d = abs(l[0]) + abs(l[-1])*2
        dis.append(d)
        d = abs(l[0])*2 + abs(l[-1])
        dis.append(d)
print(min(dis))

Submission status: Runtime Error

Solution code:
N, K   = map(int, input().split())
L = list(map(int, input().split()))
L.append(0)
L.=sorted(L)
dis = []
for i in range(N-K+1):
    if 0 in L[i:i+K+1]:
        l = L[i:i+K+1]
              
        d = abs(l[0]) + abs(l[-1])*2
        dis.append(d)
        d = abs(l[0])*2 + abs(l[-1])
        dis.append(d)
print(min(dis))

Submission status: Runtime Error

Solution code:
N, K   = map(int, input().split())
L = list(map(int, input().split()))
L.append(0)
L.sort()
dis = []
for i in range(N-K+1):
    if 0 in L[i:i+K+1]:
        l = L[i:i+K+1]
        print(l)
        d = abs(l[0]) + abs(l[-1])*2
        dis.append(d)
        d = abs(l[0])*2 + abs(l[-1])
        dis.append(d)
print(min(dis))

Submission status: Runtime Error

Solution code:
N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()
dis.append(Lright[K-1])
dis.append(Lleft[K-1])


for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    d += Lleft[remain-1]
    dis.append(d)
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    d += Lright[remain-1]
    dis.append(d)
print(min(dis))

Submission status: Runtime Error

Solution code:
N, K  = map(int,input().split())
L =list(map(int,input().split()))
Lzero = list(filter(lambda x:x==0,L))
Lright = list(filter(lambda x:x>0,L))
Lleft = list(map(lambda x:abs(x),filter(lambda x:x<0,L)))[::-1]
K -= len(Lzero)
if K <= 0:
    print(0)
    quit()

dis = []
if len(Lright) == 0:
    print(Lleft[K-1])
    quit()
if len(Lleft) == 0:
    print(Lright[K-1])
    quit()



#turn at right
for i in range(len(Lright)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lright[i])
        break
    d = 2*Lright[i]
    remain -= (i+1)
    d += Lleft[remain-1]
    dis.append(d)
for i in range(len(Lleft)):
    remain = K
    if i+1 >K:
        break
    elif i+1 == K:
        dis.append(Lleft[i])
        break
    d = 2*Lleft[i]
    remain -= (i+1)
    d += Lright[remain-1]
    dis.append(d)
print(min(dis))

Submission status: Runtime Error

Solution code:
def main():
    n, k = map(int, input().split())
    x = [int(i) for i in input().split()]

    n = []
    p = []
    ans = []
    for xi in x:
        if xi < 0:
            n.append(xi)
        else:
            p.append(xi)

    # print(n,p)

    # i: nから選ぶ個数
    # k-i: pから選ぶ個数
    for i in range(k):
        if i > len(n):
            continue
        if k-i > len(p):
            continue

        if i != 0:
          nd = -n[-i]
          pd = p[k-i-1]
          ans.append(nd + pd + min(nd, pd))
        else:
          ans.append(p[k-1])

    # print(ans)
    print(min(ans))







if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

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

#array = np.array(lst)
#po_array = array[array>=0]
#ne_array = abs(array[array<0])
#ne_array.sort()

po_array = sorted([x for x in lst if x >= 0])
ne_array = sorted([abs(x) for x in lst if x < 0])

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        else:
            dist = min(short_array[i]*2 + long_array[k-i-2], short_array[i] + long_array[k-i-2]*2)
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

if len(lst) == 1 and lst[0] == 0:
    print(0)
    exit()

array = np.array(lst)
po_array = array[array>=0]
ne_array = abs(array[array<0])
ne_array.sort()

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        else:
            dist = min(short_array[i]*2 + long_array[k-i-2], short_array[i] + long_array[k-i-2]*2)
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = abs(array[array<0])
ne_array.sort()

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        else:
            dist = short_array[i]*2 + long_array[k-i-2]
            if dist > short_array[i] + long_array[k-i-2]*2:
                dist = short_array[i] + long_array[k-i-2]*2
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = abs(array[array<0])
ne_array.sort()

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        elif k-i-2 >= 0:
            dist = short_array[i]*2 + long_array[k-i-2]
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = abs(array[array<0])
ne_array.sort()

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        elif k-i-2 >= 0
            dist = short_array[i]*2 + long_array[k-i-2]
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = abs(array[array<0])
ne_array.sort()

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        else:
            dist = short_array[i]*2 + long_array[k-i-2]
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = np.sort(abs(array[array<0]))

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
        
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        else:
            dist = short_array[i]*2 + long_array[k-i-2]
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = np.sort(abs(array[array<0]))

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
    
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        if i == k-1:
            dist = short_array[i]
        else:
            dist = short_array[i]*2 + long_array[k-i-2]
        if min_dist > dist:
            min_dist = dist
    if len(long_array) >= k:
        if min_dist > long_array[k-1]:
            min_dist = long_array[k-1]
    print(min_dist)

Submission status: Runtime Error

Solution code:
import numpy as np

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

array = np.array(lst)
po_array = array[array>=0]
ne_array = np.sort(abs(array[array<=0]))

if len(po_array) >= len(ne_array):
    long_array = po_array
    short_array = ne_array
else:
    long_array = ne_array
    short_array = po_array

if len(short_array) > k:
    min_len = k
else:
    min_len = len(short_array)
    
if min_len == 0:
    print(long_array[k-1])
else:
    min_dist = 10**10
    for i in range(min_len):
        dist = short_array[i]*2 + long_array[k-i-2]
        if min_dist > dist:
            min_dist = dist
    print(min_dist)

Submission status: Runtime Error

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

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    if k-len(m) >= 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while s+i <= len(m) and z+s+i <= n:
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[k-1+s+i]), abs(x[s+i]) + abs(x[k-1+s+i])*2))
        
        if s+i == len(m):
            ans = min(ans, x[k-1+s+i])
        if z+s+i == len(m)-1:
            ans = min(ans, abs(x[s+i]))
        i += 1
    
    print(ans)
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

Submission status: Runtime Error

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

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    z = len(m)-1
    if k-len(m) >= 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while s+i <= len(m) and z+s+i <= n:
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[k-1+s+i]), abs(x[s+i]) + abs(x[k-1+s+i])*2))
        
        if s+i == len(m):
            ans = min(ans, x[k-1+s+i])
        if z+s+i == len(m)-1:
            ans = min(ans, abs(x[s+i]))
        i += 1
        
    print(ans)    
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

Submission status: Runtime Error

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

m = []
p = []
for i in range(n):
    if x[i] < 0:
        m.append(x[i])
    elif x[i] > 0:
        p.append(x[i])

if m and p:
    z = len(m)-1
    if k-len(m) > 0:
        s = 0
    else:
        s = abs(k-len(m))+1
    
    i = 0
    ans = float('inf')
    while z+s+i < n and s+i < len(m):
        ans = min(ans, min(abs(x[s+i])*2 + abs(x[k-1+s+i]), abs(x[s+i]) + abs(x[k-1+s+i])*2))
        i += 1
        
    print(ans)
elif m:
    print(abs(x[-k]))
elif p:
    print(x[k-1])
else:
    print(0)

Submission status: Runtime Error

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

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    x_arr[1000000]
    print(minus[k-1])
    
elif minus == []:
    print(plus[k-1])
    x_arr[1000000]
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  >= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  >= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] < minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] < minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Runtime Error

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

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    sys.exit()
    print(minus[k-1])
    
elif minus == []:
    sys.exit()
    print(plus[k-1])

else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  <= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  <= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] < minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] < minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Runtime Error

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

plus = [i for i in x_arr if i >= 0]
minus = [abs(i) for i in x_arr[::-1] if i < 0]


if plus == []:
    print(minus[k-1])
    x_arr[1000000]
elif minus == []:
    print(plus[k-1])
    x_arr[1000000]
else:
    plus2 = [i * 2 for i in plus]
    minus2 = [i * 2 for i in minus]
    max1 = [0, 0]
    max2 = [0, 0]
    p_only = 10**9
    m_only = 10**9
    if len(plus) >= k:
        p_only = plus[k-1]
    if 0 in plus:
        minus.insert(0, 0)
        if len(minus)  <= k:
            m_only = minus[k-1]
            minus.remove(0)
        else:
            minus.remove(0)
    else:
        if len(minus)  <= k:
            m_only = minus[k-1]
        else:
            pass

    for i in range(k):
        if plus2 == []:
            max1[1] = minus.pop(0)
        elif minus == []:
            max1[0] = plus2.pop(0)
        elif plus2[0] < minus[0]:
            max1[0] = plus2.pop(0)
        else:
            max1[1] = minus.pop(0)
    for i in range(k):
        if plus == []:
            max2[1] = minus2.pop(0)
        elif minus2 == []:
            max2[0] = plus.pop(0)
        elif plus[0] < minus2[0]:
            max2[0] = plus.pop(0)
        else:
            max2[1] = minus2.pop(0)
    
    print(min(max1[0]+max1[1],max2[0]+max2[1], p_only, m_only))

Submission status: Runtime Error

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

R=[0]
L=[0]
for i in range(n):
    if x[i]>=0:
        R.append(x[i])
    else:
        L.append(-x[i])

R=sorted(R)
L=sorted(L)


lenR=len(R)
lenL=len(L)

if lenR>k:
    ans=R[k]
    for i in range(k):
        ans=min(ans,R[k-i]+2*L[i], 2*R[k-i]+L[i])
        #print('i,ans,R[k-i]+2*L[i], 2*R[k-i]+L[i]=',i,ans,R[k-i]+2*L[i], 2*R[k-i]+L[i])
else:
    ans=R[lenR-1]+L[k-lenR-1]
    for i in range(lenR):
        if k-i<lenL:
            ans=min(ans,R[i]+2*L[k-i], 2*R[i]+L[k-i])

print(ans)

Submission status: Runtime Error

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

kyori=2*10**8+1
for i in range(n-k+1):
#print(x[i:k+i])
  if x[k+i-1]<=0:
    kyori = min(kyori,abs(x[i]))
  elif x[i]<=0 and 0<=x[k+1-1]:
    kyori = min(kyori,abs(x[i]*2)+abs(x[k+i-1]),abs(x[i])+abs(x[k+i-1]*2))
  elif 0<=x[i]:
    kyori = min(kyori,abs(x[k+i-1]))
print(kyori)

Submission status: Runtime Error

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


def check(now, k, t, logs, ans):
    if now in logs:
        return ans
    logs.append(now)
    if k == 0:
        return min(ans, t)
    if len(x) > now + 1:
        ans = min(ans, check(now + 1, k - 1, t +
                             abs(x[now + 1] - x[now]), logs[::], ans))
    if now - 1 >= 0:
        ans = min(ans, check(now - 1, k - 1, t +
                             abs(x[now - 1] - x[now]), logs[::], ans))
    return ans


def main():
    ans = abs(sum(x))
    for i, v in enumerate(x):
        ans = min(ans, check(i, k - 1, abs(v), [], ans))
    print(ans)


main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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


def check(now, k, t, logs, ans):
    if now in logs:
        return ans
    logs.append(now)
    if k == 0:
        return min(ans, t)

    if len(x) > now + 1:
        ans = min(ans, check(now + 1, k - 1, t +
                             abs(x[now + 1] - x[now]), logs[::], ans))
    if now - 1 >= 0:
        ans = min(ans, check(now - 1, k - 1, t +
                             abs(x[now - 1] - x[now]), logs[::], ans))
    return ans


def main():
    ans = abs(sum(x))
    for i, v in enumerate(x):
        ans = min(ans, check(i, k - 1, abs(v), [], ans))
    print(ans)


main()

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
Lplus = list(filter(lambda x: x>=0, L))
Lminus = list(filter(lambda x: x<=0 ,L))[::-1]
ans_table = []
#print(L)
#right_only
if len(Lplus) >= K:
    ans_table.append(Lplus[K-1])
#left_only
if len(Lminus) >= K:
    ans_table.append(Lminus[K-1])
#once_turn_right_to_left
for i in range(len(Lplus)):
    if i >= K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if remain <= len(Lminus):
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)
#once_turn_left_to_right
Lplus,Lminus = Lminus,Lplus
for i in range(len(Lplus)):
    if i >= K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if remain <= len(Lminus):
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)

print(min(ans_table))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
Lplus = list(filter(lambda x: x>=0, L))
Lminus = list(filter(lambda x: x<0 ,L))[::-1]
ans_table = []
#print(L)
#right_only
if len(Lplus) >= K:
    ans_table.append(Lplus[K-1])
#left_only
if len(Lminus) >= K:
    ans_table.append(Lminus[K-1])
#once_turn_right_to_left
for i in range(len(Lplus)-1):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if remain <= len(Lminus):
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)
#once_turn_left_to_right
Lplus,Lminus = Lminus,Lplus
for i in range(len(Lplus)-1):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if K - i <= len(Lminus):
 #       print(Lminus[K-i-1],Lplus[i-1],i)
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)

#print(ans_table)
print(min(ans_table))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
Lplus = list(filter(lambda x: x>=0, L))
Lminus = list(filter(lambda x: x<0 ,L))[::-1]
ans_table = []
#print(L)
#right_only
if len(Lplus) >= K:
    ans_table.append(Lplus[K-1])
#left_only
if len(Lminus) >= K:
    ans_table.append(Lminus[K-1])
#once_turn_right_to_left
for i in range(len(Lplus)):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if K - i < len(Lminus):
  #      print(Lminus[K-i-1],Lplus[i-1],i)
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)
#once_turn_left_to_right
Lplus,Lminus = Lminus,Lplus
for i in range(len(Lplus)):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if K - i <= len(Lminus):
 #       print(Lminus[K-i-1],Lplus[i-1],i)
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)

#print(ans_table)
print(min(ans_table))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
Lplus = list(filter(lambda x: x>=0, L))
Lminus = list(filter(lambda x: x<0 ,L))[::-1]
ans_table = []
#print(L)
#right_only
if len(Lplus) >= K:
    ans_table.append(Lplus[K-1])
#left_only
if len(Lminus) >= K:
    ans_table.append(Lminus[K-1])
#once_turn_right_to_left
for i in range(len(Lplus)):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if K - i <= len(Lminus):
  #      print(Lminus[K-i-1],Lplus[i-1],i)
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)
#once_turn_left_to_right
Lplus,Lminus = Lminus,Lplus
for i in range(len(Lplus)):
    if i > K: break
    remain = K - i
    tmp = abs(Lplus[i-1])
    if K - i <= len(Lminus):
 #       print(Lminus[K-i-1],Lplus[i-1],i)
        tmp += abs(Lplus[i-1] - Lminus[K-i-1])
        ans_table.append(tmp)

#print(ans_table)
print(min(ans_table))

Submission status: Runtime Error

Solution code:
a,b  = ma()
x = lma()
ans = 10 **18
for i in range(a-b+1):
    a0 = x[i]
    ab = x[i+b-1]
    if ab< 0 :
        ans = min(ans,ab)
    elif a0 <=0 and 0 <= ab:
        ans = min(ans, ab-2*a0, 2*ab-a0)
    else:
        ans = min(ans,ab)
print(ans)

Submission status: Runtime Error

Solution code:
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
elif len(Xpositive) == 0:
    sub = Xnegative[K-1]
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and i-2 < len(Xpositive):
        distance = max(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

Submission status: Runtime Error

Solution code:
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
elif len(Xpositive) == 0:
    sub = Xnegative[K-1]
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and i < len(Xpositive):
        distance = max(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

Submission status: Runtime Error

Solution code:
S = list(map(int,input().split()))
N = S[0]
K = S[1]
X = list(map(int,input().split()))

Xnegative = [abs(x) for x in X if x < 0]
Xpositive = [x for x in X if x >= 0]
Xnegative = sorted(Xnegative)

i = 0

if len(Xpositive) >= K:
    sub = Xpositive[K-1]
    while i < len(Xnegative) and K-i-2 >= 0:
        distance = min(Xnegative[i],Xpositive[K-2-i]) + Xnegative[i] + Xpositive[K-2-i]
        if sub > distance:
            sub = distance
        i = i + 1
else:
    l = K-len(Xpositive)
    sub = min(Xnegative[l-1],Xpositive[-1]) + Xnegative[l-1] + Xpositive[-1]
    while i + l < len(Xnegative) and i < len(Xpositive):
        distance = max(Xnegative[i+l],Xpositive[-i-2]) + Xnegative[i+l] + Xpositive[-i-2]
        if sub > distance:
            sub = distance
        i = i + 1

print(sub)

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
Z = min(n-zero,zero,k)
cnt = (10**8)*(10**5)
for i in range(Z):
    if cnt > -2*a[zero-i]+a[zero+k-i-1]:
        cnt = -2*a[zero-i]+a[zero+k-i-1]
    else:
        break
for i in range(Z):
    if cnt > -a[zero-k+i]+2*a[zero+i-1]:
        cnt = -a[zero-k+i]+2*a[zero+i-1]
    else:
        break
print(cnt)

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
Z = min(n-zero,zero,k)
cnt = min(-sum(a[Z-k:zero]),sum(a[zero:zero+Z]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k-i-1]:
        cnt = -2*a[zero-i]+a[zero+k-i-1]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+i-1]:
        cnt = -a[zero-k+i]+2*a[zero+i-1]
    else:
        break

Submission status: Runtime Error

Solution code:
import bisect
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
zero = bisect.bisect_left(a,0)
cnt = min(-sum(a[zero-k:zero]),sum(a[zero:zero+k+1]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k-i-1]:
        cnt = -2*a[zero-i]+a[zero+k-i-1]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+k-1]:
        cnt = -a[zero-k+i]+2*a[zero+k-1]
    else:
        break
print(cnt)