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:
import sys
N,K=map(int,input().split())
a=list(map(int,input().split()))

minus=[]
for i in range (N):
    if a[i]<0:
        minus.append(a[i])
plus=a[len(minus):]

if len(minus)==0:
    print(plus[K-1])
    sys.exit()
if len(plus)==0:
    minus=minus[::-1]
    print(minus[K-1])
    sys.exit()

data=[i * 2 for i in minus]+plus
data2=minus+[i * 2 for i in plus]
data_min=10**18
for i in range (N-K+1):
    if data_min >(abs(data[i])+abs(data[i+K-1])):
        data_min=abs(data[i])+abs(data[i+K-1])
for i in range (N-K+1):
    if data_min >(abs(data2[i])+abs(data2[i+K-1])):
        data_min=abs(data2[i])+abs(data2[i+K-1])
print(data_min)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
n, k = map(int, input().split())
a = list(map(int, input().split()))
t = bisect_left(a, 0, 0, len(a))
if t + k <= n:
    ans = a[t + k - 1]
    for i in range(min(k, t)):
        ans = min(ans, a[t+k-2-i]-a[t-i-1]*2, 2*a[t+k-2-i]-a[t-i-1])
else:
    ans = float("inf")
    for i in range(min(n - k + 1, k)):
        ans = min(ans, a[n-1-i]-a[n-i-k]*2, 2*a[n-1-i]-a[n-i-k])
print(ans)

Submission status: Wrong Answer

Solution code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000000000

if N == K:
    left = problem[0]
    right = problem[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left)
else:
    for i in range(N - K):
        left = problem[i]
        right = problem[i+K-1]
        if left * right >= 0:
            if abs(right) > abs(left):
                if ans > abs(right):
                    ans = abs(right)
            else:
                if ans > abs(left):
                    ans = abs(left)
        else:
            if abs(right) > abs(left):
                if ans > (2 * abs(left) + abs(right)):
                    ans = (2 * abs(left) + abs(right))
            else:
                if ans > 2*abs(right)+abs(left):
                    ans = 2*abs(right)+abs(left)

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()]

ans=10**5+1
for i in range(len(x)-k+1):
    if x[i]<0 and x[i+k-1] <0:
        temp=-x[i]
        if ans > temp:
            ans=temp
    elif x[i]>0 and x[i+k-1] >0:
        temp=x[i+k-1]
        if ans > temp:
            ans=temp
    else:
        temp=-x[i]+x[i+k-1]
        if -x[i] <= x[i+k-1]:
            temp+=-x[i]
        else:
            temp+=x[i+k-1]
        if ans > temp:
            ans=temp
print(ans)

Submission status: Wrong Answer

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


mini = abs(x[0]) + abs(x[-1])
maxPoint = 0

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

elif x[-1] <= 0:
    print(abs(x[-K]))

else:
    x = list(map(abs, x))
    for i in range(N - K + 1):
        if abs(x[i]) + abs(x[i + K - 1]) < mini:
            mini = abs(x[i]) + abs(x[i + K - 1])
            maxPoint = i
    print(2 * min(abs(x[maxPoint]), abs(x[maxPoint + K - 1])
                  ) + max(abs(x[maxPoint]), abs(x[maxPoint + K - 1])))

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(x[len(x)-k])
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            elif i == r and r-k >= 0:
                ans = abs(x[r-k])
            else:
                ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
minsa = max(x[N-1]-x[0],x[N-1],-x[0])
fu = 0
sei = 0
for i in range(N-K+1):
    if x[i] <= 0 and x[i+K-1] <= 0:
        sa = -x[i]
        if minsa > sa:
            minsa = sa
            fu = -x[i]
            sei = 0
    if x[i] < 0 and x[i+K-1] > 0:
        sa = x[i + K-1] - x[i]
        if minsa > sa:
            minsa = sa
            fu = -x[i]
            sei = x[i+K-1]
    if x[i] >= 0 and x[i+K-1] >= 0:
        sa = x[i+K-1]
        if minsa > sa:
            minsa = sa
            fu = 0
            sei = x[i+K-1]
