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()) )
b = None

for i in range(n):
    if x[i] == 0:
        b = i
        start = True
        break
    elif x[i] > 0:
        b = i
        start = False
        break


if not b:
    m = abs(x[-k])

else:
    if start:
        del x[b]
        k -= 1
        n -= 1

    if k == 0:
        m = 0
    else:
        if b >= k:
            m1 = abs(x[b-k])
        else:
            m1 = None

        if b+k-1 <= n-1:
            m2 = abs(x[b+k-1])
        else:
            m2 = None

        m = 2*10**8
        for i in range(1, k):
            if b-i >= 0 and b-i+k-1 < n:
                m = min(2*abs(x[b-i])+abs(x[b-i+k-1]),abs(x[b-i])+2*abs(x[b-i+k-1]), m)

        if m1:
            m = min(m, m1)
        if m2:
            m = min(m, m2)

print(m)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect


def solve():
    n, k = map(int, input().split())
    al = [int(i) for i in input().split()]

    ans = sum(abs(i) for i in al)
    flag0 = 0 in al
    if flag0:
        k -= 1
        n -= 1
        if k == 0:
            print(0)
            return
        al.remove(0)
    nc = bisect.bisect_left(al, 0)
    for i in range(n):
        if i < 0 or i + k - 1 >= n:
            break
        if al[i] < 0 and al[i + k - 1] < 0:
            t1 = min(abs(al[i]), abs(al[i + k - 1]))
        else:
            t1 = min(abs(al[i]) * 2 + abs(al[i + k - 1]),
                     abs(al[i]) + abs(al[i + k - 1]) * 2)
        if al[i] > 0 and al[i + k - 1] > 0:
            t2 = max(abs(al[i]), abs(al[i + k - 1]))
        else:
            t2 = min(abs(al[i]) * 2 + abs(al[i + k - 1]),
                     abs(al[i]) + abs(al[i + k - 1]) * 2)
        t = min(t1, t2)
        ans = min(ans, t)
    for i in range(n - 1, -1, -1):
        if i - k + 1 < 0 or i >= n:
            break
        if al[i] < 0 and al[i - k + 1] < 0:
            t1 = min(abs(al[i - k + 1]), abs(al[i]))
        else:
            t1 = min(abs(al[i - k + 1]) * 2 + abs(al[i]),
                     abs(al[i - k + 1]) + abs(al[i]) * 2)
        if al[i] > 0 and al[i - k + 1] > 0:
            t1 = max(abs(al[i - k + 1]), abs(al[i]))
        else:
            t2 = min(abs(al[i - k + 1]) * 2 + abs(al[i]),
                     abs(al[i - k + 1]) + abs(al[i]) * 2)
        t = min(t1, t2)
        ans = min(ans, t)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 
import math
import sys
import itertools
import numpy as np

# なんこか入力
n,k = map(int, input().split())

# 空白区切りの数列をリストに代入
x = [int(i) for i in input().split()]
c = 0
for a in range(0,n):
    if x[a]==0:
        b=a
        c=1
        break
    if x[a]>0:
        b=a
        break

answer = 1000000000

if x[0] >= 0:
    answer = x[k-1]
elif x[n-1] <= 0:
    answer = -x[n-k]        
else:
    if n-b+c > k:
        answer = min(answer, x[b+k-1-c])
    if b+c > k:
        answer = min(answer, -x[b-k+c])
    for i in range(0,n):
        if i<b:
            if (b-i) < (k-c) and (i+k-1)<n:
                l=-x[i]+(x[i+k-1]-x[i])
                answer = min(answer,l)
        elif i==b:
            continue
        else:
            if (i-b) < (k-c) and i > (k-2):
                l=x[i] -(x[i]-x[i-k+1])




print(answer)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect


