Problem p03137 (C++)

Buggy submission (status: Runtime Error):
/*|In The Name of Allah|*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int, int>> vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define el '\n'
#define lp(i, n) for (int i = 0; i < int(n); i++)
#define rlp(i, n) for (int i = (n)-1; i >= 0; i--)
#define rng(i, a, b) for (int i = int(a); i <= int(b); i++)
#define mp(x, y) make_pair((x), (y))
#define pb(x) push_back((x))
#define all(v) ((v).begin()), ((v).end())
#define sz(x) ((int)(x).size())
#define bits(a) __builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(), v.rend())
#define sortaa(a, n) sort(a, a + n)
#define sortad(a, n) sort(a, a + n), reverse(a, a + n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it, x)                                                         \
  for (__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

void MisSiOn_ImPosSiBlE() {
  // freopen("portals.in", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

int const N = 1e3 + 9, mod = 1e9 + 7, oo = 0x3f3f3f3f;
//------------------------------------------------------------------------------------

int n;
ll res, arr[N];
int main() {
  MisSiOn_ImPosSiBlE();

  vi v;
  int m;
  cin >> m >> n;
  m--;
  lp(i, n) cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 1; i < n; i++) {
    v.push_back(arr[i] - arr[i - 1]);
  }

  sortvd(v);
  lp(i, v.size()) {
    if (m)
      m--;
    else {
      res += v[i];
    }
  }

  cout << res;

  /*
   */
  return 0;
}


Fixed submission:
/*|In The Name of Allah|*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int, int>> vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define el '\n'
#define lp(i, n) for (int i = 0; i < int(n); i++)
#define rlp(i, n) for (int i = (n)-1; i >= 0; i--)
#define rng(i, a, b) for (int i = int(a); i <= int(b); i++)
#define mp(x, y) make_pair((x), (y))
#define pb(x) push_back((x))
#define all(v) ((v).begin()), ((v).end())
#define sz(x) ((int)(x).size())
#define bits(a) __builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(), v.rend())
#define sortaa(a, n) sort(a, a + n)
#define sortad(a, n) sort(a, a + n), reverse(a, a + n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it, x)                                                         \
  for (__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

void MisSiOn_ImPosSiBlE() {
  // freopen("portals.in", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

int const N = 5e5 + 9, mod = 1e9 + 7, oo = 0x3f3f3f3f;
//------------------------------------------------------------------------------------

int n;
ll res, arr[N];
int main() {
  MisSiOn_ImPosSiBlE();

  vi v;
  int m;
  cin >> m >> n;
  m--;
  lp(i, n) cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 1; i < n; i++) {
    v.push_back(arr[i] - arr[i - 1]);
  }

  sortvd(v);
  lp(i, v.size()) {
    if (m)
      m--;
    else {
      res += v[i];
    }
  }

  cout << res;

  /*
   */
  return 0;
}