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:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
if 0 in x:
    x.remove(0)
    k -= 1
    n -= 1

if n == 0:
    print(0)
else:
    si = bisect_left(x, 0)

    ans = x[-1] - x[0]
    if si + k - 1 < len(x):
        ans = min(ans, x[si + k - 1])
    if si - k >= 0:
        ans = min(ans, abs(x[si - k]))

    for i in range(n):
        tmp_ans = 0
        if i < si and i + k -1 < len(x):
            tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
            ans = min(ans, tmp_ans)
        elif i - k + 1 >= 0:
            tmp_ans = x[i] * 2 + abs(x[i - k + 1])
            ans = min(ans, tmp_ans)

    print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
# if 0 in x:
#     x.remove(0)
#     k -= 1
#     n -= 1
#
# if n == 0:
#     print(0)

si = bisect_left(x, 0)


ans = x[-1] - x[0]
if si + k - 1 < len(x):
    ans = min(ans, x[si + k - 1])
if si - k >= 0:
    ans = min(ans, abs(x[si - k]))

for i in range(n):
    tmp_ans = 0
    if i < si and i + k -1 < len(x):
        tmp_ans = abs(x[i]) * 2 + x[i + k - 1]
        ans = min(ans, tmp_ans)
    elif i - k + 1 >= 0:
        tmp_ans = x[i] * 2 + abs(x[i - k + 1])
        ans = min(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    m = 114514
    for i in range(n - k + 1):
        m = min(m, abs(x[i + k - 1] - x[i]) + abs(x[i]))
        m = min(m, abs(x[i + k - 1] - x[i]) + abs(x[i + k - 1]))
    print(m)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

point= len(x) - 1
for i, v in enumerate(x):
    if v >= 0:
        point = i
        break

start = max(0, point - K + 1)
end = start + K - 1

if x[point] != 0:
    start -= 1
    end -= 1
    
ans = float('inf')

while True:
    if x[start] >= 0:
        time = x[end]
    elif x[end] <= 0:
        time = abs(x[start])
    elif abs(x[start]) <= x[end]:
        time = abs(x[start] * 2) + x[end]
    else:
        time = abs(x[start]) + (x[end] * 2)
    ans = min(ans, time)
    
    start += 1
    end += 1

    if start > point or end == len(x):
        break
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
c=list(map(int,input().split()))
c.sort(key=lambda x:abs(x))
lit=c[:k]
lit.sort()
if lit[0]*lit[-1]<0:
  d=abs(lit[0])+abs(lit[-1])+min(abs(lit[0]),abs(lit[-1]))
else:
  d=max(abs(lit[0]),abs(lit[-1]))
print(d)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
ans=1000000010
if N-K==0:
	print(0)
	exit() 
for i in range(N-K+1):
	if L[i]<0<L[i+K-1]:
		ans=min(ans,abs((L[i]*2)-L[i+K-1]),abs(L[i]-(L[i+K-1]*2)))
	elif L[i]<0:
		ans=min(ans,abs(0-L[i]))
	else:
		ans=min(ans,abs(0-L[i+K-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
ans=1000000010
if N-K==0:ans=0 
for i in range(N-K+1):
	if L[i]<0<L[i+K-1]:# or L[i+K-1]<0<L[i]:
		ans=min(ans,abs((L[i]*2)-L[i+K-1]),abs(L[i]-(L[i+K-1]*2)))
	elif L[i]<0:
		ans=min(ans,abs(0-L[i]))
	else:
		ans=min(ans,abs(0-L[i+K-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
ans=1000000010
if N-K==0:ans=0 
for i in range(N-K):
	if L[i]<0<L[i+K-1] or L[i+K-1]<0<L[i]:
		ans=min(ans,abs((L[i]*2)-L[i+K-1]),abs(L[i]-(L[i+K-1]*2)))
	else:
		ans=min(ans,abs(0-L[i+K-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
L=list(map(int,input().split()))
ans=1000000010
if N-K==0:ans=0 
for i in range(N-K):
	if L[i]<0<L[i+K-1] or L[i+K-1]<0<L[i]:
		ans=min(abs((L[i]*2)-L[i+K-1]),abs(L[i]-(L[i+K-1]*2)))
	else:
		ans=min(ans,abs(0-L[i+K-1]))
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 I(): return int(input())
def LS(): return input().split()
def S(): return input()

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

  l1=[]
  l2=[]

  for x in l:
    if x==0:
      k-=1
    elif x<0:
      l1.append(x)
    else:
      l2.append(x)

  l1.reverse()

  if k==0:
    print(0)
    exit()
  if len(l1)==0:
    print(l2[k-1])
    exit()
  elif len(l2)==0:
    print(l1[k-1])
    exit()

  l1=[0]+l1
  l2=[0]+l2

  sm=inf
  for i in range(k):
    a=0
    if len(l1)<i+1 or len(l2)<k-i+1:
      pass
    else:
      a-=l1[i]
      a+=l2[k-i]*2
      sm=min(sm,a)

  for i in range(k):
    a=0
    if len(l2)<i+1 or len(l1)<k-i+1:
      pass
    else:
      a+=l2[i]
      a-=l1[k-i]*2
      sm=min(sm,a)

  return sm

print(main())

Submission status: Wrong Answer

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

x_abs = sorted(x, key = lambda x: abs(x))
candles = x_abs[:K]
pos = []
neg = []
pos_exist = False
neg_exist = False
max_pos = 0
max_neg = 0
for k in range(K):
    if candles[k] >= 0:
        pos.append(candles[k])
        pos_exist = True
    else:
        neg.append(abs(candles[k]))
        neg_exist = True
if pos_exist:        
    max_pos = max(pos)
if neg_exist:
    max_neg = max(neg)

if max_pos >= max_neg:
    print(max_pos + max_neg*2)
else:
    print(max_pos*2 + max_neg)

Submission status: Wrong Answer

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

mini = 10**9
for i in range(n-k+1):
    ls = x[i:i+k]
    flg1 = abs(ls[0])+abs(ls[0]-ls[k-1])
    flg2 = abs(ls[k-2])+abs(ls[0]-ls[k-1])
    flg = min(flg1,flg2)
    mini = min(flg,mini)
print(mini)

Submission status: Wrong Answer

Solution code:
N, K = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
res = float('inf')
for i in range(N-K+1):
    tmp = abs(x[i])
    tmp += abs(x[i] - x[i+K-1])
    res = min(res, tmp)
print(res)

Submission status: Wrong Answer

Solution code:
def smaller(x, y):
	if x < y: return x
	return y

N, K = map(int, input().split())
A = list(map(int, input().split()))
min = 5000000000000000
for i in range(0, N-K+1):
	left = A[i]
	right = A[i+K-1]
	if left <= 0 and 0 <= right:
		min = smaller(min, right-left+smaller(-left, right))
	elif 0 < left:
		if i == 0:
			min = smaller(min, right)
		else:
			if A[i-1] < 0:
				min = smaller(min, right)
	else:
		if i == N-1:
			min = smaller(min, -left)
		else:
			if 0 < A[i]:
				min = smaller(min, -left)
print(min)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
res = 10**10
for i in range(N-K+1):
    if X[i]*X[i+K-1] >= 0:
        res = min(res,abs(X[i+K-1]))
    else:
        res = min(res,abs(X[i])*2+X[i+K-1],X[i+K-1]*2+abs(X[i]))
print(res)

Submission status: Wrong Answer

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

if N==K:
    ans.append(x[-1]-x[0])
else:
    for i in range(N-K+1):
        ans.append(x[i+K-1]-x[i]+min(abs(x[i+K-1]),abs(x[i])))

print(min(ans))

Submission status: Wrong Answer

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

if N==K:
    ans.append(x[-1]-x[0])
else:
    for i in range(N-K):
        ans.append(x[i+K-1]-x[i]+min(abs(x[i+K-1]),abs(x[i])))

print(min(ans))

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
c=list(map(int,input().split()))
m=10*9
for i in range(n-k+1):
        l=c[i]
        r=c[i+k-1]
        m=min(m,abs(l)+abs(r-l),abs(r)+abs(r-l))

print(m)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
c=list(map(int,input().split()))
m=1000000
for i in range(n-k+1):
        l=c[i]
        r=c[i+k-1]
        m=min(m,abs(l)+abs(r-l),abs(r)+abs(r-l))

print(m)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
candles=list(map(int,input().split()))
minimum=1000000
for i in range(n-k+1):
        minimum=min(minimum,candles[i+k-1]-candles[i]+min(abs(candles[i]),abs(candles[i+k-1])))

print(minimum)

Submission status: Wrong Answer

Solution code:
n,k=list(map(int,input().split()))
candles=list(map(int,input().split()))
rows=[]
minimum=10000000
for i in range(n-k+1):
        new=candles[i:i+k]
        l_r=abs(new[0])+abs(new[-1]-new[0])
        r_l=abs(new[-1])+abs(new[-1]-new[0])
        if minimum>min(l_r,r_l):
                minimum=min(l_r,r_l)
print(minimum)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int, input().split()))
mid = -1
if 0 in X:
    k -= 1
    X.append(10**12)
else:
    if X[0] > 0:
        X = [0] + X
    elif X[n-1] < 0:
        X.append(0)
    sign = False
    for i in range(n-1):
        if X[i]*X[i+1] < 0:
            X = X[:i+1] + [0] + X[i+1:]
            break
mid = X.index(0)
rmax = 0
lmax = 0
l = 1
r = 1
for i in range(1,k+1):
    rn = abs(X[mid+r]) if mid+r <= n else 10**11
    ln = abs(X[mid-l]) if mid-l >= 0 else 10**11
    if ln < rn:
        #print("l",l,r)
        l += 1
        if ln < 10**11: lmax = ln
    else:
        #print("r",l,r)
        r += 1
        if rn < 10*11: rmax = rn
print(2*min(lmax,rmax) + max(lmax,rmax))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int, input().split()))
mid = -1
if 0 in X:
    k -= 1
    X.append(10**11)
else:
    if X[0] > 0:
        X = [0] + X
    elif X[n-1] < 0:
        X.append(0)
    sign = False
    for i in range(n-1):
        if X[i]*X[i+1] < 0:
            X = X[:i+1] + [0] + X[i+1:]
            break
mid = X.index(0)
rmax = 0
lmax = 0
l = 1
r = 1
for i in range(1,k+1):
    rn = abs(X[mid+r]) if mid+r <= n else 10**11
    ln = abs(X[mid-l]) if mid-l >= 0 else 10**11
    if ln < rn:
        #print("l",l,r)
        l += 1
        if ln < 10**11: lmax = ln 
    else:
        #print("r",l,r)
        r += 1
        if rn < 10*11: rmax = rn
print(2*min(lmax,rmax) + max(lmax,rmax))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int, input().split()))
mid = -1
if 0 in X:
    k -= 1
    X.append(10**11)
else:
    if X[0] > 0:
        X = [0] + X
    elif X[n-1] < 0:
        X.append(0)
    sign = False
    for i in range(n-1):
        if X[i]*X[i+1] < 0:
            X = X[:i+1] + [0] + X[i+1:]
            break
mid = X.index(0)
rmax = 0
lmax = 0
l = 1
r = 1
for i in range(1,k+1):
    rn = abs(X[mid+r]) if mid+r <= n else 10**11
    ln = abs(X[mid-l]) if mid-l >= 0 else 10**11
    if ln < rn:
        #print("l",l,r)
        l += 1
        lmax = ln
    else:
        #print("r",l,r)
        r += 1
        rmax = rn
print(2*min(lmax,rmax) + max(lmax,rmax))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int, input().split()))
mid = -1
if 0 in X:
    k -= 1
else:
    if X[0] > 0:
        X = [0] + X
    elif X[n-1] < 0:
        X.append(0)
    sign = False
    for i in range(n-1):
        if X[i]*X[i+1] < 0:
            X = X[:i+1] + [0] + X[i+1:]
            break
mid = X.index(0)
rmax = 0
lmax = 0
l = 1
r = 1
for i in range(1,k+1):
    rn = abs(X[mid+r]) if mid+r <= n else 10**11
    ln = abs(X[mid-l]) if mid-l >= 0 else 10**11
    if ln < rn:
        #print("l",l,r)
        l += 1
        lmax = ln
    else:
        #print("r",l,r)
        r += 1
        rmax = rn
print(2*min(lmax,rmax) + max(lmax,rmax))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int, input().split()))
mid = -1
if 0 in X:
    k -= 1
