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:
inp1 = [int(x) for x in input().split()]
inp2 = [int(x) for x in input().split()]

ans = 999999
for i in range(inp1[0] - inp1[1] + 1):
    l = i
    r = i + inp1[1] - 1
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
for i in range(inp1[0]-1, inp1[1], -1):
    l = i - inp1[1]
    r = i
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
print(ans)

Submission status: Wrong Answer

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

ans = 999999
for i in range(inp1[0] - inp1[1] + 1):
    l = i
    r = i + inp1[1] - 1
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
for i in range(inp1[0]-1, inp1[1]-1, -1):
    l = i - inp1[1]
    r = i
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
print(ans)

Submission status: Wrong Answer

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

ans = 999999
for i in range(inp1[0] - inp1[1] + 1):
    l = i
    r = i + inp1[1] - 1
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
for i in range(inp1[0]-1, inp1[1]-1, -1):
    l = i - inp1[1] - 1
    r = i
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
print(ans)

Submission status: Wrong Answer

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

ans = 999999
for i in range(inp1[0] - inp1[1] + 1):
    l = i
    r = i + inp1[1] - 1
    pos1 = abs(inp2[l]) + abs(inp2[r] - inp2[l])
    pos2 = abs(inp2[r]) + abs(inp2[r] - inp2[l])
    
    ans = min(ans, min(pos1, pos2))
    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(a) - K):
  l = a[i]
  r = a[i+K-1]
  ans = min(ans, abs(l) + abs(r - l), abs(r) + abs(l - r))
print(ans)

Submission status: Wrong Answer

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


min = 2*(10**8)

p=0
z=0
m=0
Y=[]
for x in Xs:
    if x > 0:
        p += 1
        Y.append(x)
    elif x < 0:
        m += 1
    else:
        z +=1

    
K -= z
Z=[]
for i in range(m-1,-1,-1):
    Z.append(-Xs[i])
    

if K == 0:
    min = 0
if K-1 < p and K-1 >= 0:
    if Y[K-1] < min:
        min = Y[K-1]

if K-1 < m and K-1 >= 0:
    if Z[K-1] < min:
        min = Z[K-1]

for i in range(p):
    if i < K-1:
        if K-i-2 < m:
            if Y[i] * 2 + Z[K-i-2] < min:
                min = Y[i] * 2 + Z[K-i-2]
    
for i in range(m):
    if i < K-1:
        if K-i-2 < p:
            if Z[i] * 2 + Y[K-i-2] < min:
                min = Z[i] * 2 + Y[K-i-2]

               
print (min)

Submission status: Wrong Answer

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

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

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

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



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


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

neighbor = set_max(minus) | set_min(plus)

#print (neighbor)
answer = []
cnt = 0
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:

        #print (ls[i:i+k])
        #print (np.sign(ls[i]*ls[i+k-1]))
        temp = [abs(ls[i]),abs(ls[i+k-1])]
        if np.sign(ls[i]*ls[i+k-1])<0:

            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(max(temp))
        cnt += 1
        if cnt == k + 1:
            break
print (min(answer))

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
"""
C - Candles
X ろうそく間の距離を出して、K本目まで距離を足せば良い
O 右にいくか　左に行くか　どっちが良いか
"""

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

candles = []
for part in input().split():
    candles.append(int(part))

min_dist = 10 ** 8 + 1
for i in range(N - K + 1):
    l = i
    r = i + K - 1

    # 右に行ったとき
    r_dist = abs(candles[r]) + abs(candles[l] - candles[r])
    # 左に行ったとき
    l_dist = abs(candles[l]) + abs(candles[r] - candles[l])

    min_dist = min(min_dist, r_dist, l_dist)

print(min_dist)

Submission status: Wrong Answer

Solution code:
"""
C - Candles
X ろうそく間の距離を出して、K本目まで距離を足せば良い
O 右にいくか　左に行くか　どっちが良いか
"""

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

candles = []
for part in input().split():
    candles.append(int(part))

min_dist = 10 ** 8
for i in range(N - K + 1):
    l = i
    r = i + K - 1

    # 右に行ったとき
    r_dist = abs(candles[r]) + abs(candles[l] - candles[r])
    # 左に行ったとき
    l_dist = abs(candles[l]) + abs(candles[r] - candles[l])

    min_dist = min(min_dist, r_dist, l_dist)

print(min_dist)

Submission status: Wrong Answer

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

