Problem:


Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Runtime Error

Solution code:
#coding:utf-8

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):
    try:
        return set([max(ls)])
    except:
        return set()
def set_min(ls):
    try:
        return set([min(ls)])
    except:
        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 = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        print (ls[i:i+k])
        if sign(ls[i]*ls[i+k-1])<0:
            temp = [abs(ls[i]),abs(ls[i+k-1])]
            answer.append(2*min(temp)+max(temp))
        else:
            answer.append(2*max(temp))
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: Runtime Error

Solution code:
#coding:utf-8

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):
    try:
        return set([max(ls)])
    except:
        return set()
def set_min(ls):
    try:
        return set([min(ls)])
    except:
        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 = []
for i in range(n-k+1):
    if len(set(ls[i:i+k])&neighbor)>0:
        print (ls[i:i+k])
        temp = [0,ls[i],ls[i+k-1]]
        temp = [abs(min(temp)),abs(max(temp))]
        answer.append((2*min(temp)+max(temp)))
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: Runtime Error

Solution code:
#coding:utf-8

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]

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: Runtime Error

Solution code:
#coding:utf-8

# 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]

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: Runtime Error

Solution code:
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: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 3*10**8

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 3*10**8

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

Submission status: Runtime Error

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

zero_ind = 0
for i in range(len(coords)):
    if coords[i] > 0:
        zero_ind = i
        break

start_ind = max(zero_ind - K, 0)
end_ind = min(zero_ind + K, len(coords))

ans = []
for i in range(start_ind, end_ind):
    if i < zero_ind:
	res = K - (zero_ind - i)
        if res == 0:
            min_path = abs(coords[i])
	else:
            min_path = abs(coords[i]) + 2 * coords[zero_ind + res - 1]
	 
    else:
	included = i - zero_ind + 1
	res = K	- included
        if res == 0:
            min_path = coords[i]
    	else:
            min_path = coords[i] + 2 * abs(coords[zero_ind - res])
    ans.append(min_path)
print(min(ans))

Submission status: Runtime Error

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

a=bisect.bisect_left(x,0)
flist=[]
for i in range(a-K,a):
    if i+K-1>N-1 or i<0:
        pass
    else:
        flist.append(i)








for i in flist:
    if x[i+K+1]<0:
        if min>-x[i]:
            min=-x[i]
    elif x[i]>=0:
        if min>x[i+K-1]:
            min=x[i+K-1]
    elif x[i]+x[i+K-1]>=0:
        if min>x[i+K-1]-x[i]*2:
            min=x[i+K-1]-x[i]*2
    else:
        if min>x[i+K-1]*2-x[i]:
            min=x[i+K-1]*2-x[i]
print(min)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,k= map(int,input().split())
x=list(map(int, input().split()))
for i in range(0,n):
    if i>=1 and x[i]>=0 and x[i-1]<=0:
        z=i          #x[z]为0右边的第一个数字
    if x[0]>=0:
        z=0
if z-1<k:
    left=z
    le=k-z
else:
    left=k
    le=0
if n-z<k:
    right=n-z
    ri=k-n+z
else:
    right=k
    ri=0
f=0
if left <right:
    a=left          #a是需要循环几次
else:
    a=right
def jisuan(a):
    for each in range(a):
        if le+each==0:
            h=0-x[z-left+each]
        else:
            h=0-x[z-left+each]+2*x[z+le-1+each]
        global f
        if f==0:
            f=h
        if f>h:
            f=h
    for each in range(a):
        if ri+each==0:
            h=x[z+right-1-each]
        else:
            h=x[z+right-1-each]-2*x[z-ri-each]
        if f>h:
            f=h
if left<k and right<k:
    jisuan(a)
if left>k and right>k:
    jisuan(k)
if left>=k and right<=k:
    jisuan(a+1)
if left<=k and right>=k:
    jisuan(a+1)
print(f)

Submission status: Runtime Error

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: Runtime Error

Solution code:

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

ns = 999999999

for i in range(n - k + 1):
    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: Runtime Error

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

ns = 999999999

for i in range(n - k + 1):
    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: Runtime Error

