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

Solution (Python):
import itertools

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

pos = [0]
neg = []
for i in x:
    if i<0:
        neg.append(-i)
    else:
        pos.append(i)
neg = [0] + neg[::-1]


neg_twice = []
for i in range(min(len(neg),k)):
    neg_twice.append(neg[i]*2)
for i in range(len(neg_twice)):
    if k-i<len(pos):
        neg_twice[i] += pos[k-i]
    else:
        neg_twice[i] = float('inf')

pos_twice = []
for i in range(min(len(pos),k)):
    pos_twice.append(pos[i]*2)
for i in range(len(pos_twice)):
    if k-i<len(neg):
        pos_twice[i] += neg[k-i]
    else:
        pos_twice[i] = float('inf')
print(min(pos_twice+neg_twice))

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

Solution (Python):
N, K = map(int, input().split())
x_coordinate = list(map(int, input().split()))
zero_pos = 0
distance = []

while True:
  try:
    if x_coordinate[zero_pos] >= 0:
      break
    else:
      zero_pos += 1
  except:
    zero_pos = N - 1
    break

for i in range(zero_pos - K, zero_pos + 1):
  if (i >= 0) and (i + K - 1 < N):
    start_pos = x_coordinate[i]
    end_pos = x_coordinate[i + K - 1]
    if start_pos*end_pos >= 0:
      if abs(start_pos) > abs(end_pos):
        distance.append(abs(start_pos))
      else:
        distance.append(abs(end_pos))
    else:
      if abs(start_pos) >= abs(end_pos):
        total = abs(start_pos) + 2*abs(end_pos)
        distance.append(total)
      else:
        total = 2*abs(start_pos) + abs(end_pos)
        distance.append(total)
      
print(min(distance))

Solution (Python):
n,k=list(map(int, input().split()))
x=list(map(int, input().split()))
t=0
tmin=2*(x[n-1]-x[0])
for i in range (0,n-k+1):
    if x[i]>=0:
        t=x[i+k-1]
    else:
        if x[i+k-1]<=0:
            t=abs(x[i])
        else:
            if -1*x[i]<x[i+k-1]:
                t=-2*x[i]+x[i+k-1]
            else:
                t=-1*x[i]+2*x[i+k-1]
    if t<tmin:
        tmin=t
print(tmin)

Solution (Python):
def make_time(l, k):
	r = []
	for i in range(k-1, len(l)):
		r.append(l[i] - l[i-k+1])
	return r

if __name__ == '__main__':
	N, K  = list(map(int, input().split()))
	x = list(map(int, input().split()))
	x_time = make_time(x, K)
	times1 = [(x_time[i] + abs(x[i])) for i in range(N - K + 1)]
	x.reverse()
	x_time.reverse()
	times2 = [(x_time[i] + abs(x[i])) for i in range(N - K + 1)]
	times = times1 + times2
	print(min(times))

Solution (Python):


def getMin(X, K):
    minVal = float('inf')
    for l in range(len(X)-K+1):        
        minVal = min(minVal, abs(X[l]) + abs(X[K + l - 1] - X[l]))
        minVal = min(minVal, abs(X[K + l - 1]) + abs(X[l] - X[K + l - 1]))
    return minVal

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

Solution (Python):
str = input()
n,k = [int(x) for x in str.split()]
str = input()
list = [int(x) for x in str.split()]
ans = 1000000000
for i in range(0,n - k + 1):
	a = list[i]
	b = list[i + k - 1]
	if (a < 0) & (b > 0): 
	    ans = min(ans,min(b - a * 2,b * 2 - a))
	else:
		ans = min(ans,max(abs(a),abs(b)))
print(ans)

Solution (Python):
import math
N,K = map(int, input().split())
a = list(map(int,input().split()))
l = 0
r = K - 1
Min = float("inf")
for i in range(N - K + 1):
    if(a[l]*a[r]<=0):
        Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    elif(l == 0 and r == N-1):
        Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))        
    elif(l==0):
        if(a[r]*a[r+1]<0 or a[0]>0 or (a[N-1]<0 and r == N-1)):
            Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    elif(r==N-1):
        if(a[l-1]*a[l]<0 or (a[l]>0 and l == 0) or a[r] <= 0):
            Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    else:
        if(a[r]*a[r+1]<0 or a[l-1]*a[l]<0):
            Min = min(Min,abs(a[l])+abs(a[l]-a[r]),abs(a[r])+abs(a[r]-a[l]))
    l = l + 1
    r = r + 1
