Problem:


Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10



Sample input:
5 3
-30 -10 10 20 50


Sample output:
40

Submission status: Wrong Answer

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

if k == 1:
  print(min(list(map(abs, x))))
  
else:

  if 0 in x:
    k -= 1
  
  x_n = list(filter(lambda y: y < 0, x))
  x_p = list(filter(lambda y: y > 0, x))
  
  if x_n == []:
    print(x_p[k-1])
  elif x_p == []:
    print(x_n[-k])
    
  else:
    ans = 10**9

    #print('---p1---')
    if len(x_p) >= k:
      ans = min(ans, x_p[k-1])
  
    #print('---p2---')
    if len(x_n) >= k:
      ans = min(ans, -x_n[-k])
  
    #print('---p3---')  

    sup = min(k, len(x_p))
    inf = max(k - len(x_n), 1)
    for i in range(inf, sup+1):
      d = 2 * x_p[i-1] - x_n[-k+i]
      ans = min(ans, d)
    
    #print('---p4---')    
    
    sup = min(k, len(x_n))
    inf = max(k - len(x_p), 1)
    for i in range(inf, sup+1):
      d = - 2 * x_n[-i] + x_p[k-i-1]
      ans = min(ans, d)
      
    print(ans)

Submission status: Wrong Answer

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

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


def calc_time(X):
    x_max = X[-1]
    x_min = X[0]

    if x_max * x_min >= 0:
        if abs(x_max) > abs(x_min):
            return x_max
        else:
            return abs(x_min)
    else:
        return x_max - x_min + min(abs(x_max), abs(x_min))

def calc_start_and_goal_position(index):
    if index - K >= 0:
        start_index = index - K
    else:
        start_index = 0
    
    if N > index + K:
        goal_index = index
    else:
        goal_index = N - K + 1
    
    return start_index, goal_index

if __name__ == "__main__":
    index = -1
    for i, x in enumerate(X):
        if x >= 0:
            index = i 
    if index == -1:
        index = i
    
    start_index, goal_index = calc_start_and_goal_position(index)

    time_min = float('inf')
    for i in range(start_index, goal_index):
        time = calc_time(X[i:i+K])
        if time < time_min:
            time_min = time

    print(time_min)

Submission status: Wrong Answer

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

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

def calc_time(X):
    x_max = X[-1]
    x_min = X[0]

    if x_max * x_min >= 0:
        if abs(x_max) > abs(x_min):
            return x_max
        else:
            return x_min
    else:
        return x_max - x_min + min(abs(x_max), abs(x_min))

time_min = float('inf')
for i in range(N - K + 1):
    time = calc_time(X[i:i+K])
    if time < time_min:
        time_min = time

print(time_min)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
res = 9999999999999
for i in range(N-K+1):
    val = abs(X[i]) + abs(X[K-1+i] - X[i])
    if res > val:
        res = val
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np

def solve(x, K):
    N = x.shape[0]
    c = np.searchsorted(x, 0)
    if c == 0:
        return x[K - 1]
    if c == N:
        return -x[N - K]
    a = -x[:c][::-1]  # 負の数
    b = x[c:]  # 正の数
    if c >= K:
        ans = a[c - K]
        start = 0
    else:
        start = K - c - 1
        ans = 3 * 100000000
    if len(b) >= K:
        end = K - 1
        ans = min(ans, b[K - 1])
    else:
        end = len(b)

    for j in range(start, end):
        x1 = b[j]
        # K - j - 1
        x2 = a[K - j - 2]
        ans = min(ans, x1 + x2 + min(x1, x2))
    return ans


