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 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+1:zero]),sum(a[zero:zero+k+1]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k]:
        cnt = -2*a[zero-i]+a[zero+k]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+k]:
        cnt = -a[zero-k+i]+2*a[zero+k]
    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)
cnt = min(-sum(a[zero-k-1:zero]),sum(a[zero:zero+k+1]))
for i in range(1,k):
    if cnt > -2*a[zero-i]+a[zero+k]:
        cnt = -2*a[zero-i]+a[zero+k]
    else:
        break
for i in range(1,k):
    if cnt > -a[zero-k+i]+2*a[zero+k]:
        cnt = -a[zero-k+i]+2*a[zero+k]
    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)
cnt = (10**8)*(10**5)
cnt = min(sum(a[zero:n]),-sum(a[0:zero]))
for i in range(1,zero):
    if cnt > -2*a[zero-i]+a[n-1-i]:
        cnt = -2*a[zero-i]+a[n-1-i]
    else:
        break
for i in range(1,zero):
    if cnt > -a[i]+2*a[zero+i-1]:
        cnt = -a[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)
cnt = (10**8)*(10**5)
cnt = min(sum(a[zero:n]),-sum(a[0:zero]))
for i in range(1,zero):
    if cnt > -2*a[zero-i]+a[n-1-i]:
        cnt = -2*a[zero-i]+a[n-1-i]
    else:
        break
for i in range(1,zero):
    if cnt > -a[i]+2*a[zero+i]:
        cnt = -a[i]+2*a[zero+i]
    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)
cnt = (10**8)*(10**5)
cnt = min(sum(a[zero:n]),-sum(a[0:zero]))
for i in range(1,zero):
    if cnt > -2*a[zero-i]+a[n-i]:
        cnt = -2*a[zero-i]+a[n-i]
    else:
        break
for i in range(1,zero):
    if cnt > -a[i]+2*a[zero+i]:
        cnt = -a[i]+2*a[zero+i]
    else:
        break
print(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x=list(map(int,input().split()))
flag=0
for i in range(len(x)):
    if x[i]>=0:
        zp=i
        if 0 not in x:
            x.insert(i,0)
            flag=1
        break

for i in range(len(x)):
        x[i]=abs(x[i])

ans=10**11

if flag==1:
    for k in range(zp+K-len(x)+1,min(zp,K)+1):
        ans=min( ans, x[zp-k]+ x[zp+K-k]+ min(x[zp-k],x[zp+K-k]) )
else:
    for k in range(zp+K-len(x),min(zp,K)+1):
        ans=min( ans, x[zp-k]+ x[zp+K-k-1]+ min(x[zp-k],x[zp+K-k-1]) )

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x=list(map(int,input().split()))
for i in range(len(x)):
    if x[i]>=0:
        zp=i
        if 0 not in x:
            x.insert(i,0)
        break
for i in range(len(x)):
    if x[i]<=0:
        x[i]=abs(x[i])

ans=10**11
for k in range(zp+K-len(x)+1,min(zp,K)+1):
    ans=min( ans, x[zp-k]+ x[zp+K-k]+ min(x[zp-k],x[zp+K-k]) )

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x=list(map(int,input().split()))
for i in range(len(x)):
    if x[i]>=0:
        zp=i
        x.insert(i,0)
        break
for i in range(len(x)):
    if x[i]<=0:
        x[i]=abs(x[i])

ans=10**11
for k in range(zp+K-len(x)+1,min(zp,K)+1):
    ans=min( ans, x[zp-k]+ x[zp+K-k]+ min(x[zp-k],x[zp+K-k]) )

print(ans)

Submission status: Runtime Error

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

#b = list(set(a))
b.sort()
print(b)
 
if len(b) > 1:
    l = len(b)//2
    print(b[l])
else:
    print(b[0])

Submission status: Runtime Error

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

#b = list(set(a))
b.sort()
 
if len(b) > 1:
    l = len(b)//2
    print(b[l])
else:
    print(b[0])

Submission status: Runtime Error

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

x = list(map(int,input().split()))
#x = [ abs(int(i))  for i in input().split()]
x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)