print(Min)

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 1e9

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

Solution (Python):



def main():
    n, k = list(map(int, input().split()))
    x = list(map(int, input().split()))
    ans = float('inf')

    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]
        abs_left = abs(left)
        abs_right = abs(right)

        if left * right >= 0:
            ans = min(ans, max(abs_left, abs_right))
        else:
            ans = min(ans, 2 * abs_left + abs_right, abs_left + 2 * abs_right)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    n, k = list(map(int, input().split()))
    x = list(map(int, input().split()))
    ans = float('inf')

    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]

        if left * right >= 0:
            ans = min(ans, max(abs(left), abs(right)))
        else:
            ans = min(ans, 2 * abs(left) + abs(right), abs(left) + 2 * abs(right))

    print(ans)


if __name__ == '__main__':
    main()

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

if 0 in a:
  K = K - 1
  if K == 0:
    print('0')
    exit()

aLow = list(filter(lambda x: x < 0, a))
aLow = list(map(lambda x: abs(x), aLow))
aLow.sort()

aHigh = list(filter(lambda x: x > 0, a))

least = 99999999999999999




for i in range(0,K+1):
  j = K - i
  if i > len(aLow) or j > len(aHigh):
    continue

  if i == 0:
    dist = aHigh[j-1]
  elif j == 0:
    dist = aLow[i-1]
  else:
    dist = min(2*aLow[i-1]+aHigh[j-1], aLow[i-1]+2*aHigh[j-1])

  if least > dist:
    least = dist

print(least)

Solution (Python):
import sys

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

ans = sys.maxsize

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

    ans = min(ans, max(abs(a), abs(b)) + min(abs(a), abs(b)) * 2 * (a * b < 0))

print(ans)

Solution (Python):
N,K = map(int,input().split(" "))
array = list(map(int,input().split(" ")))

minus = 0
while minus < N and array[minus]<0:
    minus += 1

first,last = max(minus-K,0),min(minus,N-K)

ans = 2*(array[N-1]- array[0])

for i in range(first,last+1):
    m_dist, p_dist = array[i], array[K-1+i]
    if m_dist*p_dist <=0:
        dist_i = min(abs(m_dist),abs(p_dist))*2+max(abs(m_dist),p_dist)
    else:
        dist_i = max(abs(m_dist),abs(p_dist))
    ans = min(ans,dist_i)

print(ans)

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

Solution (Python):
n, k = input().split()
n = int(n)
k = int(k)
xs = input().split()
xs = [int(x) for x in xs]
ans = 1e9
for i in range(0, n-k+1):
    l = xs[i]
    r = xs[i + k - 1]
    if l < 0 and r > 0:
        ans = min(ans, abs(l) + 2 * r)
        ans = min(ans, abs(l) * 2 + r)
    else:
        ans = min(ans, max(abs(l), abs(r)))
print(ans)

Solution (Python):
n, k = input().split()
n = int(n)
k = int(k)
xs = input().split()
xs = [int(x) for x in xs]
ans = 1e9
for i in range(0, n-k+1):
    l = xs[i]
    r = xs[i + k - 1]
    if l < 0 and r > 0:
        ans = min(ans, abs(l) + 2 * r)
        ans = min(ans, abs(l) * 2 + r)
    else:
        ans = min(ans, max(abs(l), abs(r)))
print(ans)

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

Solution (Python):
n, k = map(int, input().split())
a = tuple(map(int, input().split()))
ans = a[n - 1] - a[0] + abs(a[0])
for i in range(n - k + 1):
    r = i + k - 1
    ans = min(ans, abs(a[i]) + a[r] - a[i], abs(a[r]) + a[r] - a[i])
print(ans)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
tmp = []
for i in range(n-k+1):
    tmp.append(abs(x[i])+abs(x[i+k-1]-x[i]))
x_2 = x
x_2.reverse()

for j in range(n-k+1):
    tmp.append(abs(x_2[j])+abs(x_2[j+k-1]-x_2[j]))
print(min(tmp))

Solution (Python):
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = pow(10, 10)
for i in range(N-K+1):
    temp_l = abs(x[i]) + abs(x[i]-x[i+K-1])
    temp_r = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans, temp_l, temp_r)
