Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long unsigned int ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs, x)                                                         \
  for (int i = 0; i < (x); i++)                                                \
    scanf("%d", &xs[i]);
#define fillLong(xs, x)                                                        \
  for (int i = 0; i < (x); i++)                                                \
    scanf("%ld", &xs[i]);
#define fillString(xs, x)                                                      \
  for (int i = 0; i < (x); i++)                                                \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvdi(xs) sort(xs.begin(), xs.end(), std::greater<long>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)

#define rep(i, n) for (auto i = 0; i < (n); i++)
#define isValidPoint(x, y, mx, my) x >= 0 && x < mx &&y >= 0 && y < my

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MOD = 1000000007;

long a[10010];
long b[10010];

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

  long n, m;
  cin >> n >> m;

  fillLong(a, m);
  sort(a, a + m);

  rep(i, m - 1) { b[i] = a[i + 1] - a[i]; }
  sort(b, b + m - 1, greater<long>());

  if (m == 1) {
    cout << 0 << endl;
  } else {
    long res = a[m - 1] - a[0];
    long j = 0;
    while (j < min(m - 1, n - 1)) {
      res -= b[j];
      j++;
    }
    cout << res << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long unsigned int ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define fillInt(xs, x)                                                         \
  for (int i = 0; i < (x); i++)                                                \
    scanf("%d", &xs[i]);
#define fillLong(xs, x)                                                        \
  for (int i = 0; i < (x); i++)                                                \
    scanf("%ld", &xs[i]);
#define fillString(xs, x)                                                      \
  for (int i = 0; i < (x); i++)                                                \
    cin >> xs[i];
#define sortv(xs) sort(xs.begin(), xs.end())
#define sortvdi(xs) sort(xs.begin(), xs.end(), std::greater<long>())
#define lbv(xs, x) lower_bound(xs.begin(), xs.end(), x) - xs.begin()
#define ubv(xs, x) upper_bound(xs.begin(), xs.end(), x) - xs.begin()
#define bs(xs, x) binary_search(xs.begin(), xs.end(), x)

#define rep(i, n) for (auto i = 0; i < (n); i++)
#define isValidPoint(x, y, mx, my) x >= 0 && x < mx &&y >= 0 && y < my

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

const int MOD = 1000000007;

long a[100010];
long b[100010];

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

  long n, m;
  cin >> n >> m;

  fillLong(a, m);
  sort(a, a + m);

  rep(i, m - 1) { b[i] = a[i + 1] - a[i]; }
  sort(b, b + m - 1, greater<long>());

  if (m == 1) {
    cout << 0 << endl;
  } else {
    long res = a[m - 1] - a[0];
    long j = 0;
    while (j < min(m - 1, n - 1)) {
      res -= b[j];
      j++;
    }
    cout << res << endl;
  }
}