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:
n, k = map(int, input().split())
x = list(map(int, input().split()))
INF = 2 * 1e8

def dfs(time, fire, total, tmp):
    if fire == k:
        return total

    elif time >= n:
        return INF

    else:
        ren1 = dfs(time + 1, fire + 1, total + abs(tmp - x[time]), x[time] )
        ren2 = dfs(time + 1, fire, total, tmp)
        return min(ren1, ren2)

print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
INF = 2 * 1e8

def dfs(time, fire, total, tmp):
    if fire == k or time == n:
        return total if fire == k else INF

    else:
        ren1 = dfs(time + 1, fire + 1, total + abs(tmp - x[time]), x[time] )
        ren2 = dfs(time + 1, fire, total, tmp)

        return min(ren1, ren2)

print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

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

def dfs(time, fire, total, tmp):
    if fire == k or time == n:
        return total if fire == k else float("INF")

    else:
        ren1 = dfs(time + 1, fire + 1, total + abs(tmp - x[time]), x[time] )
        ren2 = dfs(time + 1, fire, total, tmp)

        return min(ren1, ren2)

print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

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

def dfs(time, fire, total, tmp):
    if fire == k or time == n:
        return total if fire == k else float("INF")

    else:
        ren1 = dfs(time + 1, fire + 1, total + abs(tmp - x[time]), x[time] )
        ren2 = dfs(time + 1, fire, total, tmp)

        return min(ren1, ren2)

print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

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

dist = []
for i in range(N-K+1): 
    l, r = x[i], x[i+k-1]
    t = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    dist.append(t)

print(min(dist))

Submission status: Runtime Error

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

dist = []
for i in range(N-K+1):
    a = 0
    tmp = x[i:i+K]
    mini, maxi = tmp[0], tmp[-1]
    if (mini >= 0 and maxi >= 0):
        a = maxi
    elif (mini < 0 and maxi < 0):
        a = abs(mixi)
    if mini < 0 and maxi >= 0:
        a = abs(mixi) + maxi + min(abs(mixi), maxi)
    dist.append(a)

print(min(dist))

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:
    over_zero = i
    break
else:
  over_zero = n

min_cost = 10**8
#プラス側でトライ
if over_zero + k - 1 < n:
  min_cost = X[over_zero + k - 1]
#マイナス側でトライ
if over_zero - k >= 0:
  min_cost = min(X[over_zero - k], min_cost)
for i in range(0, k+1):
  #マイナスサーチ
  if over_zero - i >= 0:
    minus_cost = -(X[over_zero - i])
  else:
    continue
  #プラスサーチ
  if over_zero + k - i - 1 < n:
    plus_cost = (X[over_zero + k - i - 1])
  else:
    continue
  cost = plus_cost + minus_cost + min(plus_cost, minus_cost) 
  min_cost = min(cost, min_cost)

print(min_cost)

Submission status: Runtime Error

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

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)
    
if n == k:
  print(min(r[-1], -l[0])*2+max(r[-1], -l[0]))
  exit()

if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  print(i)
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)
print(ts)
print(min(ts))

Submission status: Runtime Error

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

l = []
r = []
for i in range(n):
  x = xs[i]
  if x < 0:
    l.append(x)
  else:
    r.append(x)

if l == []:
  print(r[k-1])
  exit()
elif r == []:
  print(-l[-k])
  exit()
  
ts = []
for i in range(max(k-len(l), 0), min(len(r), k+1)):
  if i == 0:
    t = -l[-k]
  elif i == k:
    t = r[k-1]
  else:
    t = min(r[i-1], -l[-(k-i)])*2+max(r[i-1], -l[-(k-i)])
  ts.append(t)
#print(ts)
print(min(ts))

Submission status: Runtime Error

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

if N == 1:
    print(abs(X[0]))
    exit()


flg = False

for x in X:
    if x < 0:
        flg = True

if flg is False:
    print(X[K - 1])
    exit()


def sol(lft, rgt):
    if X[lft] < 0 and X[rgt] < 0:
        return float('inf')
    elif X[lft] <= 0:
        return min(2 * abs(X[lft]) + X[rgt], abs(X[lft]) + 2 * X[rgt])
    else:
        return float('inf')


lft, rgt = 0, 0
ans = float('inf')
while True:

    if rgt == N:
        break

    if rgt - lft + 1 < K:
        rgt += 1
        if rgt - lft + 1 == K:
            ans = min(ans, sol(lft, rgt))
            lft += 1
