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:
#!/usr/bin/env python3
import sys

def append_zero(x):
    if 0 in x:
        return
    if x[0] > 0:
        x.insert(0, 0)
        return
    if x[len(x)-1] < 0:
        x.appnd(0)
        return
    for i in range(1, len(x)):
        if x[i-1] < 0 and x[i] > 0:
            x.insert(i, 0)
            return

def solve(N, K, x):
    y = [0]
    append_zero(x)
    n = len(x)
    z = 0
    for i in range(1, n):
        y.append(y[i-1] + x[i] - x[i-1])
        if x[i] == 0:
            z = i
    # print(x)
    # print(y)
    if n == K:
        if n-1 == z or z == 0:
            return y[n-1]
        else:
            l = y[z] + y[n-1]
            r = y[n-1] - y[z] + y[n-1]
            return min(l, r)
    ans = 10e20
    for i in range(n - K):
        k = i + K
        if k <= z:
            ans = min(ans, y[z] - y[i])
        if z <= i:
            ans = min(ans, y[k] - y[z])
        if i <= z and z < k:
           l = y[z] - y[i] + y[k] - y[i]
           r = y[k] - y[z] + y[k] - y[i]
           ans = min(ans, l, r)
    return ans


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(solve(N, K, x))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
l = [*map(int, input().split())]
res = float('inf')
for i in range(n - k + 1):
    a = max(0, - l[i])
    b = max(0, l[i + k - 1])
    res = min(res, a + b + min(a, b))
print(res)

Submission status: Runtime Error

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

x = [int(a) for a in input().split()]

y = [x[idx+K-1]-elem+abs(elem) for idx,elem in enumerate(x) if idx >= K-2 and idx <= N-K]

print(min(y))

Submission status: Runtime Error

Solution code:
import bisect
import collections

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

index = bisect.bisect_left(x, 0)
if x[index] != 0:
  x.insert(index, 0)
  N += 1
  K += 1

start_min = max(index-K+1, 0)
start_max = min(index, N-K)
min_time = 10**9
for i in range(start_min, start_max+1):
  min_time = min(min_time, x[i+K-1]-x[i]+min(abs(x[i+K-1]), abs(x[i])))
print(min_time)

Submission status: Runtime Error

Solution code:
import sys
import bisect


def parse(file):
    n, k = map(int, file.readline().split())
    candles = map(int, file.readline().strip().split())

    return n, k, candles


def main(n, k, candles):
    candles = list(candles)

    i0 = bisect.bisect_left(candles, 0)

    if i0 == 0:  # 左端
        return candles[k - 1]

    i1 = i0
    i2 = i1 - 1
    
    if k == 1:
        return min(candles[i1], candles[i2])

    if candles[i0] == 0:
        k -= 1

    while True:
        if i1 - i2 + 1 == k:
            v1 = candles[i1]
            v2 = abs(candles[i2])
            return min(v1 * 2 + v2, v1 + v2 * 2)

        if i1 <= n - 2:
            j1 = i1 + 1
        else:
            j1 = i1

        if i2 >= 1:
            j2 = i2 - 1
        else:
            j2 = i2

        if candles[j1] >= - candles[j2]:
            i2 = j2
        else:
            i1 = j1


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split(' '))
X=list(map(int,input().split(' ')))
left=sorted([abs(x) for x in X if x<0])
right=[x for x in X if x>0]
ans=10**9
if 0 in X:
  K-=1
if K==0:
  ans=0
else:
  #extinguish i candles located in x<0
  for i in range(min(K+1,len(left)+1)):
    if len(right)<K-i:
      #insufficient right candles
      continue
    elif i==0:
      #no visit to left candles
      d=right[K-1]
      ans=min(ans,d)
    else:
      l = left[i-1]
      r = right[K-i-1]
      d=min(l*2+r, l+r*2)
      ans=min(ans,d)
  #in case where all candles located in x>0
  if len(left)==0:
    ans=min(right[K-1],ans)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split(' '))
X=list(map(int,input().split(' ')))
left=sorted([abs(x) for x in X if x<0])
right=[x for x in X if x>0]
ans=10**9
if 0 in X:
  K-=1
