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 = [int(i) for i in input().split()]
ans = 10**9
pre = X[0]
diff = []
for i in range(1,n):
    if pre <0 and X[i]<0:
        tmp_diff = abs(pre)-abs(X[i])
    elif pre < 0 and X[i]>0:
        tmp_diff = X[i]-pre
    else:
        tmp_diff = abs(X[i]) - abs (pre)
    pre = X[i]
    diff.append(tmp_diff)

for i in range(n-k+1):
    tmp_ans = abs(X[i])
    tmp_ans += sum(diff[i:i+k-1])
    ans = min(ans,tmp_ans) 
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
X = [int(i) for i in input().split()]
ans = 10**9
for i in range(n-k+1):
    tmp_pre = 0
    tmp_ans = 0
    for index in X[i:i+k]:
        if tmp_pre<0 and index>0:
            tmp_ans += index-tmp_pre
        else:
            tmp_ans += abs(abs(tmp_pre) - abs(index))
        tmp_pre = index
    ans = min(ans,tmp_ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
left = []
right = []
cnt = 0
for i in range(N):
    if x[i] < 0:
        left += [-x[i]]
    elif x[i] > 0:
        right += [x[i]]
    else:
        cnt += 1
rest = K - cnt
left = left[::-1]
if rest != 0:
    ans = float('inf')
else:
    ans = 0
for i in range(rest):
    if i == 0 and len(right) > rest:
        ans = min(ans, right[rest-1])
    elif rest-i == 0 and len(left) > i:
        ans = min(ans, left[rest-1])
    elif len(left) > i and len(right) > rest-i:
        ans = min(ans, left[i-1]+right[rest-i-1]+min(left[i-1], right[rest-i-1]))

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x = list(map(int, input().split()))
ans = 10000
for i in range(N - K + 1):
  cost1 = abs(x[i]) + abs(x[i + K - 1] - x[i])
  if cost1 < ans:
    ans = cost1
  cost2 = abs(x[i + K - 1]) + abs(x[i + K - 1] - x[i])
  if cost2 < ans:
    ans = cost2
print(ans)

Submission status: Wrong Answer

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

Po = []
Ne = []
Ze = []
for i in range(N):
    if X[i]<0:
        Ne.append(-1*X[i])
    elif X[i]>0:
        Po.append(X[i])
    else:
        Ze.append(X[i])
Ne = Ne[::-1]
K = K-len(Ze)

out = 10**10
if len(Po)>=K and K>=1:
    out = min(out,Po[K-1]) #全部正
if len(Ne)>=K and K>=1:
    out = min(out,Ne[K-1]) #全部負

r = min(K,len(Po)) #正からr個
l = K-r#負からl個
while len(Po)+1>=r>=2 and len(Ne)-1>=l>=0:
    r += -1
    l += 1
    A = Po[r-1]
    B = Ne[l-1]
    C = min(2*A+B,2*B+A)
    out = min(out,C)
if out==10**10:
    print(0)
else:
    print(out)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
if l[0]<=0 and l[k-1]<=0:
  print(abs(l[0]))
elif l[0]>=0 and l[k-1]>=0:
  print(l[k-1])
else:
  mi=min(l[k-1],abs(l[0]))
  ma=max(l[k-1],abs(l[0]))
  print(mi*2+ma)

Submission status: Wrong Answer

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

# check
if K == 1:
    x.sort(key=lambda i: abs(i))
    print(x[0])
else:
    mov = [
        sum(
            [
                abs(x[j])
                if j == i
                else
                abs(x[j] - x[j - 1])
                for j in range(i, i + K)
            ]
        )
        for i in range(0, N - (K - 1))
    ] + [
        sum(
            [
                abs(x[j])
                if j == i
                else
                abs(x[j + 1] + x[j])
                for j in range(i, i - K, -1)
            ]
        )
        for i in range((K - 1), N)
    ]

    print(min(mov))

Submission status: Wrong Answer

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

    ans = 2*pow(10, 8)
    for i in range(N-K+1):
        L = x[i]
        R = x[i+K-1]
        if R < 0:
            ans = min(ans, abs(L))
        elif L <= 0 <= R:
            ans = min(2*abs(R)+abs(L), 2*abs(L)+abs(R), ans)
        elif 0 < L:
            ans = min(ans, abs(R))
        else:
            continue
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,k=map(int,input().split())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]>=0:
        s=i
        break
