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: Accepted

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




if x[0] <= 0 <= x[-1]:
    ans = []
    for i in range(N-K+1):
        left = x[i]
        right = x[i+K-1]
        if left <= 0 <= right:
            cost = abs(right - left) + min(abs(right),abs(left))
            ans.append(cost)
        else:
            pass
    print(min(ans))

else:
    ans = []
    for i in range(N-K+1):
        left = x[i]
        right = x[i+K-1]
        cost = abs(right - left) + min(abs(right),abs(left))
        ans.append(cost)
    print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

min = 10e8

for i in range(N-K+1):
  if ro[i] * ro[i+K-1] < 0:
    if abs(ro[i]) < abs(ro[i+K-1]):
      hoho = abs(ro[i])
    else:
      hoho = abs(ro[i+K-1])
    ans = abs(ro[i]) + ro[i+K-1] + hoho
  else:
    ans = max(abs(ro[i]), abs(ro[i+K-1]))
    
  if ans < min:
    min = ans
    
print(min)

Submission status: Accepted

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

zerofrag = 0
zerobig = []
zerosmall = []

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

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

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

Submission status: Accepted

Solution code:
# encoding: utf-8
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))

# find position of Mr.Sunuke
for i_snk, xi in enumerate(x):
    if xi >= 0: break

# (left, right) * K patterns, left + right = K
min_dist = (x[-1] - x[0]) * K
for left in range(K + 1):
    right = K - left
    # skip
    # print("#", i_snk, left, right)
    if i_snk - left < 0: continue
    elif right + i_snk > N: continue
    # calc dist
    left_x = x[i_snk - left]
    right_x = x[i_snk + right - 1]
    tocorner = int(min(math.fabs(left_x), math.fabs(right_x)))
    side2side = right_x - left_x
    # print("##", i_snk, left_x, right_x, tocorner + side2side)
    if tocorner + side2side < min_dist: min_dist = tocorner + side2side

print(min_dist)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n,k=[int(x) for x in input().split()]
x=[0]
for xi in input().split():
    xi=int(xi)
    if xi ==0:
        k-=1
    else:
        x.append(xi)
x=sorted(x)
ans=10**10

zero=x.index(0)
for i in range(max(0,zero-k-1),min(zero+1,n-k+1)):
    if x[i+k]<0 or x[i]>0:
        continue
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout

input = lambda: stdin.readline()
write = stdout.write

def main():
    N, K = map(int, input().split())
    X = tuple(map(int, input().split()))

    org = 0 - X[0]
    x = [0] * N
    for i, n in enumerate(X):
        x[i] = n + org

    ans = float('inf')
    for i in range(N - K + 1):
        dist = x[i+K-1] - x[i] + min(abs(org - x[i]), abs(x[i+K-1] - org))
        ans = min(ans, dist)
    print(ans)

main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
xl = []
xr = []
for i in x:
    if i > 0:
        xr.append(i)
    elif i < 0:
        xl.append(i)
xl = [0] + list(reversed(xl))
xr = [0] + xr

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

for i in range(max(0, k - (len(xl) - 1)), min(len(xr), k + 1)):
    # i:　ろうそくの数
    ans = min(ans, 2 * xr[i] + abs(xl[k - i]))
for i in range(max(0, k - (len(xr) - 1)), min(len(xl), k + 1)):
    ans = min(ans, 2 * abs(xl[i]) + abs(xr[k - i]))
print(ans)

Submission status: Accepted

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

xm=[0]
if zi<len(x) and x[zi]==0:
    xp=[]
else:
    xp=[0]

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

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

Submission status: Accepted

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

import bisect
zero = bisect.bisect_left(X, 0)

left = max(zero - K, 0)
ans = float('inf')
while 1:
    right = left+K-1
    if left > zero + 1 or len(X) <= right: break
    lv, rv = X[left], X[right]
    if lv * rv > 0:
        ans = min(ans, max(abs(rv), abs(lv)))
    else:
        ans = min(ans, min(rv*2 - lv, rv - lv*2))
    left += 1

print(ans)

Submission status: Accepted

Solution code:
import sys

a = input().split()
n = int(a[0])
k = int(a[1])

xlist = input().split()
xlist = [int(i) for i in xlist]