def solve():
    n, k = map(int, input().split())
    al = [int(i) for i in input().split()]

    ans = abs(al[-1] - al[0])
    flag0 = 0 in al
    if flag0:
        k -= 1
        n -= 1
        if k == 0:
            print(0)
            return
        al.remove(0)
    nc = bisect.bisect_left(al, 0)
    pc = n - nc
    for i in range(n):
        if i < 0 or i + k - 1 >= n:
            break
        if al[i] < 0 and al[i + k - 1] < 0:
            t1 = abs(al[i])
        else:
            t1 = abs(al[i]) * 2 + abs(al[i + k - 1])
        if al[i] > 0 and al[i + k - 1] > 0:
            t2 = abs(al[i + k - 1])
        else:
            t2 = abs(al[i]) + abs(al[i + k - 1]) * 2
        t = min(t1, t2)
        ans = min(ans, t)
    for i in range(n - 1, -1, -1):
        if i - k + 1 < 0 or i >= n:
            break
        if al[i] < 0 and al[i - k + 1] < 0:
            t1 = abs(al[i - k + 1])
        else:
            t1 = abs(al[i - k + 1]) * 2 + abs(al[i])
        if al[i] > 0 and al[i - k + 1] > 0:
            t2 = abs(al[i])
        else:
            t2 = abs(al[i - k + 1]) + abs(al[i]) * 2
        t = min(t1, t2)
        ans = min(ans, t)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect


def solve():
    n, k = map(int, input().split())
    al = [int(i) for i in input().split()]

    ans = abs(al[-1] - al[0])
    flag0 = 0 in al
    if flag0:
        k -= 1
        n -= 1
        if k == 0:
            print(0)
            return
        al.remove(0)
    print(al, n, k)
    nc = bisect.bisect_left(al, 0)
    pc = n - nc
    for i in range(n):
        if i < 0 or i + k - 1 >= n:
            break
        if al[i] < 0 and al[i + k - 1] < 0:
            t1 = abs(al[i])
        else:
            t1 = abs(al[i]) * 2 + abs(al[i + k - 1])
        if al[i] > 0 and al[i + k - 1] > 0:
            t2 = abs(al[i])
        else:
            t2 = abs(al[i]) * 2 + abs(al[i + k - 1])
        t = min(t1, t2)
        ans = min(ans, t)
    for i in range(n - 1, -1, -1):
        if i - k + 1 < 0 or i >= n:
            break
        if al[i] < 0 and al[i - k + 1] < 0:
            t1 = abs(al[i - k + 1])
        else:
            t1 = abs(al[i - k + 1]) * 2 + abs(al[i])
        if al[i] > 0 and al[i - k + 1] > 0:
            t2 = abs(al[i])
        else:
            t2 = abs(al[i]) * 2 + abs(al[i - k + 1])
        t = min(t1, t2)
        ans = min(ans, t)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
m = 0
c = list(map(int,input().split()))
c.sort()
for i in range(len(c)-b+1):
    if c[i] * c[i+b-1] < 0:
        if i == 0:
            m = abs(c[i]) + c[i+b-1] +abs(min(c[i],c[i+b-1]))
        else:
            m = min(m,abs(c[i]) + c[i+b-1] +abs(min(c[i],c[i+b-1])))
    else:
        if i == 0:
            m = max(abs(c[i]),abs(c[i+b-1]))
        else:
            m = min(m,max(abs(c[i]),abs(c[i+b-1])))

print(m)

Submission status: Wrong Answer

Solution code:

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

for i in range(n):
    if x[i] == 0:
        b = i
        start = True
        break
    elif x[i] > 0:
        b = i
        start = False
        break


if not b:
    m = abs(x[-k])

else:
    if start:
        del x[b]
        k -= 1
        n -= 1

    if k == 0:
        m = 0
    else:
        if b >= k:
            m1 = abs(x[b-k])
        else:
            m1 = None

        if n-b >= k:
            m2 = abs(x[b+k-1])
        else:
            m2 = None

        m = 2*10**8
        for i in range(1, k):
            if b-i >= 0 and b-i+k-1 < n:
                m = min(2*abs(x[b-i])+abs(x[b-i+k-1]),abs(x[b-i])+2*abs(x[b-i+k-1]), m)

        if m1:
            m = min(m, m1)
        if m2:
            m = min(m, m2)