else:
  s=n
res=10**9
if s+k-1<n:
  res=x[s+k-1]
if s-k>0 and res>abs(x[s-k]):
  res=abs(x[s-k])
for i in range(1,k):
    if s+k-i-1>n-1 or s-i<0:
        continue
    if x[s+k-i-1]+2*abs(x[s-i])<res:
        res=x[s+k-i-1]+2*abs(x[s-i])
for i in range(1,k):
    if s-k+i<0 or s+i-1>n-1:
        continue
    if abs(x[s-k+i])+2*x[s+i-1]<res:
        res=abs(x[s-k+i])+2*x[s+i-1]
print(res)

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,k=map(int,input().split())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]>=0:
        s=i
        break
res=10**9
if s+k-1<n:
  res=x[s+k-1]
if s-k>0 and res>abs(x[s-k]):
  res=abs(x[s-k])
for i in range(1,k):
    if s+k-i-1>n-1 or s-i<0:
        continue
    if x[s+k-i-1]+2*abs(x[s-i])<res:
        res=x[s+k-i-1]+2*abs(x[s-i])
for i in range(1,k):
    if s-k+i<0 or s+i-1>n-1:
        continue
    if abs(x[s-k+i])+2*x[s+i-1]<res:
        res=abs(x[s-k+i])+2*x[s+i-1]
print(res)

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,k=map(int,input().split())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]>=0:
        s=i
        break
res=10**9
if s+k-1<n:
  res=x[s+k-1]
if s-k>0 and res<abs(x[s-k]):
  res=abs(x[s-k])
for i in range(1,k):
    if s+k-i-1>n-1 or s-i<0:
        continue
    if x[s+k-i-1]+2*abs(x[s-i])<res:
        res=x[s+k-i-1]+2*abs(x[s-i])
for i in range(1,k):
    if s-k+i<0 or s+i-1>n-1:
        continue
    if abs(x[s-k+i])+2*x[s+i-1]<res:
        res=abs(x[s-k+i])+2*x[s+i-1]
print(res)

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,k=map(int,input().split())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]>=0:
        s=i
        break
res=10**9
if s+k-1<n:
  res=x[s+k-1]
if s-k>0 and res<x[s-k]:
  res=x[s-k]
for i in range(1,k):
    if s+k-i-1>n-1 or s-i<0:
        continue
    if x[s+k-i-1]+2*abs(x[s-i])<res:
        res=x[s+k-i-1]+2*abs(x[s-i])
for i in range(1,k):
    if s-k+i<0 or s+i-1>n-1:
        continue
    if abs(x[s-k+i])+2*x[s+i-1]<res:
        res=abs(x[s-k+i])+2*x[s+i-1]
print(res)

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,k=map(int,input().split())
x=list(map(int,input().split()))
s=0
for i in range(n):
    if x[i]>=0:
        s=i
        break
res=10**9
if s+k-1<n:
  res=x[s+k-1]
if s-k>0 and res<x[s-k]:
  res=x[s-k]
for i in range(1,k):
    if s+k-i-1>n-1 or s-i<0:
        continue
    if x[s+k-i-1]+2*abs(x[s-i])<res:
        res=x[s+k-i-1]+2*abs(x[s-i])
for i in range(1,k):
    if s-k-i<0 or s+i-1>n-1:
        continue
    if abs(x[s-k-i])+2*x[s+i-1]<res:
        res=abs(x[s-k-i])+2*x[s+i-1]
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        dis = X[n:n + K]
        l = abs(min(dis)) * 2 + abs(max(dis))
        r = abs(max(dis)) * 2 + abs(min(dis))
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)
    N += 1