stdin = open(0, 'rb')
pin = np.fromstring(stdin.read(), dtype=np.int64, sep=' ')
N, K = pin[:2]
x = pin[2:]
ans = solve(x, K)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    pos = []
    neg = []
    zero = []
    for x in map(int, input().split()):
        if x < 0: neg.append(x)
        elif x > 0: pos.append(x)
        else: zero.append(x)
    K -= len(zero)
    if K <= 0: return 0
    minT = float('inf')
    for i in range(K):
        dneg, dpos = 0, 0
        if i <= len(neg) and K-i <= len(pos):
            if i > 0:
                dneg = -neg[len(neg)-i]
            if K-i > 0:
                dpos = pos[K-i-1]
            minT = min([minT, dneg*2+dpos, dneg+2*dpos])
    return minT

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    pos = []
    neg = []
    zero = []
    for x in map(int, input().split()):
        if x < 0: neg.append(x)
        elif x > 0: pos.append(x)
        else: zero.append(x)
    K -= len(zero)
    if K <= 0: return 0
    minT = float('inf')
    for i in range(K):
        dneg, dpos = 0, 0
        if i <= len(neg) and K-i <= len(pos):
            if i > 0:
                dneg = -neg[len(neg)-i]
            if K-i > 0:
                dpos = pos[K-i-1]
            minT = min([minT, dneg*2+dpos, 2*dneg+dpos])
    return minT

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main(): 
    N, K = LI()
    x_li = LI()  # sorted

    # はじめに正の方向に動くとき：
    # マイナスなら-1倍, プラスなら2倍して再ソートする。 O(nlogn)
    plus_first = sorted([(-x, 'm') if x<0 else (2*x, 'p') for x in x_li])

    # はじめに負の方向に動くとき：
    minus_first = sorted([(-2*x, 'm') if x<0 else (x, 'p') for x in x_li])

    if plus_first[K-1][0] < minus_first[K-1][0]:  li = plus_first
    else: li = minus_first

    max_p, max_m = 0,0
    for val, pm in li[:K]:
        if pm == 'p': max_p = val
        else:         max_m = val

    print(max_p + max_m)


main()

Submission status: Wrong Answer

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

ans = max(abs(x[0]), abs(x[-1])) * 2

for i in range(n - k + 1):
    a, b = x[i], x[i + k - 1]
    if b <= 0:
        ans = min(ans, abs(a))
    elif a >= 0:
        ans = min(ans, b)
    else:
        d = min(abs(a), b) * 2 + max(abs(a), b)
        ans = min(ans, d)

print(ans)

Submission status: Wrong Answer

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

distance = []
for l in range(0, n-k + 1):
    r = l + k
    select = x[l:r]
    prevX = 0
    dx = 0
    for i in select:
        dx += abs(i - prevX)
        prevX = i
    distance.append(dx)

print(min(distance))

Submission status: Wrong Answer

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

distance = []
for l in range(0, n-k + 1):
    r = l + k
    select = x[l:r]
    print(select)

    prevX = 0
    dx = 0
    for i in select:
        dx += abs(i - prevX)
        prevX = i
    distance.append(dx)

print(min(distance))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = list(map(int,input().split()))
min_d =2*(10**8)

for i in range(n-k+1):
    l,r = X[i],X[i+k-1]
    if l<0 and r <=0:
        d = abs(l)
    elif l <=0 and r>=0:
        d = min(abs(l),abs(r))*2 + max(abs(l),abs(r))
    else:
        d = r
    min_d = min(min_d,d)
print(min_d)

Submission status: Wrong Answer

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

ans = max(abs(x[0]), abs(x[-1])) * 2

for i in range(n - k + 1):
    a, b = x[i], x[i + k - 1]
    ans = min(ans, min(abs(a), abs(b)) + abs(b - a))

print(ans)

Submission status: Wrong Answer

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

ans = max(abs(x[0]), abs(x[-1])) * 2

for i in range(n - k + 1):
    a, b = x[i], x[i + k - 1]
    if a > 0:
        ans = min(ans, b)
    elif b < 0:
        ans = min(ans, abs(a))
    else:
        d1 = abs(a) + abs(b - a)
        d2 = b + abs(a - b)
        ans = min(ans, d1, d2)

print(ans)

Submission status: Wrong Answer

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

ans = max(abs(x[0]), abs(x[-1])) * 2