if K==0:
  ans=0
else:
  #extinguish i candles located in x<0
  for i in range(min(K+1,len(left)+1)):
    if len(right)<K-i:
      continue
    elif i==0:
      d=right[K-1]
    else:
      l = left[i-1]
      r = right[K-i-1]
      d=min(l*2+r, l+r*2)
    ans=min(ans,d)
  #in case where all candles located in x>0
  if len(left)==0:
    ans=min(right[K-1],ans)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split(' '))
X=list(map(int,input().split(' ')))
left=sorted([abs(x) for x in X if x<0])
right=[x for x in X if x>0]
haszero=0 in X
#extinguish i candles located in x<0
ans=10**9
for i in range(min(K+1,len(left)+1)):
  if len(right)<K-i-(haszero):
    continue
  elif i==0:
    d=right[K-1-(haszero)] if K-1-(haszero)>=0 else 0
  else:
    l = left[i-1]
    r = right[K-i-1-(haszero)] if K-1-(haszero)>=0 else 0
    d=min(l*2+r, l+r*2)
  ans=min(ans,d)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split(' '))
X=list(map(int,input().split(' ')))
left=sorted([abs(x) for x in X if x<0])
right=[x for x in X if x>0]
haszero=0 in X
#extinguish i candles located in x<0
ans=10**9
for i in range(min(K+1,len(left)+1)):
  if len(right)<K-i-(haszero):
    continue
  elif i==0:
    d=right[K-1-(haszero)]
  else:
    d1=left[i-1]*2+right[K-i-1-(haszero)]
    d2=left[i-1]+right[K-i-1-(haszero)]*2
    d=min(d1,d2)
  ans=min(ans,d)
print(ans)

Submission status: Runtime Error

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

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

distance_dict=[[x,abs(x)] for x in candls]

for x in range(0,N):
  if candls[x]>=0:
    x-=1
    break

resultlist=[]
minus=x
if K==1:
  print (min(abs(candls[minus]),abs(candls[minus+1])))
  exit()
elif minus==-1:
  print(abs(candls[K-1]))
  exit()
elif minus-K+1>=0:
  resultlist.append(abs(candls[minus-K+1]))
  for i in range(1,2*K):
    try:
      resultlist.append(abs(candls[minus+i])+abs(candls[minus-K+1+i])+min(candls[minus+i],abs(candls[minus-K+1+i])))
    except:
      break
else:
  for i in range(1,minus+2):
    try:
      resultlist.append(abs(candls[K-1+i])+abs(candls[0+i])+min(candls[K-1+i],abs(candls[0+i])))
    except:
      break
try:
  resultlist.append(candls[minus+K])
except:
  pass

print (min(resultlist))

Submission status: Runtime Error

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

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

distance_dict=[[x,abs(x)] for x in candls]

for x in range(0,N):
  if candls[x]>=0:
    x-=1
    break

resultlist=[]
minus=x
if K==1:
  print (min(candls[minus],candls[minus+1]))
  exit()
elif minus==-1:
  print(candls[K-1])
  exit()
elif minus-K+1>=0:
  resultlist.append(abs(candls[minus-K+1]))
  for i in range(1,2*K):
    try:
      resultlist.append(abs(candls[minus+i])+abs(candls[minus-K+1+i])+min(candls[minus+i],abs(candls[minus-K+1+i])))
    except:
      break
else:
  for i in range(1,minus+2):
    try:
      resultlist.append(abs(candls[K-1+i])+abs(candls[0+i])+min(candls[K-1+i],abs(candls[0+i])))
    except:
      break
try:
  resultlist.append(candls[minus+K])
except:
  pass

print (min(resultlist))

Submission status: Runtime Error

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

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

distance_dict=[[x,abs(x)] for x in candls]

for x in range(0,N):
  if candls[x]>=0:
    x-=1
    break

resultlist=[]
minus=x
if K==1:
  print (min(candls[minus],candls[minus+1]))
  exit()
elif minus==-1:
  print(candls[K-1])
  exit()