else:
    if X[0] > 0:
        X = [0] + X
    elif X[n-1] < 0:
        X.append(0)
    sign = False
    for i in range(n-1):
        if X[i]*X[i+1] < 0:
            X = X[:i+1] + [0] + X[i+1:]
            break
mid = X.index(0)
rmax = 0
lmax = 0
l = 1
r = 1
for i in range(1,k+1):
    rn = abs(X[mid+r]) if mid+r <= n else 10**10
    ln = abs(X[mid-l]) if mid-l >= 0 else 10**10
    if ln < rn:
        #print("l",l,r)
        l += 1
        lmax = ln
    else:
        #print("r",l,r)
        r += 1
        rmax = rn
print(2*min(lmax,rmax) + max(lmax,rmax))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = list(map(int,input().split()))
data = [int(s) for s in input().split()]
ans =[]
for i in range(n-k + 1):
    ans.append( abs(data[i]) + abs(data[i+k-1] - data[i]))

print(min(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def solve(X, K):
    l = N
    for i in range(len(X)):
        if X[i] >= 0:
            l = i
            break
    if i == 0:          # X[i] > 0 for all i
        return X[K-1]
    if i == N:          # X[i] < 0 for all i
        return -X[-K]

    if X[i] == 0:
        K -= 1
        L, R = X[:i-1], X[i+1:]
    else:
        L, R = X[:i], X[i:]
    L = sorted([-r for r in L])
    #print(L, R) 
    m = 10 ** 10
    if K < len(L):
        m = min(m, L[K-1])
    if K < len(R):
        m = min(m, R[K-1])
    for r in range(len(R)):
        l = K-r-2
        if not 0 <= l < len(L):
            continue
        t =  2 * L[l] + R[r]
        #print("L", "t=%d"%t, "2*%d"%L[l], R[r], sep="\t")
        m = min(m, t)
    for l in range(len(L)):
        r = K-l-2
        if not 0 <= r < len(R):
            continue
        t = 2 * R[r] + L[l]
        #print("R", "t=%d"%t, L[l], "2*%d"%R[r], sep="\t")
        m = min(m, t)
    return m

print(solve(X, K))

Submission status: Wrong Answer

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

def solve(X):
    l = N
    for i in range(len(X)):
        if X[i] >= 0:
            l = i
            break
    if i == 0:          # X[i] > 0 for all i
        return X[K-1]
    if i == N:          # X[i] < 0 for all i
        return -X[-K]

    if X[i] == 0:
        L, R = X[:i-1], X[i+1:]
    else:
        L, R = X[:i], X[i:]
    L = sorted([-r for r in L])
    #print(L, R) 
    m = 10 ** 10
    if K < len(L):
        m = min(m, L[K-1])
    if K < len(R):
        m = min(m, R[K-1])
    for r in range(len(R)):
        l = K-r-2
        if not 0 <= l < len(L):
            continue
        t =  2 * L[l] + R[r]
        #print("L", "t=%d"%t, "2*%d"%L[l], R[r], sep="\t")
        m = min(m, t)
    for l in range(len(L)):
        r = K-l-2
        if not 0 <= r < len(R):
            continue
        t = 2 * R[r] + L[l]
        #print("R", "t=%d"%t, L[l], "2*%d"%R[r], sep="\t")
        m = min(m, t)
    return m

print(solve(X))

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]
pos_i = -1
for i in range(len(x)):
    if x[i] > 0:
        pos_i = i
        break

min_t = 200000000
if pos_i == -1:
    min_t = -x[-K]
else:
    for i in range(N-K+1):
        if i+K-1 < pos_i:
            min_t = min(min_t, -x[pos_i-K])
        elif pos_i <= i:
            min_t = min(min_t, x[pos_i+K-1])
        else:
            min_t = min(min_t, x[K+i-1]-2*x[i], x[K+i-1]*2-x[i])

print(min_t)

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]
pos_i = -1
for i in range(len(x)):
    if x[i] >= 0:
        pos_i = i
        break