Solution code:
#my answer
n, k = map(int,input().split())
li = list(map(int,input().split()))

L = []
R = [0]

for x in li:
    if(x < 0):
        L.append(abs(x))
    elif(x > 0):
        R.append(abs(x))
    else:
        k -= 1
        
x = L + R

l = len(L) - k
if(l < 0):
    l = 0
r = l + k

ans = 99999999;
for i in range(n - k):
    ans = min(ans, x[r]*2 + x[l])
    ans = min(ans, x[l]*2 + x[r])
    r += 1
    l += 1
    
if(k == 1):
    ans = min(x[r], x[r-1])
    
print(ans)

Submission status: Runtime Error

Solution code:
#my answer
n, k = map(int,input().split())
li = list(map(int,input().split()))

L = []
R = []

for x in li:
    if(x < 0):
        L.append(abs(x))
    elif(x >= 0):
        R.append(abs(x))

x = L + R

l = len(L) - k
if(l < 0):
    l = 0
r = l + k - 1

ans = 99999999;
for i in range(n - k):
    ans = min(ans, x[r]*2 + x[l])
    ans = min(ans, x[l]*2 + x[r])
    r += 1
    l += 1
    
if(k == 1):
    ans = min(x[r], x[r-1])
    
print(ans)

Submission status: Runtime Error

Solution code:
#my answer
n = int(input())
k = int(input())

L = []
R = []

for i in range(n):
    x = int(input())
    if(x < 0):
        L.append(abs(x))
    elif(x >= 0):
        R.append(abs(x))

x = L + R

l = len(L) - k
if(l < 0):
    l = 0
r = l + k - 1

ans = 99999999;
for i in range(n - k):
    ans = min(ans, x[r]*2 + x[l])
    ans = min(ans, x[l]*2 + x[r])
    r += 1
    l += 1
    
if(k == 1):
    ans = min(x[r], x[r-1])
    
print(ans)

Submission status: Runtime Error

Solution code:
num=int(input())
k=int(input())
x=[None]*(num+1)
for i in range(1,num+1):
    x[i]=int(input())
    if i>2 and x[i]>=0 and x[i-1]<=0:
        z=i        
    if x[1]>=0:
        z=1
if z-1<k:
    left=z-1
    le=k-z+1
else:
    left=k
    le=0
if num-z+1<k:
    right=num-z+1
    ri=k-num+z-1
else:
    right=k
    ri=0
f=0
if left <right:
    a=left         
else:
    a=right
def jisuan(a):
    for each in range(a):
        if le+each==0:
            h=0-x[z-left+each]
        else:
            h=0-x[z-left+each]+2*x[z+le-1+each]
        global f
        if f==0:
            f=h
        if f>h:
            f=h   
    for each in range(a):
        if ri+each==0:
            h=x[z+right-1-each]
        else:
            h=x[z+right-1-each]-2*x[z-ri-each]
        if f>h:
            f=h
if left<k and right<k:
    jisuan(a)
if left>k and right>k:
    jisuan(k)
if left>=k and right<=k:
    jisuan(a+1)
if left<=k and right>=k:
    jisuan(a+1)
print(f)

Submission status: Runtime Error

Solution code:
num=int(input())
if num<1 or num>10**5:
    exit()
print('请输入蜡烛总数:',num)
k=int(input())
if k<1 or k>num:
   exit()
print('请输入点燃的蜡烛数:',k)
x=[None]*(num+1)
for i in range(1,num+1):
    x[i]=int(input())
    if i>2 and x[i]>=0 and x[i-1]<=0:
        z=i        
    if x[1]>=0:
        z=1
if z-1<k:
    left=z-1
    le=k-z+1
else:
    left=k
    le=0
if num-z+1<k:
    right=num-z+1
    ri=k-num+z-1
else:
    right=k
    ri=0
f=0
if left <right:
    a=left         
else:
    a=right
def jisuan(a):
    for each in range(a):
        if le+each==0:
            h=0-x[z-left+each]
        else:
            h=0-x[z-left+each]+2*x[z+le-1+each]
        global f
        if f==0:
            f=h
        if f>h:
            f=h   
    for each in range(a):
        if ri+each==0:
            h=x[z+right-1-each]
        else:
            h=x[z+right-1-each]-2*x[z-ri-each]
        if f>h:
            f=h
