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:
INF = 10**15
N,K = map(int,input().split())
x = list(map(int,input().split()))
num = 0
positive = []
negative = []
for i in range(N):
    if x[i] == 0:
        N  -= 1
    elif x[i] < 0:
        negative.append(x[i])
    else:
        positive.append(x[i])
negative = negative[::-1]
dist = INF
if len(positive) >= K :
    dist = positive[K-1]
    
for i in range(len(negative)):
    if len(positive) > 0 and len(positive) >= K - (i+1) :
        tmp = - 2*negative[i] + positive[K-1-i-1]
        if dist > tmp:
            dist = tmp
            

if len(negative) >= K and dist > -negative[K-1]:
    dist  = -negative[K-1]

for i in range(len(positive)):
    if len(negative) > 0 and len(negative) >= K - (i+1) :
        tmp = -negative[K-1-i-1] + 2*positive[i]
        if dist > tmp:
            dist = tmp
if N == 0:
    print(x[0])
else:
    print(dist)

Submission status: Accepted

Solution code:
def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))
    res = 1 << 30
    for i in range(k - 1, n):
        d = xs[i] - xs[i - k + 1] + min(abs(xs[i]), abs(xs[i - k + 1]))
        res = min(res, d)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

d = 10**10
for i in range(n-k+1):
    x_l = l[i]
    x_r = l[i+k-1]

    if x_l >= 0:
        tmp = x_r
    elif x_r <= 0:
        tmp = -x_l
    else:
        tmp = min(abs(x_r), abs(x_l)) + abs(x_l-x_r)

    d = min(d, tmp)

print(d)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
if (0 in x):
    k-=1
else:
    x.append(0)
    x.sort()

zeroPos=x.index(0)
leftLen=zeroPos
rightLen=len(x)-zeroPos-1
# print(leftLen,zeroPos,rightLen)
# print(x)
left=[]
right=[]
for turn in range(leftLen+1):
    if(turn>k):
        break
    if(k-turn<=rightLen):
        left.append(x[zeroPos-turn]*-2 + x[zeroPos+k-turn])
for turn in range(rightLen+1):
    if(turn>k):
        break
    if(k-turn<=leftLen):
        right.append(2*x[zeroPos+turn] + -1*x[zeroPos-k+turn])
minL=min(left)
minR=min(right)
# print(left,right)
print(minL if minL<minR else minR)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
li = list(map(int,input().split()))
time = 10**10

for i in range(N-K+1):
    if li[i+K-1] <= 0:
        time = min(time,abs(li[i]))
    if li[i]<= 0 and li[i+K-1]>=0:
        time = min(min(abs(li[i]),abs(li[i+K-1]))+li[i+K-1]-li[i],time)
    if li[i]>=0:
        time = min(time,abs(li[i+K-1]))

print(time)

Submission status: Accepted

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

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

if 0 in x:
  K -= 1
  x.remove(0)
  
if K <= 0:
  print(0)
  exit()
if K == 1:
  print(min(abs(i) for i in x))
  exit()
  
px = [i for i in x if i > 0]
mx = [i for i in x if i < 0]


  
lp = len(px)
lm = len(mx)

mn = (abs(x[0]) + abs(x[len(x)-1])) * 2
if lp >= K:
  t = px[K-1]
  if t < mn:
    mn = t
if lm >= K:
  t = -mx[-K]
  if t < mn:
    mn = t
for i in range(max(lm-K+1, 0), min(lm,len(x)-K+1)):
  tm = -x[i]
  tp = x[i+K-1]
  t = min(tm, tp) * 2 + max(tm, tp)
  if t < mn:
    mn = t
  
print(mn)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
C = list(map(int, input().split()))
D = []

