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:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i > 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()
    if 0 in X:
        K -= 1
        if K == 0:
            return "Yes"

    ans = 10**50
    if len(A1) >= K:
        ans = A1[K-1]
    if len(A2) >= K:
        ans = min(A2[K-1], ans)

    for i in range(1, 10**10):
        i1 = i-1
        i2 = K-i-1
        if i1 >= len(A1):
            break
        if i2 >= len(A2) or i2 < 0:
            break

        #print(i1, i2)
        ans = min(2 * A1[i1]+A2[i2], ans)
        ans = min(2 * A2[i2]+A1[i1], ans)

    #print(A1)
    #print(A2)
    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i >= 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()

    ans = 10**50
    if len(A1) >= K:
        ans = A1[K-1]
    if len(A2) >= K:
        ans = min(A2[K-1], ans)

    for i in range(1, 10**10):
        i1 = i-1
        i2 = K-i-1
        if i1 >= len(A1):
            break
        if i2 >= len(A2) or i2 < 0:
            break

        #print(i1, i2)
        ans = min(2 * A1[i1]+A2[i2], ans)
        ans = min(2 * A2[i2]+A1[i1], ans)

    #print(A1)
    #print(A2)
    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i >= 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()

    ans = 10**50
    if len(A1) >= K:
        ans = A1[K-1]
    if len(A2) >= K:
        ans = min(A2[K-1], ans)

    for i in range(1, 10**10):
        i1 = i-1
        i2 = K-i-1
        if i1 >= len(A1):
            break
        if i2 >= len(A2) or i2 < 0:
            break

        print(i1, i2)
        ans = min(2 * A1[i1]+A2[i2], ans)
        ans = min(2 * A2[i2]+A1[i1], ans)

    #print(A1)
    #print(A2)
    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7
mod2 = 2**61+1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

def iip(listed=True):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def iip_ord():
    return [ord(i) - ord("a") for i in input()]


def main():
    ans = solve()
    if ans is not None:
        print(ans)

def solve():
    N, K = iip(False)
    X = iip()

    A1 = [i for i in X if i >= 0]
    A2 = [-i for i in X if i < 0]
    A2.sort()

    ans = 10**50
    if len(A1) >= K:
        ans = A1[K-1]
    if len(A2) >= K:
        ans = min(A2[K-1], ans)

    for i in range(1, len(A1)):
        i1 = i-1
        i2 = K-i-1
        if i1 >= len(A1):
            break
        if i2 >= len(A2):
            break
        ans = min(2*A1[i1]+A2[i2], ans)
        ans = min(2*A2[i2]+A1[i1], ans)

    print(ans)


#####################################################ライブラリ集ここから

def YesNo(s):
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):
    for i in s:
        print(i)

def split_print_space(s):
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def koenai_saidai_x_index(sorted_list, n):
    l = 0
    r = len(sorted_list)
    if len(sorted_list) == 0:
        return False
    if sorted_list[0] > n:
        return False

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] == n:
            return x
        elif sorted_list[x] > n:
            r = x
        else:
            l = x
    return l


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        # print(sorted_list)
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r - l > 1:
        x = (l + r) // 2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):
    return power(n, mod - 2)


def power(n, p, mod_= mod):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

x_cs = [0] * (n + 1)
for i in range(n):
    x_cs[i + 1] = x_cs[i] + x[i]

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

print(ans)

Submission status: Wrong Answer

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

max = 10000000000000000000000000
for i in range(N - K + 1):
    # i = 0
    t_1 = abs(X[i])
    t_1 += X[i + K - 1] - X[i]

    t_2 = abs(X[i + K - 1])
    t_2 = X[i] - X[i + K - 1]

    if t_1 < max:
        max = t_1
    if t_2 < max:
        max = t_2

print(max)

Submission status: Wrong Answer

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

max = 10000000000000000000000000
for i in range(N - K + 1):
    # i = 0
    t = abs(X[i])
    t += X[i + K - 1] - X[i]
    if t < max:
        max = t