if left<k and right<k:
    jisuan(a)
if left>k and right>k:
    jisuan(k)
if left>=k and right<=k:
    jisuan(a+1)
if left<=k and right>=k:
    jisuan(a+1)
print(f)

Submission status: Runtime Error

Solution code:
num=int(input())
if num<1 or num>105:
    exit()
print('请输入蜡烛总数:',num)
k=int(input())
if k<1 or k>num:
   exit()
print('请输入点燃的蜡烛数:',k)
x=[None]*(num+1)
for i in range(1,num+1):
    x[i]=int(input())
    if i>2 and x[i]>=0 and x[i-1]<=0:
        z=i        
    if x[1]>=0:
        z=1
if z-1<k:
    left=z-1
    le=k-z+1
else:
    left=k
    le=0
if num-z+1<k:
    right=num-z+1
    ri=k-num+z-1
else:
    right=k
    ri=0
f=0
if left <right:
    a=left         
else:
    a=right
def jisuan(a):
    for each in range(a):
        if le+each==0:
            h=0-x[z-left+each]
        else:
            h=0-x[z-left+each]+2*x[z+le-1+each]
        global f
        if f==0:
            f=h
        if f>h:
            f=h   
    for each in range(a):
        if ri+each==0:
            h=x[z+right-1-each]
        else:
            h=x[z+right-1-each]-2*x[z-ri-each]
        if f>h:
            f=h
if left<k and right<k:
    jisuan(a)
if left>k and right>k:
    jisuan(k)
if left>=k and right<=k:
    jisuan(a+1)
if left<=k and right>=k:
    jisuan(a+1)
print(f)

Submission status: Runtime Error

Solution code:
num=int(input())
if num<1 or num>105:
    exit()
print('请输入蜡烛总数:',num)
k=int(input())
if k<1 or k>num:
   exit()
print('请输入点燃的蜡烛数:',k)
x=[None]*(num+1)
for i in range(1,num+1):
    x[i]=int(input())
    if i>2 and x[i]>=0 and x[i-1]<=0:
        z=i          #x[z]为0右边的第一个数字
    if x[1]>=0:
        z=1
if z-1<k:
    left=z-1
    le=k-z+1
else:
    left=k
    le=0
if num-z+1<k:
    right=num-z+1
    ri=k-num+z-1
else:
    right=k
    ri=0
f=0
print("left=",left)
print("right=",right)
if left <right:
    a=left          #a是需要循环几次
else:
    a=right
def jisuan(a):
    for each in range(a):
        if le+each==0:
            h=0-x[z-left+each]
        else:
            h=0-x[z-left+each]+2*x[z+le-1+each]
        global f
        if f==0:
            f=h
        if f>h:
            f=h   
    for each in range(a):
        if ri+each==0:
            h=x[z+right-1-each]
        else:
            h=x[z+right-1-each]-2*x[z-ri-each]
        if f>h:
            f=h
if left<k and right<k:
    jisuan(a)
if left>k and right>k:
    jisuan(k)
if left>=k and right<=k:
    jisuan(a+1)
if left<=k and right>=k:
    jisuan(a+1)
print(f)

Submission status: Runtime Error

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 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: Runtime Error

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:
    min = K-lenXu-1
    max = lenXu-1
else:
    min = -1
    max = K-1

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

print(S_stock)

Submission status: Runtime Error

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:
    min = K-lenXu-1
    max = lenXu-1
else:
    min = -1
    max = K-1


S = 10**8
differ = -1

while differ<0:
    S_stock = S
    if min>=0:
        if (x[min]*(-1))<x_upper[max]:
            S = (x[min]*(-1))*2 + x_upper[max]
        else:
            S = (x[min]*(-1)) + x_upper[max]*2
    else:
        S = x_upper[max]
    differ = S - S_stock
    min += 1
    max -= 1

print(S_stock)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
a = []
for i in range(H):
    a.append(input())