for i in range(n - k + 1):
    l = C[i]
    r = C[i + k -1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    dist = min(route1, route2)
    D.append(dist)
    
print(min(D))

Submission status: Accepted

Solution code:
import bisect

N,K = list(map(int,input().strip().split()))
xlist = list(map(int,input().strip().split()))
ans = int(10e10)

if 0 not in xlist:
    bisect.insort_left(xlist,0)
else:
    K -= 1

if (bisect.bisect_left(xlist,0)+1) > K:
    del xlist[0:(bisect.bisect_left(xlist,0) - K)]
if len(xlist) < (bisect.bisect_left(xlist,0) + K):
    del xlist[(bisect.bisect_left(xlist,0)+K +1):]

for i in range(len(xlist)-K):
    low = min(abs(xlist[i]),abs(xlist[i+K]))
    high = max(abs(xlist[i]),abs(xlist[i+K]))
    ans = min(ans,low*2+high)

print(ans)

Submission status: Accepted

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

ans=10**11
for i in range(0,len(x)-K+1):
    if x[i]*x[K-1+i] <0:
        anss=abs(x[i])+ abs(x[K-1+i])
        anss += min(abs(x[i]),abs(x[K-1+i]))
    else:
        anss=max(abs(x[i]), abs(x[K-1+i]))
    ans=min( ans, anss)

print(ans)

Submission status: Accepted

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

for i in range(n - k + 1):
    l = X[i]
    r = X[i + k - 1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    temp = min(route1, route2)
    T.append(temp)
    
print(min(T))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x_list = list(map(int, input().split()))
cand = []

for i in range(n - k + 1):
    l = x_list[i]
    r = x_list[i + k - 1]
    route1 = abs(l) - l + r
    route2 = abs(r) - l + r
    cand.append(min(route1, route2))
    
print(min(cand))

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)):
    L = x[i]
    R = x[i+K-1]
    route1 = abs(L) + R - L
    route2 = abs(R) + R - L
    temp = min(route1, route2)
    ans = min(ans, temp)
    
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):
  l=X[i]
  r=X[i+k-1]
  route1=abs(l)+r-l
  route2=abs(r)+r-l
  tmp=min(route1,route2)
  ans=min(ans,tmp)
print(ans)

Submission status: Accepted

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

x.append(0)
x.sort()

#print(x)

min = 10**10

zeroIdx = x.index(0)

for i in range(zeroIdx-k,zeroIdx+1):
    if i < 0 : 
        continue
    elif len(x) <= i + k :
        break

    plsIdx = i + k 
    misIdx = i

    #print(misIdx,plsIdx,x[misIdx],x[plsIdx])

    if abs(x[plsIdx]) > abs(x[misIdx]) :
        tmp = abs(x[plsIdx]) + (abs(x[misIdx])<<1)
    else:
        tmp = (abs(x[plsIdx])<<1) + abs(x[misIdx])

    if tmp < min :
        min = tmp

print(min)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
min_time = 10**14
for i in range(N-K+1):
    l_edge = X[i]
    r_edge = X[i+K-1]
    route1 = abs(l_edge) + r_edge - l_edge
    route2 = abs(r_edge) + r_edge - l_edge
    time = min(route1,route2)
    if time < min_time:
        min_time = time
print(min_time)

Submission status: Accepted

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

Submission status: Accepted

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

def dist(i):
    a = abs(X[K - 1 + i]) + X[K - 1 + i] - X[i]
    b = abs(X[i]) + X[K - 1 + i] - X[i]
    return min(a, b)

ans = dist(0)
for i in range(N):
    if i + K > N:
        break
    ans = min(ans, dist(i))
print(ans)

Submission status: Accepted

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

print(mini)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
pos = []
neg = []
for i in x:
    if i < 0:
        neg.append(-i)
    else:
        pos.append(i)
neg = neg[::-1]

pos = [0] + pos
neg = [0] + neg

#print(pos)
#print(neg)

# posにいってからneg
res = float('inf')
for i in range(min(len(pos),k)):
    # posからi本灯して, negからK-i本灯す
    j = k - i
    if j >= len(neg):
        continue
    tmp = pos[i] * 2 + neg[j]
    res = min(res, tmp)

# negにいってからpos
for i in range(min(len(neg),k)):
    j = k - i
    if j >= len(pos):
        continue
    tmp = neg[i] * 2 + pos[j]
    res = min(res, tmp)