print(m)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect


def solve():
    n, k = map(int, input().split())
    al = [int(i) for i in input().split()]

    ans = abs(al[-1] - al[0])
    flag0 = 0 in al
    if flag0:
        k -= 1
        n -= 1
        if k == 0:
            print(0)
            return
        al.remove(0)
    nc = bisect.bisect_left(al, 0)
    pc = n - nc
    if nc >= k:
        ans = min(ans, abs(al[nc - k]))
    if pc >= k:
        ans = min(ans, abs(al[nc + k - 1]))
    for i in range(n):
        if i < 0 or i + k - 1 >= n:
            break
        if i <= nc:
            ans = min(ans, abs(al[i]) * 2 + abs(al[i + k - 1]))
        else:
            break
    for i in range(n - 1, -1, -1):
        if i - k + 1 < 0 or i >= n:
            break
        if i >= nc:
            ans = min(ans, abs(al[i - k + 1]) * 2 + abs(al[i]))
        else:
            break

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect


def solve():
    n, k = map(int, input().split())
    al = [int(i) for i in input().split()]

    ans = abs(al[-1] - al[0])
    flag0 = 0 in al
    if flag0:
        k -= 1
        n -= 1
        if k == 0:
            print(0)
            return
        al.remove(0)
    nc = bisect.bisect_left(al, 0)
    pc = n - nc
    if nc >= k:
        ans = min(ans, abs(al[nc - k]))
    if pc >= k:
        ans = min(ans, abs(al[nc + k - 1]))
    for i in range(n):
        if i < 0 or i + k - 1 >= n:
            break
        if nc < i:
            ans = min(ans, abs(al[i]) * 2 + abs(al[i + k - 1]))
        else:
            break
    for i in range(n - 1, -1, -1):
        if i - k + 1 < 0 or i >= n:
            break
        if i >= nc:
            ans = min(ans, abs(al[i - k + 1]) * 2 + abs(al[i]))
        else:
            break

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import bisect


def solve():
    n, k = map(int, input().split())
    al = [int(i) for i in input().split()]

    ans = abs(al[-1] - al[0])
    flag0 = 0 in al
    if flag0:
        k -= 1
        n -= 1
        if k == 0:
            print(0)
            return
        al.remove(0)
    nc = bisect.bisect_left(al, 0)
    pc = n - nc
    if nc >= k:
        ans = min(ans, abs(al[nc - k]))
    if pc >= k:
        ans = min(ans, abs(al[nc + k - 1]))
    for i in range(n - k + 1):
        if i < 0 or i + k - 1 >= n:
            continue
        if nc < i:
            ans = min(ans, abs(al[i]) * 2 + abs(al[i + k - 1]))
        else:
            ans = min(ans, abs(al[i]) + abs(al[i + k - 1]) * 2)
    for i in range(n - 1, k - 2, -1):
        if i - k + 1 < 0 or i >= n:
            continue
        if i >= nc:
            ans = min(ans, abs(al[i - k + 1]) * 2 + abs(al[i]))
        else:
            ans = min(ans, abs(al[i - k + 1]) + abs(al[i]) * 2)

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
m = 0
c = list(map(int,input().split()))
for i in range(len(c)-b+1):
    if c[i] * c[i+b-1] < 0:
        if i == 0:
            m = abs(c[i]) + c[i+b-1] +abs(min(c[i],c[i+b-1]))
        else:
            m = min(m,abs(c[i]) + c[i+b-1] +abs(min(c[i],c[i+b-1])))
    else:
        if i == 0:
            m = max(abs(c[i]),abs(c[i+b-1]))
        else:
            m = min(m,max(abs(c[i]),abs(c[i+b-1])))

print(m)

Submission status: Wrong Answer

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

for i in range(n):
    if x[i] == 0:
        b = i
        start = True
        break
    elif x[i] > 0:
        b = i
        start = False
        break


if not b:
    m = abs(x[-k])

