Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define LARGE 1000000000
#define MOD 100000

int N, M, sum;
int aim[100009], dis[100009];

int main() {
  dis[0] = 0;
  cin >> N >> M;
  if (N == M)
    sum = 0;
  else {
    for (int i = 0; i < M; i++)
      cin >> aim[i];
    sort(aim, aim + M);
    for (int i = 1; i < M; i++)
      dis[i] = aim[i] - aim[i - 1];
    sort(dis, dis + M);
    sum = aim[M - 1] - aim[0];
    for (int i = 0; i < N - 1; i++)
      sum -= dis[M - 1 - i];
  }
  cout << sum << endl;
  return 0;
}

// チーズ
/*
int H, W, N, ans, tag, si, sj;
char MAP[1009][1009];
int dp[1009][1009];

bool inmap(int i, int j) {
return ((i >= 0 && i <= H - 1) && (j >= 0 && j <= W - 1));
}
bool pass(int i, int j) {
return (MAP[i][j] != 'X');
}

int DFS(int i, int j) {
if (dp[i][j] != LARGE)return dp[i][j];
if (MAP[i][j] - '0' == tag) {
si = i; sj = j;
return 0;
}

if (inmap(i + 1, j) && pass(i + 1, j)) dp[i][j] = min(dp[i][j], DFS(i + 1, j) +
1); if (inmap(i - 1, j) && pass(i - 1, j)) dp[i][j] = min(dp[i][j], DFS(i - 1,
j) + 1); if (inmap(i, j + 1) && pass(i, j + 1)) dp[i][j] = min(dp[i][j], DFS(i,
j + 1) + 1); if (inmap(i, j - 1) && pass(i, j - 1)) dp[i][j] = min(dp[i][j],
DFS(i, j - 1) + 1); cout << i << " " << j << " " << dp[i][j] << endl; return
dp[i][j];
}

int main() {
cin >> H >> W >> N;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> MAP[i][j];
if (MAP[i][j] == 'S') {
si = i; sj = j;
}
}
}

for (int i = 1; i <= N; i++) {
cout << i << " " << si << " " << sj << endl << endl;
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
dp[j][k] = LARGE;
}
}
tag = i;
ans += DFS(si, sj);

}

cout << ans << endl;
return 0;
}
*/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define LARGE 1000000000
#define MOD 100000

int N, M, sum;
int aim[100009], dis[100009];

int main() {
  dis[0] = 0;
  cin >> N >> M;
  if (N >= M)
    sum = 0;
  else {
    for (int i = 0; i < M; i++)
      cin >> aim[i];
    sort(aim, aim + M);
    for (int i = 1; i < M; i++)
      dis[i] = aim[i] - aim[i - 1];
    sort(dis, dis + M);
    sum = aim[M - 1] - aim[0];
    for (int i = 0; i < N - 1; i++)
      sum -= dis[M - 1 - i];
  }
  cout << sum << endl;
  return 0;
}

// チーズ
/*
int H, W, N, ans, tag, si, sj;
char MAP[1009][1009];
int dp[1009][1009];

bool inmap(int i, int j) {
return ((i >= 0 && i <= H - 1) && (j >= 0 && j <= W - 1));
}
bool pass(int i, int j) {
return (MAP[i][j] != 'X');
}

int DFS(int i, int j) {
if (dp[i][j] != LARGE)return dp[i][j];
if (MAP[i][j] - '0' == tag) {
si = i; sj = j;
return 0;
}

if (inmap(i + 1, j) && pass(i + 1, j)) dp[i][j] = min(dp[i][j], DFS(i + 1, j) +
1); if (inmap(i - 1, j) && pass(i - 1, j)) dp[i][j] = min(dp[i][j], DFS(i - 1,
j) + 1); if (inmap(i, j + 1) && pass(i, j + 1)) dp[i][j] = min(dp[i][j], DFS(i,
j + 1) + 1); if (inmap(i, j - 1) && pass(i, j - 1)) dp[i][j] = min(dp[i][j],
DFS(i, j - 1) + 1); cout << i << " " << j << " " << dp[i][j] << endl; return
dp[i][j];
}

int main() {
cin >> H >> W >> N;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> MAP[i][j];
if (MAP[i][j] == 'S') {
si = i; sj = j;
}
}
}

for (int i = 1; i <= N; i++) {
cout << i << " " << si << " " << sj << endl << endl;
for (int j = 0; j < H; j++) {
for (int k = 0; k < W; k++) {
dp[j][k] = LARGE;
}
}
tag = i;
ans += DFS(si, sj);

}

cout << ans << endl;
return 0;
}
*/