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:

import sys
import bisect


def parse(file):
    n, k = map(int, file.readline().split())
    candles = map(int, file.readline().strip().split())

    return n, k, candles


def main(n, k, candles):
    candles = list(candles)

    i0 = bisect.bisect_left(candles, 0)

    if i0 == 0:  # 左端
        return candles[k - 1]
    if i0 == n:  # 右端
        return abs(candles[-k])

    i1 = i0
    i2 = i1 - 1
    
    if k == 1:
        return min(candles[i1], abs(candles[i2]))

    while True:
        if i1 - i2 + 1 >= k:
            break

        if i1 == n-1:
            i2 = n - k
            break
        else:
            j1 = i1 + 1

        if i2 == 0:
            i1 = k-1
            break
        else:
            j2 = i2 - 1

        if candles[j1] >= - candles[j2]:
            i2 = j2
        else:
            i1 = j1

    v1 = candles[i1]
    v2 = abs(candles[i2])
    return min(v1, v2) * 2 + max(v1, v2)


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Wrong Answer

Solution code:
import sys
import bisect


def parse(file):
    n, k = map(int, file.readline().split())
    candles = map(int, file.readline().strip().split())

    return n, k, candles


def main(n, k, candles):
    candles = list(candles)

    i0 = bisect.bisect_left(candles, 0)

    if i0 == 0:  # 左端
        return candles[k - 1]
    if i0 == n:  # 右端
        return abs(candles[-k])

    i1 = i0
    i2 = i1 - 1
    
    if k == 1:
        return min(candles[i1], abs(candles[i2]))

    while True:
        if i1 - i2 + 1 >= k:
            break

        if i1 == n-1:
            i2 = n - k
            break
        else:
            j1 = i1 + 1

        if i2 == 0:
            i1 = k-1
            break
        else:
            j2 = i2 - 1

        if candles[j1] >= - candles[j2]:
            i2 = j2
        else:
            i1 = j1

    v1 = candles[i1]
    v2 = abs(candles[i2])
    return min(v1 * 2 + v2, v1 + v2 * 2)


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Wrong Answer

Solution code:
import sys
import bisect


def parse(file):
    n, k = map(int, file.readline().split())
    candles = map(int, file.readline().strip().split())

    return n, k, candles


def main(n, k, candles):
    candles = list(candles)

    i0 = bisect.bisect_left(candles, 0)

    if i0 == 0:  # 左端
        return candles[k - 1]
    if i0 == n:  # 右端
        return abs(candles[-k])

    i1 = i0
    i2 = i1 - 1
    
    if k == 1:
        return min(candles[i1], abs(candles[i2]))

    if n == k:
        v1 = candles[i1]
        v2 = abs(candles[i2])
        return min(v1*2 + v2, v1+v2*2)

    while True:
        if i1 - i2 + 1 >= k:
            v1 = candles[i1]
            v2 = abs(candles[i2])
            return min(v1 * 2 + v2, v1 + v2 * 2)

        if i1 <= n - 2:
            j1 = i1 + 1
        else:
            j1 = i1

        if i2 >= 1:
            j2 = i2 - 1
        else:
            j2 = i2

        if candles[j1] >= - candles[j2]:
            i2 = j2
        else:
            i1 = j1


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Wrong Answer

Solution code:

import sys
import bisect


def parse(file):
    n, k = map(int, file.readline().split())
    candles = map(int, file.readline().strip().split())

    return n, k, candles


def main(n, k, candles):
    candles = list(candles)

    i0 = bisect.bisect_left(candles, 0)

    if i0 == 0:  # 左端
        return candles[k - 1]
    if i0 == n:  # 右端
        return abs(candles[-k])

    i1 = i0
    i2 = i1 - 1
    
    if candles[i0] == 0:
        k -= 1

    if k == 0:
        return 0

    if k == 1:
        return min(candles[i1], abs(candles[i2]))

    while True:
        if i1 - i2 + 1 >= k:
            v1 = candles[i1]
            v2 = abs(candles[i2])
            return min(v1 * 2 + v2, v1 + v2 * 2)

        if i1 <= n - 2:
            j1 = i1 + 1
        else:
            j1 = i1

        if i2 >= 1:
            j2 = i2 - 1
        else:
            j2 = i2

        if candles[j1] >= - candles[j2]:
            i2 = j2
        else:
            i1 = j1


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Wrong Answer

Solution code:
import sys
import bisect


def parse(file):
    n, k = map(int, file.readline().split())
    candles = map(int, file.readline().strip().split())

    return n, k, candles