print(ans)

Submission status: Runtime Error

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

c = 0
ris = 0
lenn = len(n)
for i, name in enumerate(n[::-1]):
    if name < 0:
        c += 1
    if c == b-1:
        ri = i
        rs = name
        ris = lenn - ri - 1
        break

ans = []
ans_sum = []
for i in range(b+1):
    if ris+b+i > lenn:
        break
    iii = n[(ris+i):(ris+b+i)]
    ans.append(iii)
    lln = iii[0]
    rrn = iii[-1]
    ll = abs(lln)
    rr = abs(rrn)
    if lln >=0 and rrn > 0:
        ans_sum.append(rr)
    elif lln <0 and rrn <=0:
        ans_sum.append(ll)
    elif ll <= rr:
        ans_sum.append(2*ll + rr )
    else:
        ans_sum.append(ll + 2*rr)

print(min(ans_sum))

Submission status: Runtime Error

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

c = 0
ris = 0
lenn = len(n)
for i, name in enumerate(n[::-1]):
    if name < 0:
        c += 1
    if c == b-1:
        ri = i
        rs = name
        ris = lenn - ri - 1
        break

ans = []
ans_sum = []
for i in range(b+1):
    if ris+b+i > lenn:
        break
    iii = n[(ris+i):(ris+b+i)]
    ans.append(iii)
    lln = iii[0]
    rrn = iii[1]
    ll = abs(lln)
    rr = abs(rrn)
    if ll >=0 and rr > 0:
        ans_sum.append(rr)
    elif ll <0 and rr <=0:
        ans_sum.append(ll)
    elif ll <= rr:
        ans_sum.append(2*ll + rr )
    else:
        ans_sum.append(ll + 2*rr)

print(min(ans_sum))