score = []
for i in range(n-k+1):
    score.append(abs(x[i]) + (x[i+k-1] - x[i]))
score.sort()
print(score[0])

Submission status: Wrong Answer

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

diff = [0]
for i in range(1,len(x)):
    diff.append(x[i] - x[i-1])
#print(diff)

cumsum = [0]
for i in range(1,len(diff)):
    cumsum.append(cumsum[i-1] + diff[i])
#print(cumsum)

score = []
for i in range(n-k+1):
    score.append(abs(x[i]) + cumsum[i+k-1] - cumsum[i])
#print(score)
score.sort()
print(score[0])

Submission status: Wrong Answer

Solution code:
N, k = [ int(i) for i in input().split()]
cPos = [ int(i) for i in input().split()]
time = 10**7
for i in range(N-k+1):
    if time > min( abs(cPos[i])+ abs(cPos[i]- cPos[i+k-1]), abs(cPos[i+k-1])+ abs(cPos[i]- cPos[i+k-1])):
        time = min( abs(cPos[i])+ abs(cPos[i]- cPos[i+k-1]), abs(cPos[i+k-1])+ abs(cPos[i]- cPos[i+k-1]))
print(time)

Submission status: Wrong Answer

Solution code:
N, K = [ int(i) for i in input().split()]
cPos = [ int(i) for i in input().split()]
time = 10**7
for i in range(N-K+1):
    if cPos[i]*cPos[i+K-1] < 0 :
        if time > cPos[i+K-1] -cPos[i] + min(-cPos[i], cPos[i+K-1]):
            time = cPos[i+K-1] -cPos[i] + min(-cPos[i], cPos[i+K-1])
    else:
        if time > max( abs(cPos[i]), abs(cPos[i+K-1])):
            time = max( abs(cPos[i]), abs(cPos[i+K-1]))
print(time)

Submission status: Wrong Answer

Solution code:
N, K = [ int(i) for i in input().split()]
cPos = [ int(i) for i in input().split()]
time = 10**6
for i in range(N-K+1):
    if cPos[i]*cPos[i+K-1] < 0 :
        if time > cPos[i+K-1] -cPos[i] + min(-cPos[i], cPos[i+K-1]):
            time = cPos[i+K-1] -cPos[i] + min(-cPos[i], cPos[i+K-1])
    else:
        if time > max( abs(cPos[i]), abs(cPos[i+K-1])):
            time = max( abs(cPos[i]), abs(cPos[i+K-1]))
print(time)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
tmin=1000000000
for i in range(N-K+1):
    a=x[i+K-1]
    b=x[i]
    res=a-b+min([abs(a),abs(b)])
    if tmin>a:
        tmin=a
print(tmin)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 999999999

for i in range(n - k + 1):
	if(x[i] <= 0 and x[i + k - 1] >= 0):
		a = abs(x[i]) + (x[i + k - 1] - x[i])
		b = abs(x[i + k - 1]) + (x[i + k - 1] - x[i])
		ans = min(ans, a, b)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

x_upper = []

for i in reversed(x):
    if i<0:
        break
    if i>=0:
        x_upper.append(i)
        x.pop()
        
#絶対値昇順へソート
x.reverse()
x_upper.reverse()

lenXu = len(x_upper)
lenX = len(x)

if K>lenXu:
    l = K-lenXu-1
    h = lenXu-1
else:
    l = -1
    h = K-1

S = []
while l<K and l<lenX:
    if h==-1:
        S.append(x[i]*(-1))
    elif l>=0:
        if (x[l]*(-1))<x_upper[h]:
            S.append((x[l]*(-1))*2 + x_upper[h])
        else:
            S.append((x[l]*(-1)) + x_upper[h]*2)
    else:
        S.append(x_upper[h])
    l += 1
    h -= 1

print(min(S))

Submission status: Wrong Answer

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

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

#print("problem {}".format(x))
# 絶対値でソート
x.sort(key=abs)

propose = []
pos_max = 0
neg_max = 0
for i in range(K):
    target = x[i]
    if target > 0:
        if pos_max < target:
            pos_max = target

    if target < 0:
        if neg_max < abs(target):
            neg_max = abs(target)

#print("pos:{} ".format(pos_max + 2 * neg_max))
#print("neg:{} ".format(neg_max + 2 * pos_max))
propose.append(pos_max + 2 * neg_max)
propose.append(neg_max + 2 * pos_max)