def main(n, k, candles):
    candles = list(candles)

    i0 = bisect.bisect_left(candles, 0)

    if i0 == 0:  # 左端
        return candles[k - 1]
    if i0 == n:  # 右端
        return abs(candles[-k])

    i1 = i0
    i2 = i1 - 1
    
    if candles[i0] == 0:
        k -= 1

    if k == 0:
        return 0

    if k == 1:
        return min(candles[i1], abs(candles[i2]))

    while True:
        if i1 - i2 + 1 >= k:
            v1 = candles[i1]
            v2 = abs(candles[i2])
            return min(v1 * 2 + v2, v1 + v2 * 2)

        if i1 <= n - 2:
            j1 = i1 + 1
        else:
            j1 = i1

        if i2 >= 1:
            j2 = i2 - 1
        else:
            j2 = i2

        if candles[j1] >= - candles[j2]:
            i2 = j2
        else:
            i1 = j1


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Wrong Answer

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

cost = 2*10**8
for i in range(N-K+1):
    d1 = abs(xs[i])
    d2 = abs(xs[i+K-1])
    if xs[i] * xs[i+K-1] <= 0:
        cost = min(cost, 2 * min(d1, d2) + max(d1, d2))
    else:
        cost = min(cost, max(d1, d2))

print(cost)

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-k+1):
  l = x[i]
  r = x[i+k-1]
  if l >= 0:
    dis = r
  elif r <= 0:
    dis = -l*2
  else:
    dis = min(r-2*l,r*2-l)
  if dis < ans:
    ans = dis
print(ans)

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):
  l = x[i]
  r = x[i+k-1]
  if l >= 0:
    dis = r
  elif r <= 0:
    dis = -l*2
  else:
    dis = min(r-2*l,r*2-l)
  if dis < ans:
    ans = dis
print(ans)

Submission status: Wrong Answer

Solution code:
""" AtCoder """

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
""" AtCoder """

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
""" AtCoder """

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
""" AtCoder """

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

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

for i in range(N - K + 1):
    if x[i] <= 0 and 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 + K - 1]))

print(ans)

Submission status: Wrong Answer

Solution code:
""" AtCoder """

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

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

for i in range(N - K + 1):
    if x[i] < 0 and 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 + K - 1]))

print(ans)

Submission status: Wrong Answer

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(int(abs(x[N-K])))
  sys.exit()

time = 2*10**8 + 100
if (n-K) >= 0:
  time = abs(x[n-K])
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, abs(x[n+K-1]))

print(int(time))

Submission status: Wrong Answer

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(x[N-K])
  sys.exit()

time = 2*10**8 + 100
if (n-K) >= 0:
  time = abs(x[n-K])
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, abs(x[n+K-1]))

print(int(time))

Submission status: Wrong Answer

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(x[N-K])
  sys.exit()

time = 2*10**8 + 100
if (n-K) >= 0:
  time = abs(x[n-K])
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, abs(x[n+K-1]))

print(time)

Submission status: Wrong Answer

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(x[N-K])
  sys.exit()

time = 2*10**8 + 100
if (n-K) >= 0:
  time = x[n-K]
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, abs(x[n+K-1]))

print(time)

Submission status: Wrong Answer

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(x[N-K])
  sys.exit()

time = 2*10**8 + 100
if (n-K) >= 0:
  time = x[n-K]
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, x[n+K-1])

print(time)

Submission status: Wrong Answer

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

def fast(x, y):
  return min(abs(x), abs(y)) + abs(x) + abs(y)

n = N
for k in range(N):
  if x[k] >= 0:
    n = k
    if x[k] == 0:
      K -= 1
      N -= 1
      x.pop(k)
    break

if n == 0:
  if K == 0:
    print(0)
    sys.exit()
  print(x[K-1])
  sys.exit()

if n == N:
  print(x[N-K])
  sys.exit()

time = 10**8 + 100
if (n-K) >= 0:
  time = x[n-K]
for j in range(K-n-1, N-n):
  time = min(time, fast(x[n-K+1+j], x[n+j]) )
if n + K <= N:
  time = min(time, x[n+K-1])

