Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
  int t;
  edge(int _t) : t(_t) {}
};
bool use[100010];

vector<edge> te[100010];
vector<edge> fe[100010];
int t[64];
int d[100100];
int n;

void Solve() {
  int m;
  cin >> n >> m;
  for (size_t i = 0; i < m; i++) {
    cin >> t[i];
  }
  sort(t, t + m);
  ll sum = 0;
  for (size_t i = 0; i < m - 1; i++) {
    d[i] = t[i + 1] - t[i];
    sum += d[i];
  }
  sort(d, d + (m - 1));
  reverse(d, d + (m - 1));
  if (m <= n - 1) {
    cout << 0 << endl;
    return;
  }
  for (size_t i = 0; i < n - 1; i++) {
    sum -= d[i];
  }
  cout << sum << endl;
}

int main() {
  Solve();
  return 0;
}


Fixed submission:
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
  int t;
  edge(int _t) : t(_t) {}
};
bool use[100010];

vector<edge> te[100010];
vector<edge> fe[100010];
int t[100100];
int d[100100];
int n;

void Solve() {
  int m;
  cin >> n >> m;
  for (size_t i = 0; i < m; i++) {
    cin >> t[i];
  }
  sort(t, t + m);
  ll sum = 0;
  for (size_t i = 0; i < m - 1; i++) {
    d[i] = t[i + 1] - t[i];
    sum += d[i];
  }
  sort(d, d + (m - 1));
  reverse(d, d + (m - 1));
  if (m <= n - 1) {
    cout << 0 << endl;
    return;
  }
  for (size_t i = 0; i < n - 1; i++) {
    sum -= d[i];
  }
  cout << sum << endl;
}

int main() {
  Solve();
  return 0;
}