# only plus
x_plus = []
x_minus = []
for x_ in x:
    if x_ == 0:
        x_plus.append(x_)
        x_minus.append(x_)
    if x_ > 0:
        x_plus.append(x_)
    if x_ < 0:
        x_minus.append(abs(x_))

if K <= len(x_plus):

    #print("op:{} ".format(x_plus))
    propose.append(x_plus[K-1])

if K <= len(x_minus):
    #print("om:{} ".format(x_minus))
    propose.append(x_minus[K-1])

print(min(propose))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(N-K+1):
    if a[i]<0:
        b.append(-2*a[i]+a[i+K-1])
    else:
        b.append(a[i+K-1])
print(min(b))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(N-K+1):
    if a[i]<0:
        b.append(-2*a[i]+a[i+K-1])
    else:
        b.append(a[i]+a[i+K-1])
print(min(b))

Submission status: Wrong Answer

Solution code:
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)
	times = [(x_time[i] + abs(x[i])) for i in range(N - K + 1)]
	print(min(times))

Submission status: Wrong Answer

Solution code:
def make_dist(l):
	r = [0]
	for i in range(1, len(l)):
		r.append(l[i] - l[0])
	return r

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def getMin(X, K):
    minVal = float('inf')
    for l in range(0, 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] - X[l]) + abs(X[l]))
    return minVal

if __name__ == "__main__":
    N, K = map(int, input().split())
    X = [int(x) for x in input().split()]
    print(getMin(X, K))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

import sys

def getMin(X, K):
    minVal = sys.maxsize
    if K == 1:
        return X[0]
    
    for l in range(0, len(X)-K):
        minVal = min(minVal, abs(X[l]) + abs(X[K + l - 1] - X[l]))
            
    return minVal

def solve(X, K):
    ans = min(getMin(X, K), getMin(X[::-1], K))
    return ans

if __name__ == "__main__":
    N, K = map(int, input().split())
    X = [int(x) for x in input().split()]
    print(solve(X, K))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, K = (int(i) for i in input().split(' '))
x = [ int(i) for i in input().split(' ')]

def myabs(x0, x1):
    if x0 > x1:
        return x0 - x1
    else:
        return x1 - x0
    

min = -1
len = 0
x0, x1 = (0, 0)
for start in range(0,N-K):
    len = myabs(0, x[start]) + myabs(x[start], x[start+K-1])
#    print( '{0} - {1}:   {2}'.format(x[start], x[start+K-1], len))
    if min > len or min == -1:
        min = len

    len = myabs(0,x[N-(start+1)]) + myabs(x[N-(start+1)], x[N-(K+1)])
#    print( '{0} - {1}:   {2}'.format(x[N-start-1], x[N-K-1], len))
    if min > len:
        min = len
        
print(min)

Submission status: Wrong Answer

Solution code:
str = input()
n,k = [int(x) for x in str.split()]
str = input()
list = [int(x) for x in str.split()]
ans = 200000000
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)

Submission status: Wrong Answer

Solution code:
str = input()
n,k = [int(x) for x in str.split()]
str = input()
list = [int(x) for x in str.split()]
ans = 100000000
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)

Submission status: Wrong Answer

Solution code:
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):
    r = i + k - 1
    ans = min(ans, abs(a[i]) + a[r] - a[i], abs(a[r]) + a[r] - a[i])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# Q3
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-K):
    right = left + K - 1
    dist = distance(x[left], x[right])
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Submission status: Wrong Answer

Solution code:
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]))

print(min(dis))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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 or (center == n - 1 and x[n - 1] == 0):
    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)

Submission status: Wrong Answer

Solution code:
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 or (center == n - 1 and x[n - 1] == 0):
    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))):
    print(i, k - i)
    ans = min(ans, plus[i] + minus[k - i] + min(plus[i], minus[k - i]))

print(ans)

Submission status: Wrong Answer

Solution code:
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] + a[r] - a[l])])
print(ans)

Submission status: Wrong Answer

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

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

