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

time = []
for i in range(n - k + 1):
    l, r = x[i], x[i + k - 1]
    if l * r < 0:
        time.append(min(abs(l), r) + r - l)
    else:
        time.append(max(abs(l), abs(r)))
print(min(time))

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 = abs(x[i]) + abs(x[i+k-1] - x[i])
    r = abs(x[i+k-1]) + abs(x[i+k-1] - x[i])

    if l < r:
        ans = min(ans, l)
    else:
        ans = min(ans, r)

print(ans)

Submission status: Accepted

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

lengthlist=[]

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

#print(lengthlist)
print(min(lengthlist))

Submission status: Accepted

Solution code:
n, k = [ int(v) for v in input().split() ]
coordinate_list = sorted([ int(v) for v in input().split() ]) 

def fire(a,b):
    if a * b >= 0:
        r = max(abs(a),abs(b))
    else:
        r = abs(a) + abs(b) + min(abs(a),abs(b))
    return r

ans = 10**14
for i in range(n-k+1):
    ans = min(fire(coordinate_list[i],coordinate_list[i+k-1]),ans)
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):
    temp_ans1 = abs(x[i]) + abs(x[i+K-1]-x[i])
    temp_ans2 = abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    # temp_ans2 = abs(x[N-1-i]) + abs(x[N-1-i-K]-x[N-1-i])
    # temp_ans2 = abs(x[-(i+1)]) +abs(x[-(i+K)] - x[i+1])
    ans = min(ans,temp_ans1,temp_ans2)
    # ans = min(ans,temp_ans1    ans = min(ans,temp_ans1,temp_ans2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    x = list(map(int, input().split()))
    ans = abs(x[0]) + abs(x[K-1] - x[0])
    ans = min(ans, abs(x[K-1]) + abs(x[0] - x[K-1]))
    for i in range(1, 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] - x[i+K-1]))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = []
for i in range(n-k+1):
    ans.append(min(abs(l[i]),abs(l[i+k-1]))+abs(l[i]-l[i+k-1]))
print(min(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, i = LI()
    print(n-i+1)
    return

#B
def B():
    h, w = LI()
    a = SR(h)
    dy = defaultdict(int)
    dx = defaultdict(int)
    for y in range(h):
        for x in range(w):
            if a[y][x] == "#":
                dx[x] = 1
                dy[y] = 1
    dy = list(sorted(dy.items(), key=lambda x: x[0]))
    dx = list(sorted(dx.items(), key=lambda x: x[0]))
    for y,_ in dy:
        for x,_ in dx:
            print(a[y][x], end="")
        print()
    return

#C
def C():
    n, k = LI()
    x = LI()
    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])))
            continue
        ans = min(ans, min(abs(x[i]), abs(x[i + k - 1])) * 2 + max(abs(x[i]), abs(x[i + k - 1])))
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

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):
	time = min(abs(X[i])+abs(X[i+K-1]-X[i])\
		,abs(X[i+K-1])+abs(X[i+K-1]-X[i]))
	ans = min(ans,time)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
def main(): #startline------------------------------------------
    N,K = map(int,input().split())
    x = list(map(int,input().split()))
    ans = float("inf")
    flag = False
    for i in range(N-K+1):
        flag = True
        l = x[i]
        r = x[K+i-1]
        p = abs(l)+abs(r-l)
        q = abs(r)+abs(r-l)
        s = min(p,q)
        if ans > s:
            ans = s
    print(ans if flag else abs(x[0]))
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

n, k = map(int, input().split())
li = list(map(int, input().split()))
li.append(0)
li.sort()
zero_index = li.index(0)
if zero_index == 0:
    print(li[k])
elif zero_index == len(li) -1:
    print(abs(li[-k-1]))
else:
    near_zero_small = li[zero_index-1]
    near_zero_big = li[zero_index+1]
    li.pop(zero_index)
    ans = 0
    cost = 0
    for i in range(n-k+1):
        minimam = li[i]
        maximam = li[i+k-1]
        if (minimam > 0 and maximam > 0 ):
            if minimam == near_zero_big:
                cost = maximam
        elif (minimam < 0 and maximam < 0 ):
            if minimam == near_zero_big:
                cost = abs(minimam)
        else:
            cost = min(abs(minimam), maximam)*2 + max(abs(minimam), maximam)
        if ans > cost or ans == 0:
            ans = cost
    print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
