/*#include<bits/stdc++.h>
#include<hash_map>
using namespace __gnu_cxx;
using namespace std;
const long long  maxn=500005;
long long arr[maxn];
struct node
{
	long long  posx;

	long long sum;
	friend bool operator <(const node &a,const node &b)
	{
		return a.sum>b.sum;
	}

}p[maxn];
long long  l[maxn];
long long  r[maxn];


set<pair<long long ,long long >>se;
int main()
{
	long long  n,m;
	scanf("%lld%lld",&m,&n);
	priority_queue<node>q;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&arr[i]);
	}
	sort(arr+1,arr+n+1);
	for(int i=2;i<=n;i++)
	{
		q.push(node{i-1,i,arr[i]-arr[i-1]});
	}
	for(int i=0;i<=n;i++)
	{
		l[i]=i;
		r[i]=i;
	}

	int sum=0;
	int cnt=0;
	while(!q.empty())
	{
		node k1=q.top();
		q.pop();
		if(se.count(make_pair(k1.posx,k1.posy)))
			continue;
		sum+=k1.sum;
		cnt++;
		if(cnt==m)
			break;
		se.insert(make_pair(k1.posx,k1.posy));
		long long newl=k1.posx-1;
		long long newr=k1.posy+1;
		q.push(node{newl,newr,-k1.sum+arr[k1.posx]-arr[newl]+arr[newr]-arr[k1.posy]});
	}
	cout<<sum<<"\n";






	return 0;
}*/





#include<bits/stdc++.h>
#include<ext/pb_ds/priority_queue.hpp>
#include<hash_map>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using namespace std;
const long long  maxn=500005;
long long arr[maxn];
struct node
{
	long long  pos;
	long long sum;
	friend bool operator <(const node &a,const node &b)
	{
		return a.sum>b.sum;
	}

};
long long  l[maxn];
long long  r[maxn];


long long sum[maxn];
//__gnu_pbds::priority_queue<node>::point_iterator it[maxn];

int del[maxn];
int main()
{
	long long  n,m;
	scanf("%lld%lld",&m,&n);
	//__gnu_pbds::priority_queue<node,less<node>>q;
	std::priority_queue<node>q;

	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&arr[i]);
	}
	for(int i=1;i<=n-1;i++)
	{
		l[i]=i-1;
		r[i]=i+1;
	}
	l[1]=0;
	r[n-1]=0;


	sort(arr+1,arr+n+1);
	for(int i=1;i<=n-1;i++)
	{
		q.push(node{i,arr[i+1]-arr[i]});
		sum[i]=arr[i+1]-arr[i];
	}

	int cnt=0;
	long long sumc=0;
	while(!q.empty())
	{
		node k1=q.top();
		q.pop();
		if(del[k1.pos]==1)
			continue;

		sumc+=k1.sum;
		//cout<<k1.sum<<" "<<k1.pos<<" "<<l[k1.pos]<<" "<<r[k1.pos]<<"***"<<endl;
		cnt++;
		if(cnt==m)
			break;
		long long kk=-k1.sum;
		if(l[k1.pos]!=0)
		{
			kk+=sum[l[k1.pos]];
			sum[l[k1.pos]]=0;

			del[l[k1.pos]]=1;

			if(l[l[k1.pos]]!=0)
			{
				r[l[l[k1.pos]]]=k1.pos;
				l[k1.pos]=l[l[k1.pos]];
			}
			else
			{
				l[k1.pos]=0;
			}

		}
		if(r[k1.pos]!=0)
		{
			kk+=sum[r[k1.pos]];
			sum[r[k1.pos]]=0;

			del[r[k1.pos]]=1;

			if(r[r[k1.pos]]!=0)
			{
				l[r[r[k1.pos]]]=k1.pos;
				r[k1.pos]=r[r[k1.pos]];
			}
			else
			{
				r[k1.pos]=0;
			}

		}
		if(l[k1.pos]!=0&&r[k1.pos]!=0)
		q.push(node{k1.pos,kk});
	}
	cout<<sumc<<"\n";








	return 0;
}