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: Time Limit Exceeded

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

from math import floor

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    min_time = 1e9
    origin = get_origin(xs, n)

    if origin <= 0:
        print(xs[k - 1])
    elif origin >= n - 1:
        print(abs(xs[-k]))
    else:
        for i in range(max(origin - k + 1, 0), min(origin + 1, n - k)):
            part = xs[i:i + k]
            time = abs(part[-1] - part[0]) + min(abs(part[0]), abs(part[-1]))
            min_time = min(time, min_time)

        print(time)


def get_origin(xs, n):
    if xs[0] == 0:
        return 0
    elif xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n
    else:
        l = 0
        r = n
        while True:
            if r - l < 10:
                for i in range(l, r):
                    if xs[i] == 0 or xs[i + 1] > 0:
                        return i

            m = floor((r - l) / 2 + l)
            if xs[m] == 0:
                return m
            elif xs[m] < 0:
                if m < n and xs[m + 1] > 0:
                    return m
                else:
                    l = m
            else:
                if m > 0 and xs[m - 1] < 0:
                    return m - 1
                else:
                    r = m


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

from math import floor

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    min_time = 1e9
    origin = get_origin(xs, n)

    if origin == -1:
        print(xs[k - 1])
    elif origin == n:
        print(abs(xs[-k]))
    else:
        for i in range(max(origin - k + 1, 0), min(origin + 1, n - k)):
            part = xs[i:i + k]
            time = abs(part[-1] - part[0]) + min(abs(part[0]), abs(part[-1]))
            min_time = min(time, min_time)

        print(time)


def get_origin(xs, n):
    if xs[0] == 0:
        return 0
    elif xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n
    else:
        l = 0
        r = n
        while True:
            if r - l < 5:
                for i in range(l, r):
                    if xs[i] == 0 or xs[i + 1] > 0:
                        return i

            m = floor((r - l) / 2 + l)
            if xs[m] == 0:
                return m
            elif xs[m] < 0:
                if m < n and xs[m + 1] > 0:
                    return m
                else:
                    l = m
            else:
                if m > 0 and xs[m - 1] < 0:
                    return m - 1
                else:
                    r = m


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

long_list = []
for i in range(N - K + 1):
    long = []
    for j in range(K):
        long.append(x_list[i + j])
        
    if min(long) < 0 and max(long) > 0:
        left = abs(min(long)) 
        right = abs(max(long))
        total = left + right + min(left, right)
        long_list.append(total)
    else:
        if max(long) < 0:
            long_list.append(abs(min(long)))
        else:
            long_list.append(max(long))
           

ans = min(long_list)
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    cplus = 0
    cminus = 0
    czero = 0
    for j in range(len(L)) :

        if L[j] > 0 :
            cplus += 1
        elif L[j] < 0 :
            cminus += 1
        else :
            czero += 1
    if (cplus == K-czero) or (cminus == K-czero):
        ANS.append(max(abs(min(L)),max(L)))
    else :
        ANS.append(min(abs(min(L))*2+abs(max(L)),abs(max(L))*2+abs(min(L))))
print(min(ANS))

Submission status: Time Limit Exceeded

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

from math import floor

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    time = 1e9
    origin = get_origin(xs, n)

    if origin == -1:
        print(xs[k - 1])
    elif origin == n:
        print(abs(xs[-k]))
    else:
        for i in range(max(origin - k + 1, 0), min(origin + 1, n - k)):
            part = xs[i:i + k]
            rng = abs(part[0] - part[-1])
            time = min(time, rng + abs(part[0]), rng + abs(part[-1]))

        print(time)


def get_origin(xs, n):
    if xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n
    else:
        l = 0
        r = n
        while True:
            if r - l < 100:
                for i in range(l, r):
                    if xs[i] == 0 or xs[i + 1] > 0:
                        return i

            m = floor((r - l) / 2 + l)
            if xs[m] == 0:
                return m
            elif xs[m] < 0:
                if m < n and xs[m + 1] > 0:
                    return m
                else:
                    l = m
            else:
                if m > 0 and xs[m - 1] < 0:
                    return m - 1
                else:
                    r = m


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

X = []
for i in X_:
    X.append(int(i))

qwe = 0
for i in X:
    if i < 0:
        qwe += 1
    else:
        break
qwe_ = N - qwe - 1 - K
if qwe_ > K:
    X = X[:-qwe_]
if qwe > K:
    X = X[qwe-K:]