Submission status: Runtime Error

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    ans = float("inf")
    for l, r in zip(X, X[k - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import os, sys, re, math

N,K = map(int,input().split(' '))

candle = list(map(int,input().split(' ')))
ret = math.inf

for i in range(N-K+1):
    s = candle[i]
    e = candle[i+K-1]
    if e < 0:
        ret = min(-s, ret)
    elif s > 0:
        ret = min(e, ret)
    else:
        ret = min(-s*2+e, -s+e*2, ret)

print(ret)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

input = open(sys.argv[1],"r").readline

def main():
    N,K = map(int, input().split())
    X = list(map(int, input().split()))
    ans = 3*10**8
    for i in range(0,N-K+1):
        l = X[i]
        r = X[i+K-1]
        if l*r < 0:
            ans = min(ans, 2*min(abs(l),abs(r)) + max(abs(l),abs(r)))
        else:
            ans = min(ans, max(abs(l),abs(r)))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
line = raw_input()
N, K = [int(s) for s in line.split(' ')]
line = raw_input()
Xs = [int(s) for s in line.split(' ')]
pXs = [x for x in Xs if x > 0]
mXs = [x * -1 for x in Xs if x < 0 ]
mXs.reverse()
count = 0
time = 0
ret = []
if 0 in Xs:
    time = 0
    count = 1
    if K == 1:
        ret.append(0)
if len(pXs) > K - count:
    ret.append(pXs[K-count - 1])
if len(mXs) > K - count:
    ret.append(mXs[K-count - 1])
for n in range(0, len(pXs)):
    m = (K - count) - (n + 1) - 1
    print n, m
    if m >= len(mXs) or m < 0:
        continue
    ret.append(pXs[n] * 2 + mXs[m])
    ret.append(pXs[n] + mXs[m] * 2)
# print pXs
# print mXs
# print ret
print min(ret)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = sorted(map(int, input().split()))
ret = 1000000000000000000
for s in range(n - k + 1):
    e = s + k - 1
    if x[e] <= 0:
	ret = min(ret, -x[s])
    elif x[s] <= 0:
	ret = min(ret, x[e] - x[s] + min(-x[s], x[e]))
    else:
        ret = min(ret, x[e])
print(ret)

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):
  ans.append(X[i+k-1]-X[i]+min(abs(X[i+k+1]),abs(X[i])))
print(min(ans))

Submission status: Runtime Error

Solution code:
N,K=[int(s) for s in input().split()]
ls_p=[int(s) for s in input().split()]
ls_n=[]
X=0
for i in range(N):
  if ls_p[0]>=0 : break 
  else:
    X+=1
    ls_n.append(ls_p[0])
    ls_p.pop(0)
#ls_nにX個流れている
cand=[]
for i in range(min([X-1,K-2])):
  cand.append(-ls_n[-i-1]*2+ls_p[K-i-2])
  cand.append(-ls_n[-i-1]+ls_p[K-i-2]*2)
if N-X-1>=K-1:
  cand.append(ls_p[K-1])
if X>=K:
  cand.append(-ls_n[-K])
print(min(cand))

Submission status: Runtime Error

Solution code:
N,K=[int(s) for s in input().split()]
ls_p=[int(s) for s in input().split()]
ls_n=[]
X=0
for i in range(N):
  if ls_p[0]>=0 : break 
  else:
    X+=1
    ls_n.append(ls_p[0])
    ls_p.pop(0)
print(ls_n)
print(ls_p)
#ls_nにX個流れている
cand=[]
for i in range(min([X-1,K-2])):
  cand.append(-ls_n[-i-1]*2+ls_p[K-i-2])
  cand.append(-ls_n[-i-1]+ls_p[K-i-2]*2)
if N-X-1>=K-1:
  cand.append(ls_p[K-1])
if X>=K:
  cand.append(-ls_n[-K])
print(min(cand))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = list(int(i) for i in input().split())
inf = 10**9
max_x = x[-1]
min_x = x[1]
if min_x >= 0:
    print(x[k-1])
elif max_x <= 0:
    print(x[-k])
else:
    for i in range(n-1):
        if x[i]<0 and x[i+1]>=0:
            key = i+1
            break
    if n-key>=k:
        ans1 = sum(x[key:key+k])
    else:
        ans1 = inf
    if key>=k:
        ans2 = abs(sum(x[key-k:key]))
    else:
        ans2 = inf
    ans3 = inf
    for i in range(max(0, key-k+1), min(n-k, key+k-1)):
        a, b = abs(x[i]), abs(x[i+k-1])
        t = max([a, b])+min([a, b])*2
        if t < ans3:
            ans3 = t
    print(min([ans1, ans2, ans3]))

Submission status: Runtime Error

Solution code:


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

left = []
right = []

# ある意味もとから累積和
for c in X:
    if c < 0:
        left.append(abs(c*2))
    else:
        right.append(c)

# 番兵
left.append(0)
left.reverse()
#print(left, right)


# i 個、左から取る
ans = float('inf')
for i in range(len(left)):
    remain = K-i

    # 個数足りない
    if len(right[:remain]) < remain:
        continue

    x = left[i] + right[K-i-1]
#    print(x, left[i], right[K-i-1])
    ans = min(ans, x)

print(ans)

Submission status: Runtime Error

Solution code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
if len(a) > k - cost:
    ret = min(ret, cost_a[k - cost])
if len(b) > k - cost:
    ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if 0 <= k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if 0 <= k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)

Submission status: Runtime Error

Solution code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
if len(a) >= k - cost:
    ret = min(ret, cost_a[k - cost])
if len(b) >= k - cost:
    ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if 0 <= k - i < len(cost_b):
        try:
            ret = min(ret, 2 * cost_a[i] + cost_b[k - i])
        except Exception:
            pass

for i in range(len(cost_b)):
    if 0 <= k - i < len(cost_a):
        try:
            ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
        except Exception:
            pass
print(ret)

Submission status: Runtime Error

Solution code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
if len(a) >= k - cost:
    ret = min(ret, cost_a[k - cost])
if len(b) >= k - cost:
    ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if 0 <= k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if 0 <= k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)

Submission status: Runtime Error

Solution code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
# if len(a) >= k - cost:
#     ret = min(ret, cost_a[k - cost])
# if len(b) >= k - cost:
#     ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)

Submission status: Runtime Error

Solution code:
import bisect


n, k = map(int, input().split())
xs = list(map(int, input().split()))
index = bisect.bisect_right(xs, -1)

a = xs[:index][::-1]
cost = 0
if xs[index] == 0:
    cost += 1
    b = xs[index + 1:]
else:
    b = xs[index:]

cost_a = [0]
cost_b = [0]
for c in a:
    cost_a.append(abs(c))
for c in b:
    cost_b.append(c)

