Problem:


Score : 300 points

Problem Statement

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

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

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

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

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

Sample Output 4

10



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


Sample output:
40

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
m=10**9
for i in range(n-k+1):
  a=x[i];b=x[i+k-1]
  if a*b<0:
    a=abs(a)
    t=min(a,b)*2+max(a,b)
  else:
    t=max(abs(a),abs(b))
  m=min(m,t)
print(m)

Submission status: Accepted

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


def cal_num(a, b):
    if x[a] * x[b] < 0:
        return min(abs(x[a]), abs(x[b])) * 2 + max(abs(x[a]), abs(x[b]))
    else:
        return max(abs(x[a]), abs(x[b]))

for i in range(n-k+1):
    c = cal_num(i, i+k-1)
    if num >= c:
        num = c

print(num)

Submission status: Accepted

Solution code:
import sys


def solve():
    sys.setrecursionlimit(2000)
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    x = list(map(int, input().rstrip('\n').split()))
    if len(x) == 1:
        print(abs(sum(x)))
    else:
        mt = 10 ** 10
        for i in range(k-1, n):
            if x[i] * x[i-k+1] < 0:
                mt = min(mt, abs(x[i]) + abs(x[i-k+1]) + min(abs(x[i]), abs(x[i-k+1])))
            else:
                mt = min(mt, max(abs(x[i]), abs(x[i-k+1])))
        print(mt)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

print(m)

Submission status: Accepted

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

for i in range(N-K+1):
    R=X[i+K-1]
    L=X[i]
    A=[ans, abs(L)+abs(R-L), abs(R)+abs(R-L)]
    ans=min(A)

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ress = [] 
for i in range(N-K+1):
    l = i
    r = i+K-1
    ress.append(abs(x[l]) + abs(x[r]-x[l]))
    ress.append(abs(x[r]) + abs(x[r]-x[l]))

print(min(ress))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
INF = float('inf')
ans = INF
l, r = 0, K-1
for i in range(N-K+1):
    a, b = X[l+i], X[r+i]
    A, B = abs(a), abs(b)
    if a*b > 0:
        ans = min(ans, max(A, B))
    else:
        ans = min(ans, A + B + (A if A < B else B))

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
right=[i for i in l if i>0]
left=[i for i in l if i<=0]
if not left: 
    sm=0 
    sm+=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
if not right: 
    sm=0 
    l=[abs(i) for i in l]
    l.sort()
    sm=l[0]
    for i in range(1,k):
        sm+=(l[i]-l[i-1])
    print(sm)
    exit()
le=len(left)
ri=len(right)
rl=[0]*le 
rr=[0]*ri 
left=[abs(i) for i in left]
left=left[::-1]
rl[0]=left[0]
for i in range(1,le):
    rl[i]=rl[i-1]+(left[i]-left[i-1])
rr[0]=right[0]
mini=10**20 
for i in range(1,ri) :
    rr[i]=rr[i-1]+(right[i]-right[i-1])
if len(left)>=k:
    mini=min(mini,rl[k-1])
if len(right)>=k:
    mini=min(mini,rr[k-1])
#print(left,right,rl,rr)
for a in range(1,len(left)+1):
    b=k-a 
    #print(a,b)
    if b>len(rr):
        #print('hsef')
        continue 
    mini=min(mini,2*rl[a-1]+rr[b-1],rl[a-1]+2*rr[b-1])
print(mini)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=1000000000
for i in range(N-K+1):
  tmp1=abs(x[i])+abs(x[i+K-1]-x[i])
  tmp2=abs(x[i+K-1])+abs(x[i+K-1]-x[i])
  tmp=min(tmp1,tmp2)
  ans=min(tmp,ans)

print(ans)

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))

ans = 10 ** 9
for i in range(N-K+1):
    xl, xr = X[i], X[i+K-1]
    if xl < 0 and xr <= 0:
        ans = min(ans, -xl)
    elif xl < 0 and 0 < xr:
        ans = min(ans, -2*xl + xr, -xl + 2*xr)
    else:
        ans = min(ans, xr)
        break
print(ans)

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()
 
N, K = map(int, inputs[0].split())
X = list(map(int, inputs[1].split()))
 
