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:

url = "https://atcoder.jp//contests/abc107/tasks/arc101_a"

def main():
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    # if xs[0] < 0 <= xs[-1]:
    #     pass
    # else:
    #     print(abs(xs[k*-1]))
    #     exit()

    ans = 100000000
    se = [0, k-1]
    while se[1] < n:
        if xs[se[0]] < 0 <= xs[se[1]]:
            tmp = min(abs(xs[se[0]]), xs[se[1]]) * 2 + max(abs(xs[se[0]]), xs[se[1]])
            ans = min(ans, tmp)
        elif xs[se[0]] < 0:
            ans = min(ans, abs(xs[se[0]]))
        elif xs[se[1]] > 0:
            ans = min(ans, xs[se[1]])
        se = [v + 1 for v in se]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

url = "https://atcoder.jp//contests/abc107/tasks/arc101_a"

def main():
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    if xs[0] < 0 <= xs[-1]:
        pass
    else:
        print(abs(xs[k*-1]))
        exit()

    ans = 100000000
    se = [0, k-1]
    while se[1] < n:
        if xs[se[0]] < 0 <= xs[se[1]]:
            tmp = min(abs(xs[se[0]]), xs[se[1]]) * 2 + max(abs(xs[se[0]]), xs[se[1]])
            ans = min(ans, tmp)
        elif xs[se[0]] < 0:
            ans = min(ans, abs(xs[se[0]]))
        elif xs[se[1]] > 0:
            ans = min(ans, xs[se[1]])
        se = [v + 1 for v in se]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [0]
C = [0]
for I in range(N):
  if A[I] > 0:
    for i in range(I-1, -1, -1):
      B.append(-A[i])
    C += A[I:]
    break
b = len(B)
c = len(C)
ans = 10 ** 10
for i in range(max(0, K-c+1), min(K, b-1)+1):
  ans = min(ans, B[i]+C[K-i]+min(B[i], C[K-i]))
print(ans)

Submission status: Wrong Answer

Solution code:
# n=int(input())
n, k = map(int, input().split())
xl = list(map(int, input().split()))
#l=[list(input()) for i in range(h)]

if 0 in xl:
    k -= 1
else:
    xl.append(0)
    xl.sort()
if k == 0:
    ans = 0
else:
    ans = 10**9
    zeropoint = xl.index(0)
    endpoint = len(xl)-1
    startpoint = max(0, zeropoint-(k-1))
    cur = startpoint
    while cur+k <= endpoint and cur <= zeropoint:
        # print(cur)
        # もし折り返すなら
        if cur < zeropoint < cur+k:
            tempdis = abs(xl[cur]-xl[cur+k])
            tempdis += min(abs(xl[cur]), xl[cur+k])
            ans = min(ans, tempdis)
        else:
            ans = min(ans, abs(xl[cur]-xl[cur+k]))
        cur += 1
print(ans)

Submission status: Wrong Answer

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

ans = 10**10
for i in range(n-k):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
if ans == 10**10:
  print(0)
else:
  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):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
if ans == 10**9:
  print(0)
else:
  print(ans)

Submission status: Wrong Answer

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

ans = 10**6
for i in range(n-k):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
if ans == 10**6:
  print(0)
else:
  print(ans)

Submission status: Wrong Answer

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

ans = 10**6
for i in range(n-k):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
pc = 0
nc = 0
pp = 0
np = 0
while pc + nc < K:
    if pc < lp and nc < ln:
        p = xp[pc]
        n = -xn[nc]
    elif pc >= lp:
        p = float("inf")
        n = -xn[nc]
    elif nc >= ln:
        p = xp[pc]
        n = float("inf")
    if p + np + min(p,np) < n + pp + min(n,pp):
        pp = p
        pc += 1
    elif p + np + min(p,np) > n + pp + min(n,pp):
        np = n
        nc += 1
    else:
        if pc+1 == lp:
            np = n
            nc += 1
        elif nc+1 == ln:
            pp = p
            pc += 1
        elif xp[pc+1]+np+min(xp[pc+1],np) < -xn[nc+1]+pp+min(-xn[nc+1],pp):
            pp = p
            pc += 1
        else:
            np = n
            nc += 1
