Problem:
Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

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

namespace AtCoder
{
    class Program
    {
        static List<Point> points;
        static int sum;

        static void Main(string[] args)
        {
            //[summary]C - Streamline
            var line = ReadLine();
            int n = line[0];
            int m = line[1];

            //地点の数よりコマの数が多いこともある
            //(その場合は移動距離ゼロ)
            if (n >= m)
            {
                Console.WriteLine(0);
            }
            else
            {
                var x = ReadLine();
                points = new List<Point>();

                //地点の数だけインスタンスを生成
                for (int i = 0; i < m; i++)
                {
                    points.Add(new Point(x[i]));
                }

                //座標の小さい順にソート
                points = points.OrderBy(a => a.Coord).ToList();

                //2地点間の距離を計算(右端の座標の距離は0になる)
                for (int i = 0; i < m; i++)
                {
                    int distance;

                    if (i < m - 1)
                    {
                        distance = points[i + 1].Coord - points[i].Coord;
                    }
                    else
                    {
                        //最後の地点には、1つ前の地点と同じ距離をセット
                        distance = points[i - 1].Distance;
                    }

                    points[i].Distance = distance;
                }

                //2地点間の距離が長い順にソート
                points = points.OrderByDescending(a => a.Distance).
                                ThenBy(a => a.Coord).ToList();

                //距離が長い順にコマを置いていく(移動しなくていいように)
                for (int i = 0; i < n; i++)
                {
                    points[i].IsReached = true;
                }

                //コマを置き終わったら、座標の小さい順に戻す
                points = points.OrderBy(a => a.Coord).ToList();

                foreach (var point in points)
                {
                    if (!point.IsReached)
                    {
                        sum += point.Distance;
                        point.IsReached = true;
                    }
                }

                Console.WriteLine(sum);
            }
        }

        static void MoveToLeftEdge(int m)
        {
            for(int i = 0; i < m; i++)
            {
                if (!points[i].IsReached)
                {
                    sum += points[i].Distance;
                    points[i].IsReached = true;
                }
                else
                {
                    break;
                }
            }

            ////[summary]一番左の地点に行っていなければ、一番左のコマを左側に移動
            //int index = 0;

            ////[ToDo]ループは0スタートのほうがいい？
            //points[0].IsReached = true;
            //sum = points[0].Distance;

            ////一番左にあるコマを左端まで移動する
            //for (int i = 1; i < m; i++)
            //{
            //    if (!points[i].IsReached)
            //    {
            //        sum += points[i - 1].Distance;
            //        points[i].IsReached = true;
            //    }
            //    else
            //    {
            //        //もともとコマがあった場所を記憶
            //        if (i < m - 1)
            //        {
            //            index = i;
            //        }
            //        else
            //        {
            //            //もともとコマのあった場所が右端なら、ここで終了
            //            return;
            //        }                    
            //    }
            //}

            ////もともとコマのあった場所の右隣がまだ未到達の地点なら
            ////左右どちらにあるコマを移動するか決める
            //if (!points[index + 1].IsReached)
            //{
            //    //左端から右隣のコマまで移動
            //    int forward = sum + points[index].Distance;

            //    //1つ右隣から戻る
            //    //(仮に1つ右隣にコマがなくても、そこから先は一方通行で移動するだけなので
            //    //考慮する必要がない)
            //    int reverse = points[index + 1].Distance;

            //    if (forward > reverse)
            //    {
            //        sum += reverse;
            //    }
            //    else
            //    {
            //        sum += forward;
            //    }

            //    points[index + 1].IsReached = true;
            //}
        }

        static List<int> ReadLine()
        {
            var line = Console.ReadLine().TrimEnd();
            var array = line.Split(' ');
            var numbers = new List<int>();

            foreach (var s in array)
            {
                int num = int.Parse(s);
                numbers.Add(num);
            }

            return numbers;
        }
    }

    class Point
    {
        public int Coord { set; get; }
        public int Distance { set; get; }
        public bool IsReached { set; get; }

        public Point(int coord)
        {
            Coord = coord;
        }
    }
}