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())
a = list(map(int, input().split()))
b = sorted(a, key=lambda x: abs(x))[:k]
print(abs(max(b)-min(b))+min(abs(min(b)),abs(max(b))))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles
"""
need to consider the case that ticket is not enough to lower everything
"""
N = gi()
K = gi()
xs = [0] * N
for i in range(N):
    xs[i] = gi()

ans = abs(2 * xs[0]) + xs[-1]
if xs[0] >= 0:
    ans = min(ans, xs[K - 1])
elif xs[-1] <= 0:
    ans = min(ans, xs[-K + 1])

for i in range(N):
    if (i + K - 1 >= N):
        break
    r = xs[i + K - 1]
    l = xs[i]

    if (l <= 0 and r >= 0):
        ans = min(ans, -l + r + min(-l, r))

print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(N+1-K):
    print(arr[i],arr[i+K-1])
    if arr[i]<= 0 and arr[i+K-1]>0:
        preans = min(abs(arr[i]*2) + arr[i+K-1], abs(arr[i]) + arr[i+K-1]*2)
    elif arr[i]<=0 and arr[i+K-1]<=0:
        preans = abs(arr[i])
    elif arr[i]>=0 and arr[i+K-1]>=0:
        preans = arr[i+K-1]
    ans = min(ans,preans)

print(ans)

Submission status: Wrong Answer

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

ans = 2*10**8
for i in range(N-K+1):
  l = A[i]
  r = A[i+K-1]
  a = abs(l) + r-l
  b = abs(r) + r-l
  ans = min(ans, min(a,b))
print(ans)

Submission status: Wrong Answer

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

ans = 2*10**8
for i in range(N-K+1):
  l = A[i]
  r = A[i+K-1]
  a = abs(l) + r-l
  b = abs(l) + r-l
  ans = min(ans, min(a,b))
print(ans)

Submission status: Wrong Answer

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

DIS = []

for i in range(n-k+1):
    A = X[i:i+k]
    B = []
    C = []
    for j in range(k):
        if A[j]>0:
            break
    B = A[:j]
    C = A[j:]
    if len(B) != 0:
        max_b = max(B)
    else:
        max_b = 0
    if len(C) != 0:
        max_c = max(C)
    else:
        max_c = 0
    distance = max_b + max_c + min(max_b, max_c)
    DIS.append(distance)
print(min(DIS))

Submission status: Wrong Answer

Solution code:
N, K = (int(i) for i in input().split())  
X = [int(i) for i in input().split()] 
ans = 10 ** 18
for i in range(N):
  if i + K > N:
    break
  l = X[i]
  r = X[i + K - 1]
  
  if (l < 0 and r < 0) or (l > 0 and r > 0):
    continue
  
  if r < -l:
  	ans = min(ans, r * 2 - l)
  else:
    ans = min(ans, -l * 2 + r)
    
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()] 

def get_max_pos_and_neg(sort_set):
  max_pos = 0
  max_neg = 0
  for val, neg_flg in sort_set[:K]:
    if neg_flg == 0:
      max_pos = max(max_pos, val)
    else:
      max_neg = max(max_neg, val)
  return max_pos + max_neg

neg_X = sorted(list(map(lambda x: abs(x), filter(lambda x: x <= 0, X))))
pos_X = sorted(list(filter(lambda x: x >= 0, X)))

pos_list = []
for x in pos_X:
  pos_list.append((x, 0))
  
for x in neg_X:
  if x == 0:
    continue
  pos_list.append((2 * x, 1))
  
pos_sort_set = sorted(pos_list, key = lambda x: (x[0], x[1]))

neg_list = []
for x in pos_X:
  neg_list.append((2 * x, 1))
  
for x in neg_X:
  if x == 0:
    continue
  neg_list.append((x, 0))
  
neg_sort_set = sorted(neg_list, key = lambda x: (x[0], x[1]))
ans = min(get_max_pos_and_neg(pos_sort_set), get_max_pos_and_neg(neg_sort_set))
if len(pos_X) >= K:
  ans = min(ans, max(pos_X[:K])) 
if len(neg_X) >= K:
  ans = min(ans, max(neg_X[:K]))
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()] 

def get_max_pos_and_neg(sort_set):
  max_pos = 0
  max_neg = 0
  for val, neg_flg in sort_set[:K]:
    if neg_flg == 0:
      max_pos = max(max_pos, val)
    else:
      max_neg = max(max_neg, val)
  return max_pos + max_neg

