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()))
ans=float('inf')
for i in range(N-K+1):
  l,r=x[i],x[i+K-1]
  if r<=0:
    d=-l
  elif l<=0:
    d=min((-l)*2+r,(-l)+r*2)
  else:
    d=r
  ans=min(ans,d)
print(ans)

Submission status: Accepted

Solution code:
# author:  Taichicchi
# created: 29.09.2020 22:54:27

import sys

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

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


ans = 10 ** 18

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

print(ans)

Submission status: Accepted

Solution code:
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        if num_list != [0]:
            k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k <= len(positive_list):
    ans_list.append(positive_list[k - 1])

if k <= len(negative_list):
    ans_list.append(-1 * negative_list[k - 1])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

Submission status: Accepted

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

time = 10 ** 10
for i in range(0, n-k+1):
  p = a[i]
  q = a[i+k-1]
  if q < 0:
    t = abs(p)
  elif p > 0:
    t = q
  else:
    t = abs(p) + q + min(abs(p), q)
  if t < time:
    time = t

print(time)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
def input():
  return sys.stdin.readline().rstrip()

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

ans=1<<60
for i in range(N-K+1):
  left=x[i]
  right=x[i+K-1]
  a=min(abs(left),abs(right))+right-left
  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 = 10**9

for i in range(N-K+1):
    left = x_list[i]
    right = x_list[i+K-1]
    ans_temp = min(abs(left), abs(right)) + right - left
    ans = min(ans, ans_temp)

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
m = 3*10**8

for i in range(n-k+1):
    a = x[i]
    b = x[i+k-1]
    t= min(abs(a)+abs(b-a), abs(b)+abs(b-a))
    if m > t:
        m = t
print(m)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
if n==1:
  print(abs(int(input())))
  exit()
a = list(map(int, input().split()))
g = []
for i in range(n-k+1):
  b = a[i]
  c = a[i+k-1]
  if c<=0:
    g.append(-b)
  elif b>=0:
    g.append(c)
  else:
    g.append(c-b+min(abs(b),abs(c)))
print(min(g))

Submission status: Accepted

Solution code:
N,K =(int(i) for i in input().split())
x = list((int(i) for i in input().split()))
i = 0
t_min = float("inf")
while i+K-1 <= N-1 :
  left = abs(x[i])+abs(x[i+K-1]-x[i])
  right = abs(x[i+K-1])+abs(x[i+K-1]-x[i])
  if min(left,right)<t_min:
    t_min = min(left,right)
  i += 1
print(t_min)

Submission status: Accepted

Solution code:
from collections import deque 
N, K = map(int,input().split())
Xlist = list(map(int,input().split()))
Ans = 10**13
for i in range(len(Xlist)-K+1):
    Xleft = Xlist[i]
    Xright = Xlist[i+K-1]
    Dis = min(abs(Xleft),abs(Xright))+(Xright-Xleft)
    Ans = min(Dis,Ans)