elif minus-K+1>=0:
  resultlist.append(candls[minus-K+1])
  for i in range(1,2*K):
    try:
      resultlist.append(candls[minus+i]+abs(candls[minus-K+1+i])+min(candls[minus+i],abs(candls[minus-K+1+i])))
    except:
      break
else:
  for i in range(1,minus+2):
    try:
      resultlist.append(candls[K-1+i]+abs(candls[0+i])+min(candls[K-1+i],abs(candls[0+i])))
    except:
      break
try:
  resultlist.append(candls[minus+K])
except:
  pass

print (min(resultlist))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[]
if n==1:
    print(a[0])
else:
    for i in range(n-k+1):
        if a[i]<=0 and 0<=a[i+k-1]:
            s.append(min(2*a[i+k-1]-a[i],a[i+k-1]-2*a[i]))
        elif a[i+k]<=2:
            s.append(-a[i])
        else:
            s.append(a[i+k-1])
    print(min(s))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[]
if n==1:
    print(a[0])
else:
    for i in range(n-k):
        if a[i]<=0 and 0<=a[i+k-1]:
            s.append(min(2*a[i+k-1]-a[i],a[i+k-1]-2*a[i]))
        elif a[i+k]<=2:
            s.append(-a[i])
        else:
            s.append(a[i+k-1])
    print(min(s))

Submission status: Runtime Error

Solution code:
n,k = (int(i) for i in input().split())
x = list(int(i) for i in input().split())
px=[]
mx=[]
for i in range(n):
    if x[i]>=0:
        px = x[i:]
        mx = x[:i]
        break
res = 1000000000
p = len(px)
m = len(mx)
if p>0 and m>0:
    for i in range(1,min(m,k)+1):
        if p>=k-i:
            res = min(res,2*abs(mx[-i])+px[k-i-1])
    for j in range(1,min(p,k)+1):
        if m>=k-j:
            res = min(res,2*px[j-1]+abs(mx[-k+j]))
elif p>0:
    res = px[k-1]
else:
    res = abs(mx[-k])
print(res)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>=0:
    b=X[K]
elif X[-1]<=0:
    b=abs(X[-K])
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>=0:
    b=X[K]
elif X[-1]<=0:
    b=X[-K]
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>=0:
    b=X[K]
elif X[-1]<=0:
    b=X[-K]
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
        print(X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])),j,X[a+j],X[a+j-K+1],min(K-a+1,N-a+1))   
print(b)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>0:
    b=X[K]
elif X[0]==0:
    b=X[K-1]
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K,N-a)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>0:
    b=X[K]
elif X[0]==0:
    b=X[K-1]
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K-a+2,N-2*a+2)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K-a+2,N-2*a+2)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>0:
    b=X[K]
elif X[0]==0:
    b=X[K-1]
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K-a+2,N-a+2)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K-a+2,N-a+2)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if X[0]>0:
    b=X[K]
elif X[0]==0:
    b=X[K-1]
elif 0 in X:
    a=0
    b=10**9
    for i in range(N):
        if X[i]==0:
            a=i
            break
    for j in range(max(0,K-1-a),min(K-a+1,N-a+1)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
else:
    a=0
    b=10**9
    for i in range(N):
        if X[i]>0:
            a=i
            break
    for j in range(max(-1,K-a-1),min(K-a+1,N-a+1)):
        if b>X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1])):
            b=X[a+j]-X[a+j-K+1]+min(abs(X[a+j]),abs(X[a+j-K+1]))
print(b)

Submission status: Runtime Error

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

x_min = []
flag = False
for i in range(N):
    if x[i] == 0:
        K -= 1
        flag = True
    if x[i] > 0:
        break
if flag:
    x_min = x[0:i][::-1]
else:
    x_min = [0] + x[0:i][::-1]
x_plus = [0] + x[i:]
length_m = len(x_min)
length_p = len(x_plus)
# print(x_min, x_plus)
ans = 2*abs(x[0]) + abs(x[-1])

for i in range(length_m):
    if (length_p-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_min[i]) + abs(x_plus[K-i])
#    print(temp)
    ans = min(ans, temp)