neg_X = sorted(list(map(lambda x: abs(x), filter(lambda x: x < 0, X))))
pos_X = sorted(list(filter(lambda x: x >= 0, X)))

pos_list = []
for x in pos_X:
  pos_list.append((x, 0))
  
for x in neg_X:
  pos_list.append((2 * x, 1))
  
pos_sort_set = sorted(pos_list, key = lambda x: (x[0], x[1]))

neg_list = []
for x in pos_X:
  neg_list.append((2 * x, 1))
  
for x in neg_X:
  neg_list.append((x, 0))
  
neg_sort_set = sorted(neg_list, key = lambda x: (x[0], x[1]))

print(min(get_max_pos_and_neg(pos_sort_set), get_max_pos_and_neg(neg_sort_set)))

Submission status: Wrong Answer

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

def get_max_pos_and_neg(sort_set):
  max_pos = 0
  max_neg = 0
  for val, neg_flg in sort_set[:K]:
    if neg_flg == 0:
      max_pos = max(max_pos, val)
    else:
      max_neg = max(max_neg, val)
  return max_pos + max_neg

neg_X = sorted(list(map(lambda x: abs(x), filter(lambda x: x < 0, X))))
pos_X = sorted(list(filter(lambda x: x > 0, X)))

pos_list = []
for x in pos_X:
  pos_list.append((x, 0))
  
for x in neg_X:
  pos_list.append((2 * x, 1))
  
pos_sort_set = sorted(pos_list, key = lambda x: (x[0], x[1]))

neg_list = []
for x in pos_X:
  neg_list.append((2 * x, 1))
  
for x in neg_X:
  neg_list.append((x, 0))
  
neg_sort_set = sorted(neg_list, key = lambda x: (x[0], x[1]))

print(min(get_max_pos_and_neg(pos_sort_set), get_max_pos_and_neg(neg_sort_set)))

Submission status: Wrong Answer

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

ans = float('inf')

for i in range(0,N-K+1):
    kouho = 0
    kouho += abs(A[i])
    for j in range(i,i+K-1):
        kouho += abs(A[j]-A[j+1])
    ans = min(ans, kouho)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy
input = sys.stdin.readline
N,K = [int(i) for i in input().split()]
li = [int(i) for i in input().split()]
mina = 100000001
for i in range(N-K+1) :
    ll = abs(li[i])
    lr = abs(li[i+K-1])
    if li[i] * li[i+K-1] > 0 :
        l = max(ll,lr)
    else :
        if ll >= lr :
            l = lr * 2 + ll
        else :
            l = ll * 2 + lr
    mina = min(mina,l)
print(mina)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, K = map(int, input().split())
x = np.asarray(list(map(int,input().split())))
ans = 10**9
if K == 1:
    ans = min(np.abs(x))
for i in range(N-K):
    ans = min(ans, abs(x[i])+abs(x[i+K-1]-x[i]), abs(x[i+K-1])+abs(x[i]-x[i+K-1]))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if N == K:
    ans = sum(map(abs,x))

else:
    ans = 10**9
    for i in range(N-K+1):
        start,end = x[i],x[i+K-1]
        if end <= 0:
            ans = min(ans, abs(start)) 
        elif start <= 0:
            ans = min(ans, 2*min(abs(start), end)+max(abs(start), end))
        else:
            ans = min(ans, end)

print(ans)

Submission status: Wrong Answer

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

if N == K:
    ans = sum(map(abs,x))

else:
    ans = 10**9
    for i in range(N-K):
        start,end = x[i],x[i+K-1]
        if end <= 0:
            ans = min(ans, abs(start)) 
        elif start <= 0:
            ans = min(ans, 2*min(abs(start), end)+max(abs(start), end))
        else:
            ans = min(ans, end)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