for i in range(n - k):
    a, b = x[i], x[i + k - 1]
    if a > 0:
        ans = min(ans, b)
    elif b < 0:
        ans = min(ans, abs(a))
    else:
        d1 = abs(a) + abs(b - a)
        d2 = b + abs(a - b)
        ans = min(ans, d1, d2)

print(ans)

Submission status: Wrong Answer

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

ans = max(abs(x[0]), abs(x[-1])) * 2

for i in range(n - k):
    d1 = abs(x[i]) + abs(x[i + k - 1] - x[i])
    d2 = abs(x[i + k - 1]) + abs(x[i] - x[i + k - 1])
    ans = min(ans, d1, d2)

print(ans)

Submission status: Wrong Answer

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

ans = abs(x[0] - x[-1])

for i in range(n - k):
    d1 = abs(x[i]) + abs(x[i + k - 1] - x[i])
    d2 = abs(x[i + k - 1]) + abs(x[i] - x[i + k - 1])
    ans = min(ans, d1, d2)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k= map(int,input().split())
x = list(map(int, input().split()))
if 0 not in x:
    k += 1
    x.append(0)
    x = sorted(x)
x = [abs(i) for i in x]

orig = x.index(0)

ans = float("inf")
for i in range(k):
    f = orig + i
    s = orig + i - k
    if (s >= 0) and (f <= n-1):
        temp1 = x[s]
        temp2 = x[f]
        ans = min([ans, 2*temp1+temp2, temp1+2*temp2])

print(ans)

Submission status: Wrong Answer

Solution code:
n, k= map(int,input().split())
x = list(map(int, input().split()))
if 0 not in x:
    k += 1
    x.append(0)
    x = sorted(x)
x = [abs(i) for i in x]

orig = x.index(0)

ans = float("inf")
for i in range(k):
    f = orig + i
    s = orig + i - (k - 1)
    if (s >= 0) and (f <= n-1):
        temp1 = x[s]
        temp2 = x[f]
        ans = min([ans, 2*temp1+temp2, temp1+2*temp2])

print(ans)

Submission status: Wrong Answer

Solution code:
n, k= map(int,input().split())
x = list(map(int, input().split()))
if 0 not in x:
    k += 1
    x.append(0)
x = sorted(x)
x = [abs(i) for i in x]

orig = x.index(0)

ans = float("inf")
for i in range(k):
    f = orig + i
    s = orig + i -k + 1
    if (s >= 0) and (f <= n-1):
        temp1 = x[s]
        temp2 = x[f]
        ans = min([ans, 2*temp1+temp2, temp1+2*temp2])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, K, inputs):
    candles = string_to_int(inputs[0])
    min_distance = -1
    for i in range(0, N-K+1):
        left = candles[i]
        right = candles[i+K-1]
        distance = 0
        if left <= 0 and right <= 0:
            distance = abs(left) - abs(right) + abs(0 - right)
        elif left >= 0 and right >= 0:
            distance = right
        else:
            if abs(right) > abs(left):
                distance = abs(left) + abs(left) + abs(right)
            else:
                distance = abs(right) + abs(right) + abs(left)
        if min_distance == -1 or min_distance > distance:
            min_distance = distance
        print(left, right)

    return min_distance


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    [N, K] = string_to_int(input())
    ret = solve(N, K, inputs(1))
    print(ret)

Submission status: Wrong Answer

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


def solve(N: int, K: int, x: "List[int]"):

    ans = 10000000000000000000


    al = []

    for i in range(N-K):
        if(x[i] <= 0 <= x[i+K-1]):
            w = abs(x[i+K-1] - x[i]) + min(abs(x[i+K-1]), abs(x[i]))
        else:
            w = max(abs(x[i+K-1]),abs(x[i]))
        ans = min(ans, w)
        al = [x[i], x[i+K-1]]

    if(not al):
        print(0)
    else:
        print(ans)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    x = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
a=[None]*(N-K+1)
for i in range(N-K+1):
    if x[i]<0 and x[i+K-1]>0:
        if x[i]+x[i+K-1]>0:
            a[i]=-2*x[i]+x[i+K-1]
        else:
            a[i]=-x[i]+2*x[i+K-1]
    elif x[i]>=0:
        a[i]=x[i+K-1]
    else:
        a[i]=x[i]