print(max)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    D = [0]*(N - 1)
    for i in range(1, N):
        D[i-1] = X[i] - X[i - 1]
    ans = 0
    t = 0
    T = [0]*(N - K + 1)
    for i in range(1, N):
        t += D[i-1]
        if i < K:
            if i == K - 1:
                T[0] = t
            continue
        t -= D[i - K]
        T[i - K + 1] = t
    # print(D, T)
    ans = K * 10**8
    for i in range(N - K + 1):
        a = T[i] + abs(X[i])
        ans = min(a, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# N, K = 3, 2
# X = [10, 20, 30]

max = 10000000000000000000000000
for i in range(N - K + 1):
    # i = 0
    t = abs(X[i])
    for j in range(i + 1, i + K):
        t += X[j] - X[j - 1]
    if t < max:
        max = t

print(max)

Submission status: Wrong Answer

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

max = 100000000000
for i in range(N - K + 1):
    # i = 0
    t = abs(X[i])
    for j in range(i + 1, i + K):
        t += X[j] - X[j - 1]
    if t < max:
        max = t

print(max)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
lm = []
lp = []
while l:
    x = l.pop(0)
    if x >= 0:
        lp.append(x)
    else:
        lm.append(-x)
ans = 100000000000
if lm and lp:
  for i in range(min(len(lp),K)):
    if K - i - 1 > 0 and len(lm) >= K-i-1:
      ans = min(ans,2*lp[i] + lm[-(K-i-1)],lp[i] + 2*lm[-(K-i-1)])
    if K == i:
      ans = min(ans,lp[i])
elif lp:
  ans = lp[K-1]
else:
  ans = lm[1-K]
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
Min = 1e10
for i in range(N-K+1):
    Min = min(Min, x[K+i-1]-x[i]+abs(x[i]))
print(Min)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = sorted(list(int(i) for i in input().split()))
ans = 10**5
for i in range(N-K+1):
  left =x[i]
  right =x[i+K-1]
  p1 = abs(left) + abs(right-left)
  p2 = abs(right) + abs(right-left)
  p = min(p1,p2)
  ans = min(ans,p)
  
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = [int(i) for i in input().split()]
ans = 10**5
for i in range(N-K+1):
  left =x[i]
  right =x[i+K-1]
  p1 = abs(left) + abs(right-left)
  p2 = abs(right) + abs(right-left)
  p = min(p1,p2)
  ans = min(ans,p)
  
  
print(ans)

Submission status: Wrong Answer

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

if x[0] >= 0:
    print(x[k - 1])
    exit()
elif x[n - 1] < 0:
    print(abs(x[n - k]))
    exit()
else:
    for i in range(n):
        if x[i] >= 0:
            ori = i
            break

f_inf = float("inf")
res = f_inf
for i in range(max(ori - k, 0), min(ori + k, n) - k + 1):
    time = abs(x[i]) + abs(x[i + k - 1] - x[i])
    res = min(res, time)

print(res)

Submission status: Wrong Answer

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

if x[0] >= 0:
    print(x[k - 1])
    exit()
elif x[n - 1] < 0:
    print(x[n - k])
    exit()
else:
    for i in range(n):
        if x[i] >= 0:
            ori = i
            break

f_inf = float("inf")
res = f_inf
for i in range(max(ori - k, 0), min(ori + k, n) - k + 1):
    time = abs(x[i]) + abs(x[i + k - 1] - x[i])
    res = min(res, time)

print(res)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,k=mii()
arr=limii()
ans=10**1000
for i in range(n-k+1):
    ans=min(ans, abs(arr[i])+abs(arr[i]-arr[i+k-1]))
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
X = []
Y = []
for x in map(int, input().split()):
    if x < 0:
        X.append(x)
    else:
        Y.append(x)
X.reverse()
INF = 10**9
ans = INF
if len(X) == 0:
    ans = Y[k-1]
elif len(Y) == 0:
    ans = X[k-1]
else:
    for i in range(k):
        L = k-i-1
        R = i-1
        if L >= len(X) or R >= len(Y):
            continue
        a = X[L]*(-1)
        b = Y[R]
        ans = min(ans, min(a, b)+a+b)
print(ans)

Submission status: Wrong Answer

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

ans = 10 ** 8 * 2
for i in range(N - K + 1):
    if x[i + K - 1] <= 0:
        tmp = -x[i]
    elif x[i] >= 0:
        tmp = x[i + K - 1]
    else:
        tmp = -x[i] * 2 + x[i + K - 1]
        tmp = min(tmp, -x[i] + 2 * x[i + K - 1])

    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import sys

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

index = bisect.bisect_left(x,0)

if x[0] >= 0 :
  print(x[k-1])
  sys.exit()
  
if x[-1] <= 0 :
  print(x[n-k]*(-1))
  sys.exit()

ans = 10**9
if min(index+k,n) == index+k :
  ans = x[index+k-1]

if max(0,index-k) == index - k :
  ans = min(ans,x[index-k]*(-1))
  
c = 0
for i in range(index,min(index+k,n)) :
  c += 1
  if i <= n-1 and i-(k-1) <= n-1 :
    ans = min(ans,x[i]*2+x[i-(k-1)]*(-1))

c = 0
for i in range(max(0,index-k),index) :
  c += 1
  if i <= n-1 and i+(k-1) <= n-1 :
    ans = min(ans,x[i]*(-1)*2+x[i+(k-1)])
  
print(ans)

Submission status: Wrong Answer

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

    left = 0
    right = N
    target = 0
    start = None
    before = None
    while(left <= right):
        mid = (left + right)//2
        if mid == before:
            start = mid
            break
        if Xs[mid] == target:
            start = mid
            break
        elif Xs[mid] < target:
            left = mid
        else:
            right = mid
        before = mid
    
    minimum, maximum = min(Xs[start:start+K]), max(Xs[start:start+K])
    print(abs(minimum) + maximum - minimum)

if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

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

    left = 0
    right = N
    target = 0
    start = None
    before = None
    while(left <= right):
        mid = (left + right)//2
        if mid == before:
            start = mid
            break
        if Xs[mid] == target:
            start = mid
            break
        elif Xs[mid] < target:
            left = mid
        else:
            right = mid
        before = mid
    
    print(Xs[start:start+K])
    ans = 0
    minimum, maximum = min(Xs[start:start+K]), max(Xs[start:start+K])
    print(abs(minimum) + maximum - minimum)

if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, raw_input().split())
x = map(int, raw_input().split())
ans = 10**9
for y in range(N-K):
    left = x[y]
    right = x[y+K-1]