else:
    if start:
        del x[b]
        k -= 1
        n -= 1

    if k == 0:
        m = 0
    else:
        if b >= k:
            m1 = abs(x[b-k])
        else:
            m1 = None

        if n-b >= k:
            m2 = abs(x[b+k-1])
        else:
            m2 = None

        m = 10**8
        for i in range(1, k):
            if b-i >= 0 and b-i+k-1 < n:
                m = min(2*abs(x[b-i])+abs(x[b-i+k-1]),abs(x[b-i])+2*abs(x[b-i+k-1]), m)

        if m1:
            m = min(m, m1)
        if m2:
            m = min(m, m2)

print(m)

Submission status: Wrong Answer

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

if((0 in x) == True):
    flag = n*[1]
    flag[x.index(0)] = 0
    state = x.index(0)
    count = 1
else:
    x.append(0)
    x = sorted(x)
    flag = (n+1)*[1]
    flag[x.index(0)] = 0
    state = x.index(0)
    count = 0

ans = int(1e9)
for left in range(k-count+1):
    right = k - count - left
    if(((state - left) >= 0) and ((state + right) <= (len(flag) - 1))):
        if(right > 0): tmp = -2 * x[state-left]
        else: tmp = -1 * x[state-left]
        tmp += x[state+right]
        if(ans > tmp): ans = tmp

print(ans)

Submission status: Wrong Answer

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

if((0 in x) == True):
    flag = n*[1]
    flag[x.index(0)] = 0
    state = x.index(0)
    count = 1
else:
    x.append(0)
    x = sorted(x)
    flag = (n+1)*[1]
    flag[x.index(0)] = 0
    state = x.index(0)
    count = 0

ans = int(1e9)
for left in range(k-count+1):
    right = k - count - left
    if(((state - left) >= 0) and ((state + right) <= (len(flag) - 1))):
        tmp = -2 * x[state-left]
        tmp += x[state+right]
        if(ans > tmp): ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect

N, K = map(int, input().split())
x_np = np.array(list(map(int, input().split())))

idx0 = bisect.bisect_left(x_np, 0)
right_cost = x_np[idx0:]
right_cost = np.append(np.zeros(1), right_cost)


if idx0 > 0:
    left_cost = np.zeros(1)
    left_cost = np.append(left_cost, -x_np[idx0-1::-1])

    res = 10**6
    iter_s = max(0, K - (N-idx0))
    iter_e = min(K, idx0)

    if iter_s == 0:
        res = min(res, right_cost[K])
    if iter_e == K:
        res = min(res, left_cost[K])

    for i in range(max(1, iter_s), min(K, iter_e+1)):
        res = min(res, left_cost[i]*2 + right_cost[K-i], left_cost[i] + right_cost[K-i]*2)

    print(int(res))
else:
    print(int(right_cost[K]))

Submission status: Wrong Answer

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

for i in range(n):
    if x[i] == 0:
        b = i
        start = True
        break
    elif x[i] > 0:
        b = i
        start = False
        break


if not b:
    m = abs(x[-k])

else:
    if start:
        del x[b]
        k -= 1
        n -= 1

    if k == 0:
        m = 0
    else:
        if b >= k:
            m1 = abs(x[b-k])
        else:
            m1 = None

        if n-b >= k:
            m2 = abs(x[b+k-1])
        else:
            m2 = None

        m = max(abs(2*x[0]), abs(2*x[-1]))
        for i in range(1, k):
            if b-i >= 0 and b-i+k-1 < n:
                m = min(2*abs(x[b-i])+abs(x[b-i+k-1]),abs(x[b-i])+2*abs(x[b-i+k-1]), m)

        if m1:
            m = min(m, m1)
        if m2:
            m = min(m, m2)

print(m)

Submission status: Wrong Answer

Solution code:
import math

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

plus = [0]
minus = []
zeroExist = 0

for i in range(n):
    if x[i] > 0:
        plus.append(x[i])
    elif x[i] == 0:
        zeroExist = 1
    else:
        minus.append(-x[i])
minus.append(0)
minus = list(reversed(minus))

# for i in range(1, len(minus)):
# minus[i] += minus[i-1]

