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: Wrong Answer

Solution code:
def run(n, k, x):
    ri = 0
    li = k-1
    if len(x) == 1:
        return x[0]
    x2 = x
    x = list(map(abs, x))
    for i in range(k, len(x)):
        if x[ri] > x[i]:
            ri = i-k+1
            li = i
        elif x[ri] == x[i]:
            if x[ri+1] > x[i-1]:
                ri = i-k+1
                li = i
        else:
            if x2[li] <= 0:
                return x[ri]
            elif x2[ri] >= 0:
                return x[li]
            elif x[ri] > x[li]:
                return x[ri] + 2*x[li]
            else:
                return 2*x[ri] + x[li]
    if x2[li] <= 0:
        return x[ri]
    elif x2[ri] >= 0:
        return x[li]
    elif x[ri] > x[li]:
        return x[ri] + 2*x[li]
    else:
        return 2*x[ri] + x[li]


def read_line():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    return (n, k, x)


def main():
    n, k, x = read_line()
    print(run(n, k, x))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k = map(int, raw_input().split())
lis = map(int, raw_input().split())
if 0 in lis:
    k-=1
flag=0
#print lis
if k==0:
    print 0
    flag=1
if n==1 and flag==0:
    print abs(lis[0])
    flag=1
if flag==0:
    if lis[0]>0: # all positives
        print lis[k-1]
    elif lis[n-1]<0: #all negatives
        print abs(lis[n-k])
    else:
        idx = -1
        for i in range(1, n-1):
            if lis[i]<0 and lis[i+1]>=0:
                idx = i
                break
        #print idx
        lol =[]
        x=0
        lol.append(abs(lis[idx]))
        for i in range(idx-1,-1,-1):
            lol.append(lol[x]+abs(lis[i]))
            x+=1
        #print lol
        lol1=[]
        x=0
        lol1.append(lis[idx+1])
        for i in range(idx+2,n,1):
            lol1.append(lol1[x]+(lis[i]))
            x+=1
        #print lol1

        lol=lol[::-1]
        lol3=[]
        for i in lol:
            lol3.append(i)
        for i in lol1:
            lol3.append(i)
        #print lol3
        
        ans=10000000000000000000000000000000000000000000
        lol=lol[::-1]


        lol =[]
        x=0
        lol.append(abs(lis[idx]))
        for i in range(idx-1,-1,-1):
            lol.append(abs(lis[i]))
            x+=1
        #print lol
        lol1=[]
        x=0
        lol1.append(lis[idx+1])
        for i in range(idx+2,n,1):
            lol1.append((lis[i]))
            x+=1
        #print lol1
        #print lol
        if k<=len(lol):
            ans=min(ans, lol[k-1])
        if k<=len(lol1):
            ans=min(ans, lol1[k-1])
        for i in range(1,len(lol)):
            fromright=k-i
            if fromright>len(lol1):
                continue
            val=2*lol[i-1]
            val+=(lol1[fromright-1])
            #print "val=",val
            if val<ans:
                ans=val
        
        for i in range(1,len(lol1)):
            fromleft=k-i
            if fromleft>len(lol):
                continue
            val=2*lol1[i-1]
            val+=(lol[fromleft-1])
            #print "val=",val
            if val<ans:
                ans=val
        print ans

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop
n,k=map(int,input().split())
h=[]
X=list(map(int,input().split()))
for x in X:
    if x==0:
        k-=1
        if k==0:
            print(0)
            exit()
    else:
        heappush(h,(abs(x),x))
p=[x for x in X if x>0]
n=[-x for x in X if x<0][::-1]
s=[[]for _ in'12']
for _ in[0]*k:
    c,x=heappop(h)
    s[x>0]+=[c]
    if x==0:
        s[x>=0]+=[c]
if len(s[0])==0:
    print(max(s[1]))
elif len(s[1])==0:
    print(max(s[0]))
else:
    a,b=sorted(map(max,s))
    ans=[b+a*2]
    if len(n)>=k:
        ans+=[max(n[:k])]
    if len(p)>=k:
        ans+=[max(p[:k])]
    print(min(ans))

Submission status: Wrong Answer

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


maxi = 0
maxPoint = 0

if x[0] > 0:
    print(x[K - 1])