#    print left, right                                                                                                                                    

    if left < 0 and right <= 0:
        ans = min(ans, -left)
    elif left <= 0 and right > 0:
        ans = min(ans, -2*left+right, -left+2*right)
    else:
        ans = min(ans, right)

if ans == 10**9:
    print 0
else:
    print ans

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
a, b = 0, 0
s, t = 0, 0
for i in range(n):
    if x[i] < 0:
        a += 1
        s = i
    elif x[i] == 0:
        k -= 1
        b += 1
    else:
        t = i
        break
c = n - a - b
ans = 1145141919810
for i in range(n - k - b + 1):
    if x[i] < 0 and x[i + k + b - 1] > 0:
        m = 2 * min(abs(x[i]), abs(x[i + k + b - 1])) + max(abs(x[i]), abs(x[i + k + b - 1]))
        if m < ans:
            ans = m
if a >= k:
    if abs(x[s - k - 1]) < ans:
        ans = abs(x[s - k - 1])
if c >= k:
    if abs(x[t + k - 1]) < ans:
        ans = abs(x[t + k - 1])
print(ans)

Submission status: Wrong Answer

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

ans = 10 ** 10
for i in range(n - k):
    left_d = max([abs(j) for j in X[i:i+k] if j < 0] + [0])
    right_d = max(X[i:i+k] + [0])
    ans_ = max(left_d, right_d) + 2 * min(left_d, right_d)
    ans = min(ans, ans_)

