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: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(time)

Submission status: Wrong Answer

Solution code:
import bisect
N, K = map(int,input().split())
x = list(map(int,input().split()))
ans = float("inf")
if x[0]*x[-1] >= 0:
    if x[0] >= 0:
        ans = x[K-1]
    else:
        ans = x[-K]
    print(ans)
else:
    M = []
    P = []
    for e in x:
        if e > 0:
            P.append(e)
        else:
            M.append(-e)
    M.sort()
    M = [0] + M
    P = [0] + P
    for k in range(max(len(P),len(M))):
        if k < len(P) and 0 <= K-k < len(M):
#            print(M[K-k],P[k])
            ans = min(ans,2*M[K-k]+P[k],M[K-k]+2*P[k])
    print(ans)

Submission status: Wrong Answer

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

for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    if l < 0 and r > 0:
        if abs(l) >= abs(r):
            total = abs(l) + abs(r) * 2
        else:
            total = abs(l) * 2 + abs(r)
    elif l >= 0:
        total = r
    elif r <= 0:
        total = abs(l)
    time = min(time, total)

print(time)

Submission status: Wrong Answer

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

for i in range(n - k + 1):
    l = x[i]
    r = x[i + k - 1]
    if l < 0 and r > 0:
        if abs(l) >= abs(r):
            total = abs(l) + abs(r) * 2
        else:
            total = abs(l) * 2 + abs(r)
    elif l >= 0:
        total = r
    else:
        total = abs(l)
    time = min(time, total)

print(time)

Submission status: Wrong Answer

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

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

if k == 1:
    min_num = 10**20
    for i in x:
        if abs(i) < min_num:
            min_num = abs(i)
    print(min_num)
    exit()

min_dist = 10**20

dist = 0

for i in range(n-k):
    dist = (x[i+k-1] - x[i])
    dist += min(abs(x[i+k-1]), abs(x[i]))
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Submission status: Wrong Answer

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

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

min_dist = 10**20

dist = 0

for i in range(n-k):
    dist = (x[i+k-1] - x[i])
    dist += min(abs(x[i+k-1]), abs(x[i]))
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ls = []
rs = []
for x in X:
    if x < 0:
        ls.append(-x)
    else:
        rs.append(x)
ls.reverse()

ans = float('inf')
if len(ls) >= K:
    ans = min(ans, ls[K-1])
if len(rs) >= K:
    ans = min(ans, rs[K-1])

for l in range(1,K):
    if l >= len(ls): break
    r = K-l
    if r >= len(rs): continue
    ans = min(ans, ls[l-1]*2 + rs[r-1])
    ans = min(ans, rs[r-1]*2 + ls[l-1])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ls = []
rs = []
for x in X:
    if x < 0:
        ls.append(-x)
    else:
        rs.append(x)
ls.reverse()

ans = float('inf')
if len(ls) >= K:
    ans = min(ans, ls[K-1])
if len(rs) >= K:
    ans = min(ans, rs[K-1])

for l in range(1,K):
    r = K-l
    if l >= len(ls) or r >= len(rs): break    
    ans = min(ans, ls[l-1]*2 + rs[r-1])
    ans = min(ans, rs[r-1]*2 + ls[l-1])
print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    x = [int(x) for x in input().split()]
    answer = float("inf")
    if 1 < n:
        for i in range(n - k):
            if x[i + k - 1] < 0:
                answer = min(answer, -x[i])
            elif 0 < x[i]:
                answer = min(answer, x[i + k - 1])
            else:
                answer = min(answer, min(abs(x[i]), abs(x[i + k - 1])) + x[i + k - 1] - x[i])
    else:
        answer = abs(x[0])
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n, k = na()
x = na()

def dist(l, r):
    mn = min(abs(l), abs(r))
    mx = max(abs(l), abs(r))
    if l < 0 and r < 0:
        return mn
    elif l >= 0 and r >= 0:
        return mx
    else:
        return mn * 2 + mx