lis=[]
for i in range(n-k+1):
  if x[i]<=0 and x[i+k-1]<=0:
    lis.append(abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
  else:
    lis.append(abs(x[i])+abs(x[i+k-1]-x[i]))
print(min(lis))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(l, r):
    # 左だけ行く
    if r <= 0:
        return -l
    # 右だけ行く
    elif l >= 0:
        return r
    # 両方行く
    else:
        # 短い方を先に行って往復
        return min(-l, r) * 2 + max(-l, r)

N, K = MAP()
A = LIST()

ans = check(A[0], A[-1])
for i in range(K):
    if i+K > N:
        break
    l = A[i]
    r = A[i+K-1]
    ans = min(ans, check(l, r))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()

ans = A[-1] - A[0]
for i in range(K):
    if i+K > N:
        break
    l = A[i]
    r = A[i+K-1]
    # 左だけ行く
    if r <= 0:
        ans = min(ans, -l)
    # 右だけ行く
    elif l >= 0:
        ans = min(ans, r)
    # 両方行く
    else:
        # 短い方を先に行って往復
        ans = min(ans, min(-l, r) * 2 + max(-l, r))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()

ans = INF
for i in range(K):
    if i+K > N:
        break
    l = A[i]
    r = A[i+K-1]
    # 左だけ行く
    if r <= 0:
        ans = min(ans, -l)
    # 右だけ行く
    elif l >= 0:
        ans = min(ans, r)
    # 両方行く
    else:
        # 短い方を先に行って往復
        ans = min(ans, min(-l, r) * 2 + max(-l, r))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
x = x[np.nonzero(x)]
if K == 0:
    print(0)
    sys.exit()
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(abs(xn[K-1]))
if len(xp) >= K:
    ans.append(abs(xp[K-1]))

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K+1]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-2] + abs(xn[-1]) + min(xp[-2],abs(xn[-1])))
    ans.append(xp[-1] + abs(xn[-2]) + min(xp[-1],abs(xn[-2])))
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
x = x[np.nonzero(x)]
if K == 0:
    print(0)
    sys.exit()
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(abs(xn[K-1]))
if len(xp) >= K:
    ans.append(abs(xp[K-1]))

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-1] + abs(xn[-1]) + min(xp[-1],abs(xn[-1])))
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
if K == 0:
    print(0)
    sys.exit()
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(abs(xn[K-1]))
if len(xp) >= K:
    ans.append(abs(xp[K-1]))

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K+1]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-1] + abs(xn[-1]) + min(xp[-1],abs(xn[-1])))
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
if K == 0:
    print(0)
    sys.exit()
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(abs(xn[K-1]))
if len(xp) >= K:
    ans.append(abs(xp[K-1]))

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-1] + abs(xn[-1]) + min(xp[-1],abs(xn[-1])))
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
N, K = [int(x) for x in input().split()]
x = np.array([int(x) for x in input().split()])
if 0 in x:
    K -= 1
if K == 0:
    print(0)
    sys.exit()
xn = x[(x<0)]
xn = xn[::-1]
xp = x[(x>0)]
ans = []
if len(xn) >= K:
    ans.append(xn[K-1])
if len(xp) >= K:
    ans.append(abs(xp[K-1]))

x = x.tolist()
x = sorted(x,key=lambda x:abs(x))[:K]
x = np.array(x)
xn = x[(x<0)]
xp = x[(x>0)]
if len(xp) > 0 and len(xn) > 0:
    ans.append(xp[-1] + abs(xn[-1]) + min(xp[-1],abs(xn[-1])))
print(min(ans))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
if N==1:
  print(0);exit()

ans=float("Inf")
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:
n, k = map(int, input().split())
x = list(map(int, input().split()))

# c = combinations(x, k)
c = []
for i in range(n - k):
    l = x[i]
    r = x[i + k - 1]
    c.append([l, r])


# print(list(c))

ans = 10 ** 10

for l, r in c:
    tmp = min(abs(l) + abs(r - l), abs(r) + abs(r - l))
    ans = min(ans, tmp)

# for g in c:
#     a = max(0, max(g))
#     b = min(0, min(g))
#     # print(a, b)
#     if (a * b) < 0:
#         tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
#     else:
#         tmp = abs(a) + abs(b)
#     ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

# c = combinations(x, k)
c = []
for i in range(n - k):
    l = x[i]
    r = x[i + k - 1]
    c.append([l, r])


# print(list(c))

ans = 10 ** 10

for g in c:
    a = max(0, max(g))
    b = min(0, min(g))
    # print(a, b)
    if (a * b) < 0:
        tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
    else:
        tmp = abs(a) + abs(b)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

# c = combinations(x, k)
c = []
for i in range(n - k):
    c.append(x[i : i + k])


# print(list(c))

ans = 10 ** 10

for g in c:
    a = max(0, max(g))
    b = min(0, min(g))
    # print(a, b)
    if (a * b) < 0:
        tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
    else:
        tmp = abs(a) + abs(b)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

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

# c = combinations(x, k)
c = []
for i in range(n - k):
    c.append(x[i : i + k])


# print(list(c))