ret = float('inf')
if len(a) >= k - cost:
    ret = min(ret, cost_a[k - cost])
if len(b) >= k - cost:
    ret = min(ret, cost_b[k - cost])

for i in range(len(cost_a)):
    if k - i < len(cost_b):
        ret = min(ret, 2 * cost_a[i] + cost_b[k - i])

for i in range(len(cost_b)):
    if k - i < len(cost_a):
        ret = min(ret, 2 * cost_b[i] + cost_a[k - i])
print(ret)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
time=float'inf'
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  time=min(time,abs(l)+r-l,abs(r)+r-l)
print(time)

Submission status: Runtime Error

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

ans = 0
for i in range(n):
  d = x[i+k-1] - x[i] + min(abs(x[i]), abs(x[i+k-1]))
  ans = min(ans, d)
  
print(ans)

Submission status: Runtime Error

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

if k == 1:
  print(min(list(map(abs, x))))
  
else:

  if 0 in x:
    k -= 1
  ans = 10**9

  x_p = list(filter(lambda x: x > 0, x))
  x_n = list(filter(lambda x: x < 0, x))

  if len(x_p) >= k and k != 0:
    ans = min(ans, x_p[k-1])
  if len(x_n) >= k and k != 0:
    ans = min(ans, -x_n[-k])
  
  for i in range(n):
    if x[i] < 0 and x[i+k-1] > 0:
      d = x[i+k-1] - x[i] + min(-x[i], x[i+k-1])
      ans = min(ans, d)
    
  print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
p = n
for i in range(n):
  if x[i] > 0:
    p = i
    break
result = float('inf')
for i in range(max(p - k, 0), min(k - p, k + 1)):
  if i == 0:
    result = min(result, x[p + k - 1])
  elif i == k:
    result = min(result, -x[p - k])
  else:
    l = x[p - i]
    r = x[p + (k - i) - 1]
    result = min(result, r - 2 * l, r * 2 - l)
print(result)

Submission status: Runtime Error

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

# X = list(map(int, input().split()))

with open("test.txt", 'r') as f:
    N, K = map(int, f.readline().split())
    X = list(map(int, f.readline().split()))

def calc_time(x_max, x_min):
    if x_max * x_min >= 0:
        if abs(x_max) > abs(x_min):
            return x_max
        else:
            return abs(x_min)
    else:
        return x_max - x_min + min(abs(x_max), abs(x_min))

def calc_start_and_goal_position(index):
    if index - K >= 0:
        start_index = index - K
    else:
        start_index = 0
    
    if N > index + K + 1:
        goal_index = index + 1
    else:
        goal_index = N - K + 1
    
    return start_index, goal_index

if __name__ == "__main__":
    index = -1
    for i, x in enumerate(X):
        if x >= 0:
            index = i 
            break
    if index == -1:
        index = i
    start_index, goal_index = calc_start_and_goal_position(index)

    time_min = float('inf')
    for i in range(start_index, goal_index):
        time = calc_time(X[i+K-1], X[i])
        if time < time_min:
            time_min = time
    print(time_min)

Submission status: Runtime Error

Solution code:
def solve():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    dists = []
    for i in range(n - k):
        part = xs[i:i + k]
        dist = min(abs(part[0]) + abs(part[0] - part[-1]), abs(part[-1]) + abs(part[0] - part[-1]))
        dists.append(dist)
    return min(dists)


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

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
m=0
zf=0
for i in range(n):
    if l[i]==0:
        zf=1
if zf==0:
    l.append(0)
    l.sort()
else:
    k-=1
if k==0:
    print(0)
    exit()
n=len(l)
pl=[]
nl=[]
for i in range(n):
    if l[i]==0:
        m=i
    elif l[i]>0:
        pl.append(l[i])
    else:
        nl.append(-l[i])
        
ans=1e10
for i in range(min(k-1,len(pl))):
    a=len(nl)-k+i+1
    if a>=0 and a<len(nl):
        ans=min(ans,2*pl[i]+nl[len(nl)-k-i+1])
for i in range(min(k-1,len(nl))):
    a=len(nl)-i-1
    b=k-i-2
    if b>=0 and b<len(pl):
        ans=min(ans,2*nl[a]+pl[k-i-2])
if k<=len(pl):
    ans=min(ans,pl[k-1])