if N == 1:
    print(X[0])
else:
    ans = 10 ** 9
    for i in range(N-K+1):
        x1, x2 = X[i], X[i+K-1]
        if x1 <= 0 and x2 <= 0:
            ans = min(ans, -x1)
        elif x1 <= 0 and 0 < x2:
            if -x1 < x2:
                ans = min(ans, -2*x1 + x2)
            else:
                ans = min(ans, -x1 + 2*x2)
        else:
            ans = min(ans, x2)
            break
    print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
m=n-k+1
ans=float('inf')
for i in range(m):
    a=abs(x[i])+abs(x[i+k-1]-x[i])
    b=abs(x[i+k-1])+abs(x[i+k-1]-x[i])
    ans=min(ans,b,a)
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
k = float("INF")
for i in range(N-K+1):
	l = X[i]
	r = X[i+K-1]
	k = min(k, abs(l)+abs(r-l), abs(r)+abs(r-l))
print(k)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left as bl
input = sys.stdin.readline
N, K = map(int, input().split())
x = list(map(int, input().split()))
dp = [float("inf")] * (K + 2)
s = bl(x, 0)
l = 0
r = K - 1
if r + 1 < s:
  l = s - 1 - r
  r = s - 1
def calc(l, r):
  if x[l] * x[r] < 0:
    return min(abs(x[l]), abs(x[r])) * 2 + max(abs(x[l]), abs(x[r]))
  else:
    return max(abs(x[l]), abs(x[r]))
dp[0] = calc(l, r)
for i in range(1, K + 2):
  l += 1
  r += 1
  if l > s + 1 or r >= N:
    break
  dp[i] = calc(l, r)
print(min(dp))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import bisect

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

zero=bisect.bisect_left(X,0)

ans=10**18
start=max(0,zero-K)
for i in range(start,start+N-K+1):
    #print(i)
    m=min(0,X[i])
    M=max(0,X[i+K-1])
    ans=min((abs(m)+abs(M)*2),(abs(m)*2+abs(M)),ans)
    if i+K==N:
        break
    
print(ans)

Submission status: Accepted

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

    ans = 10 ** 10
    for i in range(N-K+1):
        L = x[i]
        R = x[i+K-1]
        if L <= 0 and R <= 0:
            tmp = abs(L)
        elif L >= 0 and R >= 0:
            tmp = R
        else:
            tmp = abs(L) + (R-L)
            if tmp > 2 * R + abs(L):
                tmp = 2 * R + abs(L)
        if tmp < ans:
            ans = tmp
    print(ans)

Submission status: Accepted

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

#print(candle)

ans = 10 ** 13
if K == 1:
    print(candle[0])
    exit()
# From Left Start
#print("left")
for i in range(N-K+1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i+K-1])
    #print("_",tmp)
    if tmp < ans:
        ans = tmp
# From Right Start 
#print("right")
for i in range(N-1,K-2,-1):
    tmp = abs(candle[i]) + abs(candle[i] - candle[i-K+1])
    #print("_",tmp)
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
ans,t=3e8,0
for i in range(n-k+1):
  l=x[i]
  r=x[i+k-1]
  if l*r>=0:
    t=max(abs(l),abs(r))
  else:
    t=abs(l)+abs(r)+min(abs(l),abs(r))
  ans=min(ans,t)
print(ans)

Submission status: Accepted

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

ans = 2 * 10**9

for i in range(N - K + 1):
    time = abs(X[i + K - 1] - X[i]) + min(abs(X[i]), abs(X[i + K - 1]))
    ans = min(ans, time)

print(ans)

Submission status: Accepted

Solution code:
import sys
n,k=map(int,input().split())
x=list(map(int,input().split()))
left=[]
right=[]
origin=0
for u in x:
    if u<0:
        left.append(abs(u))
    elif u==0:
        origin+=1
    else:
        right.append(u)
left.sort()
k-=origin
if k==0:
    print(0)
    sys.exit()
r=len(right)
l=len(left)
ans=float("inf")
if k<=r:
    ans=right[k-1]
if k<=l:
    ans=min(ans,left[k-1])