mini = sys.maxsize
for i in range(n - k +1):
	if xlist[i] <= 0 and xlist[i + k - 1] <= 0:
		mini = min(mini, (-xlist[i]))
	elif xlist[i] <= 0 and xlist[i + k - 1] >= 0:
		mini = min(mini,(min(-2*xlist[i] + xlist[i + k - 1], -xlist[i] + 2 * xlist[i + k - 1])))
	elif xlist[i] >= 0 and xlist[i + k - 1] >= 0:
		mini = min(mini, (xlist[i + k - 1]))
		
print (mini)

Submission status: Accepted

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

# print(n,k)
minx = 300000000
if k==1:
    x = list(map(abs,x))
    print(min(x))
else:
    for idx in range(n-k+1):
        if x[idx]*x[idx+k-1] < 0 :
            sumx = min(abs(x[idx]),abs(x[idx+k-1])) + abs(x[idx])+abs(x[idx+k-1])
        else :
            sumx = max(abs(x[idx]),abs(x[idx+k-1]))
        if sumx < minx:
            minx = sumx
        # print(x[idx:idx+k], minx)

    print(minx)

Submission status: Accepted

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


def main():
    n, k = map(int, input().split())
    c = list(map(int, input().split()))

    ans = 10 ** 15
    for i in range(n-k+1):
        if c[i+k-1] <= 0:
            t = abs(c[i])
        elif c[i] < 0 < c[i+k-1]:
            t = min(abs(c[i]), abs(c[i+k-1])) + abs(c[i+k-1]-c[i])
        else:
            t = c[i+k-1]

        ans = min(ans, t)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = - v[0] * 2 + v[-1] * 2

for i in range(n - k + 1):
    s = v[i]
    g = v[i + k - 1]
    if s > 0 or g < 0:
        if s < 0:
            m = - s
        else:
            m = g
    else:
        m = min(-s * 2 + g, -s + 2 * g)
    ans = min(ans, m)
print(ans)

Submission status: Accepted

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

import bisect

x=bisect.bisect_right(C,0)
#最初左に行く場合
if x>=K:
    ANS=-(C[x-K])
else:
    ANS=10**10


for i in range(1,min(x+1,K+1)):
    if x-i+K-1>=N:
        continue

    
    
    elif ANS>=C[x-i+K-1]-2*C[x-i]:
        ANS=C[x-i+K-1]-2*C[x-i]


#print(ANS)


CC=[None]*N
for i in range(N):
    CC[i]=-C[i]

CC.reverse()


x=bisect.bisect_right(CC,0)

if x>=K:
    if ANS>-(CC[x-K]):
        ANS=-(CC[x-K])



#print(ANS)

for i in range(1,min(x+1,K)):
    if x-i+K-1>=N:
        continue

    
    
    elif ANS>=CC[x-i+K-1]-2*CC[x-i]:
        ANS=CC[x-i+K-1]-2*CC[x-i]







    #print(x,i,CC[x-i+K-1],CC[x-i])

print(ANS)

Submission status: Accepted

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

x.insert(0, 0)

minimum = 2**31

for left in range(1, N-K+2):
    right = left + K - 1
    lefttime = abs(x[left]) + abs(x[right] - x[left])
    righttime = abs(x[right]) + abs(x[right] - x[left])
    minimum = min(minimum, min(lefttime, righttime))
    
print(minimum)

Submission status: Accepted

Solution code:
from itertools import accumulate

import sys
sdin = sys.stdin.readline

n,k = map(int, sdin().split())
x = list(map(int, sdin().split()))
 
# x1を0とする累積和をとる
x_0 = [xi - x[0] for xi in x]
x0_cum = list(accumulate(x_0))

# K個のろうそくを含む
ans = float('inf')
for i in range(n-k+1):
    ans_cand = x[i+k-1] - x[i] + min(abs(x[i]), abs(x[i+k-1]))

    ans = min(ans_cand, ans)
    
    
print(ans)

Submission status: Accepted

Solution code:
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

N, K = getMN()
candles = getlist()

shift = N - K + 1
ans = 400000000
for i in range(shift):
    l, r = candles[i] ,candles[i+K-1]
    if r > 0:
        if l >= 0:
            cost = r
        else:
            cost = min(r + 2*abs(l), 2*r + abs(l))
    elif r == 0:
        cost = abs(l)
    else:
        cost = abs(l)
    if cost < ans:
        ans = cost


print(ans)