# print(plus)
# print(minus)

if zeroExist == 1:
    k -= 1

cost = 10**8
for i in range(k + 1):
    if i < len(plus) and k - i < len(minus):
        # print(i)
        if i == 0 or i == k:
            cost = min(cost, plus[i] + minus[k - i])
        else:
            cost = min(cost, plus[i] + minus[k - i]
                       + min(plus[i], minus[k - i]))
            # print("{}:{}".format(i, cost))
print(cost)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
L = [0] + [x for x in X if x < 0][::-1]
R = [0] + [x for x in X if x >= 0]


ans = float("inf")
#print(L)
if len(L) > K:
    ans = min(ans, abs(L[K]))

for i in range(len(L)):
    if K - i < 0:
        continue
    try:
        ans = min(ans, abs(L[i]) * 2 + R[K-i])
    except:
        pass
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
N, K = map( int, input().split())
X = list( map( int, input().split()))
I = bisect(X, -1)
Xmin = X[:I]
Xmin = [ -x for x in Xmin[::-1]]
Xplu = X[I:]
print(Xmin)
H = [10**9]*(10**5)
Xmin += H
Xplu += H
ans = 10**9
for i in range( min(I, K)):
    if i == K-1:
        ans = min(ans, Xmin[i])
    else:
        ans = min(ans, Xmin[i]*2 + Xplu[K-i-2])
for i in range( min(N-I, K)):
    if i == K-1:
        ans = min(ans, Xplu[i])
    else:
        ans = min(ans, Xplu[i]*2 + Xmin[K-i-2])
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
L = [0] + [x for x in X if x < 0]
L2 = [0] + [x for x in X if x < 0][::-1]
R = [0] + [x for x in X if x >= 0]


ans = float("inf")
if len(L) > K:
    ans = min(ans, abs(L[K]))

for i in range(len(L2)):
    if K - i < 0:
        continue
    try:
        ans = min(ans, abs(L2[i]) * 2 + R[K-i])
    except:
        pass
print(ans)

Submission status: Wrong Answer

Solution code:
def main(N, K, xs):

    if xs[0] >= 0:
        return xs[K-1]
    if xs[-1] <= 0:
        return abs(xs[-1 * K])

    for i, x in enumerate(xs):
        if x == 0:
            xs.pop(i)
            if K == 1:
                return 0
            K = K-1
            N = N-1
        if x > 0:
            zi = i
            break

    result = None

    if zi - K >= 0:
        result = xs[zi - K]
    if zi + K - 1 <= N-1:
        if result == None:
            result = xs[zi + K - 1]
        result = min(result, xs[zi + K - 1])

    for i in range(K-1):
        if zi - K + i + 1 < 0:
            continue
        if zi - 1 + i + 1 > N-1:
            continue
        l = abs(xs[zi-K+i+1])
        r = abs(xs[zi-1+i+1])
        if result == None:
            result = 2 * min(l, r) + max(l, r)
        else:
            if result > 2 * min(l, r) + max(l, r):
                result = 2 * min(l, r) + max(l, r)
    return result

N, K = [int(i) for i in input().split(' ')]
xs = [int(i) for i in input().split(' ')]

print(main(N, K, xs))

Submission status: Wrong Answer

Solution code:
n, k = tuple(map(int, input().split()))
arr = list(map(int, input().split()))
res = 1000000000
for i in range(n-k+1):
    if arr[i+k-1] <= 0:
        res = min(res, arr[i]*-2)
    elif arr[i] >= 0:
        res = min(res, 2*arr[i+k-1])
    else:
        res = min([res, -2*arr[i]+arr[i+k-1], 2*arr[i+k-1]-arr[i]])
print(res)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
N, K = map( int, input().split())
X = list( map( int, input().split()))
I = bisect(X, -1)
Xmin = X[:I]
Xmin = [ -x for x in Xmin[::-1]]
Xplu = X[I:]
H = [10**8]*(10**5)
Xmin += H
Xplu += H
ans = 10**9
for i in range( min(I, K)):
    if i == K-1:
        ans = min(ans, Xmin[i])
    else:
        ans = min(ans, Xmin[i]*2 + Xplu[K-i-2])
