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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            var line = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = line[0], K = line[1];
            var array = Console.ReadLine().Split().Select(int.Parse).ToArray();

            if (N == 1)
            {
                Console.WriteLine(array[0]);
                return;
            }

            var baseIndex = 0;
            // 0付近の位置を特定する
            for (var i = 0; i < N; i++)
            {
                if (array[i] >= 0)
                {
                    baseIndex = i;
                    break;
                }
            }

            var minusPoint = 1;
            if (array[baseIndex] == 0)
            {
                K--;
                baseIndex++;
                minusPoint++;
            }

            // ダサいが思いつかないから4パターンに分けてみる
            var min = int.MaxValue;

            // 右に行きっぱなし
            if (baseIndex + K-1 < N)
                min = Math.Min(array[baseIndex + K - 1], min);

            // 1番目が0以上の時はマイナス方向が無いから処理しない
            if (array[0] < 0)
            {
                // 右に行ってから左に行きっぱなし
                for (var hosuu = 0; hosuu < K; hosuu++)
                {
                    var zan = K - hosuu - 1;
                    if (baseIndex + hosuu < 0 || baseIndex + hosuu - zan >= baseIndex || baseIndex - zan < 0) break;
                    min = Math.Min(Math.Abs(array[baseIndex + hosuu] * 2) + Math.Abs(array[baseIndex - zan]), min);
                }
            }

            // マイナス方向を起点にする
            baseIndex -= minusPoint;
            // 左に行きっぱなし
            if (baseIndex - K + 1 > 0)
                min = Math.Min(Math.Abs(array[baseIndex - K + 1]), min);

            // 最後が0以下の時はプラス方向が無いから処理しない
            if (array[N-1] > 0)
            {
                // 左に行ってから右に行きっぱなし
                for (var hosuu = 0; hosuu < K; hosuu++)
                {
                    var zan = K - hosuu - 1;
                    if (baseIndex - hosuu < 0 || baseIndex - hosuu + zan <= baseIndex || baseIndex + zan >= N) break;
                    min = Math.Min(Math.Abs(array[baseIndex - hosuu] * 2) + array[baseIndex + zan], min);
                }
            }
            Console.WriteLine(min);
        }
    }
}