print(min(a))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
a=[None]*(N-K+1)
for i in range(N-K+1):
    if x[i]<0 and x[i+K-1]>0:
        if x[i]+x[i+K-1]>0:
            a[i]=-2*x[i]+x[i+K-1]
        else:
            a[i]=-x[i]+2*x[i+K-1]
    elif x[i]>0:
        a[i]=x[i+K-1]
    else:
        a[i]=x[i]
print(min(a))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
m=0
zf=0
for i in range(n):
    if l[i]==0:
        zf=1
if zf==0:
    l.append(0)
    l.sort()
else:
    k-=1
if k==0:
    print(0)
    exit()
n=len(l)
pl=[]
nl=[]
for i in range(n):
    if l[i]==0:
        m=i
    elif l[i]>0:
        pl.append(l[i])
    else:
        nl.append(-l[i])
        
ans=1e10
for i in range(min(k-1,len(pl))):
    a=len(nl)-k-i+1
    if a>=0 and a<len(nl):
        ans=min(ans,2*pl[i]+nl[len(nl)-k-i+1])
for i in range(min(k-1,len(nl))):
    a=len(nl)-i-1
    b=k-i-2
    if b>=0 and b<len(pl):
        ans=min(ans,2*nl[a]+pl[k-i-2])
if k<=len(pl):
    ans=min(ans,pl[k-1])
if k<=len(nl):
    ans=min(ans,nl[len(nl)-k])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
m=0
zf=0
for i in range(n):
    if l[i]==0:
        zf=1
if zf==0:
    l.append(0)
    l.sort()
else:
    k-=1
if k==0:
    print(0)
    exit()
n=len(l)
pl=[]
nl=[]
for i in range(n):
    if l[i]==0:
        m=i
    elif l[i]>0:
        pl.append(l[i])
    else:
        nl.append(-l[i])
        
ans=1e10
for i in range(min(k,len(pl))):
    a=len(nl)-k-i+1
    if a>=0 and a<len(nl):
        ans=min(ans,2*pl[i]+nl[len(nl)-k-i+1])
for i in range(min(k,len(nl))):
    a=len(nl)-i-1
    b=k-i-2
    if b>=0 and b<len(pl):
        ans=min(ans,2*nl[a]+pl[k-i-2])
if k<=len(pl):
    ans=min(ans,pl[k-1])
if k<=len(nl):
    ans=min(ans,nl[len(nl)-k])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


right_candle = [0] + [i for i in candle_list if i>0]
left_candle = [i for i in candle_list if i<=0] + [0]


right_only = left_only = float('inf')

if k <= len(right_candle) - 1:
    right_only = right_candle[k]
if k <= len(left_candle) - 1:
    left_only = -left_candle[-k-1]


left = -1
right = 0
for i in range(k):
    if abs(left) == len(left_candle):
        right = k - (abs(left) - 1)
        break
    if right == len(right_candle) - 1:
        left = right - k  - 1
        break
    elif abs(left_candle[left] - left_candle[left-1]) < right_candle[right+1] - right_candle[right]:
        left -= 1
    else:
        right += 1


round_trip = min(abs(left_candle[left]), right_candle[right]) * 2 + max(abs(left_candle[left]), right_candle[right])
print(min(left_only, right_only, round_trip))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = [int(i) for i in input().split()]
min = 10**9
tmp = 10**9
for i in range(n-k+1):
    if abs(x[i] - x[i+k-1]) < min:
        min = abs(x[i] - x[i+k-1])
        tmp = i
if abs(x[tmp]) <= abs(x[tmp+k-1]):
    print(abs(x[tmp]) + min)
else:
    print(abs(x[tmp+k-1]) + min)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = [int(i) for i in input().split()]
min = 10**9
tmp = 10**9
for i in range(n-k+1):
    if abs(x[i] - x[i+k-1]) < min:
        min = abs(x[i] - x[i+k-1])
        tmp = i