print(ans)

Solution (Python):
N,K=map(int,input().split())
x=list(map(int,input().split()))
ans=10**9
for i in range(N-K+1):
    tmp1=abs(x[i])+abs(x[i]-x[i+K-1])
    tmp2=abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans=min([ans,tmp1,tmp2])
print(ans)

Solution (Python):

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

def distance(left, right):
    if right < 0:
        return abs(left)
    elif left > 0:
        return right
    else:
        return -left + right + min(-left, right)

min_dist = float('inf')
for left in range(N):
    right = left + K - 1
    if right >= N:
        break
    dist = distance(x[left], x[right])
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Solution (Python):
n,k = map(int,input().split())
x = list(map(int,input().split()))
ans = []
for i in range(n):
    if (i + k -1) >= n:
        break
    x1 = x[i]
    x2 = x[i+k-1]
    ans1 = abs(x2) + abs(x2-x1)
    ans2 = abs(x1) + abs(x1-x2)
    ans.append(ans1)
    ans.append(ans2)
print(min(ans))

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

Solution (Python):
N, K =list(map(int, input().split()))
x=list(map(int, input().split()))
ans=10000000000000
for i in range(N-K+1):
    if min(abs(x[i])+abs(x[i+K-1]-x[i]), abs(x[i+K-1])+abs(x[i+K-1]-x[i]))<ans:
        ans=min(abs(x[i])+abs(x[i+K-1]-x[i]), abs(x[i+K-1])+abs(x[i+K-1]-x[i]))
print(ans)

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

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

ans = 10000000000010

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

print(ans)

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

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

if not len(dist) == 0:
    print(min(dist))
else:
    print(0)

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

dist = float('inf')
for i in range(n-(k-1)):
    l = x[i]
    r = x[i+k-1]
    temp = min(abs(l), abs(r)) + abs(l-r)
    dist = min(dist, temp)

print(dist)

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

for center in range(n):
    if x[center] >= 0:
        break

if center == n - 1:
    print(-1 * x[n - k])
    exit()
elif center == 0:
    print(x[k - 1])
    exit()

plus = [0] + x[center:]
minus = [0] + list(reversed(list(map(lambda i:i * -1, x[:center]))))

ans = 10 ** 10

for i in range(max(k - len(minus) + 1, 0), min(k + 1, len(plus))):
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - i]))

print(ans)

Solution (Python):
N,K = map(int,raw_input().split())
a = map(int,raw_input().split())
b = []
for i in range(N-K+1):
  k_1 = a[i+K-1]-a[i]+abs(a[i])
  k_2 = a[i+K-1]-a[i]+abs(a[i+K-1])
  if k_1<k_2:
    b.append(k_1)
  else:
    b.append(k_2)
print(min(b))

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







distance = []
for i in range(n-k+1):
    distance.append(abs(x[i]) + abs(x[k+i-1]-x[i]))
    distance.append(abs(x[k+i-1]) + abs(x[i]-x[k+i-1]))

if not len(distance) == 0:
    print(min(distance))
else:
    print(0)

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

tmin = float('inf')

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

print(tmin)

Solution (Python):
def main():
    """
    1 <= N <= 10^5
    1 <= K <= N
    |xi| <= 10^8
        -10^8 <= xi <= 10^8
    xs: sorted
    """
    N, K = map(int, input().split())
    *x, = map(int, input().split())
    
    ans = editorial(N, K, x)
    print(ans)


def editorial(N, K, x):
    from bisect import bisect_left
    try:
        
        
        
        idx = x.index(0)
        left = max(idx - K + 1, 0)
        right = min(idx + 1, N - K)
    except:
        
        
        
        idx = bisect_left(x, 0)
        left = max(idx - K, 0)
        right = min(idx + 1, N - K)

    _range = range(left, right + 1)
    
    ans = float("inf")
    for left in _range:
        right = left + K - 1
        
        a1 = abs(x[left]) + abs(x[right] - x[left])
        a2 = abs(x[right]) + abs(x[left] - x[right])
        ans = min([ans, a1, a2])

    return ans


if __name__ == '__main__':
    main()

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

m = 99999999999999999999
for l in range(n-k+1):
    r = l + k - 1
    m = min(m, abs(x[l]) + abs(x[r] - x[l]))
    m = min(m, abs(x[r]) + abs(x[l] - x[r]))