MIN = 2 * 10**8
for i in range(K+1):
    if s - K + i >= 0 and s + i <N:
        f = X[s -K+i]
        l = X[s+i]
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        MIN =min(MIN,ans)

print(MIN)

Submission status: Wrong Answer

Solution code:
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)
    N += 1

MIN = 2 * 10**8
for i in range(K+1):
    if s - K + i >= 0 and s + i <N:
        f = X[s -K+i]
        l = X[s+i]
        print(f,l)
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        print(ans)
        MIN =min(MIN,ans)

print(MIN)

Submission status: Wrong Answer

Solution code:
import bisect

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

s = bisect.bisect_left(X,0)
if 0 in X:
    K -= 1
else:
    bisect.insort_left(X,0)

MIN = 2 * 10**8
for i in range(K+1):
    if s - K + i >= 0 and s + i <N+1:
        f = X[s -K+i]
        l = X[s+i]
        if abs(f) < abs(l):
            ans = abs(f) * 2 + abs(l)
        else:
            ans = abs(l) * 2 + abs(f)
        MIN =min(MIN,ans)

print(MIN)

Submission status: Wrong Answer

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

ans=max(max(x)-min(x),max(x),abs(min(x)))

for i in range(N-K+1):
    ans=min(ans,x[i+K-1]-x[i]+min(abs(x[i]),abs(x[i+K-1])))
#    print(i,i+K-1,x[i],x[i+K-1],x[i+K-1]-x[i]+min(abs(x[i]),abs(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=max(x)-min(x)

for i in range(N-K+1):
    ans=min(ans,x[i+K-1]-x[i]+min(abs(x[i]),abs(x[i+K-1])))
#    print(i,i+K-1,x[i],x[i+K-1],x[i+K-1]-x[i]+min(abs(x[i]),abs(x[i+K-1])))
if N==1:
    ans=x[0]
print(ans)

Submission status: Wrong Answer

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

ans=max(x)-min(x)