for i in range( min(N-I, K)):
    if i == K-1:
        ans = min(ans, Xplu[i])
    else:
        ans = min(ans, Xplu[i]*2 + Xmin[K-i-2])
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools as it
import collections as col
import bisect

import sys
sys.setrecursionlimit(10000)

def main():
    N, K = [int(x) for x in input().split()]
    reqs = [int(x) for x in input().split()]

    index_p = bisect.bisect_left(reqs, 0)
    index_n = index_p - 1
    for _ in range(K):
        if index_p >= N:
            index_n -= 1
        elif index_n < 0:
            index_p += 1
        elif (abs(reqs[index_p]) < abs(reqs[index_n])):
            index_p += 1
        else:
            index_n -= 1

    if reqs[index_p-1] < 0:
        return abs(reqs[index_n+1]) 

    if reqs[index_n+1] > 0:
        return abs(reqs[index_p-1])
    
    a = min(abs(reqs[index_p-1]), abs(reqs[index_n+1]))
    b = max(abs(reqs[index_p-1]), abs(reqs[index_n+1]))

    return 2*a + b
    
if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect

N, K = map(int, input().split())
x_np = np.array(list(map(int, input().split())))

idx0 = bisect.bisect_left(x_np, 0)
right_cost = x_np[idx0:]
right_cost = np.append(np.zeros(1), right_cost)


if idx0 > 0:
    left_cost = -x_np[idx0-1::-1]
    left_cost = np.append(np.zeros(1), left_cost)

    res = 10**6
    iter_s = max(0, K - (N-idx0))
    iter_e = min(K, idx0)
    for i in range(iter_s, iter_e+1):
        if i == K:
            res = min(res, left_cost[K])
        else:
            res = min(res, left_cost[i]*2 + right_cost[K-i])
    print(int(res))
else:
    print(int(right_cost[K]))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
L = [0] + [x for x in X if x < 0][::-1]
R = [0] + [x for x in X if x >= 0]

#print(L, R)
ans = float("inf")
if len(L) > K:
    ans = min(ans, abs(L[K]))
for i in range(K-len(R)+1, len(L)):
    if K - i < 0:
        continue
    try:
        ans = min(ans, abs(L[i]) * 2 + R[K-i])
    except:
        pass
print(ans)

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
x_list = list(map(int, input().split()))

zero_cnt = 0
posi_ind = N
nega_ind = -1

# minus
for i in range(N):
    if x_list[i] == 0:
        zero_cnt = 1
        nega_ind = i-1
    elif x_list[i] > 0:
        posi_ind = i
        if zero_cnt == 0:
            nega_ind = i-1
        break
if zero_cnt == 1:
    K -= 1

if x_list[N-1] < 0:
    nega_ind = N-1
    
nega_seq = list(reversed(x_list[:(nega_ind+1)]))
posi_seq = x_list[posi_ind:]

res = 10**13
if K == 0:
    res = 0
else:
    for i in range(1, min(len(posi_seq), K)):
        if K-i-1 >= len(nega_seq) or K-i-1 < 0:
            continue
        p = posi_seq[i-1]
        n = -nega_seq[K-i-1]
        r = min(2*p+n, 2*n+p)
        res = min(res, r)
    if len(posi_seq) >= K:
        r = posi_seq[K-1]
        res = min(res, r)
    if len(nega_seq) >= K:
        r = -nega_seq[K-1]
        res = min(res, r)
print(res)

Submission status: Wrong Answer

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

ans = 10 ** 10
for  i,x  in enumerate(xs):
    if N == 1:
        ans = abs(x)
    if i +  K  == N:
        break
    num = abs(xs[ i + K - 1 ] - xs[i]) + abs(xs[i])
    num1 = abs(xs[ i + K - 1 ] - xs[i]) + abs(xs[i + K -1])
    ans = min([num,num1,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()))