print(res)

Submission status: Accepted

Solution code:
m,k=map(int,input().split())
lst=list(map(int,input().split()))
if 0 in lst:
    k-=1
    if k==0:
        print(0)
        exit()
p=list(filter(lambda x:x>0,lst))
n=list(map(abs,filter(lambda x:x<0,lst)))
n.sort()
ans=[]
if len(n)>=k:
    ans.append(n[k-1])
i=min(len(n),k-1)-1
while i>=0 and 0<=k-i-2<len(p):
    ans.append(n[i]*2+p[k-i-2])
    i-=1
i=min(len(p),k-1)-1
if len(p)>=k:
    ans.append(p[k-1])
while i>=0 and 0<=k-i-2<len(n):
    ans.append(p[i]*2+n[k-i-2])
    i-=1
print(min(ans))

Submission status: Accepted

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

ans = float('inf')
for left in range(n-k+1):
    right = left + k - 1
    if x[right] <= 0:
        ans = min(ans, abs(x[left]))
    elif x[left] >= 0:
        ans = min(ans, x[right])
    else:
        ans = min(ans, abs(x[left]) * 2 + x[right])
        ans = min(ans, abs(x[left]) + x[right] * 2)

print(ans)

Submission status: Accepted

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

ans = 10**18
for l in range(N-K+1):
    r = l+K-1
    if X[l]*X[r] >= 0:
        tmp = max(abs(X[l]), abs(X[r]))
    else:
        tmp1 = abs(X[l])*2 + abs(X[r])
        tmp2 = abs(X[r])*2 + abs(X[l])
        tmp = min(tmp1, tmp2)
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
x = list(li())

ans = 10**18

for st in range(n-k+1):
    ed = st+k-1
    cand = max(abs(x[st]), abs(x[ed]))
    if x[st] * x[ed] < 0:
        cand += 2 * min(abs(x[st]), abs(x[ed]))
        
    ans = min(ans,cand)
    
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]

ll=len(l)
rr=len(r)
lx=sum(l)
rx=sum(r)

if ll<k: lx=lx*2+sum(r[:(k-ll)])
if rr<k: rx=rx*2+sum(l[:(k-rr)])
ans=min(lx,rx)
o=[ll,[0,k-ll][ll<k]]
p=[rr,[0,k-rr][rr<k]]


for i in range(1,k):
    if o[1]==0: lx*=2
    if p[1]==0: rx*=2
    if o[1]+1<=rr and o[0]>0:
        lx-=l[o[0]-1]*2
        lx+=r[o[1]]
        o[0]-=1
        o[1]+=1
        ans=min(ans,lx)
    if p[1]+1<=ll and p[0]>0:
        rx-=r[p[0]-1]*2
        rx+=l[p[1]]
        p[0]-=1
        p[1]+=1
        ans=min(ans,rx)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, k = list(map(int, input().split()))
    x = list(map(int, input().split()))
    ans = float('inf')
 
    for i in range(n - k + 1):
        left = x[i]
        right = x[i + k - 1]
 
        if left * right >= 0:
            ans = min(ans, max(abs(left), abs(right)))
        else:
            ans = min(ans, 2 * abs(left) + abs(right), abs(left) + 2 * abs(right))
 
    print(ans)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
N, K = map(int, input().split())
x = [int(s) for s in input().split()]

izero = bisect_left(x, 0)
zpos = x[izero:]
neg = [-v for v in reversed(x[:izero])]

ans = 1000000000
if len(neg) >= K and ans > neg[K - 1]:
    ans = neg[K - 1]
if len(zpos) >= K and ans > zpos[K - 1]:
    ans = zpos[K - 1]