elif x[-1] < 0:
    print(abs(x[-K]))

else:
    x = list(map(abs, x))
    for i in range(N - K + 1):
        if abs(x[i]) + abs(x[i + K - 1]) > maxi:
            maxi = abs(x[i]) + abs(x[i + K - 1])
            maxPoint = i

    print(2 * min(x[i], x[i + K - 1]) + max(x[i], x[i + K - 1]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop
n,k=map(int,input().split())
h=[]
X=list(map(int,input().split()))
for x in X:
    if x==0:
        k-=1
        if k==0:
            print(0)
            exit()
    else:
        heappush(h,(abs(x),x))
p=[x for x in X if x>0]
n=[-x for x in X if x<0]
s=[[]for _ in'12']
for _ in[0]*k:
    c,x=heappop(h)
    s[x>0]+=[c]
    if x==0:
        s[x>=0]+=[c]
if len(s[0])==0:
    print(max(s[1]))
elif len(s[1])==0:
    print(max(s[0]))
else:
    a,b=sorted(map(max,s))
    ans=[b+a*2]
    if len(n)>=k:
        ans+=[max(n[:k])]
    if len(p)>=k:
        ans+=[max(p[:k])]
    print(min(ans))

Submission status: Wrong Answer

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

sort_x = sorted(x, key=lambda y: abs(y))[:K]

min_x = min(sort_x)
max_x = max(sort_x)

if min_x * max_x < 0:
  min_dist = min(map(abs, [min_x, max_x]))
  max_dist = max(map(abs, [min_x, max_x]))
  dist = max_dist + min_dist * 2
elif max_x > 0:
  dist = max_x
else:
  dist = abs(min_x)
  
print dist

Submission status: Wrong Answer

Solution code:
input_s = str(input())
[n, k] = list(map(int, input_s.split(' ')))
input_s = str(input())
l_i = list(map(int, input_s.split(' ')))
l_i = sorted(l_i, key=abs)
ta = l_i[:k]
tb = [i for i in ta if i < 0]
tc = [i for i in ta if i >= 0]

result = 0
if len(tb) == 0:
    result = max(tc)
elif len(tc) == 0:
    result = -(min(tb))
elif max(tc) >= -(min(tb)):
    result = max(tc) + -(min(tb)) * 2
else:
    result = max(tc) * 2 + -(min(tb))

print(result)

Submission status: Wrong Answer

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

# print(n,k)
minx = 200000000
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: Wrong Answer

Solution code:
n, k = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]

plus = sum(i > 0 for i in x)
minus = sum(i < 0 for i in x)
zero = x.count(0)

if plus + zero > k:
    x = x[:-(plus + zero - k)]
if minus + zero > k:
    x = x[minus + zero - k:]

min_result = max(abs(x[0]), abs(x[-1]), abs(x[-1] - x[0]))

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

print(min_result)

Submission status: Wrong Answer

Solution code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000

for i in range(N - K):
    part = problem[i:i+K]
    left = part[0]
    right = part[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left) 

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
min_T = float('inf')
if A[0] * A[N-1]>= 0:
    if A[0] >= 0:
        min_A = 0
        max_A = A[K-1]
        min_T = max_A - min_A
    else:
        min_A = A[N-K+1]
        max_A = 0
        min_T = max_A - min_A
else:
    for i in range(N-K):
        min_A = A[i]
        max_A = A[i+K-1]
        min_T = min(min_T, min(-1 * min_A * 2 + max_A, -1 * min_A + 2 * max_A))
        #print(min_T)
print(min_T)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
hidari=0
migi=N-1
for i in range(N-K):
  if abs(X[hidari])>=abs(X[migi]):
    hidari=hidari+1
  else:
    migi=migi-1

if X[hidari]<0 and X[migi]>0:
  if abs(X[hidari])>=abs(X[migi]):
    print(2*abs(X[migi])+abs(X[hidari]))
  else:
    print(2*abs(X[hidari])+abs(X[migi]))
else:
  if abs(X[hidari])>=abs(X[migi]):
    print(abs(X[hidari]))
  else:
    print(abs(X[migi]))

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

ans = 200000000
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: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

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

import copy

            
    
def main(n, k, l):
    cost_l = []
    for i in range(n - k):
        t = [l[i + j] for j in range(k)]
        if t[0] * t[-1] < 0:
            cost_l.append(min(- t[0] * 2 + t[-1], - t[0] + t[-1] * 2))
        elif t[0] >= 0:
            cost_l.append(t[-1])
        elif t[-1] < 0:
            cost_l.append(-t[0])
    try:
        print(min(cost_l))
    except ValueError:
        print(0)
    
    
if __name__ == '__main__':
    try:
        n, k = list(map(int,input().strip().split(" ")))
        l = list(map(int,input().strip().split(" ")))
        main(n, k, l)
    except EOFError:
        pass

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

def solve():
    N, K = map(int, input().split())
    C = np.array(list(map(int,input().split())))
    D = list()
    for i in range(N-K):
      if C[0] > 0 or -C[0] < C[-1]:
        C = C[:-1]
      else:
        C = C[1:]
    l, r = min(min(C),0), max(max(C),0)
    d = -2*l + r if -l < r else  -l + 2*r
    return str(d)

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

Submission status: Wrong Answer

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)):
    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: Wrong Answer

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