# plus
if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx - k])
    if tmp < min :
        min = tmp

# plus/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= 0:
        m = abs(x[misIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

Submission status: Runtime Error

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

x = list(map(int,input().split()))
#x = [ abs(int(i))  for i in input().split()]
x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)

# plus
if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx - k])
    if tmp < min :
        min = tmp

# plus/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= 0:
        m = abs(x[plsIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

Submission status: Runtime Error

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

x = list(map(int,input().split()))
#x = [ abs(int(i))  for i in input().split()]
x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)

# plus
if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx + k])
    if tmp < min :
        min = tmp

# plus/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= 0:
        m = abs(x[plsIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

Submission status: Runtime Error

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

x = list(map(int,input().split()))
#x = [ abs(int(i))  for i in input().split()]
x.append(0)
x.sort()

min = 10**8

zeroIdx = x.index(0)

# plus
if zeroIdx + k < len(x) :
    min = x[zeroIdx + k] 

# minus 
if zeroIdx - k >= 0 :
    tmp = abs(x[zeroIdx + k])
    if tmp < min :
        min = tmp

# plus/minus
plsIdx = k+1
misIdx = k-1
while True:
    p = 0
    m = 0
    if plsIdx + 1 <  len(x):
        p = abs(x[plsIdx])

    if misIdx - 1 >= len(x):
        m = abs(x[plsIdx])
    
    if p == 0 :
        misIdx -= 1
    elif m == 0 :
        plsIdx += 1
    elif p < m :
        plsIdx += 1
    else: 
        misIdx -= 1

    if plsIdx - misIdx >= k :
        if abs(x[plsIdx]) > abs(x[misIdx]) :
            tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
        else:
            tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])
        break

if tmp < min :
    min = tmp
print(min)

Submission status: Runtime Error

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

m = min(b - a + min(abs(a), bas(b)) for a, b in zip(x, x[k-1]))
print(m)

Submission status: Runtime Error

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

for i in range(n - k + 1):
    x_k = [ x[i], x_k[i+k-1] ]
    #print(x_k)
    
    if x_k[0] >= 0:
        t = x_k[1]
            
    elif x_k[1] <= 0:
        t = -x_k[0]
    
    else:
        t = min(-x_k[0] + x_k[1] *2,-x_k[0] * 2 + x_k[1])

    time = min(t, time)
    
print(time)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
if K==1 and (0 in x):
    print(0)
else:
    if 0 in x:
        pass
    else:
        K += 1
        counter = 0
        for i in x:
            if i > 0:
                break
            else:
                counter += 1
        x.insert(counter,0)

    result = []
    g,s = x.index(0),x.index(0)-K+1
    while (not s>x.index(0)):
        if s<0 or g>(len(x)-1):
            s += 1
            g += 1
            continue
        else:
            result.append(2*(-x[s]) + x[g])
            result.append(-x[s] + 2*(x[g]))
            s += 1
            g += 1
    print(result.sort()[0])

Submission status: Runtime Error

Solution code:
N, K = [int(item) for item in input().split()]
candles = []
for item in input().split():
  if int(item) == 0:
    K -= 1
    continue
  candles.append(int(item))
K = max(K-1, 0) 
l = []
for i, candle in enumerate(candles):
  sign = candle > 0
  s = 1 if sign else -1
  ik = i-K if sign else i+K
  if ik > N or ik < 0:
    continue
  if (candles[ik] > 0) == sign:
    l.append(candle * s)
  else:
    l.append(candle * s + candles[ik] * -2 * s)
if len(l) == 0:
  l.append(0)
l.sort()
print(l[0])

Submission status: Runtime Error

Solution code:
N, K = [int(item) for item in input().split()]
candles = []
for item in input().split():
  if int(item) == 0:
    K -= 1
    continue
  candles.append(int(item))
K = max(K-1, 0) 
print(K)
l = []
for i, candle in enumerate(candles):
  sign = candle > 0
  s = 1 if sign else -1
  ik = i-K if sign else i+K
  if ik > N or ik < 0:
    continue
  if (candles[ik] > 0) == sign:
    l.append(candle * s)
  else:
    l.append(candle * s + candles[ik] * -2 * s)