#print(minsa)
#print(fu)
#print(sei)
print(min(fu,sei) * 2 + max(fu,sei))

Submission status: Wrong Answer

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

lst = []
for i in range(N-K+1):
    lst.append(abs(x[i]))
    for j in range(1, K):
        lst[i] += x[i+j] - x[i+j-1]
print(min(lst))

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(x[len(x)-k])
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = abs(x[i])
            if i == r and r-k >= 0:
                ans = abs(x[r-k])
            else:
                ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

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

if 0 in x:
    x.remove(0)
    k -= 1
    
x.append(0)
tm = sorted(x)

if k==0:
    print(0)
else:
    MIN = 2*abs(x[0]+x[-1])

    for i in range(n-k+1):
    #     xtmp = x[i:i+k]
    #     print(i, xtmp)
    #     print(abs(xtmp[0])+2*abs(xtmp[-1]))
    #     print(2*abs(xtmp[0])+abs(xtmp[-1]))
        tmtmp = tm[i:i+k+1]
#         print(i, tmtmp)
        if tmtmp[0] != 0 and tmtmp[-1]!=0:
    #         print(abs(tmtmp[0])+2*abs(tmtmp[-1]))
    #         print(2*abs(tmtmp[0])+abs(tmtmp[-1]))
            MIN = min(MIN, abs(tmtmp[0])+2*abs(tmtmp[-1]))
            MIN = min(MIN, 2*abs(tmtmp[0])+abs(tmtmp[-1]))
        elif tmtmp[0]==0:
            MIN = min(MIN, abs(tmtmp[-1]))
        else:
            MIN = min(MIN, abs(tmtmp[0]))        

    print(MIN)

Submission status: Wrong Answer

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


mini = abs(x[0]) + abs(x[-1])
maxPoint = 0

if x[0] > 0:
    print(x[K - 1])

elif x[-1] < 0:
    print(abs(x[-K]))

else:
    x = list(map(abs, x))
    for i in range(N - K + 1):
        if abs(x[i]) + abs(x[i + K - 1]) < mini:
            mini = abs(x[i]) + abs(x[i + K - 1])
            maxPoint = i
    print(2 * min(abs(x[maxPoint]), abs(x[maxPoint + K - 1])
                  ) + max(abs(x[maxPoint]), abs(x[maxPoint + K - 1])))

Submission status: Wrong Answer

Solution code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000000

if N == K:
    left = problem[0]
    right = problem[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left)
else:
    for i in range(N - K):
        left = problem[i]
        right = problem[i+K-1]
        if left * right >= 0:
            if abs(right) > abs(left):
                if ans > abs(right):
                    ans = abs(right)
            else:
                if ans > abs(left):
                    ans = abs(left)
        else:
            if abs(right) > abs(left):
                if ans > (2 * abs(left) + abs(right)):
                    ans = (2 * abs(left) + abs(right))
            else:
                if ans > 2*abs(right)+abs(left):
                    ans = 2*abs(right)+abs(left)

print(ans)

Submission status: Wrong Answer

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

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

# 絶対値でソート
x.sort(key=abs)


pos_max = 0
neg_max = 0
for i in range(K):
    target = x[i]
    if target > 0:
        if pos_max < target:
            pos_max = target

    if target < 0:
        if neg_max < abs(target):
            neg_max = abs(target)

if pos_max > neg_max: 
    print(pos_max + 2 * neg_max)
else:
    print(neg_max + 2 * pos_max)

Submission status: Wrong Answer

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


mini = abs(x[0]) + abs(x[-1])
maxPoint = 0

if x[0] > 0:
    print(x[K - 1])

elif x[-1] < 0:
    print(abs(x[-K]))

