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=map(int,input().split())
X=[int(i) for i in input().split()]+[0]
X.sort()
sn=X.index(0)
road=[]
if sn+k<=n+1:
    for j in range(sn):
        if X[j+k]>0:
            road.append(abs(X[j]-0)+abs(X[j+k]-X[j]))
        else:
            road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))

    for j in range(sn+1,n-k+1):
        road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))
else:
    for j in range(n-k+1):
        road.append(abs(X[j]-0)+abs(X[j+k-1]-X[j]))
if len(road)>0:
    print(min(road))
else:
    print(0)

Submission status: Wrong Answer

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

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

if N == 1 or K == 1:
    print(x[0])
    exit()

ans = 10**8
for i in range(N):
    if i < N-K+1:
        tmp1 = abs(x[i]) + abs(x[i] - x[i+K-1])
        tmp2 = abs(x[i+K-1]) + abs(x[i] - x[i+K-1])
        ans = min(tmp1, tmp2, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, os, bisect

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n , k = f()
    a = f()

    ans = int(1e10)
    for i in range(n):
        if i+k-1<n:
            cur_ans = 0
            if a[i]* a[i+k-1]>=0:
                cur_ans = max(abs(a[i+k-1]),a[i])
            else:
                cur_ans = min(2*abs(a[i])+abs(a[i+k-1]), abs(a[i]) + 2*abs(a[i+k-1]))
            ans = min(ans, cur_ans)
    
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
import sys, os, bisect

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n , k = f()
    a = f()

    ans = int(1e10)
    for i in range(n):
        if i+k-1<n:
            cur_ans = 0
            if a[i]* a[i+k-1]>=0:
                cur_ans = abs(a[i+k-1])
            else:
                cur_ans = min(2*abs(a[i])+abs(a[i+k-1]), abs(a[i]) + 2*abs(a[i+k-1]))
            ans = min(ans, cur_ans)
    
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
import sys, os, bisect

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n , k = f()
    a = f()

    if 0 in a:
        k -= 1

    idx = bisect.bisect_right(a, 0)

    if idx == n or idx == 0:
        print(abs(a[k-1]))
        return
    minid = max(0, idx-k)
    maxid = min(n-k, idx)

    ans = int(1e10)
    for i in range(minid, maxid+1):
        mi = a[i]
        mx = a[i + k-1]
        cur_ans = 0
        if mi*mx>0:
            cur_ans = max(abs(mi), abs(mx))
        else:
            cur_ans = min(abs(mx) + 2*abs(mi), abs(mi) + 2*abs(mx))
        ans = min(ans, cur_ans)

    print(ans)

solve()

Submission status: Wrong Answer

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

ans = 100000000000
#最初と最後だけ調べればOK
for i in range(N - K + 1):
    tmp_min = x[i]
    tmp_max = x[i + K - 1]
    tmp = abs(tmp_min) + abs(tmp_min - tmp_max)
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

ans = 100000000000
#最初と最後だけ調べればOK
for i in range(N - K + 1):
    tmp_min = x[i]
    tmp_max = x[i + K - 1]
    ans = min(ans, abs(tmp_min) + abs(tmp_min - tmp_max))
print(ans)

Submission status: Wrong Answer

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

candidate = []
for i in range(N - K + 1):
    tmp = []
    for j in range(i, i + K):
        tmp.append(x[j])
    candidate.append(tmp)

#すべて+なら0
#すべて-なら-1
#混合なら1
check = 0
ans = 10000000
for i in candidate:
    tmp = 0
    #最初の要素がマイナスだとマイナスとプラスの混合の可能性がある
    if i[0] < 0:
        #混合の場合
        if i[-1] > 0:
            #マイナスに最後に行く場合
            if i[0] > i[-1]:
                tmp += abs(i[-1]) * 2
                tmp += abs[i[0]]
            #プラスに最後に行く場合
            else:
                tmp += abs(i[0]) * 2
                tmp += abs(i[-1])
        #混合じゃない
        tmp += i[0]
    else:
        #単純にすべて足す。
        tmp += i[-1]
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

candidate = []
for i in range(N - K + 1):
    tmp = []
    for j in range(i, i + K):
        tmp.append(x[j])
    candidate.append(tmp)

#すべて+なら0
#すべて-なら-1
#混合なら1
check = 0
ans = 10000000
for i in candidate:
    tmp = 0
    #最初の要素がマイナスだとマイナスとプラスの混合の可能性がある
    if i[0] < 0:
        check = -1
        for j in i:
            if j > 0:
                check = 1
                break
    if check == 1:
        #マイナスに最後に行く場合
        if candidate[0] > candidate[-1]:
            tmp += abs(i[-1]) * 2
            tmp += abs[i[0]]
        #プラスに最後に行く場合
        else:
            tmp += abs(i[0]) * 2
            tmp += abs(i[-1])
    else:
        #単純にすべて足す。
        tmp += i[-1]
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  if n==1 and k==1 and l[0]==0:
    exit()

  if l.count(0)>0:
    k-=1
    l.remove(0)

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

  if len(l)==0:
    print(0)
    exit()

  idx=len(l)-1
  for i in range(len(l)):
    if l[i]>0:
      idx=i
      break
  if max(l)<0:
    idx=len(l)

  l1=[abs(x) for x in l[idx:]]
  l2=[abs(x) for x in l[:idx]]
  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(len(l1)):
    if i+1>=k:
      mn=min(mn,l1[i])
    else:
      if len(l2)>=k-(i+1):
        # print(l1[i],l2[k-(i+1)-1])
        mn=min(mn,l1[i]+2*l2[k-(i+1)-1])

  for i in range(len(l2)):
    if i+1>=k:
      mn=min(mn,l2[i])
    else:
      if len(l1)>=k-(i+1):
        # print(l2[i],l1[k-(i+1)-1])
        mn=min(mn,l2[i]+2*l1[k-(i+1)-1])

  print(mn)

main()
# print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  if n==1 and k==1 and l[0]==0:
    exit()

  if l.count(0)>0:
    k-=1
    l.remove(0)

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

  if len(l)==0:
    print(0)
    exit()

  idx=len(l)-1
  for i in range(len(l)):
    if l[i]>0:
      idx=i
      break
  if min(l)<0:
    idx=len(l)

  l1=[abs(x) for x in l[idx:]]
  l2=[abs(x) for x in l[:idx]]
  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(len(l1)):
    if i+1>=k:
      mn=min(mn,l1[i])
    else:
      if len(l2)>=k-(i+1):
        # print(l1[i],l2[k-(i+1)-1])
        mn=min(mn,l1[i]+2*l2[k-(i+1)-1])

  for i in range(len(l2)):
    if i+1>=k:
      mn=min(mn,l2[i])
    else:
      if len(l1)>=k-(i+1):
        # print(l2[i],l1[k-(i+1)-1])
        mn=min(mn,l2[i]+2*l1[k-(i+1)-1])

  print(mn)

main()
# print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  if n==1 and k==1 and l[0]==0:
    exit()

  if l.count(0)>0:
    k-=1
    l.remove(0)

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

  if len(l)==0:
    print(0)
    exit()

  idx=len(l)-1
  for i in range(len(l)):
    if l[i]>0:
      idx=i
      break

  l1=[abs(x) for x in l[idx:]]
  l2=[abs(x) for x in l[:idx]]
  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(len(l1)):
    if i+1>=k:
      mn=min(mn,l1[i])
    else:
      if len(l2)>=k-(i+1):
        mn=min(mn,l1[i]+2*l2[k-(i+1)-1])

  print(mn)

main()
# print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  if n==1 and k==1 and l[0]==0:
    exit()

  if l.count(0)>0:
    k-=1
    l.remove(0)

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

  if len(l)==0:
    print(0)
    exit()

  idx=len(l)-1
  for i in range(len(l)):
    if l[i]>0:
      idx=i
      break

  l1=[abs(x) for x in l[idx:]]
  l2=[abs(x) for x in l[:idx]]
  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(len(l1)):
    if i+1>=k:
      mn=min(mn,l1[i])
    else:
      if len(l2)>=k-(i+1):
        mn=min(mn,l1[i]+2*l2[k-(i+1)-1])

  for i in range(len(l2)):
    if i+1>=k:
      mn=min(mn,l2[i])
    else:
      if len(l1)>=k-(i+1):
        mn=min(mn,l2[i]+2*l1[k-(i+1)-1])

  print(mn)

main()
# print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,k=LI()
  l=LI()

  if n==1 and k==1 and l[0]==0:
    exit()

  if l.count(0)>0:
    k-=1
    l.remove(0)

  if len(l)==0:
    print(0)
    exit()

  idx=len(l)-1
  for i in range(len(l)):
    if l[i]>0:
      idx=i
      break

  l1=[abs(x) for x in l[idx:]]
  l2=[abs(x) for x in l[:idx]]
  l2.reverse()

  # print(l1)
  # print(l2)

  mn=inf
  for i in range(len(l1)):
    if i+1>=k:
      mn=min(mn,l1[i])
    else:
      if len(l2)>=k-(i+1):
        mn=min(mn,l1[i]+2*l2[k-(i+1)-1])

  for i in range(len(l2)):
    if i+1>=k:
      mn=min(mn,l2[i])
    else:
      if len(l1)>=k-(i+1):
        mn=min(mn,l2[i]+2*l1[k-(i+1)-1])

  print(mn)

main()
# print(main())

Submission status: Wrong Answer

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

if N == K:
    ans = X[-1]-X[0]+min(abs(X[-1]),abs(X[0]))

for i in range(N-K):
    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:
def answer(n, k, x):
    ans = 2 * (x[n - 1] - x[0])
    
    for i in range(0, n - k):
        if abs(x[i]) < abs(x[i + k - 1]):
            temp = abs(x[i])
        else:
            temp = abs(x[i + k - 1])

        temp += abs(x[i + k - 1] - x[i])

        if temp < ans:
            ans = temp

    return ans


[n, k] = list(map(int, input().split()))
x = list(map(int, input().split()))    
print(str(answer(n, k, x)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
l = list(map(int, input().split()))
h = []
e = []
for i in range(N - K + 1):
  h.append(l[i + K -1] - l[i] + abs(l[i]))
  if l[i] <= 0:
      e.append(abs(l[i]))
f = []
if len(e) >=K:
    e.reverse()
    for i in range(len(e) - K+1):
        f.append(e[i+K-1])
a=min(h)
if len(e) >= K:
    b = min(f)
    print(min(a,b))
else:
    print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
INF = 1000000000
distances = [INF]*(2*(N-K+1))
for l in range(N-K+1):
    distances[l]=abs(X[l]) + abs(X[l] - X[l+K-1])
    distances[l+1]=abs(X[l+K-1]) + abs(X[l] - X[l+K-1])

print(min(distances))

Submission status: Wrong Answer

Solution code:
 # -*- coding: utf-8 -*-
"""
Created on Fri May 17 21:35:10 2019

@author: avina
"""

N , K = map(int, input().split())
l = list(map(int, input().split()))
sums = 0
p = l[0]
for i in range(1,K):
    sums += l[i] - p
    p = l[i]
m = sums
k = 0; p = 0
for i in range(1,N - K + 1):
    sums = sums - l[p + 1] + l[p] + l[K+i-1] - l[K+i -2] 
    if sums < m:
        m = sums
        k = i
    
    
  
e = l[k]
d = 0; u1 = False; u2 = False
for i in range(k+1,k+K):
    d += l[i] - e
    e = l[i]
    if l[i]> 0:
        u1=True
    if l[i] < 0:
        u2 = True
if u1 and u2:
    print(d)
else:
    print(d+(abs(l[k])))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri May 17 21:35:10 2019

@author: avina
"""

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

sums = sum(l[:K])
m = 1e10
k = 0
for i in range(N - K):

    if sums < m:
        m = sums
        k = i
    sums = sums + l[i + K] - l[i]
e = l[k]
d = 0; u1 = False; u2 = False
for i in range(k+1,k+K):
    d += l[i] - e
    e = l[i]
    if l[i]>= 0:
        u1=True
    if l[i] < 0:
        u2 = True
if u1 and u2:
    print(d)
else:
    print(d+l[k])

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = 1e9
        for i in range(K+1):
            if i == 0:
                min = abs(negatives[-K])
            elif i > 0 and i < K:
                ne = abs(negatives[-K+i])
                po = positives[i-1]
                min = calc(ne, po, min)
            elif i == K:
                tmp = positives[K-1]
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[K+i-1]
            min = calc(ne, po, min)

        return min

    if nega_size <= K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min
        return min

    if posi_size <= K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-i-posi_size-1])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = abs(negatives[-K])
        for i in range(K+1):
            if i == 0:
                continue
            elif i > 0 and i < K:
                ne = abs(negatives[-K+i])
                po = positives[i-1]
                min = calc(ne, po, min)
            elif i == K:
                tmp = positives[K-1]
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[K+i-1]
            min = calc(ne, po, min)

        return min

    if nega_size < K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min
        return min

    if posi_size < K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-i-posi_size-1])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = abs(negatives[-K])
        for i in range(K+1):
            if i == 0:
                continue
            elif i > 0 and i < K:
                ne = abs(negatives[-K+i])
                po = positives[i-1]
                min = calc(ne, po, min)
            elif i == K:
                tmp = abs(positives[K-1])
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[N + i - K+1]
            min = calc(ne, po, min)

        return min

    if nega_size < K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min

        return min

    if posi_size < K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-i-posi_size-1])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = negatives[-K]
        for i in range(K+1):
            if i == 0:
                continue
            elif i > 0 and i < K:
                ne = abs(negatives[-K+i])
                po = positives[i-1]
                min = calc(ne, po, min)
            elif i == K:
                tmp = abs(positives[K-1])
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[N + i - K+1]
            min = calc(ne, po, min)

        return min

    if nega_size < K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min

        return min

    if posi_size < K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-i-posi_size-1])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


def calc(m, n, default=100000000):
    tmp = 0
    if m > n:
        tmp = m + 2*n
    else:
        tmp = 2*m + n

    min = tmp if default > tmp else default

    return min


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = positives[K-1]
        for i in range(K+1):
            if i == 0:
                continue
            elif i == K:
                tmp = abs(negatives[nega_size-K])
                min = tmp if min > tmp else min
            elif i > 0 and i < K+1:
                new_posi = positives[:K-i]
                new_nega = negatives[nega_size-i:]
                min = calc(new_posi[-1], abs(new_nega[0]), min)

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[N + i - K+1]
            min = calc(ne, po, min)

        return min

    if nega_size < K and K <= posi_size:
        min = 1e9
        for i in range(nega_size):
            po = x[K+i-1]
            ne = abs(x[i])
            min = calc(ne, po, min)

        min = abs(positives[K-1]) if abs(positives[K-1]) < min else min

        return min

    if posi_size < K and K <= nega_size:
        min = 1e9
        for i in range(posi_size):
            po = x[-i-1]
            ne = abs(x[-i-posi_size-1])
            min = calc(po, ne, min)

        min = abs(negatives[-K]) if abs(negatives[-K]) < min else min
        return min


print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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


def my_index(l, i, default=False):
    if i in l:
        return True
    else:
        return default


def main():
    global x, K
    if 0 in x:
        K -= 1
        if K == 0:
            return 0

    x = np.array(x)
    positives = x[np.where(x > 0)]
    negatives = x[np.where(x < 0)]

    nega_size = np.size(negatives)
    posi_size = np.size(positives)
    if nega_size == 0:
        return positives[K-1]

    if posi_size == 0:
        return abs(negatives[-K])

    if K <= nega_size and K <= posi_size:
        min = positives[K-1]
        for i in range(K+1):
            if i == 0:
                continue
            elif i == K:
                tmp = abs(negatives[nega_size-K])
                min = tmp if min > tmp else min
            elif i > 0 and i < K+1:
                new_posi = positives[:K-i]
                new_nega = negatives[nega_size-i:]
                tmp = 2 * new_posi[-1] + abs(new_nega[0])
                if tmp > new_posi[-1] + 2 * abs(new_nega[0]):
                    tmp = new_posi[-1] + 2 * abs(new_nega[0])
                min = tmp if min > tmp else min

        return min

    if K > nega_size and K > posi_size:
        min = 1e9
        for i in range(N-K+1):
            ne = abs(x[i])
            po = x[N + i - K+1]
            tmp = ne + 2 * po
            if tmp > 2 * ne + po:
                tmp = 2 * ne + po

            min = tmp if min > tmp else min

        return min

    if nega_size < K and K <= posi_size:
        min = positives[K-1]
        for i in range(1, nega_size):
            tmp = abs(negatives[-i]) + 2 * positives[K-i] if abs(negatives[-i]
                                                                 ) > positives[K-i] else 2 * abs(negatives[-i]) + positives[K-i]
            min = tmp if min > tmp else min

        return min

    if posi_size < K and K <= nega_size:
        min = negatives[-1]
        for i in range(1, posi_size):
            tmp = positives[-i-1] + 2 * abs(negatives[-i]) if abs(
                negatives[-i]) < positives[-i-1] else 2 * positives[-i-1] + abs(negatives[-i])
            min = tmp if min > tmp else min

        return min


print(main())

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

N, K = map(int, input().split())
x = list(map(int, input().split()))
ans, tmp = 10**9, 10**9

#print(bisect.bisect(x, 0))

for i in range(len(x) - K + 1):
  if x[i] <= 0 and x[i+K-1] >= 0:
    tmp = abs(x[i]) + x[i+K-1] + min(abs(x[i]), x[i+K-1])
  elif i == bisect.bisect(x, 0):
    tmp = x[i+K-1]
  elif i+K-1 == bisect.bisect(x, 0):
    tmp = abs[x[i]]
#  print("i:{}, i+K-1:{}, tmp:{}".format(i, i+K-1, tmp))
  ans = min(tmp, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
dis = sorted(x, key=lambda x: abs(x))
dis = dis[:k]
if all(d <= 0 for d in dis) or all(d >= 0 for d in dis):
    print(abs(dis[-1]))
else:
    if dis[-1] > 0:
        print(dis[-1] + 2 * abs(min(dis)))
    else:
        print(-dis[-1] + 2 * abs(max(dis)))

Submission status: Wrong Answer

Solution code:
import sys

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

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
all_p = 10 ** 10
all_m = 10 ** 10
if K <= lastx and K != 0:
    all_p = lst_p[K-1]
if K <= lasty and K != 0 :
    all_m = lst_m[lasty - K]

#print (all_p, all_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m), all_p, abs(all_m)))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] <= abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1

Submission status: Wrong Answer

Solution code:
import sys

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

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
all_p = 10 ** 10
all_m = 10 ** 10
if K <= lastx and K != 0:
    all_p = lst_p[K-1]
if K <= lasty and K != 0 :
    all_m = lst_m[lasty - K]

#print (all_p, all_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m), all_p, abs(all_m)))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] < abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1

Submission status: Wrong Answer

Solution code:
import sys

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

lst_p = []
lst_m = []
for i in range(N):
    if lst[i] == 0:
        K -= 1
    if lst[i] > 0:
        lst_p += [lst[i]]
    elif lst[i] < 0:
        lst_m += [lst[i]]


p = 0
m = 0
x, y = 0, len(lst_m)-1
lastx = len(lst_p)
lasty = len(lst_m)
all_p = 10 ** 10
all_m = 10 ** 10
if K <= lastx and K != 0:
    all_p = lst_p[K-1]
if K <= lasty and K != 0 :
    all_m = lst_m[lasty - K]

#print (all_p, all_m)
while True:
#    print (K)
    if K == 0: #K=残り回数が0のとき出力して終了
        print (min(2 * p + abs(m), p + 2 * abs(m), all_p, all_m))
        sys.exit()
    if x == lastx: #正側の数字を全て使い切った時
        m = lst_m[y]
        y -= 1
        K -= 1
    elif y == -1: #負側の数字を全て使い切った時
        p = lst_p[x]
        x += 1
        K -= 1
    elif lst_p[x] < abs(lst_m[y]): #負側のリスト絶対値のほうが大きい時-->正側の数字を採用
        p = lst_p[x]
        x += 1
        K -= 1
    else: #正側のリストのほうが大きい時-->負側の数字を採用
        m = lst_m[y]
        y -= 1
        K -= 1