k = N - K + 1
ans = 10 ** 10
for i in range(k):
    x = X[i:i+K]
    a, b = min(x), max(x)
    
    if a*b < 0:
        ans_ = min(abs(a), abs(b))*2 + max(abs(a), abs(b))
    else:
        ans_ = max(abs(a), abs(b))
    
    if ans_ < ans:
        ans = ans_

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(n-k+1):
    t = X[i:i+k]
    a,b=t[0],t[-1]
    if b<0:
        ans=min(ans,-a)
    elif a>0:
        ans=min(ans,b)
    else:
        tmp=min(-a,b)*2+max(-a,b)
        ans=min(ans,tmp)
print(ans)

Submission status: Time Limit Exceeded

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

c = []
for i in range(N - K + 2):
    if (0 not in x[i:i+K+1]) or (len(x[i:i+K+1]) != K+1):
        continue
    p = x[i:i+K+1]
    if min(p) < 0 and max(p) > 0:
        if p[-1] + p[-1] - p[0] < abs(p[0]) + p[-1] -p[0]:
            c.append(p[-1] + (p[-1] - p[0]))
        else:
            c.append(abs(p[0]) + p[-1] - p[0])
    elif min(p) >= 0:
        c.append(p[-1])
    elif max(p) <= 0:
        c.append(abs(p[0]))


print(min(c))

Submission status: Time Limit Exceeded

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

from math import floor

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    time = 1e9
    origin = get_origin(xs, n)

    if origin == -1:
        print(xs[k - 1])
    elif origin == n - 1:
        print(abs(xs[-k]))
    else:
        for i in range(max(origin - k + 1, 0), min(origin + 1, n - k)):
            part = xs[i:i + k]
            rng = abs(part[0] - part[-1])
            time = min(time, rng + abs(part[0]), rng + abs(part[-1]))

        print(time)


def get_origin(xs, n):
    if xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n - 1
    else:
        l = 0
        r = n
        while True:
            if r - l < 5:
                for i in range(l, r):
                    if xs[i] == 0 or xs[i + 1] > 0:
                        return i

            m = floor((r - l) / 2 + l)
            if xs[m] == 0:
                return m
            elif xs[m] < 0:
                if m < n and xs[m + 1] > 0:
                    return m
                else:
                    l = m
            else:
                if m > 0 and xs[m - 1] < 0:
                    return m - 1
                else:
                    r = m


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
X = list(map(int,input().split()))
ANS = []
for i in range(N-K+1) :
    L = X[i:K+i]
    cplus = 0
    cminus = 0
    czero = 0
    for j in range(len(L)) :

        if L[j] > 0 :
            cplus += 1
        elif L[j] < 0 :
            cminus += 1
        else :
            czero += 1
        if (cplus == K-czero) or (cminus == K-czero):
            ANS.append(max(abs(min(L)),max(L)))
        else :
            ANS.append(min(abs(min(L))*2+abs(max(L)),abs(max(L))*2+abs(min(L))))
print(min(ANS))

Submission status: Time Limit Exceeded

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

from math import floor

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    time = 1e9
    origin = get_origin(xs, n)

    if origin == -1:
        print(xs[k - 1])
    elif origin == n - 1:
        print(abs(xs[-k]))
    else:
        for i in range(max(origin - k + 1, 0), min(origin + k, n - k)):
            part = xs[i:i + k]
            rng = abs(part[0] - part[-1])
            time = min(time, rng + abs(part[0]), rng + abs(part[-1]))

        print(time)


def get_origin(xs, n):
    if xs[0] > 0:
        return -1
    elif xs[-1] < 0:
        return n - 1
    else:
        l = 0
        r = n
        while True:
            if r - l < 5:
                for i in range(l, r):
                    if xs[i] == 0 or xs[i + 1] > 0:
                        return i

            m = floor((r - l) / 2 + l)
            if xs[m] == 0:
                return m
            elif xs[m] < 0:
                if m < n and xs[m + 1] > 0:
                    return m
                else:
                    l = m
            else:
                if m > 0 and xs[m - 1] < 0:
                    return m - 1
                else:
                    r = m


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools as ito
N,K=map(int,input().split(" "))
X=list(map(int,input().split(" ")))

Ex=list(ito.permutations(X,K))

ans=0
for i in range(len(Ex)):
    tans=0
    tx=0
    for j in range(len(Ex[i])):
        tans+=abs(tx-Ex[i][j])
        tx=Ex[i][j]
        
    if tans<ans or i==0:
        ans=tans
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,k = map(int,input().split())
a = list(map(int,input().split()))
i = 0
while i < N:
    if a[i] >= 0:
        break
    i += 1