print(Ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
XN, XP = [], []
for x in X:
    if x < 0: XN.append(-x)
    else: XP.append(x)
XN.reverse()
T = [0]
S = 0
for i, x in enumerate(XN):
    if i==K-1:
        T.append(x)
        break
    T.append(2*x)
res = float("inf")
for i in range(K):
    if K-1-i >= len(XP) or i>=len(T):
        continue
    res = min(T[i] + XP[K-1-i], res)

T = [0]
S = 0
for i, x in enumerate(XP):
    if i==K-1:
        T.append(x)
        break
    T.append(2*x)
for i in range(K):
    if K-1-i >= len(XN) or i>=len(T):
        continue
    res = min(T[i] + XN[K-1-i], res)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

xlm = []
xlp = []
for x in xl:
    if x < 0: xlm.append(x*(-1))
    elif x == 0: k-=1
    else: xlp.append(x)

xlm.sort()
ans = 10**9
for i in range(min(k,len(xlm))+1):
    if i == 0: lv = 0
    else: lv = xlm[i-1]
    
    rem = k-i
    if rem > len(xlp): continue
    if rem == 0: rv = 0
    else: rv = xlp[rem-1]
    v = lv*2+rv
    ans = min(ans,v)

for i in range(min(k,len(xlp))+1):
    if i == 0: rv = 0
    else: rv = xlp[i-1]
    
    rem = k-i
    if rem > len(xlm): continue
    if rem == 0: lv = 0
    else: lv = xlm[rem-1]
    v = lv+rv*2
    ans = min(ans,v)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n,k=map(int,input().split())
x=list(map(int,input().split()))
#1. 初期状態をつくる
deq = deque()
r_init = 0
for i in range(n):
  if len(deq) == k and x[i] > 0:
    break
  deq.append(x[i])
  r_init = i
  if len(deq) > k:
    deq.popleft()

left = min(deq[0], 0)
right = max(deq[-1], 0)
ans = 0
if right <= 0:
  ans = abs(left)
elif left >= 0:
  ans = abs(right)
else:
  ans = min(abs(left),abs(right))+abs(left)+abs(right)
#2.1個ずつ右のものを取ることを考える。0起点で往復したときの距離を保持する
#2-1.右からk個とるときまで続ける
for i in range(r_init+1, n):
  m = deq.popleft()
  deq.append(x[i])
  if m >= 0:
    break
  left = abs(deq[0])
  right = abs(x[i])
  ans = min(ans, min(left,right)+left+right)
#3.全パターンについて最小値を取る
print(ans)

Submission status: Accepted

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

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


ans=float('inf')
for i in range(n-k+1):
  ans=min(abs(x[i])+abs(x[i+k-1]-x[i]),ans)
  ans=min(abs(x[i+k-1])+abs(x[i+k-1]-x[i]),ans)
  
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):
    x1, xk = x[i], x[i + k - 1]
    if x1 * xk >= 0:
        t = max(abs(x1),abs(xk))
    elif x1 * xk < 0:
        t = min(abs(x1), abs(xk)) + abs(x1 - xk)
    ans = min(ans, t)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
INF =  float("inf")
import bisect

N, K = list(map(int, input().split()))
x = [-INF] + list(map(int, input().split()))

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 1001001001
for xi, xj in zip(x, x[k-1:]):
    ans = min(ans, min(abs(xi), abs(xj)) + abs(xi - xj))
print(ans)

Submission status: Accepted

Solution code:
import bisect

def main():
  n, k, *lst = map(int, open(0).read().split())
  z = bisect.bisect_left(lst, 0)
  if not z:
    print(lst[k - 1])
  elif z >= n - 1:
    print(-lst[-k])
  else:
    idx = max(z - k + 1, 0)
    print(min(min(abs(i), j) + j - i for i, j in zip(lst[idx:], lst[idx + k - 1:])))

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import bisect

def main():
  n, k, *lst = map(int, open(0).read().split())
  z = bisect.bisect_left(lst, 0)
  if not z:
    print(lst[k - 1])
  elif z >= n - 1:
    print(-lst[-k])
  else:
    res = float('inf')
    for i in range(max(z - k + 1, 0), min(z, n - k) + 1):
      tmp = lst[i + k - 1] - lst[i] + min(abs(lst[i]), lst[i + k - 1])
      if tmp < res:
        res = tmp
    print(res)

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import bisect

n, k, *lst = map(int, open(0).read().split())
z = bisect.bisect_left(lst, 0)
if not z:
  print(lst[k - 1])
  quit()
if z >= n - 1:
  print(-lst[-k])
  quit()
res = float('inf')
for i in range(max(z - k + 1, 0), min(z, n - k) + 1):
  tmp = lst[i + k - 1] - lst[i] + min(abs(lst[i]), lst[i + k - 1])
  if tmp < res:
    res = tmp
print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, x: "List[int]"):
    from bisect import bisect
    if 0 in x:
        x.remove(0)
        K -= 1
    if K == 0:
        return 0
    ans = float('inf')
    for l, r in zip(x, x[K-1:]):
        ans = min(ans, abs(r)+r-l)
        ans = min(ans, abs(l)+r-l)
    return ans

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(solve(N, K, x))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

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

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

S = [0]
for i in range(N-1):
  S.append(S[i]+X_dif[i])

#print(X, X_dif, S)

def f(x, y):
  return min(abs(x), abs(y))