for i in range(k-1):
    if i<r and k-i-2<l:
        y=right[i]+left[k-i-2]+min(right[i],left[k-i-2])
        ans=min(ans,y)
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ary = list(map(int, input().split()))
ary2 = sorted(ary+[0])
zero = ary2.index(0)
k = [abs(i - zero) if i < zero else ((i + 1) - zero)for i in range(N)]
ans = float("Inf")
for i in range(N - K + 1):
    if k[i] <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i + (K - 1)]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i + (K - 1)])
for i in range(N-1, K-2, -1):
    if k[i] <= K:
        if ans > abs(ary[i]) + abs(ary[i] - ary[i - (K-1)]):
            ans = abs(ary[i]) + abs(ary[i] - ary[i - (K-1)])
print(ans)

Submission status: Accepted

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

ret = float('inf')
for i in range(N-K+1) :
    l = x[i]
    r = x[i+K-1]
    if l < 0 and r < 0 :
        ret = min(ret, -l)
    elif l > 0 and r > 0 :
        ret = min(ret, r)
    else :
        m, M = min(-l, r), max(-l, r)
        ret = min(ret, m * 2 + M)
print(ret)

Submission status: Accepted

Solution code:
INF = 10**9
N, K = map(int, input().split())
s = [int(c) for c in input().split()]

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
ary = [0] + list(map(int, input().split()))
ary.sort()
zero = ary.index(0)
a = [abs(i - zero) for i in range(N + 1)]
ary.pop(zero)
ans = float("Inf")
for i in range(N - K + 1):
    b = N - i - 1
    d = abs(ary[i])
    if a[i] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[i] - ary[i + K - 1]))
    d = abs(ary[b])
    if a[b] >= K:
        ans = min(ans, d)
    ans = min(ans, d + abs(ary[b] - ary[b - K + 1]))
print(ans)

Submission status: Accepted

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

import bisect

zero_index = bisect.bisect_left(x,0)
start_index = max(0,zero_index-k)
x = x[start_index:zero_index+k]

answer = (x[-1]-x[0])*k
for i in range(len(x)-k+1):
  if x[i+k-1]*x[i]>0:
    answer = min(answer,max(abs(x[i+k-1]),abs(x[i]))) 
  else:
    answer = min(answer,min(abs(x[i+k-1]),abs(x[i]))*2+max(abs(x[i+k-1]),abs(x[i])))

print(answer)

Submission status: Accepted

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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

def calcCost(a, b):
    mina = abs(a)+abs(a-b)
    minb = abs(b)+abs(b-a)

    return min(mina, minb)