INF=10**9+7
if 0 in l:k=k-1
lm=[0]+[i for i in l if i<0][::-1]
if len(lm)<k+1:
    lm=lm+[-INF]*(k+1-len(lm))
lp=[0]+[i for i in l if i>0]
if len(lp)<k+1:
    lp=lp+[INF]*(k+1-len(lp))
ans=INF*10
for i in range(k+1):
    tmp=min(2*lp[i]-lm[k-i],lp[i]-2*lm[k-i])
    if tmp<ans:ans=tmp
print(ans)

Submission status: Accepted

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

a = []

for i in range(n-k+1):
    l = x[i]
    r = x[i+k-1]
    h = abs(l-r)
    s = min(h + abs(l), h + abs(r))
  #  print(s)
    a.append(s)

#print(a)           
print(min(a))

Submission status: Accepted

Solution code:
n,k=map(int, input().split())
x=list(map(int, input().split())) 
v=[]
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]-x[i+k-1])
    v.append(min(a,b))
print(min(v))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

minusx = []
plusx = []
for x in l:
    if x < 0:
        minusx.append(x)
    else:
        plusx.append(x)

m = len(minusx)
a = len(plusx)
res = float("inf")
if m == 0:
    res = plusx[k-1]
elif a == 0:
    res = abs(minusx[-k])
else:
    for i in range(1,k+1):
        if  i <= a and  k-i <= m:
            p = plusx[i-1]
            q = abs(minusx[-(k-i)])
            t = max(p,q)+min(p,q)*2
            if t < res:
                res = t
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

List = []
for j in range(N-K+1):
    if X[j]<=0 and X[j+K-1]>=0:
        List.append(X[j+K-1]-X[j]+min(abs(X[j]),X[j+K-1]))
    else:
        List.append(max(abs(X[j]),abs(X[j+K-1])))

print(min(List))

Submission status: Accepted

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

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

ans = 2*(X[N-1] - X[0])
for i in range(N-K+1):
    zerodim = min(abs(X[i]), abs(X[i+K-1]))
    dis = Xs[K+i-1] - Xs[i]
    #print(X[i], X[i+K-1])
    #print(zerodim)
    #print(dis)
    ans = min(dis + zerodim, 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):
    ans = min(ans, X[i+K-1] - X[i] + min(abs(X[i]), abs(X[i+K-1])))
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int, input().split())
X = []
idx = 0
for i,x in enumerate(input().split()):
    x = int(x)
    X.append(x)
    if x < 0:
        idx = i + 1
if idx == N:
    idx = N-1
ans = 10**9
for i in range(max(0,idx-K), min(N-K+1,idx+K)):
    ans = min(ans, X[i+K-1] - X[i] + min(abs(X[i]), abs(X[i+K-1])))
print(ans)

Submission status: Accepted

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

X = []
idx = 0
for i,x in enumerate(input().split()):
    x = int(x)
    X.append(x)
    if x < 0:
        idx = i + 1
if idx == N:
    idx = N-1
ans = 10**9
for i in range(max(0,idx-K), min(N-K+1,idx+K)):
    tmp = 0
    if i <= idx <= i+K-1 or i >= idx or i+K-1 <= idx:
        tmp = min(abs(X[i]), abs(X[i+K-1]))
    # print(i,tmp)
    ans = min(ans, X[i+K-1] - X[i] + tmp)
print(ans)

Submission status: Accepted

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

ans = 9999999999999

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

Submission status: Accepted

Solution code:
n,c=map(int,input().split())
can=list(map(int,input().split()))

res=[0]*(n-c+1)

for i in range(n-c+1):
	s=can[i]
	e=can[i+c-1]
	if s>=0 and e>=0:
		res[i]=e
	else:
		if s<=0 and e<=0:
			res[i]=-s
		else:
			if s<=0 and e>=0:
				if abs(s)>=e:
					res[i]+=-s+e*2
				else:
					res[i]+=-s*2+e