for i in range(N-K+1):
    ans=min(ans,x[i+K-1]-x[i]+min(abs(x[i]),abs(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=max(x)-min(x)

for i in range(N-K):
    ans=min(ans,x[i+K-1]-x[i]+min(abs(x[i]),abs(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")
for i in range(N-K+1):
  l = i
  r = i + K - 1
  if X[l] * X[r] < 0:
    ans = min(ans, 2 * min(abs(X[l]), abs(X[r])) + max(abs(X[l]), abs(X[r])))
  else:
    ans = max(ans, min(abs(X[l]), abs(X[r])))
print(ans)

Submission status: Wrong Answer

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

global_min = float('inf')
for i in range(N-K):
    left = i
    right = i+K-1

    if 0 <= Xs[left]:
        local_min = Xs[right]
    elif Xs[right] <= 0:
        local_min = abs(Xs[left])
    else: # 先にマイナス側に行く場合と、先にプラス場合に行く場合がある。
        tmp1 = 2 * abs(Xs[left]) + Xs[right]
        tmp2 = abs(Xs[left]) + 2 * Xs[right]
        local_min = min(tmp1, tmp2)

    global_min = min(global_min, local_min)

if global_min == float('inf'):
    print(0)
else:
    print(global_min)

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    # from math import ceil, floor, gcd
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7

    N,K = LMIIS()
    X = LMIIS()


    #left:X[0] right:X[K-1]
    left = 0
    right = K-1
    syaku = abs(X[right] - X[left])
    if abs(X[left]) > abs(X[right]):
        min_time = syaku + abs(X[right])
    else:
        min_time = syaku + abs(X[left])


    for i in range(N-K):
        syaku = abs(X[right] - X[left])
        if abs(X[left]) > abs(X[right]):
            min_time = min(min_time, syaku + abs(X[right]))
        else:
            min_time = min(min_time, syaku + abs(X[left]))
        
        left += 1
        right += 1
    
    print(min_time)



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    # from math import ceil, floor, gcd
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10

    N,K = LMIIS()
    X = LMIIS()


    #left:X[0] right:X[K-1]
    left = 0
    right = K-1
    syaku = X[right] - X[left]
    if abs(X[left]) > abs(X[right]):
        min_time = syaku + abs(X[right])
    else:
        min_time = syaku + abs(X[left])


    for i in range(N-K):
        syaku = X[right] - X[left]
        if abs(X[left]) > abs(X[right]):
            min_time = min(min_time, syaku + abs(X[right]))
        else:
            min_time = min(min_time, syaku + abs(X[left]))
        
        left += 1
        right += 1
    
    print(min_time)



    
main()

Submission status: Wrong Answer

Solution code:
def main():
    from sys import setrecursionlimit, stdin
    from os import environ
    from collections import defaultdict, deque, Counter
    # from math import ceil, floor, gcd
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**9+10

    N,K = LMIIS()
    X = LMIIS()



    D = [0]*(N-1)
    pre = X[0]
    for i in range(1,N):
        D[i-1] = X[i] - pre
        pre = X[i]

    #left:X[0] right:X[K-1]
    left = 0
    right = K-1
    syaku = X[right] - X[left]
    if abs(X[left]) > abs(X[right]):
        min_time = syaku + abs(X[right])
    else:
        min_time = syaku + abs(X[left])


    for i in range(N-K):
        if abs(X[left]) > abs(X[right]):
            min_time = min(min_time, syaku + abs(X[right]))
        else:
            min_time = min(min_time, syaku + abs(X[left]))
        syaku = syaku + (X[right+1] - X[right]) - (X[left+1] - X[left])
        left += 1
        right += 1
    
    print(min_time)



    
main()

Submission status: Wrong Answer

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

ans = 2 * 10 ** 9

if k == 1:
    for i in range(n):
        ans = min(ans, abs(x[i]))
elif n == k:
    ans = min(abs(x[0]-x[n-1]) + abs(x[n-1]), abs(x[0]) + abs(x[0] - x[n-1]))
else:
    for i in range(n-k):
        ans = min(ans, abs(x[i]-x[i+k-1]) + abs(x[i+k-1]), abs(x[i]) + abs(x[i] - 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 = 10 ** 9

if k == 1:
    for i in range(n):
        ans = min(ans, abs(x[i]))
elif n == k:
    ans = min(abs(x[0]-x[n-1]) + abs(x[n-1]), abs(x[0]) + abs(x[0] - x[n-1]))
else:
    for i in range(n-k):
        ans = min(ans, abs(x[i]-x[i+k-1]) + abs(x[i+k-1]), abs(x[i]) + abs(x[i] - 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 = 10 ** 9

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

print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

n, k = map(int, input().split())
x = list(map(int, input().split()))
ans = 0
min_dis = 10 ** 20
a = 0
for i in range(n - k + 1):
    dis = x[i + k - 1] - x[i]
    if min_dis > dis:
        ans = dis + min(abs(x[i]), abs(x[i + k - 1]))
        min_dis = dis
print(ans)

Submission status: Wrong Answer

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

is_zero = l.count(0)
K -= is_zero

if K:
    ans = float('inf')
    for i in range(1, N-K+1):
        lb, rb = l[i], l[i+K-1]
        steps = abs(rb-lb) + min(abs(rb), abs(lb))
        ans = min(ans, steps)

    print(ans)
else:
    print(0)

Submission status: Wrong Answer

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


res=2*10**8+1

for i in range(n-k+1):
	l=x[i]
	r=x[i+k-1]
	if l*r>=0:
		res=min(res,max(abs(l),abs(r)))
	else:
		res=min(res,abs(l)+abs(r)+min(abs(l),abs(r)))
	
print(res)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
data = list(map(int, input().split()))
l = 0
r = k - 1
ans = float("inf")
while True:
    if r == n:
        break
    ans = min(ans, abs(data[l]) + abs(data[l] - data[r]), abs(data[r]) + abs(data[r]) - data[l])
    l += 1
    r += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
data = list(map(int, input().split()))
l = 0
r = k - 1
ans = 100000000000000000000000000
while True:
    if r == n:
        break
    ans = min(ans, abs(data[l]) + abs(data[l] - data[r]), abs(data[r]) + abs(data[r]) - data[l])
    l += 1
    r += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
data = list(map(int, input().split()))
l = 0
r = k - 1
ans = 1000000
while True:
    if r == n:
        break
    ans = min(ans, abs(data[l]) + abs(data[l] - data[r]), abs(data[r]) + abs(data[r]) - data[l])
    l += 1
    r += 1
print(ans)

Submission status: Wrong Answer

Solution code:
cand_num, tenka = map(int, input().split(" "))
candles = list(map(int, input().split(" ")))
mini = 10 ** 8

for i in range(cand_num - tenka + 1):
    if candles[i] * candles[tenka + i - 1] >= 0:
        temp = max(abs(candles[i]), abs(candles[tenka + i - 1]))
    else:
        temp = min(abs(candles[i]), abs(candles[tenka + i- 1])) * 2 + max(abs(candles[i]), abs(candles[tenka + i - 1]))
    mini = temp if mini > temp else mini
print(mini)

Submission status: Wrong Answer

Solution code:
cand_num, tenka = map(int, input().split(" "))
candles = list(map(int, input().split(" ")))
mini = 10 ** 8

for i in range(cand_num - tenka + 1):
    if candles[i] * candles[tenka - 1] >= 0:
        temp = max(abs(candles[i]), abs(candles[tenka + i - 1]))
    else:
        temp = min(abs(candles[i]), abs(candles[tenka + i- 1])) * 2 + max(abs(candles[i]), abs(candles[tenka + i - 1]))
    mini = temp if mini > temp else mini
print(mini)

Submission status: Wrong Answer

Solution code:
cand_num, tenka = map(int, input().split(" "))
candles = list(map(int, input().split(" ")))
mini = 10 ** 8

for i in range(cand_num - tenka + 1):
    if candles[i] * candles[tenka - 1] >= 0:
        temp = max(candles[i], candles[tenka + i - 1])
    else:
        temp = min(abs(candles[i]), abs(candles[tenka + i- 1])) * 2 + max(abs(candles[i]), abs(candles[tenka + i - 1]))
    mini = temp if mini > temp else mini
print(mini)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

ans = x_list[-1] - x_list[0]
i = bisect_left(x_list, 0)
for j in range(max(i - k, 0), min(i + 1, n - k + 1)):
    if j == i - k:
        temp = abs(x_list[j])
    elif j == i:
        temp = abs(x_list[j + k - 1])
    else:
        temp = x_list[j + k - 1] - x_list[j] + min(-x_list[j], x_list[j + k - 1])
    
    if temp < ans:
        ans = temp
print(ans)

Submission status: Wrong Answer

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

ptns = []
for i in range(n-k+1):
    ptns.append(l[i:i+k])

ans = 10**8
for j in ptns:
    ans = min(ans, abs(0 -j[-1]) + abs(j[-1] - j[0]), abs(0 - j[0]) + abs(j[0] - j[-1]))

print(ans)

Submission status: Wrong Answer

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

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
flag = True
for i in range(n+1):
    if i == n or x[i] > 0:
        x.insert(i,0)
        zahyou = i
        break
ans = 10**18
for i in range(k+1):
    flag = True
    if zahyou + i < n+1 and zahyou -k+i >= 0:
        mainasu = x[zahyou]-x[zahyou-k+i]
        purasu = x[zahyou+i]-x[zahyou]
        if mainasu != 0 and purasu != 0:
            purasu += min(mainasu,purasu)
            print(purasu,mainasu)
        ans = min(ans,mainasu+purasu)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
flag = True
for i in range(n+1):
    if i == n or x[i] > 0:
        x.insert(i,0)
        zahyou = i
        break
ans = 10**18
print(x)
for i in range(k+1):
    mainasu = 0
    purasu = 0
    flag = True
    for j in range(k):
        a = zahyou-k+i+j
        if  a >= 0 and a < n:
            if x[a] < 0:
                mainasu += x[a+1]-x[a]
            else:
                purasu += x[a+1]-x[a]
        else:
            flag = False
        print(a,purasu,mainasu,flag)
    if mainasu != 0 and purasu != 0:
        purasu += min(mainasu,purasu)
    if flag:
        ans = min(ans,mainasu+purasu)
print(ans)

Submission status: Wrong Answer

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

cost = float("inf")

for l in range(N-K+1):
    r = l+K-1
    from_left = abs(x[l]+abs(x[r]-x[l]))
    from_right = abs(x[r]+abs(x[l]-x[r]))
    cost = min(cost, from_left, from_right)
print(cost)

Submission status: Wrong Answer

Solution code:
import bisect
n,k = map(int,input().split())
x = list(map(int,input().split()))
zero = bisect.bisect(x,0)
left = [0] + (x[:zero])[::-1]
right = [0] + x[zero:]
ans = 10**7
for i in range(min(k+1,len(left))):
  j = k-i
  if j >= len(right):
    continue
  ans = min(ans,2*right[j]-left[i],-2*left[i]+right[j])
print(ans)

Submission status: Wrong Answer

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

ans=10**12
s=z-k+1
if s < 0:
  s=0
g=z+k-1
if g >= n or g==0:
  g=n

for i in range(s,g):
  if i+k > n:
    break
  t=x[i:i+k]
  xl=min(t)
  xr=max(t)
  ans=min(ans,abs(xr)+abs(xr-xl),abs(xl)+abs(xr-xl))
print(ans)

Submission status: Wrong Answer

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

ans=10**12
s=z-k+1
if s < 0:
  s=0
g=z+k-1
if g >= n or g==0:
  g=n

for i in range(s,g):
  if i+k > n:
    break
  t=x[i:i+k]
  if min(t)*max(t) > 0:
    ans=min(ans, abs(max(t)))
  else:
    tmp=abs(min(t))+max(t)
    tmp+=min(abs(min(t)),abs(max(t)))
    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=10**12
for i in range(n):
  if i+k <= n:
    t=x[i:i+k]
    if min(t)*max(t) > 0:
      ans=min(ans, abs(max(t)))
    else:
      ans=min(ans,min(abs(min(t)),max(t))*2+max(abs(min(t)),max(t)))
  else:
    break
    
print(ans)

Submission status: Wrong Answer

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

ans=10**12
for i in range(n):
  if i+k <= n:
    t=x[i:i+k]
    if min(t)*max(t) > 0:
      ans=min(ans, abs(max(t)))
    else:
      ans=min(ans,min(abs(min(t)),max(t))*2+max(abs(min(t)),max(t)))
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
x = list(map(int,input().split()))
from collections import deque
anslist = []
minus = 0
plus = 0
p = 1 if 0 in x else 0
for i in range(n):
    if x[i] > 0:
        minus = i
        plus = n-i+p
        break

if plus >= k:
    anslist.append(x[minus+k-1])
if minus >= k:
    anslist.append(x[minus-k])
right = n-1
left = 0
while abs(right-left) >= k:
    if abs(x[left]) >= abs(x[right]):
        left += 1
    else:
        right -= 1
#print(x[left:right+1])
anslist.append(min(abs(x[left]),abs(x[right]))*2+max(abs(x[left]),abs(x[right])))
print(min(anslist))

Submission status: Wrong Answer

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

if k == 1:
    print(min(abs(max(li)),abs(min(li))))
    exit()

""" tmp = li[:k]
a = tmp[0]
b = tmp[1]
c = tmp[-1]
time_li = []
for i in range(n-k+1):
    

 """
time_li = []
for i in range(n-k+1):
    #print(li[i:i+k]) [-30, -10, 10]
    tmp_time_li = []
    mi = li[i]
    ma = li[i+k-1]
    if mi*ma >0:
        tmp_time_li.append(ma)
    else:
        orikaeshi = min(abs(mi),abs(ma))*2+max(abs(mi),abs(ma))
        tmp_time_li.append(orikaeshi)
    time_li.append(tmp_time_li[0])
print(min(time_li))

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
li = list(map(int,input().split()))
time_li = []
for i in range(n-k+1):
    #print(li[i:i+k]) [-30, -10, 10]
    tmp_time_li = []
    tmp = li[i:i+k]
    mi = min(tmp)
    ma = max(tmp)
    if mi*ma >0:
        tmp_time_li.append(ma)
    else:
        orikaeshi = min(abs(mi),abs(ma))*2+max(abs(mi),abs(ma))
        tmp_time_li.append(orikaeshi)
    time_li.append(tmp_time_li[0])
print(min(time_li))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right, bisect_left

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

ans = 3*10**8 + 1
x_0 = bisect_right(x, 0)
x_1 = bisect_left(x, 0)

for i in range(N-K+1):
    if i + K -1 >= x_1 and i <= x_0:
        if x[i + K - 1] <= 0:
            ans = min(ans, -x[i])
        elif x[i] >= 0:
            ans = min(ans, x[i + K - 1])
        else:
            ans = min(ans, min(-x[i]*2 + x[i+K-1], -x[i] + x[i+K-1]*2))
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right, bisect_left

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

ans = 10**8 + 1
x_0 = bisect_right(x, 0)
x_1 = bisect_left(x, 0)

for i in range(N-K+1):
    if i + K -1 >= x_1 and i <= x_0:
        if x[i + K - 1] <= 0:
            ans = min(ans, -x[i])
        elif x[i] >= 0:
            ans = min(ans, x[i + K - 1])
        else:
            ans = min(ans, min(-x[i]*2 + x[i+K-1], -x[i] + x[i+K-1]*2))
print(ans)

Submission status: Wrong Answer

Solution code:
# Problem C - Candles

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

# initialization
ans = float('INF')
loop_count = N - K

# count
if N-K==0:
    loop_count = 1
for i in range(loop_count):
    f = x_nums[i]
    l = x_nums[i+K-1]
    if f<0:
        if l>=0:
            tmp_time_1 = abs(f)*2 + l
            tmp_time_2 = abs(l)*2 + abs(f)
            tmp_time = min(tmp_time_1, tmp_time_2)
            ans = min(ans, tmp_time)
        else:
            ans = min(ans, abs(f))
    else:
        ans = min(ans, l)

# output
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
xp=[xi for xi in x if xi>=0]
xm=[xi for xi in x if xi<0]
xm.sort(reverse=True)
ans=2*10**8
for i in range(k+1):# xpからi個のろうそくをつける
    if i>len(xp) or k-i>len(xm):
        continue
    a=xp[i-1] if len(xp)>0 and i-1 >-1 else 0
    b=xm[k-i-1] if len(xm)>0 and k-i-1 >-1 else 0
    ans=min(ans,2*a-b,a-2*b)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x=list(map(int,input().split()))
xp=[xi for xi in x if xi>=0]
xm=[xi for xi in x if xi<0]
xm.sort(reverse=True)
ans=2*10**8
for i in range(k+1):# xpからi個のろうそくをつける
    if i>len(xp) or k-i>len(xm):
        continue
    a=xp[i-1] if len(xp)>0 else 0
    b=xm[k-i-1] if len(xm)>0 else 0
    ans=min(ans,2*a-b,a-2*b)
print(ans)