if len(l) == 0:
  l.append(0)
l.sort()
print(l[0])

Submission status: Runtime Error

Solution code:
N, K = [int(item) for item in input().split()]
candles = []
for item in input().split():
  if int(item) == 0:
    K -= 1
    continue
  candles.append(int(item))
K-=1
l = []
for i in range(N):
  sign = candles[i] > 0
  s = 1 if sign else -1
  ik = i-K if sign else i+K
  if ik > N or ik < 0:
    continue
  if (candles[ik] > 0) == sign:
    l.append(candles[i] * s)
  else:
    l.append(candles[i] * s + candles[ik] * -2 * s)
l.sort()
print(l[0])

Submission status: Runtime Error

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

nlis = []
for i in lis:
    if not i >= 0:
        nlis.insert(0,-i)
    else:
        plis = lis[lis.index(i):]
        break

res = []
n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = 2*min(plis[i],nlis[K-i-2])+max(plis[i],nlis[K-i-2])
    res.append(res1)

print(min(res))

Submission status: Runtime Error

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

nlis = []
for i in lis:
    if not i >= 0:
        nlis.insert(0,-i)
    else:
        plis = lis[lis.index(i):]
        break
res = False

if plis==[]:
    res = nlis[K-1]
elif nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = 2*min(plis[i],nlis[K-i-2])+max(plis[i],nlis[K-i-2])
    if not res or res>res1:
        res = res1
print(res)

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
lis = list(map(int, input().split()))
plis = []
nlis = []
for i in lis:
    if not i >= 0:
        nlis.insert(0,-i)
    else:
        plis = lis[lis.index(i):]
        break
res = False

if plis==[]:
    res = nlis[K-1]
elif nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = 2*min(plis[i],nlis[K-i-2])+max(plis[i],nlis[K-i-2])
    if res>res1:
        res = res1
    else:
        res = res1
print(res)

Submission status: Runtime Error

Solution code:
N,K = map(int, input().split())
lis = list(map(int, input().split()))
plis = []
nlis = []
for i in lis:
    if not i >= 0:
        nlis.insert(0,-i)
    else:
        plis = lis[lis.index(i):]
        break
res = False

if plis==[]:
    res = nlis[K-1]
elif nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

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

plis = []
nlis = []
for i in lis:
    if i >= 0:
        plis.append(i)
    else:
        nlis.insert(0,-i)
res = False

if plis==[]:
    res = nlis[K-1]
elif nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

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

for i in lis:
    if i >= 0:
        plis.append(i)
    else:
        nlis.insert(0,-i)
res = False

if plis==[]:
    res = nlis[K-1]
elif nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

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


plis = [i for i in lis if i >= 0][:K]
nlis = sorted([-1*i for i in lis if i < 0])[:K]

res = False

if plis==[]:
    res = nlis[K-1]
elif nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

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

plis = [i for i in lis if i >= 0]
nlis = sorted([-1*i for i in lis if i < 0])

res = False

if plis==[]:
    res = nlis[K-1]
if nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

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

plis = [i for i in lis if i >= 0]
nlis = sorted([-1*i for i in lis if i < 0])
nlis.sort()

res = False

if plis==[]:
    res = nlis[K-1]
if nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

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

plis = [i for i in lis if i >= 0]
nlis = sorted([-1*i for i in lis if i < 0])
nlis.sort()

res = False

if plis==[]:
    res = nlis[K-1]
if nlis==[]:
    res = plis[K-1]

n = min([len(plis),len(nlis),K])
for i in range(n):
    res1 = plis[i] + 2*nlis[K-i-2]
    res2 = 2*plis[i] + nlis[K-i-2]
    print(plis[i], nlis[K-i-2])
    if res:
        res = min([res,res1,res2])
    else:
        res = min([res1,res2])
print(res)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
L = [0]*(N-K+1) 

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

Submission status: Runtime Error

Solution code:
import copy
#横にn個入力
n, k = map(int, input().split())

#n...普通、a...横で入力された複数の要素を配列で取得
a = input().split()

x = [int(m) for m in a]

X = []

X = copy.deepcopy(x)
X.append(0)
X.sort()