for lenneg in range(max(1, K - len(zpos)), min(K - 1, len(neg)) + 1):
    cand = 2 * neg[lenneg - 1] + zpos[K - lenneg - 1]
    if ans > cand:
        ans = cand
    cand = neg[lenneg - 1] + 2 * zpos[K - lenneg - 1]
    if ans > cand:
        ans = cand
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int,input().split())
x = list(map(int,input().split()))
time = 10**9
x_k = [0, 0]
for i in range(n - k + 1):
    x_k[0] =  x[i] 
    x_k[1] =  x[i+k-1] 
    #print(x_k)
    
    if x_k[0] >= 0:
        t = x_k[1]
            
    elif x_k[1] <= 0:
        t = -x_k[0]
    
    else:
        t = min(-x_k[0] + x_k[1] *2,-x_k[0] * 2 + x_k[1])

    time = min(t, time)
    
print(time)

Submission status: Accepted

Solution code:
nk = list(map(int, input().split()))
n = nk[0]
k = nk[1]
x = list(map(int, input().split()))
min_d = 10 ** 9
for i in range(n - k + 1):
    max_i = x[i + k - 1]
    min_i = x[i]
    if max_i <= 0:
        min_d_i = - min_i
    elif min_i >= 0:
        min_d_i = max_i
    else:
        min_d_i = min(- min_i + 2 * max_i, -2 * min_i + max_i)
    if min_d_i < min_d:
            min_d = min_d_i
print(min_d)

Submission status: Accepted

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

result = []
for i in range(N-K+1):
    s,g = i,i+K-1
    result.append(abs(x[s]) + abs(x[s] - x[g]))
    result.append(abs(x[g]) + abs(x[g] - x[s]))
result.sort()
print(result[0])

Submission status: Accepted

Solution code:
N, K = [int(item) for item in input().split()]
candles = [int(item) for item in input().split()]
ans = []
for i in range(N-K+1):
  l, r = i, i+K-1
  ans.append(abs(candles[l]) + abs(candles[l] - candles[r]))
  ans.append(abs(candles[r]) + abs(candles[r] - candles[l]))
ans.sort()
print(ans[0])

Submission status: Accepted

Solution code:
N,K = map(int,input().split(" "))
cdn = list(map(int,input().split(" ")))
n = N - K + 1
neko = 0
ans = []
i = 0
while i < n:
  if abs(cdn[i]) >= abs(cdn[i+K-1]):
    neko = abs(cdn[i+K-1]) + cdn[i+K-1] - cdn[i]
    ans.append(neko)
  else:
     neko = abs(cdn[i]) + cdn[i+K-1] - cdn[i]
     ans.append(neko)
  i = i + 1
ans.sort()
print(ans[0])

Submission status: Accepted

Solution code:
N, K = [int(_) for _ in input().split()]
xs = [int(_) for _ in input().split()]
L = []
R = [0]
for i in range(N):
    if xs[i] < 0:
        L += [-xs[i]]
    elif xs[i] > 0:
        R += [xs[i]]
    else:
        K -= 1
L = [0] + L[::-1]