for i in range(length_p):
    if (length_m-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_plus[i]) + abs(x_min[K-1])
#        print(temp)
    ans = min(ans, temp)

print(ans)

Submission status: Runtime Error

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

x_min = []
for i in range(N):
    if x[i] == 0:
        K -= 1
    if x[i] > 0:
        break

x_min = [0] + x[0:i][::-1]
x_plus = [0] + x[i:]
length_m = len(x_min)
length_p = len(x_plus)
# print(x_min, x_plus)
ans = 2*abs(x[0]) + abs(x[-1])

for i in range(length_m):
    if (length_p-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_min[i]) + abs(x_plus[K-i])
#    print(temp)
    ans = min(ans, temp)

for i in range(length_p):
    if (length_m-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_plus[i]) + abs(x_min[K-1])
#        print(temp)
    ans = min(ans, temp)

print(ans)

Submission status: Runtime Error

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

x_min = []
for i in range(N):
    if x[i] == 0:
        K -= 1
    if x[i] > 0:
        break

x_min = [0] + x[0:i][::-1]
x_plus = [0] + x[i:]
length_m = len(x_min)
length_p = len(x_plus)
# print(x_min, x_plus)
ans = 2*(x[0]) + x[-1]

for i in range(length_m):
    if (length_p-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_min[i]) + abs(x_plus[K-i])
#    print(temp)
    ans = min(ans, temp)

for i in range(length_p):
    if (length_m-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_plus[i]) + abs(x_min[K-1])
#        print(temp)
    ans = min(ans, temp)

print(ans)

Submission status: Runtime Error

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

import bisect
# 別に二分探索でなくても良いが一応
insert = bisect.bisect_left(x, 0)
if x[insert] == 0:
    k -= 1
    if k == 0 :
        print(0)
        exit()
    x = x[:insert] + x[insert+1:]

# l[i]はi個左に動いたときの座標
l = [0] + list(map(abs, x[:insert]))[::-1]
r = [0] + x[insert:]
# print(l, r)
ans = float("inf")
for i in range(k+1):
    if i < len(l) and k-i < len(r):
        ans = min(ans, l[i] + 2*r[k-i], 2*l[i] + r[k-i])
print(ans)

Submission status: Runtime Error

Solution code:
N = input()
k = input()
array = []
for i in range(N):
    x = input()
    array.append(x)
left_index = 0
right_index = 0
if array[0]*array[-1]>=0:
    if array[0] >= 0:
        answer = array[k-1]
    else:
        answer = abs(array[-k])
else:
    result = []
    x = min([abs(i) for i in array])
    if x == 0:
        index = array.index(x)
        if index < k - 1:
            left_index = 0
        else:
            left_index = index - k + 2
            result.append(abs(array[index-k+1]))
        if len(array) - 1 - index < k - 1:
            right_index = len(array)
        else:
            right_index = index + k - 1
            result.append(array[index+k-1])

    else:
        if x in array:
            index = array.index(x)
            middle_left_index = index - 1
            middle_right_index = index
        else:
            index = array.index(-x)
            middle_left_index = index
            middle_right_index = index + 1
        if middle_left_index < k - 1:
            left_index = 0
        else:
            left_index = middle_left_index - k + 2
            result.append(abs(array[middle_left_index - k + 1]))
        if len(array) - 1 - middle_right_index < k - 1:
            right_index = len(array)
        else:
            right_index = middle_right_index + k - 1
            result.append(array[middle_right_index+k-1])
    new_array = array[left_index:right_index]
    if len(new_array) >= k:
        min_route = max(abs(new_array[0]), abs(new_array[-1]))**2
        for i in range(len(new_array)):
            if i+k-1 <= len(new_array) - 1:
                left_abs = abs(new_array[i])
                right_abs = abs(new_array[i+k-1])
                current_min_route = min(left_abs, right_abs) + left_abs + right_abs
                if min_route > current_min_route:
                    min_route = current_min_route
        result.append(min_route)
    answer = min(result)
print answer

Submission status: Runtime Error

Solution code:
import time
N = input()
k = input()
array = []
for i in range(N):
    x = input()
    array.append(x)