min_t = 200000000
if pos_i == -1:
    min_t = -x[-K]
else:
    for i in range(N-K+1):
        if i+K-1 < pos_i:
            min_t = min(min_t, -x[pos_i-K])
        elif pos_i <= i:
            min_t = min(min_t, x[pos_i+K-1])
        else:
            min_t = min(min_t, x[K+i-1]-2*x[i], x[K+i-1]*2-x[i])

print(min_t)

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]
pos_i = -1
for i in range(len(x)):
    if x[i] >= 0:
        pos_i = i
        break

min_t = 200000000
if pos_i == -1:
    min_t = -x[-K]
else:
    for i in range(N-K+1):
        if i+K < pos_i:
            min_t = min(min_t, -x[pos_i-K])
        elif pos_i <= i:
            min_t = min(min_t, x[pos_i+K-1])
        else:
            min_t = min(min_t, x[K+i-1]-2*x[i], x[K+i-1]*2-x[i])

print(min_t)

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
x = [int(_) for _ in input().split()]
pos_i = -1
for i in range(len(x)):
    if x[i] >= 0:
        pos_i = i
        break

min_t = 200000000
if pos_i == -1:
    min_t = -x[-K]
else:
    for i in range(N-K+1):
        if i+K < pos_i:
            min_t = -x[pos_i-K]
        elif pos_i <= i:
            min_t = x[pos_i+K-1]
        else:
            min_t = min(min_t, x[K+i-1]-2*x[i], x[K+i-1]*2-x[i])