if abs(x[tmp]) < abs(x[tmp+k-1]):
    print(abs(x[tmp]) + min)
else:
    print(abs(x[tmp+k-1]) + min)

Submission status: Wrong Answer

Solution code:
# 2019/08/10

from bisect import bisect_left

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

idx=bisect_left(x,0)
rn=n-idx
l=idx if rn>=k else idx-(k-rn)
r=l+k-1
ans=float('inf')

if idx==0:
    print(x[k-1])
    exit()
elif idx==n-1:
    print(x[-k])
    exit()

while 0<=l and idx<=r:
    lf=abs(x[l])*2+x[r] 
    rf=x[r]*2+abs(x[l])
    ans=min(min(lf,rf),ans)

    l-=1;r-=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
from bisect import bisect_left

def main():
	n, k = map(int, input().strip().split())
	x = list(map(int, input().strip().split()))
	zero = bisect_left(x, 0)
	# print("zero:{}".format(zero))
	ans = 10**12
	for l in range(n):
		r = k-l-1
		left_i = zero-l
		right_i = zero+r
		if left_i >= 0 and right_i < n:
			if x[left_i] >= 0:
				ans = min(ans, x[right_i])
			elif x[right_i] <= 0:
				ans = min(ans, abs(x[left_i]))
			else:
				ans = min(ans, min(2*abs(x[left_i])+x[right_i], 2*x[right_i]+abs(x[left_i])))
		# print("l:{}, r:{}, ans:{}".format(left_i, right_i, ans))
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from operator import itemgetter
from bisect import bisect_left

def main():
	n, k = map(int, input().strip().split())
	x = list(map(int, input().strip().split()))
	zero = bisect_left(x, 0)
	# print("zero:{}".format(zero))
	ans = 10**12
	for l in range(k):
		r = k-l-1
		left_i = zero-l
		right_i = zero+r
		if left_i >= 0 and right_i < n:
			if x[left_i] >= 0:
				ans = min(ans, x[right_i])
			elif x[right_i] <= 0:
				ans = min(ans, abs(x[left_i]))
			else:
				ans = min(ans, min(2*abs(x[left_i])+x[right_i], 2*x[right_i]+abs(x[left_i])))
		# print("l:{}, r:{}, ans:{}".format(left_i, right_i, ans))
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

minus = []
plus = []
for x in xarr:
    if x < 0:
        minus.append(x)
    else:
        plus.append(x)

ans = 0
if len(minus) == 0:
    ans = plus[k-1]
elif len(plus) == 0:
    ans = abs(minus[-k])
else:
    acm = [0]
    acp = [0]
    ans = 1000000000000000000000
    for i in range(len(minus)):
        acm.append(abs(minus[-i-1]))
    for i in range(len(plus)):
        acp.append(plus[i])

    for i in range(len(minus)+1):
        temp1 = 0
        temp2 = 0
        if i > k:
            break
        lest = k-i
        if lest > len(plus)-1:
            continue
        temp1 = acm[i]*2+acp[lest]
        temp2 = acm[i]+acp[lest]*2
        t = min(temp1,temp2)
        # print(i,lest,temp1,temp2)
        if t < ans:
            ans= t
    # if k <= len(plus):
    #     t = plus[k-1]
    #     if t < ans:
    #         ans = t

print(ans)

Submission status: Wrong Answer

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

minus = []
plus = []
for x in xarr:
    if x < 0:
        minus.append(x)
    else:
        plus.append(x)

ans = 0
if len(minus) == 0:
    ans = plus[k-1]
elif len(plus) == 0:
    ans = abs(minus[-k])
else:
    acm = [0]
    acp = [0]
    ans = 1000000000000000000000
    for i in range(len(minus)):
        acm.append(abs(minus[-i-1]))
    for i in range(len(plus)):
        acp.append(plus[i])

    for i in range(len(minus)):
        temp1 = 0
        temp2 = 0
        if i > k:
            break
        lest = k-i
        if lest > len(plus)-1:
            continue
        temp1 = acm[i]*2+acp[lest]
        temp2 = acm[i]+acp[lest]*2
        t = min(temp1,temp2)
        # print(i,lest,temp1,temp2)
        if t < ans:
            ans= t
    if k <= len(plus):
        t = plus[k-1]
        if t < ans:
            ans = t