if ans == 10 ** 10:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
ans=10**10
for i in range(n+1-k):
  s=A[i]
  t=A[i+k-1]
  if s*t>=0:
    a=abs(max(s,t))
  else:
    a=abs(max(s,t))+abs(min(s,t))*2
  if a<ans:
    ans=a
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(map(int,input().split()))
ans=float("inf")
if N==1:
	ans=abs(S[0])
for i in range(N-K):
	L=S[i]
	R=S[i+K-1]
	ans=min(abs(L)+abs(R-L),abs(R)+abs(L-R),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(map(int,input().split()))
ans=float("inf")
if N==1:
	ans=S[0]
for i in range(N-K):
	L=S[i]
	R=S[i+K-1]
	ans=min(abs(L)+abs(R-L),abs(R)+abs(L-R),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
S=list(map(int,input().split()))
ans=float("inf")
for i in range(N-K):
	L=S[i]
	R=S[i+K-1]
	ans=min(abs(L)+abs(R-L),abs(R)+abs(L-R),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):
    if ans > abs(x[i]) + (x[i + K - 1] - x[i]):
        ans = abs(x[i]) + (x[i + K - 1] - x[i])
    if ans > abs(x[i + K - 1]) + (x[i + K - 1] - x[i]):
        ans = abs(x[i]) + (x[i + K - 1] - x[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k, *a = map(int, open(0).read().split())
a = [-1e11] + a + [1e11]

ans = 1e11
for i in range(1, n-k+2):
    if a[i-1]<0 and a[i+k]>=0:
        if a[i]>=0: ans = min(ans, a[i+k-1])
        elif a[i+k-1]<=0: ans = min(ans, a[i+k-1])
        else: ans = min(ans, a[i+k-1]*2-a[i], a[i+k-1]-a[i]*2)
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):
    temp=x[i+k-1]-x[i]+abs(min(x[i],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()))
x_sei = []
x_hu = []
for i in range(n):
    if x[i] < 0:
        x_hu.append(x[i])
    else:
        x_sei.append(x[i])
x_hu.reverse()
x_sei.append(int(1e10))
x_hu.append(int(-1e10))
if len(x_hu) == 1:
    print(x_sei[k-1])
    exit()
if len(x_sei) == 1:
    print(abs(x_hu[k-1]))
    exit()
cnt = 0
sei_i = 0
hu_i = 0
sei_nax = 0
hu_nax = 0
while sei_i + hu_i != k:
    a = x_sei[sei_i]
    b = x_hu[hu_i]
    if a <= abs(b):
        sei_i += 1
        sei_nax = a
    else:
        hu_i += 1
        hu_nax = b
print(min(sei_nax, abs(hu_nax))*2+max(sei_nax, abs(hu_nax)))

Submission status: Wrong Answer

Solution code:
# なぜかうまくいかない
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_right, bisect_left

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
A = LIST()
# 0 0 0の左
zero = A.count(0)
# A.(0)
l = bisect_left(A, 0)
r = bisect(A, 0)
zero = r - l
C = []
for a in A[:l]:
    if a == 0:
        continue
    C.append(a)
    
B = C + A[r:]


w = K - zero
ans = INF
B.insert(0, -INF)
B.append(INF)
if zero == K:
    print(0)
    exit()

for i in range(len(B) - K + 1):
    r = B[i + K - zero - 1]
    l = B[i]
    tmp = INF
    if l < 0 and r > 0:
        tmp = min(abs(l), r) + r - l
    # ans = min(ans, tmp)
    elif l > 0 and B[i-1] < 0:
        tmp = min(abs(l), r) + r - l

    elif r < 0 and B[i + K - zero - 1 + 1] > 0:
        tmp = min(abs(l), r) + r - l
    ans = min(ans, tmp)
print(ans)


        # if abs(B[i]) <= :

#         tmp = B[i + K - zero-1] - B[i]
#         print(B[i+K-zero-1])
#         print(B[i])
#         ans = min(ans, tmp)
# 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):
    temp=x[i+k-1]-x[i]+abs(x[i])
    if ans>temp:ans=temp
print(ans)