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: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("ARC101.C.Test")]

namespace ARC101.C
{
    using Common;
    
    class Program : SolverBase<Input, Unit, Output, int>
    {
        static void Main(string[] args)
        {
            new Program().Run(Console.In, Console.Out);
        }

        public override void Solve(Input input, Output output, IContext context)
        {
            if (input.K == 0)
            {
                output.OutputItem(0);
            }
            else if (input.Xs.First() > 0)
            {
                output.OutputItem(input.Xs[input.K - 1]);
            }
            else if (input.Xs.Last() < 0)
            {
                output.OutputItem(-input.Xs[input.Xs.Length - input.K]);
            }
            else
            {
                var zeroIndex = ~Array.BinarySearch(input.Xs, 0);
                var minDistance = int.MaxValue;

                for (var n = 0; n <= input.K; n++)
                {
                    var p = input.K - n;
                    var pIndex = zeroIndex + p - 2;
                    var nIndex = zeroIndex - n - 1;

                    if (pIndex >= input.Xs.Length || nIndex < 0)
                    {
                        continue;
                    }

                    var pValue = input.Xs[pIndex];
                    var nValue = -input.Xs[nIndex];

                    var distance = Math.Min(pValue, nValue) * 2 + Math.Max(pValue, nValue);

                    minDistance = Math.Min(minDistance, distance);
                }

                output.OutputItem(minDistance);
            }
        }
    }

    class Input : InputBase<Input, Unit>
    {
        public int N { get; private set; }
        public int K { get; private set; }

        public int[] Xs { get; private set; }

        protected override Unit PerseItem(string line, IContext context)
        {
            throw new NotImplementedException();
        }

        protected override int ReadHeaders(TextReader reader, IContext context)
        {
            var parts = reader.ReadLine().Split(' ').Select(int.Parse).ToArray();

            N = parts[0];
            K = parts[1];
            var xs = reader.ReadLine().Split(' ').Select(int.Parse).ToList();

            var index = xs.BinarySearch(0);

            if (index >= 0)
            {
                xs.RemoveAt(index);
                K--;
            }

            Xs = xs.ToArray();

            return 0;
        }
    }

    class Output : OutputBase<Output, int>
    {

    }

    namespace Common
    {
        struct Unit { }

        interface IContext : IDisposable
        {
            T Do<T>(Func<IContext, T> action, string description = null);
            void Do(Action<IContext> action, string description = null);
        }

        class ThinContext : IContext
        {
            public static readonly ThinContext Instance = new ThinContext();

            public void Dispose()
            {

            }

            public T Do<T>(Func<IContext, T> action, string description = null)
            {
                return action(this);
            }

            public void Do(Action<IContext> action, string description = null)
            {
                action(this);
            }
        }

        interface ISolver<TInput, TOutput>
        {
            // ##SKIP-SLIM
            void Run(TextReader inputReader, TextWriter outputWriter, IContext context = null);
        }

        abstract class SolverBase<TInput, TInputItem, TOutput, TOutputItem> : ISolver<TInput, TOutput>
            where TInput : IInput<TInput, TInputItem>, new()
            where TOutput : IOutput<TOutput, TOutputItem>, new()
        {
            public abstract void Solve(TInput input, TOutput output, IContext context);

            public void Run(TextReader inputReader, TextWriter outputWriter, IContext context = null)
            {
                context = context ?? ThinContext.Instance;

                using (var input = context.Do(innerContext => new TInput().Initialize(inputReader, innerContext)))
                using (var output = context.Do(innerContext => new TOutput().Initialize(outputWriter, innerContext)))
                {
                    context.Do(innerContext => Solve(input, output, innerContext), "solving");
                }
            }
        }

        interface IInput<TSelf, TInputItem> : IEnumerable<TInputItem>, IDisposable
                where TSelf : IInput<TSelf, TInputItem>
        {
            TSelf Initialize(TextReader reader, IContext context);
        }

        interface IOutput<TSelf, TOutputItem> : IDisposable
            where TSelf : IOutput<TSelf, TOutputItem>
        {
            TSelf Initialize(TextWriter writer, IContext context);
        }

        abstract class InputBase<TSelf, TInputItem> : IInput<TSelf, TInputItem>
                where TSelf : class, IInput<TSelf, TInputItem>
        {
            protected TextReader Reader { get; private set; }
            public IContext Context { get; private set; }
            protected int TotalItemCount { get; private set; }

            public virtual TSelf Initialize(TextReader reader, IContext context)
            {
                Reader = reader;
                Context = context;

                TotalItemCount = ReadHeaders(reader, context);

                return this as TSelf;
            }

            protected abstract int ReadHeaders(TextReader reader, IContext context);
            protected virtual TInputItem ReadItem(TextReader reader, IContext context)
            {
                return PerseItem(reader.ReadLine(), context);
            }

            protected abstract TInputItem PerseItem(string line, IContext context);

            public virtual void Dispose()
            {
                if (Context != null)
                {
                    Context.Dispose();
                    Context = null;
                }

                Reader = null;
            }

            public virtual IEnumerator<TInputItem> GetEnumerator()
            {
                for (var i = 0; i < TotalItemCount; i++)
                {
                    yield return ReadItem(Reader, Context);
                }
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }

        abstract class OutputBase<TSelf, TOutputItem> : IOutput<TSelf, TOutputItem>
                where TSelf : class, IOutput<TSelf, TOutputItem>
        {
            protected TextWriter Writer { get; private set; }
            public IContext Context { get; private set; }

            public virtual TSelf Initialize(TextWriter writer, IContext context)
            {
                Writer = writer;
                Context = context;

                return this as TSelf;
            }

            public virtual void OutputItem(TOutputItem item)
            {
                OutputItem(item, Writer, Context);
            }

            protected virtual void OutputItem(TOutputItem item, TextWriter writer, IContext context)
            {
                writer.WriteLine(item);
            }

            public virtual void Dispose()
            {
                if (Context != null)
                {
                    Context.Dispose();
                    Context = null;
                }

                Writer = null;
            }
        }
    }
}