print(ans)

Submission status: Wrong Answer

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

minus = []
plus = []
for x in xarr:
    if x < 0:
        minus.append(x)
    else:
        plus.append(x)

ans = 0
if len(minus) == 0:
    ans = plus[k-1]
elif len(plus) == 0:
    ans = abs(minus[-k])
else:
    acm = [0]
    acp = [0]
    temp = 0
    ans = 1000000000000000000000
    for i in range(len(minus)):
        acm.append(abs(minus[-i-1]))
    temp = 0
    for i in range(len(plus)):
        acp.append(plus[i])

    for i in range(len(minus)):
        temp1 = 0
        temp2 = 0
        if i > k:
            break
        lest = k-i
        if lest > len(plus)-1:
            continue
        temp1 = acm[i]*2+acp[lest]
        temp2 = acm[i]+acp[lest]*2
        t = min(temp1,temp2)
        # print(i,lest,temp1,temp2)
        if t < ans:
            ans= t

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
mini=100000000000000
for i in range(N-K):
    if A[i]<0 and A[i+K-1]>=0:
        if abs(A[i])>abs(A[i+K-1]):
            if mini>abs(A[i+K-1])*2+abs(A[i]):
                mini=abs(A[i+K-1])*2+abs(A[i])
        else:
            if mini > abs(A[i]) * 2 + abs(A[i+K-1]):
                mini = abs(A[i]) * 2 + abs(A[i+K-1])
    else:
       if mini>max(abs(A[i]),abs(A[i+K-1])):
           mini=max(abs(A[i]),abs(A[i+K-1]))
print(mini)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
x=0
S=N-K
N-=1
count=0

if N+1==K:
    if A[x] < 0 and A[N] >= 0:
        if abs(A[x]) >= abs(A[N]):
            print(abs(A[N]) * 2 + abs(A[x]))
        else:
            print(abs(A[x]) * 2 + abs(A[N]))
    else:
        print(max(abs(A[x]), abs(A[N])))
    exit(0)


while flag==0:
    if abs(A[x])==abs(A[N]):
        if abs(A[x])-abs(A[x+1])>=abs(A[N])-abs(A[N-1]):
            x+=1
            count+=1
        else:
            N-=1
            count+=1
    elif abs(A[x])>=abs(A[N]):
        x+=1
        count+=1
    else:
        N-=1
        count+=1
    if count==S:
        flag=1

if A[x]<0 and A[N]>=0:
    if abs(A[x])>=abs(A[N]):
        print(abs(A[N])*2+abs(A[x]))
    else:
        print(abs(A[x])*2+abs(A[N]))
else:
    print(max(abs(A[x]),abs(A[N])))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
flag=0
x=0
S=N-K
N-=1
count=0

if N+1==K:
    if A[x] < 0 and A[N] >= 0:
        if abs(A[x]) >= abs(A[N]):
            print(abs(A[N]) * 2 + abs(A[x]))
        else:
            print(abs(A[x]) * 2 + abs(A[N]))
    else:
        print(max(abs(A[x]), abs(A[N])))
    exit(0)


while flag==0:
    if abs(A[x])>=abs(A[N]):
        x+=1
        count+=1
    else:
        N-=1
        count+=1
    if count==S:
        flag=1

if A[x]<0 and A[N]>=0:
    if abs(A[x])>=abs(A[N]):
        print(abs(A[N])*2+abs(A[x]))
    else:
        print(abs(A[x])*2+abs(A[N]))
else:
    print(max(abs(A[x]),abs(A[N])))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
ans = 10 ** 10
for i in range(N-K+1):
	left = lst[i]
	right = lst[i+K-1]
	if (left > right):
		dis = abs(left - right) + abs(right)
	else:
		dis = abs(left - right) + abs(left)
	if (dis < ans):
		ans = dis    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