ans = 99999999999
for i in range(0,len(xs)-K+1):
  sliced = xs[i:i+K]
  head = abs(sliced[0])
  delta = sliced[-1] - sliced[0]
  ans = min(head+delta, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

t = 10 ** 8

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

print(t)

Submission status: Wrong Answer

Solution code:
def update():
    cost = list(map(abs, candles))
    min_cost_index = cost.index(min(cost))
    print(min_cost_index)
    start_index = max(0, min_cost_index - K)
    end_index = min(N, min_cost_index + K)
    new_candles = candles[start_index: end_index]
    return new_candles

def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return point_candles[-1] - point_candles[0] + min(abs(point_candles[0]), abs(point_candles[-1])) 

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



candles = update()

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


for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Wrong Answer

Solution code:
def compute_cost(point_candles):
    if(point_candles[0] * point_candles[-1] >= 0):
        return max(abs(point_candles[0]), abs(point_candles[-1]))
    else:
        return abs(point_candles[0]) + abs(point_candles[-1])

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

patterns = N - K + 1

min_cost = None

for start_index in range(patterns):
    cost = compute_cost(candles[start_index:start_index + K])
    print(cost)
    if(min_cost == None or min_cost > cost):
        min_cost = cost
print(min_cost)

Submission status: Wrong Answer

Solution code:
def main(init, lcandle, rcandle, ifzero):
	target = init[1]
	
	if ifzero:
		target -= 1
	
	cost = sum(lcandles) + sum(rcandles)
	if len(lcandles) == 0 and len(rcandles)>0:
		cost = min(rcandles[target-1], cost)
	elif len(rcandles) == 0 and len(lcandles)>0:
		cost = min(lcandles[-(target-1)],cost)
	elif len(rcandles) == 0 and len(lcandles) == 0:
		cost = 0
	else:
		if len(rcandles)>= target:
			cost = min(rcandles[target-1], cost)
		if len(lcandles) >= target:
			cost = min(lcandles[-(target)],cost)
		for j in range(len(lcandles)):
			if target-j-1 <= len(rcandles):
				#print(lcandles[-j-1], rcandles[target-j-2])
				cost = min(lcandles[-j-1]*2 + rcandles[target-j-2], cost)
		for j in range(len(rcandles)):
			if target-j-1 <= len(lcandles):
				cost = min(rcandles[j]*2 + lcandles[-(target-j-1)], cost)
				#print(cost)
				
		
	return cost
	
	
init = input().split(" ")
init = [int(c) for c in init]

candles = input().split(" ")
candles = [int(c) for c in candles]

lcandles = [abs(candle) for candle in candles if candle < 0]
rcandles = [candle for candle in candles if candle > 0]
ifzero = 0 in candles

print(main(init, lcandles, rcandles, ifzero))

Submission status: Wrong Answer

Solution code:
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]
    this_time = 0
    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(this_time)

Submission status: Wrong Answer

Solution code:
def main(init, lcandle, rcandle, ifzero):
	target = init[1]
	
	if ifzero:
		target -= 1
		
	
	cost = sum(lcandles) + sum(rcandles)
	if len(lcandles) == 0 and len(rcandles)>0:
		cost = rcandles[target-1]
	elif len(rcandles) == 0 and len(lcandles)>0:
		cost = lcandles[-(target-1)]
	else:
		for j in range(len(lcandles)):
			if target-j <= len(rcandles):
				cost = min(lcandles[-(j)]*2 + rcandles[target-j-1], cost)
		for j in range(len(rcandles)):
			if target-j <= len(lcandles):
				cost = min(rcandles[j]*2 + lcandles[-(target-j-1)], cost)	
		
	return cost
	
	
init = input().split(" ")
init = [int(c) for c in init]

candles = input().split(" ")
candles = [int(c) for c in candles]

lcandles = [abs(candle) for candle in candles if candle < 0]
rcandles = [candle for candle in candles if candle > 0]
ifzero = 0 in candles

print(main(init, lcandles, rcandles, ifzero))

Submission status: Wrong Answer

Solution code:
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, x[i] - x[i - K] + min(x[i], -x[i - K]))
print result

Submission status: Wrong Answer

Solution code:
def main():
    N, K = list(map(int, input().split(" ")))
    X = list(map(int, input().split(" ")))
    output = solve(N, K, X)
    print(output)
    return

#________________________________________
#  Solve
#________________________________________
def solve(N, K, X):
    """
    解答のコード
    """
    oab_min = X[-1] - X[0]

    a_list = [ i for i in range(len(X)) if X[i] >= 0 ]  # O(N)
    b_list = [ i for i in range(len(X)) if X[i] < 0 ]  # O(N)

    if not a_list:
        # if a_list is empty
        ob = - X[b_list[K-1]]
        oab_min = min( ob, oab_min)
    if not b_list:
        oa = X[a_list[K-1]]
        oab_min = min( oa, oab_min)
    left = max(0, K - len(b_list))
    right = min(len(a_list), K)
    for i in range(left, right):
        # if a_list is empty, skip this process
        if (i==0) and (a_list[0] != 0):
            ob = - X[b_list[K-1]]
            oab_min = min( ob, oab_min )
        oa = X[a_list[i]]
        ob = abs(X[a_list[i] - K + 1])
        oab = min( oa, ob ) + oa + ob
        oab_min = min(oab, oab_min)
    return oab_min