ans = 10 ** 9
for i in range(0, len(L)):
    if 0 <= K - i and K - i < len(R):
        ans = min(ans, 2 * L[i] + R[K - i], L[i] + 2 * R[K - i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

min_time = 2 * abs(candles[0] - candles[-1])

for i in range(N):
    end_index = i + K - 1
    if end_index >= N:
        break
    left_end = candles[i]
    right_end = candles[end_index]
    d = abs(right_end - left_end)

    time = min(abs(left_end) + d, abs(right_end) + d)
    min_time = min(time, min_time)

print(min_time)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
iN,iK = [int(_) for _ in input().split()]
aX = [int(_) for _ in input().split()]
ans = 10**9
iDiff = iN-iK
for i in range(iDiff + 1):
    iL = aX[i]
    iR = aX[iN-iDiff+i-1]
    ans = min(ans ,abs(iL) + abs(iL-iR),abs(iR) + abs(iR-iL))
print(ans)

Submission status: Accepted

Solution code:

#とりあえず全検索
iN,iK = [int(_) for _ in input().split()]
aX = [int(_) for _ in input().split()]
aP = [_ for _ in aX if  _ >= 0]
aM = [-1*_ for _ in aX if  _ < 0][::-1]
iP = len(aP)
iM = len(aM)
if iP == 0:
    print(aM[iK-1])
    exit()
elif iM == 0:
    print(aP[iK-1])
    exit()
iD = 2*aP[-1]+2*aM[-1]
for i in range(min(iP-1,iK-1),-1,-1):
    if iK - 2 -i < 0:
        iD = min(iD,aP[i])
    elif iM > iK -2 - i :
        iD = min(iD,aP[i] + 2*aM[iK-i-2])
for i in range(min(iM-1,iK-1),-1,-1):
    if iK - 2 - i < 0:
        iD = min(iD,aM[i])
    elif iP > iK -2 - i :
        iD =  min(iD,aM[i] + 2*aP[iK-i-2])
print(iD)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
ANS = []
for i in range(len(L)-K+1) :
    p = L[i]
    q = L[i+K-1]
    if p*q == 0 :
        r = abs(p-q)
    elif p*q < 0 :
        r = 2*min(abs(p),q) + max(abs(p),q)
    else :
        if p > 0 :
            r = q
        else :
            r = abs(p)
    ANS.append(r)
ans = min(ANS)
print(ans)

Submission status: Accepted

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

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


for i in range(n):
    if l[i] >= 0:
        ind = i

left = min(n-1,i+k)

ans = 1e10

while left >= k-1:
    if l[left]*l[left-k+1]>=0:
        d = max(abs(l[left]),abs(l[left-k+1]))
    else:
        a = abs(l[left])
        b = abs(l[left-k+1])
        d = min(2*a+b,a+2*b)
    if d < ans:
        ans=d
    left -=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

sum = 10 ** 8 * 3 + 1
for i in range(n - k + 1):
    cur = arr[i + k - 1] - arr[i] + min(abs(arr[i + k - 1]), abs(arr[i]))
    if sum > cur:
        sum = cur
print sum

Submission status: Accepted

Solution code:
global x
def get(left,right):
    if(x[left]*x[right]<0):
        return min([2*abs(x[left]) +x[right], 2*abs(x[right]) + abs(x[left])])

    else:
        return max([abs(x[left]),x[right]])


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


tmp_min=get(0,k-1)
for i in range(1,n):
    if(i+k-1 >=n):
        break
    tmp_min=min([tmp_min ,get(i,i+k-1)])
    if(x[i]>=0):
        break
    

print(tmp_min)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

    n, p = [], []
    for x in sorted(X):
        if x == 0:
            K -= 1
        elif x < 0:
            n.append(x)
        else:
            p.append(x)

    ans = INF
    n = n[::-1]
    for i in range(K + 1):
        j = K - i
        if i <= len(n) and j <= len(p):
            a, b = 0, 0
            if i > 0:
                a = abs(n[i - 1])
            if j > 0:
                b = abs(p[j - 1])
            ans = min(ans, a + b + min(a, b))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = [int(i) for i in input().split()]
y = list(map(abs,x))
xl=0
xr=0
loop=n-k+1
ans=100000000000

for i in range(loop):
    if(x[i]*x[i+k-1]>=0):
        tmp=max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
    else:
        tmp=2*min(y[i],y[i+k-1])+max(y[i],y[i+k-1])
        if(ans>tmp):
            ans=tmp
print(ans)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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

mn = 0xFFFFFFFF
for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    a = abs(l) + abs(r - l)
    b = abs(r) + abs(r - l)
    mn = min(a, b, mn)
print(mn)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N,K=(int(i) for i in input().split(' '))
x=[int(i) for i in input().split(' ')]
time=None
index=0
l=len(x)
while True:
  if x[index]==0:
    K-=1
    x.remove(index)
  elif x[index]>0:
    break
  index+=1
  if index==l:
    break
x.insert(index,0)
if K==0:
  time=0
for i in range(max(0,K-len(x)+index+1),min(K+1,index+1)):
  if -x[index-i]>x[index+K-i]:
    time_tmp=-x[index-i]+2*x[index+K-i]
  else:
    time_tmp=-2*x[index-i]+x[index+K-i]
  if time==None:
    time=time_tmp
  if time_tmp<time:
    time=time_tmp
print(time)

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):
    l,r = i,i+K-1
    ans = min(abs(x[r])+abs(x[l]-x[r]),min(ans,abs(x[l])+abs(x[r]-x[l])))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect


def m():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = [abs(i) for i in a]
    right = bisect.bisect_right(a,0)
    left = right-1
    ans = 99999999999
    for i in range(n-k+1):
        s = 0
        if left >= i+k-1:
            s = b[i]
        elif right <= i:
            s = b[i+k-1]
        else:
            s = min(b[i] + b[i+k-1]*2, b[i]*2 + b[i+k-1])
        ans = min(ans, s)
    return ans

print(m())

Submission status: Accepted

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

shortest = float("inf")
for i in range(N - K + 1):
    left = x[i]
    right = x[i + K - 1]
    left_first = abs(left) + abs(left - right)
    right_first = abs(right) + abs(left - right)
    shortest = min(shortest, left_first, right_first)
print(shortest)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = float('inf')

for i in range(n - k + 1):
	if arr[i] * arr[k + i -1] >= 0:
		tmp = max(abs(arr[i]), abs(arr[k + i -1]))
	else:
		tmp = abs(arr[i]) + abs(arr[k + i -1]) + min(abs(arr[i]), abs(arr[k + i -1]))
	ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

m = np.sum(np.array(a) < 0) - 1

cs = list(np.abs(a))
result = []
for s, e in zip(range(0, n - k + 1), range(k - 1, n + 1)):
    if(s <= m and e > m):
        left = cs[s]
        right = cs[e]

        result.append(left * 2 + right)
        result.append(left + right * 2)
    elif s <= m:
        result.append(cs[s])
    else:
        result.append(cs[e])
print(min(result))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return [int(i) for i in input().split()]

n,k = inpl()
C =inpl()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
if 0 in a:
  a.remove(0)
  n -= 1
  k -= 1
if k == 0:
  print(0)
  exit()
m = 300000000
for i in range(n+1-k):
  if a[i+k-1] < 0:
    x = -a[i]
  elif a[i] > 0:
    x = a[i+k-1]
  else:
    x = (a[i+k-1]-a[i]) + min(-a[i],a[i+k-1])
  m = min(m,x)
print(m)

Submission status: Accepted

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

for i in range(N-K+1):
	length=abs(x[i]-x[i+K-1])
	cost.append(length+abs(x[i]))
	cost.append(length+abs(x[i+K-1]))
print(min(cost))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N - K + 1):
    l = i
    r = i + K - 1

    Xl, Xr = X[l], X[r]
    if i == 0:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr))
    else:
        ans = min(abs(Xl) + abs(Xr - Xl), abs(Xr) + abs(Xl - Xr) , ans)


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
candles = list(map(int, input().split()))
abs_c = [abs(c) for c in candles]
result = []
for n in range(N-K+1):
    r1 = abs(candles[n]) + abs(candles[n+K-1]-candles[n])
    r2 = abs(candles[n+K-1]) + abs(candles[n]-candles[n+K-1])
    result.append(min(r1, r2))

print(min(result))

Submission status: Accepted

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

shortest = float("inf")
for i in range(N - K + 1):
    left = x[i]
    right = x[i + K - 1]
    left_first = abs(left) + abs(left - right)
    right_first =  abs(right) + abs(left - right)
    shortest = min(shortest, left_first, right_first)
print(shortest)

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):
    a = X[i]
    b = X[i+K-1]
    if a * b > 0:
        v = max(abs(a), abs(b))
    else:
        v = abs(a) + abs(b) + min(abs(a), abs(b))
    ans = min(ans, v)
print(ans)

Submission status: Accepted

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

if __name__ == "__main__":
    N,K = map(int,input().split(" "))
    place = list(map(int,input().split(" ")))
    time = []
    for i in range(N-K+1):
        left = place[i]
        right = place[i+K-1]
        n1 = abs(left) + abs(right - left)
        n2 = abs(right) + abs(left - right)
        time.append(min(n1,n2))
    print(min(time))

Submission status: Accepted

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