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

ans = []

for _ in range(n - k + 1):
    if x[_] >= 0:
        ans.append(abs(x[k + _ - 1]) + abs(x[k + _ - 1] - x[_]))
    else:
        ans.append(abs(x[_]) + abs(x[_] - x[k + _ - 1]))
    

print(min(ans))

Submission status: Wrong Answer

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

ans = []

for _ in range(n - k + 1):
    if x[_] >= 0:
        ans.append(abs(x[_]) + abs(x[k + _ - 1] - x[_]))
    else:
        ans.append(abs(x[k + _ - 1]) + abs(x[_] - x[k + _ - 1]))
    

print(min(ans))

Submission status: Wrong Answer

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

ans = []

for _ in range(n - k + 1):
    ans.append(abs(x[_]) + abs(x[k + _ - 1] - x[_]))

print(min(ans))

Submission status: Wrong Answer

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

ans = []

for _ in range(k):
    if k + _ > n:
        continue
    ans.append(abs(x[_]) + abs(x[k + _ - 1] - x[_]))

print(min(ans))

Submission status: Wrong Answer

Solution code:
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k < len(positive_list):
    ans_list.append(positive_list[k - 1])

if k < len(negative_list):
    ans_list.append(-1 * negative_list[k - 1])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

Submission status: Wrong Answer

Solution code:
a = [list(map(int, input().split())) for i in range(2)]
k = a[0][1]

num_list = a[1]

positive_list = []
negative_list = []

ans_positive = []
ans_negative = []

ans_list = []

for _ in num_list:
    if _ > 0:
        positive_list.append(_)
    elif _ < 0:
        negative_list.insert(0, _)
    else:
        k -= 1

for _ in list(range(len(positive_list))):
    if not 0 <= k - _ - 2 < len(negative_list):
        continue
    ans_positive.append(2 * positive_list[_] - negative_list[k - _ - 2])

for _ in list(range(len(negative_list))):
    if not 0 <= k - _ - 2 < len(positive_list):
        continue
    ans_negative.append(-2 * negative_list[_] + positive_list[k - _ - 2])

if ans_positive:
    ans_list.append(min(ans_positive))

if ans_negative:
    ans_list.append(min(ans_negative))

if k < len(positive_list):
    ans_list.append(positive_list[k])

if k < len(negative_list):
    ans_list.append(-1 * negative_list[k])

if ans_list == []:
    print(0)

else:
    print(min(ans_list))

Submission status: Wrong Answer

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

ans = 2*10**8+1

for i in range(N-K+1):
    left = x_list[i]
    right = x_list[i+K-1]
    ans_temp = min(abs(left), abs(right)) + right - left
    ans = min(ans, ans_temp)

print(ans)

Submission status: Wrong Answer

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

ans = 10**6

for i in range(N-K+1):
    left = x_list[i]
    right = x_list[i+K-1]
    ans_temp = min(abs(left), abs(right)) + right - left
    ans = min(ans, ans_temp)

print(ans)

Submission status: Wrong Answer

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

ans = 10**5 + 1

for i in range(N-K+1):
    left = x_list[i]
    right = x_list[i+K-1]
    ans_temp = min(abs(left), abs(right)) + right - left
    ans = min(ans, ans_temp)

print(ans)

Submission status: Wrong Answer

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

for i in range(n-k+1):
    a = x[i]
    b = x[i+k-1]
    t= min(abs(a)+abs(b-a), abs(b)+abs(b-a))
    if m > t:
        m = t
print(m)

Submission status: Wrong Answer

Solution code:
from scipy.special import comb
import itertools

n, k = map(int, input().split())
x = list(map(int, input().split()))
X = []
m = 2*10**8

for v in itertools.combinations(x, k):
    X.append(list(v))

for i in range(comb(n, k, exact=True)):
    a = min(abs(min(X[i]))+abs(max(X[i])-min(X[i])), abs(max(X[i]))+abs(max(X[i])-min(X[i])))
    if m > a:
        m = a
print(m)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
X = list(map(int, input().split()))
XN, XP = [], []
for x in X:
    if x < 0: XN.append(-x)
    else: XP.append(x)
XN.reverse()
T = [0]
S = 0
for i, x in enumerate(XN):
    if i==K-1:
        T.append(x)
        break
    T.append(2*x)
res = float("inf")
for i in range(K):
    if K-1-i >= len(XP) or i>=len(T):
        continue
    res = min(T[i] + XP[K-1-i], res)