if k<=len(nl):
    ans=min(ans,nl[len(nl)-k])
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
m=0
zf=0
for i in range(n):
    if l[i]==0:
        zf=1
if zf==0:
    l.append(0)
    l.sort()
else:
    k-=1
    
n=len(l)
pl=[]
nl=[]
for i in range(n):
    if l[i]==0:
        m=i
    elif l[i]>0:
        pl.append(l[i])
    else:
        nl.append(-l[i])
        
ans=1e10
for i in range(min(k,len(pl))):
    a=len(nl)-k-i+1
    if a>=0 and a<len(nl):
        ans=min(ans,2*pl[i]+nl[len(nl)-k-i+1])
for i in range(min(k,len(nl))):
    a=len(nl)-i-1
    b=k-i-2
    if b>=0 and b<len(pl):
        ans=min(ans,2*nl[a]+pl[k-i-2])
if k<=len(pl):
    ans=min(ans,pl[k-1])
if k<=len(nl):
    ans=min(ans,nl[len(nl)-k])
print(ans)

Submission status: Runtime Error

Solution code:
X,Y = map(int,input().split())
whole = []
for i in range(X):
  now = input()
  if now == '.' * Y:
    pass
  else:
    add = []
    for i in range(Y):
      if now[i] == '.':
        add.append(0)
      else:
        add.append(1)
    whole.append(add)
j = 0
while j < len(whole[0]):
  for i in range(len(whole)):
    if whole[i][j] == 1:
      j += 1
      break
  else:
    for i in range(len(whole)):
      del whole[i][j]
for i in range(len(whole)):
  now = whole[i]
  ans = ''
  for j in range(len(now)):
    if now[j] == 0:
      ans += '.'
    else:
      ans += '#'
  print(ans)

Submission status: Runtime Error

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

split_idx = None
for i,x in enumerate(x_list):
    if x >= 0:
        split_idx = i
        break
if split_idx == None:
    lefts = x_list
    rights = []
else:
    lefts = x_list[:split_idx]
    rights = x_list[split_idx:]

if rights[0]==0:
    K -= 1
    rights = rights[1:]

if K == 0:
    print(0)
else:
    min_time = 10**9
    for left_cnt in range(K+1):
        right_cnt = K-left_cnt
        if left_cnt > len(lefts) or right_cnt > len(rights):
            continue
        if left_cnt ==0 and right_cnt == 0:
            tmp_time = 0
        elif left_cnt == 0:
            tmp_time = rights[right_cnt-1]
        elif right_cnt == 0:
            tmp_time = -lefts[-left_cnt]
        elif -lefts[-left_cnt] >= rights[right_cnt-1]:
            tmp_time = rights[right_cnt-1]*2 + (-lefts[-left_cnt])
        else:
            tmp_time = rights[right_cnt-1] + (-lefts[-left_cnt]) * 2
        if tmp_time < min_time:
            min_time = tmp_time
    print(min_time)

Submission status: Runtime Error

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

split_idx = None
for i,x in enumerate(x_list):
    if x >= 0:
        split_idx = i
        break
if split_idx == None:
    lefts = x_list
    rights = []
else:
    lefts = x_list[:split_idx]
    rights = x_list[split_idx:]

if rights[0]==0:
    K -= 1
    rights = rights[1:]

if K == 0:
    print(0)
else:
    min_time = 10**9
    for left_cnt in range(K):
        right_cnt = K-left_cnt
        if left_cnt > len(lefts) or right_cnt > len(rights):
            continue
        if left_cnt ==0 and right_cnt == 0:
            tmp_time = 0
        elif left_cnt == 0:
            tmp_time = rights[right_cnt-1]
        elif right_cnt == 0:
            tmp_time = lefts[-left_cnt]
        elif -lefts[-left_cnt] >= rights[right_cnt-1]:
            tmp_time = rights[right_cnt-1]*2 + (-lefts[-left_cnt])
        else:
            tmp_time = rights[right_cnt-1] + (-lefts[-left_cnt]) * 2
        if tmp_time < min_time:
            min_time = tmp_time
    print(min_time)