print(min(res))

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
dat_x = list(map(int, input().split()))
l = 0
r = k - 1
total = 0
for i in range(1, k):
    # print("{0} - {1}".format(dat_x[i] , dat_x[i - 1] ))
    # print("abc: {0}".format(abs(dat_x[i] - dat_x[i - 1])))
    total += abs(dat_x[i] - dat_x[i - 1])

total += min(abs(dat_x[l]), abs(dat_x[r]))

m = total
# print("m = {0}".format(m))

#print("check: {0}, {1} = {2}".format(l, r, total))
r += 1
l += 1

while r < n:
    #print("!!!!!!!!")
    #print(dat_x[l])
    #print(dat_x[l-1])
    total -= min(abs(dat_x[l-1]), abs(dat_x[r-1]))
    #print("check-0: {0}, {1} = {2}".format(l, r, total))
    total -= abs(dat_x[l] - dat_x[l - 1])
    #print("check-1: {0}, {1} = {2}".format(l, r, total))
    total += abs(dat_x[r] - dat_x[r - 1])
    #print("check-2: {0}, {1} = {2}".format(l, r, total))
    total += min(abs(dat_x[l]), abs(dat_x[r]))

    m = min(m, total)
    r += 1
    l += 1

print(m)

Submission status: Accepted

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

def calc_costs(l, r):
    axl = abs(x[l])
    axr = abs(x[r])
    if 0 <= x[l] and 0 <= x[r]:
        return x[r]
    elif x[l] <= 0 and x[r] <= 0:
        return abs(x[l])
    elif axl < axr:
        return axl * 2 + axr
    else:
        return axl + axr * 2

l = 0
r = K-1
min_costs = calc_costs(l, r)
for i in range(1, N-K+1):
    l += 1
    r += 1
    costs = calc_costs(l, r)
    if costs < min_costs:
        min_costs = costs
print(min_costs)

Submission status: Accepted

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

# min( 左端に到達し右へ, 右端に到達し左へ)
ans=10**9
for i in range(N-K+1):
    l=abs(x[i])+abs(x[i]-x[i+K-1])
    r=abs(x[i+K-1])+abs(x[i]-x[i+K-1])
    ans=min(ans,min(l,r))
print(ans)

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = list(map(int, sys.stdin.readline().split()))
X = list(map(int, sys.stdin.readline().split()))
ans = INF
for i in range(N - K + 1):
    L = X[i]
    r = X[i + K - 1]
    if r <= 0:
        ans = min(ans, abs(L))
    elif 0 <= L:
        ans = min(ans, abs(r))
    else:
        al = abs(L)
        ar = abs(r)
        ans = min(ans, min(al, ar) * 2 + max(al, ar))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC107 C