print(res)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float('inf')
if(0 in x):
    K=K-1
    x.remove(0)
    if(K==0):
        print(0)
        exit()
for i in range(0,N-K):
    if((x[i]<0)):
        if(x[i+K-1]>0):
            ans = min(ans,abs(x[i])*2+x[i+K-1],abs(x[i])+x[i+K-1]*2)
        else:
            ans = min(ans,abs(x[i]))
    else:
            ans = min(ans,x[i+K-1])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float('inf')
if(0 in x):
    K=K-1
    x.remove(0)
    if(K==0):
        print(0)
        exit()
for i in range(0,N-K):
    if((x[i]<0)):
        if(x[i+K-1]>0):
            ans = min(ans,abs(x[i])*2+x[i+K-1],abs(x[i])+x[i+K-1]*2)
        else:
            ans = min(ans,abs(x[i])*2)
    else:
            ans = min(ans,x[i+K-1])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
x = list(map(int,input().split()))
ans = float('inf')
if(0 in x):
    K=K-1
for i in range(0,N-K):
    if((x[i]<0)):
        if(x[i+K-1]>0):
            ans = min(ans,abs(x[i])*2+x[i+K-1],abs(x[i])+x[i+K-1]*2)
        else:
            ans = min(ans,abs(x[i])*2)
    else:
            ans = min(ans,x[i+K-1])
print(ans)

Submission status: Wrong Answer

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

x=[int(x) for x in input().rstrip().split()]


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

Submission status: Wrong Answer

Solution code:
import bisect

n, k, *lst = map(int, open(0).read().split())
z = bisect.bisect_left(lst, 0)
if not z:
  print(lst[z + k - 1])
  quit()

res = float('inf')
for i in range(max(z - k + 1, 0), min(z, n - k) + 1):
  tmp = lst[i + k - 1] - lst[i] + min(abs(lst[i]), lst[i + k - 1])
  if tmp < res:
    res = tmp
print(res)

Submission status: Wrong Answer

Solution code:
N, K = map( int, input().split() )
x = list( map( int, input().split() ))
 
ret = 10 ** 18
for i in range( N - K + 1 ):
  s = x[ i: K + i ]
  l, r = s[ 0 ], x[ - 1 ]
  ret = min( ret, abs( l - r ) + min( abs( r ), abs( l ) ) )
print( ret )

Submission status: Wrong Answer

Solution code:
N, K = map( int, input().split() )
x = list( map( int, input().split() ))
 
x = sorted( x, key = lambda x: abs(x) )[ : K ]
x.sort()
 
m, M = x[ 0 ], x[ - 1 ]
print( abs( M - m ) + min( abs( m ), abs( M ) ))

Submission status: Wrong Answer

Solution code:
N, K = map( int, input().split() )
x = list( map( int, input().split() ))
x.sort( key = lambda x: abs(x) )
x = x[ : K ]
x.sort()
m = x[ 0 ]
M = x[ K - 1 ]
 
print( abs( M - m ) + min( abs( m ), abs( M ) ))

Submission status: Wrong Answer

Solution code:
N, K = map( int, input().split() )
x = list( map( int, input().split() ))
x.sort( key = lambda x: abs(x) )
x = x[ : K ]
x.sort()
m = x[ 0 ]
M = x[ K - 1 ]

print( M - m + min( abs( m ), abs( M ) ))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
list_X = list(map(int, input().split()))
ans = pow(10, 18)
for i in range(n-k):
    ans = min(ans, abs(list_X[i])+list_X[i+k-1]-list_X[i], abs(list_X[i+k-1])+list_X[i+k-1]-list_X[i])
if n == 1:
    ans = list_X[0]
print(ans)

Submission status: Wrong Answer

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

if n == 1:
    ans = list_X[0]
print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import *
n,k=map(int,input().split())
c=list(map(int,input().split()))
a=bisect_left(c,0)
insort_left(c,0)
j=a
while 1:
  if j+1<=n:
    if c[j+1]==0:
      k-=1
      j+=1
    else:
      break
  else:
    break
ans=10**18
for i in range(k+1):
  if a+i<n and a-k+i>=0:
    ans=min(ans,min(c[a+i],-c[a-k+i])*2+max(c[a+i],-c[a-k+i]))
if ans==10**18:
  print(0)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import *
n,k=map(int,input().split())
c=list(map(int,input().split()))
a=bisect_left(c,0)
insort_left(c,0)
j=a
while 1:
  if j+1<=n:
    if c[j+1]==0:
      k-=1
      j+=1
    else:
      break
  else:
    break