print(min_t)

Submission status: Wrong Answer

Solution code:
def fugou(n):
    if n<0:
        return -1
    else:
        return 1

def answer(N,K,A):
    ans=float('inf')
    for i in range(N-K+1):
        print(A[i],A[i+K-1])
        if fugou(A[i])==fugou(A[i+K-1]) and fugou(A[i])==1:
            kari=A[i+K-1]
        elif fugou(A[i])==fugou(A[i+K-1]) and fugou(A[i])==-1:
            kari=fugou(A[i])
        else:
            if abs(A[i])<=abs(A[i+K-1]):
                kari=2*abs(A[i])+A[i+K-1]
            else:
                kari=abs(A[i])+2*A[i+K-1]
        print(kari,ans)
        if kari<ans:
            ans=kari
    return ans

Submission status: Wrong Answer

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

cnt =0
seek = 0

#左から原点をまたぐ場合
if x[0] <= 0 and 0 <= x[N-1]:
    #左に長い場合
    if abs(x[0]) >= abs(x[N-1]):
        for i in range(x[N-1]+1):
            
            if i in x:
                cnt +=1
                if cnt == K:
                    print(i)
                    exit()
            seek = abs(i)

        seek = seek *2

        for i in range(0, x[0]-1, -1):
            seek += 1
            if i in x:
                cnt +=1
                if cnt == K:
                    print(seek)
                    exit()


    #右に長い場合
    else:

        for i in range(0, x[0]-1, -1):
            print(i)
            if i in x:
                cnt +=1
                print(cnt)
                if cnt == K:
                    print(i)
                    exit()
            seek = abs(i)

        seek = seek *2

        for i in range(x[N-1]+1):
            seek += 1
            if i in x:
                cnt +=1
                if cnt == K:
                    print(seek)
                    exit()



