Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = INT32_MAX;
const long long int Mod = 1000000007;
const long long int mod = 100000; // 10^5
using ll = long long int;         // long long int は64bit
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using pii = pair<int, int>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (int i = 0; i < N; i++)
#define np nullptr
template <class T> bool chmax(T &former, const T &b) {
  if (former <= b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \
  std::sort(v.begin(), v.end(),                                                \
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define fill(x, num) memset(x, num, sizeof(x));
typedef vector<vector<ll>> matrix;
//       cin.tie(0);ios::sync_with_stdio(false);
void cinit() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
//////////////////////////^^ *Emile
///^^^//////////////////////////////////////////////////////

int main() {
  ll n, m;
  cin >> n >> m;
  vi v;
  ll aa;
  rep(i, m) {
    cin >> aa;
    v.pb(aa);
  }
  sort(all(v));
  vi vd;
  if (m == 1) {
    cout << v[v.size() - 1] - v[0] << endl;
    return 0;
  }
  rep(i, v.size() - 1) { vd.pb(v[i + 1] - v[i]); }
  Sort(vd);
  ll ans = v[v.size() - 1] - v[0];
  rep(i, n - 1) { ans -= vd[i]; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = INT32_MAX;
const long long int Mod = 1000000007;
const long long int mod = 100000; // 10^5
using ll = long long int;         // long long int は64bit
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using pii = pair<int, int>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (int i = 0; i < N; i++)
#define np nullptr
template <class T> bool chmax(T &former, const T &b) {
  if (former <= b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \
  std::sort(v.begin(), v.end(),                                                \
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define fill(x, num) memset(x, num, sizeof(x));
typedef vector<vector<ll>> matrix;
//       cin.tie(0);ios::sync_with_stdio(false);
void cinit() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
//////////////////////////^^ *Emile
///^^^//////////////////////////////////////////////////////

int main() {
  ll n, m;
  cin >> n >> m;
  vi v;
  ll aa;
  rep(i, m) {
    cin >> aa;
    v.pb(aa);
  }
  sort(all(v));
  vi vd;
  if (m == 1) {
    cout << v[v.size() - 1] - v[0] << endl;
    return 0;
  }
  rep(i, v.size() - 1) { vd.pb(v[i + 1] - v[i]); }
  Sort(vd);
  ll ans = v[v.size() - 1] - v[0];
  rep(i, n - 1) {
    if (i >= vd.size()) {
      break;
    }
    ans -= vd[i];
  }
  cout << ans << endl;
  return 0;
}