co1 = 0
co2 = 0
co3 = 0
co4 = 0
co3ar = []
co4ar = []

if 0 in x:
    if len(x) > x.index(0) + k - 1:
        co1 = x[x.index(0) + k - 1]
    else:
        co1 = 10 ** 9
else:
    if len(X) > X.index(0) + k:
        co1 = X[X.index(0) + k]
    else:
        co1 = 10 ** 9

if 0 in x:
    if 0 <= x.index(0) - (k - 1):
        co2 = - x[x.index(0) - (k - 1)]
    else:
        co2 = 10 ** 9
else:
    if 0 <= X.index(0) - k:
        co2 = - X[X.index(0) - k]
    else:
        co2 = 10 ** 9

if 0 in x:
    for i in range(1, n + 1):
        if i >= k - i:
            break
        else:
            co3ar.append(x[x.index(0) + i] + x[x.index(0) + i] - x[x.index(0) + 1 - (k - i)])
else:
    for i in range(1, n + 1):
        if i >= k - i:
            break
        else:
            co3ar.append(X[X.index(0) + i] + X[X.index(0) + i] - X[X.index(0) - (k - i)])


if 0 in x:
    for i in range(1, n + 1):
        if i >= k - i:
            break
        else:
            co4ar.append(- x[x.index(0) - i] + x[x.index(0) + k - i - 1] - x[x.index(0) - i])
else:
    for i in range(1, n + 1):
        if i >= k - i:
            break
        else:
            co4ar.append(- X[X.index(0) - i] + X[X.index(0) + k - i] - X[X.index(0) - i])

if co3ar == []:
    co3 = 10 ** 9
else:
    co3 = min(co3ar)

if co4ar == []:
    co4 = 10 ** 9
else:
    co4 = min(co4ar)

print(min(co1,co2,co3,co4))

Submission status: Runtime Error

Solution code:
def candles(n, k, xs):
    mini = 1000000000000000

    def _hoge(i, filled, dis, is_left, can_rotate):
        nonlocal mini

        ri = i + 1
        li = i - 1

        if i < 0 or len(xs) == i:
            return
        if not filled:
            dis += abs(xs[i])
        elif is_left:
            dis += abs(xs[i] - xs[i + 1])
        else:
            dis += abs(xs[i] - xs[i - 1])
        if i not in filled:
            filled = list(filled)
            filled += [i]
            if len(filled) == k:
                if mini > dis:
                    mini = dis
                return

        if mini < dis:
            return

        if is_left:
            _hoge(li, filled, dis, is_left, can_rotate)
        else:
            _hoge(ri, filled, dis, is_left, can_rotate)
        if can_rotate:
            if is_left:
                _hoge(ri, filled, dis, False, False)
            else:
                _hoge(li, filled, dis, True, False)

    if max(xs) > 0:
        ri = min(filter(lambda x: x >= 0, xs))
        _hoge(xs.index(ri), [], 0, False, True)

    if min(xs) < 0:
        li = max(filter(lambda x: x <= 0, xs))
        _hoge(xs.index(li), [], 0, True, True)
    return mini

n, k = map(int, input().split())
xs = list(map(int, input().split()))
print(candles(n, k, xs))

Submission status: Runtime Error

Solution code:
def candles(n, k, xs):
    mini = 1000000000000000

    def _hoge(i, filled, dis, is_left, can_rotate):
        nonlocal mini

        ri = i + 1
        li = i - 1

        if i < 0 or len(xs) == i:
            return
        if not filled:
            dis += abs(xs[i])
        elif is_left:
            dis += abs(xs[i] - xs[i + 1])
        else:
            dis += abs(xs[i] - xs[i - 1])
        if i not in filled:
            filled = list(filled)
            filled += [i]
            if len(filled) == k:
                if mini > dis:
                    mini = dis
                return

        if mini < dis:
            return

        if is_left:
            _hoge(li, filled, dis, is_left, can_rotate)
        else:
            _hoge(ri, filled, dis, is_left, can_rotate)
        if can_rotate:
            if is_left:
                _hoge(ri, filled, dis, False, False)
            else:
                _hoge(li, filled, dis, True, False)

    ri = min(filter(lambda x: x >= 0, xs))
    _hoge(xs.index(ri), [], 0, False, True)

    li = max(filter(lambda x: x <= 0, xs))
    _hoge(xs.index(li), [], 0, True, True)

    return mini