print(m)

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

X.sort()
Ans = 10**20
for i in range(N-K+1):
    l = X[i]
    r = X[i+K-1]
    
    if l*r >= 0:
        ans = max(abs(l),(r))
    
    else:
        ans = max(abs(l),abs(r)) + min(abs(l),abs(r))*2
    Ans = min(Ans,ans)

print(Ans)

Solution (Python):
N, K = map(int, input().split())
x = [int(xi) for xi in input().split()]
best = float('inf')
for i in range(N - K + 1):
    start = x[i]
    end = x[i+K-1]
    near = min(start, end)
    if end <= 0:
        dist = abs(start)
    elif 0 <= start:
        dist = abs(end)
    else:
        if abs(start) < abs(end):
            dist = abs(start) * 2 + abs(end)
        else:
            dist = abs(end) * 2 + abs(start)
    best = min(dist, best)
if best == float('inf'):
    print(0)
else:
    print(best)

Solution (Python):
n,k,*x=map(int,open(0).read().split());print(min(min(abs(x[i]),abs(x[i+k-1]))+x[i+k-1]-x[i]for i in range(n-k+1)))

Solution (Python):
import math
n, k = map(int, input().split())
x_tmp = input().split()
x = [int(_) for _ in x_tmp]
ans = []


def abs(x):
    '''絶対値をint型で返す
    return int-type abs-x
    '''
    return int(math.fabs(x))


for i in range(n-k+1):
    ans_left = abs(x[i]) + abs(x[i+k-1] - x[i])
    ans_right = abs(x[i+k-1]) + abs(x[i+k-1] - x[i])
    ans.extend([ans_left, ans_right])
print(min(ans))

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

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

ans = abs(pos[0])+abs(pos[0]-pos[k-1])

for i in range(n-k+1):
    val = 0
    val += abs(pos[i] - pos[i+k-1])
    val += min(abs(pos[i]), abs(pos[i+k-1]))

    if val < ans:
        ans = val
print(ans)

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

Solution (Python):


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

ans_list = []
for i in range(N-K+1):
    l = xi[i]
    r = xi[i+K-1]
    if l*r < 0:
        a = abs(l)*2 + abs(r)
        b = abs(r)*2 + abs(l)
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif l < 0:
        ans_list.append(abs(l))
    else: 
        ans_list.append(abs(r))

print(min(ans_list))

Solution (Python):


INF = float('inf')


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


N, K = inpl()
X = inpl()

A = [0]
B = []
for i in range(N):
    if X[i] >= 0:
        A.append(X[i])
    else:
        B.append(-X[i])
B.append(0)
B = B[::-1]

ans = INF
for i in range(K + 1):
    cost = INF
    j = K - i
    if i > len(A) - 1 or j > len(B) - 1: continue

    cost = A[i] + B[j] + min(A[i], B[j])
    ans = min(ans, cost)

print(ans)

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

lpos = 0
rpos = K-1

near = abs(xlist[lpos]) if (abs(xlist[lpos]) < abs(xlist[rpos])) else abs(xlist[rpos])
bet = abs(xlist[lpos] - xlist[rpos])

ans = bet + near

for i in range(N-K):
    lpos += 1
    rpos += 1

    near = abs(xlist[lpos]) if (abs(xlist[lpos]) < abs(xlist[rpos])) else abs(xlist[rpos])
    bet = abs(xlist[lpos] - xlist[rpos])

    tmp = bet+near

    if tmp < ans:
        ans = tmp


print(ans)

Solution (Python):



N,K  = map(int,input().split())
moto = input().split()
for i in range(N):
    moto[i] = int(moto[i])

def real(n):
    if n >= 0:
        return n 
    else:
        return -1*n 

l = []
for i in range(N-K+1):
    l.append(real(moto[i]) + real(moto[i+K-1]-moto[i]))
    l.append(real(moto[i+K-1])+real(moto[i+K-1]-moto[i]))
print(min(l))

Solution (Python):


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


if X.count(0) == 1:
    n -= 1
    k -= 1
    X.remove(0)


if X == []:
    print(0)