ans = - v[0] + 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: Wrong Answer

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 + k - 1]))
	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: Wrong Answer

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

# print(n,k)
minx = 200000
if k==1:
    x = list(map(abs,x))
    print(min(x))
else:
    r = 0
    l = k-1
    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: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
m = 10**8
for i in range(N-K+1):
    if x[i] >= 0:
        t = x[i+K-1]
    elif x[i+K-1] <= 0:
        t = -x[i]
    elif -x[i] <= x[i+K-1]:
        t = -x[i]*2 + x[i+K-1]
    else:
        t = -x[i] + x[i+K-1]*2
    if t < m:
        m = t
print(m)

Submission status: Wrong Answer

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 ** 12
    for i in range(n-k+1):
        if c[i+k-1] <= 0:
            t = abs(c[i+k-1]-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: Wrong Answer

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

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

mini = 9223372036854775807
for i in range(n - k +1):
	if xlist[i] <= 0 and xlist[i + k - 1] <= 0:
		mini = min(mini, (xlist[i + k - 1]))
	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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
hidari=0
migi=N-1
for i in range(N-K):
  if abs(X[hidari])>=abs(X[migi]):
    hidari=hidari+1
  else:
    migi=migi-1

if abs(X[hidari])>=abs(X[migi]):
  print(2*abs(X[migi])+abs(X[hidari]))
else:
  print(2*abs(X[hidari])+abs(X[migi]))

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k = map(int, input().split(" "))
data1 = np.array(list(map(int, input().split(" "))))

ind = np.argsort(np.abs(data1))
d2 = np.array([data1[ind[i]] for i in range(k)])

t = sum([abs(d2[i] - d2[i-1]) if i!=0 else abs(d2[i]) for i in range(k)])
print(t)

Submission status: Wrong Answer

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

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

for i in range(n - k + 1):
    s = v[i]
    g = v[i + k - 1]
    if s * 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: Wrong Answer

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

import bisect
zleft = bisect.bisect_left(x, 0)
zright = bisect.bisect_right(x, 0)
znum = zright - zleft

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

print(ans)

Submission status: Wrong Answer

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

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

    if k == 1:
        print(min(map(abs, xs)))
        return

    time = 1e9
    for i in range(0, n - k):
        part = xs[i:i + k]
        r = abs(part[0] - part[-1])
        time = min(time, r + abs(part[0]), r + abs(part[-1]))

    print(time)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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: Wrong Answer

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 = r + 2*abs(l)
    elif r == 0:
        cost = abs(l)
    else:
        cost = abs(l)
    if cost < ans:
        ans = cost
    

print(ans)

Submission status: Wrong Answer

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

zero = len(xs) - 1
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):
    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: Wrong Answer

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):
  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 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: Wrong Answer

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):
    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: Wrong Answer

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

dist = 1000000000
for index, value in enumerate(x):
    if index > (N - K):
        break
    cur_dist = 0
    now = 0
    if value < 0:
        cur_dist += value * (-1)
        now = value
    for j in x[index:index+K]:
        cur_dist += (j - now)
        now = j
    if cur_dist <= dist:
        dist = cur_dist