#原点よりすべて正
elif  x[0] >= 0 and 0 < x[N-1]:
    for i in range(x[N-1]):
        if i in x:
            cnt +=1
            if cnt == K:
                print(i)
                exit()



#原点よりすべて負
elif x[0] < 0 and 0 >= x[N-1]:
    for i in range(x[0]):
        if i in x:
            cnt +=1
            if cnt == K:
                print(i)
                exit()

Submission status: Wrong Answer

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

if X[0] >= 0:
    print(X[K - 1])
    exit()

if X[-1] <= 0:
    print(-X[N - K])
    exit()

ans = 2 * 10**8
for i in range(K, N + 1):
    l = i - K
    r = i - 1
    if X[l] <= 0 <= X[r]:
        abs_l = -X[l]
        abs_r = X[r]
        ans = min(ans, min(abs_l, abs_r) * 2 + max(abs_l, abs_r))
print(ans)

Submission status: Wrong Answer

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

if X[0] >= 0:
    print(X[K - 1])
    exit()

if X[-1] <= 0:
    print(X[N - K])
    exit()

ans = 2 * 10**8
for i in range(K, N + 1):
    l = i - K
    r = i - 1
    if X[l] <= 0 <= X[r]:
        abs_l = -X[l]
        abs_r = X[r]
        ans = min(ans, min(abs_l, abs_r) * 2 + max(abs_l, abs_r))
print(ans)

Submission status: Wrong Answer

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

if min(X) >= 0:
    print(X[K - 1])
    exit()

if max(X) <= 0:
    print(X[N - K])
    exit()

