Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
typedef double db;
typedef string str;
typedef vector<str> vs;

#define in(x) cin >> x
#define ini(x)                                                                 \
  int x;                                                                       \
  cin >> x
#define inl(x)                                                                 \
  ll x;                                                                        \
  cin >> x
#define ins(x)                                                                 \
  str s;                                                                       \
  cin >> x

#define out(x) cout << x
#define outb(x) cout << x << ' '
#define outn(x) cout << x << '\n'

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz size()
#define len length()
#define all(x) x.begin(), x.end()
#define alln(a, n) a, a + n

#define forn(i, s, e) for (int i = s; i < e; i++)
#define forr(i, s, e) for (int i = s; i >= e; i--)
#define ford(i, s, e, d) for (int i = s; i < e; i += d)
#define fors(i, n) for (int i = 1; i * i <= n; i++)

const int MAX = (int)1e5 + 9;

int arr[MAX];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ini(n);
  ini(m);
  forn(i, 0, m) in(arr[i]);
  sort(alln(arr, m));
  int md = arr[m - 1] - arr[0];
  vi d;
  forn(i, 1, m) d.pb(arr[i] - arr[i - 1]);
  sort(all(d));
  reverse(all(d));
  forn(i, 0, n - 1) md -= d[i];
  outn(md);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
typedef double db;
typedef string str;
typedef vector<str> vs;

#define in(x) cin >> x
#define ini(x)                                                                 \
  int x;                                                                       \
  cin >> x
#define inl(x)                                                                 \
  ll x;                                                                        \
  cin >> x
#define ins(x)                                                                 \
  str s;                                                                       \
  cin >> x

#define out(x) cout << x
#define outb(x) cout << x << ' '
#define outn(x) cout << x << '\n'

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define sz size()
#define len length()
#define all(x) x.begin(), x.end()
#define alln(a, n) a, a + n

#define forn(i, s, e) for (int i = s; i < e; i++)
#define forr(i, s, e) for (int i = s; i >= e; i--)
#define ford(i, s, e, d) for (int i = s; i < e; i += d)
#define fors(i, n) for (int i = 1; i * i <= n; i++)

const int MAX = (int)1e5 + 9;

int arr[MAX];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ini(n);
  ini(m);
  forn(i, 0, m) in(arr[i]);
  if (n >= m) {
    outn(0);
    return 0;
  }
  sort(alln(arr, m));
  int md = arr[m - 1] - arr[0];
  vi d;
  forn(i, 1, m) d.pb(arr[i] - arr[i - 1]);
  sort(all(d));
  reverse(all(d));
  forn(i, 0, n - 1) md -= d[i];
  outn(md);

  return 0;
}