#________________________________________


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

left = sorted([abs(i) for i in x if i < 0])
right = [i for i in x if i > 0]


ans = 999999999999999999999999999


l = 0

if 0 in x:
    k -= 1
    if k == 0:
        print(0)
        exit()

if len(right) >= k:
    ans = right[k-1]

if len(left) >= k:
    ans = left[k-1]



while len(left) != l:

    distance = 0

    nokori = k - l - 1


    distance += left[l] * 2
    l += 1


    if nokori > len(right):
        continue

    if nokori == 0:
        ans = min(ans,distance)
        break
    else:
        ans = min(ans,distance + right[nokori-1])
        

print(ans)

Submission status: Wrong Answer

Solution code:
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(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

Submission status: Wrong Answer

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

left = sorted([abs(i) for i in x if i < 0])
right = [i for i in x if i >= 0]

ans = 999999999999999999999999999

l = 0

if len(right) >= k:
    ans = right[k-1]

if len(left) >= k:
    ans = left[k-1]



while len(left) != l:

    distance = 0

    nokori = k - l - 1


    distance += left[l] * 2
    l += 1


    if nokori > len(right):
        continue

    if nokori == 0:
        ans = min(ans,distance)
        break
    else:
        ans = min(ans,distance + right[nokori-1])
        

print(ans)

Submission status: Wrong Answer

Solution code:
# vim: set fileencoding=utf-8:
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]:
            best = min(best, 2 * abs(x[bottom + i]) + x[bottom + i + K - 1])
        elif 0 <= x[bottom + i]:
            best = min(best, x[bottom + i + K - 1])

    print(best)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

l = 0
r = k-1
mnm = 1000100010
while r<n:
    print(r)
    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)

Submission status: Wrong Answer

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

left = sorted([abs(i) for i in x if i < 0])
right = [i for i in x if i >= 0]

ans = 9999999999999999

l = 0

if len(right) >= k:
    ans = right[k-1]

if len(left) >= k:
    ans = left[k-1]



while len(left) != l:

    distance = 0

    nokori = k - l - 1


    distance += left[l] * 2
    l += 1


    if nokori > len(right):
        continue

    if nokori == 0:
        ans = min(ans,distance)
        break
    else:
        ans = min(ans,distance + right[nokori-1])
        

print(ans)

Submission status: Wrong Answer

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

l = 0
r = k-1
mnm = 100010001
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)

Submission status: Wrong Answer

Solution code:
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)
b_pos = f_pos - 1
result = sys.maxint
if f_pos + K - 1 < N:
	result = min(x[f_pos + K - 1], result)
if b_pos - K + 1 >= 0:
	result = min(x[b_pos - K + 1], result)
for i in range(K):
	if f_pos + i < N and f_pos - K + i + 1 >= 0:
		result = min(x[f_pos + i] * 2 - x[f_pos - K + i + 1], result)
	if b_pos - i >= 0 and b_pos + K - i - 1 < N:
		result = min(-x[b_pos - i] * 2 + x[b_pos + K - i - 1], result)
print result

Submission status: Wrong Answer

Solution code:
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)
b_pos = f_pos - 1
result = sys.maxint
if f_pos + K - 1 < N:
	result = min(x[f_pos + K - 1], result)
if b_pos - K + 1 >= 0:
	result = min(x[b_pos - K + 1], result)
for i in range(K):
	if f_pos + i < N and f_pos - K + i >= 0:
		result = min(x[f_pos + i] * 2 - x[f_pos - K + i + 1], result)
	if b_pos - i >= 0 and b_pos + K - i < N:
		result = min(x[b_pos - i] * 2 + x[b_pos + K - i - 1], result)
print result

Submission status: Wrong Answer

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

for i in range(n-k+1):
    xtmp = x[i:i+k]
#     print(i, xtmp)

    if i==0:
        MIN = abs(xtmp[0])+2*abs(xtmp[-1])

    MIN = min(MIN, abs(xtmp[0])+2*abs(xtmp[-1]))
    MIN = min(MIN, 2*abs(xtmp[0])+abs(xtmp[-1]))

    i+=1

print(MIN)