Submission status: Accepted

Solution code:
num,k = map(int, input().split())
moves=[int(x) for x in input().split()]

def findMinPath(nums,k):
    if 0 in nums:
        k-=1
    if k==0:
        return 0
    left=[];right=[]
    for n in nums:
        if n<0:
            left.append(abs(n))
        elif n>0:
            right.append(n)
    left[:]=left[::-1]
    left.insert(0,0)
    right.insert(0,0)

    lsize=len(left);rsize=len(right)
    res=max(2*left[-1]+right[-1],2*right[-1]+left[-1])
    for i in range(k+1):
        j=k-i
        if i<lsize and k-i<rsize:
            tmpMax=min(2*left[i]+right[j],2*right[j]+left[i])
            res=min(res,tmpMax)
    return res

print(findMinPath(moves,k))

Submission status: Accepted

Solution code:
n,k = list(map(int,input().split()))
d = list(map(int, input().split()))
d_m = []
d_p = [i for i in d if i >= 0 or d_m.append(i)]
ret = -1
for i in range(k+1):
  if len(d_p) >= i and len(d_m) >= k-i:
    dp_t = d_p[i-1] if i > 0 else 0
    dm_t = -1 * d_m[-(k-i)] if k-i != 0 else 0
    if dp_t == 0:
      ret_t = dm_t
    elif dm_t == 0:
      ret_t = dp_t
    else:
      ret_t = dp_t + dm_t + min(dp_t, dm_t)
#    print(i, k-i, dp_t, dm_t, ret_t)
    if ret > ret_t or ret < 0:
      ret = ret_t