n, k = map(int, input().split())
xs = list(map(int, input().split()))
print(candles(n, k, xs))

Submission status: Runtime Error

Solution code:
N,K=(int(i) for i in input().split(' '))
x=[int(i) for i in input().split(' ')]
time=None
index=0
while True:
  if x[index]==0:
    K-=1
    x.remove(index)
  elif x[index]>0:
    break
  index+=1
  if index==len(x):
    break
x.insert(index,0)
for i in range(max(0,K-len(x)+index+1),min(K+1,index+1)):
  if -x[index-i]>x[index+K-i]:
    time_tmp=-x[index-i]+2*x[index+K-i]
  else:
    time_tmp=-2*x[index-i]+x[index+K-i]
  if time==None:
    time=time_tmp
  if time_tmp<time:
    time=time_tmp
print(time)

Submission status: Runtime Error

Solution code:
import sys

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

r = 0
for idx, xi in enumerate(x):
    if xi >= 0:
        r = idx
        break

s = max(r - K, 0)
t = min(r+K-1, len(x))


result = []

if (r - K >= 0):
    result.append(-x[r-K])

for i in range(s+1, r):
    lhs = -x[i]
    rhs = x[i + K - 1]
    result.append(lhs * 2 + rhs)
    result.append(lhs + rhs * 2)
    pass


if (r + K - 1 < N):
    result.append(x[r + K - 1])

print(min(result))

sys.exit(0)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
  a.remove(0)
  n -= 1
  k -= 1
m = 300000000
for i in range(n+1-k):
  if a[i+k-1] < 0:
    x = -a[i]
  if a[i] > 0:
    x = a[i+k-1]
  else:
    x = (a[i+k-1]-a[i]) + min(-a[i],a[i+k-1])
  m = min(m,x)
print(m)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
for i in range(n):
  if x[i]>=0:
    zero=i
    break
if n-zero+1>k:
  mindis=x[zero+k-1]
else:
  mindis=max(x[n-1],abs(x[n-k]))+min(x[n-1],abs(x[n-k]))*2
for i in range(1,k):
  if zero+k-i-1>n-1:
    continue
  if zero-i<0:
    break
  under=abs(x[zero-i])
  up=x[zero+k-i-1]
  tmp=max(under,up)+min(under,up)*2
  if tmp<mindis:
    mindis=tmp
print(mindis)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
for i in range(n):
  if x[i]>=0:
    zero=i
    break
if n-zero+1>k:
  mindis=x[zero+k-1]
else:
  mindis=max(x[n-1],abs(x[n-k]))+min(x[n-1],abs(x[n-k]))*2
for i in range(1,k):
  if zero+k-i-1>n:
    continue
  if zero-i<0:
    break
  under=abs(x[zero-i])
  up=x[zero+k-i-1]
  tmp=max(under,up)+min(under,up)*2
  if tmp<mindis:
    mindis=tmp
print(mindis)

Submission status: Runtime Error

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

for i in range(N - K + 1):
    l = i
    r = i + K - 1

    Xl, Xr = X[l], X[r]
    if i == 0:
        ans = abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr)
    else:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr) , ans)


print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
for i in range(N-K+1):
  b.append(min(abs(a[i])+abs(a[i+k-1]-a[i]),abs(a[i+k-1])+abs(a[i+k-1]-a[i])))
print(min(b))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
dis = N-K+1
for i in range(dis):
  b.append(min(abs(a[i])+abs(a[i]-a[i+dis-1]),abs(a[i+dis-1])+abs(a[i+dis-1]-a[i])))
print(min(b))

Submission status: Runtime Error

Solution code:
from sys import stdin

N, K = [int(i) for i in stdin.readline().rstrip().split()]
x = [int(i) for i in stdin.readline().rstrip().split()]

if N == 1:
    print(x[0])