else:
    
    minus = list(filter(lambda x : x < 0, X ))
    plus = list(filter(lambda x : x > 0, X ))
    
    
    t =[]
    for i in range(k+1):
        
        if len(minus) < i or len(plus) < k-i:
            t.append(float("inf"))
        else:
            
            if i == 0:
                t.append(plus[k-1])
            
            elif i == k:
                t.append(-minus[-i])
            
            else:
                t.append(min(-minus[-i], plus[k-i-1])*2 + max(-minus[-i], plus[k-i-1]))
    
    print(min(t))

Solution (Python):
N,K = map(int, input().split())
x = list(map(int, input().split()))
score = float('inf')
for i in range(N - K + 1):
  l = x[i]
  r = x[i + K - 1]
  score = min(score, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(score)

Solution (Python):


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

t = float('inf')

for i in range(n - k + 1):
    l, r = x[i], x[i + k - 1]
    t = min(t, r - l + min(abs(r), abs(l)))

print(t)

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

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

answer=10**9

for i in range(N-K+1):
  left=candle[i]
  right=candle[i+K-1]
  move=max(abs(left),abs(right))
  if left*right<0:
    move+=min(abs(left),abs(right))*2
  answer=min(move,answer)

print(answer)
  

'''
getcandle=0
for i in range(N):
  if candle[i]>=0:
    plusindex=i
    if candle[i]!=0:
      candle.insert(i,0)
    else:
      getcandle+=1
    break;

print(candle)
distance=[]
for i in range(len(candle)-1):
  distance.append(abs(candle[i]-candle[i+1]))
print(distance)

'''

Solution (Python):
n, m = map(int, input().split())
candles = list(map(int, input().split()))
ans = 10 ** 9
for i in range(n - m + 1):
    a = candles[i]
    b = candles[i + m - 1]
    x = max(abs(a), abs(b))
    if a < 0 and b > 0:
        x += min(abs(a) * 2, abs(b) * 2)
    ans = min(ans, x)

print(ans)

Solution (Python):
N, K = list(map(int, input().strip().split(" ")))
candles = input().strip()
min_cost_index = candles.count("-")
candles = list(map(int, candles.split(" ")))

def update():
    start_index = max(0, min_cost_index - K - 1)
    end_index = min(N, min_cost_index + K + 1)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(start_index):
    start_point = candles[start_index]
    end_point = candles[start_index + K - 1]
    if(start_point * end_point >= 0):
        return max(abs(start_point), abs(end_point))
    else:
        return end_point - start_point + min(-start_point, end_point) 

candles = update()

N = len(candles)
min_cost = compute_cost(0)
patterns = N - K + 1

for start_index in range(patterns):
    cost = compute_cost(start_index)
    if(min_cost > cost):
        min_cost = cost
print(min_cost)

Solution (Python):
N, K = map(int, input().split())
*X, = map(int, input().split())

k = 0
while k < N and X[k] < 0:
    k += 1
P = X[:k]; Q = X[k:]
ans = 10**30
if len(P) >= K:
    ans = min(ans, -P[-K])
if len(Q) >= K:
    ans = min(ans, Q[K-1])
for i in range(1, K):
    if len(P) < i or len(Q) < K-i:
        continue
    p = -P[-i]; q = Q[K-i-1]
    ans = min(ans, min(2*p+q, p+2*q))
print(ans)

Solution (Python):
n, k = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]

Y = []
Z = []

for x in X:
    if x == 0:
        Y.append(0)
        Z.append(0)
    elif x < 0:
        Y.append(abs(x))
    else:
        Z.append(x)

ans = sum(Y) + sum(Z)
Y.reverse()

for i, y in zip(range(1, len(Y)+1), Y):
    if i <= k and k - i <= len(Z):
        ans = min(ans, y + (0 if i == k else 2 * Z[k-(i+1)]))

for i, z in zip(range(1, len(Z)+1), Z):
    if i <= k and k - i <= len(Y):
        ans = min(ans, (0 if i == k else 2 * Y[k-(i+1)]) + z)

print(ans)

Solution (Python):


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

pos = list(int(i) for i in input().split())

ans = abs(pos[0])+abs(pos[0]-pos[k-1])

for i in range(n-k+1):
    val = 0
    val += abs(pos[i] - pos[i+k-1])
    val += min(abs(pos[i]), abs(pos[i+k-1]))

    if val < ans:
        ans = val
print(ans)

Solution (Python):



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

time = 10 ** 20

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

    if time > this_time:
        time = this_time

print(time)

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