print(ret)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
X = list(map(int, input().split()))
ans = 1E20
for i in range(N-K+1):
  ans = min(ans, X[i+K-1]-X[i]+abs(X[i]))
  ans = min(ans, X[i+K-1]-X[i]+abs(X[i+K-1]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

zero = len(xs)
for i in range(len(xs)):
    if xs[i] >= 0:
        zero = i
        break

dic_positive = {0: 0}
dic_negative = {0: 0}
for i in range(len(xs) - zero):
    dic_positive[i + 1] = xs[zero + i]

for i in range(zero):
    dic_negative[i + 1] = abs(xs[zero - i - 1])


init_max = 1000000000000000
candidate = init_max
for i in range(k + 1):
    if dic_negative.get(i) is not None and dic_positive.get(k - i) is not None:
        candidate = min(
            candidate,
            dic_negative[i] + dic_negative[i] + dic_positive[k - i],
            dic_negative[i] + dic_positive[k - i] + dic_positive[k - i]
        )

print(candidate)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
row = list(map(int,input().split()))
p = 10000000000000000
i = 0
j = 1
while i + k <= n:
  if(row[i]*row[i+k-1] < 0):
    l = abs(row[i])
    r = abs(row[i+k-1])
    ma = max(l,r)
    mi = min(l,r)
    t = mi*2 + ma

    if(t < p):
      p = t
  elif(row[i] < 0):
    t = abs(row[i])
    if(t < p):
      p = t
  else:
    t = row[i+k-1]
    if(t < p):
      p = t
  i += 1
print(p)

Submission status: Accepted

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

min_cost = 10 ** 20

if N == K:
    print(X[N-1] - X[0] + min(abs(X[N-1]), abs(X[0])))
else:
    for i in range(N-K+1):
        min_cost = min(min_cost, X[i + K - 1] - X[i] + min(abs(X[i + K - 1]), abs(X[i])))
    print(min_cost)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    x = [int(x) for x in stdin.readline().rstrip().split()]
    x1 = [-i for i in x if i < 0][::-1]
    x2 = [i for i in x if i >= 0]
    x1 = [0] + x1
    x2 = [0] + x2
    result = 10000000000
    for l in range(min(K + 1, len(x1))):
        r = K - l
        if len(x2) <= r:
            continue
        tmp = x1[l] * 2 + x2[r]
        result = min(result, tmp)
    for r in range(min(K + 1, len(x2))):
        l = K - r
        if len(x1) <= l:
            continue
        tmp = x1[l] + x2[r] * 2
        result = min(result, tmp)
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = float('inf')
for l in range(N-K+1):
    r = l + K - 1
    if src[l] > 0:
        ans = min(ans, src[r])
    elif src[r] < 0:
        ans = min(ans, -src[l])
    else:
        ans = min(ans, src[r]*2 - src[l], -src[l]*2 + src[r])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

plus = -1
for i in range(n):
    if a[i] > 0:
        plus = i
        break
    plus = n    
record = 10**10
for i in range(k+1):
    if i>plus or k-i>n-plus:
        continue
    if i == 0:
        record = a[plus+k-i-1]
    elif i==k:
        r = -a[plus-i]
        if r<record:
            record = r
    else:
        u  = a[plus+k-i-1]
        v = -a[plus-i]
        r1 = 2*u+v
        r2 = 2*v+u
        if r1<record:
            record = r1
        if r2<record:
            record = r2
            
print(record)

Submission status: Accepted

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

x1 = []
x2 = []
for i in range(N):
    if x[i] >= 0:
        x1.append(x[i])
    else:
        x2.append(-1 * x[i])
x2 = x2[::-1]

N1 = len(x1)
N2 = len(x2)

ans = float('inf')

if N1 >= K:
    ans = min(ans, x1[K - 1])
if N2 >= K:
    ans = min(ans, x2[K - 1])

for i in range(K - 1):
    if i < N1 and K - i - 2 < N2:
        d1 = 2 * x1[i] + x2[K - i - 2]
        d2 = x1[i] + 2 * x2[K - i - 2]
        ans = min(ans, d1, d2)
print(ans)

Submission status: Accepted

Solution code:
n,k=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
ans=10**9
for i in range(n-k+1):
  s=a[i]
  l=a[i+k-1]
  if s<0 and l>=0:
    ans=min(ans,abs(s)*2+l,abs(s)+l*2)
  elif l<=0:
    ans=min(ans,abs(s))
  else:
    ans=min(ans,l)
print(ans)

Submission status: Accepted

Solution code:
import sys

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

minus_x = []
plus_x = []

for x in X:
    if(x < 0):
        minus_x.append(x)
    elif(x > 0):
        plus_x.append(x)
    else:
        N -= 1
        K -= 1

n_minus = len(minus_x)
n_plus = len(plus_x)

if(K == 0):
    print(0)
    sys.exit()

ans = sys.maxsize
for i in range(K + 1):
    m = i
    p = K - i
    if(m > n_minus or p > n_plus):
        continue

    if(p == 0):
        tmp = -1 * minus_x[-1 * m]
    elif(m == 0):
        tmp = plus_x[p - 1]
    else:
        t_m = -1 * minus_x[-1 * m]
        t_p = plus_x[p - 1]
        tmp = min(t_m * 2 + t_p, t_m + t_p * 2)

    if(tmp < ans):
        ans = tmp

print(ans)

Submission status: Accepted

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

mn = 1000000000
for i in range(N - K + 1):
	x1 = X[i]
	x2 = X[i + K - 1]
	if x1 >= 0:
		m = x2
	elif x2 <= 0:
		m = - x1
	else:
		m = x2 - x1 + min(x2, -x1)

	if mn > m:
		mn = m

print(mn)

Submission status: Accepted

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

minti=abs(X[0])*2+abs(X[n-1])*2

for i in range(n-k+1):
    le=X[i]
    ri=X[i+k-1]
    if (le>=0 and ri>=0) or (le<=0 and ri<=0):
        ti=max(abs(le),abs(ri))
    else:
        ti=min(abs(le),abs(ri))*2+max(abs(le),abs(ri))
    minti=min(ti,minti)

print(minti)

Submission status: Accepted

Solution code:
# sys.stdin.readline()
import sys
input = sys.stdin.readline

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

x_p = [0] + [p for p in x if p > 0]#正
x_z = x.count(0)#0
x_n = [(-1) * ng for ng in x if ng < 0]#負
x_n = [0] + sorted(x_n)#0から始まる絶対値

myK = k - x_z#左右に移動して点火するろうそく数

if myK <= 0:
    print(0)
else:
    ans = float('inf')
    for i in range(myK + 1):
        if 0 <= i < len(x_p) and 0 <= myK - i < len(x_n):
            ans = min(ans, x_p[i] + x_n[myK - i] + min(x_p[i], x_n[myK - i]))
    print(ans)

Submission status: Accepted

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

ans = 10**9

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy

from math import *

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    x_list1 = []

    for i in range(N):
        x = int(input_raw[i])
        x_list1.append(x)


    min_dist = sys.maxint
    for i in range(N - K + 1):
        L = i;
        R = i + K - 1
        if x_list1[L] >= 0 and x_list1[R] >= 0:
            dist = x_list1[R]
        elif x_list1[L] <= 0 and x_list1[R] <= 0:
            dist = abs(x_list1[L])
        else:
            dist = min(2*x_list1[R] - x_list1[L], x_list1[R] - 2 * x_list1[L])
        if min_dist > dist:
            min_dist = dist
    print min_dist

if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = 1 << 128

for i in range(0, N - K + 1):
    a = x[i]
    b = x[i + K - 1]

    if a < 0 < b:
        if abs(a) > abs(b):
            cost = abs(b) * 2 + abs(a)
        else:
            cost = abs(a) * 2 + abs(b)
    else:
        cost = max(abs(a), abs(b))

    m = min(m, cost)

print m

Submission status: Accepted

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

cand = []

for i in range(N - K + 1):
    cand.append(min(abs(X[i]), abs(X[i + K - 1])) + abs(X[i + K - 1] - X[i]))

print(min(cand))

Submission status: Accepted

Solution code:
import numpy as np
import math


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

X = np.zeros(N, dtype=int)

for n in range(N):
    X[n] = ina[n]


D = np.zeros(N - K+1, dtype=int)

for i in range(N-K+1):
    if X[i] > 0:
        D[i] = X[i+K-1]
    elif X[i+K-1] < 0:
        D[i] = abs(X[i])
    else:
        D[i] = min(abs(X[i]), abs(X[i+K-1])) + X[i+K-1] - X[i]

# print(D)
        
print(min(D))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
line = input().split()
n = int(line[0])
k = int(line[1])
x = [int(i) for i in input().split()]


ret = 9**10
for i in range(n-k+1):
    mins = x[i]
    maxs = x[i+k-1]
    if mins<0 and maxs>0:
        if abs(mins)<maxs:
            r = abs(mins)*2+maxs
        else:
            r = maxs*2+abs(mins)
    elif maxs<=0:
        r = abs(mins)
    else:
        r = maxs

    if r<ret:
        ret = r

print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

N, K = map(int, raw_input().split())

x = map(int, raw_input().split())

ans = 10 ** 18

for i in range(N - K + 1):
    p1 = x[i]
    p2 = x[i + K - 1]
    if p1 * p2 >= 0:
        ans = min(ans, max(abs(p1), abs(p2)))
    else:
        ans = min(ans, abs(p1) + abs(p2) + min(abs(p1), abs(p2)))

print ans

Submission status: Accepted

Solution code:
N,  K = map(int, input().split())
candles = list(map(int, input().split()))
ans = float('inf')
for i in range(N - K + 1):
    if candles[i] * candles[i + K - 1] >= 0:
        t = max(abs(candles[i]), abs(candles[i + K - 1]))
    else:
        t = candles[i + K - 1] - candles[i] + min(abs(candles[i]), abs(candles[i + K - 1]))
    ans = min(ans, t)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(input().split()))
ls = []
for i in range(n-k):
  if l[i] <0 and l[i+k]<0:
    ls.append(abs(l[i]))
  elif l[i] >0 and l[i+k]>0:
    ls.append(abs(l[i+k]))
  elif l[i] <0 and l[i+k]>0:
    ls.append(2*min(abs(l[i]),l[i+k]) + max(abs(l[i]),l[i+k]))
print(min(ls))

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
l = list(map(int,input().split()))
ls = []
for i in range(n-k):
  if l[i] <0 and l[i+k]<0:
    ls.append(abs(l[i]))
  elif l[i] >0 and l[i+k]>0:
    ls.append(abs(l[i+k]))
  elif l[i] <0 and l[i+k]>0:
    ls.append(2*min(abs(l[i]),l[i+k]) + max(abs(l[i]),l[i+k]))
print(min(ls))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
positive=[]
negative=[]
for i in range(N):
    if x[i]<0:
        negative.append(-x[i])
    else:
        positive.append(x[i])
negative.sort()
n=len(negative)
m=len(positive)
ans=1000000000
if n>=K:
    ans=min(ans,negative[K-1])
if m>=K:
    ans=min(ans,positive[K-1])

for i in range(n):
    j=K-(i+1)
    if j<=0:
        continue
    ans=min(ans,negative[i]+positive[j-1]+min(negative[i],positive[j-1]))
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
pc = min(K,lp)
pp = xp[pc-1]
if K>lp:
    nc = K-lp
    np = -xn[nc-1]
else:
    nc = 0
    np = 0
ans = pp + np + min(pp,np)
for i in range(pc-1):
    if nc == ln:
        break
    pp = xp[pc-i-2]
    np = -xn[nc]
    nc += 1
    ans = min(ans,pp + np + min(pp,np))
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
pc = min(K,lp)
pp = xp[pc-1]
if K>lp:
    nc = K-lp
    np = -xn[nc-1]
else:
    nc = 0
    np = 0
ans = pp + np + min(pp,np)
for i in range(pc):
    if nc == ln:
        break
    pp = xp[pc-i-2]
    np = -xn[nc]
    nc += 1
    ans = min(ans,pp + np + min(pp,np))
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
xlist=list(map(int, input().split()))
num0=0
nump=0
numm=0
for i in xlist:
  if i>0:
    nump+=1
  elif i<0:
    numm+=1
  else:
    num0+=1
listp=[0]*(nump+1)
listm=[0]*(numm+1)
for i in range(1,nump+1):
  listp[i]=xlist[numm+num0+i-1]
for i in range(1,numm+1):
  listm[i]=-xlist[numm-i]
cnt=k-num0
ans=10**30
for i in range(cnt):
  if i<=numm and cnt-i<=nump:
    ans=min(ans,listm[i]+listp[cnt-i]+min(listm[i],listp[cnt-i]))
if numm==0 and nump==0:
  ans=0
elif numm==0:
  ans=listp[cnt+1]
elif nump==0:
  ans=listm[cnt+1]
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int, input().split())
xlist=list(map(int, input().split()))
num0=0
nump=0
numm=0
for i in xlist:
  if i>0:
    nump+=1
  elif i<0:
    numm+=1
  else:
    num0+=1