ans = sys.maxsize
for i in range(0, n-k+1, 1):
    tmpans = calcCost(data[i], data[i+k-1])
    ans = min(ans, tmpans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
def time(i):
    if X[i]*X[i+K-1]<0:
        return min(abs(X[i]),abs(X[i+K-1]))+abs(X[i])+abs(X[i+K-1])
    else:
        return max(abs(X[i]),abs(X[i+K-1]))
r=10**9
for i in range(N-K+1):
    r=min(r,time(i))
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k= map(int, input().split())
x= list(map(int, input().split()))
s=0
cnt=0
while x[s]<0:
    s+=1
    if n==s+1:
        break
a=[]
if x[s]>=0:
    for i in range(s,min(s+k,n)):
        cnt+=1
        if i-k-1>=0:
            a.append(2*x[i]-x[i-k+1])
        if cnt==k:
            a.append(x[i])
    s-=1
cnt=0
for i in range(s,max(s-k,-1),-1):
    cnt+=1
    if i+k-1<n:
        a.append(-2*x[i]+x[i+k-1])
    if cnt==k:
        a.append(-x[i])
print(min(a))

Submission status: Accepted

Solution code:
import bisect

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

x_negative = x[:bisect.bisect_left(x,0)]
x_positive = x[bisect.bisect_left(x,0):]

# 0以上の地点にロウソクが無い場合
if len(x_positive) == 0:
    print(abs(x_negative[-K]))
    exit()

# 地点0にロウソクがある場合
if x_positive[0] == 0:
    if K == 1:
        print(0)
        exit()
    K -= 1
    del x_positive[0]

t_list = []

# 全くターンしない場合
if len(x_negative) >= K:
    t_list.append(abs(x_negative[-K]))
if len(x_positive) >= K:
    t_list.append(x_positive[K-1])

# 1回だけターンする場合
for i in range(1, K-1):
    try:
        t_list.append(2*abs(x_negative[-i]) + x_positive[K-i-1])
    except IndexError:
        pass
    
for i in range(1, K-1):
    try:
        t_list.append(2*x_positive[i-1] + abs(x_negative[-(K-i)]))
    except IndexError:
        pass

print(min(t_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

ans = (10**8)*K
for i in range(N-K+1):
    ans = min(ans, abs(X[i])+abs(X[i]-X[K-1+i]),abs(X[K-1+i])+abs(X[i]-X[K-1+i]))
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
res = 10**10
for i in range(0,n-k+1):
    l = x[i]
    r = x[i+k-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)

Submission status: Accepted

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

Distance=[]
for i in range(N-K+1):
        Distance.append(abs(Candles[i]-Candles[i+K-1])+min(abs(Candles[i]),abs(Candles[i+K-1])))
print(min(Distance))

Submission status: Accepted

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

ans = 2*(10**9)
for i in range(n-k+1):
    total_d = x[i+k-1] - x[i]
    zero_d = min(abs(x[i]), abs(x[i+k-1]))
    ans = min(ans, total_d + zero_d)
print(ans)

Submission status: Accepted

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

ans = 10000000000

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

Submission status: Accepted

Solution code:

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

ans=float('inf')
for i in range(n-k+1):
    mini=x[i]
    maxi=x[i+k-1]
    if mini*maxi==0:
        a=max(abs(mini),abs(maxi))
        ans=min(ans,a)
    elif mini*maxi<0:
        a=abs(mini)*2+maxi
        b=abs(mini)+maxi*2
        ans=min(ans,a,b)
    else:
        a=max(abs(mini),abs(maxi))
        ans=min(ans,a)
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
ans = float("inf")
for i in range(n - k + 1):
    start = i
    end = i + k - 1
    left = abs(x_list[start]) + abs(x_list[end] - x_list[start])
    right = abs(x_list[end]) + abs(x_list[end] - x_list[start])
    ans = min(ans, left, right)
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

ans = float("inf")
for i in range(N-K+1):
    temp = min(abs(x[i]) + abs(x[i+K-1]-x[i]), abs(x[i+K-1]) + abs(x[i]-x[i+K-1]))
    if ans > temp:
        ans = temp

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
positive = []
negative = []
cnt_zero = 0
for i in x:
    if i<0:
        negative.append(abs(i))
    elif i==0:
        cnt_zero +=1
    else:
        positive.append(i)
negative.sort()

m = k -cnt_zero
if m==0:
    print(0)
    exit()

ans = float('inf')

for i in range(m):
    if (0<=i<=len(negative)-1) and (0<= m - i -2 <=len(positive)-1):
        ans = min(ans,negative[i]*2+positive[m-i-2])
    if (0<=i<=len(positive)-1) and (0<= m - i -2 <=len(negative)-1):
        ans = min(ans,positive[i]*2+negative[m-i-2])
if m<=len(positive):
    ans = min(ans,positive[m-1])
if m<=len(negative):
    ans = min(ans,negative[m-1])
    
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
res = 10**10
for i in range(0,n-k+1):
    l = x[i]
    r = x[i+k-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
kx = x[0:k]
l=kx[0]
r = kx[-1]
lrsmall = min(abs(l),abs(r))
res = r-l+lrsmall
for i in range(1,n-k+1):
    del kx[0]
    kx.append(x[i+k-1])
    l=kx[0]
    r = kx[-1]
    lrsmall = min(abs(l),abs(r))
    res = min(r-l+lrsmall,res)
print(res)

Submission status: Accepted

Solution code:
import numpy as np
n,m = [int(i) for i in input().split()]

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

neg = a[a < 0][::-1]
pos = a[a >= 0]

ans = 1000000000000000000
if len(neg) == 0:
    print(pos[m-1])
    exit()
else:
    for i in reversed(range(len(pos) + 1)):
        if i > m:
            continue
        elif m - i > len(neg):
            break
        if i == m:
            ans = min(ans, pos[i - 1])
        elif i == 0:
            ans = min(ans, abs(neg[m - 1]))
        else:
            ans = min(ans, pos[i-1]*2 + abs(neg[m-i-1]), abs(neg[m-i-1] * 2) + pos[i-1])

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(n-k+1):
	count = 0	
	left = x[i]
	right = x[i+k-1]
	
	if left < 0 and right >= 0:
		a = abs(left)*2 + right 
		b = right*2 + abs(left)
		count = min(a, b)
		ans = min(ans, count)
	else:
		count = max(abs(x[i]), abs(x[i+k-1]))
		ans = min(ans, count)
		
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
line=list(map(int,input().split()))
ans=float("inf")
for i in range(n-k+1):
    left,right=i,i+k-1
    ans=min(ans,
            abs(line[left])+abs(line[left]-line[right]),
            abs(line[right])+abs(line[right]-line[left])
            )
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

answer = []
for i in range(N-K+1):
    a, b = X[i], X[i+K-1]
    answer.append(min(abs(a-b) + abs(b), abs(b-a) + abs(a)))
print(min(answer))

Submission status: Accepted

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

answer = 10 ** 9
for i in range(N-K+1):
    b, c = X[i], X[i+K-1]
    if b * c >= 0:
        answer = min(answer, max(abs(b), abs(c)))
    else:
        d, e = min(abs(b), abs(c)), max(abs(b), abs(c))
        answer = min(answer, d*2+e)
print(answer)

Submission status: Accepted

Solution code:
import sys
N,K = map(int,input().split())
Data = list(map(int,input().split()))
Move = K
if 0 in Data:
    Move -= 1
Pos = []
Neg = []
for d in Data:
    if d > 0:
        Pos.append(d)
    if d < 0:
        Neg.append(-d)
Pos.sort()
Neg.sort()
posMaxRange = min(Move,len(Pos))
negMaxRange = min(Move,len(Neg))

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

minlen = -1
for t in range(1,posMaxRange+1):
    distance  = Pos[t-1]
    left = Move-t
    if left > 0:
        if len(Neg) < left:
            continue
        distance += Pos[t-1]
        distance += Neg[left-1]

    if minlen == -1:
        minlen = distance
    else:
        if minlen > distance:
            minlen = distance

for t in range(1,negMaxRange+1):
    distance = Neg[t-1]
    left = Move - t
    if left > 0:
        if len(Pos) < left:
            continue
        distance += Neg[t-1]
        distance += Pos[left-1]

    if minlen == -1:
        minlen = distance
    else:
        if minlen > distance:
            minlen = distance

print(minlen)

Submission status: Accepted

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

min_v = 10 ** 10
for i in range(N-K+1):

    v = min(abs(x[i]), abs(x[i+K-1])) + abs(x[i] - x[i+K-1])
    min_v = min(min_v, v)
    
print(min_v)

Submission status: Accepted

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

Submission status: Accepted

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

ans=10**9
for i in range(n-k+1):
    xl=x[i]
    xr=x[i+k-1]
    ans=min(ans,abs(xl)+xr-xl,abs(xr)+xr-xl)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
c=10**9
for i in range(n-k+1):
	h=x[i]
	m=x[i+k-1]
	H=abs(h)
	M=abs(m)
	if h*m<0:
		c=min(c,2*H+M,2*M+H)
	else:
		c=min(c,max(M,H))
print(c)

Submission status: Accepted

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

neg = 0
pos = 0

left = 0
right = 0

ans = 10**10

for l in range(N - K + 1):
    left = x[l]
    right = x[l + K - 1]
    neg = min(left, 0)
    pos = max(right, 0)
    if neg < 0 and pos > 0:
        ans = min(ans, abs(neg) * 2 + pos, abs(neg) + pos * 2)
    else:
        ans = min(ans, abs(neg) + pos)

# print('----------output----------')
print(ans)

Submission status: Accepted

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

x = list(map(int, input().split()))
ans = 10 ** 9
for i in range(len(x)-K+1):
  if x[i] > 0:
    ans = min(ans, x[i+K-1])
  elif x[i+K-1] < 0:
    ans = min(ans, abs(x[i]))
  else:
    ans = min(ans, min(abs(x[i]), abs(x[i+K-1])) + x[i+K-1]-x[i])
print (ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
x = 0
besttime = 10**9
while x + K - 1< N:
    if lst[x]*lst[x+K-1] < 0:
        time = min(abs(lst[x])*2+lst[x+K-1], abs(lst[x])+2*lst[x+K-1])
    else:
        time = max(abs(lst[x]), abs(lst[x+K-1]))
    if besttime > time:
        besttime = time
    x += 1
print(besttime)

Submission status: Accepted

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

dist = []
for i in range(N-K+1): 
    l, r = x[i], x[i+K-1]
    t = min(abs(l)+abs(l-r), abs(r)+abs(r-l))
    dist.append(t)

print(min(dist))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
# for i in range(n):
#     if x[i] >= 0:
#         ln = i
#         break
import bisect
ln=bisect.bisect_right(x,0)

l = [0] + x[:ln][::-1]
r = [0] + x[ln:]
ans = 1000000000000
for i in range(k + 1):
    if i >= len(l): continue
    if k - i >= len(r): continue
    t= -l[i] + r[k - i] + min(-l[i], r[k - i])
    ans = min(ans,t)
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
tmin = 10**13
for i in range(N-K+1):
    x = X[i]
    y = X[i+K-1]
    if y<0:
        T = -x
    elif x>0:
        T = y
    elif x<=0 and y>=0:
        T = min(2*(-x)+y,-x+2*y)
    tmin = min(tmin,T)
print(tmin)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

if 0 in x:
    K -= 1
    x.remove(0)

if K == 0:
    ans = 0
else: # Kが1以上
    ans = 10**9
    for i in range(N-K+1):
        left = i # 0からN-K
        right = i+K-1 # K-1からN-1
        ans = min(ans, x[right] - x[left] + min(abs(x[right]), abs(x[left])))

print(ans)

Submission status: Accepted

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

ans=10**9

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

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
x = list(map(int, input().split()))
left = []
right = []
for i in x:
    if i < 0:
        left.append(-i)
    elif i > 0:
        right.append(i)
    else:
        k -= 1
if k <= 0:
    print(0)
else:
    left = [0] + left[::-1]
    right = [0] + right
    ans = float("Inf")
    for i in range(k+1):
        # 左にi 右にk-i
        if len(left) > i and len(right) > k-i:
            tmp = left[i]+right[k-i] + min(left[i], right[k-i])
            ans = min(ans, tmp)
    print(ans)

Submission status: Accepted

Solution code:

n,k=map(int,input().split())
x=sorted(list(map(int,input().split())))
print(min((min(abs(x[i])+abs(x[i+k-1]-x[i]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))) for i in range(n-k+1)))

Submission status: Accepted

Solution code:
# WIP (WA: 1_01.txt, 1_06.txt, 1_07.txt)
import sys
input_s = sys.stdin.readline

N, K = map(int, input_s().split())
xxx = list(map(int, input_s().split()))

min_elapsed_time = 10**10
for left, right in zip(xxx, xxx[K - 1:]):
    elapsed_time = right - left + min(abs(left), abs(right))
    """
    right - left : a section distance
    min(abs(left), abs(right)) : a start point to move the section
    """
    min_elapsed_time = \
        elapsed_time if elapsed_time < min_elapsed_time else min_elapsed_time

print(min_elapsed_time)

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    N, K, *X = map(int, open(0).read().split())
    cand = [r - l + min(abs(l), abs(r)) for l, r in zip(X, X[K - 1 :])]
    ans = min(cand)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
x = list(map(int,input().split()))
#l = 0#最後はl = N - K
#r = K - 1#最後はr = N - 1
ans = 10 ** 15

for i in range(N - K + 1):#K個の連続したろうそくの選び方で全探索
  l = i 
  r = l + K - 1
  sum1 = abs(x[l]) + abs(x[r] - x[l])
  sum2 = abs(x[r]) + abs(x[l] - x[r])#l地点とr地点のどちらに先に行くか
  ans = min(ans, sum1, sum2) 
  
print(ans)

Submission status: Accepted

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