print(time)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
ans=10000000000000000000
for i in range(0,N-K+1):
    ls=(x[i])
    rs=(x[i+K-1])
    print(x[i],x[i+K-1])
    if(ls>0 and rs>0):
        ans=min(ans,rs)
    elif(ls<0 and rs<0):
        ans=min(ans,abs(ls))
    else:
        ans=min(abs(ls)+rs+min(abs(ls),rs),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
ans=10000000000000000000
for i in range(0,N-K+1):
    ls=(x[i])
    rs=(x[i+K-1])
    if(ls>0 and rs>0):
        ans=min(ans,rs)
    elif(ls<0 and rs<0):
        ans=min(ans,ls)
    else:
        ans=min(abs(ls)+rs+min(abs(ls),rs),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=list(map(int,input().split()))
ans=10000000000000000000
for i in range(0,N-K+1):
    ls=abs(min(x[i:i+K]))
    rs=abs(max(x[i:i+K]))

    ans=min(ls+rs+min(ls,rs),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k,*a=map(int,open(0).read().split());print(sum(list(sorted(map(abs,a)))[:k]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
X = list(map(int, input().split()))
ans = [0 for _ in range(n-k+1)]

for i in range(n-k+1):
    temp = 0
    if X[i] < 0:
        temp += abs(X[i])
        if X[i+k-1] > 0:
            temp += abs(X[i]) + abs(X[i+k-1])
    else:
        temp += X[i+k-1]
    ans[i] = temp

print(min(ans))

Submission status: Wrong Answer

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

for i in range(n-k+1):
    temp = 0
    if X[i] < 0:
        temp += abs(X[i])
        if X[i+k-1] > 0:
            temp += abs(X[i]) + abs(X[i+k-1])
    else:
        temp += X[i+k-1]
    ans = min(ans, temp)

print(ans)

Submission status: Wrong Answer

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

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

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

minus = []
plus = []
for x in Xs:
    if x < 0:
        minus.append(x)
    elif x == 0:
        K -= 1
    else:
        plus.append(x)


# 左にいったあと折り返して右
ans = 1e20
for left_num in range(len(minus) + 1):
    dist = 0
    remain = K

    # まず左に行き原点まで戻る
    if left_num != 0:
        dist = abs(minus[len(minus) - left_num]) * 2
        remain -= left_num

    if len(plus) < remain:
        continue
    elif remain > 0:
        dist += plus[remain - 1]

    ans = min(ans, dist)

# 右にいったあと折り返して左
for right_num in range(len(plus) + 1):
    dist = 0
    remain = K

    # まず左に行き原点まで戻る
    if right_num != 0:
        dist = abs(plus[right_num - 1]) * 2
        remain -= right_num

    if len(minus) < remain:
        continue
    elif remain > 0:
        dist += abs(minus[len(minus) - left_num])

    ans = min(ans, dist)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
x = list(int(i) for i in input().split())
res = 0.0
for i in range(n-k):
    xl = x[i]
    xr = x[i+k]
    tmp = min(abs(xl)+abs(xr-xl), abs(xr)+abs(xr-xl))
    if type(res)==type(0.0):
        res = tmp
    else:
        res = min(res,tmp)
print(res)

Submission status: Wrong Answer

Solution code:
n,k = (int(i) for i in input().split())
x = list(int(i) for i in input().split())
px=[]
mx=[]
for i in range(n):
    if x[i]>=0:
        px = x[i:]
        mx = x[:i]
        break
res = 1000000000
p = len(px)
m = len(mx)
if p>0 and m>0:
    for i in range(1,min(m,k)+1):
        if p>=k-i:
            res = min(res,2*abs(mx[-i])+px[k-i-1])
    for j in range(1,min(p,k)+1):
        if m>=k-j:
            res = min(res,2*px[j-1]+abs(mx[-k+j]))
elif p>0:
    res = px[k-1]
elif m>0:
    res = abs(mx[-k])
print(res)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
print((N//K)**3)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
ans = abs(x[0]) + abs(x[0]-x[0+K-1])

for i in range(N-K):
    temp1 = abs(x[i]) + abs(x[i]-x[i+K-1])
    temp2 = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans, temp1, temp2)

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split(' '))
x = list(map(int, input().split(' ')))
ans = 2 * abs(x[0]) + abs(x[-1])

for i in range(N-K):
    temp1 = abs(x[i]) + abs(x[i]-x[i+K-1])
    temp2 = abs(x[i+K-1]) + abs(x[i]-x[i+K-1])
    ans = min(ans, temp1, temp2)

print(ans)

Submission status: Wrong Answer

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

flag = False
for i in range(N):
    if x[i] == 0:
        K -= 1
        flag = True
    if x[i] > 0:
        break
if flag:
    x_min = x[0:i][::-1]
else:
    x_min = [0] + x[0:i][::-1]
x_plus = [0] + x[i:]
length_m = len(x_min)
length_p = len(x_plus)
# print(x_min, x_plus)
ans = 2*abs(x[0]) + abs(x[-1])

for i in range(length_m):
    if (length_p-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_min[i]) + abs(x_plus[K-i])
#    print(temp)
    ans = min(ans, temp)

for i in range(length_p):
    if (length_m-1) + i < K:
        continue
    if (i+1) > K:
        break
    temp = 2 * abs(x_plus[i]) + abs(x_min[K-i])
#    print(temp)
    ans = min(ans, temp)

print(ans)

Submission status: Wrong Answer

Solution code:
str = raw_input()
str1 = raw_input()
N,k = map(int, str.split())
array = map(int, str1.split())

INF = 10**8*2
answer = INF

for i in range(N-k+1):
    answer = min(answer, array[i+k-1]-array[i]+min(abs(array[i+k-1]), abs(array[i])))
    
print answer

Submission status: Wrong Answer

Solution code:
str = raw_input()
str1 = raw_input()
N,k = map(int, str.split())
array = map(int, str1.split())

INF = 10**8*2
for i in range(N-k+1):
    answer = min(INF, array[i+k-1]-array[i]+min(array[i+k-1], array[i]))

print answer

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x = list(map(int,input().split()))
sd=100000000
if n==k:
  sd=min(abs(x[0])+abs(x[0]-x[k-1]),abs(x[k-1])+abs(x[0]-x[k-1]))
else:
  for i in range(n-k):
    d=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
    if sd>d:
      sd=d
print(sd)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
x = list(map(int,input().split()))
sd=100000000
if n==1 and k==1:
  sd=abs(x[0])
for i in range(n-k):
  d=min(abs(x[i])+abs(x[i]-x[i+k-1]),abs(x[i+k-1])+abs(x[i]-x[i+k-1]))
  if sd>d:
    sd=d
print(sd)

Submission status: Wrong Answer

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

ret = float('inf')
for i in range(0, N - K + 1):
    ret = min(ret,
              abs(x[K + i - 1] - x[i]) + min(abs(x[i]), x[K + i - 1])
              )
print(ret)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
L = list(map(int,input().split()))
A = []
A.append(abs(L[0])+(L[K-1]-L[0]))
for i in range(N-K+1):
  p = abs(L[i])+(L[i+K-1]-L[i])
  if p < A[0]:
    del A[0]
    A.append(p)
print(A[0])

Submission status: Wrong Answer

Solution code:
input_data = input().split(" ")                                                                      
data = [int(n) for n in input_data]

N = data[0]
K = data[1]

candlePlot = input().split(" ")
candlePlot = [int(n) for n in candlePlot]

answerBuff = []

buffSum = 0 

formerCandlePlot = 0 

for n in range(N-K+1):
    
    candlePlotBuff = candlePlot[n:n+K]

    for m in candlePlotBuff:
        buffSum += abs(m - formerCandlePlot)
        formerCandlePlot = m 
    
    answerBuff.append(buffSum)
    formerCandlePlot = 0 
    buffSum = 0 

print(min(answerBuff))

Submission status: Wrong Answer

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

cnt1=0
cnt2=0
for i in range(0,a) :
    if x[i]<0 : cnt1=cnt1+int(1)
    else : cnt2=cnt2+int(1)
f=[200000000]*(a+1)
z=[200000000]*(a+1)
ans=200000000

f[0]=0
z[0]=0
for i in range(1,cnt1+1) :
    f[i]=-x[cnt1-i]
for i in range(1,cnt2+1) :
    z[i]=x[a-cnt2+i-1]
    ans=min(ans,min(2*z[i]+f[b-i],2*f[b-i]+z[i]))
print (ans)

Submission status: Wrong Answer

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

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

pos_start_index = 0

if X[0] > 0:
  pos_start_index = 0
elif X[N-1] < 0:
  pos_start_index = N-1

for n in range(N-1):
  if X[n] == 0:
    pos_start_index = n
    break
  elif X[n] < 0 and X[n+1] > 0:
    pos_start_index = n+1
    break


neg_sum_list = []

for j, idx in enumerate(range(pos_start_index-1, -1, -1)):
  neg_sum_list.append(-X[idx])

pos_sum_list = []

for j, idx in enumerate(range(pos_start_index, N)):
  pos_sum_list.append(X[idx])


res = []

for j, neg_sum in enumerate(neg_sum_list):
  neg_count = j+1
  pos_count = K - neg_count

  if neg_count == K:
    res.append(neg_sum)
    break

  if pos_count <= len(pos_sum_list):
    pos_sum = pos_sum_list[pos_count-1]
    res.append(2*neg_sum + pos_sum)

for j, pos_sum in enumerate(pos_sum_list):
  pos_count = j+1

  neg_count = K - pos_count

  if pos_count == K:
    res.append(pos_sum)
    break

  if neg_count <= len(neg_sum_list):
    neg_sum = neg_sum_list[neg_count-1]
    res.append(2*pos_sum + neg_sum)

print(min(res))

Submission status: Wrong Answer

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

min_path=4*(10**8)
for i in range(N-K):
	if x[i]<0 and x[i+K-1]<0:
		if min_path>-x[i]:
			min_path=-x[i]
	if x[i]<0 and x[i+K-1]>=0:
		if -x[i]>x[i+K-1]:
			if min_path > -x[i]+2*x[i+K-1]:
				min_path=-x[i]+2*x[i+K-1]
		else:
			if min_path > -x[i]*2 + x[i+K-1]:
				min_path=-x[i]*2 + x[i+K-1]
	if x[i]>=0 and x[i+K-1]>=0:
		if min_path>x[i+K-1]:
			min_path=x[i+K-1]

			
print min_path

Submission status: Wrong Answer

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

min_path=4*(10**5)
for i in range(N-K-1):
	if x[i]<0 and x[i+K-1]<0:
		if min_path>-x[i]:
			min_path=-x[i]
	if x[i]<0 and x[i+K-1]>=0:
		if -x[i]>x[i+K-1]:
			if min_path > -x[i]+2*x[i+K-1]:
				min_path=-x[i]+2*x[i+K-1]
		else:
			if min_path > -x[i]*2 + x[i+K-1]:
				min_path=-x[i]*2 + x[i+K-1]
	if x[i]>=0 and x[i+K-1]>=0:
		if min_path>x[i]:
			min_path=x[i]

			
print min_path

Submission status: Wrong Answer

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

min_path=4*(10**5)
for i in range(N-K-1):
	if x[i]<0 and x[i+K-1]<0:
		if min_path>-x[i]:
			min_path=-x[i]
	if x[i]<0 and x[i+K-1]>=0:
		if -x[i]>x[i+K-1]:
			if min_path > -x[i]+2*x[i+K-1]:
				min_path=-x[i]+2*x[i+K-1]
		else:
			if min_path > -x[i]*2 + x[i+K-1]:
				min_path=-x[i]*2 + x[i+K-1]
	if x[i]>=0 and x[i+K-1]>=0:
		if min_path>x[i+K-1]:
			min_path=x[i+K-1]

			
print min_path

Submission status: Wrong Answer

Solution code:
def search(x):
    if x[-1] <= 0:
        return abs(x[0])
    elif x[0] >= 0:
        return x[-1]
    else:
        t1 = abs(x[0])
        t2 = abs(x[-1])
        ans = max(t1,t2) + min(t1,t2) * 2
        return ans

n, k = map(int, input().split())
x = list(map(int, input().split()))
m = 2 * 10 ** 8
for i in range(n-k+1):
    m = min(m,search(x[i:i+k]))
print(m)

Submission status: Wrong Answer

Solution code:
def search(x):
    if x[-1] <= 0:
        return abs(x[0])
    elif x[0] >= 0:
        return x[-1]
    else:
        t1 = abs(x[0])
        t2 = abs(x[-1])
        ans = max(t1,t2) + min(t1,t2) * 2
        return ans

n, k = map(int, input().split())
x = list(map(int, input().split()))
m = 2 * 10 ** 8
for i in range(n-k+1):
    if m > search(x[i:i+k]):
        m = search(x[i:i+k])
print(m)

Submission status: Wrong Answer

Solution code:
def search(x):
    if x[-1] <= 0:
        return x[0]
    elif x[0] >= 0:
        return x[-1]
    else:
        t1 = abs(x[0])
        t2 = abs(x[-1])
        ans = max(t1,t2) + min(t1,t2) * 2
        return ans

n, k = map(int, input().split())
x = list(map(int, input().split()))
m = 2 * 10 ** 8
for i in range(n-k+1):
    if m > search(x[i:i+k]):
        m = search(x[i:i+k])
print(m)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    bi = bisect.bisect_left(a, 0)
    r = inf
    if bi >= k:
        r = abs(a[bi-k])
    if bi+k-1 < n and r > a[bi+k-1]:
        r = a[bi+k-1]
    for i in range(n):
        if 0 > bi - i or bi + k - i - 1 >= n:
            continue
        t = -a[bi-i]
        u = a[bi+k-i-1]
        s = t + u + min(t,u)
        if r > s:
            r = s
    return r



print(main())

Submission status: Wrong Answer

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

neg_list = []
pos_list = []
for p in x_list:
    if p < 0:
        neg_list.append(-p)
    elif p == 0:
        k -= 1
    else:
        pos_list.append(p)

neg_list.sort()

result = 9999999

def make(first_list, second_list, turns_at, k):
    # print first_list, second_list, turns_at, k
    if turns_at+ 1 > len(first_list) :
        return 99999999
    elif k-turns_at-1 > len(second_list):
        return 99999999
    if turns_at == k-1:
        return first_list[k-1]

    else:
        # print first_list[turns_at]* 2 + second_list[k-turns_at-2]
        return first_list[turns_at]* 2 + second_list[k-turns_at-2]


for turns_at in xrange(0,k):

    # take neg first
    candidate = make(pos_list, neg_list, turns_at, k)
    if result > candidate:
        result = candidate
    # take pos first
    candidate2 = make(neg_list,pos_list,turns_at,k)
    if result > candidate2:
        result = candidate2

print result

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
x=[int(i) for i in input().split()]
ans=10**9
A=[]
aaa=[]
for i in range(N):
    if x[i]>=0:
        A.append(x[i])
    else:
        aaa.append(-x[i])
B=aaa[::-1]
C=len(A)
D=len(B)
if C==0:
    ans=min([B[K-1],ans])
if D==0:
    ans=min([A[K-1],ans])
for i in range(1,K):
    if i>=C or K-i>=D:
        continue
    tmp1=2*A[i-1]+B[K-i-1]
    tmp2=A[i-1]+2*B[K-i-1]
    ans=min([ans,tmp1,tmp2])
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
A=[]
aaa=[]
for i in range(N):
    if x[i]>=0:
        A.append(x[i])
    else:
        aaa.append(-x[i])
B=aaa[::-1]
C=len(A)
D=len(B)
if C==0:
    print(B[K-1])
    exit()
if D==0:
    print(A[K-1])
    exit()
for i in range(K+1):
    if i>=C or K-i>=D:
        continue
    tmp1=2*A[i-1]+B[K-i-1]
    tmp2=A[i-1]+2*B[K-i-1]
    ans=min([ans,tmp1,tmp2])
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
mlist = list(filter(lambda x: x < 0, x))
plist = list(filter(lambda x: x >= 0, x))
now_l = 0
now_r = 0
ans_l = 0
ans_r = 0
for i in range(k):
    if mlist and plist:
        if abs(mlist[-1] - now_l) == plist[0] - now_r:
            if abs(now_l) < now_r:
                now_r = plist.pop(0)
                ans_r = abs(now_r)
            else:
                now_l = mlist.pop(-1)
                ans_l = abs(now_l)                

        elif abs(mlist[-1] - now_l) < plist[0] - now_r:
            now_l = mlist.pop(-1)
            ans_l = abs(now_l)
        else:
            now_r = plist.pop(0)
            ans_r = abs(now_r)
    elif mlist:
        now_l = mlist.pop(-1)
        ans_l = abs(now_l)
    elif plist:
        now_r = plist.pop(0)
        ans_r = abs(now_r)

ans = min(ans_l, ans_r) * 2 + max(ans_l, ans_r)
print(ans)

Submission status: Wrong Answer

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

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


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

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

    if k == 0:
        m = 0
    elif k == 1:
        if b-1 >= 0:
            m = min(x[b], abs(x[b-1]))
        else:
            m = x[b]

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

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

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

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

print(m)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
mlist = list(filter(lambda x: x < 0, x))
plist = list(filter(lambda x: x >= 0, x))
now_l = 0
now_r = 0
ans_l = 0
ans_r = 0
for i in range(k):
    if mlist and plist:
        if abs(mlist[-1] - now_l) < plist[0] - now_r:
            now_l = mlist.pop(-1)
            ans_l = abs(now_l)
        else:
            now_r = plist.pop(0)
            ans_r = abs(now_r)
    elif mlist:
        now_l = mlist.pop(-1)
        ans_l = abs(now_l)
    elif plist:
        now_r = plist.pop(0)
        ans_r = abs(now_r)

ans = min(ans_l, ans_r) * 2 + max(ans_l, ans_r)
print(ans)