x = []
y = []
for j in range(i-k,i+1):
    if j>=0 and j+k-1<N:
        x = a[j:j+k]
        if x[0] < 0 and x[k-1] > 0:
            y.append(min(-x[0],x[k-1])*2 + max(-x[0],x[k-1]))
        else:
            y.append(max(-x[0],x[k-1]))
print(min(y))

Submission status: Time Limit Exceeded

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

X = []
for i in X_:
    X.append(int(i))

k = N - K + 1
ans = 10 ** 10
for i in range(k):
    x = X[i:i+K]
    a, b = min(x), max(x)
    
    if a*b < 0:
        ans_ = min(abs(a), abs(b))*2 + max(abs(a), abs(b))
    else:
        ans_ = max(abs(a), abs(b))
    
    if ans_ < ans:
        ans = ans_

print(ans)

Submission status: Time Limit Exceeded

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

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

times = []

for i in range(len(candles) - K + 1):
    extracted = candles[i:i+K]
    #print(extracted)
    #print(i, K)

    times.append(abs(extracted[0]) + (extracted[-1] - extracted[0]))
    times.append(abs(extracted[-1]) + (extracted[-1] - extracted[0]))

print(min(times))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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


def mindist(XLIST):
    dist = float('Inf')
    for i in itertools.permutations(XLIST, len(XLIST)):
        dist_ = 0
        curx = 0
        for j in range(len(i)):
            dist_ += abs(i[j]-curx)
            curx = i[j]
        if (dist_ < dist):
            dist = dist_
    return(dist)

dist = float('Inf')
for i in itertools.combinations(X, K):
    dist_ = mindist(i)
    if (dist_ < dist):
        dist = dist_

print(dist)

Submission status: Time Limit Exceeded

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

def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))

    ans = float('inf')
    for i in range(n - k + 1):
        arry = x_list[i:i+k]
        if arry[0] >= 0 and arry[-1] >= 0:
            ans = min(ans, arry[-1])
        elif arry[0] < 0 and arry[-1] < 0:
            ans = min(ans, -arry[0])
        else:
            ans = min(ans, -arry[0] * 2 + arry[-1])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():
    n, k = map(int, input().split())
    x_list = list(map(int, input().split()))

    ans = float('inf')
    for i in range(n - k + 1):
        arry = x_list[i:i+k]
        if all(x >= 0 for x in arry):
            ans = min(ans, arry[-1])
        elif all(x < 0 for x in arry):
            ans = min(ans, -arry[0])
        else:
            ans = min(ans, -arry[0] * 2 + arry[-1])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def c_candles(N, K, X):
    from bisect import bisect_left
    negative_pos = X[:bisect_left(X, 0)][::-1]
    positive_pos = X[bisect_left(X, 0):]
    negative_pos_rel = negative_pos[:]
    positive_pos_rel = positive_pos[:]
    for k in range(1, len(negative_pos)):
        negative_pos_rel[k] -= negative_pos[k - 1]
    for k in range(1, len(positive_pos)):
        positive_pos_rel[k] -= positive_pos[k - 1]
    # print(X, negative_pos_rel, positive_pos_rel)

    ans = float('inf')
    for k in range(len(negative_pos) + 1):
        length = 0
        n = K - k
        if n > len(positive_pos):
            continue

        length += abs(sum(negative_pos_rel[:k]))
        length += length
        length += sum(positive_pos_rel[:n])
        ans = min(ans, length)
    #     print(n, k, length)
    # print('-')
    for n in range(len(positive_pos) + 1):
        length = 0
        k = K - n
        if k > len(negative_pos):
            continue
        length += sum(positive_pos_rel[:n])
        length += length
        length += abs(sum(negative_pos_rel[:k]))
        ans = min(ans, length)
        # print(n, k, length)

    return ans

N, K = [int(i) for i in input().split()]
X = [int(i) for i in input().split()]
print(c_candles(N, K, X))

Submission status: Time Limit Exceeded

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):
        part = problem[i:i+K]
        left = part[0]
        right = part[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: Time Limit Exceeded

Solution code:
N,k = map(int,input().split())
a = list(map(int,input().split()))
i = 0
while i < N:
    if a[i] >= 0:
        break
    i += 1
x = []
for j in range(i-k,i+1):
    if j>=0 and j+k-1<N:
        x.append(a[j:j+k])
y = []
for i in range(len(x)):
    if x[i][0] < 0 and x[i][k-1] > 0:
        y.append(min(-x[i][0],x[i][k-1])*2 + max(-x[i][0],x[i][k-1]))
    else:
        y.append(max(-x[i][0],x[i][k-1]))
print(min(y))

Submission status: Time Limit Exceeded

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

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


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])
	ans = min(ans,b)