n,k = map(int,input().split())
x = list(map(int,input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = list(map(int, input().split()))
data = list(map(int, input().split()))
result = 10**9
for i in range(N-M+1):
  if data[i] < 0 and data[i+M-1] >= 0:
    result = min([data[i+M-1]-data[i]*2, 
                  data[i+M-1]*2-data[i],
                  result])
  elif data[i+M-1] >= 0:
    result = min([data[i+M-1], result])
  else:
    result = min([-data[i], result])
print(result)

Submission status: Accepted

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

time = 10 ** 10
for i in range(N - K + 1):
    if candle[i] < 0 and candle[i + K-1] > 0:
        if abs(candle[i]) < candle[i + K-1]:
            kouho = 2 * abs(candle[i]) + candle[i + K-1]
        else:
            kouho = abs(candle[i]) + 2 * candle[i + K-1]
    else:
        kouho = max(abs(candle[i]), abs(candle[i + K-1]))

    if kouho < time:
        time = kouho
print(time)

Submission status: Accepted

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


def comp(k, lst):
    temp =10**9
    for i in range(N-k+1):
        temp = min(temp, min(abs(lst[i]-lst[i+k-1])+abs(lst[i]), abs(lst[i]-lst[i+k-1])+abs(lst[i+k-1])))
    return temp

print(comp(K, X))

Submission status: Accepted

Solution code:
inf = float('inf')

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

m, p = [], []
for xx in x:
    if xx > 0:
        p.append(xx)
    elif xx < 0:
        m.append(-xx)
    else:
        K -= 1
        if K == 0:
            print(0)
            exit()
m.sort()
lp = len(p)
lm = len(m)

ans = inf
for from_minus in range(min(lm, K) + 1):
    from_plus = K - from_minus
    if from_plus > lp:
        continue
    if from_minus == 0:
        ans = min(ans, p[K - 1])
    elif from_minus == K:
        ans = min(ans, m[K - 1])
    else:
        ans = min(ans, p[from_plus - 1] * 2 + m[from_minus - 1])
        ans = min(ans, m[from_minus - 1] * 2 + p[from_plus - 1])
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

si = bisect_left(x, 0)

ans = (x[-1] - x[0]) * 2
if si + k - 1 < len(x):
    ans = min(ans, x[si + k - 1])
if si - k >= 0:
    ans = min(ans, abs(x[si - k]))

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
if 0 in x:
    x.remove(0)
    k -= 1
    n -= 1

if n == 0:
    print(0)
else:
    si = bisect_left(x, 0)

    ans = (x[-1] - x[0]) * 2
    if si + k - 1 < len(x):
        ans = min(ans, x[si + k - 1])
    if si - k >= 0:
        ans = min(ans, abs(x[si - k]))

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

    print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    m = 10**10
    for i in range(n - k + 1):
        m = min(m, abs(x[i + k - 1] - x[i]) + abs(x[i]))
        m = min(m, abs(x[i + k - 1] - x[i]) + abs(x[i + k - 1]))
    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
if N == 1:
    ans = abs(X[0])
else:
    tmp = 0
    ans = 10**9
    for left in range (N-K+1):
        right = left + K - 1
        if X[right] <= 0:
            tmp = 0 - X[left]
        elif X[left] >= 0:
            tmp = X[right]
        else:
            tmp = min(X[right] - 2 * X[left], 2 * X[right] - X[left])
        ans = min(tmp, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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


ans = float("INF")
for i in range(N - K + 1):
    l = lst_x[i]
    r = lst_x[i + K - 1]
    dx = r - l
    d = min(abs(l) + dx, abs(r) + dx)
    ans = min(ans, d)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int, input().split()))
ans = 10 **10
rx = x[k-1:]
for i, j in zip(x,rx):
    if ans >= abs(i -j) + min(abs(i),abs(j)):
        ans = abs(i -j) + min(abs(i),abs(j))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k-1
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_left)

print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = sorted(list(map(int, input().split())) + [0])
ans = 10**9 + 10

for i in range(n-k+1):
    left = i
    right = i+k
    to_right = abs(x[left]) + abs(x[right]-x[left])
    to_left = abs(x[right]) + abs(x[right]-x[left])
    if min(to_right, to_left) < ans:
        ans = min(to_right, to_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, abs(x[i + K - 1] - x[i]) + min(abs(x[i + K - 1]), abs(x[i])))

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
N, K = [int(x) for x in stdin.readline().rstrip().split()]
data = [int(x) for x in stdin.readline().rstrip().split()]
ans = []
for i in range(N - K + 1):
    #print(data[i],data[i+K-1])
    ans.append(data[i+K-1] - data[i] + abs(data[i+K-1]))
    ans.append(data[i+K-1] - data[i] + abs(data[i]))
print(min(ans))

Submission status: Accepted

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

point= len(x) - 1
for i, v in enumerate(x):
    if v == 0:
        point = i
        break
    elif v > 0:
        point = i - 1
        break

start = max(0, point - K + 1)
end = start + K - 1

if x[point] != 0:
    point += 1
    
ans = float('inf')

while True:
    if x[start] >= 0:
        time = x[end]
    elif x[end] <= 0:
        time = abs(x[start])
    elif abs(x[start]) <= x[end]:
        time = abs(x[start] * 2) + x[end]
    else:
        time = abs(x[start]) + (x[end] * 2)
    ans = min(ans, time)
    
    start += 1
    end += 1

    if start > point or end == len(x):
        break
    
print(ans)

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    x = tuple(map(int, input().rstrip().split()))
    ans = float("inf")
    for l, r in zip(x, x[k - 1 :]):
        ans = min(ans, r - l + min(abs(l), abs(r)))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    _, k = map(int, input().split())
    x = tuple(map(int, input().split()))
    print(min(r - l + min(abs(l), abs(r)) for l, r in zip(x, x[k - 1 :])))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC107C - Candles (ARC101C)