print(pp+np+min(pp,np))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
pc = 0
nc = 0
pp = 0
np = 0
while pc + nc < K:
    if pc < lp and nc < ln:
        p = xp[pc]
        n = -xn[nc]
    elif pc >= lp:
        p = float("inf")
        n = -xn[nc]
    elif nc >= ln:
        p = xp[pc]
        n = float("inf")
    if p + np + min(p,np) < n + pp + min(n,pp):
        pp = p
        pc += 1
    elif p + np + min(p,np) > n + pp + min(n,pp):
        np = n
        nc += 1
    else:
        if pc+1 == lp:
            np = n
            nc += 1
        elif nc+1 == ln:
            pp = p
            pc += 1
        elif xp[pc+1] < -xn[nc+1]:
            pp = p
            pc += 1
        else:
            np = n
            nc += 1
print(pp+np+min(pp,np))

Submission status: Wrong Answer

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


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, K = MI()
    targets = LI()
    if N == 1:
        print(abs(targets[0]))
        return
    length = targets[K - 1] - targets[0]
    ans = length + min(abs(targets[K-1]), abs(targets[0]))
    for i in range(N-K):
        length = targets[i + K - 1] - targets[i]
        cost = length + min(abs(targets[i+K-1]), abs(targets[i]))
        ans = min(ans, cost)

    print(ans)


main()

Submission status: Wrong Answer

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


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, K = MI()
    targets = LI()
    if N == 1:
        print(abs(targets[0]))
        return
    ans = 10 ** 19
    for i in range(N-K):
        length = targets[i + K - 1] - targets[i]
        cost = length + min(abs(targets[i+K-1]), abs(targets[i]))
        ans = min(ans, cost)

    print(ans)


main()

Submission status: Wrong Answer

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


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, K = MI()
    targets = LI()
    if N == 1:
        print(targets[0])
        return
    ans = 10 ** 19
    for i in range(N-K):
        length = targets[i + K - 1] - targets[i]
        cost = length + min(abs(targets[i+K-1]), abs(targets[i]))
        ans = min(ans, cost)

    print(ans)


main()

Submission status: Wrong Answer

Solution code:
import sys
N , K = map(int,input().split())
x = list(map(int,input().split()))
m = 2* 10**8
for i in range(N - K + 1):
  r = N - i - 1
  l = N - i - K
  s1 = abs(x[r]) + abs(x[r] - x[l])
  s2 = abs(x[l]) + abs(x[r] - x[l])
  s = min([s1,s2])
  if s<m:
    m = s
print(m)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
N ,K = map(int,input().split())
A = list(map(int,input().split()))
zero = bisect_left(A, 0)
ans = float('inf')
for i in range(N-K+1):
  if i > zero:
    break
  elif i+K-1 < zero:
    continue
  s = A[i]
  g = A[i+K-1]
  if s >= 0 and g >= 0:
    s = 0
  elif s < 0 and g < 0:
    g = 0

  s = abs(s)
  g = abs(g)
  ans = min(min(s,g)*2+max(s,g), ans)
print(ans)

Submission status: Wrong Answer

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

# ==================================================-
# 二分探索
# functionを満たす,search_listの最大の要素を出力
# 【注意点】searchリストの初めの方はfunctionを満たし、後ろに行くにつれて満たさなくなるべき
import math
import sys
sys.setrecursionlimit(10**9)
def binary_research(start,end,function):
    if start==end:
        return start
    middle=math.ceil((start+end)/2)
    if function(middle):
        start=middle
    else:
        end=middle-1
    return binary_research(start, end, function)
positive=[]
negative=[]
zero=False


for i in x:
    if i<0:
        negative.append(abs(i))
    elif i>0:
        positive.append(i)
    else:
        zero=True