for i in range(N-K+1):
  #print(ans, S[i+K-1] - S[i] + f(X[i], X[i+K-1]))
  ans = min(ans, S[i+K-1] - S[i] + f(X[i], X[i+K-1]))

print(ans)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,k=n1()
X=n1()

ans=float("inf")
for i in range(n-k+1):
    a=X[i]
    b=X[i+k-1]
    if a*b<0:
        tmp=min(abs(a)*2+abs(b),abs(a)+abs(b)*2)
    else:
        tmp=max(abs(a),abs(b))
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,k=n1()
X=n1()

x0=bisect_left(X,0)

if x0<n and X[x0]==0:
    k=k-1
    P=X[x0+1:]
    M=X[:x0]
else:
    P=X[x0:]
    M=X[:x0]

if k==0:
    print(0)
else:
    ans1=ans2=ans3=float("inf")
    if len(P)>=k:
        ans1=P[k-1]
    if len(M)>=k:
        ans2=-M[-k]

    for i in range(1,k):
        j=k-i
        if len(P)>=i and len(M)>=j:
            ans3=min(ans3,P[i-1]*2-M[-j],P[i-1]-2*M[-j])
    print(min(ans1,ans2,ans3))

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,k=n1()
X=n1()

x0=bisect_left(X,0)
if x0<n and X[x0]==0:
    k=k-1
    P=X[x0+1:]
    M=X[:x0]
else:
    P=X[x0:]
    M=X[:x0]

if k==0:
    print(0)
else:
    ans1=ans2=ans3=float("inf")
    if len(P)>=k:
        ans1=P[k-1]
    if len(M)>=k:
        ans2=-M[-k]

    for i in range(1,k):
        j=k-i
        if len(P)>=i and len(M)>=j:
            ans3=min(ans3,P[i-1]*2-M[-j],P[i-1]-2*M[-j])
    print(min(ans1,ans2,ans3))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
n,k = map(int,input().split())
x = [int(i) for i in input().split()]
l = []
r = []
for i in x:
  if i < 0:
    l.append(-i)
  else:
    r.append(i)
    
l = l[::-1]

if l == []:
  print(r[k-1])
elif r == []:
  print(l[k-1])
elif k == 1:
  print(min(r[0],l[0]))
else:
  L = len(l)
  R = len(r)
  ans = 3*10**8
  if L > k-1:
    ans = l[k-1]
  if R > k-1:
    ans = min(ans,r[k-1])
  for i in range(max(L,R)):
    if i < R and 0 <= k-2-i <= L-1 :
      ans = min(ans,r[i]*2+l[k-2-i])
    if i < L and 0 <= k-2-i <= R-1:
      ans = min(ans,l[i]*2+r[k-2-i])
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
list_X = list(map(int, input().split()))
ans = pow(10, 18)
for i in range(n-k+1):
    ans = min(ans, abs(list_X[i])+list_X[i+k-1]-list_X[i], abs(list_X[i+k-1])+list_X[i+k-1]-list_X[i])
if n == 1:
    ans = list_X[0]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

result=10**9

#プラスになる場所の特定
if xn[-1]<=0:
    result=-xn[-K]
elif xn[0]>=0:
    result=xn[K-1]
else:
    #プラスになる場所の特定
    for i in range(N):
        if xn[i]>=0:
            posi=i
            break
    
    for j in range(K-min(posi,K),min(K,N-posi)+1):
        if j==0:
            hantei=-xn[posi-K]

        elif j==K:
            hantei=xn[posi+K-1]
        else:
            hantei=min(xn[posi+j-1]*2-xn[posi-(K-j)],(xn[posi+j-1]-2*xn[posi-(K-j)]))
        result=min(hantei,result)

print(result)

Submission status: Accepted

Solution code:
N,M=list(map(int,input().split()))
n_l=list(map(int,input().split()))
ans=float("INF")
for i in range(N-M+1):
   ans=min(ans,abs(0-n_l[i])+abs(n_l[i]-n_l[i+M-1]),abs(0-n_l[i+M-1])+abs(n_l[i]-n_l[i+M-1]))
print(ans)

Submission status: Accepted

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

