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 JavaScript (node.js v5.12) (judge verdict: Accepted)

'use strict';

const main = (input) => {
    const lines = input.split('\n');
    const header = lines[0].split(' ');
    const N = parseInt(header[0], 10);
    const raw_K = parseInt(header[1], 10);
    const raw_x = lines[1].split(' ').map(v => parseInt(v, 10));
    const x = raw_x.filter(v => v != 0);
    const K = raw_K - (raw_x.indexOf(0) != -1 ? 1 : 0) 

    // 原点のみ
    if (x.length == 0) {
        console.log(0);
        return;
    }
    // x>=0のみ
    if (Math.min(...x) >= 0) {
        console.log(x[K - 1]);
        return;
    }
    // x<=0のみ
    if (Math.max(...x) <= 0) {
        console.log(-x[x.length - 1 - (K - 1)]);
        return;
    }

    // 左に移動した後、右に移動して、また左に移動するのは無駄なので、
    // 左に移動してから右に移動、もしくは右に移動してから左に移動することになる。
    const neg_count = x.filter(v => v < 0).length;
    const pos_count = x.filter(v => v > 0).length;

    const extra_count = x.length - K;

    // 巡回しないロウソクを割り振って全数探索
    let min_cost = Math.pow(10, 8) * 3;
    for (let i = 0; i <= extra_count; i++) {
        const neg_extra = i;
        const pos_extra = extra_count - i;
        if (neg_extra > neg_count || pos_extra > pos_count) {
            continue;
        }
        const cost_neg = neg_extra == neg_count ? 0 : -x[neg_extra];
        const cost_pos = pos_extra == pos_count ? 0 : x[x.length - 1 - pos_extra];
        const neg_first_cost = cost_neg + cost_neg + cost_pos;
        const pos_first_cost = cost_pos + cost_pos + cost_neg;
        min_cost = Math.min(min_cost, neg_first_cost);
        min_cost = Math.min(min_cost, pos_first_cost);
    }
    console.log(min_cost);
};

const isTest = false;

if (isTest) {
    main('5 3\n-30 -10 10 20 50');
    main('3 2\n10 20 30');
    main('1 1\n0');
    main('8 5\n-9 -7 -4 -3 1 2 3 4');
} else {
    main(require('fs').readFileSync('/dev/stdin', 'utf8'));
}