else:
    ans = min((abs(x[0]) + abs(x[K - 1] - x[0])),
              (abs(x[K - 1]) + abs(x[0] - x[K - 1])))

    for i in range(1, len(x) - K):
        tmp = min((abs(x[i]) + abs(x[i + K - 1] - x[i])),
                  (abs(x[i + K - 1]) + abs(x[i] - x[i + K - 1])))
    if tmp < ans:
        ans = tmp

    print(ans)

Submission status: Runtime Error

Solution code:
n,k = map(int, input().split())
list_x = list((map(int, input().split())))
ans = 10**9
for i in range(0,n-k+1):
    prob_list = list_x[i:i+k]
    if list_x[i+k-1] < 0 and list_x[i+k] < 0 and i != n-k-1 :
        continue
    elif list_x[i] > 0 and list_x[i-1] > 0 and i != 0:
        continue
    dist = min(abs(list_x[i]),abs(list_x[i+k-1])) + (list_x[i+k-1] - list_x[i])
    if ans > dist:
        ans = dist
print(ans)

Submission status: Runtime Error

Solution code:
num,point = list(map(int,input().split()))
nums = list(map(int,input().split()))
nums.append(0)
nums.sort()
lis = list(map(abs,nums))
for i in range(num-point-1):
  lis.remove(max(lis))
if lis[0] == lis[point+1]:
  if lis[1] < lis[point]:
    lis.pop(0)
  else:
    lis = lis[:point+1]
else:
  lis.remove(max(lis))
box = [lis[0],lis[point]]
print(min(box)*2+max(box))

Submission status: Runtime Error

Solution code:
num,point = list(map(int,input().split()))
nums = list(map(int,input().split()))
nums.append(0)
nums.sort()
lis = list(map(abs,nums))
print(lis)
for i in range(num-point-1):
  lis.remove(max(lis))
print(lis)
if lis[0] == lis[point+1]:
  if lis[1] < lis[point]:
    lis.pop(0)
  else:
    lis = lis[:point+1]
else:
  lis.remove(max(lis))
box = [lis[0],lis[point]]
print(box)
print(min(box)*2+max(box))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

ans = 0

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

count = inp1[1]
pos = 0

sa = 100000
sa_pos = -1
for i in range(len(inp2)):
    a = abs(pos - inp2[i])
    if a < sa:
        sa = a
        sa_pos = i
    else:
        ans += abs(pos - inp2[i-1])
        break

count -= 1
pos = sa_pos

r = 0
l = 0
while True:
    if count <= 0:
        break
    
    length = len(inp2)
    
    if length-1 >= pos+1:
        r = abs(inp2[pos] - inp2[pos+1])
    else:
        r = 999999
    
    if 0 <= pos-1:
        l = abs(inp2[pos] - inp2[pos-1])
    else:
        l = 999999
        
    del inp2[pos]
    
    if r > l:
        ans += l
        pos -= 1
    else:
        ans += r
        
    count -= 1
    
    
    
    
print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

ans = 0

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

count = inp1[1]
pos = 0

sa = 100000
sa_pos = -1
for i in range(len(inp2)):
    a = abs(pos - inp2[i])
    if a < sa:
        sa = a
        sa_pos = i
    else:
        ans += a
        break

count -= 1
pos = sa_pos

r = 0
l = 0
while True:
    if count <= 0:
        break
    
    length = len(inp2)
    
    if length-1 >= pos+1:
        r = abs(inp2[pos] - inp2[pos+1])
    else:
        r = 999999
    
    if 0 <= pos-1:
        l = abs(inp2[pos] - inp2[pos-1])
    else:
        l = 999999
        
    del inp2[pos]
    
    if r > l:
        ans += l
        pos -= 1
    else:
        ans += r
        
    count -= 1
    
    
    
    
print(ans)

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))

# n, k = 5, 3
# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]

def set_max(ls):
    if len(ls)>0:
        return set([max(ls)])
    else:
        return set()
def set_min(ls):
    if len(ls)>0
        return set([min(ls)])
    else:
        return set()


plus = list(filter(lambda n:n>=0, ls))
minus = list(filter(lambda n:n<=0, ls))

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []
# for i in range(size):
#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))