print(ans)

Submission status: Time Limit Exceeded

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

def main():
    n, k = map(int, input().split())
    xs = list(map(int, input().split()))

    if n == k:
        print(abs(xs[-1] - xs[0] + min(abs(xs[0]), abs(xs[-1]))))
        return

    time = 1e9
    for i in range(0, n - k):
        part = xs[i:i + k]
        r = abs(part[0] - part[-1])
        time = min(time, r + abs(part[0]), r + abs(part[-1]))

    print(time)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def solve():
    N, K = map(int, input().split())
    C = np.array(list(map(int,input().split())))
    D = list()
    for i in range(N-K+1):
      c = C[i:i+K]
      l, r = min(min(c),0), max(max(c),0)
      d = -2*l + r if -l < r else  -l + 2*r
      D.append(d)
    return str(min(D))

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

Submission status: Accepted

Solution code:

def resolve():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 1 << 60

    for i in range(N - K + 1):
        l = A[i]
        r = A[i + K - 1]
        l_to_r = abs(l) + abs(l - r)
        r_to_l = abs(r) + abs(r - l)
        res = min(l_to_r, r_to_l)
        ans = min(ans, res)

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X_list = list(map(int, input().split()))
INF = 10 ** 18
ans = INF
import bisect

zero_index = bisect.bisect_left(X_list, 0)
candidate_list = []
ans = INF
for i in range(K + 1):
    head = zero_index - i
    tail = head + K - 1
    if head < 0 or tail > N - 1: continue
    if i == 0:
        ans = min(ans, abs(X_list[tail]))
    elif i == K:
        ans = min(ans, abs(X_list[head]))
    else:
        head_abs = abs(X_list[head])
        tail_abs = abs(X_list[tail])
        ans = min(ans, head_abs + tail_abs + min(head_abs, tail_abs))
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():

    n, k= LI()
    x = LI()
    ans = float("inf")

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

    print(ans)

main()

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc107/tasks/arc101_a"

def main():
    n, k = list(map(int, input().split()))
    xs = list(map(int, input().split()))
    ans = 100000000000000
    se = [0, k-1]
    while se[1] < n:
        if xs[se[0]] < 0 <= xs[se[1]]:
            tmp = min(abs(xs[se[0]]), xs[se[1]]) * 2 + max(abs(xs[se[0]]), xs[se[1]])
            ans = min(ans, tmp)
        elif xs[se[0]] < 0:
            ans = min(ans, abs(xs[se[0]]))
        elif xs[se[1]] >= 0:
            ans = min(ans, xs[se[1]])
        se = [v + 1 for v in se]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
# 
# 
# @stop_watch
def solve(N, K, x):
    l = [abs(xi) for xi in x if xi < 0] + [0]
    l.reverse()
    r = [0] + [xi for xi in x if xi >= 0]
    ans = 10 ** 18 * 2 + 1
    for i in range(max(K - (len(l) - 1), 0), min(K + 1, len(r))):
        ll = l[K - i]
        rr = r[i]
        ans = min(ans, min(ll, rr) * 2 + max(ll, rr))
    print(ans)


if __name__ == '__main__':
    # S = input()
    # N = int(input())
    N, K = map(int, input().split())
    x = [int(i) for i in input().split()]
    # B = [int(i) for i in input().split()]
    # AB = [[int(i) for i in input().split()] for _ in range(N)]
    solve(N, K, x)

    # # test
    # from random import randint
    # from func import random_str
    # solve()

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(abs,x))
ans = float("inf")

for i in range(n-k+1):
    if 0 < x[i] * x[i+k-1]:
        cost = max(y[i],y[i+k-1])
        
    else:
        cost = y[i] + y[i+k-1] + min(-x[i],x[i+k-1])
        
    ans = min(ans,cost)
    
print(ans)

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = []
for j in range(n-k+1):
    if x[j] > 0:
        ans.append(x[j+k-1])
    elif x[j] <= 0 <= x[j+k-1]:
        ans.append(min(2*abs(x[j])+x[j+k-1], abs(x[j])+2*x[j+k-1]))
    else:
        ans.append(-x[j])
print(min(ans))

Submission status: Accepted

Solution code:
# n=int(input())
n, k = map(int, input().split())
xl = list(map(int, input().split()))
#l=[list(input()) for i in range(h)]

if 0 in xl:
    k -= 1
else:
    xl.append(0)
    xl.sort()
if k == 0:
    ans = 0