positive.sort()
negative.sort()


def function(time):
    if time==-1:
        return True
    ans_list=[]
    ans=0
    if zero:
        ans+=1
    for i in positive:
        if i<=time:
            ans_list.append(i)
            ans+=1
        else:
            break
    negative_list=[]
    for i in negative:
        if i<=time:
            negative_list.append(i)
        else:
            break
    negative_list=negative_list[::-1]
    newans=ans
    while negative_list:
        newans,ans_list,negative_list=function2(ans_list,negative_list,newans,time)
        ans=max(ans,newans)
        if ans_list==[]:
            break
        else:
            ans_list.pop()
            newans-=1
    if ans>=k:
        return False
    return True



def function2(ans_list,negative_list,ans,time):
    if ans_list==[]:
        max_num=0
    else:
        max_num=ans_list[-1]
    remain_time=time-max_num*2
    while negative_list:
        i=negative_list.pop()
        if max_num==0:
            if i<=remain_time:
                ans+=1
        elif max_num+i+min(i,max_num)<=time:
            ans+=1
        else:
            negative_list.append(i)
            break
    return ans,ans_list,negative_list


print(binary_research(-1,5*10,function)+1)

Submission status: Wrong Answer

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

import bisect
l=bisect.bisect_left(X,0)
L=max(0, l-K+1)
R=min(N-1, l+K-1)

ans=float('inf')
for i in range(L,R-K+2):
    t=0
    s=X[i:i+K]
    if s[0]<0:
        t+=abs(s[0])
    if 0<X[-1]:
        t+=t+s[-1]
    # print(s,t)
    ans=min(ans,t)
print(ans)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    if X[i] >= 0:
        num = X[K+i-1]
    else:
        num = X[K+i-1] + abs(X[i]*2)
    if num < ans:
        ans = num
print(ans)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    num = X[K+i-1] + abs(X[i]*2)
    if num < ans:
        ans = num
print(ans)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    num = X[K+i-1] - X[i] + abs(X[i])
    if num < ans:
        ans = num
print(ans)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    num = X[K+i-1] - X[i] + abs(X[i])
    if num < ans:
        ans = num
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from bisect import *

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

if all(list(map(lambda i: i>0, x))):
    print(x[k-1])
    exit()
if all(list(map(lambda i: i<0, x))):
    print(x[n-k])
    exit()

# 0をまたぐ場合
zr = bisect_left(x, 0)
zl = zr-1

i = 1 
anss = []
for i in range(1, k+2):
    tmp = 0 
    if zr-k+i >= 0 and zr+i-1 < n:
        tmp = abs(x[zr+i-1] - x[zr-k+i])
#        print('x[{}] = {}'.format(zr-k+i, x[zr-k+i]))
#        print('x[{}] = {}'.format(zr+i-1, x[zr+i-1]))
        if i <= k//2:
            tmp += abs(x[zr+i-1])
        else:
            tmp += abs(x[zr-k+i])
        anss.append(tmp)