ans = 10 ** 10

for g in c:
    a = max(0, max(g))
    b = min(0, min(g))
    # print(a, b)
    if (a * b) < 0:
        tmp = min(2 * abs(a) + abs(b), 2 * abs(b) + abs(a))
    else:
        tmp = abs(a) + abs(b)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

from bisect import bisect_right
position_zero = bisect_right(x,0)
#0の左のk本に火を付ける、
#0の左のk-1本に火を付ける、右の1本に火を付ける
#....で場合分けし、左から行く場合と右から行く場合で最小のものをそれぞれの場合で計算
#ただし、0の左のろうそくがk本未満の場合は、左のposition_zero本に火を付ける、右のk-position_zero本に火を付ける場合からスタート

if k==1:
    if position_zero==n:
        ret = x[position_zero-1]
    else:
        ret = min(x[position_zero],x[position_zero-1])
else:
    ret =10**8*2
    l_k = min(k,position_zero)
    while l_k >= 0 and position_zero - l_k + k - 1 < n:
        l = x[position_zero - l_k]
        r = x[position_zero - l_k + k - 1]
        if l <= 0 and r <= 0:
            l *= -1
            r = 0
        elif l<=0 and r > 0:
            l *= -1
        elif l > 0 and r > 0:
            l = 0
        ret = min(ret,min(l*2+r,l+r*2))
        l_k-=1
print(ret)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

from bisect import bisect_right
position_zero = bisect_right(x,0)
#0の左のk本に火を付ける、
#0の左のk-1本に火を付ける、右の1本に火を付ける
#....で場合分けし、左から行く場合と右から行く場合で最小のものをそれぞれの場合で計算
#ただし、0の左のろうそくがk本未満の場合は、左のposition_zero本に火を付ける、右のk-position_zero本に火を付ける場合からスタート

if k==1:
    if position_zero==n:
        ret = x[position_zero-1]
    else:
        ret = min(x[position_zero],x[position_zero-1])
else:
    ret =10**8*2
    l_k = min(k,position_zero)
    while l_k >= 0 and position_zero - l_k + k - 1 < n:
        l = abs(min(0,x[position_zero - l_k]))
        r = x[position_zero - l_k + k - 1]
        ret = min(ret,min(l*2+r,l+r*2))
        l_k-=1
print(ret)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

from bisect import bisect_right
position_zero = bisect_right(x,0)
#0の左のk本に火を付ける、
#0の左のk-1本に火を付ける、右の1本に火を付ける
#....で場合分けし、左から行く場合と右から行く場合で最小のものをそれぞれの場合で計算
#ただし、0の左のろうそくがk本未満の場合は、左のposition_zero本に火を付ける、右のk-position_zero本に火を付ける場合からスタート

ret =10**8*2
l_k = min(k,position_zero)
while l_k >= 0 and position_zero - l_k + k - 1 < n:
    l = abs(x[position_zero - l_k])
    r = x[position_zero - l_k + k - 1]
    ret = min(ret,abs(min(l*2+r,l+r*2)))
    l_k-=1
print(ret)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
A = MAP()

if min(A) > 0:
	print(A[K-1])
	sys.exit()
elif max(A) < 0:
	print(list(reversed(A))[K-1])
	sys.exit()

# print(A)
o = bisect.bisect_left(A, 0)
# print(o)

ans = float('INF')
for i in range(K+1):
	j = K - i
	# print('=====', i, j)
	if j==0 and o+K <= N-1:
		# print('zenbu migi',abs(A[o+K-1]))
		ans = min(ans, abs(A[o+K]))
	elif i==0 and o-K >= 0:
		# print('zenbu hidari', abs(A[o-K]))
		ans = min(ans, abs(A[o-K]))
	elif o+i-1 <= N-1 and o-j >=0:
		if i<j:
			# print('migi itte hidari', abs(A[o+i-1]*2) + abs(A[o-j]))
			ans = min(ans, abs(A[o+i-1]*2) + abs(A[o-j]))
		elif i>j:
			# print('hidari itte migi', abs(A[o+i-1]) + abs(A[o-j]*2))
			ans = min(ans, abs(A[o+i-1]) + abs(A[o-j]*2))
		else:
			ans = min(ans, abs(A[o+i-1])+abs(A[o-j])*2, abs(A[o+i-1])*2+abs(A[o-j]))

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N, K = MAP()
A = MAP()

if min(A) > 0:
	print(A[K-1])
	sys.exit()
