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

Solution in Ruby (2.7.1) (judge verdict: Runtime Error)

#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

N, K = get_ints
X = get_ints

minus = []
plus = []
zero = nil

X.each do |x|
  if x < 0
    minus << -x
  elsif x > 0
    plus << x
  else
    zero = true
  end
end

k = zero ? K - 1 : K

if plus.length == 0 && minus.length == 0
  puts 0
else
  minus.reverse!

  min = 10 ** 32

  plus.each.with_index do |x, i|
    break if i >= k - 1

    if minus.length > k-i-2
      stock = x * 2 + minus[k-i-2]
      min = stock if stock < min
    end
  end
  if plus.length >= k
    min = plus[k-1] if plus[k-1] < min
  end

  minus.each.with_index do |x, i|
    break if i >= k - 1
    if minus.length > k-i-2
      stock = x * 2 + plus[k-i-2]
      min = stock if stock < min
    end
  end
  if minus.length >= k
    min = minus[k-1] if minus[k-1] < min
  end

  puts min
end