ans = min(anss)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    S = []
    ans = 10 ** 9 + 7

    start = bisect.bisect(X, 0)

    for i in range(start+1):
        temp = 0
        if N - start < K-i:  #右にある個数が取るべき個数を超えたときはスキップ
            continue

        if i ==0:
            ans = min(X[start+K-1],ans)
            continue
        # 左から
        temp += -X[start - i] * 2
        temp += X[start + K - i-1]
        ans = min(temp, ans)

        # 右から

        temp = X[start + K - i-1] * 2
        temp += -X[start - i]
        ans = min(temp, ans)



        if i ==K:
            ans =min(-X[start-K],ans)

    print(ans)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    xs = [int(item) for item in input().split()]
    
    min_lr = 10**9
    min_rl = 10**9

    if N != K:
        for i in range(N-K):
            min_lr = min(min_lr, abs(xs[i]) + abs(xs[i+K-1] - xs[i]))
            min_rl = min(min_rl, abs(xs[i+K-1]) + abs(xs[i+K-1] - xs[i]))

        print(min(min_lr, min_rl))
    elif N ==1:
        print(abs(xs[0]))
    else:
        print(min(abs(xs[0]) + abs(xs[N-1] - xs[0]), abs(xs[N-1]) + abs(xs[N-1] - xs[0])))

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    xs = [int(item) for item in input().split()]
    
    min_lr = 10**9
    min_rl = 10**9

    if N != K:
        for i in range(N-K):
            min_lr = min(min_lr, abs(xs[i]) + abs(xs[i+K-1] - xs[i]))
            min_rl = min(min_rl, abs(xs[i+K-1]) + abs(xs[i+K-1] - xs[i]))

        print(min(min_lr, min_rl))
    elif N ==1:
        print(xs[0])
    else:
        print(min(abs(xs[0]) + abs(xs[N-1] - xs[0]), abs(xs[N-1]) + abs(xs[N-1] - xs[0])))

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    N, K = [int(item) for item in input().split()]
    xs = [int(item) for item in input().split()]
    
    min_lr = 10**9
    min_rl = 10**9

    if N != K:
        for i in range(N-K):
            min_lr = min(min_lr, abs(xs[i]) + abs(xs[i+K-1] - xs[i]))
            min_rl = min(min_rl, abs(xs[i+K-1]) + abs(xs[i+K-1] - xs[i]))

        print(min(min_lr, min_rl))
    else:
        print(min(abs(xs[0]) + abs(xs[N-1] - xs[0]), abs(xs[N-1]) + abs(xs[N-1] - xs[0])))

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
import sys
if min(a)>=0:
  print(a[k-1])
  sys.exit()
if max(a)<=0:
  print((-1)*a[n-k])
  sys.exit()
  
ans=0
b=[]
for i in range(n-k+1):
  b.append(a[i+k-1]-a[i])

ans=float("INF")
for i in range(n-k+1):
  #i-k+1の正負、i+k-1の正負により変わる
  if i-k+1>=0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+min(b[i],b[i-k+1]))
  elif i-k+1<0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+b[i])
  elif i-k+1>=0 and i+k-1>n-1:
    ans=min(ans,abs(a[i])+b[i-k+1])
  else:
    ans=ans
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
import sys
if min(a)>=0:
  print(a[k-1])
  sys.exit()
if max(a)<=0:
  print((-1)*a[n-k])
  sys.exit()
  
ans=0
b=[]
for i in range(n-k+1):
  b.append(a[i+k-1]-a[i])

ans=float("INF")
for i in range(n-k+1):
  #i-k+1の正負、i+k-1の正負により変わる
  if i-k+1>=0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+min(b[i],b[i-k+1]))
  elif i-k+1<0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+b[i])
  elif i-k+1>=0 and i+k-1>n-1:
    ans=min(ans,abs(a[i])+b[i-k+1])
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
import sys
if min(a)>=0:
  print(a[k-1])
  sys.exit()
if max(a)<=0:
  print((-1)*a[n-k])
  sys.exit()
  
ans=0
b=[]
for i in range(n-k+1):
  b.append(a[i+k-1]-a[i])

ans=float("INF")
for i in range(n-k+1):
  #i-k+1の正負、i+k-1の正負により変わる
  if i-k+1>=0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+min(b[i],b[i-k+1]))
  elif i-k+1<0:
    ans=min(ans,abs(a[i])+b[i])
  elif i+k-1>n-1:
    ans=min(ans,abs(a[i])+b[i-k+1])
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
import sys
if min(a)>=0:
  print(a[k-1])
  sys.exit()
if max(a)<0:
  print(-a[n-k])
  sys.exit()
  
ans=0
b=[]
for i in range(n-k+1):
  b.append(a[i+k-1]-a[i])