else:
    x = list(map(abs, x))
    for i in range(N - K + 1):
        if abs(x[i]) + abs(x[i + K - 1]) < mini:
            mini = abs(x[i]) + abs(x[i + K - 1])
            maxPoint = i
    print(maxPoint)
    print(2 * min(abs(x[maxPoint]), abs(x[maxPoint + K - 1])
                  ) + max(abs(x[maxPoint]), abs(x[maxPoint + K - 1])))

Submission status: Wrong Answer

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

if a[N-1]<0:
    a=list(reversed(list(map(abs,a))))
if N == K:
    print(a[0]+a[N-1]+min(abs(a[0]),abs(a[N-1])))
else:
    ans=10000000000
    for i in range(N-K):
        ans = min(ans,a[K+i-1]-a[i]+min(abs(a[i]),abs(a[K+i-1])))
    print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
cs   = list(map(int, input().split()))
t    = -1
i    = 0

while i + k <= len(cs) and cs[i] < 0:
    x = abs(cs[i + k - 1] - cs[i]) + abs(cs[i])

    if t == -1 or x < t:
        t = x

    i += 1

if t == -1 or i + k <= len(cs) and cs[i + k - 1] < t:
    t = cs[i + k - 1]

print(t)

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w
    #print(x)

    r = bisect.bisect_left(x,0)
    
    if x[-1]<0:
        print(x[len(x)-k])
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            if i-r+1 >= k:
                ans = x[i]
            else:
                ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

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

time = 10 ** 20
# 0以上の最小の整数
base = 0

for i in range(N):
    if x[i] >= 0:
        base = x[i]
        break

for i in range(N - K + 1):
    if x[i+K-1] < 0:
        time = min(abs(x[i+K-1]) * 2, time)
    elif x[i] < 0 and  x[i+K-1] >= 0:
        time = min(min(abs(x[i]), x[i+K-1]) * 2 + max(abs(x[i]), x[i+K-1]), time)
    else:
        time = min(x[i] * 2, time)

print(time)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
cs   = list(map(int, input().split()))
t    = -1
i    = 0

while i + k <= len(cs) and cs[i] < 0:
    x = abs(cs[i + k - 1]) + abs(cs[i]) * 2

    if t == -1 or x < t:
        t = x

    i += 1

if t == -1 or i + k <= len(cs) and cs[i + k - 1] < t:
    t = cs[i + k - 1]

print(t)

Submission status: Wrong Answer

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

from bisect import bisect_right

n, k = map(int, input().split())
X = list(map(int, input().split()))
zero = bisect_right(X, 0)
left = X[:zero]
left = [-i for i in left]
left.sort()
right = X[zero:]

if len(left) == 0:
    print(right[k-1])
    exit()
elif len(right) == 0:
    print(left[k-1])
    exit()

left_count, right_count = 0, 0
left_position, right_position = -1, -1
for i in range(len(left) + len(right)):
    if left_position+1 < len(left) and right_position+1 < len(right):
        if left[left_position+1] < right[right_position+1]:
            left_position += 1
            left_count += 1
            if left_count + right_count < k:
                continue
            print(2*right[right_position]+left[left_position])
            exit()
        elif left[left_position+1] > right[right_position+1]:
            right_position += 1
            right_count += 1
            if left_count + right_count < k:
                continue
            print(2*left[left_position]+right[right_position])
            exit()
        else:
            if left_count + right_count + 2 < k:
                left_position += 1
                left_count += 1
                right_position += 1
                right_count += 1
                continue
            else:
                if left_count < right_count:
                    right_position += 1
                    print(2*left[left_position]+right[right_position])
                    exit()
                else:
                    left_position += 1
                    print(2*right[right_position]+left[left_position])
                    exit()
    elif left_position+1 >= len(left):
        right_position += 1
        right_count += 1
        if left_count + right_count < k:
            continue
        print(2*left[left_position]+right[right_position])
        exit()
    elif right_position+1 >= len(right):
        left_position += 1
        left_count += 1
        if left_count + right_count < k:
            continue
        print(2*right[right_position]+left[left_position])
        exit()