ans=10**18
for i in range(k):
  if a+i<n and a-k+i>=0:
    ans=min(ans,min(c[a+i],-c[a-k+i])*2+max(c[a+i],-c[a-k+i]))
if ans==10**18:
  print(0)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
g=bisect(x,0)
if x[n-1]<=0 or x[0]>=0:
    print(max(abs(x[k-1]),abs(x[0])))
else:
    if g>=k:
        s=g-k
    else:
        s=0
    t=0
    while s<=n-k:
        if x[s]<0 and x[s+k-1]>0:
            if t==0:
                t=min(2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
            else:
                t=min(t,2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
        else:
            t=min(t,max(abs(x[s]),abs(x[s+k-1])))
        s+=1
    print(t)

Submission status: Wrong Answer

Solution code:
def resolve():
    from bisect import bisect
    n,k=map(int,input().split())
    x=list(map(int,input().split()))
    g=bisect(x,0)
    if x[k-1]<=0 or x[0]>=0:
        print(max(abs(x[k-1]),abs(x[0])))
    else:
        if g>=k:
            s=g-k
        else:
            s=0
        t=0
        while s<=n-k:
            if x[s]<0 and x[s+k-1]>0:
                if t==0:
                    t=min(2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
                else:
                    t=min(t,2*abs(x[s])+x[s+k-1],abs(x[s])+2*x[s+k-1])
            else:
                t=min(t,max(abs(x[s]),abs(x[s+k-1])))
            s+=1
        print(t)

resolve()

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

if(N > 1):        
    print(ans)
else:
    print(a[0])

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

ans = float('inf')
for i in range(N-K+1):
  left = X[i]; right = X[i+K-1]
  if left >= 0:
    ans = min(ans, right)
    break
  else:
    t = min(-left, right)*2 + max(-left, right)
    ans = min(ans, t)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
xminus =[]
xzero = []
xplus = []
for i in range(n):
    if x[i] < 0:
        xminus.append(x[i])
    if x[i] == 0:
        xzero.append(x[i])
    if x[i] > 0:
        xplus.append(x[i])
k -= len(xzero)
if k == 0:
    print(0)
    exit()
mint = 10000000000
if xminus:
    for i in range(max(1,k-len(xplus)),min(k,len(xminus))):
        t = -2*xminus[-i] + xplus[k-i-1]
        mint = min(t,mint)
if xplus:
    for i in range(max(1,k-len(xminus)),min(k,len(xplus))):
        t = 2*xplus[i-1] - xminus[-k+i]
        mint = min(t,mint)
if k <= len(xplus):
    mint = min(xplus[k-1],mint)
if k <= len(xminus):
    mint = min(-xminus[-k],mint)
print(mint)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
xminus =[]
xzero = []
xplus = []
for i in range(n):
    if x[i] < 0:
        xminus.append(x[i])
    if x[i] == 0:
        xzero.append(x[i])
    if x[i] > 0:
        xplus.append(x[i])
k -= len(xzero)
if k == 0:
    print(0)
    exit()
mint = 1000000
if xminus:
    for i in range(max(1,k-len(xplus)),min(k,len(xminus))):
        t = -2*xminus[-i] + xplus[k-i-1]
        mint = min(t,mint)
if xplus:
    for i in range(max(1,k-len(xminus)),min(k,len(xplus))):
        t = 2*xplus[i-1] - xminus[-k+i]
        mint = min(t,mint)
if k <= len(xplus):
    mint = min(xplus[k-1],mint)
if k <= len(xminus):
    mint = min(-xminus[-k],mint)
print(mint)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
xminus =[]
xzero = []
xplus = []
for i in range(n):
    if x[i] < 0:
        xminus.append(x[i])
    if x[i] == 0:
        xzero.append(x[i])
    if x[i] > 0:
        xplus.append(x[i])
k -= len(xzero)
if k == 0:
    print(0)
    exit()
mint = 1000000
if xminus:
    for i in range(max(1,k-len(xplus)),min(k,len(xminus))):
        t = -2*xminus[-i] + xplus[k-i-1]
        mint = min(t,mint)
if xplus:
    for i in range(max(1,k-len(xminus)),min(k,len(xplus))):
        t = 2*xplus[i-1] - xminus[-k+i]
        mint = min(t,mint)
if k <= len(xplus):
    mint = min(xplus[k-1],mint)
if k <= len(xminus):
    mint = min(xminus[-k],mint)
print(mint)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
a = A[0]
b = A[K-1]
ans = 0
if a*b < 0:
    ans = abs(min(a, b)*2)+abs(max(a, b))
else:
    ans = max(abs(a), abs(b))
for i in range(1, N-K+1):
    tmp = 0
    a = A[i]
    b = A[i+K-1]
    if a*b < 0:
        tmp = abs(min(a, b)*2)+abs(max(a, b))
    else:
        tmp = max(abs(a), abs(b))
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7


def main():
    # sys.stdin = open("sample.txt")
    N, K = Scanner.map_int()
    X = Scanner.map_int()
    left = 0
    right = K - 1
    ans = int(1e09)
    while right < N:
        if Math.sign(X[left]) == Math.sign(X[right]):
            ans = min(ans, abs(X[left] - X[right]))
        else:
            ans = min(ans, abs(X[left]) * 2 + abs(X[right]))
            ans = min(ans, abs(X[left]) + abs(X[right]) * 2)
        left += 1
        right += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

for i in range(N - K + 1):
    x1 = abs(X[i]) + abs(X[K - 1 + i] - X[i])
    x2 = abs(X[K - 1 + i]) + abs(X[i] - X[K - 1 + i])
    
    x_tmp = min(x1, x2)
    x = min(x, x_tmp)

print(x)

Submission status: Wrong Answer

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

md = 10**10
for xl, xr in zip(X[:K], X[K - 1:]):
    if xl >= 0:
        d = xr
    elif xr <= 0:
        d = -xl
    else:
        d = min(-xl, xr) * 2 + max(-xl, xr)

    if d < md:
        md = d
print(md)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
y=[0]*n
for i in range(n):
    y[i]=-x[n-1-i]
#print(y)

if x[0]>=0:print(x[k-1])
elif x[-1]<=0:print(abs(x[n-k]))
else:
    c=bisect.bisect_left(x,0)
    d=bisect.bisect_right(x,0)
    ans=float('inf')
    if c+k-1<=n-1:
        ans=abs(x[c+k-1])
    if d-k>=0:
        ans=min(ans,abs(x[d-k]))
    for i in range(n-1,c-1,-1):
        ans1=x[i]+2*abs(x[i-k+1])
        #print(ans1)
        if ans1<ans:
            ans=ans1
    #print(ans)
if y[0]<0 and y[-1]>0:
    c=bisect.bisect_left(y,0)
    d=bisect.bisect_right(y,0)
    Ans=float('inf')
    if c+k-1<=n-1:
        Ans=abs(y[c+k-1])
    if d-k>=0:
        Ans=min(Ans,abs(y[d-k]))
    for i in range(n-1,c-1,-1):
        Ans1=y[i]+2*abs(y[i-k+1])
        #print(Ans1)
        if Ans1<Ans:
            Ans=Ans1
    #print(Ans)
    print(min(Ans,ans))

Submission status: Wrong Answer

Solution code:
import bisect
n,k=map(int,input().split())
x=list(map(int,input().split()))
y=[0]*n
for i in range(n):
    y[i]=-x[n-1-i]
#print(y)

if x[0]>=0:print(x[k-1])
elif x[-1]<=0:print(abs(x[n-k]))
else:
    c=bisect.bisect_left(x,0)
    d=bisect.bisect_right(x,0)
    ans=float('inf')
    if c+k-1<=n-1:
        ans=abs(x[c+k-1])
    if d-k>=0:
        ans=min(ans,abs(x[d-k]))
    for i in range(n-1,c-1,-1):
        ans1=x[i]+2*abs(x[i-k+1])
        #print(ans1)
        if ans1<ans:
            ans=ans1
    #print(ans)

if y[0]>=0:print(y[k-1])
elif y[-1]<=0:print(abs(y[n-k]))
else:
    c=bisect.bisect_left(y,0)
    d=bisect.bisect_right(y,0)
    Ans=float('inf')
    if c+k-1<=n-1:
        Ans=abs(y[c+k-1])
    if d-k>=0:
        Ans=min(Ans,abs(y[d-k]))
    for i in range(n-1,c-1,-1):
        Ans1=y[i]+2*abs(y[i-k+1])
        #print(Ans1)
        if Ans1<Ans:
            Ans=Ans1
    #print(Ans)
    print(min(Ans,ans))

Submission status: Wrong Answer

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

if x[0]>=0:print(x[k-1])
elif x[-1]<=0:print(abs(x[n-k]))
else:
    c=bisect.bisect_left(x,0)
    d=bisect.bisect_right(x,0)
    ans=float('inf')
    if c+k-1<=n-1:
        ans=abs(x[c+k-1])
    if d-k>=0:
        ans=min(ans,abs(x[d-k]))
    for i in range(n-1,c-1,-1):
        ans1=x[i]+2*abs(x[i-c])
        #print(ans1)
        if ans1<ans:
            ans=ans1
    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

a = []
for i in range(1,n):
    a.append(x[i] - x[i-1])
acc = [0] + list(accumulate(a))
ans = 10**9
for i in range(n-k+1):
    tmp = abs(x[i]) + acc[i+k-1] - acc[i]
    if x[i+k-1] == 0:
        tmp -= abs(x[i])
    ans = min(tmp, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate


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

a = []
for i in range(1,n):
    a.append(x[i] - x[i-1])
acc = [0] + list(accumulate(a))

ans = 10**9
for i in range(n-k+1):
    ans = min(abs(x[i]) + acc[i+k-1] - acc[i], ans)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

abx=max(sorted(map(abs,x))[:K])

x=list(filter(lambda x:x>=-abx and x<=abx,x))

a=[]
for i in range(len(x)-K+1):
  a.append(x[K-1+i]-x[i]+abs(x[i]))
print(min(a))

Submission status: Wrong Answer

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

for _ in range(N):
    tmp = x.pop(0)
    if tmp < 0:
        y.insert(0, -tmp)
    else:
        x.insert(0, tmp)
        x.insert(0, 0)
        y.insert(0, 0)
        break

idx = min(K, len(x)-1)
ans = 10**6
for i in range(idx):
    if i > len(y)-1:
        break
    else:
        ans = min(ans, x[idx-i]*2+y[i], x[idx-i]+y[i]*2)
      
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    x = list(map(int, input().split()))
    ans = 100000000
    for i in range(n - k + 1):
        l, r = x[i], x[i + k - 1]
        if l < 0 and r > 0:
            d = max(abs(l), abs(r)) + min(abs(l), abs(r)) * 2
        else:
            d = max(abs(l), abs(r))
        ans = min(d, ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

now=0
ans=0
plus=[]
minus=[]

for i in range(n):
	if x[i]>0:
		plus = x[i:n]
		minus = x[0:i]
		break

max_plus = 0
min_minus = 0
for g in range(k):
	if plus==[] and minus==[]:
		break
	if plus==[]:
		min_minus = minus[-1]
		minus.pop()
	elif minus==[]:
		max_plus = plus[0]
		plus.pop(0)
	else:
		if abs(minus[-1]) < plus[0]:
			min_minus = minus[-1]
			minus.pop()
		else:
			max_plus = plus[0]
			plus.pop(0)

if max_plus > abs(min_minus):
	print(max_plus - 2*min_minus)
else:
	print(2*max_plus - min_minus)

Submission status: Wrong Answer

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

now=0
ans=0
plus=[]
minus=[]

for i in range(n):
	if x[i]>0:
		plus = x[i:n]
		minus = x[0:i]
		break

max_plus = 0
min_minus = 0
for g in range(k):
	if plus==[] and minus==[]:
		break
	if plus==[]:
		min_minus = minus[-1]
		minus.pop()
	elif minus==[]:
		max_plus = plus[0]
		plus.pop(0)
	else:
		if abs(minus[-1]) < plus[0]:
			min_minus = minus[-1]
			minus.pop()
		else:
			max_plus = plus[0]
			plus.pop(0)

if max_plus > min_minus:
	print(max_plus - 2*min_minus)
else:
	print(2*max_plus - min_minus)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#ARC101C
import sys

#n= int(input())
tmp = input().split()
n,k = list(map(lambda a: int(a), tmp))
tmp = input().split()
x = list(map(lambda a: int(a), tmp))

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#ARC101C
import sys

#n= int(input())
tmp = input().split()
n,k = list(map(lambda a: int(a), tmp))
tmp = input().split()
x = list(map(lambda a: int(a), tmp))

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

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
mi = x[-1] - x[0] + min(abs(x[-1]),abs(x[0]))
if n == 1:
    print("a")
    print(abs(x[0]))
    exit()
while i <= n - k:
    if mi >= ( x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]),abs(x[i])):
        mi = (x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]),abs(x[i]))
    i += 1
print(mi)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
mi = x[-1] - x[0] + min(abs(x[-1]),abs(x[0]))
if n == 1:
    print("a")
    print(abs(x[0]))
    exit()
while i <= n - k:
    print("{} {}".format(x[i + k - 1],  x[i]))
    if mi >= ( x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]),abs(x[i])):
        mi = (x[i + k - 1] - x[i]) + min(abs(x[i + k - 1]),abs(x[i]))
    i += 1