ans = float('inf')

for i in range(N-K+1):
    start = A[i]
    end = A[i+K-1]

    if start*end>0:
        dist = max(abs(start), abs(end))
    else:
        dist = abs(start)+abs(end)+min(abs(start), abs(end))

    ans = min(ans, dist)

print(ans)

Submission status: Accepted

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

timelist=[]
for i in range(n-(k-1)):
	bpoint=xlist[i+k-1]
	spoint=xlist[i]
	timelist.append(bpoint-spoint+min(abs(bpoint),abs(spoint)))

print(min(timelist))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dist=[]
for i in range(n-k+1):
  if a[i+k-1]<=0:
    dist.append(-a[i])
  elif a[i]>=0:
    dist.append(a[i+k-1])
  else:
    dist.append(min(2*a[i+k-1]-a[i],a[i+k-1]-2*a[i]))

print(min(dist))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
N_minus = len([m for m in x if m < 0])
ans = float('inf')

for i in range(N):
    left = i
    right = i + K - 1
    if right >= N:
        break

    if x[right] <= 0:
        ans = min(x[left] * (-1), ans)
    elif x[left] < 0:
        ans = min(x[left] * (-1) * 2 + x[right], ans)
        ans = min(x[left] * (-1) + x[right] * 2, ans)
    else:
        ans = min(x[right], ans)

print(ans)

Submission status: Accepted

Solution code:
from bisect import *
n,k=map(int,input().split())
c=list(map(int,input().split()))
a=bisect_left(c,0)
insort_left(c,0)
j=a
while 1:
  if j+1<=n:
    if c[j+1]==0:
      k-=1
      j+=1
    else:
      break
  else:
    break
ans=10**18
for i in range(k+1):
  if a+i<=n and a-k+i>=0:
    ans=min(ans,min(c[a+i],-c[a-k+i])*2+max(c[a+i],-c[a-k+i]))