ans = inf
for i in range(n - k + 1):
    ans = min(ans, dist(x[i], x[i + k - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
Alist = list(map(int, input().strip().split()))
p = 1
for i in range(len(Alist)-1):
    if Alist[i]*Alist[i+1] <= 0:
        q = i
        p = p*(-1)
length = 10**10
ans2 = 10**10
if p == 1:
    if Alist[0] > 0:
        ans = Alist[K-1]
    if Alist[0] < 0:
        ans = -Alist[0]
if p == -1:
    for i in range(len(Alist)):
        if i + K - 1 < len(Alist):
            if i + K - 1 == q:
                length = min(abs(Alist[i]),length)
            elif i == q + 1:
                length = min((Alist[i+K-1]),length)
            else:
                length = min(min(abs(Alist[i+K-1]-Alist[i])+abs(Alist[i]),abs(Alist[i+K-1]-Alist[i]),abs(Alist[i+K-1])),length)
    ans = length
for i in range(len(Alist)):
    ans2 = min(ans2,abs(Alist[i]))
if K == 1:
    print(ans2)
else:
    if len(Alist) > 1:
        print(ans)
    if len(Alist) ==1:
        print(Alist[0])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
Alist = list(map(int, input().strip().split()))
p = 1
for i in range(len(Alist)-1):
    if Alist[i]*Alist[i+1] <= 0:
        q = i
        p = p*(-1)
length = 10**10
ans2 = 10**10
if p == 1:
    if Alist[0] > 0:
        ans = Alist[K-1]
    if Alist[0] < 0:
        ans = -Alist[0]
if p == -1:
    for i in range(len(Alist)):
        if i + K - 1 < len(Alist):
            if i + K - 1 == q:
                length = min(abs(Alist[i]),length)
            elif i == q + 1:
                length = min((Alist[i+K-1]),length)
            else:
                length = min(min(2*Alist[i+K-1]-Alist[i],Alist[i+K-1]-2*Alist[i]),length)
    ans = length
for i in range(len(Alist)):
    ans2 = min(ans2,abs(Alist[i]))
if K == 1:
    print(ans2)
else:
    if len(Alist) > 1:
        print(ans)
    if len(Alist) ==1:
        print(Alist[0])

Submission status: Wrong Answer

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[k-1+i]*x[i] < 0:
    if x[k-1+i] <= abs(x[i]) :
      temp = 2*x[k-1+i] - x[i]
    elif abs(x[i]) < x[k-1+i]:
      temp = x[k-1+i] - 2*x[i]
      
  elif (0 <= x[i]):
    temp = x[k+i-1]

  elif (x[k+i-1] <= 0):
    temp = -x[0]
    
  if temp < ans:
    ans = temp
    
print(ans)

Submission status: Wrong Answer

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[k-1+i]*x[i] < 0:
    if x[k-1+i] <= abs(x[i]) :
      temp = 2*x[k-1+i] - x[i]
    elif abs(x[i]) < x[k-1+i]:
      temp = x[k-1+i] - 2*x[i]
      
  elif 0 <= x[i]:
    temp = x[k+i-1]

  elif x[k-1] <= 0:
    temp = -x[0]
    
  if temp < ans:
    ans = temp
    
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
Alist = list(map(int, input().strip().split()))
p = 1
for i in range(len(Alist)-1):
    if Alist[i]*Alist[i+1] < 0:
        q = i
        p = p*(-1)
    if Alist[i]*Alist[i+1] == 0:
        s = i
        p = p*0
length = 10**10
ans2 = 10**10
if p == 1:
    if Alist[0] > 0:
        ans = Alist[K-1]
    if Alist[0] < 0:
        ans = -Alist[0]
if p == -1:
    for i in range(len(Alist)):
        if i + K - 1 < len(Alist):
            if i + K - 1 == q:
                length = min(abs(Alist[i]),length)
            elif i == q + 1:
                length = min((Alist[i+K-1]),length)
            else:
                length = min(min(2*Alist[i+K-1]-Alist[i],Alist[i+K-1]-2*Alist[i]),length)
    ans = length
if p == 0:
    for i in range(len(Alist)):
        if i + K - 1 < len(Alist):
            if i + K - 1 == s:
                length = min(abs(Alist[i]),length)
            elif i == s + 1:
                length = min((Alist[i+K-1]),length)
            else:
                length = min(min(2*Alist[i+K-1]-Alist[i],Alist[i+K-1]-2*Alist[i]),length)
    ans = length
for i in range(len(Alist)):
    ans2 = min(ans2,abs(Alist[i]))
if K == 1:
    print(ans2)
else:
    if len(Alist) > 1:
        print(ans)
    if len(Alist) ==1:
        print(Alist[0])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
Alist = list(map(int, input().strip().split()))
p = 1
for i in range(len(Alist)-1):
    if Alist[i]*Alist[i+1] < 0:
        q = i
        p = p*(-1)
    if Alist[i]*Alist[i+1] == 0:
        s = i
        p = p*0
length = 10**10
if p == 1:
    if Alist[0] > 0:
        ans = Alist[K-1]
    if Alist[0] < 0:
        ans = -Alist[0]
if p == -1:
    for i in range(len(Alist)):
        if i + K - 1 < len(Alist):
            if i + K - 1 == q:
                length = min(abs(Alist[i]),length)
            elif i == q + 1:
                length = min((Alist[i+K-1]),length)
            else:
                length = min(min(2*Alist[i+K-1]-Alist[i],Alist[i+K-1]-2*Alist[i]),length)
    ans = length
if p == 0:
    for i in range(len(Alist)):
        if i + K - 1 < len(Alist):
            if i + K - 1 == s:
                length = min(abs(Alist[i]),length)
            elif i == s + 1:
                length = min((Alist[i+K-1]),length)
            else:
                length = min(min(2*Alist[i+K-1]-Alist[i],Alist[i+K-1]-2*Alist[i]),length)
    ans = length
if len(Alist) > 1:
    print(ans)
if len(Alist) ==1:
    print(Alist[0])

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N, K = i_l()
x = i_l()

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

i_i = lambda: int(i_s())
i_l = lambda: list(map(int, stdin.readline().split()))
i_s = lambda: stdin.readline().rstrip()

N, K = i_l()
x = i_l()

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lis = list(map(int, input().split()))
su = []

for i in range(n):
    su.append(lis[i] - lis[0])

print(su)
res = 10**9
for i in range(n - m + 1):
    res = min(res, su[i + m - 1] - su[i] + min(abs(lis[i]), abs(lis[i + m - 1])))
print(res)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
lis = list(map(int, input().split()))
su = []

for i in range(n):
    su.append(lis[i] - lis[0])

res = 10**9
for i in range(n - m + 1):
    res = min(res, su[i + m - 1] - su[i] + abs(lis[i]))
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
xs = list(map(int, input().split()))
xs.append(xs[-1])
ans = float('inf')

for i in range(N-K+1):
    tmp = (abs(xs[i+K-1] - xs[i]))
    if xs[i]*xs[i+K-1] < 0 or xs[i] > 0: 
        tmp += min(abs(xs[i]), abs(xs[i+K-1]))
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
xs = list(map(int, input().split()))
xs.append(xs[-1])
ruiseki = []

prev = xs[0]
for i in range(N-K+1):
    ruiseki.append(abs(min(xs[i:i+K]) - max(xs[i:i+K])) + abs(min(xs[i:i+K])))
print(min(ruiseki))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,k = map(int, input().split())
    x = list(map(int, input().split()))
    p, n = [], []
    for i in x:
        if  i > 0:
            p.append(i)
        elif i < 0:
            n.append(-i)
    p.sort()
    n.sort()
    lenp = len(p)
    lenn = len(n)
    if lenp == 0 and lenn == 0:
        print(0)
        exit()
    if lenn == 0:
        print(p[k-1])
        exit()
    if lenp == 0:
        print(n[k-1])
        exit()
    res = 10000000000000000000000
    if lenp > lenn:
        for i in range(k-lenn-1, lenn):
            res = min(res, n[i]+p[k-i-2]+min(n[i], p[k-i-2]))
        print(res)
    else:
        for i in range(k-lenp-1, lenp):
            res = min(res, p[i]+n[k-i-2]+min(p[i], n[k-i-2]))
        print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, k, *x = map(int, open(0).read().split())
    d = 2 * 10 ** 8
    for i, j in zip(x, x[k - 1:]):
        d = min(d, j - i + min(abs(i), abs(j)))
    print(d)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
if k == 1 and 0 in s:
  print(0)
  exit()
if (n-k)<=1:
  print(abs(s[0]-s[-1])+min(abs(s[0]),abs(s[-1])))
  exit()
a = 10**9
for i in range(n-k):
  m = abs(s[i]-s[i+k-1])+min(abs(s[i]),abs(s[i+k-1]))
  if m<a:
    #print(s[i],s[i+k-1])
    a=m
print(a)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
s = list(map(int,input().split()))
if (n-k)<1:
  print(abs(s[0]-s[-1])+min(abs(s[0]),abs(s[-1])))
  exit()
a = 10**9
for i in range(n-k):
  m = abs(s[i]-s[i+k-1])+min(abs(s[i]),abs(s[i+k-1]))
  if m<a:
    #print(s[i],s[i+k-1])
    a=m
print(a)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i > 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()
    if 0 in X:
        K -= 1
        if K == 0:
            return 0

    ans = 10**50

    if K == 1:
        return min(A1[0], A2[0])

    for i in range(N):
        i1 = i-1
        i2 = K-i-1

        #print(i1, i2, len(A1), len(A2))
        #print(A1, A2)
        if i1 < -1 or i2 < -1 or i1 >= len(A1) or i2 >= len(A2):
            continue

        if i1 == -1:
            a = 0
        else:
            a = A1[i1]

        if i2 == -1:
            b = 0
        else:
            b = A2[i2]

        ans = min(ans, a*2+b, b*2+a)

    #print(A1)
    #print(A2)
    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i > 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()
    if 0 in X:
        K -= 1
        if K == 0:
            return 0

    ans = 10**50
    if len(A1) >= K:
        ans = A1[K-1]
    if len(A2) >= K:
        ans = min(A2[K-1], ans)

    if K == 1:
        return min(A1[0], A2[0])

    for i in range(1, 10**10):
        i1 = i-1
        i2 = K-i-1
        if i1 >= len(A1):
            break
        if i2 >= len(A2) or i2 < 0:
            break

        #print(i1, i2)
        ans = min(A1[i1] + A2[i2] * 2, ans)
        ans = min(A1[i1] * 2 + A2[i2], ans)

    #print(A1)
    #print(A2)
    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i > 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()
    if 0 in X:
        K -= 1
        if K == 0:
            return 0

    ans = 10**50
    if len(A1) >= K:
        ans = A1[K-1]
    if len(A2) >= K:
        ans = min(A2[K-1], ans)

    for i in range(1, 10**10):
        i1 = i-1
        i2 = K-i-1
        if i1 >= len(A1):
            break
        if i2 >= len(A2) or i2 < 0:
            break

        #print(i1, i2)
        ans = min(2 * A1[i1]+A2[i2], ans)
        ans = min(2 * A2[i2]+A1[i1], ans)

    #print(A1)
    #print(A2)
    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()