Submission status: Runtime Error

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N, K = (int(_) for _ in input().split())
    x = list(int(_) for _ in input().split())
    ans = INF
    for i in range(N-K+1):
        l = i
        r = i + K-1
        ans = min(abs(x[l]) + abs(x[l]-x[r]),ans)
        
    for i in range(N-K+1):
        l = N-K+1-i
        r = N-1-i
        ans = min(abs(x[r]) + abs(x[l]-x[r]),ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n, k = getints()
xs = list(sorted(getints()))

lower = list(filter(lambda x: x < 0, xs))
upper = list(filter(lambda x: x > 0, xs))

if xs.count(0) > 0:
    k -= 1

lower = list(reversed(lower))

res = 1 << 28
for i in range(-1, len(lower)):
    need = k - (i + 1)
    if need - 1 >= len(upper):
        continue
    side1 = abs(0 if i < 0 else lower[i])
    side2 = upper[need - 1]
    res = min(res, side1 + side2 + min(side1, side2))

print(res)

Submission status: Runtime Error

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

for i in range(K):
    minNum = candles[i]
    maxNum = candles[i+K-1]

Submission status: Runtime Error

Solution code:
import sys

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

tmp = sys.maxsize

for i in range(K):
    minNum = candles[i]
    maxNum = candles[i+K-1]

print(tmp)

Submission status: Runtime Error

Solution code:
import sys

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

tmp = sys.maxsize

for i in range(K):
    minNum = candles[i]
    maxNum = candles[i+K-1]
    if maxNum < 0:
        ans = abs(minNum)
    elif (minNum < 0) and (0 <= maxNum):
        if abs(minNum) <= abs(maxNum):
            ans = 2*abs(minNum) + maxNum
        else:
            ans = 2*maxNum + abs(minNum)
    elif 0 <= minNum:
        ans = maxNum
    tmp = min(tmp,ans)

print(tmp)

Submission status: Runtime Error

Solution code:
import sys

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

tmp = sys.maxsize

for i in range(K):
    minNum = candles[i]
    maxNum = candles[i+K-1]
    if maxNum < 0:
        ans = minNum
    elif (minNum < 0) and (0 <= maxNum):
        if abs(minNum) <= abs(maxNum):
            ans = 2*abs(minNum) + maxNum
        else:
            ans = 2*maxNum + abs(minNum)
    elif 0 <= minNum:
        ans = maxNum
    tmp = min(tmp,ans)

print(tmp)

Submission status: Runtime Error

Solution code:
import sys

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

tmp = sys.maxsize

for i in range(K):
    minNum = candles[i]
    maxNum = candles[i+K]
    if maxNum < 0:
        ans = minNum
    elif (minNum < 0) and (0 <= maxNum):
        if abs(minNum) <= abs(maxNum):
            ans = 2*abs(minNum) + maxNum
        else:
            ans = 2*maxNum + abs(minNum)
    elif 0 <= minNum:
        ans = maxNum
    tmp = min(tmp,ans)

print(tmp)

Submission status: Runtime Error

Solution code:
import sys

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

tmp = sys.maxsize

for i in range(K):
    minNum = candles[i]
    maxNum = candles[i+K]
    if minNum >= 0 and maxNum >= 0:
        ans = maxNum
    elif maxNum >= 0 and minNum < 0:
        a = 2 * abs(minNum) + maxNum
        b = 2 * maxNum + abs(minNum)
        ans = min(a,b)
    elif maxNum < 0 and minNum < 0:
        ans = abs(minNum)
    tmp = min(ans, tmp)

print(tmp)

Submission status: Runtime Error

Solution code:
import bisect

n,k= map(int,input().split())
x = [int(i) for i in input().split()]

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

print(x[idx])

print(ans)
exit()

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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n,k= map(int,input().split())
x = [int(i) for i in input().split()]

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

if x[idx] == 0:
    print(ans)

print(ans)
exit()

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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n,k= map(int,input().split())
x = [int(i) for i in input().split()]

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

if x[idx] == 0 and idx - k + 1 >= 0:
    print(ans)

print(ans)
exit()

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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n,k= map(int,input().split())
x = [int(i) for i in input().split()]

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

if x[idx] == 0 and idx - k + 1 >= 0:
    ans = min(ans,ans)

print(ans)
exit()

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

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

n,k= map(int,input().split())
x = [int(i) for i in input().split()]

idx = bisect.bisect_left(x,0)
"""
if x[idx] == 0:
    x.insert(idx,0)
"""

ans = 10**100

if x[idx] == 0 and idx - k + 1 >= 0:
    ans = min(ans,-x[0])

print(ans)
exit()

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

print(ans)