if ans==10**18:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
def resolve():
    from bisect import bisect
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    g=bisect(x,0)
    if x[n-1]<=0:
        print(abs(x[n-k]))
    elif x[0]>=0:
        print(x[k-1])
    else:
        if g>=k:
            s=g-k
        else:
            s=0
        t=0
        while s<=n-k:
            if x[s]<0 and x[s+k-1]>0:
                if t==0:
                    t=min(2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
                else:
                    t=min(t,2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
            else:
                t=min(t,max(abs(x[s]),abs(x[s+k-1])))
            s+=1
        print(t)
resolve()

Submission status: Accepted

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

ans = 10**10
for i in range(N-K+1):
    if(a[i] < 0 and a[i+K-1] > 0):
        ans = min(ans, abs(a[i])*2+a[i+K-1], abs(a[i+K-1])*2+abs(a[i]))
    else:
        if(a[i] < 0):
            ans = min(ans, abs(a[i]))
        else:
            ans = min(ans, abs(a[i+K-1]))

if(N > 1):        
    print(ans)
else:
    print(a[0])

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
dist=[]
for i in range(n-k+1):
  if a[i+k-1]<=0:
    dist.append(-a[i])
  elif a[i]>=0:
    dist.append(a[i+k-1])
  else:
    dist.append(min(2*a[i+k-1]-a[i],a[i+k-1]-2*a[i]))

print(min(dist))

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):
    if x[i + K - 1] <= 0:
        tmp = -x[i]
    elif x[i] >= 0:
        tmp = x[i + K - 1]
    else:
        tmp = -x[i] * 2 + x[i + K - 1]
        tmp = min(tmp, -x[i] + 2 * x[i + K - 1])

    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 = float('inf')
for i in range(N-K+1):
  left = X[i]; right = X[i+K-1]
  if left >= 0:
    ans = min(ans, right)
    break
  elif right <= 0:
    ans = min(ans, -left)
  else:
    t = min(-left, right)*2 + max(-left, right)
    ans = min(ans, t)

print(ans)

Submission status: Accepted

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

#連続するk本に火をつけるのが最適
ans=float("inf")
for i in range(n-k+1):
    if x[i] <= 0 and x[i+k-1] >= 0:
        ans = min(abs(x[i])+abs(x[i+k-1]) + min(abs(x[i]),abs(x[i+k-1])),ans)
    if x[i] >= 0 and x[i+k-1] >= 0:
        ans = min(x[i+k-1],ans)
    if x[i] <= 0 and x[i+k-1] <= 0:
        ans = min(abs(x[i]),ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
N, K = map(int, input().split())
Xs = [int(s) for s in input().split()]

result = sys.maxsize
for i in range(len(Xs) - K + 1):
  a = abs(0 - Xs[i]) + Xs[i + K - 1] - Xs[i]
  b = abs(0 - Xs[i + K - 1]) + Xs[i + K - 1] - Xs[i]
  shortest = min(a, b)
  if shortest < result:
    result = shortest
    
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
x.sort()
ans=[]
for i in range(0,N-K+1):
    start=x[i]
    goal=x[i+K-1]
    if goal<0:
        ans.append(abs(start))
    elif start<0:
        test=min(2*abs(start)+goal,2*goal+abs(start))
        ans.append(test)
    else:
        ans.append(goal)
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
LOCAL = len(sys.argv) > 1 and sys.argv[1] == 'LOCAL'


def dprint(*args):
  if LOCAL:
    print(*args)


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

k -= x.count(0)

if k <= 0:
  print(0)
  exit()

n = [0]
for e in reversed(x):
  if e < 0:
    n.append(-e)

p = [0]
for e in x:
  if e > 0:
    p.append(e)


dprint(n, p)

anses = []

for i in range(k + 1):
  j = k - i
  if 0 <= i < len(p) and 0 <= j < len(n):
    anses.append(p[i] * 2 + n[j])
    anses.append(p[i] + n[j] * 2)

dprint(anses)

print(min(anses))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7


def main():
    # sys.stdin = open("sample.txt")
    N, K = Scanner.map_int()
    X = Scanner.map_int()
    left = 0
    right = K - 1
    ans = int(1e09)
    while right < N:
        if Math.sign(X[left]) == Math.sign(X[right]):
            ans = min(ans, max(abs(X[left]), abs(X[right])))
        else:
            ans = min(ans, abs(X[left]) * 2 + abs(X[right]))
            ans = min(ans, abs(X[left]) + abs(X[right]) * 2)
        left += 1
        right += 1
    print(ans)


if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

i = 0
ans = float("inf")
while  i+k - 1 < n:
    l = x[i]
    r = x[i+k-1]
    ans = min(ans, min(abs(l), abs(r)) + r - l)
    i += 1

print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
PO, NE = [0], []
for a in input().split():
    a = int(a)
    if a >= 0: PO.append(a)
    else: NE.append(-a)

NE = [0] + NE[::-1]
ans = 1<<99

for i in range(K+1):
    j = K - i
    if i < len(PO) and j < len(NE):
        ans = min(ans, PO[i] * 2 + NE[j], PO[i] + NE[j] * 2)
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N, K, *x = map(int, open(0).read().split())
    
    ans = 10 ** 9
    for l in range(N - K + 1):
        r = l + K - 1
        ans = min(ans, abs(x[l]) + abs(x[r] - x[l]), abs(x[r]) + abs(x[r] - x[l]))
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

for i in range(N - K + 1):
    x1 = abs(X[i]) + abs(X[K - 1 + i] - X[i])
    x2 = abs(X[K - 1 + i]) + abs(X[i] - X[K - 1 + i])
    
    x_tmp = min(x1, x2)
    x = min(x, x_tmp)

print(x)

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

md = 10**10
for i in range(N - K + 1):
    xl, xr = X[i], X[i + K - 1]
    if xl >= 0:
        d = xr
    elif xr <= 0:
        d = -xl
    else:
        d = min(-xl, xr) * 2 + max(-xl, xr)

    if d < md:
        md = d
print(md)

Submission status: Accepted

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

ans=10**9
for i in range(1,N-K+2):
    a,b=x[i],x[i+K-1]
    if b<=0:
        d=-a
    elif 0<=a:
        d=b
    elif a<0<b:
        d=-a+b+min(-a,b)
    ans=min(ans,d)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
N,K= map(int,input().split())
x= list(map(int,input().split()))

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