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 Rust (1.15.1) (judge verdict: Accepted)

use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;

#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

#[allow(dead_code)]
fn read_as_string() -> String{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn calc_time(first: &Vec<i64>, second: &Vec<i64>, k: usize, num_from_first: usize) -> i64{
    let num_from_second = k - num_from_first;
    let res = first[num_from_first-1]*2;

    if num_from_second == 0{
        return res/2;
    }else{
        return res +  second[num_from_second-1];
    }

}


//01:45
fn main(){
    let line = read_as_vec::<usize>();
    let (n, k) = (line[0], line[1]);

    let mut x = read_as_vec::<i64>();
    let mut left: Vec<i64> = vec![];
    let mut right: Vec<i64> = vec![];


    for &elem in &x{
        if elem > 0{
            right.push(elem.abs());
        }else{
            left.push(elem.abs());
        }
    }
    left.sort();
    right.sort();
    //println!("left = {:?}\nright={:?}",left, right);

    let mut ans = std::i64::MAX;
    //go right first
    for i in 0..k{
        let num_take_right = i+1;
        let num_take_left = k - num_take_right;
        //println!("right:{}, left:{}", num_take_right, num_take_left);
        if right.len() < num_take_right || left.len() < num_take_left{
            //println!("cannnot serch this range");
            continue;
        }
        ans = cmp::min(calc_time(&right, &left, k, i+1), ans);
        //println!("{}", ans);
    }

    for i in 0..k{
        let num_take_left = i+1;
        let num_take_right = k - num_take_left;
        if right.len() < num_take_right || left.len() < num_take_left{
            continue;
        }

        ans = cmp::min(calc_time(&left, &right, k, i+1), ans);
    }

    println!("{}", ans);
}