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:
import numpy as np
n,k = list(map(int,input().split()))
x = list(map(int,input().split()))
n = len(x)
if 0 in x:
    x.remove(0)
    k = k - 1

if len(x) == 0:
    print(0)
else:
    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    scores = []
    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
    if neg >= k:
        if b > 0:
            scores.append(np.sum(xx[pos_ind == False][-(k-1)]))
        elif b == 0:
            scores.append(np.sum(xx[pos_ind == False][-(k)]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)
    for i in range(min([neg,k])):
        if b - i + k - 2 > len(xx):
            break
        score = - xx[b - i - 1] * 2 + xx[b - i + k - 2]
        scores.append(score)
#     print(scores)
    print(np.min(scores))

Submission status: Runtime Error

Solution code:
if len(x) == 0:
    print(0)
else:
    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    scores = []
    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
    if neg >= k:
        if b > 0:
            scores.append(np.sum(xx[pos_ind == False][-(k-1)]))
        elif b == 0:
            scores.append(np.sum(xx[pos_ind == False][-(k)]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)
    for i in range(min([neg,k])):
        if b - i + k - 2 > len(xx):
            break
        score = - xx[b - i - 1] * 2 + xx[b - i + k - 2]
        scores.append(score)
#     print(scores)
    print(np.min(scores))

Submission status: Runtime Error

Solution code:
if len(x) == 0:
    print(0)
else:
    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    scores = []
    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
    if neg >= k:
        scores.append(np.sum(xx[pos_ind == False][-(k)]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)
    for i in range(min([neg,k])):
        if b - i + k - 2 > len(xx):
            break
        score = - xx[b - i - 1] * 2 + xx[b - i + k - 2]
        scores.append(score)
#     print(scores)
    print(np.min(scores))

Submission status: Runtime Error

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

if len(x) == 0:
    print(0)
else:
    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    try:
        b = np.where(a)[0][0]
    except:
        b = 0

    scores = []
    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
    if neg >= k:
        scores.append(np.sum(xx[pos_ind == False][-(k-1)]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)
    for i in range(min([neg,k])):
        if b - i + k - 2 > len(xx):
            break
        score = - xx[b - i - 1] * 2 + xx[b - i + k - 2]
        scores.append(score)
#     print(scores)
    print(np.min(scores))

Submission status: Runtime Error

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

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


for i in range(n-k+1):
	if x[i+k-1] <= 0:
		ans = min(ans,-x[i])
	elif x[i] >= 0:
		ans = min(x[i+k-1],ans)
	else:
		ans = min(x[i+k-1]-x[i]+min(abs(x[i]),x[i+k-1]))
print(ans)

Submission status: Runtime Error

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

if 0 in x:
    x.remove(0)
    k = k - 1

if len(x) == 0:
    print(0)
else:
    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    b = np.where(a)[0][0]

    scores = []
    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
    if neg >= k:
        scores.append(np.sum(xx[pos_ind == False][-(k-1)]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)
    for i in range(min([neg,k])):
        if b - i + k - 2 > len(xx):
            break
        score = - xx[b - i - 1] * 2 + xx[b - i + k - 2]
        scores.append(score)
#     print(scores)
    print(np.min(scores))

Submission status: Runtime Error

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

if 0 in x:
    x.remove(0)
    k = k - 1

if len(x) == 0:
    print(0)
else:
    xx = np.array(x)
    a = xx >= 0
    pos = sum(a)
    neg = n - pos
    pos_ind = a
    b = np.where(a)[0][0]

    scores = []
    if pos >= k:
        scores.append(np.sum(xx[pos_ind][k-1]))
    if neg >= k:
        scores.append(np.sum(xx[pos_ind == False][-(k-1)]))
#     print(scores)
    for i in range(min([pos,k])):
        if b + i - k + 1 < 0:
            break
        score = xx[b + i] * 2 - xx[b + i - k + 1]
        scores.append(score)
#     print(scores)
    for i in range(min([neg,k])):
        if b - i + k - 2 > len(xx):
            break
        score = - xx[b - i - 1] * 2 + xx[b - i + k - 2]
        scores.append(score)
#     print(scores)
    print(np.min(scores))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8
#

import copy

            
    
def main(n, k, l):
    cost_l = []

    if k == 0:
        print(0)
        return
    
    if k == 1:
        t = [abs(l[i]) for i in range(len(l)) ]
        print(min(t))
        return
    
    for i in range(n - k):
        mae = i
        ato = i + k - 1
        if l[mae] * l[ato] < 0:
            cost_l.append(min(- l[mae] * 2 + l[ato], - l[mae] + l[ato] * 2))
        elif l[mae] >= 0:
            cost_l.append(l[ato])
        elif l[ato] <= 0:
            cost_l.append(-l[mae])
            
    print(min(cost_l))

    
    
if __name__ == '__main__':
    try:
        n, k = list(map(int,input().strip().split(" ")))
        l = list(map(int,input().strip().split(" ")))
        main(n, k, l)
    except EOFError:
        pass

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8
#

import copy

            
    
def main(n, k, l):
    cost_l = []
    if k == 1:
        t = [abs(l[i]) for i in range(len(l)) ]
        print(min(t))
        return
    
    for i in range(n - k):
        mae = i
        ato = i + k - 1
        if l[mae] * l[ato] < 0:
            cost_l.append(min(- l[mae] * 2 + l[ato], - l[mae] + l[ato] * 2))
        elif l[mae] >= 0:
            cost_l.append(l[ato])
        elif l[ato] <= 0:
            cost_l.append(-l[mae])
            
    print(min(cost_l))

    
    
if __name__ == '__main__':
    try:
        n, k = list(map(int,input().strip().split(" ")))
        l = list(map(int,input().strip().split(" ")))
        main(n, k, l)
    except EOFError:
        pass

Submission status: Runtime Error

Solution code:
a, b = map(int, input().split())
s = list(map(int,input().split( )))
c=a-b
e = 10000000000
if a==b:
  e=s[b]-s[0]+min(abs(s[b]),abs(s[0]))
else:
  for j in range(0,c+1):
    if e>s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j])):
      e=s[j+b-1]-s[j]+min(abs(s[j+b-1]),abs(s[j]))
print(e)

Submission status: Runtime Error

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

zerofrag = 0
zerobig = []
zerosmall = []

for i in a:
    if i == 0:
        zerofrag =1
    elif i > 0:
        zerobig.append(i)
    else:
        zerosmall.append(abs(i))

if k == 1 and zerofrag == 1:
    print(0)
else:
    if zerofrag == 1:
        k = k -1

    zerobig.sort()
    zerosmall.sort()
    #print(zerobig,zerosmall)
    kouho = []
    if len(zerobig) >=k:
        kouho.append(zerobig[k-1])
    if len(zerosmall) >=k:
        kouho.append(zerosmall[k-1])
        
    for ii in range(len(zerosmall)):
        if k - (ii) > len(zerobig):
            continue
        else:
            try:
                kouho.append(zerosmall[ii] * 2 + zerobig[k - ii - 2])
            except:
                continue
    for jj in range(len(zerobig)):
        if k - (jj ) > len(zerosmall):
            continue
        else:
            try:
                kouho.append(zerobig[jj] * 2 + zerosmall[k - jj - 2])
            except:
                continue
    #print(kouho)
    if zerofrag == 1:
        k = k + 1
    if n == k :
        print(min( (max(zerosmall)*2 + max(zerobig)) ,  max(zerobig) * 2 + max(zerosmall)) )
    else:
        print(min(kouho))

Submission status: Runtime Error

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

k = []
# print(X)

k0 = abs(X[0])
for n in range(0+1, K):
    k0 += X[n] - X[n-1]
# print(k0)
k.append(k0)
for i in range(1, len(X)-K+1):
    # print(i, i-1, i+K, i+K-1)
    ki = k[i-1] - (X[i] - X[i-1]) + (X[i+K-1] - X[i+K-2]) - abs(X[i-1]) + abs(X[i])
    k.append(ki)
print(min(k))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8
#

import copy

            
    
def main(n, k, l):
    cost_l = []
    if k == 1:
        print(min(l))
        return
    
    for i in range(n - k):
        mae = i
        ato = i + k - 1
        if l[mae] * l[ato] < 0:
            cost_l.append(min(- l[mae] * 2 + l[ato], - l[mae] + l[ato] * 2))
        elif l[mae] >= 0:
            cost_l.append(l[ato])
        elif l[ato] <= 0:
            cost_l.append(-l[mae])
            
    print(min(cost_l))

    
    
if __name__ == '__main__':
    try:
        n, k = list(map(int,input().strip().split(" ")))
        l = list(map(int,input().strip().split(" ")))
        main(n, k, l)
    except EOFError:
        pass

Submission status: Runtime Error

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

X_plus = [0]
X_minus = [0]
for i in range(N):
    if X[i] < 0:
        X_minus.append(X[i])
    else:
        X_plus.append(X[i])

n_minus = len(X_minus)-1
n_plus = len(X_plus)-1
res = []
for i in range(1, n_minus+1):
    if n_plus > K-i-1:
        res.append(abs(X_minus[-i])*2 + abs(X_plus[K-i])) 

for i in range(1, n_plus+1):
    if n_minus >= K-i-1:
        res.append(abs(X_plus[i])*2 + abs(X_minus[min(-(K-i), 0)])) 
if n_minus >= K:
    res.append(abs(X_minus[-K]))
if n_plus >= K:
    res.append(X_plus[K])
        
print(min(res))

Submission status: Runtime Error

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

zerofrag = 0
zerobig = []
zerosmall = []

for i in a:
    if i == 0:
        zerofrag =1
    elif i > 0:
        zerobig.append(i)
    else:
        zerosmall.append(abs(i))

if k == 1 and zerofrag == 1:
    print(0)
else:
    if zerofrag == 1:
        k = k -1

    zerobig.sort()
    zerosmall.sort()
    #print(zerobig,zerosmall)
    kouho = []
    if len(zerobig) >=k:
        kouho.append(zerobig[k-1])
    if len(zerosmall) >=k:
        kouho.append(zerosmall[k-1])
        
    for ii in range(len(zerosmall)):
        if k - (ii) > len(zerobig):
            continue
        else:
            try:
                kouho.append(zerosmall[ii] * 2 + zerobig[k - ii - 2])
            except:
                continue
    for jj in range(len(zerobig)):
        if k - (jj ) > len(zerosmall):
            continue
        else:
            try:
                kouho.append(zerobig[jj] * 2 + zerosmall[k - jj - 2])
            except:
                continue
    #print(kouho)
    print(min(kouho))

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
x=list(map(int, input().split()))
zi=0
for xi in x:
    if xi>=0:
        break
    zi+=1

xm=[0]
if x[zi]==0:
    xp=[]
else:
    xp=[0]

xm.extend(reversed(x[:zi]))
xp.extend(x[zi:])

ans=float('inf')
for i in range(k):
    if i<len(xm) and k-i<len(xp):
        ans=min(xm[i]*2*(-1) + xp[k-i], ans)
    if i<len(xp) and k-i<len(xm):
        ans=min(xp[i]*2 + xm[k-i]*(-1), ans)
if len(x)==1 and x[0]==0:
    ans=0
print(ans)

Submission status: Runtime Error

Solution code:
import bisect

N, K = map(int, input().split())
x = list(map(int, input().split()))
flags = [0] * N
index = bisect.bisect_left(x, 0)
cost = abs(x[index]-0)
flags[index] = 1
value = x[index]
for i in range(1, K):
    r = bisect.bisect_left(x, value)
    while flags[r]:
        r += 1
        if r > len(x):
            break
    l = r-1
    while flags[l]:
        if l < 0:
            break
        l -= 1
    if abs(value-x[r]) > abs(value-x[l]):
        cost += abs(value-x[l])
        flags[l] = 1
        value = x[l]
    else:
        cost += abs(value-x[r])
        flags[r] = 1
        value = x[r]
print(cost)

Submission status: Runtime Error

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

time = 10 ** 20
base = 0

for i in range(N):
    if x[i] >= 0:
        base = i
        break

if N - base > 0:
    for i in range(base, N):
        count = 1
        right = x[i] * 2 + x[base - K - count]
        left = x[i] + x[base - K - count]  * 2
        time = min(time, right, left)
else:
    time = x[N - K] * 2

print(time)

Submission status: Runtime Error

Solution code:
import bisect
n, k = map(int, input().split())
x = list(map(int, input().split()))
z = bisect.bisect_left(x, 0)
a = sorted(map(abs, x[:z]))
b = x[z:]
ans = float("inf")
if a and b:
    for i in range(min([k, len(a), len(b)])):
        ans = min(ans, min(a[i], b[k - i - 2]) * 2 + max(a[i], b[k - i - 2]))
    print(ans)
elif a:
    print(a[k - 1])
else:
    print(b[k - 1])

Submission status: Runtime Error

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

m = min(x[i] - x[i-K+1] + min(abs(x[i], abs(x[i-K+1])) for i in range(K-1, N))
print(m)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

def solve():
    N, K = map(int, input().split())
    C = np.array(list(map(int,input().split())))
    D = list()
    for i in range(N-K+1):
      c = C[i:i+K]
      l, r = max(c), min(c)
      d = -2*r + l if l > r else  -r + 2*l
      D.append(d)
    return str(min(D)

if __name__ == '__main__':
    print(solve())