row = [False]*H
col = [False]*W
for i in range(H):
    for j in range(W):
        if a[i][j] == "#":
            row[i] = True
            col[j] = True
for i in range(H):
    if row[i]:
        for j in range(W):
            if col[j]:
                print(a[i][j], end = "")
        print("", end = "\n")

Submission status: Runtime Error

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

# print(N, K)
# print(x)

def calc_time(x):
    x_max = max(x)
    x_min = min(x)
    if x_max * x_min > 0:
        if x_max > 0:
            # print('return abs(x_max)')
            return abs(x_max)
        else:
            # print('return abs(x_min)')
            return abs(x_min)
    else:
        if abs(x_max) > abs(x_min):
            # print('return abs(x_max) + abs(x_min) * 2')
            return abs(x_max) + abs(x_min) * 2
        else:
            # print('return abs(x_max) * 2 + abs(x_min)')
            return abs(x_max) * 2 + abs(x_min)

times = []
x_argmin = x.index(min(list((map(abs, x)))))

if x_argmin - K < 0:
    left = 0
else:
    left = x_argmin - K

if x_argmin + K > N - K + 1:
    right = N - K + 1
else:
    right = x_argmin + K

for i in range(left, right):
    # print(x[i: i + K])
    times.append(calc_time(x[i: i + K]))

print(min(times))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

while True:
  if x_coordinate[zero_pos] >= 0:
    break
  else:
    zero_pos += 1

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))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in (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: Runtime Error

Solution code:
a,b=input().split("\n")
n,k=a.split(" ")
x=b.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)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
k -= x_list.count(0)

index = 0   # x_listで最初に0以上の数が出てくる場所のインデックス
while True:
    if x_list[index] >= 0:
        break
    else:
        index += 1

move_total = list()  # 総移動距離のリスト
for i in range(index-k+1, index+1):
    try:
        start = x_list[i]
        end = x_list[i+k-1]

        if (start > 0) == (end > 0):
            if abs(start) > abs(end):
                move_total.append(abs(start))
            else:
                move_total.append(abs(end))
        else:
            if -start > end:
                move_total.append(-start + 2*end)
            else:
                move_total.append(2*(-start) + end)
    except IndexError:
        pass

if move_total:
    print(min(move_total))
else:
    print("0")

Submission status: Runtime Error

Solution code:
def make_time(l, k):
	r = []
	for i in range(k-1, len(l)):
		r.append(l[i] - l[i-k+1])
	for i in range(len(l)-1, k-2, -1):
		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)]
	print(min(times))

Submission status: Runtime Error

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 = map(int, input().split())
    print(solve(X, K))

Submission status: Runtime Error

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
a:int
b:int
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: Runtime Error

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

Submission status: Runtime Error

Solution code:
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):#左端の時
        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)

Submission status: Runtime Error

Solution code:
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):#左端の時
        if(a[r]*a[r+1]<0 or a[0]>0):
            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):
            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)

Submission status: Runtime Error

Solution code:
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):#左端の時
        if(a[r]*a[r+1]<0 or a[0]>0):
            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):
            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)

Submission status: Runtime Error

Solution code:

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):#左端の時
        if(a[r]*a[r+1]<0):
            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):
            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)

Submission status: Runtime Error

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

if 0 in a:
  N = N - 1
  if N == 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

#print(aLow)
#print(aHigh)

for i in range(0,N+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)

Submission status: Runtime Error

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

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

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

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

least = 99999999999999999

for i in range(0,len(aLow)):
  j = N - i
  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)

Submission status: Runtime Error

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

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

#first,last = max(minus-K,0),min(minus,N-K)
first = 0
last = 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)

Submission status: Runtime Error

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

minus = 0
while array[minus] < 0 and minus < N:
    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)

Submission status: Runtime Error

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

minus = 0
while array[minus] < 0 and minus < N:
    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)

Submission status: Runtime Error

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

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

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

ans = 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)

Submission status: Runtime Error

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

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

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

ans = 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),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)

Submission status: Runtime Error

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

Submission status: Runtime Error

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]))
    dis.append(abs(x[i] - x[i + K - 1]) + abs(x[i + k - 1]))
print(min(dis))