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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define bp  __builtin_popcount
#define pb push_back
#define sz(s) (int)(s.size())

using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const int maxn=1e5+5;
const  double EPS = 1e-14;
const int MOD = 1e9+7;

/*
ll xo,yo,z;
void extend_euclidean(ll a,ll b)
{	ll temp;
	if(b==0)
	{
		z=a;
		xo=1;
		yo=0;
	}
	else
	{
		extend_euclidean(b,a%b);
		temp=xo;
		xo=yo;
		yo=temp-(a/b)*yo;
	}
return;
}



//     O(sqrtn)

vector<ll> factorize(ll n) {
    vector<ll> res;
    for (ll i = 2; i * i <= n; ++i) {
        while (n % i == 0) {
            res.push_back(i);
            n /= i;
        }
    }
    if (n != 1) {
        res.push_back(n);
    }
    return res;
}

*************FACTORIZE IN 0(LOG(N)) PROVIDED N IS INTEGER**************

int minPrime[100000];

void modified_sieve(int n){
for (int i = 2; i * i <= n; ++i) {
    if (minPrime[i] == 0) {         //If i is prime
        for (int j = i * i; j <= n; j += i) {
            if (minPrime[j] == 0) {
                minPrime[j] = i;
            }
        }
    }
}
for (int i = 2; i <= n; ++i) {
    if (minPrime[i] == 0) {
        minPrime[i] = i;
    }
}


vector<int> factorize(int n) {
    vector<int> res;
    while (n != 1) {
        res.push_back(minPrime[n]);
        n /= minPrime[n];
    }
    return res;
}

*************************************************
bool isPrime[maxn];   o(n log(log(n)))
void sieve()
{
    memset(isPrime,true,maxn);
    isPrime[0] = false;
    isPrime[1] = false;
    for(int i = 2; i * i <= maxn ; ++i)
    {
        if(isPrime[i])
        {
            for(int j = i * 2; j <= maxn ; j += i)
                isPrime[j] = false;
        }
    }
}

ll binomialCoeff(ll n, ll k)
{
    ll res = 1;
    if(n<k)
        return 0;

    // Since C(n, k) = C(n, n-k)
    if ( k > n - k )
        k = n - k;

    // Calculate value of
    // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
    for (ll i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}
***********************************************************
sieve with linear time complexity o(n)
void faster_sieve(int N)
{
  int lp[N+1];
  vector<int> pr;
  for (int i=2; i<=N; ++i)
   {
     if (lp[i] == 0)
     {
        lp[i] = i;
        pr.push_back (i);
     }
     for(int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
        lp[i * pr[j]] = pr[j];
   }
}
******************************************************************************
faster factorize
vector<long long> factorization(int N){
    while (n % 2 == 0) {
        factorization.push_back(2);
        n /= 2;
    }
    for (long long d = 3; d * d <= n; d += 2) {
        while (n % d == 0) {
            factorization.push_back(d);
            n /= d;
        }
    }
    if (n > 1)
        factorization.push_back(n);
    return factorization;
}

ll GCD(ll a,ll b){return (a)?GCD(b%a,a):b;}
ll LCM(ll a, ll b){return a*b/GCD(a,b);}
ll fastpow(ll b, ll p){if(!p)return 1;ll ret = fastpow(b, p >> 1);ret *= ret;if(p&1)ret*= b;return ret;}
*/
int arr[maxn];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
     //freopen("in.txt", "r", stdin);
#endif
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
        cin>>arr[i];
    sort(arr,arr+m);
    int cnt=0;
    vector<ll>v;
    for(int i=0;i<m;i++)
    {
        cnt=abs(arr[i]-arr[i+1]);
        v.pb(cnt);
    }
    int x=sz(v)-n-1;
    int ans=0;
    sort(v.begin(),v.end());
    for(int i=0;i<x;i++)
        ans+=v[i];
    cout<<ans;
}