pos = bisect_left(x, 0)
#print(bisect_left(x, 0))

if x[0] >= 0:
	print(x[k - 1])
elif x[-1] <= 0:
	print(-x[-k])
else:
	ans = float("inf")
	if pos + k <= n:
		ans = x[pos + k - 1]
	if x[pos] == 0:
		if pos - k + 1 >= 0:
			ans = min(ans, -x[pos - k + 1])
	else:
		if pos - k >= 0:
			ans = min(ans, -x[pos - k])
	for i in range(max(pos, k - 1 - pos), min(pos + k - 1, n)):
		#print(i)
		right = x[i]
		left = -x[pos - (k - (i - pos + 1))]
		#print(right, left)
		ans = min(ans, 2 * right + left, 2 * left + right)

	print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
#from itertools import accumulate
N, K = map( int, input().split())
X = list( map( int, input().split()))
I = bisect(X, -1)
Xmin = X[:I]
Xmin = [ -x for x in Xmin[::-1]]
Xplu = X[I:]
H = [10**8]*(10**5)
Xmin += H
Xplu += H
#Xminac = accumulate(Xmin)
#Xpluac = accumulate(Xplu)
ans = X[-1]*(10**5)
for i in range( min(I, K)):
    if i == K-1:
        ans = min(ans, Xmin[i])
    else:
        ans = min(ans, Xmin[i]*2 + Xplu[K-i-2])
for i in range( min(N-I, K)):
    if i == K-1:
        ans = min(ans, Xplu[i])
    else:
        ans = min(ans, Xplu[i]*2 + Xmin[K-i-2])
print(ans)

Submission status: Wrong Answer

Solution code:
# C
N, K = map(int, input().split())
x_list = list(map(int, input().split()))

zero_cnt = 0
posi_ind = N
nega_ind = -1

# minus
for i in range(N):
    if x_list[i] == 0:
        zero_cnt = 1
        nega_ind = i-1
    elif x_list[i] > 0:
        posi_ind = i
        if zero_cnt == 0:
            nega_ind = i-1
        break
if zero_cnt == 1:
    K -= 1

    
nega_seq = list(reversed(x_list[:(nega_ind+1)]))
posi_seq = x_list[posi_ind:]

res = 10**13
if K == 0:
    res = 0
else:
    for i in range(1, min(len(posi_seq), K)):
        if K-i-1 >= len(nega_seq) or K-i-1 < 0:
            continue
        p = posi_seq[i-1]
        n = -nega_seq[K-i-1]
        r = min(2*p+n, 2*n+p)
        res = min(res, r)
    if len(posi_seq) >= K:
        r = posi_seq[K-1]
        res = min(res, r)
    if len(nega_seq) >= K:
        r = -nega_seq[K-1]
        res = min(res, r)
print(res)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
pos = bisect_left(x, 0)
#print(bisect_left(x, 0))

if x[0] >= 0:
	print(x[k - 1])
elif x[-1] <= 0:
	print(-x[-k])
else:
	ans = float("inf")
	if pos + k <= n:
		ans = x[pos + k - 1]
	if x[pos] == 0:
		if pos - k - 1 >= 0:
			ans = min(ans, -x[pos - k - 1])
	else:
		if pos - k >= 0:
			ans = min(ans, -x[pos - k])
	for i in range(max(pos, k - 1 - pos), min(pos + k, n)):
		#print(i)
		right = x[i]
		left = -x[pos - (k - (i - pos + 1))]
		#print(right, left)
		ans = min(ans, 2 * right + left, 2 * left + right)

	print(ans)

Submission status: Wrong Answer

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

ans = 10 ** 10
for  i,x  in enumerate(xs):
    if N == 1:
        ans = abs(x)
    if i +  K  == N:
        break
    if x >= 0 and i > 0:
        break
    num = abs(xs[ i + K - 1 ] - xs[i]) + abs(xs[i])
    num1 = abs(xs[ i + K - 1 ] - xs[i]) + abs(xs[i + K -1])
    ans = min([num,num1,ans])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import bisect