ans = 10**18
for i in range(N - K + 1):
    p1 = x[i]
    p2 = x[i+K-1]
    lr = abs(p1) + abs(p2 - p1)
    rl = abs(p2) + abs(p1 - p2)
    if lr < rl:
        ans = min(ans,lr)
    else:
        ans = min(ans,rl)
print(ans)

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

ans = abs(x[0]) + abs(x[K-1] - x[0])
for i in range(N-K+1):
    xl = x[i]
    xr = x[i+K-1]
    tmp = abs(xl) + abs(xr - xl)
    if tmp > abs(xr) + abs(xr - xl):
        tmp = abs(xr) + abs(xr - xl)
    if (ans > tmp):
        ans = tmp
print(ans)

Solution (Python):
import bisect
import sys
 
N, K = map(int, raw_input().split())
x = map(int, raw_input().split())
res = [sys.maxint] * N
 
pos = 0
f_pos = bisect.bisect_left(x, 0)
x.insert(f_pos, 0)
result = sys.maxint
if f_pos + K <= N:
	result = min(result, x[f_pos + K])
if f_pos - K >= 0:
	result = min(result, -x[f_pos - K])
for i in range(K):
	if f_pos + i <= N and f_pos - K + i >= 0:
		result = min(result, x[f_pos + i] - x[f_pos -  K + i] + min(x[f_pos + i], -x[f_pos - K + i]))
print result

Solution (Python):
import bisect
import sys

N, K = map(int, raw_input().split())
x = map(int, raw_input().split())
res = [sys.maxint] * N

pos = 0
f_pos = bisect.bisect_left(x, 0)
x.insert(f_pos, 0)
result = sys.maxint
for i in range(N + 1):
	if i - K >= 0:
		result = min(result, abs(x[i]) + abs(x[i - K]) + min(abs(x[i]), abs(x[i - K])))
print result

Solution (Python):

import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        l = x[bottom + i]
        r = x[bottom + i + K - 1]
        if l < 0 <= r:
            best = min(best, 2 * min(abs(l), r) + max(abs(l), r))
        elif l < 0 and r < 0:
            best = min(best, abs(l))
        elif 0 <= l and 0 <= r:
            best = min(best, abs(r))

    print(best)


if __name__ == "__main__":
    main()

Solution (Python):

candle_num, set_fire_num = list(map(int, input().split()))
candle_list = list(map(int, input().split()))

answer_time = 10 ** 15


def check_time(current_time):
    global answer_time
    if current_time < answer_time:
        answer_time = current_time



for l in range(candle_num - set_fire_num + 1):
    r = l + set_fire_num - 1

    
    if candle_list[l] >= 0:
        check_time(candle_list[r])
        continue

    
    
    if candle_list[r] <= 0:
        check_time(abs(candle_list[l]))
        continue
    else:
        if abs(candle_list[l]) >= abs(candle_list[r]):
            check_time(candle_list[r]*2 + abs(candle_list[l]))
            continue
        else:
            check_time(abs(candle_list[l])*2 + candle_list[r])
            continue


print(answer_time)

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

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

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

for i in range(n-k+1):
    xl = x[i]
    xr = x[i+k-1]
    if i==0:
        MIN = abs(xl-xr) + abs(xl)

    MIN = min(MIN, abs(xl-xr) + abs(xl))
    MIN = min(MIN, abs(xl-xr) + abs(xr))

print(MIN)

Solution (Python):
aa = input().strip().split()
n = int(aa[0])
k = int(aa[1])
x = [int(s) for s in input().strip().split()]
r = -1

for i in range(n - k + 1):
    if 0 <= x[i] and (r == -1 or x[i + k - 1] < r):
        r = x[i + k - 1]

    if x[i] < 0 and (r == -1 or x[i + k - 1] - 2 * x[i] < r):
        r = x[i + k - 1] - 2 * x[i]

    if 0 <= x[i + k - 1] and (r == -1 or 2 * x[i + k - 1] - x[i] < r):
        r = 2 * x[i + k - 1] - x[i]

    if x[i + k - 1] < 0 and (r == -1 or -x[i] < r):
        r = - x[i]

print(r)

Solution (Python):
N,K=map(  int,raw_input().split() )
X=map(  int,raw_input().split() )

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


print ans

Solution (Python):
N,K=map(  int,raw_input().split() )
X=map(  int,raw_input().split() )