else:
    ans = 10**9
    zeropoint = xl.index(0)
    endpoint = len(xl)-1
    startpoint = max(0, zeropoint-k)
    cur = startpoint
    while cur+k <= endpoint and cur <= zeropoint:
        # print(cur)
        # もし折り返すなら
        if cur < zeropoint < cur+k:
            tempdis = abs(xl[cur]-xl[cur+k])
            tempdis += min(abs(xl[cur]), xl[cur+k])
            ans = min(ans, tempdis)
        else:
            ans = min(ans, abs(xl[cur]-xl[cur+k]))
        cur += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

rui=[0]
for i in range(n-1):
    rui.append(x[i+1]-x[i]+rui[i])
# print(rui)

ans=float('inf')
for i in range(n-k+1):
    tmp=rui[i+k-1]-rui[i]
    ans=min(ans,tmp+min(abs(x[i]),abs(x[i+k-1])))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
a= []
for i in range(N-K+1):
    l = X[i]
    r = X[i+K-1]
    a.append(min(abs(l)+abs(r-l), abs(r) + abs(r-l)))
print(min(a))

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
k-=1
ans=float('inf')

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



print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
positive=[]
negative=[]
for i in range(N):
    if x[i]<0:
        negative.append(-x[i])
    else:
        positive.append(x[i])
negative.sort()
n=len(negative)
m=len(positive)
ans=1000000000
if n>=K:
    ans=min(ans,negative[K-1])
if m>=K:
    ans=min(ans,positive[K-1])

for i in range(n):
    j=K-(i+1)
    if j>m:
        continue
    if j<=0:
        continue
    ans=min(ans,negative[i]+positive[j-1]+min(negative[i],positive[j-1]))
print(ans)

Submission status: Accepted

Solution code:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 10**9
for i in range(N-K+1):
    l, r = A[i], A[i+K-1]
    if l*r >= 0:
        ans = min(ans, max(abs(l), abs(r)))
    else:
        ans = min(ans, r-l+min(abs(l), abs(r)))
print(ans)

Submission status: Accepted

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

ans = 10**10
for i in range(n-k+1):
  d1 = abs(X[i]) + abs(X[i] - X[i+k-1])
  d2 = abs(X[i+k-1]) + abs(X[i] - X[i+k-1])
  if ans > min(d1, d2):
    ans = min(d1,d2)
if ans == 10**10:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
xp = [i for i in x if i >= 0]
xn = [i for i in x if i < 0]
xn.reverse()
lp = len(xp)
ln = len(xn)
if lp == 0:
    print(-xn[K-1])
    exit()
if ln == 0:
    print(xp[K-1])
    exit()
pc = min(K,lp)
pp = xp[pc-1]
if K>lp:
    nc = K-lp
    np = -xn[nc-1]
else:
    nc = 0
    np = 0
ans = pp + np + min(pp,np)
for i in range(pc-1):
    if nc == ln:
        break
    pp = xp[pc-i-2]
    np = -xn[nc]
    nc += 1
    ans = min(ans,pp + np + min(pp,np))
print(ans)

Submission status: Accepted

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


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N, K = MI()
    targets = LI()
    if N == 1:
        print(abs(targets[0]))
        return
    length = targets[K - 1] - targets[0]
    ans = length + min(abs(targets[K-1]), abs(targets[0]))
    for i in range(N-K + 1):
        length = targets[i + K - 1] - targets[i]
        cost = length + min(abs(targets[i+K-1]), abs(targets[i]))
        ans = min(ans, cost)

    print(ans)


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
pos, nega = [], []
for v in x:
    if v >= 0:pos.append(v)
    else:nega.append(-v)
nega.sort()
ans = 10**9
if k <= len(pos):ans = min(ans, pos[k-1])
if k <= len(nega):ans = min(ans, nega[k-1])
for i in range(k):
    if i < len(pos) and 0 <= k - i - 2 < len(nega):
        ans = min(ans, 2 * pos[i] + nega[k-i-2])
    if i < len(nega) and 0 <= k - i - 2 < len(pos):
        ans = min(ans, 2 * nega[i] + pos[k-i-2])
print(ans)

Submission status: Accepted

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

import bisect

i=bisect.bisect_left(X,0)
L=max(0, bisect.bisect_right(X,0)-K)
R=min(N-1, bisect.bisect_left(X,0)+K-1)

ans=float('inf')

for i in range(L,R-K+2):
    t=0
    l=X[i]
    r=X[i+K-1]
    if r<=0:
        t=abs(l)
    elif 0<=l:
        t=r
    else:
        t=min(abs(l),r)*2+max(abs(l),r) 
    # print(X[i:i+K],t)
    ans=min(ans,t)
print(ans)