#include<bits/stdc++.h>
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define Debug(x)      cerr << #x << "is " << x << '\n';
#define pb            emplace_back
#define mp            make_pair
#define x             first
#define y             second
//#define LuckyAurora
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

using namespace std;

template<typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}

typedef pair<int, int> pii;
const int N = 5002;
const int M = int(5e5) + 2;
const ll oo = (ll)1e10;

int n, k, _a[M], cur[3];
vector<ll> f[3];
struct TIrene {
    int x, i;
    bool operator<(const TIrene& o)const&{
        return i < o.i;
    }
} a[M];

void Enter()
{
     cin >> k >> n;
     for(int i = 0; i < n; ++i) cin >> _a[i];
     sort(_a, _a + n);
     for(int i = 0; i < n; ++i) {
        if(i) a[i].x = _a[i] - _a[i - 1];
        a[i].i = i;
     }
     sort(a + 1, a + n, [](const TIrene& a, const TIrene& b) {
              if(a.x != b.x) return a.x < b.x;
              return a.i > b.i;
          });
     int _n = 2 * k, cnt = 2;
     while(_n <= n && a[_n].x == a[_n + 1].x && cnt) ++_n, --cnt;
     n = _n;
     sort(a + 1, a + n);
}

void Solve()
{
     for(int i = 0; i < 3; ++i) {
        f[i].resize(k + 1);
        fill(f[i].begin(), f[i].end(), oo);
        cur[i] = i;
     }
     for(int i = 1; i < n; ++i) {
        if(i - 1) f[cur[2]][1] = min(f[cur[1]][1], (ll)a[i].x);
        else f[cur[2]][1] = a[1].x;
        for(int j = 2, l = min(k, i); j <= l; ++j) {
            if(a[i].i > a[i - 1].i + 1) f[cur[2]][j] = min(f[cur[1]][j], f[cur[1]][j - 1] + a[i].x);
            else f[cur[2]][j] = min(f[cur[1]][j], f[cur[0]][j - 1] + a[i].x);
        }
        int t = cur[0];
        cur[0] = cur[1], cur[1] = cur[2], cur[2] = t;
     }
     cout << f[cur[1]][k];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(FileName".inp", "r")) {
        freopen(FileName".inp", "r", stdin);
        freopen(FileName".out", "w", stdout);
    }

    /*int nTest; cin >> nTest;
    while(nTest --)*/ Enter(), Solve();

    return 0;
}


#include<bits/stdc++.h>
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define Debug(x)      cerr << #x << "is " << x << '\n';
#define pb            emplace_back
#define mp            make_pair
#define x             first
#define y             second
//#define LuckyAurora
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

using namespace std;

template<typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}

typedef pair<int, int> pii;
const int N = 5002;
const int M = int(5e5) + 2;

int n, k, _a[M];
ll f[2 * N][N];
struct TIrene {
    ll x; int i;
    bool operator<(const TIrene& o)const&{
        return i < o.i;
    }
} a[M];

void Enter()
{
     cin >> k >> n;
     for(int i = 0; i < n; ++i) cin >> _a[i];
     sort(_a, _a + n);
     for(int i = 0; i < n; ++i) {
        if(i) a[i].x = _a[i] - _a[i - 1];
        a[i].i = i;
     }
     sort(a + 1, a + n, [](const TIrene& a, const TIrene& b) {
              return a.x < b.x;
          });
     n = 2 * k;
     sort(a + 1, a + n);
}

void Solve()
{
     ll res = (ll)1e10;
     fill_n(&f[0][0], 2 * N * N, (ll)1e10);
     for(int i = 1; i < n; ++i) {
        if(i - 1) f[i][1] = min(f[i - 1][1], a[i].x);
        else f[i][1] = a[1].x;
        for(int j = 2, l = min(k, i); j <= l; ++j) {
            f[i][j] = min(f[i - 1][j], f[i - 2][j - 1] + a[i].x);
            if(a[i].i > a[i - 1].i + 1) f[i][j] = min(f[i][j], f[i - 1][j - 1] + a[i].x);
        }
     }
     cout << f[n - 1][k];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(FileName".inp", "r")) {
        freopen(FileName".inp", "r", stdin);
        freopen(FileName".out", "w", stdout);
    }

    /*int nTest; cin >> nTest;
    while(nTest --)*/ Enter(), Solve();

    return 0;
}


#include<bits/stdc++.h>
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define Debug(x)      cerr << #x << "is " << x << '\n';
#define pb            emplace_back
#define mp            make_pair
#define x             first
#define y             second
//#define LuckyAurora
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

using namespace std;

template<typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}

typedef pair<int, int> pii;
const int N = 5002;
const int M = int(5e5) + 2;

int n, k, _a[M];
ll f[2 * N][N];
struct TIrene {
    int x, i;
    bool operator<(const TIrene& o)const&{
        return i < o.i;
    }
} a[M];

void Enter()
{
     cin >> k >> n;
     for(int i = 0; i < n; ++i) cin >> _a[i];
     sort(_a, _a + n);
     for(int i = 0; i < n; ++i) {
        if(i) a[i].x = _a[i] - _a[i - 1];
        a[i].i = i;
     }
     sort(a + 1, a + n, [](const TIrene& a, const TIrene& b) {
              return a.x < b.x;
          });
     n = 2 * k;
     sort(a + 1, a + n);
}

void Solve()
{
     ll res = (ll)1e10;
     fill_n(&f[0][0], 2 * N * N, (ll)1e10);
     for(int i = 1; i < n; ++i) {
        if(i - 1) f[i][1] = min(f[i - 1][1], (ll)a[i].x);
        else f[i][1] = a[1].x;
        for(int j = 2, l = min(k, i); j <= l; ++j) {
            f[i][j] = min(f[i - 1][j], f[i - 2][j - 1] + a[i].x);
            if(a[i].i > a[i - 1].i + 1) f[i][j] = min(f[i][j], f[i - 1][j - 1] + a[i].x);
        }
     }
     cout << f[n - 1][k];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(FileName".inp", "r")) {
        freopen(FileName".inp", "r", stdin);
        freopen(FileName".out", "w", stdout);
    }

    /*int nTest; cin >> nTest;
    while(nTest --)*/ Enter(), Solve();

    return 0;
}


#include<bits/stdc++.h>
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define Debug(x)      cerr << #x << "is " << x << '\n';
#define pb            emplace_back
#define mp            make_pair
#define x             first
#define y             second
//#define LuckyAurora
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

using namespace std;

template<typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}

typedef pair<int, int> pii;
const int N = 5002;
const int M = int(5e5) + 2;
const ll oo = (ll)1e10;

int n, k, _a[M], cur[3];
vector<ll> f[3];
struct TIrene {
    int x, i;
    bool operator<(const TIrene& o)const&{
        return i < o.i;
    }
} a[M];

void Enter()
{
     cin >> k >> n;
     for(int i = 0; i < n; ++i) cin >> _a[i];
     sort(_a, _a + n);
     for(int i = 0; i < n; ++i) {
        if(i) a[i].x = _a[i] - _a[i - 1];
        a[i].i = i;
     }
     sort(a + 1, a + n, [](const TIrene& a, const TIrene& b) {
              if(a.x != b.x) return a.x < b.x;
              return a.i > b.i;
          });
//     int _n = 2 * k, cnt = 2;
//     while(_n <= n && a[_n].x == a[_n - 1].x && cnt) ++_n, --cnt;
//     n = _n;
     sort(a + 1, a + n);
}

void Solve()
{
     for(int i = 0; i < 3; ++i) {
        f[i].resize(k + 1);
        fill(f[i].begin(), f[i].end(), oo);
        cur[i] = i;
     }
     for(int i = 1; i < n; ++i) {
        if(i - 1) f[cur[2]][1] = min(f[cur[1]][1], (ll)a[i].x);
        else f[cur[2]][1] = a[1].x;
        for(int j = 2, l = min(k, i); j <= l; ++j) {
            if(a[i].i > a[i - 1].i + 1) f[cur[2]][j] = min(f[cur[1]][j], f[cur[1]][j - 1] + a[i].x);
            else f[cur[2]][j] = min(f[cur[1]][j], f[cur[0]][j - 1] + a[i].x);
        }
        int t = cur[0];
        cur[0] = cur[1], cur[1] = cur[2], cur[2] = t;
     }
     cout << f[cur[1]][k];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(FileName".inp", "r")) {
        freopen(FileName".inp", "r", stdin);
        freopen(FileName".out", "w", stdout);
    }

    /*int nTest; cin >> nTest;
    while(nTest --)*/ Enter(), Solve();

    return 0;
}


#include<bits/stdc++.h>
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define Debug(x)      cerr << #x << "is " << x << '\n';
#define pb            emplace_back
#define mp            make_pair
#define x             first
#define y             second
//#define LuckyAurora
//#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

using namespace std;

template<typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}

typedef pair<int, int> pii;
const int N = 5002;
const int M = int(5e5) + 2;
const ll oo = (ll)1e10;

int n, k, _a[M], cur[3];
vector<ll> f[3];
struct TIrene {
    int x, i;
    bool operator<(const TIrene& o)const&{
        return i < o.i;
    }
} a[M];

void Enter()
{
     cin >> k >> n;
     for(int i = 0; i < n; ++i) cin >> _a[i];
     sort(_a, _a + n);
     for(int i = 0; i < n; ++i) {
        if(i) a[i].x = _a[i] - _a[i - 1];
        a[i].i = i;
     }
     sort(a + 1, a + n, [](const TIrene& a, const TIrene& b) {
              if(a.x != b.x) return a.x < b.x;
              return a.i > b.i;
          });
     n = min(n, 3 * k);
     sort(a + 1, a + n);
}

void Solve()
{
     for(int i = 0; i < 3; ++i) {
        f[i].resize(k + 1);
        fill(f[i].begin(), f[i].end(), oo);
        cur[i] = i;
     }
     for(int i = 1; i < n; ++i) {
        if(i - 1) f[cur[2]][1] = min(f[cur[1]][1], (ll)a[i].x);
        else f[cur[2]][1] = a[1].x;
        for(int j = 2, l = min(k, i); j <= l; ++j) {
            if(a[i].i > a[i - 1].i + 1) f[cur[2]][j] = min(f[cur[1]][j], f[cur[1]][j - 1] + a[i].x);
            else f[cur[2]][j] = min(f[cur[1]][j], f[cur[0]][j - 1] + a[i].x);
        }
        int t = cur[0];
        cur[0] = cur[1], cur[1] = cur[2], cur[2] = t;
     }
     cout << f[cur[1]][k];
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if(fopen(FileName".inp", "r")) {
        freopen(FileName".inp", "r", stdin);
        freopen(FileName".out", "w", stdout);
    }

    /*int nTest; cin >> nTest;
    while(nTest --)*/ Enter(), Solve();

    return 0;
}