begin = time.time()
print begin
left_index = 0
right_index = 0
if array[0]*array[-1]>=0:
    if array[0] >= 0:
        answer = array[k-1]
    else:
        answer = abs(array[-k])
else:
    result = []
    x = min([abs(i) for i in array])
    if x == 0:
        index = array.index(x)
        if index < k - 1:
            left_index = 0
        else:
            left_index = index - k + 2
            result.append(abs(array[index-k+1]))
        if len(array) - 1 - index < k - 1:
            right_index = len(array)
        else:
            right_index = index + k - 1
            result.append(array[index+k-1])

    else:
        if x in array:
            index = array.index(x)
            middle_left_index = index - 1
            middle_right_index = index
        else:
            index = array.index(-x)
            middle_left_index = index
            middle_right_index = index + 1
        if middle_left_index < k - 1:
            left_index = 0
        else:
            left_index = middle_left_index - k + 2
            result.append(abs(array[middle_left_index - k + 1]))
        if len(array) - 1 - middle_right_index < k - 1:
            right_index = len(array)
        else:
            right_index = middle_right_index + k - 1
            result.append(array[middle_right_index+k-1])
    new_array = array[left_index:right_index]
    if len(new_array) >= k:
        min_route = max(abs(new_array[0]), abs(new_array[-1]))**2
        for i in range(len(new_array)):
            if i+k-1 <= len(new_array) - 1:
                left_abs = abs(new_array[i])
                right_abs = abs(new_array[i+k-1])
                current_min_route = min(left_abs, right_abs) + left_abs + right_abs
                print left_abs, right_abs
                if min_route > current_min_route:
                    min_route = current_min_route
        result.append(min_route)
    print result
    answer = min(result)

print answer
end = time.time()
print end
print "Processing time is: %f seconds" % (end-begin)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
zero = 0
for i in range(N):
    if X[i] >= 0:
        break
    zero += 1
p = len(X[zero:])
m = len(X[:zero])
pl = X[zero:]
mi = X[:zero]
mi = mi[::-1]
num = float("inf")
if m == 0:
    num = min(num, pl[K - 1])
elif p == 0:
    num = min(num, mi[K - 1])
else:
    for i in range(K + 1):
        j = K - i
        if 0 < j <= m and 0 < i:
            rec1 = pl[i - 1] * 2 + abs(mi[j - 1])
            rec2 = pl[i - 1] + 2 * abs(mi[j - 1])
            num = min(num, rec1)
            num = min(num, rec2)
        elif j == 0 and K - 1 < p:
            num = min(num, pl[K - 1])
        elif i == 0 and K - 1 < m:
            num = min(num, mi[K - 1])


print(num)

Submission status: Runtime Error

Solution code:
def find_center(arr):
    for i in range(len(arr)-1):
        if arr[i] < 0 <= arr[i+1]:
            return i+1
    return len(arr) + 1

def candles(N,K,arr):
    center = find_center(arr)
    left = list(map(abs,arr[center-1::-1]))
    right = arr[center:]
    # print("center: {}".format(center))
    # print("left: {}".format(left))
    # print("right: {}".format(right))

    min_dis = None
    for i in range(min(len(left),K),-1,-1):
        left_dis = left[i-1]
        right_dis = right[K-i-1]
        # print("index: {}".format(i))
        # print("left_dis: {}".format(left_dis))
        # print("right_dis: {}".format(right_dis))

        if len(left[0:i]) < K:
            dis = left_dis * 2 + right_dis if left_dis < right_dis else right_dis * 2 + left_dis
        elif len(left[0:i]) == K:
            dis = left_dis
        elif len(right[0:K-i]) == K:
            dis = right_dis
        else:
            print("ERROR")

        # print("distance: {}".format(dis))

        if min_dis is None or dis < min_dis:
            min_dis = dis

    return min_dis

    


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

print(candles(N,K,arr))

Submission status: Runtime Error

Solution code:
def find_center(arr):
    for i in range(len(arr)-1):
        if arr[i] < 0 <= arr[i+1]:
            return i+1
    return len(arr) + 1

