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 itertools
import collections
n,k=map(int,input().split())
candle=list(map(int,input().split()))
com=list(itertools.product([1,0], repeat=n))
ans=200000000
for i in range(2**n):
  count=collections.Counter(com[i])
  if count[1]==k:
    begin=0
    end=0
    cnt=0
    temp=0
    print(
    for j in range(n):
      if com[i][j]==1:
        cnt+=1
        if cnt==1:
          start=candle[j]
        elif cnt==k:
          end=candle[j]
          break;
    if start>=0:
      temp=abs(end)
      if temp<ans:
        ans=temp
    if end<=0:
      temp=abs(start)
      if temp<ans:
        ans=temp
    else:
      temp=min(abs(start), end)*2+max(abs(start), end)
      if temp<ans:
        ans=temp
print(ans)

Submission status: Runtime Error

Solution code:
INF = float('inf')
n, k = map(int, input().split())
L = list(map(int, input().split()))
for i in range(n):
    if L[i] >= 0:
        break
l = L[max(0, (i-k)):i]
l.sort(reverse=True)
r = L[i:(i+k)]
s = min(k, len(r))
e = min(k, len(l))
ans = INF
for j in range(max(1, min(s, e))):
    tr = r[(s-1)-j]
    u = k - (s - 1) + j - 1
    tl = -l[u-1] if u > 0 else 0
    t = tr + tl + min(tr, tl)
    ans = min(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    idx = bisect_left(X, 0)
    #print("idx={}".format(idx))
    ans = 10**10
    if 0 == X[idx]:
        for i in range(idx + 1):
            num = idx - i + 1
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 <= idx + (K - num) and idx + (K - num) < N:
                ans = min(ans, -2 * X[i] + X[idx + (K - num)])
        for i in range(idx + 1, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 <= idx - (K - num) and idx - (K - num) < N: ans = min(ans, 2 * X[i] - X[idx - (K - num)])
    else:
        for i in range(idx):
            num = idx - i
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 <= idx + (K - num) - 1 and idx + (K - num) - 1 < N:
                ans = min(ans, -2 * X[i] + X[idx + (K - num) - 1])
            #print("i={}, num={}, cand={}, ans={}".format(i, idx - (K - num) - 1, -2 * X[i] + X[idx + (K - num) - 1], ans))
        for i in range(idx, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 <= idx - (K - num) and idx - (K - num) < N:
                ans = min(ans, 2 * X[i] - X[idx - (K - num)])
            #print("i={}, ans={}".format(i, ans))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    idx = bisect_left(X, 0)
    #print("idx={}".format(idx))
    ans = 10**10
    if 0 == X[idx]:
        for i in range(idx + 1):
            num = idx - i + 1
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 <= idx - (K - num) < N: ans = min(ans, -2 * X[i] + X[idx + (K - num)])
        for i in range(idx + 1, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 <= idx - (K - num) < N: ans = min(ans, 2 * X[i] - X[idx - (K - num)])
    else:
        for i in range(idx):
            num = idx - i
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 <= idx + (K - num) - 1 and idx + (K - num) - 1 < N:
                ans = min(ans, -2 * X[i] + X[idx + (K - num) - 1])
            #print("i={}, num={}, cand={}, ans={}".format(i, idx - (K - num) - 1, -2 * X[i] + X[idx + (K - num) - 1], ans))
        for i in range(idx, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 <= idx - (K - num) and idx - (K - num) < N:
                ans = min(ans, 2 * X[i] - X[idx - (K - num)])
            #print("i={}, ans={}".format(i, ans))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    X.sort()
    idx = bisect_left(X, 0)
    #print("idx={}".format(idx))
    ans = 10**10
    if 0 == X[idx]:
        for i in range(idx + 1):
            num = idx - i + 1
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif idx - (K - num) < N: ans = min(ans, -2 * X[i] + X[idx + (K - num)])
        for i in range(idx + 1, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif idx - (K - num) < N: ans = min(ans, 2 * X[i] - X[idx - (K - num)])
    else:
        for i in range(idx):
            num = idx - i
            if num > K: continue
            elif num == K: ans = min(ans, -X[i])
            elif 0 < idx - (K - num) - 1 < N: ans = min(ans, -2 * X[i] + X[idx + (K - num) - 1])
            #print("i={}, ans={}".format(i, ans))
        for i in range(idx, N):
            num = i - idx + 1
            if num > K: continue
            elif num == K: ans = min(ans, X[i])
            elif 0 < idx - (K - num) < N: ans = min(ans, 2 * X[i] - X[idx - (K - num)])
            #print("i={}, ans={}".format(i, ans))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

ans = []
for i in range(n - k + 1):
    left = x[i]
    right = x[i + k - 1]
    elif abs(left) > abs(right):
        ans.append(abs(right) + abs(left - right))
    else:
        ans.append(abs(left) + abs(right - left))

print(min(ans))

Submission status: Runtime Error

Solution code:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

N, K = map(int, input().split())
x = list(map(int, input().split()))
MAX = 10**9

has0 = 1 if 0 in x else 0
if has0 and K==1:
    print(0)
    exit()
if not(has0):
    x.append(0)
    x.sort()

# minusC = len([val for val in x if val<0])
# plusC = len([val for val in x if val>0])
ind0 = x.index(0)

res = MAX
for i, xi in enu(x):
    if i < ind0:
        min_flg = True
    elif i == ind0:
        continue
    else:
        min_flg = False
    rousoku_cnt = abs(i - ind0) + has0

    if K < abs(rousoku_cnt):
        continue
    elif K == abs(rousoku_cnt):
        res = min(res, abs(xi))
    else:
        if min_flg:
            val = abs(2*x[ind0+(K-rousoku_cnt)])
            res = min(res, abs(xi) + val)
        else:
            val = abs(2*x[ind0-(K-rousoku_cnt)])
            res = min(res, abs(xi) + val)
        # print(abs(xi) + val)
print(res)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [0]+[abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [0]+[a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 1:
  print(minus[k])
elif len(minus) == 1:
  print(plus[k])
elif len(plus) < len(minus):
  for i in range(min(k+1, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-i], plus[i]+2*minus[k-i])
  print(min_)
else:
  for i in range(min(k+1, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-i], minus[i]+2*plus[k-i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [0]+[abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [0]+[a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k])
elif len(minus) == 0:
  print(plus[k])
elif len(plus) < len(minus):
  for i in range(min(k+1, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-i], plus[i]+2*minus[k-i])
  print(min_)
else:
  for i in range(min(k+1, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-i], minus[i]+2*plus[k-i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k-1])
elif len(minus) == 0:
  print(plus[k-1])
elif len(plus) < len(minus):
  for i in range(k-len(minus), min(k-1, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
  if i == k-2 and i+1 < len(plus):
    i += 1
    min_ = min(min_, plus[i])
  print(min_)
else:
  for i in range(k-len(plus), min(k-1, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-2-i], minus[i]+2*plus[k-2-i])
  if i == k-2 and i+1 < len(minus):
    i += 1
    min_ = min(min_, minus[i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k-1])
elif len(minus) == 0:
  print(plus[k-1])
elif len(plus) < len(minus):
  for i in range(min(k-1, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
  if i == k-2 and i+1 < len(plus):
    i += 1
    min_ = min(min_, plus[i])
  print(min_)
else:
  for i in range(min(k-1, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-2-i], minus[i]+2*plus[k-2-i])
  if i == k-2 and i+1 < len(minus):
    i += 1
    min_ = min(min_, minus[i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k-1])
elif len(minus) == 0:
  print(plus[k-1])
elif len(plus) < len(minus):
  for i in range(min(k-1, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
  i += 1
  min_ = min(min_, plus[i])
  print(min_)
else:
  for i in range(min(k-1, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-2-i], minus[i]+2*plus[k-2-i])
  i += 1
  min_ = min(min_, minus[i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] >= 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k-1])
elif len(minus) == 0:
  print(plus[k-1])
elif len(plus) < len(minus):
  for i in range(min(k, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
  print(min_)
else:
  for i in range(min(k, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-2-i], minus[i]+2*plus[k-2-i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [abs(a[i]) for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] > 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k-1])
elif len(minus) == 0:
  print(plus[k-1])
elif len(plus) < len(minus):
  for i in range(min(k, len(plus))):
    min_ = min(min_, 2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
  print(min_)
else:
  for i in range(min(k, len(minus))):
    min_ = min(min_, 2*minus[i]+plus[k-2-i], minus[i]+2*plus[k-2-i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [a[i] for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] > 0]

min_ = 10**18

if len(plus) == 0:
  print(minus[k-1])
elif len(minus) == 0:
  print(plus[k-1])
elif len(plus) < len(minus):
  for i in range(min(k, len(plus))):
    min_ = min(2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
  print(min_)
else:
  for i in range(min(k, len(minus))):
    min_ = min(2*minus[i]+plus[k-2-i], minus[i]+2*plus[k-2-i])
  print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a = list(map(int,input().split()))
minus = [a[i] for i in range(n) if a[i] < 0][::-1]

plus = [a[i] for i in range(n) if a[i] > 0]

min_ = 10**18

for i in range(min(k, len(plus))):
  min_ = min(2*plus[i]+minus[k-2-i], plus[i]+2*minus[k-2-i])
print(min_)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=[]
for i in range(n-k+1):
  ans.append(min(abs(l[i])+abs([l[i]-l[i+k-1]),abs(l[i+k-1])+abs(l[i+k-1]-l[i])))
print(min(ans))

Submission status: Runtime Error

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

# check
mov = [
    sum(
        [
            abs(x[j])
            if j == i
            else
            abs(x[j] - x[j - 1])
            for j in range(i, i + K)
        ]
    )
    for i in range(0, N - K)
] + [
    sum(
        [
            abs(x[j])
            if j == i
            else
            abs(x[j + 1] + x[j])
            for j in range(i, i - K, -1)
        ]
    )
    for i in range(K, N)
]

print(min(mov))

Submission status: Runtime Error

Solution code:
import bisect

N,K = (int(x) for x in input().split())
x = list(map(int, input().split()))
bs = bisect.bisect_left(x,0)
ans = float('inf')
if x[bs] == 0:
    x.pop(bs)
    K -= 1
neg = bs
pos = N - bs
if len(x) == 0 or K == 0:
    print('0')
elif neg == 0:
    print(x[K-1])
elif pos == 0:
    print(x[-K]*(-1))
else:
    for i in range(min(neg,K)+1):
        if i + pos < K:
                continue
        else:
            if i == 0:
                ans = min(ans,x[bs+K-1])
            elif i == K:
                ans = min(ans,x[bs-K]*(-1))
            else:
                tempn = x[bs-i] * (-1)
                tempp = x[bs+K-i-1]
                ans = min(ans,min(tempn,tempp)*2+max(tempn,tempp))
    print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,K = IL()
X = IL()

if 0 in X:
  X.remove(0)
  K -= 1

p = bisect.bisect(X,0)
count = []
dic = DD(lambda:(INF,INF))
dic_gyaku = DD(lambda:INF)
for i in range(N):
  dic[X[i]]=(max(p-i,i-p+1),-1 if i<p else 1)
  dic_gyaku[(max(p-i,i-p+1),-1 if i<p else 1)] = X[i]

MIN = INF
for i in range(N):
  num, sign = dic[X[i]]
  if sign < 0:
    left = K-num
    if left > 0:
      MIN = min(MIN,abs(X[i])*2+abs(dic_gyaku[(left,1)]))
    else:
      MIN = min(MIN,abs(X[i]))
  else:
    left = K-num
    if left > 0:
      MIN = min(MIN,abs(X[i])*2+abs(dic_gyaku[(left,-1)]))
    else:
      MIN = min(MIN,abs(X[i]))
print(MIN)

Submission status: Runtime Error

Solution code:
import sys
import bisect

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    if 0 in x:
        K -= 1
        x.remove(0)
    idx = bisect.bisect_left(x, 0)
    x.insert(idx, 0)
    ans1 = float('inf'); ans2 = float('inf')
    if N-idx >= K:
        ans1 = x[idx + K]
    if idx >= K:
        ans2 = abs(x[idx - K])
    ans3 = float('inf')
    for m in range(1, min(N-idx, K)+1):
        tmp = 2*x[idx+m] + abs(x[idx-(K-m)])
        ans3 = min(tmp, ans3)
    ans4 = float('inf')
    for m in range(1, min(idx, K)+1):
        tmp = 2*abs(x[idx-m]) + abs(x[idx+(K-m)])
        ans4 = min(tmp, ans4)
    ans = min(ans1, ans2, ans3, ans4)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor, gcd
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10

    N,K = LMIIS()
    X = LMIIS()



    D = [0]*(N-1)
    pre = X[0]
    for i in range(1,N):
        D[i-1] = X[i] - pre
        pre = X[i]

    #left:X[0] right:X[K-1]
    left = 0
    right = K-1
    syaku = X[right] - X[left]
    if abs(X[left]) > abs(X[right]):
        min_time = syaku + abs(X[right])
    else:
        min_time = syaku + abs(X[left])


    for i in range(N-K):
        if abs(X[left]) > abs(X[right]):
            min_time = min(min_time, syaku + abs(X[right]))
        else:
            min_time = min(min_time, syaku + abs(X[left]))
        syaku = syaku + (X[right+1] - X[right]) - (X[left+1] - X[left])
        left += 1
        right += 1
    
    print(min_time)



    
main()

Submission status: Runtime Error

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i): is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    n, k = MII()
    xx = MII()
    minv = 3*10**8
    for i in range(n-k+1):
        l = xx[i]
        r = xx[i+k-1]
        if l < 0 and r <= 0:
            minv = minv(abs(l),minv)
            continue
        if l < 0 and r > 0:
            minv = min(min(abs(l),r)*2+max(abs(l),r), minv)
            continue
        if l >= 0:
            minv = min(r, minv)
            continue
    print(minv)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i): is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    n, k = MII()
    xx = MII()
    minv = 10**8+100
    for i in range(n-k+1):
        l = xx[i]
        r = xx[i+k-1]
        if l < 0 and r <= 0:
            minv = minv(abs(l),minv)
            continue
        if l < 0 and r > 0:
            minv = min(min(abs(l),r)*2+max(abs(l),r), minv)
            continue
        if l >= 0:
            minv = min(r, minv)
    print(minv)


if __name__ == '__main__':
    main()

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)

ans=10**12
s=z-k+1
if s < 0:
  s=0
if z == 0:
  z+=1
for i in range(s,z):
  xl=x[i]
  xr=x[i+k-1]
  ans=min(ans,abs(xr)+abs(xr-xl),abs(xl)+abs(xr-xl))
print(ans)

Submission status: Runtime Error

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

def f(list1):
  min1 = list1[0]
  max1 = list1[-1]
  if min1>=0 and max1>=0:
    return abs(max1)
  elif min1<0 and max1 <0:
    return abs(min1)
  else:
    return total+min([abs(min1),abs(max1)])

ans = 2*10**8 + 2
for i in range(N-K+1):
  dist =f (xlist[i:i+K]) 
  if dist < ans :
    ans = dist

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x =list(map(int,input().split())) 
sakai = 0
for i in range(N):
  if x[i]>= 0:
    sakai = i
    break
  sakai = N
sakai += K  

x = [float("inf") for i in range(K)] + x + [float("inf") for i in range(K)]

def main(sakai):
  ans = float("inf")
  ans = min(ans,x[sakai-K])
  ans = min(ans,x[sakai+K-1])
  for i in range(1,K+1):
    ans = min(ans,2*abs(x[sakai-i])+x[sakai+(K-i-1)])
    ans = min(ans,abs(x[sakai-i])+2*x[sakai+(K-i-1)])
  if sakai == K:
    ans = x[2K-1]
  return print(ans)

main(sakai)

Submission status: Runtime Error

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
id = bisect.bisect_left(x, 0)
ans = max(x) - min(0, min(x))
if id >= K:
    ans = min(ans, x[id-K])
if id + K - 1 < N:
    ans = min(ans, x[id+K-1])

for i in range(max(0, id - K + 1), id):
    ans = min(ans, -x[i]*2 + x[i+K-1], -x[i]+x[i+K-1]*2)

print(ans)

Submission status: Runtime Error

Solution code:
def examA():
    N = I()
    ans = ((N+1)//2)/N
    print(ans)
    return

def examB():
    N, K = LI()
    H = [I()for _ in range(N)]
    H.sort()
    ans = inf
    for i in range(N-K+1):
        cur = H[i+K-1]-H[i]
        if ans>cur:
            ans = cur
    print(ans)
    return

def examC():
    N = I()
    A = LI()
    ans = [0]*N
    C = Counter(A)
    base = 0
    for c in C.values():
        base += c*(c-1)//2
    #print(C)
    #print(base)
    for i in range(N):
        ans[i] = base - (C[A[i]]-1)
    for v in ans:
        print(v)
    return

def examD():
    N, K = LI()
    X = LI()
    L = []; R = [0]
    for x in X:
        if x<0:
            L.append(-x)
        else:
            R.append(x)
    L.append(0)
    L = L[::-1]
    ans = inf
    for i in range(len(L)):
        if len(R)<=K-i:
            continue
        cur = min(L[i] * 2 + R[K - i], L[i] + R[K - i] * 2)
        # print(cur)
        if ans > cur:
            ans = cur
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examD()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Runtime Error

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def solve(X, K):
    N = len(X)

    cumsum = [0 for _ in range(N)]
    for i in range(1, N):
        cumsum[i] = abs(X[i] - X[i - 1]) + cumsum[i - 1]

    idx_zero = bisect.bisect_left(X, 0)

    ret = 1000000005
    for i in range(0, N):
        if X[i] >= 0:
            break
        res = -X[i]
        k = idx_zero - i
        if k < K:
            res += cumsum[idx_zero + K - k - 1] - cumsum[i]
        ret = min(ret, res)

    return ret


if __name__ == '__main__':
    N, K = map(int, input().split())
    X = list(map(int, input().split()))

    res = 1000000005
    if min(X) >= 0:
        print(X[K - 1])
    elif max(X) <= 0:
        X = X[::-1]
        print(-X[K - 1])
    else:
        res = min(res, solve(X, K))
        X = [-x for x in X]
        res = min(res, solve(X[::-1], K))
        print(res)

Submission status: Runtime Error

Solution code:
def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    if 0 in X:
        N -= 1
        K -= 1
        X.remove(0)
    ans = 10**9
    for i in range(N-K+1):
        if X[i+K-1]<=0:
            p = -X[i]
        elif X[i]>=0:
            p = X[i+K-1]
        else:
            p = X[i+K-1]-X[i]+min(X[i+K-1],abs(X[i]))
        ans = min(ans,p)
    return ans
print(solve())

Submission status: Runtime Error

Solution code:
def solve():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    if 0 in X:
        N -= 1
        K -= 1
        X.remove(0)
    ans = X[-1]-X[0]+min(X[-1],abs(X[0]))
    for i in range(N-K+1):
        if X[i+K-1]<=0:
            p = -X[i]
        elif X[i]>=0:
            p = X[i+K-1]
        else:
            p = X[i+K-1]-X[i]+min(X[i+K-1],abs(X[i]))
        ans = min(ans,p)
    return ans
print(solve())