Language: C++



#coding:utf-8
import numpy as np
n,k = map(int,input().split())
ls = list(map(int,input().split()))

# n, k = 5, 3
# ls = [-30,-10,10,20,50]
#
# n,k = 3,2
# ls=[10,20,30]

# n,k = 1,1
# ls = [0]

# n,k=8,5
# ls=[-9,-7,-4,-3,1,2,3,4]
if not (0 in ls):
    ls.append(0)
    k+=1
#print (ls)

ls = np.array(ls)
ls.sort()
#print (ls)

idx = np.where(ls==0)[0][0]
#print (idx,k)
#print (idx+k)
start = max(0,idx-k)
goal = min(idx+k,len(ls)-1)

#print (start,goal)
answer = []
for i in range(goal-(k-1)+1):
    #print (i,i+k-1)
    mi = min(abs(ls[i]),abs(ls[i+k-1]))
    ma = max(abs(ls[i]),abs(ls[i+k-1]))
    #print (mi,ma)
    answer.append(2*mi+ma)
print (min(answer))
# def set_max(ls):
#     if len(ls)>0:
#         return set([max(ls)])
#     else:
#         return set()
# def set_min(ls):
#     if len(ls)>0:
#         return set([min(ls)])
#     else:
#         return set()
#
#
# plus = list(filter(lambda n:n>=0, ls))
# minus = list(filter(lambda n:n<=0, ls))
#
# neighbor = set_max(minus) | set_min(plus)
#
# #print (neighbor)
# answer = []
# cnt = 0
# for i in range(n-k+1):
#     if len(set(ls[i:i+k])&neighbor)>0:
#
#         #print (ls[i:i+k])
#         #print (np.sign(ls[i]*ls[i+k-1]))
#         temp = [abs(ls[i]),abs(ls[i+k-1])]
#         if np.sign(ls[i]*ls[i+k-1])<0:
#
#             answer.append(2*min(temp)+max(temp))
#         else:
#             answer.append(max(temp))
#         cnt += 1
#         if cnt == k + 1:
#             break
# print (min(answer))

# first = -1
# for i,point in enumerate(ls):
#     if point >= 0:
#         first = i
#         break
# size=k+1
# ans = []
# for i in range(size):
#     if first-size+i+1>=0 and first+i-1< len(ls):
#         temp = [0,ls[first-size+i+1],ls[first+i-1]]
#         temp = [abs(min(temp)),abs(max(temp))]
#         ans.append((2*min(temp)+max(temp)))
# print (min(ans))


Compiler output:
a.cc:3:2: error: invalid preprocessing directive #coding
    3 | #coding:utf-8
      |  ^~~~~~
a.cc:8:3: error: invalid preprocessing directive #n
    8 | # n, k = 5, 3
      |   ^
a.cc:9:3: error: invalid preprocessing directive #ls
    9 | # ls = [-30,-10,10,20,50]
      |   ^~
a.cc:11:3: error: invalid preprocessing directive #n
   11 | # n,k = 3,2
      |   ^
a.cc:12:3: error: invalid preprocessing directive #ls
   12 | # ls=[10,20,30]
      |   ^~
a.cc:14:3: error: invalid preprocessing directive #n
   14 | # n,k = 1,1
      |   ^
a.cc:15:3: error: invalid preprocessing directive #ls
   15 | # ls = [0]
      |   ^~
a.cc:17:3: error: invalid preprocessing directive #n
   17 | # n,k=8,5
      |   ^
a.cc:18:3: error: invalid preprocessing directive #ls
   18 | # ls=[-9,-7,-4,-3,1,2,3,4]
      |   ^~
a.cc:22:2: error: invalid preprocessing directive #print
   22 | #print (ls)
      |  ^~~~~
a.cc:26:2: error: invalid preprocessing directive #print
   26 | #print (ls)
      |  ^~~~~
a.cc:29:2: error: invalid preprocessing directive #print
   29 | #print (idx,k)
      |  ^~~~~
a.cc:30:2: error: invalid preprocessing directive #print
   30 | #print (idx+k)
      |  ^~~~~
a.cc:34:2: error: invalid preprocessing directive #print
   34 | #print (start,goal)
      |  ^~~~~
a.cc:37:6: error: invalid preprocessing directive #print
   37 |     #print (i,i+k-1)
      |      ^~~~~
a.cc:40:6: error: invalid preprocessing directive #print
   40 |     #print (mi,ma)
      |      ^~~~~
a.cc:43:3: error: invalid preprocessing directive #def; did you mean #ifdef?
   43 | # def set_max(ls):
      |   ^~~
      |   ifdef
a.cc:44:13: error: missing binary operator before token "("
   44 | #     if len(ls)>0:
      |             ^
a.cc:46:11: warning: extra tokens at end of #else directive [-Wendif-labels]
   46 | #     else:
      |           ^
a.cc:47:11: error: invalid preprocessing directive #return
   47 | #         return set()
      |           ^~~~~~
a.cc:48:3: error: invalid preprocessing directive #def; did you mean #ifdef?
   48 | # def set_min(ls):
      |   ^~~
      |   ifdef
a.cc:49:13: error: missing binary operator before token "("
   49 | #     if len(ls)>0:
      |             ^
a.cc:51:11: warning: extra tokens at end of #else directive [-Wendif-labels]
   51 | #     else:
      |           ^
a.cc:52:11: error: invalid preprocessing directive #return
   52 | #         return set()
      |           ^~~~~~
a.cc:55:3: error: invalid preprocessing directive #plus
   55 | # plus = list(filter(lambda n:n>=0, ls))
      |   ^~~~
a.cc:56:3: error: invalid preprocessing directive #minus
   56 | # minus = list(filter(lambda n:n<=0, ls))
      |   ^~~~~
a.cc:58:3: error: invalid preprocessing directive #neighbor
   58 | # neighbor = set_max(minus) | set_min(plus)
      |   ^~~~~~~~
a.cc:60:3: error: invalid preprocessing directive ##
   60 | # #print (neighbor)
      |   ^
a.cc:61:3: error: invalid preprocessing directive #answer
   61 | # answer = []
      |   ^~~~~~
a.cc:62:3: error: invalid preprocessing directive #cnt
   62 | # cnt = 0
      |   ^~~
a.cc:63:3: error: invalid preprocessing directive #for
   63 | # for i in range(n-k+1):
      |   ^~~
a.cc:64:13: error: missing binary operator before token "("
   64 | #     if len(set(ls[i:i+k])&neighbor)>0:
      |             ^
a.cc:87: error: unterminated #if
   87 | #     if first-size+i+1>=0 and first+i-1< len(ls):
a.cc:81: error: unterminated #if
   81 | #     if point >= 0:
a.cc:75: error: unterminated #if
   75 | #         if cnt == k + 1:
a.cc:69: error: unterminated #else
   69 | #         if np.sign(ls[i]*ls[i+k-1])<0:
a.cc:64: error: unterminated #if
   64 | #     if len(set(ls[i:i+k])&neighbor)>0:
a.cc:49: error: unterminated #else
   49 | #     if len(ls)>0:
a.cc:44: error: unterminated #else
   44 | #     if len(ls)>0:
a.cc:4:1: error: 'import' does not name a type
    4 | import numpy as np
      | ^~~~~~
a.cc:4:1: note: C++20 'import' only available with '-fmodules-ts'