Submission status: Wrong Answer

Solution code:

n, k = [ int(i) for i in input().split()]

x_list = [ int(i) for i in input().split()]

x_plus = sorted([x for x in x_list if x > 0 ])
x_minus = sorted([x for x in x_list if x < 0 ], reverse=True)
zero_x = [x for x in x_list if x == 0 ]

if len(x_plus) == 0 and len(x_minus) == 0:
    print(1)
elif len(x_plus) == 0:
    print(abs(x_minus[k-1-len(zero_x)]))
elif len(x_minus) == 0:
    print(abs(x_plus[k-1-len(zero_x)]))
else:
    max_x = max(x_plus)
    min_x = min(x_minus)

    cost_list = []
    for i in range(len(x_plus)):
        cost = 0
        cnt = i+1 + len(zero_x)
        if len(x_minus)+cnt < k or cnt > k:
            continue
        if cnt == k:
            cost += abs(x_plus[i])
        else:
            cost+=abs(x_minus[k-cnt-1])*2
            cost+=abs(x_plus[i])
        cost_list.append(cost)

    for i in range(len(x_minus)):
        cost = 0
        cnt = i+1 + len(zero_x)
        if len(x_plus)+cnt < k or cnt > k:
            continue
        if cnt == k:
            cost += abs(x_minus[i])
        else:
            cost+=abs(x_plus[k-cnt-1])*2
            cost+=abs(x_minus[i])
        cost_list.append(cost)


    print(min(cost_list))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
X=list(map(int,input().split()))
hidari=0
migi=N-1
for i in range(N-K):
  if abs(X[hidari])>=abs(X[migi]):
    hidari=hidari+1
  else:
    migi=migi-1

if X[hidari]<=0 and X[migi]>=0:
  if abs(X[hidari])>=abs(X[migi]):
    print(2*abs(X[migi])+abs(X[hidari]))
  else:
    print(2*abs(X[hidari])+abs(X[migi]))
else:
  if abs(X[hidari])>=abs(X[migi]):
    print(abs(X[hidari]))
  else:
    print(abs(X[migi]))

Submission status: Wrong Answer

Solution code:
inputs = input().strip().split()

N = int(inputs[0])
K = int(inputs[1])

inputs = input().strip().split()
problem = list(map(lambda x: int(x), inputs))

ans = 10000000000000

if N == K:
    left = problem[0]
    right = problem[K - 1]
    if left * right >= 0:
        if abs(right) > abs(left):
            if ans > abs(right):
                ans = abs(right)
        else:
            if ans > abs(left):
                ans = abs(left)
    else:
        if abs(right) > abs(left):
            if ans > (2 * abs(left) + abs(right)):
                ans = (2 * abs(left) + abs(right))
        else:
            if ans > 2*abs(right)+abs(left):
                ans = 2*abs(right)+abs(left)
else:
    for i in range(N - K):
        left = problem[i]
        right = problem[i+K-1]
        if left * right >= 0:
            if abs(right) > abs(left):
                if ans > abs(right):
                    ans = abs(right)
            else:
                if ans > abs(left):
                    ans = abs(left)
        else:
            if abs(right) > abs(left):
                if ans > (2 * abs(left) + abs(right)):
                    ans = (2 * abs(left) + abs(right))
            else:
                if ans > 2*abs(right)+abs(left):
                    ans = 2*abs(right)+abs(left)

print(ans)

Submission status: Wrong Answer

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

d = []
for n in range(N-K+1):
  d.append(abs(cans[n]) + abs(cans[n + (K-1)] - cans[n]))

print(min(d))

Submission status: Wrong Answer

Solution code:
input_s = str(input())
[n, k] = list(map(int, input_s.split(' ')))
input_s = str(input())
l_i = list(map(int, input_s.split(' ')))
l_i = sorted(l_i, key=abs)
ta = l_i[:k]
tb = [i for i in ta if i < 0]
tc = [i for i in ta if i >= 0]