print(mi)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
mi = x[-1] - x[0]
if n == 1:
    print(abs(x[0]))
    exit()
while i <= n - k:
    if mi >= x[i + k - 1] - x[i] + min(abs(x[i + k - 1]),abs(x[i])):
        mi = x[i + k - 1] - x[i] + min(abs(x[i + k - 1]),abs(x[i]))
    i += 1
print(mi)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
mi = x[-1] - x[0]
while i <= n - k:
    if mi > x[i + k - 1] - x[i] + min(abs(x[i + k - 1]),abs(x[i])):
        mi = x[i + k - 1] - x[i] + min(abs(x[i + k - 1]),abs(x[i]))
    i += 1
print(mi)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
i = 0
mi = x[-1] - x[0]
while i <= n - k:
    temp = x[i:i + k]
    if mi > temp[-1] - temp[0] + min(abs(temp[-1]),abs(temp[0])):
        mi = temp[-1] - temp[0] + min(abs(temp[-1]),abs(temp[0]))
    i += 1
print(mi)

Submission status: Wrong Answer

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

ans = [10**6] * N

for i in range(N-K+1):
    tmp = x[i+K-1] - x[i] + abs(x[i])
    ans[i] = tmp

ans = min(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
X = list(map(int,input().split()))
fu = 0
sei = 0
for i in range(N):
    if X[i] < 0:
        fu += 1
    else:
        sei += 1
#X[0]からX[fu-1]までは負なのだよ(ただしfu >= 1)
#このときX[fu]からX[N-1]までは0以上なのだよ
#X[-1] >= 0ならfu+1通りのシミュレーションをすればいいのだよ

if fu == 0:
    print(str(X[K-1]))
else:
    kouho = []
    for i in range(fu):
        # fu-i個の負数をさらうよ(K-(fu-i)個の0以上の数をさらうよ) X[fu]で1個, X[fu + K-(fu-i) - 1]でK-(fu-i)個
        # fu-i+sei >= Kじゃないとだめだよ
        if(fu-i+sei < K):
            break
        else:
            kouho.append(min(2*abs(X[i])+abs(X[fu + K-(fu-i) - 1]), abs(X[i])+abs(2*X[fu + K-(fu-i) - 1])))
    if sei >= K:
        kouho.append(X[fu+K-1])
    print(str(min(kouho)))

Submission status: Wrong Answer

Solution code:
candle_num,heat_num=map(int,input().split())
candle_list=list(map(str,input().split()))
current_locatino=0
ans=10000000
min=0

def candle_2list_function(function_x,function_num):
    candles_set = []
    candles = []
    for i in function_x:
        candles.append(i)
        if(len(candles)==function_num):
            candles_set.append(list(candles))
            candles.pop(0)

    return candles_set

candle_2list=candle_2list_function(candle_list,heat_num)

for i in range(candle_num-heat_num+1):
    left=int(candle_2list[i][0])
    right=int(candle_2list[i][heat_num-1])
    if left<0 and right<0:
        min=abs(right)+abs(right-left)
    elif left>0 and right>0:
        min=abs(left)+abs(right-left)
    elif left>right:
        min=abs(right)+abs(right-left)
    else:
        min=abs(left)+abs(right-left)
    if ans>min:
        ans=min

print(ans)

Submission status: Wrong Answer

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

T = abs(x[K-1]-x[0]) + min((abs(x[K-1]),abs(x[0])))
if N != K:
    for i in range(N - K):
        T1 = abs(x[K+i-1]-x[i]) + min((abs(x[K+i-1]),abs(x[i])))
        if T > T1:
            T = T1
print(T)

Submission status: Wrong Answer

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

x = [int(x) for x in input().split()]

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

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):
    L = x[i]
    R = x[i + K - 1]
    if R < 0:
        ans = min(ans, -L)
    if L < 0 <= R:
        ans = min(ans, -2 * L + R, -L + 2 * R)
    else:
        ans = min(ans, R)

print(ans)