elif max(A) < 0:
	print(list(reversed(A))[K-1])
	sys.exit()

B = [[abs(a), a] for a in A]
B.sort()
#print(B)

ans = 0
loc = 0
for i in range(K):
	ans += B[i][0] - loc
	loc = B[i][1]
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect
N,K=map(int,input().split())
X=[int(i) for i in input().split()]
min_=10**10
for i in range(N-K+1):
    if X[i] == 0 or X[i+K-1] == 0:
        #print(max(abs(X[i]),abs(X[i+K-1])))
        min_=min(min_,max(abs(X[i]),abs(X[i+K-1])))
    elif np.sign(X[i]) == np.sign(X[i+K-1]):
        #print(max(abs(X[i]),abs(X[i+K-1])))
        min_=min(min_,max(abs(X[i]),abs(X[i+K-1])))
    else:
        #print(abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
        min_=min(min_,abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
print(min_)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect
N,K=map(int,input().split())
X=[int(i) for i in input().split()]
min_=10**10
for i in range(N-K+1):
    if X[i] == 0 or X[i+K-1] == 0:
        print(max(abs(X[i]),abs(X[i+K-1])))
        min_=min(min_,max(abs(X[i]),abs(X[i+K-1])))
    elif np.sign(X[i]) == np.sign(X[i+K-1]):
        print(max(abs(X[i]),abs(X[i+K-1])))
        min_=min(min_,max(abs(X[i]),abs(X[i+K-1])))
    else:
        print(abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
        min_=min(min_,abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
print(min_)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect
N,K=map(int,input().split())
X=[int(i) for i in input().split()]
min_=10**10
for i in range(N-K+1):
    if np.sign(X[i]) == np.sign(X[i+K-1]):
        #print(max(abs(X[i]),abs(X[i+K-1])))
        min_=min(min_,max(abs(X[i]),abs(X[i+K-1])))
    else:
        #print(abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
        min_=min(min_,abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
print(min_)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect
N,K=map(int,input().split())
X=[int(i) for i in input().split()]
min_=10**9
for i in range(N-K+1):
    if np.sign(X[i]) == np.sign(X[i+K-1]):
        #print(max(abs(X[i]),abs(X[i+K-1])))
        min_=min(min_,max(abs(X[i]),abs(X[i+K-1])))
    else:
        #print(abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
        min_=min(min_,abs(min(X[i],X[i+K-1]))*2+abs(max(X[i],X[i+K-1])))
print(min_)

Submission status: Wrong Answer

Solution code:
import bisect
n,k = map(int,input().split())
x = list(map(int,input().split()))
bindex = bisect.bisect_left(x,0)
mainasu = x[:bindex] #[-30,-20,-10]
purasu = x[bindex:] #[10,20,30]
"""
for i in range(len(mainasu)-2,-1,-1): #0は対象ではない
  mainasu[i] = mainasu[i+1]+mainasu[i]
#[-60,-30,-10]
for i in range(1,len(purasu)):
  purasu[i] = purasu[i-1]+purasu[i]
#[10,30,60]
"""
ans = 10**9
if len(mainasu) >= k:
  ans = min(ans,abs(mainasu[-k]))
if len(purasu) >= k:
  ans = min(ans,purasu[k-1])

for i in range(0,len(mainasu)): #-2~2
  if i >= len(purasu):break
  a = abs(mainasu[i])
  b = purasu[i]
  ans = min(ans,a*2+b,a+b*2)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

f=0
s=0
t=N
for i in range(N):
    if(A[i]>=0):
        t=i
        break

M=A[0:t]
for i in range(len(M)):
    M[i]*=-1
M=sorted(M)

if(t==N):
    P=[]
elif(A[t]==0):
    P=A[t+1:N]
    s+=1
else:
    P=A[t:N]
M.append(10**10)
M.insert(0,0)
P.append(10**10)
P.insert(0,0)

t=s
m=1
p=1
a=0
while(True):
    if(t>=K):
        a=P[p-1]+(M[m-1]*2)
        break
    if(P[p]<=(M[m]*2)):
        p+=1
    else:
        m+=1
    t+=1

t=s
m=1
p=1
bin=0
while(True):
    if(t>=K):        
        b=(P[p-1]*2)+M[m-1]
        break
    if((P[p]*2)<=(M[m])):
        p+=1
    else:
        m+=1
    t+=1


print(min(a,b))

Submission status: Wrong Answer

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

f=0
s=0
t=N
for i in range(N):
    if(A[i]>=0):
        t=i
        break

M=A[0:t]
for i in range(len(M)):
    M[i]*=-1
M=sorted(M)

if(t==N):
    P=[]
elif(A[t]==0):
    P=A[t+1:N]
    s+=1
else:
    P=A[t:N]
M.append(10**10)
M.insert(0,0)
P.append(10**10)
P.insert(0,0)

t=s
m=1
p=1
a=0
while(True):
    if(t<=K):
        a=P[p-1]+(M[m-1]*2)
        break
    if(P[p]<=(M[m]*2)):
        p+=1
    else:
        m+=1
    t+=1

t=s
m=1
p=1
bin=0
while(True):
    if(t<=K):        
        b=(P[p-1]*2)+M[m-1]
        break
    if((P[p]*2)<=(M[m])):
        p+=1
    else:
        m+=1
    t+=1


print(min(a,b))

Submission status: Wrong Answer

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

f=0
s=0
t=N
for i in range(N):
    if(A[i]>=0):
        t=i
        break

M=A[0:t]
for i in range(len(M)):
    M[i]*=-1
M=sorted(M)

if(t==N):
    P=[]
elif(A[t]==0):
    P=A[t+1:N]
    s+=1
else:
    P=A[t:N]
M.append(10**10)
M.insert(0,0)
P.append(10**10)
P.insert(0,0)

t=s
m=1
p=1
a=0
while(True):
    if(t==K):
        a=P[p-1]+(M[m-1]*2)
        break
    if(P[p]<=(M[m]*2)):
        p+=1
    else:
        m+=1
    t+=1

t=s
m=1
p=1
bin=0
while(True):
    if(t==K):        
        b=(P[p-1]*2)+M[m-1]
        break
    if((P[p]*2)<=(M[m])):
        p+=1
    else:
        m+=1
    t+=1


print(min(a,b))

Submission status: Wrong Answer

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

f=0
s=0
t=N-1
for i in range(N):
    if(A[i]>=0):
        t=i
        break

M=A[0:t]
for i in range(len(M)):
    M[i]*=-1
M=sorted(M)

if(A[t]==0):
    P=A[t+1:N]
    s+=1
else:
    P=A[t:N]
M.append(10**10)
M.insert(0,0)
P.append(10**10)
P.insert(0,0)

t=s
m=1
p=1
a=0
while(True):
    if(t==K):
        a=P[p-1]+(M[m-1]*2)
        break
    if(P[p]<=(M[m]*2)):
        p+=1
    else:
        m+=1
    t+=1

t=s
m=1
p=1
while(True):
    if(t==K):        
        if(a>(P[p-1]*2)+M[m-1]):
            a=(P[p-1]*2)+M[m-1]
        break
    if((P[p]*2)<=(M[m])):
        p+=1
    else:
        m+=1
    t+=1


print(a)

Submission status: Wrong Answer

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

l_x=[i for i in x if i<0]
r_x=[i for i in x if i>=0]

ans=10**12
for r_num in range(k+1):
  l_num=k-r_num
  
  if l_num>len(l_x) or r_num>len(r_x):
    continue
  if l_num==0: #右片道
    ans=min(ans,r_x[r_num-1])
  elif r_num==0: #左片道
    ans=min(ans,abs(l_x[l_num-1]))
  elif abs(l_x[-1*l_num])<=r_x[r_num-1]: #左往復右片道
    ans=min(ans,abs(l_x[-1*l_num])*2+r_x[r_num-1])
  else: #右往復左片道
    ans=min(ans,abs(l_x[-1*l_num])+(r_x[r_num-1])*2)
print(ans)

Submission status: Wrong Answer

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

l_x=[i for i in x if i<0]
r_x=[i for i in x if i>=0]

ans=10**10
for r_num in range(k+1):
  l_num=k-r_num
  
  if l_num>len(l_x) or r_num>len(r_x):
    continue
  if l_num==0: #右片道
    ans=min(ans,r_x[r_num-1])
  elif r_num==0: #左片道
    ans=min(ans,abs(l_x[l_num-1]))
  elif abs(l_x[-1*l_num])<=r_x[r_num-1]: #左往復右片道
    ans=min(ans,abs(l_x[-1*l_num])*2+r_x[r_num-1])
  else: #右往復左片道
    ans=min(ans,abs(l_x[-1*l_num])+(r_x[r_num-1])*2)
print(ans)