result = 0
if len(tb) == 0:
    result = max(tc)
elif len(tc) == 0:
    result = -(min(tb))
elif max(tc) >= -(min(tb)) and max(tc) == 0:
    result = -(min(tb))
elif max(tc) >= -(min(tb)) and max(tc) != 0:
    result = max(tc) + -(min(tb)) * 2
else:
    result = max(tc) * 2 + -(min(tb))

print(result)

Submission status: Wrong Answer

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

min_result = max(abs(x[0]), abs(x[-1]), abs(x[-1] - x[0]))

for i in range(0, len(x)-k+1):
    if x[i+k-1] <= 0:
        min_result = min(min_result, -x[i])
    elif x[i] < 0 and x[i+k-1] > 0:
        min_result = min(min_result, -x[i]*2+x[i+k-1], -x[i]+x[i+k-1]*2)
    else:
        min_result = min(min_result, x[i+k-1])

print(min_result)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
minsa = max(x[N-1]-x[0],x[N-1],-x[0])
fu = 0
sei = 0
for i in range(N-K):
    if x[i] <= 0 and x[i+K-1] <= 0:
        sa = -x[i]
        if minsa > sa:
            minsa = sa
            fu = -x[i]
            sei = 0
    if x[i] < 0 and x[i+K-1] > 0:
        sa = x[i + K-1] - x[i]
        if minsa > sa:
            minsa = sa
            fu = -x[i]
            sei = x[i+K-1]
    if x[i] >= 0 and x[i+K-1] >= 0:
        sa = x[i+K-1]
        if minsa > sa:
            minsa = sa
            fu = 0
            sei = x[i+K-1]
#print(minsa)
#print(fu)
#print(sei)
print(min(fu,sei) * 2 + max(fu,sei))

Submission status: Wrong Answer

Solution code:
def run(n, k, x):
    ri = 0
    li = k-1
    if len(x) == 1:
        return x[0]
    x2 = x
    x = list(map(abs, x))
    for i in range(k, len(x)):
        if x[ri] > x[i]:
            ri = i-k+1
            li = i
        elif x[ri] == x[i]:
            if x[ri+1] < x[i-1]:
                ri = i-k+1
                li = i
        else:
            if x2[li] <= 0:
                return x[ri]
            elif x2[ri] >= 0:
                return x[li]
            elif x[ri] > x[li]:
                return x[ri] + 2*x[li]
            else:
                return 2*x[ri] + x[li]
    if x2[li] <= 0:
        return x[ri]
    elif x2[ri] >= 0:
        return x[li]
    elif x[ri] > x[li]:
        return x[ri] + 2*x[li]
    else:
        return 2*x[ri] + x[li]


def read_line():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    return (n, k, x)


def main():
    n, k, x = read_line()
    print(run(n, k, x))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
min_T = float('inf')
for i in range(N-K+1):
    min_A = A[i]
    max_A = A[i+K-1]
    if min_A * max_A>= 0:
        if min_A >= 0:
            min_A = 0
            max_A = A[K-1]
            min_T = min(min_T, max_A - min_A)
        else:
            min_A = A[N-K+1]
            max_A = 0
            min_T = min(min_T, max_A - min_A)
    else:
        min_A = A[i]
        max_A = A[i+K-1]
        min_T = min(min_T, min(-1 * min_A * 2 + max_A, -1 * min_A + 2 * max_A))
        #print(min_T)
print(min_T)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
cs   = list(map(int, input().split()))
t    = -1
i    = 0

while i + k <= len(cs) and cs[i] < 0:
    x = cs[i + k - 1] - cs[i] * 2

    if t == -1 or x < t:
        t = x

    i += 1

if t == -1 or i + k <= len(cs) and cs[i + k - 1] < t:
    t = cs[i + k - 1]

print(t)

Submission status: Wrong Answer