def candles(N,K,arr):
    center = find_center(arr)
    left = list(map(abs,arr[center-1::-1]))
    right = arr[center:]
    # print("center: {}".format(center))
    # print("left: {}".format(left))
    # print("right: {}".format(right))

    min_dis = None
    for i in range(min(len(left),K),-1,-1):
        left_dis = left[i-1]
        right_dis = right[K-i-1]
        # print("index: {}".format(i))
        # print("left_dis: {}".format(left_dis))
        # print("right_dis: {}".format(right_dis))

        if len(left[0:i]) < K:
            dis = left_dis * 2 + right_dis if left_dis < right_dis else right_dis * 2 + left_dis
        elif len(left[0:i]) == K:
            dis = left_dis
        elif len(right[0:K-i]) == K:
            dis = right_dis
        else:
            # print("ERROR")

        # print("distance: {}".format(dis))

        if min_dis is None or dis < min_dis:
            min_dis = dis

    return min_dis

    


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

print(candles(N,K,arr))

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    bi = bisect.bisect_left(a, 0)
    r = inf
    if bi >= k:
        r = abs(a[bi-k])
    if bi+k-1 < n and r > a[bi+k-1]:
        r = a[bi+k-1]
    for i in range(1, min([k-1, bi+1, n])):
        t = -a[bi-i]
        u = a[bi+k-i-1]
        s = t + u + min(t,u)
        if r > s:
            r = s
    return r



print(main())

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    bi = bisect.bisect_left(a, 0)
    r = inf
    if bi >= k:
        r = abs(a[bi-k])
    if bi+k-1 < n and r > a[bi+k-1]:
        r = a[bi+k-1]
    for i in range(1, min(k-1, bi+1)):
        t = -a[bi-i]
        u = a[bi+k-i-1]
        s = t + u + min(t,u)
        if r > s:
            r = s
    return r



print(main())

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
num=list(map(int,input().split()))
mark=[0]*N
count=0
log=0

while K!=0:
    for i in range(N):
        if mark[i]==0 and abs(log-num[i])==min(abs(log-num[i])):
            mark[i]=1
            count+=abs(log-num[i])
            log=num[i]
            K-=1
            break
print(count)

Submission status: Runtime Error

Solution code:
import statistics

def Medi(Arra, Haba):
    re = []
    for i_M in range(len(Arra) - Haba + 1):
        re.append(statistics.median_high(Arra[i_M: i_M+Haba]))
    return re

N = int(input())
A = list(map(int, input().split()))
print(A)
ans = []

if N == 1:
    ans.append(A[0])
else:
    kuri = max(N//2, 10)
    for i in range(1, N+1):
        ans += Medi(A, i)
        so = ans
        so.sort()
        print(so)


print(statistics.median_high(ans))

Submission status: Runtime Error

Solution code:
import bisect

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

zero_ind=bisect.bisect_left(x,0)
"""
print("max",0,k-zero_ind-1)
print("min",k,n-zero_ind)
print("max",1,k-n+zero_ind-1)
print("min",k,zero_ind+2)
"""
v,w=10**9,10**9
if(n-zero_ind >= k):v=abs(x[zero_ind+k-1])
if(zero_ind >= k):w=abs(x[zero_ind-k])

y=[10**9]
z=[10**9]

if(zero_ind != 0 and zero_ind != n):
    y=[abs(x[zero_ind+i])+abs(x[zero_ind+i]-x[zero_ind-(k-i)+1]) for i in range(max(0,k-zero_ind-1),min(k,n-zero_ind))]
    z=[abs(x[zero_ind-i])+abs(x[zero_ind-i]-x[zero_ind+(k-i)-1]) for i in range(max(1,k-n+zero_ind-1),min(k,zero_ind+2))]

"""
for i in range(k):
    print(abs(x[zero_ind+i]))
    print(x[zero_ind-(k-i)+1])

for i in range(1,k):
    print(abs(x[zero_ind-i]))
    print(x[zero_ind+(k-i)-1])
"""
print(min([v,w]+y+z))
"""
print(y)
print(z)
"""