N, K = map(int, input().split())
x_np = np.array(list(map(int, input().split())))

idx0 = bisect.bisect_left(x_np, 0)
right_cost = x_np[idx0:]
right_cost = np.append(np.zeros(1), right_cost)


if idx0 > 0:
    left_cost = -x_np[idx0-1::-1]
    left_cost = np.append(np.zeros(1), left_cost)

    res = 10**6
    iter_s = max(0, K - (N-idx0))
    iter_e = min(K, idx0)
    for i in range(iter_s, iter_e):
        res = min(res, left_cost[i]*2 + right_cost[K-i])
    print(int(res))
else:
    print(int(right_cost[K]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 10 ** 10
for  i,x  in enumerate(xs):
    if i +  K  == N:
        break
    if x >= 0 and i > 0:
        break
    num = abs(xs[ i + K - 1 ] - xs[i]) + abs(xs[i])
    num1 = abs(xs[ i + K - 1 ] - xs[i]) + abs(xs[i + K -1])
    ans = min([num,num1,ans])
print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(N-K+1):
    ans = min(ans, (X[i+K-1]-X[i]) + abs(X[i]))

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
x_n = [0] + sorted([-x for x in X if x < 0])
x_p = [0] + sorted([x for x in X if x >= 0])
size_n,size_p = len(x_n), len(x_p)
ans = 2 * 10**10
for i in range(K+1):
	if i < size_n and K-i < size_p:
		print(i, x_n[i], x_p[K-i])
		ans = min(ans, x_n[i] + x_p[K-i] + min(x_n[i], x_p[K-i]))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from bisect import bisect, bisect_left
def inpl(): return list(map(int, input().split()))
N, K = inpl()
X = inpl()
ans = 10**9

r0 = bisect_left(X, 0)

for i in range(N):
   # print(r0+i, r0-K+i+1)
    if (0 <= r0+i < N) and (0 <= r0-K+i+1 < N):
        if X[r0+i] < 0 or X[r0-K+i+1] > 0:
            continue
        #print(2*X[r0+i] - X[r0-K+i+1])
        #print(X[r0+i] - 2*X[r0-K+i+1])
        ans = min(ans, 2*X[r0+i] - X[r0-K+i+1])
        ans = min(ans, X[r0+i] - 2*X[r0-K+i+1])

if r0+K-1 < N:
    ans = min(X[r0+K-1], ans)
if r0-K >= 0:
    ans = min(X[r0-K], ans)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())

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


mm = len(c)

for i in range(mm):
	if c[i] < 0:
		c[i] = abs(c[i])

for i in range(mm-1):
	c[i+1] += c[i]
m = c[b-1]
for i in range(mm-b):
	m = min(m,c[i+b]-c[i])
print(m)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
pos = bisect_left(x, 0)
#print(bisect_left(x, 0))

if x[0] >= 0:
	print(x[k - 1])
elif x[-1] <= 0:
	print(-x[-k])
else:
	ans = float("inf")
	if pos + k <= n:
		ans = x[pos + k - 1]
	if x[pos] == 0:
		if pos - k - 1 >= 0:
			ans = min(ans, -x[pos - k - 1])
	else:
		if pos - k >= 0:
			ans = min(ans, -x[pos - k])
	for i in range(max(pos, k - 1 - pos), min(pos + k - 1, n)):
		#print(i)
		right = x[i]
		left = -x[pos - (k - (i - pos + 1))]
		#print(right, left)
		ans = min(ans, 2 * right + left, 2 * left + right)

	print(ans)

Submission status: Wrong Answer

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

ans = float('inf')
for i in range(N-K+1):
    if x[i] >= 0 and x[i+K-1] >= 0:
        ans = min(ans,x[i+K-1])
    elif x[i] < 0 and x[i+K-1] < 0:
        ans = min(ans,-x[i+K-1])
    else:
        l = -x[i]
        r = x[i+K-1]
        mi = min(l,r)
        mx = max(l,r)
        ans = min(ans,mi*2+mx)

print(ans)

Submission status: Wrong Answer

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