listp=[0]*(nump+1)
listm=[0]*(numm+1)
for i in range(1,nump+1):
  listp[i]=xlist[numm+num0+i-1]
for i in range(1,numm+1):
  listm[i]=-xlist[numm-i]
cnt=k-num0
ans=10**30
for i in range(cnt):
  if i<=numm and cnt-i<=nump:
    ans=min(ans,listm[i]+listp[cnt-i]+min(listm[i],listp[cnt-i]))
if numm==0:
  ans=listp[cnt+1]
if nump==0:
  ans=listm[cnt+1]
print(ans)

Submission status: Runtime Error

Solution code:
あ

Submission status: Runtime Error

Solution code:
import sys
N,K = map(int,input().split())
lsx = list(map(int,input().split()))
if 0 in lsx:
    K = K-1
    lsx.remove(0)
if bool(lsx) == False or K == 0:
    print(0)
    sys.exit()
lshu = []
for i in range(len(lsx)):
    if lsx[i] < 0:
        lshu.append(lsx[i])
    else:
        sei = i
        break
lssei = lsx[sei:]
lenhu = len(lshu)
lensei = len(lssei)
lshu2 = [abs(lshu[i]) for i in range(lenhu-1,-1,-1)]
dishu = [0] + lshu2
dissei = [0] + lssei
print(dissei,dishu)
ans = 10*10**8
for i in range(K):
    if i <= lenhu and K-i <= lensei:
        ans = min(ans,2*dishu[i]+dissei[K-i],dishu[i]+2*dissei[K-i])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
N,K = map(int,input().split())
lsx = list(map(int,input().split()))
if 0 in lsx:
    K = K-1
    lsx.remove(0)
if bool(lsx) == False or K == 0:
    print(0)
    sys.exit()
lshu = []
for i in range(N):
    if lsx[i] < 0:
        lshu.append(lsx[i])
    else:
        sei = i
        break
lssei = lsx[sei:]
lenhu = len(lshu)
lensei = len(lssei)
lshu2 = [abs(lshu[i]) for i in range(lenhu-1,-1,-1)]
dishu = [0] + lshu2
dissei = [0] + lssei
ans = 10*10**8
for i in range(K):
    if i <= lenhu and K-i <= lensei:
        ans = min(ans,2*dishu[i]+dissei[K-i],dishu[i]+2*dissei[K-i])
print(ans)