Solution code:
import sys

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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


if __name__ == "__main__":
    n, k = LI()
    X = LI()
    l_count = sum((x <= 0 for x in X))
    r_count = n - l_count

    if l_count == 0:
        print(X[k-1])
    elif r_count == 0:
        print(X[-k+1])
    else:
        times = []
        for i in range(l_count):
            try:
                left = X[l_count - i - 1]
                right = X[l_count + k - i - 2]
                times.append(abs(2 * left) + abs(right))
                times.append(abs(left) + abs(2 * right))
            except:
                continue
        print(min(times))

Submission status: Wrong Answer

Solution code:
n,k = map(int, raw_input().split())
lis = map(int, raw_input().split())
if 0 in lis:
    k-=1
flag=0
#print lis
if k==0:
    print 0
    flag=1
if n==1 and flag==0:
    print abs(lis[0])
    flag=1
if flag==0:
    if lis[0]>0: # all positives
        print lis[k-1]
    elif lis[n-1]<0: #all negatives
        print abs(lis[n-k])
    else:
        idx = -1
        for i in range(0, n-1):
            if lis[i]<0 and lis[i+1]>=0:
                idx = i
                break
        #print idx
        lol =[]
        x=0
        lol.append(abs(lis[idx]))
        for i in range(idx-1,-1,-1):
            lol.append(lol[x]+abs(lis[i]))
            x+=1
        #print lol
        lol1=[]
        x=0
        lol1.append(lis[idx+1])
        for i in range(idx+2,n,1):
            lol1.append(lol1[x]+(lis[i]))
            x+=1
        #print lol1

        lol=lol[::-1]
        lol3=[]
        for i in lol:
            lol3.append(i)
        for i in lol1:
            lol3.append(i)
        #print lol3
        
        ans=10000000000000000000000000000000000000000000
        lol=lol[::-1]


        lol =[]
        x=0
        lol.append(abs(lis[idx]))
        for i in range(idx-1,-1,-1):
            lol.append(abs(lis[i]))
            x+=1
        #print lol
        lol1=[]
        x=0
        lol1.append(lis[idx+1])
        for i in range(idx+2,n,1):
            lol1.append((lis[i]))
            x+=1
        #print lol1
        #print lol
        if k<=len(lol):
            ans=min(ans, lol[k-1])
        if k<=len(lol1):
            ans=min(ans, lol1[k-1])
        for i in range(1,len(lol)):
            fromright=k-i
            if fromright>len(lol1):
                continue
            val=2*lol[i-1]
            val+=(lol1[fromright-1])
            #print "val=",val
            if val<ans:
                ans=val
        
        for i in range(1,len(lol1)):
            fromleft=k-i
            if fromleft>len(lol):
                continue
            val=2*lol1[i-1]
            val+=(lol[fromleft-1])
            #print "val=",val
            if val<ans:
                ans=val
        print ans

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):
    if a[i] < 0 and a[i+k-1] <= 0:
        ans = min(ans,abs(a[i]))
    elif a[i] <= 0 and 0 <= a[i+k-1]:
        ans = min(ans,2*abs(a[i])+a[i+k-1],abs(a[i])+2*a[i+k-1])
    elif 0 <= a[i]:
        ans = min(ans,a[i+k-1])
if ans == float("inf"):
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N,K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
ans1 = sys.maxsize
ans2 = sys.maxsize
ans3 = sys.maxsize
plus = []
minus = []
for i in X:
    if i <= 0:
        minus.append(i)
    if i >= 0:
        plus.append(i)
if len(minus) >= K:
    ans1 = minus[K-1]
if len(plus) >= K:
    ans2 = plus[K-1]

X = [(abs(i),i) for i in X]
Y = sorted(X)[:K]
Z = sorted(Y,key=lambda x:x[1])
minus = abs(Z[0][1])
plus = abs(Z[-1][1])
ans3 = min(minus,plus)*2+max(minus,plus)
print(min(ans1,ans2,ans3))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
 