#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#define int long long
using namespace std;
#define N 500010
priority_queue<pair<int,int> >q;
int n,m,x,y,z,ans,a[N],pre[N],ne[N],b[N];
void del(int x) {
  pre[ne[x]]=pre[x];ne[pre[x]]=ne[x];b[x]=1;
}
signed main() {
  scanf("%lld%lld",&m,&n);
  for (int i=1;i<=n;++i) scanf("%lld",&a[i]);
  sort(a+1,a+n+1);
  for (int i=1;i<n;++i) {
    pre[i]=i-1; ne[i]=i+1;
    a[i]=a[i+1]-a[i];
    a[i]=-a[i];
    q.push(make_pair(a[i],i));
  }
  a[0] = a[n] = -1e9;
  while (m--) {
    while (b[q.top().second]) q.pop();
    int val=q.top().first;
    int pos=q.top().second;
    q.pop();
    ans+=val;
    a[pos]=a[pre[pos]]+a[ne[pos]]-val;
    q.push(make_pair(a[pos],pos));
    del(pre[pos]);del(ne[pos]);
  }
  printf("%lld\n",-ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 10005;
int n;
int dp[N][5005];
int v[500005];

struct segment{
    int start;
    int finish;

    bool operator<(const segment &a) {
        int cost = finish - start;
        int cost_a = a.finish - a.start;
        return cost < cost_a;
    }
};

vector< pair<int, int> > options;
int solve(int pos, int k){
    if(k == 0)
        return 0;
    if(pos >= options.size())
        return (1LL << 50);

    if(dp[pos][k] != -1)
        return dp[pos][k];

    long long ans = solve(pos + 1, k);
    long long cost = options[pos].second - options[pos].first;
    if(pos+1 != options.size()){
        int next_start = options[pos+1].first;
        int current_finish = options[pos].second;
        if(next_start == current_finish)
            ans = min(ans, cost + solve(pos+2, k-1));
        else
            ans = min(ans, cost + solve(pos+1, k-1));
    }else {
        ans = min(ans, cost + solve(pos+1, k-1));
    }

    return dp[pos][k] = ans;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int k;
    cin >> k >> n;

    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v, v+n);

    vector<segment> segments;
    //cout << "CREATING SEGMENTS" << endl;
    for(int i = 0; i < n-1; i++) {
        segments.push_back((segment){v[i], v[i+1]});
        //cout << v[i] << " " << v[i+1] << endl;

    }
    sort(segments.begin(), segments.end());

    for(int i = 0; i < min(N, (int)segments.size()); i++) {
        options.push_back( make_pair(segments[i].start, segments[i].finish) );
        //cout << "BEST SEGMENTS " << segments[i].start << " " << segments[i].finish << endl;
    }

    sort(options.begin(), options.end());
    memset(dp, -1, sizeof dp);

    cout << solve(0, k) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 20005;
int n;
long long dp[N][5005];
int v[500005];

struct segment{
    int start;
    int finish;

    bool operator<(const segment &a) {
        int cost = finish - start;
        int cost_a = a.finish - a.start;
        return cost < cost_a;
    }
};

vector< pair<int, int> > options;
long long solve(int pos, int k){
    if(k == 0)
        return 0;
    if(pos >= options.size())
        return (1LL << 50);

    if(dp[pos][k] != -1)
        return dp[pos][k];

    long long ans = solve(pos + 1, k);
    long long cost = options[pos].second - options[pos].first;
    if(pos+1 != options.size()){
        int next_start = options[pos+1].first;
        int current_finish = options[pos].second;
        if(next_start == current_finish)
            ans = min(ans, cost + solve(pos+2, k-1));
        else
            ans = min(ans, cost + solve(pos+1, k-1));
    }else {
        ans = min(ans, cost + solve(pos+1, k-1));
    }

    return dp[pos][k] = ans;
}
int main()
{
    int k;
    cin >> k >> n;

    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v, v+n);

    vector<segment> segments;
    //cout << "CREATING SEGMENTS" << endl;
    for(int i = 0; i < n-1; i++) {
        segments.push_back((segment){v[i], v[i+1]});
        //cout << v[i] << " " << v[i+1] << endl;

    }
    sort(segments.begin(), segments.end());

    for(int i = 0; i < min(N, (int)segments.size()); i++) {
        options.push_back( make_pair(segments[i].start, segments[i].finish) );
        //cout << "BEST SEGMENTS " << segments[i].start << " " << segments[i].finish << endl;
    }

    sort(options.begin(), options.end());
    memset(dp, -1, sizeof dp);

    cout << solve(0, k) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 10005;
int n;
int dp[N][5005];
int v[500005];

struct segment{
    int start;
    int finish;

    bool operator<(const segment &a) {
        int cost = finish - start;
        int cost_a = a.finish - a.start;
        return cost < cost_a;
    }
};

vector< pair<int, int> > options;

int solve(int pos, int k){
    if(k == 0)
        return 0;
    if(pos >= options.size())
        return (1 << 30);

    if(dp[pos][k] != -1)
        return dp[pos][k];

    int ans = solve(pos + 1, k);
    int cost = options[pos].second - options[pos].first;
    if(pos+1 != options.size()){
        int next_start = options[pos+1].first;
        int current_finish = options[pos].second;
        if(next_start == current_finish)
            ans = min(ans, cost + solve(pos+2, k-1));
        else
            ans = min(ans, cost + solve(pos+1, k-1));
    }else {
        ans = min(ans, cost + solve(pos+1, k-1));
    }

    return dp[pos][k] = ans;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int k;
    cin >> k >> n;

    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v, v+n);

    vector<segment> segments;
    //cout << "CREATING SEGMENTS" << endl;
    for(int i = 0; i < n-1; i++) {
        segments.push_back((segment){v[i], v[i+1]});
        //cout << v[i] << " " << v[i+1] << endl;

    }
    sort(segments.begin(), segments.end());

    for(int i = 0; i < min(N, (int)segments.size()); i++) {
        options.push_back( make_pair(segments[i].start, segments[i].finish) );
        //cout << "BEST SEGMENTS " << segments[i].start << " " << segments[i].finish << endl;
    }

    sort(options.begin(), options.end());
    memset(dp, -1, sizeof dp);

    cout << solve(0, k) << endl;
    return 0;
}


#include<bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair

using namespace std;

int n, k;
long long dp[4][5001];
int ar[500001], u[500001];
vector<int> vec;
pair<int, pair<int, int> > ars[500001];

int main()
{
    cin >> k >> n;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &ar[i]);
    }
    sort(ar + 1, ar + n + 1);

    for(int i = 2; i <= n; i++)
        ars[i] = mk(ar[i] - ar[i - 1], mk(i - 1, i));

    sort(ars + 2, ars + n + 1);

    for(int i = 2; i <= k * 2 + 1; i++)
    {
        u[ars[i].sc.fr] = 1;
        u[ars[i].sc.sc] = 1;
    }

    vec.push_back(0);
    for(int i = 1; i <= n; i++)
        if(u[i] == 1)
            vec.push_back(ar[i]);
    for(int i = 0; i < 4; i++)
        for(int j = 1; j < 5001; j++)
            dp[i][j] = 1e18;

    for(int i = 2; i < vec.size(); i++)
    {
        for(int j = 1; j <= k; j++)
        {
            dp[3][j] = dp[3 - 1][j];
            dp[3][j] = min(dp[3][j], dp[3 - 2][j - 1] + (vec[i] - vec[i - 1]));
        }
        for(int a = 0; a < 3; a++)
        for(int b = 1; b < 5001; b++)
            dp[a][b] = dp[a + 1][b];
    }

    cout << dp[3][k];
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e18
#define mod 1000000007

ll n,k;
ll a[500001];
ll d[500001];
bool used[500001];
multiset<ll> sta,stb;