ans=float("INF")
for i in range(n-k+1):
  #i-k+1の正負、i+k-1の正負により変わる
  if i-k+1>=0 and i+k-1<=n-1:
    ans=min(ans,abs(a[i])+min(b[i],b[i-k+1]))
  elif i-k+1<0:
    ans=min(ans,abs(a[i])+b[i])
  elif i+k-1>n-1:
    ans=min(ans,abs(a[i])+b[i-k+1])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k= list(map(int,input().split()))
pos= list(map(int,input().split()))
pos_abs =list(map(abs, pos))
pos_abs.sort()
#print(pos_abs)
ls = pos_abs[:k]
high = 0
low = 0
count =0
for a in ls[::-1]:
    if a in pos :
        high = a
        break
for a in ls[::-1]:
    if a not in pos or ls.count(a) == 2:
        low = a
        break


#print(high,low)
if high >=low:
    print(high+ low*2)
else:
    print(high*2+low)

Submission status: Wrong Answer

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

res= 10**6

for i in range(n-k+1):
    st = al[i]
    en = al[i+k-1]
    if st *en <0:
        temp = min(abs(st),abs(en))*2+max(abs(st),abs(en))
    else:
        temp = max(abs(st),abs(en))
    res = min(temp,res)

print(res)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)

ans=10**10
if k<=nb:
    ans=b[k-1]
for i in range(1,na+1):
    if 0<=k-i-1<=nb-1:
        ans=min(ans,min(-2*a[-i]+b[k-i-1],-a[i-i]+2*b[k-i-1]))
    elif k-i<0:
        ans=min(ans,-a[-k])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
a=[x[i] for i in range(n) if x[i]<0]
b=[x[i] for i in range(n) if x[i]>=0]
na=len(a)
nb=len(b)

ans=10**10
if k<=nb:
    ans=b[k-1]
for i in range(1,na+1):
    if 0<=k-i-1<=nb-1:
        ans=min(ans,-2*a[-i]+b[k-i-1])
    elif k-i<0:
        ans=min(ans,-a[-i])
print(ans)

Submission status: Wrong Answer

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

x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')

if count1 == 0:
    print(x1[k-1])
elif count0 == 0:
    print(x2[k-1])
else:
    if 0 in x1:
        x1.remove(0)
        k -= 1
        count0 -= 1
    for i in range(min(count0, k)):
        if count1 >= k-i-1:
            ans = min(ans, x1[i]*2-x2[k-i-2])
    for i in range(min(count1, k)):
        if count0 >= k-i-1:
            ans = min(ans, -x2[i]*2+x1[k-i-2])
    print(ans)

Submission status: Wrong Answer

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

x1 = [i for i in x0 if i >= 0]
x2 = list(reversed([i for i in x0 if i < 0]))
count0 = len(x1)
count1 = len(x2)
ans = float('inf')

if count1 == 0:
    print(x1[k-1])
elif count0 == 0:
    print(x2[k-1])
else:
    for i in range(min(count0, k)):
        if count1 >= k-i-1:
            ans = min(ans, x1[i]*2-x2[k-i-2])
    for i in range(min(count1, k)):
        if count0 >= k-i-1:
            ans = min(ans, -x2[i]*2+x1[k-i-2])
    print(ans)

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int, input().split())
x = list(map(int, input().split()))
for i in range(N):
  if x[i] == 0:
    p, m = i, i+1
    break
  elif x[i] > 0:
    p, m = i, i
    break
    
if (x[0]<0) and (x[-1]>0):
  pls = x[p:]
  mns = x[:m]
  ans = math.inf

  for i in range(len(pls)):
    if i < K-1:
      if pls[0] == 0:
        n = K - i
      else:
        n = K - i - 1
      if len(mns) >= n:
        tmp = 2*abs(mns[-n]) + pls[i]
        ans = min(ans, tmp)
    if i == K-1:
      ans = min(ans, pls[i])
      break
    
  for i in range(len(mns)):
    m = len(mns)-i
    if m < K:
      if pls[0] == 0:
        n = K - m
      else:
        n = K - m - 1
      if len(pls) > n:
        tmp = 2*pls[n] + abs(mns[-m])
        print(i, tmp)
        ans = min(ans, tmp)
    if m == K:
      ans = min(ans, abs(mns[i]))
    