ans = float('inf')
for i in range(K, N):
    l = i - K
    r = i - 1
    if X[l] <= 0 <= X[r]:
        abs_l = abs(X[l])
        abs_r = X[r]
        ans = min(ans, min(abs_l, abs_r) * 2 + max(abs_l, abs_r))
print(ans)

Submission status: Wrong Answer

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

if min(X) >= 0:
    print(X[K - 1])
    exit()

if max(X) <= 0:
    print(X[N - K])
    exit()

ans = float('inf')
for k in range(K, N):
    Y = X[k - K:k]
    if min(Y) <= 0 <= max(Y):
        abs_l = abs(min(Y))
        abs_r = max(Y)
        dist = 0
        if abs_l > abs_r:
            dist = abs_r * 2 + abs_l
        else:
            dist = abs_l * 2 + abs_r
        ans = min(ans, dist)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 1e8

for i in range(N-K+1):
    l,r = x[i],x[i+K-1]
    if l < 0 and r <= 0: temp = -l
    elif l >= 0 and r > 0:   temp = r
    else:
        if -l < r:    temp = 2*(-l) + r
        else:    temp = -l + 2*r  
    
    if ans > temp:  ans = temp

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
n,k = map(int, input().split())
x = list(map(int, input().split()))
left = bisect_left(x, 0)
l = [abs(i) for i in x[left-1::-1]]
r = x[left:]
l = [0] + l
r = [0] + r

#print(l, r)

ans = float("inf")
for i in range(k+1):
    j = k-i
    if i >= len(l) or j >= len(r):
        continue
    ans = min(ans, l[i]+r[j] + min(l[i], r[j]))
print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    N-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if i[0] <= len(xp) and i[1] <= len(xm):
                if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
                else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
                
                if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    N-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if i[0] > len(xp) or i[1] > len(xm):    continue
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    N-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N-1:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if i[0] > len(xp) or i[1] > len(xm):    continue
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N-1:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if i[0] > len(xp) or i[1] > len(xm):    continue
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N-1:
        xp,xm = x[border:],x[:border][::-1]
        if len(xp) >= K and len(xm) >= K:
            ind = [[K-i, i] for i in range(K)] 
        elif len(xp) < len(xm):
            ind = [[len(xp)-i, K-len(xp)+i] for i in range(len(xp))]
        else:
            ind = [[K-len(xm)+i, len(xm)-i] for i in range(len(xm))]

        for i in ind:
            if i[0] > len(xp) or i[1] > len(xm):    break
            if xp[i[0]-1] > -xm[i[1]-1]:    temp = xp[i[0]-1] - 2*xm[i[1]-1]
            else:   temp = 2*xp[i[0]-1] - xm[i[1]-1]
            
            if anso > temp: anso = temp
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,mmax,i = xn[-1],0,2
            while i <= K and xn[-i] > 0:
                i += 1
            if i <= K:  mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            pmax,mmax,i = 0,xn[-1],2
            while i <= K and xn[-i] < 0:
                i += 1
            if i <= K:  pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    x.pop(x.index(0))

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K+1]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,mmax,i = xn[-1],0,2
            while i <= K and xn[-i] > 0:
                i += 1
            if i <= K:  mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            pmax,mmax,i = 0,xn[-1],2
            while i <= K and xn[-i] < 0:
                i += 1
            if i <= K:  pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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

if  0 in x:
    K-=1
    x.pop(0)

if K > 0:
    ansp,ansm,anso,border = 1e8,1e8,1e8,0
    while border < N and x[border] < 0:
        border += 1
    
    if N - border >= K: ansp = x[border+K-1]
    if border >= K: ansm = -x[border-K+1]
    
    if border!=0 and border!=N-1:
        xn = sorted(x,key = lambda x: abs(x))[:K]
        if xn[-1] > 0:
            pmax,mmax,i = xn[-1],0,2
            while i <= K and xn[-i] > 0:
                i += 1
            if i <= K:  mmax = xn[-i]
            anso = pmax - 2*mmax
        else:
            pmax,mmax,i = 0,xn[-1],2
            while i <= K and xn[-i] < 0:
                i += 1
            if i <= K:  pmax = xn[-i]
            anso = 2*pmax - mmax
    
    ans = min(ansp,ansm,anso)

print(ans)

Submission status: Wrong Answer

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