k = []
# print(X)
 
k0 = abs(X[0])
for n in range(0+1, K):
    k0 += X[n] - X[n-1]
# print(k0)
k.append(k0)
for i in range(1, len(X)-K+1):
    # print(i, i-1, i+K, i+K-1)
    ki = k[i-1] - (X[i] - X[i-1]) + (X[i+K-1] - X[i+K-2]) - abs(X[i-1]) + abs(X[i])
    k.append(ki)
print(min(k))

Submission status: Wrong Answer

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

x = list(map(int,input().split()))
ans = 100000000000000


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

Submission status: Wrong Answer

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

if K == 1:
    a = list(map(abs,a))
    print(min(a))
else:
    if N == K:
        print(a[0]+a[N-1]+min(abs(a[0]),abs(a[N-1])))
    else:
        ans=10000000000
        for i in range(N-K):
            ans = min(ans,a[K+i-1]-a[i]+min(abs(a[i]),abs(a[K+i-1])))
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


if __name__ == "__main__":
    n, k = LI()
    X = LI()
    l_count = sum((x <= 0 for x in X))
    r_count = n - l_count

    if l_count == 0:
        print(X[k-1])
    elif r_count == 0:
        print(X[-k+1])
    else:
        times = []
        for i in range(l_count):
            try:
                left = X[l_count - i - 1]
                right = X[l_count + k - i - 2]
                times.append(abs(2 * left) + abs(right))
                times.append(abs(left) + abs(2 * right))
            except:
                continue
        print(times)
        print(min(times))

Submission status: Wrong Answer

Solution code:
import math
N,K = map(int,input().split())
xs = input().split()
res_list = []
for i in range(N-K+1):
    diff = int(xs[i+K-1]) - int(xs[i]) + abs(int((xs[i])))
    res_list.append(diff)
print(min(res_list))

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
min_T = float('inf')
for i in range(N-K):
    min_A = A[i]
    max_A = A[i+K-1]
    if min_A * max_A>= 0:
        if min_A >= 0:
            min_A = 0
            max_A = A[K-1]
            min_T = min(min_T, max_A - min_A)
        else:
            min_A = A[N-K+1]
            max_A = 0
            min_T = min(min_T, max_A - min_A)
    else:
        min_A = A[i]
        max_A = A[i+K-1]
        min_T = min(min_T, min(-1 * min_A * 2 + max_A, -1 * min_A + 2 * max_A))
        #print(min_T)
print(min_T)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
cs   = list(map(int, input().split()))
t    = -1
i    = 0

while i + k <= len(cs) and cs[i] < 0:
    x = cs[i + k - 1] - cs[i] * 2

    if t == -1 or x < t:
        t = x

    i += 1

if t == -1 or i + k < len(cs) and cs[i + k - 1] < t:
    t = cs[i + k - 1]

print(t)

Submission status: Wrong Answer

Solution code:
# cook your dish here
import bisect 
n,k = map(int,input().split())
x = list(map(int,input().split()))

w  = x.count(0)
for i in range(w):
    x.remove(0)

if k <= w:
    print(0)
else:
    k = k -w

    r = bisect.bisect_left(x,0)
    if x[-1]<0:
        print(x[len(x)-k])
    else:
        ass = 10 ** 10
        for i in range(r,min(len(x),r + k )):
            ans = x[i] + abs(x[i-k+1]) + min(x[i],abs(x[i-k+1]))
            ass = min(ass,ans)
        print(ass)

Submission status: Wrong Answer

Solution code:
import math
N,K = map(int,input().split())
xs = input().split()
res = 10**9
for i in range(N-K+1):
    diff = int(xs[i+K-1]) - int(xs[i]) + abs(int((xs[i])))
    if diff <= res:
        res = diff
    else:
        break
print(res)

Submission status: Wrong Answer

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

x = list(map(int,input().split()))
ans = 10000000000


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