elif x[0] >= 0:
  ans = x[K-1]
  
else:
  ans = abs(x[-K])
  
print(ans)

Submission status: Wrong Answer

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

for i in range(n):
    if x[i] > 0:
        neg_num = i
        break
pos_num = n - neg_num

l = min(k, neg_num)
res = 10 ** 13
for i in range(l, -1, -1):
    r = k - i
    if r <= pos_num:
        l_val = min(0, x[neg_num - i])
        r_val = x[neg_num + k - i - 1]
        tmp = min( -l_val * 2 + r_val, -l_val + r_val * 2)
        res = min(tmp, res)
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<=0:
            if right <= 0:
                best_time = min(best_time, abs(left))
            else:
                best_time = abs(left) + right + min(abs(left), right)
        else:
            best_time = min(best_time, right)
            break

    print(best_time)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<=0:
            if right <= 0:
                best_time = min(best_time, abs(left))
            else:
                best_time = -left + right + min(abs(left), right)
        else:
            best_time = min(best_time, right)
            break

    print(best_time)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<=0:
            if right <= 0:
                best_time = min(best_time, -left)
            else:
                best_time = -left + right +min(-left, right)
        else:
            best_time = min(best_time, right)
            break

    print(best_time)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<=0:
            best_time = -left + right +min(-left, right)
        else:
            best_time = min(best_time, right)
            break

    print(best_time)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<0:
            best_time = -left + right +min(-left, right)
        else:
            break

    print(best_time)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal

INF = 1 << 50


def run():
    N, K = map(int, sysread().split())
    X = list(map(int, sysread().split()))

    best_time = INF
    for i in range(0, N-K+1):
        left = X[i]
        right =  X[i+K-1]
        if left<0:
            best_time = -left + right +min(-left, right)
        else:
            best_time = min(best_time, right)

    print(best_time)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
from collections import deque
    
N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10*9
    
l=inf
dp=deque(x[N-K:N])
i=0
while N-K-i>=0:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        dp[K-1]=abs(dp[K-1])
        dp[0]=abs(dp[0])
        if dp[K-1]>=dp[0]:
            tmp=dp[0]*2+dp[K-1]
        else:
            tmp=dp[K-1]*2+dp[0]
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)

Submission status: Wrong Answer

Solution code:
from collections import deque
    
N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10*9
    
l=inf
dp=deque(x[N-K:N])
i=0
while N-K-i>=0:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if dp[K-1]>=abs(dp[0]):
            tmp=-dp[0]*2+dp[K-1]
        else:
            tmp=dp[K-1]*2-dp[0]
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)

Submission status: Wrong Answer

Solution code:
from collections import deque
    
N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10*9
    
l=inf
dp=deque(x[N-K:N])
i=0
while N-K-i>=0:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if dp[K-1]>=dp[0]:
            tmp=-dp[0]*2+dp[K-1]
        else:
            tmp=dp[K-1]*2-dp[0]
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)

Submission status: Wrong Answer

Solution code:
from collections import deque
    
N,K=map(int,input().strip().split())
x=list(map(int,input().strip().split()))
inf=10*8
    
l=inf
dp=deque(x[N-K:N])
i=0
while N-K-i>=0:
    if dp[0]>=0:
        tmp=dp[K-1]
    elif dp[K-1]<=0:
        tmp=-dp[0]
    else:
        if dp[K-1]>=dp[0]:
            tmp=-dp[0]*2+dp[K-1]
        else:
            tmp=dp[K-1]*2-dp[0]
    l=min(l,tmp)
    i+=1
    if N-K-i<0:
        break
    else:
        dp.appendleft(x[N-K-i])
        dp.pop()
print(l)