def main():
    n, k = tuple(map(int, input().rstrip().split()))
    x = tuple(map(int, input().rstrip().split()))
    ans = float("inf")
    for i in range(n - k + 1):
        a, b = x[i], x[i + k - 1]
        if a < b < 0 or 0 < a < b:
            ans = min(ans, max(abs(a), abs(b)))
        else:
            ans = min(ans, max(abs(a), abs(b)) + min(abs(a), abs(b)) * 2)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = float("inf")
for i in range(N - K + 1):
    l = x[i]
    r = x[i + K - 1]
    ans = min(ans,min(abs(r)+abs(r-l),abs(l)+abs(l-r)))
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
c=list(map(int,input().split()))
ans=10**100
for i in range(n-k+1):
  l,r=i,i+k-1
  d=min(abs(c[l]),abs(c[r]))+abs(c[l]-c[r])
  if d<ans:
    ans=d
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
ans = 10**9
for l in range(N-K+1):
  r = l + K - 1
  tmp = x[r]-x[l]
  if x[r] < 0:
    tmp += abs(x[r])
  elif x[l] < 0 and 0 <= x[r]:
    tmp += min(abs(x[l]), x[r])
  else:
    tmp += x[l]
  ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

    retval = float("inf")
    for i in range(0, N - (K - 1)):
        left = x[i]
        right = x[i + K - 1]
        time = max(abs(left), abs(right))
        if abs(left + right) != abs(left) + abs(right):
            time += min(abs(left), abs(right)) * 2
        retval = min(retval, time)
    print(retval)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

sorted_lis = sorted(x_lis)

output = [0] * (N - K + 1)
for i in range(N - K + 1):
    if sorted_lis[i] < 0 and sorted_lis[i + K - 1] > 0:
        first = abs(sorted_lis[i]) * 2 + sorted_lis[i + K -1]
        second = abs(sorted_lis[i + K - 1]) * 2 - sorted_lis[i]
        #print(first, second)
        output[i] = min(first, second)
    elif sorted_lis[i] < 0 and sorted_lis[i + K - 1] <= 0:
        output[i] = -sorted_lis[i]
        #print(output[i])
    elif sorted_lis[i] >= 0 and sorted_lis[i + K - 1] > 0:
        output[i] = sorted_lis[i + K - 1]
        #print(output[i])
print(min(output))

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

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=abs(a[0])*2+abs(a[-1])*2#とりあえず最大
for i in range(n-k+1):
  x,y=a[i],a[i+k-1]
  if x*y<0:m=min(abs(x),y)*2+max(abs(x),y)
  else:m=max(abs(x),abs(y))
  ans=min(ans,m)
print(ans)

Submission status: Accepted

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

cost=10**13

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

print(cost)

Submission status: Accepted

Solution code:
import bisect

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

ub = bisect.bisect(x, 0)
result = 10 ** 9
for left in range(min(K, ub), -1, -1):
    right = K - left
    if right > N - ub:
        break
    if left > 0:
        left_time = -x[ub - left]
    else:
        left_time = 0
    if right > 0:
        right_time = x[ub + right - 1]
    else:
        right_time = 0
    left_right = 2 * left_time + right_time
    right_left = 2 * right_time + left_time
    result = min(result, min(left_right, right_left))
print(result)

Submission status: Accepted

Solution code:
import numpy as np
N,K = map(int,input().split())
X = np.array(input().split(), dtype=np.int64)

INF = 10 ** 18
neg = np.concatenate([-X[X <= 0][::-1], np.full(N+1,INF,dtype=np.int64)])[:K]
pos = np.concatenate([X[X > 0], np.full(N+1,INF,dtype=np.int64)])[:K]

answer = INF
# 左のみ
answer = min(answer, neg[-1])
# 右のみ
answer = min(answer, pos[-1])
if K >= 2:
  # 左にa個、右にK-a個
  answer = min(answer, (neg[:K-1] * 2 + pos[K-2::-1]).min())
  # 右にa個、右にK-a個
  answer = min(answer, (pos[:K-1] * 2 + neg[K-2::-1]).min())

print(answer)