#include <bits/stdc++.h>
using namespace std;
const long long max_n = 500010;
const long long inf = 1e9 + 7;
void read(long long &a, char c = 0) {
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (a = 0; isdigit(c); a = a * 10 + c - '0', c = getchar())
    ;
}
long long n, k, ans;
long long val[max_n], pre[max_n], nxt[max_n], a[max_n];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    q;
int main() {
  read(k);
  read(n);
  long long base;
  for (long long i = 1; i <= n; ++i) read(a[i]);
  sort(a + 1, a + n + 1);
  for (long long i = 1; i <= n; ++i) {
    val[i] = a[i] - base;
    pre[i] = i - 1;
    nxt[i] = i + 1;
    base = a[i];
  }
  for (long long i = 2; i <= n; ++i) q.push(make_pair(val[i], i));
  pre[2] = 0;
  nxt[n] = 0;
  for (long long i = 1; i <= k; ++i) {
    while (q.top().first != val[q.top().second]) q.pop();
    long long x = q.top().second;
    long long L = pre[x], R = nxt[x];
    ans += val[x];
    q.pop();
    pre[nxt[x] = nxt[R]] = x;
    nxt[pre[x] = pre[L]] = x;
    val[x] = L && R ? min(inf, val[L] + val[R] - val[x]) : inf;
    val[L] = val[R] = inf;
    q.push(make_pair(val[x], x));
  }
  printf("%I64d\n", ans);
  return 0;
}