ans=float("inf")
t=float("inf")
for l in range(N-K+1):
	r=l+K-1
	if X[l]<0:
		if X[r]<0:	
			ans=min(ans, abs(X[l]) )
		else:	
			ans=min(ans, 2*abs(X[l])+abs(X[r]) , abs(X[l])+2*abs(X[r]) )
	elif X[l]>=0 and X[r]>=0:
		ans=min(ans, X[r] )


print ans

Solution (Python):

import bisect


def main():
    N, K = map(int, raw_input().split())
    x = [int(ele) for ele in raw_input().split()]
    o = bisect.bisect_right(x, 0)
    top = min(o + K, N)
    bottom = max(o - K, 0)
    best = 999999999999
    for i in xrange(K):
        if top < bottom + i + K:
            break

        if x[bottom + i + K - 1] < 0:
            best = min(best, abs(x[bottom + i]))
        elif x[bottom + i] < 0 <= x[bottom + i + K - 1]:
            ans1 = 2 * min(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans2 = max(abs(x[bottom + i]), x[bottom + i + K - 1])
            ans = min(best, ans1 + ans2)
            best = min(best, ans)
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Solution (Python):


n, k = map(int, input().split())
X = list(map(int, input().split()))
ans = 10**10
for i in range(n-k+1):
    left = X[i]
    right = X[i+k-1]
    ans = min(ans, abs(left)+abs(left-right), abs(right)+abs(right-left))
print(ans)

Solution (Python):


n, k = map(int, input().split())
X = list(map(int, input().split()))
ans = 10**10
for i in range(n-k+1):
    left = X[i]
    right = X[i+k-1]
    ans = min(ans, abs(left)+abs(left-right), abs(right)+abs(right-left))
print(ans)

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

l = 0
r = k-1
mnm = 1000100010
while r<n:
    a = abs(x[l])+abs(x[l]-x[r])
    b = abs(x[r])+abs(x[r]-x[l])
    if a<=b:
        if a < mnm:
            mnm = a
    else:
        if b < mnm:
            mnm = b
    l += 1
    r += 1
print(mnm)

Solution (Python):
N, K = map(int,input().split())
listMap = [int(i) for i in input().split()]
minMin = float("inf")

if N == 1:
    minMin = listMap[0]

for j in range(N - (K - 1)):
    L = j
    R = j + (K - 1)
    sumL = abs(listMap[L]) + abs(listMap[L] - listMap[R])
    sumR = abs(listMap[R]) + abs(listMap[R] - listMap[L])
    sum = min(sumL, sumR)
    if sum < minMin:
        minMin = sum

print(minMin)

Solution (Python):
import sys

ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: [int(x) for x in ns().split()]
nall = lambda: [int(x) for x in sys.stdin.readlines()]
sall = lambda: sys.stdin.readlines()

def main():
    n, k = na()
    xs = na()

    m = float('inf')
    for i in range(n-k+1):
        if xs[i] * xs[i+k-1] < 0:
            l = abs(xs[i]) + abs(xs[i+k-1]) + min(abs(xs[i]), abs(xs[i+k-1]))
        else:
            l = max(abs(xs[i]), abs(xs[i+k-1]))

        m = min(m, l)

    print(m)
    
main()

Solution (Python):
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = float("inf")
for l in range(n-k+1):
    r = l+k-1
    ansl = abs(x[l]) + abs(x[r]-x[l])
    ansr = abs(x[r]) + abs(x[r]-x[l])
    ans = min(ans,ansl,ansr)
print(ans)

Solution (Python):
import sys

n,k=map(int,input().split())
x=list(map(int,input().split()))
if(n==1):
    print(x[0])
    sys.exit()
ans=[]
for i in range(n-(k-1)):
    f=abs(x[i+(k-1)])+abs(x[i+(k-1)]-x[i])
    b=abs(x[i])+abs(x[i]-x[i+(k-1)])
    t=min(b,f)
    ans.append(t)
ans.sort()
print(ans[0])

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

ans = float('inf')
for i in range(N - K + 1):
    tmp = abs(X[i]) + abs(X[i + K - 1] - X[i])
    tmp2 = abs(X[i + K - 1]) + abs(X[i + K - 1] - X[i])
    ans = min(ans, tmp, tmp2)

print(ans)