int main(){
  cin>>k>>n;
  rep(i,n){
    scanf("%lld",a+i);
  }
  sort(a,a+n);
  rep(i,n-1){
    d[i]=a[i+1]-a[i];
    sta.insert(d[i]);
  }

  ll res=0;
  rep(i,k){
    ll ta=INF,tb=INF;
    if(sta.size()>0){
      ta=*sta.begin();
    }
    if(stb.size()>0){
      tb=*stb.begin();
    }
    if(ta<tb){
      res+=ta;
      ll idx=lower_bound(d,d+n,ta)-d;
      used[idx]=true;
      ll add=-ta;
      ll cnt=0;
      sta.erase(sta.find(ta));
      if(idx>0&&!used[idx-1]&&sta.find(d[idx-1])!=sta.end()){
        sta.erase(sta.find(d[idx-1]));
        add+=d[idx-1];
        cnt++;
        used[idx-1]=true;
      }
      if(idx+1<n&&!used[idx+1]&&sta.find(d[idx+1])!=sta.end()){
        sta.erase(sta.find(d[idx+1]));
        add+=d[idx+1];
        cnt++;
        used[idx+1]=true;
      }
      if(cnt==2)stb.insert(add);
    }else{
      res+=tb;
      stb.erase(stb.find(tb));
    }
  }
  cout<<res<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp make_pair
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define long long long // for codeforces

const int INF = 1123456789;

template<typename T>
class MinCostFlow{
private:
  struct edge{int to; T cap, cost; int rev;};
  using P = pair<T,int>;
  vector<vector<edge> > Graph;
  vector<int> prevv, preve;
  vector<T> h, d; // ポテンシャル，最短距離
public:
  MinCostFlow(int v){
    // 頂点数vで初期化
    Graph.resize(v);
    prevv.resize(v);
    preve.resize(v);
    h.resize(v);
    d.resize(v);
  }
  T min_cost_flow(int s, int t, T f){
    T res = 0;
    fill(all(h), 0);
    // 負の辺が含まれるとき
    // rep(v,Graph.size()){
    //   rep(j,Graph[v].size()){
    //     edge &e = Graph[v][j];
    //     if(e.cap==0) continue;
    //     int u = e.to;
    //     h[u] = min(h[u],h[v]+e.cost);
    //   }
    // }
    while(f>0){
      priority_queue<P, vector<P>, greater<P>> pq;
      fill(all(d), INF);
      d[s] = 0;
      pq.push(mp(0,s));
      while(!pq.empty()){
        auto p = pq.top(); pq.pop();
        int v = p.se;
        if(d[v] < p.fi) continue;
        rep(i,Graph[v].size()){
          edge &e = Graph[v][i];
          if(e.cap > 0 && d[e.to] > d[v] + e.cost + h[v] - h[e.to]){
            d[e.to] = d[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            pq.push(mp(d[e.to], e.to));
          }
        }
      }
      if(d[t] == INF) return -1;
      rep(i,Graph.size()) h[i] += d[i];

      T nf = f;
      for(int v=t; v!=s; v = prevv[v]){
        nf = min(nf, Graph[prevv[v]][preve[v]].cap);
      }
      f -= nf;
      res += nf * h[t];
      for(int v=t; v!=s; v=prevv[v]){
        edge &e = Graph[prevv[v]][preve[v]];
        e.cap -= nf;
        Graph[v][e.rev].cap += nf;
      }
    }
    return res;
  }
  void add_edge(int from ,int to, T cap, T cost){
    Graph[from].pb(((edge){to, cap, cost, (int)Graph[to].size()}));
    Graph[to].pb(((edge){from, 0, -cost, (int)Graph[from].size()-1}));
  }
};

int main(){
  int k,n;
  scanf("%d%d", &k, &n);
  vector<int> a(n);
  rep(i,n) scanf("%d", &a[i]);
  sort(all(a));

  MinCostFlow<int> flow(n+2);
  int s = n, t = n+1;

  rep(i,n){
    if(i%2==0) flow.add_edge(s,i,1,0);
    else flow.add_edge(i,t,1,0);
  }

  rep(i,n-1){
    if(i%2==0) flow.add_edge(i, i+1, 1, a[i+1] - a[i]);
    else flow.add_edge(i+1, i, 1, a[i+1] - a[i]);
  }

  cout << flow.min_cost_flow(s, t, k) << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll n,k;
ll a[500001];
ll d[500001];
multiset<ll> sta,stb;

int main(){
  cin>>k>>n;
  rep(i,n){
    scanf("%lld",a+i);
  }
  sort(a,a+n);
  rep(i,n-1){
    d[i]=a[i+1]-a[i];
    sta.insert(d[i]);
  }

  ll res=0;
  rep(i,k){
    ll ta=INF,tb=INF;
    if(sta.size()>0){
      ta=*sta.begin();
    }
    if(stb.size()>0){
      tb=*stb.begin();
    }
    if(ta<tb){
      res+=ta;
      ll idx=lower_bound(d,d+n,ta)-d;
      ll add=-ta;
      sta.erase(sta.find(ta));
      if(idx>0&&sta.find(d[idx-1])!=sta.end()){
        sta.erase(sta.find(d[idx-1]));
        add+=d[idx-1];
      }
      if(idx+1<n&&sta.find(d[idx+1])!=sta.end()){
        sta.erase(sta.find(d[idx+1]));
        add+=d[idx+1];
      }
      if(idx>0&&idx<n-1)stb.insert(add);
    }else{
      res+=tb;
      stb.erase(stb.find(tb));
    }
  }
  cout<<res<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp make_pair
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define long long long // for codeforces

const int INF = 123456789;

template<typename T>
class MinCostFlow{
private:
  struct edge{int to; T cap, cost; int rev;};
  using P = pair<T,int>;
  vector<vector<edge> > Graph;
  vector<int> prevv, preve;
  vector<T> h, d; // ポテンシャル，最短距離
public:
  MinCostFlow(int v){
    // 頂点数vで初期化
    Graph.resize(v);
    prevv.resize(v);
    preve.resize(v);
    h.resize(v);
    d.resize(v);
  }
  T min_cost_flow(int s, int t, T f){
    T res = 0;
    fill(all(h), 0);
    // 負の辺が含まれるとき
    // rep(v,Graph.size()){
    //   rep(j,Graph[v].size()){
    //     edge &e = Graph[v][j];
    //     if(e.cap==0) continue;
    //     int u = e.to;
    //     h[u] = min(h[u],h[v]+e.cost);
    //   }
    // }
    while(f>0){
      priority_queue<P, vector<P>, greater<P>> pq;
      fill(all(d), INF);
      d[s] = 0;
      pq.push(mp(0,s));
      while(!pq.empty()){
        auto p = pq.top(); pq.pop();
        int v = p.se;
        if(d[v] < p.fi) continue;
        rep(i,Graph[v].size()){
          edge &e = Graph[v][i];
          if(e.cap > 0 && d[e.to] > d[v] + e.cost + h[v] - h[e.to]){
            d[e.to] = d[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            pq.push(mp(d[e.to], e.to));
          }
        }
      }
      if(d[t] == INF) return -1;
      rep(i,Graph.size()) h[i] += d[i];

      T nf = f;
      for(int v=t; v!=s; v = prevv[v]){
        nf = min(nf, Graph[prevv[v]][preve[v]].cap);
      }
      f -= nf;
      res += nf * h[t];
      for(int v=t; v!=s; v=prevv[v]){
        edge &e = Graph[prevv[v]][preve[v]];
        e.cap -= nf;
        Graph[v][e.rev].cap += nf;
      }
    }
    return res;
  }
  void add_edge(int from ,int to, T cap, T cost){
    Graph[from].pb(((edge){to, cap, cost, (int)Graph[to].size()}));
    Graph[to].pb(((edge){from, 0, -cost, (int)Graph[from].size()-1}));
  }
};

int main(){
  int k,n;
  scanf("%d%d", &k, &n);
  vector<int> a(n);
  rep(i,n) scanf("%d", &a[i]);
  sort(all(a));

  MinCostFlow<int> flow(n+2);
  int s = n, t = n+1;

  rep(i,n){
    if(i%2==0) flow.add_edge(s,i,1,0);
    else flow.add_edge(i,t,1,0);
  }

  rep(i,n-1){
    if(i%2==0) flow.add_edge(i, i+1, 1, a[i+1] - a[i]);
    else flow.add_edge(i+1, i, 1, a[i+1] - a[i]);
  }

  cout << flow.min_cost_flow(s, t, k) << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define fi first
#define sc second
ll n,k;
ll b[500005];
ll a[500005];
multiset<pair<ll,int> >M;
multiset<pair<int,ll> >rM;
ll ans[500005];
int main(){
  cin >> k >> n;
  for(int i=1;i<=n;i++){
    cin >> b[i];
  }
  sort(b+1,b+n+1);
  n--;
  for(int i=1;i<=n;i++){
    a[i]=b[i+1]-b[i];
    M.insert(mp(a[i],i));
    rM.insert(mp(i,a[i]));
  }
  M.insert(mp(1e18,0)); M.insert(mp(1e18,n+1));
  rM.insert(mp(0,1e18)); rM.insert(mp(n+1,1e18));
  ans[0] = 0;
  for(int num=1;num<=(n+1)/2;num++){
    pair<ll,int> p = *M.begin();
    ans[num] = ans[num-1] + p.first;
    if(num==k)cout << ans[num] << endl;
    multiset<pair<int,ll> >::iterator it = rM.lower_bound(mp(p.sc,-1e18));
    it--;
    pair<int,ll>a = *it;
    M.erase(M.find(mp(a.sc,a.fi)));
    rM.erase(it++);
    pair<int,ll>b = *it;
    M.erase(M.find(mp(b.sc,b.fi)));
    rM.erase(it++);
    pair<int,ll>c = *it;
    M.erase(M.find(mp(c.sc,c.fi)));
    rM.erase(it++);
    rM.insert(mp(b.fi,a.sc+c.sc-b.sc));
    M.insert(mp(a.sc+c.sc-b.sc,b.fi));
  }
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 200005
#define M 5005
#define INF 0x3f3f3f3f
#define R register
using namespace std;

int n, m, a[N], d[N], dp[M][M * 3][2];

int main()
{
    scanf("%d%d", &m, &n);
    for(R int i = 1; i <= n; i++) scanf("%d", a + i);
    sort(a + 1, a + 1 + n);
    for(R int i = 1; i < n; i++) d[i] = a[i + 1] - a[i];
    memset(dp, INF, sizeof dp);
    n--;
    n = min(n, 3 * m);
    for(int i = 0; i <= n; i++) dp[i][0][0] = 0;
    for(int j = 1; j <= m; j++)
        for(int i = 1; i <= n; i++)
        {
            dp[i][j][1] = dp[i - 1][j - 1][0] + d[i];
            dp[i][j][0] = min(dp[i - 1][j][1], dp[i - 1][j][0]);
        }
    // cout << dp[2][1][1] << " " << d[2] << endl;
    printf("%d\n", min(dp[n][m][0], dp[n][m][1]));
    return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 200005
#define M 5005
#define INF 0x3f3f3f3f
#define R register
#define int long long
#define P pair<int, int>
using namespace std;

int n, m, a[N], d[N], dp[M * 3][2][2];
bool vis[N];
P zlh[N];

signed main()
{
    scanf("%lld%lld", &m, &n);
    for(R int i = 1; i <= n; i++) scanf("%lld", a + i);
    sort(a + 1, a + 1 + n);
    for(R int i = 1; i < n; i++) d[i] = a[i + 1] - a[i], zlh[i] = P(d[i], i);
    sort(zlh + 1, zlh + n);
    memset(dp, INF, sizeof dp);
    n--;
    n = min(n, 3 * m);
    for(int i = 1; i <= n; i++) swap(zlh[i].first, zlh[i].second);
    sort(zlh + 1, zlh + 1 + n);
    // for(int i = 1; i <= n; i++) cout << zlh[i].first << " " << zlh[i].second << endl;
    for(R int i = 1; i <= n; i++)
        vis[i] = !(zlh[i].first == zlh[i - 1].first + 1), d[i] = zlh[i].second;
    // for(int i = 1; i <= n; i++) printf("%d ", vis[i]);
    // puts("");
    // for(int i = 1; i <= n; i++) printf("%lld ", d[i]);
    // puts("");
    for(R int i = 0; i <= n; i++) dp[i][0][0] = 0;
    for(R int j = 1; j <= m; j++)
    {
        for(R int i = 1; i <= n; i++)
        {
            if(!vis[i]) dp[i][j & 1][1] = dp[i - 1][!(j & 1)][0] + d[i];
            else dp[i][j & 1][1] = min(dp[i - 1][!(j & 1)][0], dp[i - 1][!(j & 1)][1]) + d[i];
            dp[i][j & 1][0] = min(dp[i - 1][j & 1][1], dp[i - 1][j & 1][0]);
        }
        memset(dp[0], INF, sizeof dp[0]);
    }
    printf("%lld\n", min(dp[n][m & 1][0], dp[n][m & 1][1]));
    return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#define N 500005
#define M 5005
#define INF 0x3f3f3f3f
#define R register
#define int long long
#define P pair<int, int>
using namespace std;

int n, m, a[N], d[N], dp[M * 3][2][2];
bool vis[N];
P zlh[N];

signed main()
{
    scanf("%lld%lld", &m, &n);
    for(R int i = 1; i <= n; i++) scanf("%lld", a + i);
    // srand(time(NULL));
    // m = 50000;
    // n = 200000;
    // for(R int i= 1;i<=n;i++) a[i] = rand();
    sort(a + 1, a + 1 + n);
    for(R int i = 1; i < n; i++) d[i] = a[i + 1] - a[i], zlh[i] = P(d[i], i);
    sort(zlh + 1, zlh + n);
    memset(dp, INF, sizeof dp);
    n--;
    n = min(n, 3 * m);
    // cout<<n<<endl;
    for(int i = 1; i <= n; i++) swap(zlh[i].first, zlh[i].second);
    sort(zlh + 1, zlh + 1 + n);
    for(R int i = 1; i <= n; i++)
        vis[i] = !(zlh[i].first == zlh[i - 1].first + 1), d[i] = zlh[i].second;
    for(R int i = 0; i <= n; i++) dp[i][0][0] = 0;
    for(R int j = 1; j <= m; j++)
    {
        for(R int i = 1; i <= n; i++)
        {
            if(!vis[i]) dp[i][j & 1][1] = dp[i - 1][!(j & 1)][0] + d[i];
            else dp[i][j & 1][1] = min(dp[i - 1][!(j & 1)][0], dp[i - 1][!(j & 1)][1]) + d[i];
            dp[i][j & 1][0] = min(dp[i - 1][j & 1][1], dp[i - 1][j & 1][0]);
        }
        memset(dp[0], INF, sizeof dp[0]);
    }
    printf("%lld\n", min(dp[n][m & 1][0], dp[n][m & 1][1]));
    return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 200005
#define M 5005
#define INF 0x3f3f3f3f
#define R register
using namespace std;

int n, m, a[N], d[N], dp[M][M * 3];

int main()
{
    scanf("%d%d", &m, &n);
    for(R int i = 1; i <= n; i++) scanf("%d", a + i);
    sort(a + 1, a + 1 + n);
    for(R int i = 1; i < n; i++) d[i] = a[i + 1] - a[i];
    memset(dp, INF, sizeof dp);
    n--;
    n = min(n, 3 * m);
    for(int i = 0; i <= n; i++) dp[i][0][0] = 0;
    for(int j = 1; j <= m; j++)
        for(int i = 1; i <= n; i++)
        {
            dp[i][j][1] = dp[i - 1][j - 1][0] + d[i];
            dp[i][j][0] = min(dp[i - 1][j][1], dp[i - 1][j][0]);
        }
    // cout << dp[2][1][1] << " " << d[2] << endl;
    printf("%d\n", min(dp[n][m][0], dp[n][m][1]));
    return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 200005
#define M 5005
#define INF 0x3f3f3f3f
#define R register
#define int long long
using namespace std;

int n, m, a[N], d[N], dp[M * 3][2][2];

signed main()
{
    scanf("%lld%lld", &m, &n);
    for(R int i = 1; i <= n; i++) scanf("%lld", a + i);
    sort(a + 1, a + 1 + n);
    for(R int i = 1; i < n; i++) d[i] = a[i + 1] - a[i];
    memset(dp, INF, sizeof dp);
    n--;
    n = min(n, 3 * m);
    for(int i = 0; i <= n; i++) dp[i][0][0] = 0;
    for(int j = 1; j <= m; j++)
    {
        for(int i = 1; i <= n; i++)
        {
            dp[i][j & 1][1] = dp[i - 1][!(j & 1)][0] + d[i];
            dp[i][j & 1][0] = min(dp[i - 1][j & 1][1], dp[i - 1][j & 1][0]);
        }
        memset(dp[0], INF, sizeof dp[0]);
    }
    printf("%lld\n", min(dp[n][m & 1][0], dp[n][m & 1][1]));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;

constexpr ll inf = 1e18;

int main() {
    int k, n;
    cin >> k >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(begin(a), end(a));

    set<pair<ll, ll>> index_value, value_index;
    for(int i = 0; i + 1 < n; ++i) {
        index_value.emplace(i, a[i + 1] - a[i]);
        value_index.emplace(a[i + 1] - a[i], i);
    }
    index_value.emplace(-1, inf);
    index_value.emplace(n, inf);
    value_index.emplace(inf, -1);
    value_index.emplace(inf, n);

    ll res = 0;
    for(int lp = 0; lp < k; ++lp) {
        int v, l, r;
        ll val, lval, rval;
        tie(val, v) = *begin(value_index);
        auto it = index_value.lower_bound(make_pair(v, val));
        tie(l, lval) = *prev(it);
        tie(r, rval) = *next(it);

        index_value.erase(make_pair(v, val));
        index_value.erase(make_pair(l, lval));
        index_value.erase(make_pair(r, rval));
        value_index.erase(make_pair(val, v));
        value_index.erase(make_pair(lval, l));
        value_index.erase(make_pair(rval, r));
        index_value.emplace(v, lval + rval - val);
        value_index.emplace(lval + rval - val, v);
        res += val;
    }

    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;

constexpr ll inf = 1e18;

int main() {
    int k, n;
    cin >> k >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(begin(a), end(a));

    set<pair<ll, ll>> index_value, value_index;
    for(int i = 0; i + 1 < n; ++i) {
        index_value.emplace(i, a[i + 1] - a[i]);
        value_index.emplace(a[i + 1] - a[i], i);
    }
    index_value.emplace(-1, inf);
    index_value.emplace(n, inf);
    value_index.emplace(inf, -1);
    value_index.emplace(inf, n);

    ll res = 0;
    for(int lp = 0; lp < k; ++lp) {
        int v, l, r;
        ll val, lval, rval;
        tie(val, v) = *begin(value_index);
        auto it = index_value.lower_bound(make_pair(v, val));
        tie(l, lval) = *prev(it);
        tie(r, rval) = *next(it);

        index_value.erase(make_pair(v, val));
        index_value.erase(make_pair(l, lval));
        index_value.erase(make_pair(r, rval));
        value_index.erase(make_pair(val, v));
        value_index.erase(make_pair(lval, l));
        value_index.erase(make_pair(rval, r));
        index_value.emplace(v, lval + rval - val);
        index_value.emplace(lval + rval - val, v);
        res += val;
    }

    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int N=5e4+1,INF=2e9;
int n,k,A[N];
vector<pair<int,int> > v;
int dp[2][N],use[2][N];
bool cmp(pair<int,int> A,pair<int,int> B){
    return (A.second<B.second);
}
int main()
{
    scanf("%d %d",&k,&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
    }
    sort(A+1,A+n+1);
    for(int i=2;i<=n;i++){
        v.push_back(make_pair(A[i]-A[i-1],i-1));
    }
    sort(v.begin(),v.end());

    while(v.size()>3*k) v.pop_back();
    sort(v.begin(),v.end(),cmp);
    for(int i=0;i<2;i++){
        fill(dp[i]+1,dp[i]+k+1,INF);
        fill(use[i]+1,use[i]+k+1,INF);
    }
    dp[1][1]=v[0].first;
    for(int i=1;i<v.size();i++){
        for(int j=1;j<=k;j++){
            use[0][j]=min(dp[0][j],dp[1][j]);
        }
        if(v[i].second==(v[i-1].second+1)){
            for(int j=1;j<=k;j++){
                use[1][j]=dp[0][j-1]+v[i].first;
            }
        }
        else{
            for(int j=1;j<=k;j++){
                use[1][j]=min(dp[0][j-1],dp[1][j-1])+v[i].first;
            }
        }
        for(int j=1;j<=k;j++){
            dp[0][j]=min(dp[1][j],dp[0][j]);
            dp[0][j]=min(dp[0][j],use[0][j]);
        }
        for(int j=1;j<=k;j++){
            dp[1][j]=use[1][j];
        }
        for(int s=0;s<2;s++){
            for(int j=1;j<=k;j++){
                use[s][j]=INF;
            }
        }
    }
    int ans=INF;
    for(int i=0;i<2;i++){
        for(int j=0;j<v.size();j++){
            ans=min(ans,dp[i][k]);
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=5e5+1,INF=1e9+1;
int n,k,A[N];
vector<pair<int,int> > v;
int dp[2][N],use[2][N];
bool cmp(pair<int,int> A,pair<int,int> B){
    return (A.second<B.second);
}
int main()
{
    scanf("%d %d",&k,&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
    }
    sort(A+1,A+n+1);
    for(int i=2;i<=n;i++){
        v.push_back(make_pair(A[i]-A[i-1],i-1));
    }
    sort(v.begin(),v.end());
    while(v.size()>3*k) v.pop_back();
    sort(v.begin(),v.end(),cmp);
    for(int i=0;i<2;i++){
        fill(dp[i]+1,dp[i]+k+1,INF);
        fill(use[i]+1,use[i]+k+1,INF);
    }
    dp[1][1]=v[0].first;
    for(int i=1;i<v.size();i++){
        for(int j=1;j<=k;j++){
            use[0][j]=min(dp[0][j],dp[1][j]);
        }
        if(v[i].second==(v[i-1].second+1)){
            for(int j=1;j<=k;j++){
                use[1][j]=dp[0][j-1]+v[i].first;
            }
        }
        else{
            for(int j=1;j<=k;j++){
                use[1][j]=min(dp[0][j-1],dp[1][j-1])+v[i].first;
            }
        }
        for(int j=1;j<=k;j++){
            dp[0][j]=min(dp[1][j],dp[0][j]);
            dp[0][j]=min(dp[0][j],use[0][j]);
        }
        for(int j=1;j<=k;j++){
            dp[1][j]=use[1][j];
        }
        for(int s=0;s<2;s++){
            for(int j=1;j<=k;j++){
                use[s][j]=INF;
            }
        }
    }
    int ans=INF;
    for(int i=0;i<2;i++){
        ans=min(ans,dp[i][k]);
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=5e4+1,INF=1e9+1;
int n,k,A[N];
vector<pair<int,int> > v;
int dp[2][N],use[2][N];
bool cmp(pair<int,int> A,pair<int,int> B){
    return (A.second<B.second);
}
int main()
{
    scanf("%d %d",&k,&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&A[i]);
    }
    sort(A+1,A+n+1);
    for(int i=2;i<=n;i++){
        v.push_back(make_pair(A[i]-A[i-1],i-1));
    }
    sort(v.begin(),v.end());
    while(v.size()>3*k) v.pop_back();
    sort(v.begin(),v.end(),cmp);
    for(int i=0;i<2;i++){
        fill(dp[i]+1,dp[i]+k+1,INF);
        fill(use[i]+1,use[i]+k+1,INF);
    }
    dp[1][1]=v[0].first;
    for(int i=1;i<v.size();i++){
        for(int j=1;j<=k;j++){
            use[0][j]=min(dp[0][j],dp[1][j]);
        }
        if(v[i].second==(v[i-1].second+1)){
            for(int j=1;j<=k;j++){
                use[1][j]=dp[0][j-1]+v[i].first;
            }
        }
        else{
            for(int j=1;j<=k;j++){
                use[1][j]=min(dp[0][j-1],dp[1][j-1])+v[i].first;
            }
        }
        for(int j=1;j<=k;j++){
            dp[0][j]=min(dp[1][j],dp[0][j]);
            dp[0][j]=min(dp[0][j],use[0][j]);
        }
        for(int j=1;j<=k;j++){
            dp[1][j]=use[1][j];
        }
        for(int s=0;s<2;s++){
            for(int j=1;j<=k;j++){
                use[s][j]=INF;
            }
        }
    }
    int ans=INF;
    for(int i=0;i<2;i++){
        ans=min(ans,dp[i][k]);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define x first
#define y second
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define PI (atan(1)*4)
#define mp make_pair
using namespace std;
const int maxn = 5001;
ll dp[2][maxn][2];
int ptr;
const ll infy = 1e14;
bool comp(pair<ll, int> a, pair<ll, int> b) {
    return a.y < b.y;
}
int main()
{
    fastread;
    int n, k;
    cin >> k >> n;
    vector<int> seq(n);
    vector< pair<ll, int> > v;
    for (int i = 0; i < n; i++) {
        cin >> seq[i];
    }
    sort(seq.begin(), seq.end());
    for (int i = 1; i < n; i++)
        v.pb({seq[i] - seq[i - 1], i - 1});
    sort(v.begin(), v.end());
    while (v.size() > (3 * k))
        v.pop_back();
    sort(v.begin(), v.end(), comp);
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < maxn; j++) {
            for (int p = 0; p < 2; p++) {
                dp[i][j][p] = infy;
            }
        }
    }
    dp[0][0][0] = 0;
    dp[0][1][1] = v[0].x;
    dp[1][0][0] = 0;
    for (int i = 1; i < v.size(); i++) {
        for (int j = 1; j <= k; j++) {
            dp[1 - ptr][j][0] = min(dp[ptr][j][1], dp[ptr][j][0]);
            if (v[i].y == (v[i - 1].y + 1)) {
                dp[1 - ptr][j][1] = dp[ptr][j - 1][0] + v[i].x;
            } else {
                dp[1 - ptr][j][1] = min(dp[ptr][j - 1][0], dp[ptr][j - 1][1]) + v[i].x;
            }
        }
        ptr = 1 - ptr;
    }
    cout << min(dp[ptr][k][0], dp[ptr][k][1]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define SET(x, a) memset(x, a, sizeof x);
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pld pair<ld, ld>
const ll MOD = 1000000007;
const ll INF = 1000000000;
template <class T> T read(T &x) {
  char c;
  int flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag *= -1;
  x = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = x * 10 + c - '0';
  x *= flag;
  return x;
}
#define int ll
int k, n;
int a[500005];
int dp[500005];
int cnt[500005];
bool check (int cost) {
  dp[0] = dp[1] = cnt[0] = cnt[1] = 0;
  for (int i = 2; i <= n; ++i) {
    dp[i] = dp[i-1], cnt[i] = cnt[i-1];
    int x = dp[i-2] + a[i] - a[i-1] - cost;
    if(x < dp[i]) dp[i] = x, cnt[i] = cnt[i-2] + 1;
  }
  return cnt[n] >= k;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> k >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  sort(a + 1, a + 1 + n);
  int l = 0, r = 2 * INF;
  int ans = 2 * INF;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if(check(mid)) ans = mid, r = mid - 1;
    else l = mid + 1;
  }
  check(ans);
  cout << dp[n] + ans * k << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define SET(x, a) memset(x, a, sizeof x);
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pld pair<ld, ld>
const ll MOD = 1000000007;
const ll INF = 1000000000;
template <class T> T read(T &x) {
  char c;
  int flag = 1;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      flag *= -1;
  x = c - '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = x * 10 + c - '0';
  x *= flag;
  return x;
}
#define int ll
int k, n;
int a[500005];
int dp[500005];
int cnt[500005];
bool check (int cost) {
  dp[0] = dp[1] = cnt[0] = cnt[1] = 0;
  for (int i = 2; i <= n; ++i) {
    dp[i] = dp[i-1], cnt[i] = cnt[i-1];
    int x = dp[i-2] + a[i] - a[i-1] - cost;
    if(x < dp[i] || (x == dp[i] && cnt[i-2] + 1 > cnt[i])) dp[i] = x, cnt[i] = cnt[i-2] + 1;
  }
  return cnt[n] >= k;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> k >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  sort(a + 1, a + 1 + n);
  int l = 0, r = 2 * INF;
  int ans = 2 * INF;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if(check(mid)) ans = mid, r = mid - 1;
    else l = mid + 1;
  }
  check(ans);
  cout << dp[n] + ans * k << endl;
}


/////////////////////////   _LeMur_
#include <iostream>
#include <algorithm>
#include <time.h>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

const int N = 500005;
const int K = 5005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int k , n;
long long a[N] , b[N];
long long dp[3][K][3];

int main(){
    scanf("%d%d",&k,&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    sort(a + 1 , a + n + 1);
    for(int i=1;i<n;i++){
        b[i] = a[i + 1] - a[i];
    }
    for(int i=0;i<=k;i++){
        dp[0][i][0] = dp[0][i][1] = inf;
        dp[1][i][0] = dp[1][i][1] = inf;
    }
    dp[0][0][1] = dp[0][0][0] = 0;
    for(int i=1;i<n;i++){
        int v = i % 2;
        for(int j=0;j<=k;j++){
            if(j) dp[v][j][1] = dp[v ^ 1][j - 1][0] + b[i];
            dp[v][j][0] = min(dp[v ^ 1][j][0] , dp[v ^ 1][j][1]);
        }
    }
    cout<<min(dp[(n - 1) % 2][k][0] , dp[(n - 1) % 2][k][1])<<endl;
    return 0;
}


/////////////////////////   _LeMur_
#include <iostream>
#include <algorithm>
#include <time.h>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

const int N = 500005;
const int K = 5005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int k , n;
long long a[N] , b[N];
pair <long long,int> p[N];
long long dp[3][K][3];
vector <int> ind;

int main(){
    scanf("%d%d",&k,&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    sort(a + 1 , a + n + 1);
    for(int i=1;i<n;i++){
        b[i] = a[i + 1] - a[i];
        p[i] = {b[i] , i};
    }
    sort(p + 1 , p + n);
    int sz = min(n - 1 , 3 * k + 5);
    for(int i=1;i<=sz;i++){
        ind.push_back(p[i].second);
    }
    sort(ind.begin() , ind.end());
    for(int i=0;i<=k;i++){
        dp[0][i][0] = dp[0][i][1] = inf;
        dp[1][i][0] = dp[1][i][1] = inf;
    }
    dp[0][0][1] = dp[0][0][0] = 0;
    for(int i=0;i<sz;i++){
        int Id = (i == 0) ? 0 : ind[i - 1];
        int id = ind[i];
        int v = (i + 1) % 2;
        for(int j=0;j<=k;j++){
            if(id > Id + 1){
                if(j) dp[v][j][1] = min(dp[v ^ 1][j - 1][0] , dp[v ^ 1][j - 1][1]) + b[id];
                dp[v][j][0] = min(dp[v ^ 1][j][0] , dp[v ^ 1][j][1]);
            }
            else{
                if(j) dp[v][j][1] = dp[v ^ 1][j - 1][0] + b[id];
                dp[v][j][0] = min(dp[v ^ 1][j][0] , dp[v ^ 1][j][1]);
            }
        }
    }
    cout<<min(dp[sz % 2][k][0] , dp[sz % 2][k][1])<<endl;
    return 0;
}


/////////////////////////   _LeMur_
#include <iostream>
#include <algorithm>
#include <time.h>
#include <stack>
#include <string>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>

using namespace std;

const int N = 500005;
const int K = 5005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int k , n;
int a[N] , b[N];
int dp[3][K][3];

int main(){
    scanf("%d%d",&k,&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    sort(a + 1 , a + n + 1);
    for(int i=1;i<n;i++){
        b[i] = a[i + 1] - a[i];
    }
    for(int i=0;i<=k;i++){
        dp[0][i][0] = dp[0][i][1] = 2 * inf;
        dp[1][i][0] = dp[1][i][1] = 2 * inf;
    }
    dp[0][0][1] = dp[0][0][0] = 0;
    for(int i=1;i<n;i++){
        int v = i % 2;
        for(int j=0;j<=k;j++){
            if(j) dp[v][j][1] = dp[v ^ 1][j - 1][0] + b[i];
            dp[v][j][0] = min(dp[v ^ 1][j][0] , dp[v ^ 1][j][1]);
        }
    }
    cout<<min(dp[(n - 1) % 2][k][0] , dp[(n - 1) % 2][k][1])<<endl;
    return 0;
}


/*input
4 12
15 7 4 19 3 30 14 1 5 23 17 25
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i,j,k)     for(int i = j ; i < k ; ++i)
#define RREP(i,j,k)    for(int i = j ; i >=k ; --i)
#define A    first
#define B    second
#define mp   make_pair
#define pb   emplace_back
#define PII pair<int , int>
#define MEM(i,j)   memset(i , j , sizeof i)
#define ALL(i)     i.begin() , i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0) , cout.sync_with_stdio(0)
#define endl "\n"
///------------------------------------------------------------
#define int long long
#define MAX 500900
#define INF 1000000000000000LL

int n , k , x[MAX];
PII dp[MAX];
PII judge(int cost){
	REP(i , 2 , n + 1)
		dp[i] = min(dp[i - 1] , mp(dp[i - 2].A + cost + x[i] - x[i - 1] , dp[i - 2].B + 1));
	return dp[n];
}
int32_t main(){
	IOS;
	cin >> k >> n;
	REP(i , 1 , n + 1) cin >> x[i];
	sort(x + 1 , x + 1 + n);
	int l = -INF , r = INF;
	while(r - l > 1){
		int mid = l + (r - l) / 2;
		PII res = judge(mid);
		// DB4(mid , res.A , res.B , res.A - mid * res.B);
		if(res.B == k){
			cout << res.A - mid * res.B << endl;
			return 0;
		}
		else if(res.B > k) l = mid;
		else if(res.B < k) r = mid;
	}
	// cout << "here" << endl;
	PII res = judge(r);
	cout << res.A - k * r << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MXN = 5e5 + 5;
const double eps = 1e-8;
int k,n,m,a[MXN],s[MXN]; double f[MXN];
int main() {
#ifdef iloi
	freopen("test.in","r",stdin);
#endif
	ios::sync_with_stdio(false);
	cin>>m>>n;
	for(int i=1;i<=n;++i) cin>>a[i];
	sort(a+1,a+1+n);
	double l=-1e9,r=0;
	while(r-l>eps) {
		double mid = (l+r)/2;
		for(int i=2;i<=n;++i) {
			f[i]=f[i-1];s[i]=s[i-1];
			if(f[i-2]+a[i]-a[i-1]+mid<f[i]) f[i]=f[i-2]+a[i]-a[i-1]+mid,s[i]=s[i-2]+1;
		}
		if(s[n]==m) {
			printf("%lld\n",(long long)(f[n]-mid*m+1e-3));
			return 0;
		}
		if(s[n]>m) l=mid; else r=mid;
	}
	for(int i=2;i<=n;++i) {
		f[i]=f[i-1];s[i]=s[i-1];
		if(f[i-2]+a[i]-a[i-1]+l<f[i]) f[i]=f[i-2]+a[i]-a[i-1]+l,s[i]=s[i-2]+1;
	}
	printf("%lld\n",(long long)(f[n]-l*m+1e-3));
#ifdef iloi
	fprintf(stderr,"My time is %.3lf\n",(double)clock()/CLOCKS_PER_SEC);
#endif
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define per(i,a,b) for(int i=(b),i##_end=(a);i>=i##_end;--i)
#define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i)
#define foe(i,a) for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define Es(x,i) for(Edge *i=G[x];i;i=i->nxt)
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
#ifdef flukehn
#define debug(...) fprintf(stderr, __VA_ARGS__);
#else
#define debug(...)
#endif
typedef double db;
typedef long long ll;
typedef long double lf;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=~0u>>1,mo=1e9+7;
template<typename T>inline void admo(int&a,const T&b){a=(a+b>=mo?a+b-mo:a+b);}
template<typename T>inline void sumo(int&a,const T&b){a=(a-b<0?a-b+mo:a-b);}
template<typename T,typename S>inline bool upmx(T&a,const S&b){return a<b?a=b,1:0;}
template<typename T,typename S>inline bool upmn(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T>inline T gcd(T a,T b){while(b)a%=b,swap(a,b);return a;}
inline int Pow(int a,int b,int p=mo){int r=1;for(;b;b>>=1,a=1ll*a*a%p)if(b&1)r=1ll*r*a%p;return r;}
#define I inline
struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define RX x=0;char t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){db u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;char t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int&x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char&x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char*x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(db&x){RX;RL;RT}I OP db(){db x;TR}I Fr&OP,(lf&x){RX;RL;RT}I OP lf()
{lf x;TR}I Fr&OP,(uint&x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull&x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){db t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char*x){while(*x)P(*x++);RT}
I Fw&OP()(const char*x){while(*x)P(*x++);RT}I Fw&OP()(db x,int y){WL;RT}I Fw&OP()(lf x,int y){WL;RT}};Fw<Cp>out;
const int N=5e5+11,M=5011;
int k,n;
int a[N],d[N],sz;
int f[M<<1][M];
int cmp(int i,int j){
	return a[i]-a[i-1]<a[j]-a[j-1];
}
int main(){
#ifdef flukehn
	freopen("g.in","r",stdin);
#endif
	in,k,n;
	rep(i,1,n)in,a[i];
	sort(a+1,a+n+1);
	rep(i,2,n)d[i]=i;
	sort(d+2,d+n+1,cmp);
	memset(f,0x3f,sizeof f);
	sort(d+2,d+n+1);
	f[0][0]=f[1][0]=0;
	int m=2*k+1;
	rep(i,2,m){
		debug("%d\n",d[i])
		memcpy(f[i],f[i-1],sizeof f[i]);
		rep(j,1,k){
			upmn(f[i][j],f[i-1-(d[i]==d[i-1]+1)][j-1]+a[d[i]]-a[d[i]-1]);
		}
	}
	out,f[m][k],'\n';
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define per(i,a,b) for(int i=(b),i##_end=(a);i>=i##_end;--i)
#define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i)
#define foe(i,a) for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define Es(x,i) for(Edge *i=G[x];i;i=i->nxt)
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
#ifdef flukehn
#define debug(...) fprintf(stderr, __VA_ARGS__);
#else
#define debug(...)
#endif
typedef double db;
typedef long long ll;
typedef long double lf;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=~0u>>1,mo=1e9+7;
template<typename T>inline void admo(int&a,const T&b){a=(a+b>=mo?a+b-mo:a+b);}
template<typename T>inline void sumo(int&a,const T&b){a=(a-b<0?a-b+mo:a-b);}
template<typename T,typename S>inline bool upmx(T&a,const S&b){return a<b?a=b,1:0;}
template<typename T,typename S>inline bool upmn(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T>inline T gcd(T a,T b){while(b)a%=b,swap(a,b);return a;}
inline int Pow(int a,int b,int p=mo){int r=1;for(;b;b>>=1,a=1ll*a*a%p)if(b&1)r=1ll*r*a%p;return r;}
#define I inline
struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define RX x=0;char t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){db u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;char t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int&x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char&x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char*x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(db&x){RX;RL;RT}I OP db(){db x;TR}I Fr&OP,(lf&x){RX;RL;RT}I OP lf()
{lf x;TR}I Fr&OP,(uint&x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull&x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){db t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char*x){while(*x)P(*x++);RT}
I Fw&OP()(const char*x){while(*x)P(*x++);RT}I Fw&OP()(db x,int y){WL;RT}I Fw&OP()(lf x,int y){WL;RT}};Fw<Cp>out;
const int N=5e5+11,M=5011;
int k,n;
int a[N],d[N],sz;
int f[M<<1][M];
int cmp(int i,int j){
	return a[i]-a[i-1]<a[j]-a[j-1];
}
int main(){
#ifdef flukehn
	freopen("g.in","r",stdin);
#endif
	in,k,n;
	rep(i,1,n)in,a[i];
	sort(a+1,a+n+1);
	rep(i,2,n)d[i]=i;
	sort(d+2,d+n+1,cmp);
	memset(f,0x3f,sizeof f);
	int m=2*k+5;
	for(;!d[m];)--m;
	sort(d+2,d+m+1);
	f[0][0]=f[1][0]=0;
	rep(i,2,m){
		debug("%d\n",d[i])
		memcpy(f[i],f[i-1],sizeof f[i]);
		rep(j,1,k){
			upmn(f[i][j],f[i-1-(d[i]==d[i-1]+1)][j-1]+a[d[i]]-a[d[i]-1]);
		}
	}
	out,f[m][k],'\n';
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define per(i,a,b) for(int i=(b),i##_end=(a);i>=i##_end;--i)
#define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i)
#define foe(i,a) for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define Es(x,i) for(Edge *i=G[x];i;i=i->nxt)
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
#ifdef flukehn
#define debug(...) fprintf(stderr, __VA_ARGS__);
#else
#define debug(...)
#endif
typedef double db;
typedef long long ll;
typedef long double lf;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=~0u>>1,mo=1e9+7;
template<typename T>inline void admo(int&a,const T&b){a=(a+b>=mo?a+b-mo:a+b);}
template<typename T>inline void sumo(int&a,const T&b){a=(a-b<0?a-b+mo:a-b);}
template<typename T,typename S>inline bool upmx(T&a,const S&b){return a<b?a=b,1:0;}
template<typename T,typename S>inline bool upmn(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T>inline T gcd(T a,T b){while(b)a%=b,swap(a,b);return a;}
inline int Pow(int a,int b,int p=mo){int r=1;for(;b;b>>=1,a=1ll*a*a%p)if(b&1)r=1ll*r*a%p;return r;}
#define I inline
struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define RX x=0;char t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){db u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;char t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int&x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char&x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char*x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(db&x){RX;RL;RT}I OP db(){db x;TR}I Fr&OP,(lf&x){RX;RL;RT}I OP lf()
{lf x;TR}I Fr&OP,(uint&x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull&x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){db t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char*x){while(*x)P(*x++);RT}
I Fw&OP()(const char*x){while(*x)P(*x++);RT}I Fw&OP()(db x,int y){WL;RT}I Fw&OP()(lf x,int y){WL;RT}};Fw<Cp>out;
const int N=5e5+11,M=5011;
int k,n;
int a[N],d[N],sz;
ll f[M<<1][M];
int cmp(int i,int j){
	return a[i]-a[i-1]<a[j]-a[j-1];
}
int main(){
#ifdef flukehn
	freopen("g.in","r",stdin);
#endif
	in,k,n;
	rep(i,1,n)in,a[i];
	sort(a+1,a+n+1);
	rep(i,2,n)d[i]=i;
	sort(d+2,d+n+1,cmp);
	memset(f,0x3f,sizeof f);
	sort(d+2,d+n+1);
	f[0][0]=f[1][0]=0;
	int m=2*k+1;
	rep(i,2,m){
		debug("%d\n",d[i])
		memcpy(f[i],f[i-1],sizeof f[i]);
		rep(j,1,k){
			upmn(f[i][j],f[i-1-(d[i]==d[i-1]+1)][j-1]+a[d[i]]-a[d[i]-1]);
		}
	}
	out,f[m][k],'\n';
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a),i##_end=(b);i<=i##_end;++i)
#define per(i,a,b) for(int i=(b),i##_end=(a);i>=i##_end;--i)
#define For(i,a,b) for(int i=(a),i##_end=(b);i<i##_end;++i)
#define foe(i,a) for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define Es(x,i) for(Edge *i=G[x];i;i=i->nxt)
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
#ifdef flukehn
#define debug(...) fprintf(stderr, __VA_ARGS__);
#else
#define debug(...)
#endif
typedef double db;
typedef long long ll;
typedef long double lf;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=~0u>>1,mo=1e9+7;
template<typename T>inline void admo(int&a,const T&b){a=(a+b>=mo?a+b-mo:a+b);}
template<typename T>inline void sumo(int&a,const T&b){a=(a-b<0?a-b+mo:a-b);}
template<typename T,typename S>inline bool upmx(T&a,const S&b){return a<b?a=b,1:0;}
template<typename T,typename S>inline bool upmn(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T>inline T gcd(T a,T b){while(b)a%=b,swap(a,b);return a;}
inline int Pow(int a,int b,int p=mo){int r=1;for(;b;b>>=1,a=1ll*a*a%p)if(b&1)r=1ll*r*a%p;return r;}
#define I inline
struct Cg{I char operator()(){return getchar();}};
struct Cp{I void operator()(char x){putchar(x);}};
#define OP operator
#define RT return *this;
#define RX x=0;char t=P();while((t<'0'||t>'9')&&t!='-')t=P();bool f=0;\
if(t=='-')t=P(),f=1;x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define RL if(t=='.'){db u=0.1;for(t=P();t>='0'&&t<='9';t=P(),u*=0.1)x+=u*(t-'0');}if(f)x=-x
#define RU x=0;char t=P();while(t<'0'||t>'9')t=P();x=t-'0';for(t=P();t>='0'&&t<='9';t=P())x=x*10+t-'0'
#define TR *this,x;return x;
I bool IS(char x){return x==10||x==13||x==' ';}template<typename T>struct Fr{T P;I Fr&OP,(int&x)
{RX;if(f)x=-x;RT}I OP int(){int x;TR}I Fr&OP,(ll &x){RX;if(f)x=-x;RT}I OP ll(){ll x;TR}I Fr&OP,(char&x)
{for(x=P();IS(x);x=P());RT}I OP char(){char x;TR}I Fr&OP,(char*x){char t=P();for(;IS(t);t=P());if(~t){for(;!IS
(t)&&~t;t=P())*x++=t;}*x++=0;RT}I Fr&OP,(db&x){RX;RL;RT}I OP db(){db x;TR}I Fr&OP,(lf&x){RX;RL;RT}I OP lf()
{lf x;TR}I Fr&OP,(uint&x){RU;RT}I OP uint(){uint x;TR}I Fr&OP,(ull&x){RU;RT}I OP ull(){ull x;TR}};Fr<Cg>in;
#define WI(S) if(x){if(x<0)P('-'),x=-x;char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
#define WL if(y){db t=0.5;for(int i=y;i--;)t*=0.1;if(x>=0)x+=t;else x-=t,P('-');*this,(ll)(abs(x));P('.');if(x<0)\
x=-x;while(y--){x*=10;x-=floor(x*0.1)*10;P(((int)x)%10+'0');}}else if(x>=0)*this,(ll)(x+0.5);else *this,(ll)(x-0.5);
#define WU(S) if(x){char s[S],c=0;while(x)s[c++]=x%10+'0',x/=10;while(c--)P(s[c]);}else P('0')
template<typename T>struct Fw{T P;I Fw&OP,(int x){WI(10);RT}I Fw&OP()(int x){WI(10);RT}I Fw&OP,(uint x){WU(10);RT}
I Fw&OP()(uint x){WU(10);RT}I Fw&OP,(ll x){WI(19);RT}I Fw&OP()(ll x){WI(19);RT}I Fw&OP,(ull x){WU(20);RT}I Fw&OP()
(ull x){WU(20);RT}I Fw&OP,(char x){P(x);RT}I Fw&OP()(char x){P(x);RT}I Fw&OP,(const char*x){while(*x)P(*x++);RT}
I Fw&OP()(const char*x){while(*x)P(*x++);RT}I Fw&OP()(db x,int y){WL;RT}I Fw&OP()(lf x,int y){WL;RT}};Fw<Cp>out;
const int N=5e5+11,M=5011;
int k,n;
int a[N],d[N],sz;
int f[M<<1][M];
int cmp(int i,int j){
	return a[i]-a[i-1]<a[j]-a[j-1];
}
int main(){
#ifdef flukehn
	freopen("g.in","r",stdin);
#endif
	in,k,n;
	rep(i,1,n)in,a[i];
	sort(a+1,a+n+1);
	rep(i,2,n)d[i]=i;
	sort(d+2,d+n+1,cmp);
	memset(f,0x3f,sizeof f);
	sort(d+2,d+n+1);
	f[0][0]=f[1][0]=0;
	int m=2*k+1;
	if(!d[m])--m;
	rep(i,2,m){
		debug("%d\n",d[i])
		memcpy(f[i],f[i-1],sizeof f[i]);
		rep(j,1,k){
			upmn(f[i][j],f[i-1-(d[i]==d[i-1]+1)][j-1]+a[d[i]]-a[d[i]-1]);
		}
	}
	out,f[m][k],'\n';
}


/*
   ,##.                   ,==.
 ,#    #.                 \ o ',
#        #     _     _     \    \
#        #    (_)   (_)    /    ;
 `#    #'                 /   .'
   `##'                   "=="
*/

#include <bits/stdc++.h>
#define ll long long
#define null NULL
#define double long double

using namespace std;

const ll MOD = 1e9 + 7;

int n, k;
vector<int> a, b;

pair<double, int> calc_dp(double lambda) {
	//cout << "lambda = " << lambda << endl;
	vector<double> dp(n + 1);
	vector<int> cnt(n + 1);
	for (int i = 2; i <= n; ++i) {
		double val = (double)b[i - 2] - lambda;
		if (dp[i - 2] + val < dp[i - 1]) {
			dp[i] = dp[i - 2] + val;
			cnt[i] = cnt[i - 2] + 1;
		}
		else {
			dp[i] = dp[i - 1];
			cnt[i] = cnt[i - 1];
		}
	}
	/*cout << "cnt" << endl;
	for (int i = 0; i <= n; ++i) {
		cout << cnt[i] << " ";
	}
	cout << endl;
	cout << "dp" << endl;
	for (int i = 0; i <= n; ++i) {
		cout << dp[i] << " ";
	}
	cout << endl;*/
	return {dp[n], cnt[n]};
}

int solve() {
	double l = 0, r = 1e16;
	for (int i = 0; i < 75; ++i) {
		double mid = (l + r) / 2;
		pair<double, int> res = calc_dp(mid);
		if (res.second > k)
			r = mid;
		else
			l = mid;
	}
	pair<double, int> res = calc_dp(l);
	double t = res.first + l * (double)k;
	//cout << "t = " << t << endl;
	return (int)(t + 0.5);
}

int main() {
    //cout.setf(ios_base::fixed);
    //cout.precision(8);
	cin >> k >> n;
	a.resize(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	for (int i = 1; i < n; ++i) {
		b.push_back(a[i] - a[i - 1]);
	}
	int res = solve();
	cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int64;
const int MAX = 5e5 + 5;
int k, n, a[MAX];
multiset <pair <int64, int> > s_val;
multiset <pair <int, int64> > s_pos;

int main() {
	scanf("%d%d", &k, &n);

	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);

	sort(a + 1, a + n + 1);

	for(int i = 1; i < n; i++) {
		s_pos.insert({i, a[i + 1] - a[i]});
		s_val.insert({a[i + 1] - a[i], i});
	}

	long long ans = 0;

	while(k > 0) {
		int64 val = s_val.begin() -> first;
		int pos = s_val.begin() -> second;
		s_val.erase(s_val.begin());

		ans += val;

		//printf("%lld\n", val);

		int64 sum = -val;

		auto l = s_pos.lower_bound({pos, LLONG_MIN});

		assert(l != s_pos.end());

		int id = pos, has = 0;

		if(l != s_pos.begin()) {
			has++;
			l--;

			s_val.erase(s_val.find({l -> second, l -> first}));

			sum += l -> second;
			id = min(id, l -> first);
			l++;
		}

		l++;

		if(l != s_pos.end()) {
			has++;
			s_val.erase(s_val.find({l -> second, l -> first}));

			sum += l -> second;
			id = min(id, l -> first);
			l--;
		}

		//borrar de s_pos

		auto it = s_pos.lower_bound({pos, LLONG_MIN});

		if(it != s_pos.begin()) {
			it--;
			s_pos.erase(it);
		}

		it = s_pos.lower_bound({pos, LLONG_MIN});

		s_pos.erase(it);

		it = s_pos.lower_bound({pos, LLONG_MIN});

		if(it != s_pos.end())
			s_pos.erase(it);

		//insertar

		if(has == 2) {
			s_pos.insert({id, sum});
			s_val.insert({sum, id});
		}

		k--;
	}

	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
#define INF 1e18
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 200002, K = 5001;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
ll dp[2][K][2];
ll mid;

ll solve() {
    n = arr2.size();
    int p = 1;
    dp[0][0][1] = dp[0][1][0] = INF;
    dp[0][1][1] = arr2[0];
    for(int j = 2;j<=k;j++)
        dp[0][j][0] = dp[0][j][1] = INF;
    for(int i = 1;i<n;i++) {
        for(int j = 0;j<=k;j++) {
            dp[p][j][0] = min(dp[!p][j][0], dp[!p][j][1]);
            dp[p][j][1] = j?dp[!p][j-1][0] + arr2[i]:INF;
        }
        p ^= 1;
    }
    return min(dp[!p][k][0], dp[!p][k][1]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 3*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        if(taken[i])
            arr2.push_back(taken[i]);
    printf("%lld", solve());
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
#define INF 1e18
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 200002, K = 5001;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
ll dp[2][K][2];
ll mid;

ll solve() {
    int i = 0;
    while(!arr2[i])
        i++;
    int p = 1;
    dp[0][0][1] = dp[0][1][0] = INF;
    dp[0][1][1] = arr2[i++];
    for(int j = 2;j<=k;j++)
        dp[0][j][0] = dp[0][j][1] = INF;
    for(;i<n;i++) {
        if(!arr2[i])
            continue;
        for(int j = 0;j<=k;j++) {
            dp[p][j][0] = min(dp[!p][j][0], dp[!p][j][1]);
            if(arr2[i-1])
                dp[p][j][1] = j?dp[!p][j-1][0] + arr2[i]:INF;
            else
                dp[p][j][1] = j?min(dp[!p][j-1][0] + arr2[i], dp[!p][j-1][1] + arr2[i]):INF;
        }
        p ^= 1;
    }
    return min(dp[!p][k][0], dp[!p][k][1]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 3*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        arr2.push_back(taken[i]);
    n--;
    printf("%lld", solve());
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
#define INF 1e18
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 500005, K = 5005;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
ll dp[2][K][2];
ll mid;

ll solve() {
    int i = 0;
    while(!arr2[i])
        i++;
    int p = 1;
    dp[0][0][1] = dp[0][1][0] = INF;
    dp[0][1][1] = arr2[i++];
    for(int j = 2;j<=k;j++)
        dp[0][j][0] = dp[0][j][1] = INF;
    for(;i<n;i++) {
        if(!arr2[i])
            continue;
        for(int j = 0;j<=k;j++) {
            dp[p][j][0] = min(dp[!p][j][0], dp[!p][j][1]);
            if(arr2[i-1])
                dp[p][j][1] = j?dp[!p][j-1][0] + arr2[i]:INF;
            else
                dp[p][j][1] = j?min(dp[!p][j-1][0] + arr2[i], dp[!p][j-1][1] + arr2[i]):INF;
        }
        p ^= 1;
    }
    return min(dp[!p][k][0], dp[!p][k][1]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 3*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        arr2.push_back(taken[i]);
    n--;
    printf("%lld", solve());
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 200002, K = 5005;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
ll dp[4*K][K];
bool vis[N][2];
int cnt[N][2];
ll mid;
ll solve(int i, int rem) {
    if(rem < 0)
        return 1e18;
    if(i >= arr2.size())
        return rem == 0?0:1e18;
    if(dp[i][rem] != -1)
        return dp[i][rem];
    return dp[i][rem] = min(solve(i+1, rem), solve(i+2, rem-1) + arr2[i]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 4*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        if(taken[i])
            arr2.push_back(taken[i]);
    memset(dp,-1,sizeof dp);
    printf("%lld", solve(0,k));
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 200002, K = 5001;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
ll dp[3*K][K];
ll mid;
ll solve(int i, int rem) {
    if(rem < 0)
        return 1e18;
    if(i >= arr2.size())
        return rem == 0?0:1e18;
    if(dp[i][rem] != -1)
        return dp[i][rem];
    return dp[i][rem] = min(solve(i+1, rem), solve(i+2, rem-1) + arr2[i]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 3*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        if(taken[i])
            arr2.push_back(taken[i]);
    memset(dp,-1,sizeof dp);
    printf("%lld", solve(0,k));
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 200002, K = 5001;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
ll dp[4*K][K];
ll mid;
ll solve(int i, int rem) {
    if(rem < 0)
        return 1e18;
    if(i >= arr2.size())
        return rem == 0?0:1e18;
    if(dp[i][rem] != -1)
        return dp[i][rem];
    return dp[i][rem] = min(solve(i+1, rem), solve(i+2, rem-1) + arr2[i]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 4*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        if(taken[i])
            arr2.push_back(taken[i]);
    memset(dp,-1,sizeof dp);
    printf("%lld", solve(0,k));
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define BASE 37
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 200002, K = 5001;
int n,k;
int a[N];
vector<pii> arr;
vector<int> arr2;
int taken[N];
int dp[3*K][K];
ll mid;
ll solve(int i, int rem) {
    if(rem < 0)
        return 1e18;
    if(i >= arr2.size())
        return rem == 0?0:1e18;
    if(dp[i][rem] != -1)
        return dp[i][rem];
    return dp[i][rem] = min(solve(i+1, rem), solve(i+2, rem-1) + arr2[i]);
}

int main()
{
    scanf("%d%d",&k,&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = 1;i<n;i++)
        arr.push_back( {a[i] - a[i-1], i-1});
    sort(arr.begin(),arr.end());
    for(int i = 0;i<min((int)arr.size(), 3*k);i++)
        taken[arr[i].second] = arr[i].first;
    for(int i = 0;i<n-1;i++)
        if(taken[i])
            arr2.push_back(taken[i]);
    memset(dp,-1,sizeof dp);
    printf("%d", solve(0,k));
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
const long long MOD = 100000000007;
const long long M = 500100;
long long a[M], k,n;
pair<long long,long long> b[M];
pair<long long,long long> d[M];
void tryuse(pair<long long,long long> &x, long long y, long long index) {
    if (x.second > y) {
        x = {index, y};
    }
}
void out(pair<long long,long long> x) {
    printf("{%lld %lld}\n ",x.first, x.second);
}

int main ( )
{
    scanf("%lld %lld",&k,&n);
    for (long long i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    sort(a, a+n);
    for (long long i = 0; i < n - 1; i++) {
        b[i] = {a[i+1] - a[i], i};
    }
    sort(b, b+n-1);
    for (long long i = 0; i < min(2 * k, n -1); i++) {
        d[i] = {b[i].second, b[i].first};
    }
    sort(d, d + min(2 * k, n -1));
    pair<long long,long long> f[3][5050][3];
    long long current = 1;
    long long last = 0;
    for (long long i = 0; i <= k; i++) {
        for (long long sta = 0; sta <= 1; sta++) {
            f[current][i][sta] = {-10,1000000000};
        }
    }
    f[current][0][0] = {-10, 0};
    f[current][1][1] = {d[0].first, d[0].second};

    for (long long i = 1; i < min(2 * k, n - 1); i++) {
        swap(current, last);
        for (long long j = 1; j <= k; j++) {
            f[current][j][0] = {-10, 1000000000};
            if (i >= j) {
                tryuse(f[current][j][0], f[last][j][0].second, f[last][j][0].first);
                tryuse(f[current][j][0], f[last][j][1].second, f[last][j][1].first);
            }
            //printf("%lld,%lld 0: %lld %lld\n",i, j, f[current][j][0].first, f[current][j][0].second);

            f[current][j][1] = {-10, 1000000000};
            if (i+1 >= j) {
                if (f[last][j-1][1].first < d[i].first - 1) {
                    tryuse(f[current][j][1], f[last][j-1][1].second + d[i].second, d[i].first);
                }
                tryuse(f[current][j][1],f[last][j-1][0].second + d[i].second, d[i].first);
            }
            //printf("%lld,%lld 1: %lld %lld\n",i,j,f[current][j][1].first, f[current][j][1].second);
        }
    }
    pair<long long,long long> ans  = {-10, 1000000000};
    tryuse(ans, f[current][k][0].second, 0);
    tryuse(ans, f[current][k][1].second, 0);
    cout<< ans.second;
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
const long long MOD = 100000000007;
const long long M = 500100;
long long a[M], k,n;
pair<long long,long long> b[M];
pair<long long,long long> d[M];
void tryuse(pair<long long,long long> &x, long long y, long long index) {
    if (x.second > y) {
        x = {index, y};
    }
}
void out(pair<long long,long long> x) {
    printf("{%lld %lld}\n ",x.first, x.second);
}

int main ( )
{
    scanf("%lld %lld",&k,&n);
    long long t = min(3 * k, n -1);
    for (long long i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    sort(a, a+n);
    for (long long i = 0; i < n - 1; i++) {
        b[i] = {a[i+1] - a[i], i};
    }
    sort(b, b+n-1);

    for (long long i = 0; i < t; i++) {
        d[i] = {b[i].second, b[i].first};
    }
    sort(d, d + t);
    pair<long long,long long> f[3][5050][3];
    long long current = 1;
    long long last = 0;
    for (long long i = 0; i <= k; i++) {
        for (long long sta = 0; sta <= 1; sta++) {
            f[current][i][sta] = {-10,1000000000};
        }
    }
    f[current][0][0] = {-10, 0};
    f[current][1][1] = {d[0].first, d[0].second};\
    for (long long i = 1; i <t; i++) {
        swap(current, last);
        for (long long j = 0; j <= k; j++) {
            f[current][j][0] = {-10, 1000000000};
            if (i >= j) {
                tryuse(f[current][j][0], f[last][j][0].second, f[last][j][0].first);
                tryuse(f[current][j][0], f[last][j][1].second, f[last][j][1].first);
            }
            //printf("%lld,%lld 0: %lld %lld\n",i, j, f[current][j][0].first, f[current][j][0].second);

            f[current][j][1] = {-10, 1000000000};
            if (j > 0 && i+1 >= j) {
                if (f[last][j-1][1].first < d[i].first - 1) {
                    tryuse(f[current][j][1], f[last][j-1][1].second + d[i].second, d[i].first);
                }
                tryuse(f[current][j][1],f[last][j-1][0].second + d[i].second, d[i].first);
            }
            //printf("%lld,%lld 1: %lld %lld\n",i,j,f[current][j][1].first, f[current][j][1].second);
        }
    }
    pair<long long,long long> ans  = {-10, 1000000000};
    tryuse(ans, f[current][k][0].second, 0);
    tryuse(ans, f[current][k][1].second, 0);
    cout<< ans.second;
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
const long long MOD = 100000000007;
const long long M = 500100;
long long a[M], k,n;
pair<long long,long long> b[M];
pair<long long,long long> d[M];
void tryuse(pair<long long,long long> &x, long long y, long long index) {
    if (x.second > y) {
        x = {index, y};
    }
}
void out(pair<long long,long long> x) {
    printf("{%lld %lld}\n ",x.first, x.second);
}

int main ( )
{
    scanf("%lld %lld",&k,&n);
    long long t = min(2 * k+10, n -1);
    for (long long i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    sort(a, a+n);
    for (long long i = 0; i < n - 1; i++) {
        b[i] = {a[i+1] - a[i], i};
    }
    sort(b, b+n-1);

    for (long long i = 0; i < t; i++) {
        d[i] = {b[i].second, b[i].first};
    }
    sort(d, d + t);
    pair<long long,long long> f[3][5050][3];
    long long current = 1;
    long long last = 0;
    for (long long i = 0; i <= k; i++) {
        for (long long sta = 0; sta <= 1; sta++) {
            f[current][i][sta] = {-10,1000000000};
        }
    }
    f[current][0][0] = {-10, 0};
    f[current][1][1] = {d[0].first, d[0].second};\
    for (long long i = 1; i <t; i++) {
        swap(current, last);
        for (long long j = 0; j <= k; j++) {
            f[current][j][0] = {-10, 1000000000};
            if (i >= j) {
                tryuse(f[current][j][0], f[last][j][0].second, f[last][j][0].first);
                tryuse(f[current][j][0], f[last][j][1].second, f[last][j][1].first);
            }
            //printf("%lld,%lld 0: %lld %lld\n",i, j, f[current][j][0].first, f[current][j][0].second);

            f[current][j][1] = {-10, 1000000000};
            if (j > 0 && i+1 >= j) {
                if (f[last][j-1][1].first < d[i].first - 1) {
                    tryuse(f[current][j][1], f[last][j-1][1].second + d[i].second, d[i].first);
                }
                tryuse(f[current][j][1],f[last][j-1][0].second + d[i].second, d[i].first);
            }
            //printf("%lld,%lld 1: %lld %lld\n",i,j,f[current][j][1].first, f[current][j][1].second);
        }
    }
    pair<long long,long long> ans  = {-10, 1000000000};
    tryuse(ans, f[current][k][0].second, 0);
    tryuse(ans, f[current][k][1].second, 0);
    cout<< ans.second;
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
const long long MOD = 100000000007;
const long long M = 500100;
long long a[M], k,n;
pair<long long,long long> b[M];
pair<long long,long long> d[M];
void tryuse(pair<long long,long long> &x, long long y, long long index) {
    if (x.second > y) {
        x = {index, y};
    }
}
void out(pair<long long,long long> x) {
    printf("{%lld %lld}\n ",x.first, x.second);
}

int main ( )
{
    scanf("%lld %lld",&k,&n);
    for (long long i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    sort(a, a+n);
    for (long long i = 0; i < n - 1; i++) {
        b[i] = {a[i+1] - a[i], i};
    }
    sort(b, b+n-1);

    for (long long i = 0; i < min(2 * k, n -1); i++) {
        d[i] = {b[i].second, b[i].first};
    }
    sort(d, d + min(2 * k, n -1));
    pair<long long,long long> f[3][5050][3];
    long long current = 1;
    long long last = 0;
    for (long long i = 0; i <= k; i++) {
        for (long long sta = 0; sta <= 1; sta++) {
            f[current][i][sta] = {-10,1000000000};
        }
    }
    f[current][0][0] = {-10, 0};
    f[current][1][1] = {d[0].first, d[0].second};\
    for (long long i = 1; i < min(2 * k, n - 1); i++) {
        swap(current, last);
        for (long long j = 0; j <= k; j++) {
            f[current][j][0] = {-10, 1000000000};
            if (i >= j) {
                tryuse(f[current][j][0], f[last][j][0].second, f[last][j][0].first);
                tryuse(f[current][j][0], f[last][j][1].second, f[last][j][1].first);
            }
            //printf("%lld,%lld 0: %lld %lld\n",i, j, f[current][j][0].first, f[current][j][0].second);

            f[current][j][1] = {-10, 1000000000};
            if (j > 0 && i+1 >= j) {
                if (f[last][j-1][1].first < d[i].first - 1) {
                    tryuse(f[current][j][1], f[last][j-1][1].second + d[i].second, d[i].first);
                }
                tryuse(f[current][j][1],f[last][j-1][0].second + d[i].second, d[i].first);
            }
            //printf("%lld,%lld 1: %lld %lld\n",i,j,f[current][j][1].first, f[current][j][1].second);
        }
    }
    pair<long long,long long> ans  = {-10, 1000000000};
    tryuse(ans, f[current][k][0].second, 0);
    tryuse(ans, f[current][k][1].second, 0);
    cout<< ans.second;
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
const long long MOD = 100000000007;
const long long M = 500100;
long long a[M], k,n;
pair<long long,long long> b[M];
pair<long long,long long> d[M];
void tryuse(pair<long long,long long> &x, long long y, long long index) {
    if (x.second > y) {
        x = {index, y};
    }
}
void out(pair<long long,long long> x) {
    printf("{%lld %lld}\n ",x.first, x.second);
}

int main ( )
{
    scanf("%lld %lld",&k,&n);
    long long t = min(2 * k+2, n -1);
    for (long long i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    sort(a, a+n);
    for (long long i = 0; i < n - 1; i++) {
        b[i] = {a[i+1] - a[i], i};
    }
    sort(b, b+n-1);

    for (long long i = 0; i < t; i++) {
        d[i] = {b[i].second, b[i].first};
    }
    sort(d, d + t);
    pair<long long,long long> f[3][5050][3];
    long long current = 1;
    long long last = 0;
    for (long long i = 0; i <= k; i++) {
        for (long long sta = 0; sta <= 1; sta++) {
            f[current][i][sta] = {-10,1000000000};
        }
    }
    f[current][0][0] = {-10, 0};
    f[current][1][1] = {d[0].first, d[0].second};\
    for (long long i = 1; i <t; i++) {
        swap(current, last);
        for (long long j = 0; j <= k; j++) {
            f[current][j][0] = {-10, 1000000000};
            if (i >= j) {
                tryuse(f[current][j][0], f[last][j][0].second, f[last][j][0].first);
                tryuse(f[current][j][0], f[last][j][1].second, f[last][j][1].first);
            }
            //printf("%lld,%lld 0: %lld %lld\n",i, j, f[current][j][0].first, f[current][j][0].second);

            f[current][j][1] = {-10, 1000000000};
            if (j > 0 && i+1 >= j) {
                if (f[last][j-1][1].first < d[i].first - 1) {
                    tryuse(f[current][j][1], f[last][j-1][1].second + d[i].second, d[i].first);
                }
                tryuse(f[current][j][1],f[last][j-1][0].second + d[i].second, d[i].first);
            }
            //printf("%lld,%lld 1: %lld %lld\n",i,j,f[current][j][1].first, f[current][j][1].second);
        }
    }
    pair<long long,long long> ans  = {-10, 1000000000};
    tryuse(ans, f[current][k][0].second, 0);
    tryuse(ans, f[current][k][1].second, 0);
    cout<< ans.second;
    return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

// less_equal if supports multiple items
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    dataSet;
const long long MOD = 100000000007;
const long long M = 500100;
long long a[M], k,n;
pair<long long,long long> b[M];
pair<long long,long long> d[M];
void tryuse(pair<long long,long long> &x, long long y, long long index) {
    if (x.second > y) {
        x = {index, y};
    }
}
void out(pair<long long,long long> x) {
    printf("{%lld %lld}\n ",x.first, x.second);
}

int main ( )
{
    scanf("%lld %lld",&k,&n);
    for (long long i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
    }
    sort(a, a+n);
    for (long long i = 0; i < n - 1; i++) {
        b[i] = {a[i+1] - a[i], i};
    }
    sort(b, b+n-1);
    for (long long i = 0; i < min(2 * k, n -1); i++) {
        d[i] = {b[i].second, b[i].first};
    }
    sort(d, d + min(2 * k+2, n -1));
    pair<long long,long long> f[3][5050][3];
    long long current = 1;
    long long last = 0;
    for (long long i = 0; i <= k; i++) {
        for (long long sta = 0; sta <= 1; sta++) {
            f[current][i][sta] = {-10,1000000000};
        }
    }
    f[current][0][0] = {-10, 0};
    f[current][1][1] = {d[0].first, d[0].second};

    for (long long i = 1; i < min(2 * k+2, n - 1); i++) {
        swap(current, last);
        for (long long j = 0; j <= k; j++) {
            f[current][j][0] = {-10, 1000000000};
            if (i >= j) {
                tryuse(f[current][j][0], f[last][j][0].second, f[last][j][0].first);
                tryuse(f[current][j][0], f[last][j][1].second, f[last][j][1].first);
            }
            //printf("%lld,%lld 0: %lld %lld\n",i, j, f[current][j][0].first, f[current][j][0].second);

            f[current][j][1] = {-10, 1000000000};
            if (j > 0 && i+1 >= j) {
                if (f[last][j-1][1].first < d[i].first - 1) {
                    tryuse(f[current][j][1], f[last][j-1][1].second + d[i].second, d[i].first);
                }
                tryuse(f[current][j][1],f[last][j-1][0].second + d[i].second, d[i].first);
            }
            //printf("%lld,%lld 1: %lld %lld\n",i,j,f[current][j][1].first, f[current][j][1].second);
        }
    }
    pair<long long,long long> ans  = {-10, 1000000000};
    tryuse(ans, f[current][k][0].second, 0);
    tryuse(ans, f[current][k][1].second, 0);
    cout<< ans.second;
    return 0;
}