print(dist)

Submission status: Wrong Answer

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):
        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: Wrong Answer

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(fans==100000000000000000000000000000):
    if(arr[0]<0 and arr[n-1]<=0):
        fans=min(fans,abs(arr[0]))
    elif(arr[0]<0 and arr[n-1]>0):
        if(abs(arr[0])>=arr[n-1]):
            fans=min(fans,abs(arr[0])+(arr[n-1]*2))
        else:
            fans=min(fans,(abs(arr[0])*2)+arr[n-1])
    else:
        fans=min(fans,arr[n-1])
print(fans)

Submission status: Wrong Answer

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]))
for j in range(0,c):
  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: Wrong Answer

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

min_cost = 10 ** 15

if N == K:
    print(X[N-1] - X[0] + min(abs(X[N-1]), abs(X[0])))
else:
    for i in range(N-K):
        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: Wrong Answer

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

zero = len(xs) - 1
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])


candidate = 1000000000
for i in range(k):
    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]
        )

if candidate == 1000000000:
    print(0)
else:
    print(candidate)

Submission status: Wrong Answer

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)):
    jogen=abs(x[i+k])
    kagen=abs(x[i])
    ans=min(ans,jogen*2+kagen,kagen*2+jogen)
print(ans)

Submission status: Wrong Answer

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] + abs(x[i-K+1]) for i in range(K-1, N))
print(m)

Submission status: Wrong Answer

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[-1])
if N2 >= K:
    ans = min(ans, x2[-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: Wrong Answer

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]))
print(ans)

Submission status: Wrong Answer

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

zero = len(xs) - 1
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])


candidate = 1000000000
for i in range(k):
    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: Wrong Answer

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:
    ans=min(ans,abs(s)*2+l,abs(s)+l*2)
  else:
    ans=min(ans,l)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
fans=10000000000000000000
for i in range(0,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]>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(fans==10000000000000000000):
    fans=0
print(fans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

def solve():
    N, K = map(int, input().split())
    C = np.array(list(map(int,input().split())))
    D = list()
    for i in range(N-K+1):
      c = C[i:i+K]
      l, r = max(c), min(c)
      d = -2*r + l if l > r else  -r + 2*l
      D.append(d)
    return str(min(D))

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
sum=0
xx=[]
if x[0]<0 and x[n-1]<0 or x[0]>=0 and x[n-1]>=0:
    print(x[k-1])
else:
    for i in range(n):
        xx.append(abs(x[i]))
    xx.sort()
    for i in range(k):
        sum+=xx[i]
    print(sum)

Submission status: Wrong Answer

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

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

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: Wrong Answer

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 = 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: Wrong Answer

Solution code:
a, b = map(int, input().split())
s = list(map(int,input().split( )))
c=a-b
e = 10000000000000000000000
if a==b:
  e=s[b-1]-s[0]+min(abs(s[b-1]),abs(s[0]))
for j in range(0,c):
  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: Wrong Answer

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

m = 1 << 128

if N == K:
    m = sum(x)
else:
    for i in range(0, N - K):
        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: Wrong Answer

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

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

for i in range(n-k+1):
    le=X[i]
    ri=X[i+k-1]
    if le*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: Wrong Answer

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 = r + 2*abs(l)
    else:
        cost = abs(l)
    if cost < ans:
        ans = cost

print(ans)

Submission status: Wrong Answer

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

cand = []

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

print(min(cand))

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop
n,k=map(int,input().split())
h=[]
t=[[]for _ in'12']
for x in map(int,input().split()):
    heappush(h,(abs(x),x))
    t[x>0]+=[x]
    if x==0:
        t[x>=0]+=[x]
s=[[]for _ in'12']
for _ in[0]*k:
    c,x=heappop(h)
    s[x>0]+=[c]
    if x==0:
        s[x>=0]+=[c]
if len(s[0])==0:
    print(max(s[1]))
elif len(s[1])==0:
    print(max(s[0]))
else:
    a,b=sorted(map(max,s))
    ans=[b+a*2]
    if len(t[0])>=k:
        ans+=[max(t[0][:k])]
    if len(t[1])>=k:
        ans+=[max(t[1][:k])]
    print(min(ans))