ans = 10 ** 10
for i in range(N-K+1):
	k_lst = lst[i:i+K]
	left = k_lst[0]
	right = k_lst[-1]
	if (left > right):
		dis = abs(left - right) + abs(right)
	else:
		dis = abs(left - right) + abs(left)
	if (dis < ans):
		ans = dis    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
ans = 2 * 10 ** 5
for i in range(N-K+1):
	k_lst = lst[i:i+K]
	left = min(k_lst)
	right = max(k_lst)
	if (left > right):
		dis = abs(right - left) + abs(right)
	else:
		dis = abs(right - left) + abs(left)
	if (dis < ans):
		ans = dis    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
ans = 2 * 10 ** 5
for i in range(N-K+1):
	k_lst = lst[i:i+K]
	left = min(k_lst)
	right = max(k_lst)
	if (left > right):
		dis = abs(left - right) + abs(right)
	else:
		dis = abs(left - right) + abs(left)
	if (dis < ans):
		ans = dis    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
lst = list(map(int, input().split()))
ans = 2 * 10 ** 5
for i in range(N-K+1):
	k_lst = lst[i:i+K]
	if (min(k_lst) > max(k_lst)):
		dis = abs(min(k_lst) - max(k_lst)) + abs(max(k_lst))
	else:
		dis = abs(max(k_lst) - min(k_lst)) + abs(min(k_lst))
	if (dis < ans):
		ans = dis    
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
N, K = map(int, input().split())
lst = list(map(abs,list(map(int, input().split()))))
dct = collections.Counter(lst)
cnt = 0
prev = 0
ans = 0
for k, v in sorted(dct.items(), key=lambda x: x[0]):
  cnt += v
  if (cnt >= K):
    if (prev != 0): 
      ans = k + 2*prev
    else:
      ans = k
    break
  elif (v == 2):
    prev = k
print(ans)

Submission status: Wrong Answer

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

cnt = 0
ans = 0
right = 0
left = 0
if (min(lst) > 0):
	for i in range(max(lst) + 1):
		if (i in lst):
			cnt += 1
		if (cnt == K):
			ans = i
			break
else:
	l = max(lst) if (max(lst) > min(lst)) else min(lst)
	for i in range(l + 1):
		if (i in lst):
			cnt += 1
			right = i
			if (cnt == K):
				ans = 2 * left + right
				break
			ans += i
		if (-i in lst):
			cnt += 1
			left = i
			if (cnt == K):
				ans = 2 * right + left
				break
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N, K = (int(_) for _ in input().split())
    x = list(int(_) for _ in input().split())
    ans = INF
    for i in range(N-K+1):
        l = i
        r = i + K-1
        ans = min(abs(x[i]) + abs(x[l]-x[r]),ans)
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N, K = (int(_) for _ in input().split())
    x = list(int(_) for _ in input().split())
    sub_x = []

    for i in range(N-1):
        sub_x.append(x[i+1]-x[i])
    
    ans = abs(x[0])
    tmp = 0
    for i in range(K-1):
        tmp += sub_x[i]
        
    ans += tmp
    j = 1
    for i in range(K-1,N-1):        
        tmp = tmp - sub_x[i-(K-1)]
        tmp = tmp + sub_x[i]
        ans = min(ans,tmp+abs(x[j]))
        j+=1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n, k = getints()
xs = list(sorted(getints()))

lower = list(filter(lambda x: x < 0, xs))
upper = list(filter(lambda x: x > 0, xs))

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

lower = list(reversed(lower))

res = 1 << 28
for i in range(-1, len(lower)):
    need = k - (i + 1)
    if need - 1 >= len(upper):
        continue
    side1 = abs(0 if i < 0 else lower[i])
    side2 = 0 if need - 1 < 0 else upper[need - 1]
    res = min(res, side1 + side2 + min(side1, side2))

print(res)

Submission status: Wrong Answer

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

for i in range(N-K+1):
    minNum = candles[i]
    maxNum = candles[i+K-1]

Submission status: Wrong Answer

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

for i in range(K):
    minNum = candles[i]