prithivMLmods/Coder-Stat huggingface dataset:
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  REP(i, M) cin >> X[i];
  sort(X.begin(), X.end());
  vector<int> D(M - 1);
  REP(i, M - 1) D[i] = X[i + 1] - X[i];
  sort(D.begin(), D.end());
  REP(i, N - 1) { D.pop_back(); }
  int ans = 0;
  REP(i, D.size()) { ans += D[i]; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  REP(i, M) cin >> X[i];
  sort(X.begin(), X.end());
  vector<int> D(M - 1);
  if (N >= M) {
    cout << 0 << endl;
  } else {
    REP(i, M - 1) D[i] = X[i + 1] - X[i];
    sort(D.begin(), D.end());
    REP(i, N - 1) { D.pop_back(); }
    int ans = 0;
    REP(i, D.size()) { ans += D[i]; }
    cout << ans << endl;
  }
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  vector<long> X;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    long x;
    cin >> x;
    X.push_back(x);
  }
  sort(X.begin(), X.end());

  vector<long> ivl;
  for (int i = 0; i < M - 1; i++) {
    ivl.push_back(X[i + 1] - X[i]);
  }
  sort(ivl.begin(), ivl.end(), greater<long>());

  if (M == 1) {
    cout << "0" << endl;
    return 0;
  }

  long ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += ivl[i];
  }
  cout << X[M - 1] - X[0] - ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M;
  vector<long> X;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    long x;
    cin >> x;
    X.push_back(x);
  }
  sort(X.begin(), X.end());

  vector<long> ivl;
  for (int i = 0; i < M - 1; i++) {
    ivl.push_back(X[i + 1] - X[i]);
  }
  sort(ivl.begin(), ivl.end(), greater<long>());

  if (N >= M) {
    cout << "0" << endl;
    return 0;
  }

  long ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += ivl[i];
  }
  cout << X[M - 1] - X[0] - ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
// Author: Fuadul Hasan(fuadul202@gmail.com)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int N = 2e5 + 5;

int main() {

  int n, k;
  cin >> n >> k;
  int a[n];

  for (int i = 0; i < k; i++) {
    cin >> a[i];
  }

  sort(a, a + k);
  std::vector<int> v;
  for (int i = 1; i < k; i++) {
    v.push_back(a[i] - a[i - 1]);
  }
  sort(v.begin(), v.end());
  ll sum = 0;
  for (int i = (v.size() - n); i >= 0; i--) {
    // cout<<v[i]<<" ";
    sum += (ll)v[i];
  } // cout<<endl;
  cout << sum << endl;
  return 0;
}

Fixed submission:
// Author: Fuadul Hasan(fuadul202@gmail.com)
#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int N = 2e5 + 5;

int main() {

  int n, k;
  cin >> n >> k;
  int a[k];

  for (int i = 0; i < k; i++) {
    cin >> a[i];
  }

  sort(a, a + k);
  std::vector<int> v;
  for (int i = 1; i < k; i++) {
    v.push_back(a[i] - a[i - 1]);
  }
  sort(v.begin(), v.end());
  ll sum = 0;
  for (int i = (v.size() - n); i >= 0; i--) {
    // cout<<v[i]<<" ";
    sum += (ll)v[i];
  } // cout<<endl;
  cout << sum << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>
#include <vector>
using namespace std;
#define PI 3.14159265358979323846
#define MAXINF 1e18L
#define MININF -1e18L
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<int>())
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \
  cerr << #v << ":";                                                           \
  for (int i = 0; i < v.size(); i++)                                           \
    cerr << " " << v[i];                                                       \
  cerr << endl
#define pb push_back
template <class A> void pr(A a) { cout << (a) << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
typedef long long ll;
typedef pair<int, int> intint;

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }

  SORT(x);
  vector<int> diff(m - 1);
  for (int i = 0; i < m - 1; i++) {
    diff[i] = (x[i + 1] - x[i]);
  }
  RSORT(diff);

  int ans = x[x.size() - 1] - x[0];

  REP(i, n - 1) { ans -= diff[i]; }
  pr(ans);
}

Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
#include <vector>
using namespace std;
#define PI 3.14159265358979323846
#define MAXINF 1e18L
#define MININF -1e18L
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<int>())
#define DEBUG(x) cerr << #x << ": " << x << endl;
#define DEBUG_VEC(v)                                                           \
  cerr << #v << ":";                                                           \
  for (int i = 0; i < v.size(); i++)                                           \
    cerr << " " << v[i];                                                       \
  cerr << endl
#define pb push_back
template <class A> void pr(A a) { cout << (a) << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  pr(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  pr(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  pr(b, c, d);
}
typedef long long ll;
typedef pair<int, int> intint;

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }

  if (n >= m) {
    pr(0);
    return 0;
  }

  SORT(x);
  vector<int> diff(m - 1);
  for (int i = 0; i < m - 1; i++) {
    diff[i] = (x[i + 1] - x[i]);
  }
  RSORT(diff);

  int ans = x[x.size() - 1] - x[0];

  REP(i, n - 1) { ans -= diff[i]; }
  pr(ans);
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

using namespace std;

int input[100005];
int cache[100005];

long long ans = 0;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d", &input[i]);
  sort(input + 1, input + 1 + m);
  for (int i = 1; i <= m - 1; i++) {
    ans += input[i + 1] - input[i];
    cache[i] = input[i + 1] - input[i];
  }
  sort(cache + 1, cache + m);
  for (int i = 0; i < n - 1; i++) {
    ans -= cache[m - 1 - i];
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

int input[100005];
int cache[100005];

long long ans = 0;

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d", &input[i]);
  if (n >= m) {
    printf("0\n");
    return 0;
  }
  sort(input + 1, input + 1 + m);
  for (int i = 1; i <= m - 1; i++) {
    ans += input[i + 1] - input[i];
    cache[i] = input[i + 1] - input[i];
  }
  sort(cache + 1, cache + m);
  for (int i = 0; i < n - 1; i++) {
    ans -= cache[m - 1 - i];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

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

  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  sort(x.begin(), x.end());

  vector<int> dif(m - 1);
  rep(i, m - 1) dif[i] = x[i + 1] - x[i];
  sort(dif.begin(), dif.end(), greater<int>());

  int ans = x[m - 1] - x[0];
  rep(i, n - 1) ans -= dif[i];

  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

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

  int n, m;
  cin >> n >> m;
  if (n >= m) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  sort(x.begin(), x.end());

  vector<int> dif(m - 1);
  rep(i, m - 1) dif[i] = x[i + 1] - x[i];
  sort(dif.begin(), dif.end(), greater<int>());

  int ans = x[m - 1] - x[0];
  rep(i, n - 1) ans -= dif[i];

  cout << ans << '\n';
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N, M;

int main() {
  cin >> N >> M;
  if (N >= M) {
    cout << 0 << endl;
  }
  vector<int> Xs(M);
  for (int i = 0; i < M; i++) {
    cin >> Xs.at(i);
  }
  sort(Xs.begin(), Xs.end());
  vector<int> Ds(M - 1);
  for (int i = 0; i < M - 1; i++) {
    Ds.at(i) = Xs.at(i + 1) - Xs.at(i);
  }
  sort(Ds.begin(), Ds.end());
  int steps = accumulate(Ds.begin(), Ds.end() - (N - 1), 0);
  cout << steps << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N, M;

int main() {
  cin >> N >> M;
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> Xs(M);
  for (int i = 0; i < M; i++) {
    cin >> Xs.at(i);
  }
  sort(Xs.begin(), Xs.end());
  vector<int> Ds(M - 1);
  for (int i = 0; i < M - 1; i++) {
    Ds.at(i) = Xs.at(i + 1) - Xs.at(i);
  }
  sort(Ds.begin(), Ds.end());
  int steps = accumulate(Ds.begin(), Ds.end() - (N - 1), 0);
  cout << steps << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N, M;
  int ans = 0;
  vector<int> X(100000, 0);
  vector<int> diffX(100000, 0);

  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  X.erase(X.begin() + M, X.end());
  diffX.erase(diffX.begin() + M - 1, diffX.end());

  sort(X.begin(), X.end());
  for (int i = 0; i < M - 1; i++) {
    diffX[i] = X[i + 1] - X[i];
  }
  sort(diffX.begin(), diffX.end(), greater<int>());
  if (N < M) {
    ans = accumulate(diffX.begin() + N - 1, diffX.end(), 0);
  } else {
    ans = 0;
  }
  cout << ans;
  return 1;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N, M;
  int ans = 0;
  vector<int> X(100000, 0);
  vector<int> diffX(100000, 0);

  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  X.erase(X.begin() + M, X.end());
  diffX.erase(diffX.begin() + M - 1, diffX.end());

  sort(X.begin(), X.end());
  for (int i = 0; i < M - 1; i++) {
    diffX[i] = X[i + 1] - X[i];
  }
  sort(diffX.begin(), diffX.end(), greater<int>());
  if (N < M) {
    ans = accumulate(diffX.begin() + N - 1, diffX.end(), 0);
  } else {
    ans = 0;
  }
  cout << ans;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, M;
  cin >> N >> M;

  int X[M];
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X, X + M);
  int distance[M];
  distance[0] = 0;
  for (int i = 1; i < M; i++) {
    distance[i] = X[i] - X[i - 1];
  }
  sort(distance, distance + M, greater<int>());

  int ans = abs(X[M - 1] - X[0]);
  for (int i = 2; i <= N; i++) {
    ans -= distance[i - 2];
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, M;
  cin >> N >> M;

  int X[M];
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X, X + M);
  int distance[M];
  distance[0] = 0;
  for (int i = 1; i < M; i++) {
    distance[i] = X[i] - X[i - 1];
  }
  sort(distance, distance + M, greater<int>());

  int ans = abs(X[M - 1] - X[0]);
  for (int i = 2; i <= min(N, M); i++) {
    ans -= distance[i - 2];
  }

  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++)
    cin >> x[i];
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(x.begin(), x.end());
  priority_queue<int> d;
  for (int i = 0; i < m - 1; i++)
    d.push(abs(x[i + 1] - x[i]));
  for (int i = 0; i < n - 1; i++)
    d.pop();
  int ans = 0;
  while (!d.empty()) {
    ans += d.top();
    d.pop();
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++)
    cin >> x[i];
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(x.begin(), x.end());
  priority_queue<int> d;
  for (int i = 0; i < m - 1; i++)
    d.push(abs(x[i + 1] - x[i]));
  for (int i = 0; i < n - 1; i++)
    d.pop();
  int ans = 0;
  while (!d.empty()) {
    ans += d.top();
    d.pop();
  }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n;
  cin >> m;

  int a[m];
  int y[m - 1];

  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m);

  for (int i = 0; i < n - 1; i++) {
    y[i] = 0;
  }

  for (int i = 0; i < m - 1; i++) {
    y[i] = abs(a[i + 1] - a[i]);
  }

  sort(y, y + m - 1);
  int all = 0;

  for (int i = 0; i < m - 1 - (n - 1); i++) {
    all += y[i];
  }
  cout << all << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n;
  cin >> m;

  int a[m];
  int y[n - 1];

  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m);

  for (int i = 0; i < n - 1; i++) {
    y[i] = 0;
  }

  for (int i = 0; i < m - 1; i++) {
    y[i] = abs(a[i + 1] - a[i]);
  }

  sort(y, y + m - 1);
  int all = 0;

  for (int i = 0; i < m - 1 - (n - 1); i++) {
    all += y[i];
  }
  cout << all << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pii pair<int, int>

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int x[m];
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  sort(x, x + m);
  int ans = x[m - 1] - x[0];
  if (m > 1) {
    vector<int> d(m - 1);
    for (int i = 0; i < m - 1; i++) {
      d[i] = x[i + 1] - x[i];
    }
    sort(d.rbegin(), d.rend());
    for (int i = 2; i <= n; i++) {
      ans -= d[i - 2];
    }
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x.size())
#define fi first
#define se second
#define pii pair<int, int>

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int x[m];
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  sort(x, x + m);
  int ans = x[m - 1] - x[0];
  if (m > 1) {
    vector<int> d(m - 1);
    for (int i = 0; i < m - 1; i++) {
      d[i] = x[i + 1] - x[i];
    }
    sort(d.rbegin(), d.rend());
    for (int i = 0; i < min(n - 1, sz(d)); i++) {
      ans -= d[i];
    }
  }
  cout << ans << "\n";
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(x.begin(), x.end());
  vector<int> diff(m - 1);
  rep(i, m - 1) { diff[i] = x[i + 1] - x[i]; }
  sort(diff.rbegin(), diff.rend());
  int ans = 0;
  rep(i, m - 1) { ans += diff[i]; }
  rep(i, n - 1) { ans -= diff[i]; }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  if (m == 1 || n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(x.begin(), x.end());
  vector<int> diff(m - 1);
  rep(i, m - 1) { diff[i] = x[i + 1] - x[i]; }
  sort(diff.rbegin(), diff.rend());
  int ans = 0;
  rep(i, m - 1) { ans += diff[i]; }
  rep(i, n - 1) { ans -= diff[i]; }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

#define MAX_N 100000
#define MAX_M 100000

template <class T, class InputIterator>
bool isin(T x, InputIterator first, InputIterator last) {
  return std::any_of(first, last, [x](T y) { return x == y; });
}

int main() {
  int n, m;
  std::vector<int> x(MAX_M), mark(MAX_M - 1);
  std::cin >> n >> m;

  if (n >= m) {
    std::cout << 0 << std::endl;
    return 0;
  }

  for (int i = 0; i < m; ++i) {
    std::cin >> x[i];
    mark[i] = i;
  }
  std::sort(x.begin(), x.begin() + m);

  int cost = *(x.begin() + m - 1) - *(x.begin());
  std::vector<int> selected(MAX_N - 1);
  int count = 0;
  while (count < n - 1) {
    int max_dist = 0;
    int cand;
    for (int i = 1; i < m; ++i) {
      if (x[i] - x[i - 1] > max_dist &&
          not isin(i, selected.begin(), selected.end() + count)) {
        max_dist = x[i] - x[i - 1];
        cand = i;
      }
    }
    cost -= max_dist;
    selected[count] = cand;
    count++;
  }

  std::cout << cost << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

#define MAX_N 100000
#define MAX_M 100000

template <class T, class InputIterator>
bool isin(T x, InputIterator first, InputIterator last) {
  return std::any_of(first, last, [x](T y) { return x == y; });
}

int main() {
  int n, m;
  std::vector<int> x(MAX_M), mark(MAX_M - 1);
  std::cin >> n >> m;

  if (n >= m) {
    std::cout << 0 << std::endl;
    return 0;
  }

  for (int i = 0; i < m; ++i) {
    std::cin >> x[i];
    mark[i] = i;
  }
  std::sort(x.begin(), x.begin() + m);

  std::vector<int> dist(MAX_M - 1);
  for (int i = 1; i < m; ++i) {
    dist[i] = x[i] - x[i - 1];
  }
  std::sort(dist.begin(), dist.begin() + m);

  int cost = x[m - 1] - x[0];
  for (int i = 0; i < n - 1; ++i) {
    cost -= dist[m - 1 - i];
  }

  std::cout << cost << std::endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
vector<int> a;
vector<int> b;
int n, x, m;
long long s;

int main() {
  cin >> m >> n;

  for (int i = 1; i <= n; i++) {
    cin >> x;
    a.push_back(x);
  }

  sort(a.begin(), a.end());

  for (int i = 0; i < n - 1; i++)
    b.push_back(a[i + 1] - a[i]);

  sort(b.begin(), b.end(), greater<int>());

  s = a[n - 1] - a[0] + 1 - m;

  if (s <= 0) {
    cout << 0;
  } else {
    for (int i = 0; i < m - 1; i++) {
      s -= b[i] - 1;
    }
    cout << s;
  }
}


Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;
vector<int> a;
vector<int> b;
int n, x, m;
long long s;

int main() {
  cin >> m >> n;

  for (int i = 1; i <= n; i++) {
    cin >> x;
    a.push_back(x);
  }

  sort(a.begin(), a.end());

  for (int i = 0; i < n - 1; i++)
    b.push_back(a[i + 1] - a[i]);

  sort(b.begin(), b.end(), greater<int>());

  s = a[n - 1] - a[0] + 1 - m;

  if (s <= 0 || m > n) {
    cout << 0;
  } else {
    for (int i = 0; i < m - 1; i++) {
      s -= b[i] - 1;
    }
    cout << s;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;

int main() {
  int n, m;
  cin >> n >> m;
  vi a(m);
  rep(i, m) cin >> a[i];
  sort(a.begin(), a.end());
  // cout << a[0] << " " << a[m-1] << endl;
  int lg = abs(a[0] - a[m - 1]);
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  // cout << lg << endl;
  vi b(m - 1);
  rep(i, m - 1) { b[i] = abs(a[i] - a[i + 1]); }
  sort(b.begin(), b.end(), greater<int>());
  // cout << b[0] << endl;
  int cnt = 0;
  while (cnt < n - 1) {
    lg -= b[cnt];
    cnt++;
  }
  cout << lg << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using Graph = vector<vi>;
using P = pair<int, int>;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;

int main() {
  int n, m;
  cin >> n >> m;
  vi a(m);
  rep(i, m) cin >> a[i];
  sort(a.begin(), a.end());
  // cout << a[0] << " " << a[m-1] << endl;
  int lg = abs(a[0] - a[m - 1]);
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  // cout << lg << endl;
  vi b(m - 1);
  rep(i, m - 1) { b[i] = abs(a[i] - a[i + 1]); }
  sort(b.begin(), b.end(), greater<int>());
  // cout << b[0] << endl;
  int cnt = 0;
  while (cnt < n - 1 && cnt < m - 1) {
    lg -= b[cnt];
    cnt++;
  }
  cout << lg << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> xv(m);
  for (auto &x : xv) {
    cin >> x;
  }

  if (m <= n) {
    cout << 0 << endl;
  } else {

    sort(xv.begin(), xv.end());

    vector<int> lv(m - 1);
    for (int i = 1; i < m; ++i) {
      lv[i] = xv[i] - xv[i - 1];
    }

    sort(lv.begin(), lv.end());
    int ret = 0;
    for (int i = 0; i < m - n; ++i) {
      ret += lv[i];
    }

    cout << ret << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> xv(m);
  for (auto &x : xv) {
    cin >> x;
  }

  if (m <= n) {
    cout << 0 << endl;
  } else {

    sort(xv.begin(), xv.end());

    vector<int> lv(m - 1);
    for (int i = 1; i < m; ++i) {
      lv[i - 1] = xv[i] - xv[i - 1];
    }

    sort(lv.begin(), lv.end());
    int ret = 0;
    for (int i = 0; i < m - n; ++i) {
      ret += lv[i];
    }

    cout << ret << endl;
  }

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  if (m == 1)
    cout << 0 << endl;
  else {
    sort(x.begin(), x.end());
    vector<int> l(m - 1, 0);
    for (int i = 0; i < m - 1; i++) {
      l[i] = x[i + 1] - x[i];
    }

    sort(l.begin(), l.end());
    reverse(l.begin(), l.end());
    int sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += l[i];
    }

    cout << x[m - 1] - x[0] - sum << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  if (n >= m)
    cout << 0 << endl;
  else {
    sort(x.begin(), x.end());
    vector<int> l(m - 1, 0);
    for (int i = 0; i < m - 1; i++) {
      l[i] = x[i + 1] - x[i];
    }

    sort(l.begin(), l.end());
    reverse(l.begin(), l.end());
    int sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += l[i];
    }

    cout << x[m - 1] - x[0] - sum << endl;
  }
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
#define ll long long
using namespace std;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> X(M);
  if (M != 1) {
    vector<ll> d(M - 1);
    for (ll i = 0; i < M; i++)
      cin >> X.at(i);
    sort(X.begin(), X.end());
    ll ans = X.at(M - 1) - X.at(0);
    for (ll i = 0; i < M - 1; i++)
      d.at(i) = X.at(i + 1) - X.at(i);
    sort(d.begin(), d.end());
    for (ll i = 0; i < N - 1; i++)
      ans -= d.at(M - 2 - i);
    cout << ans << endl;
  } else
    cout << 0 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
#define ll long long
using namespace std;

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> X(M);
  if (M != 1) {
    if (N < M) {
      vector<ll> d(M - 1);
      for (ll i = 0; i < M; i++)
        cin >> X.at(i);
      sort(X.begin(), X.end());
      ll ans = X.at(M - 1) - X.at(0);
      for (ll i = 0; i < M - 1; i++)
        d.at(i) = X.at(i + 1) - X.at(i);
      sort(d.begin(), d.end());
      for (ll i = 0; i < N - 1; i++)
        ans -= d.at(M - 2 - i);
      cout << ans << endl;
    } else
      cout << 0 << endl;
  } else
    cout << 0 << endl;
}
Problem p03137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, x, n) for (int i = (x); i < (n); i++)
#define reps(i, x, n) for (int i = (x); i <= (n); i++)
#define lol long long
#define SUM(n) (((n) + 1) * (n) / 2) // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) (max(x, y) - min(x, y))
#define all(v) v.begin(), v.end()
#define UPDigit(a, b) ((a + b - 1) / b) // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<lol, lol>;
typedef vector<int> vit;
// 八方向を見るのに使うと便利(楽)
const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

// 最大公約数
lol gcd(lol x, lol y) { return (y != 0 ? gcd(y, x % y) : x); }
// 最小公倍数
lol lcm(lol x, lol y) { return ((x * y) / gcd(max(x, y), min(x, y))); }

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int x[m + 5];
  rep(i, 0, m) { cin >> x[i]; }
  sort(x, x + m);
  if (m == 1) {
    cout << "0\n";
    return 0;
  }
  priority_queue<int> que;
  rep(i, 1, m) { que.push(abs(x[i - 1], x[i])); }
  rep(i, 1, n) { que.pop(); }
  lol ans = 0;
  while (!que.empty()) {
    ans += que.top();
    que.pop();
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, x, n) for (int i = (x); i < (n); i++)
#define reps(i, x, n) for (int i = (x); i <= (n); i++)
#define lol long long
#define SUM(n) (((n) + 1) * (n) / 2) // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) (max(x, y) - min(x, y))
#define all(v) v.begin(), v.end()
#define UPDigit(a, b) ((a + b - 1) / b) // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<lol, lol>;
typedef vector<int> vit;
// 八方向を見るのに使うと便利(楽)
const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

// 最大公約数
lol gcd(lol x, lol y) { return (y != 0 ? gcd(y, x % y) : x); }
// 最小公倍数
lol lcm(lol x, lol y) { return ((x * y) / gcd(max(x, y), min(x, y))); }

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  n = min(n, m);
  int x[m + 5];
  rep(i, 0, m) { cin >> x[i]; }
  sort(x, x + m);
  if (m == 1) {
    cout << "0\n";
    return 0;
  }
  priority_queue<int> que;
  rep(i, 1, m) { que.push(abs(x[i - 1], x[i])); }
  rep(i, 1, n) { que.pop(); }
  lol ans = 0;
  while (!que.empty()) {
    ans += que.top();
    que.pop();
  }
  cout << ans << '\n';
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1010001000;
const ll INF = 1001000100010001000;
const int mod = (int)1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, 0, m) { cin >> x.at(i); }
  sort(all(x));
  vector<int> sa(m - 1);

  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, 0, m - 1) { sa.at(i) = abs(x.at(i + 1) - x.at(i)); }
  sort(all(sa), greater<int>());
  int sum = 0;
  if (n == 1) {
    cout << abs(x.at(m - 1) - x.at(0)) << endl;
    return 0;
  }
  rep(i, 0, n - 1) {
    sum += sa.at(i);
    // cout<<sa.at(i)<<endl;
  }
  cout << abs(x.at(m - 1) - x.at(0)) - sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1010001000;
const ll INF = 1001000100010001000;
const int mod = (int)1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, 0, m) { cin >> x.at(i); }
  sort(all(x));
  vector<int> sa(m - 1);

  if (m == 1 or n >= m) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, 0, m - 1) { sa.at(i) = abs(x.at(i + 1) - x.at(i)); }
  sort(all(sa), greater<int>());
  int sum = 0;
  if (n == 1) {
    cout << abs(x.at(m - 1) - x.at(0)) << endl;
    return 0;
  }
  rep(i, 0, n - 1) {
    sum += sa.at(i);
    // cout<<sa.at(i)<<endl;
  }
  cout << abs(x.at(m - 1) - x.at(0)) - sum << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;

#define SCIN(n)                                                                \
  string(n);                                                                   \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define ALL(obj) (obj).begin(), (obj).end()

#define rt return
#define ENDL cout << endl
#define s second
#define f first

typedef string::const_iterator State;
class PalseError {};
class Edge {
public:
  LL from, to, value;
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> vec;
  for (int a = 0; a < M; a++) {
    int b;
    cin >> b;
    vec.push_back(b);
  }
  sort(vec.begin(), vec.end());
  vector<int> diff;
  for (int a = 0; a < M - 1; a++) {
    diff.push_back(vec.at(a + 1) - vec.at(a));
  }
  sort(diff.begin(), diff.end());
  reverse(diff.begin(), diff.end());
  LL ans = vec.back() - vec.front();
  for (int a = 0; a < N - 1; a++) {
    ans -= diff.at(a);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;

#define SCIN(n)                                                                \
  string(n);                                                                   \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define ALL(obj) (obj).begin(), (obj).end()

#define rt return
#define ENDL cout << endl
#define s second
#define f first

typedef string::const_iterator State;
class PalseError {};
class Edge {
public:
  LL from, to, value;
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> vec;
  for (int a = 0; a < M; a++) {
    int b;
    cin >> b;
    vec.push_back(b);
  }
  sort(vec.begin(), vec.end());
  vector<int> diff;
  for (int a = 0; a < M - 1; a++) {
    diff.push_back(vec.at(a + 1) - vec.at(a));
  }
  sort(diff.begin(), diff.end());
  reverse(diff.begin(), diff.end());
  LL ans = vec.back() - vec.front();
  for (int a = 0; a < N - 1; a++) {
    ans -= diff.at(a);
  }
  cout << ans << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge {
  ll to, weight;
  Edge(ll t, ll w) : to(t), weight(w) {}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> x(m);
  for (ll i = 0; i < m; ++i) {
    cin >> x[i];
  }

  sort(x.begin(), x.end());
  vector<ll> d(m - 1);
  for (ll i = 0; i < m - 1; ++i) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d.begin(), d.end());
  ll ans = 0;
  for (ll i = 0; i < d.size() - (n - 1); ++i) {
    ans += d[i];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge {
  ll to, weight;
  Edge(ll t, ll w) : to(t), weight(w) {}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n, m;
  cin >> n >> m;
  n = min(n, m);
  vector<ll> x(m);
  for (ll i = 0; i < m; ++i) {
    cin >> x[i];
  }

  sort(x.begin(), x.end());
  vector<ll> d(m - 1);
  for (ll i = 0; i < m - 1; ++i) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d.begin(), d.end());
  ll ans = 0;
  for (ll i = 0; i < d.size() - (n - 1); ++i) {
    ans += d[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <math.h>
#include <stack>
#include <string>
#include <vector>
// #include<stdlib.h>
using namespace std;
int main() {
  int n, m, a = 0;
  cin >> n >> m;
  vector<int> x(n);
  vector<int> l(n - 1);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  std::sort(x.begin(), x.end());
  for (int i = 0; i < n - 1; i++) {
    l[i] = x[i + 1] - x[i];
  }
  std::sort(l.begin(), l.end());
  for (int i = 0; i < n + 1 - n; i++) {
    a += l[i];
  }
  cout << a;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <math.h>
#include <stack>
#include <string>
#include <vector>
// #include<stdlib.h>
using namespace std;
int main() {
  int n, m, a = 0;
  cin >> n >> m;
  vector<int> x(m);
  vector<int> l(m - 1);
  if (m == 1) {
    a = 0;
  } else {
    for (int i = 0; i < m; i++) {
      cin >> x[i];
    }
    std::sort(x.begin(), x.end());
    for (int i = 0; i < m - 1; i++) {
      l[i] = x[i + 1] - x[i];
    }
    std::sort(l.begin(), l.end());
    for (int i = 0; i < m - n; i++) {
      a += l[i];
    }
  }
  cout << a;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define swap(a, b) (a += b, b = a - b, a -= b)
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M - 1);
  rep(i, M) { cin >> a[i]; }
  sort(a.begin(), a.end());
  rep(i, M - 1) { b[i] = a[i + 1] - a[i]; }
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  rep(i, N + 1) { b[i] = 0; }
  int sum = 0;
  rep(i, M) { sum += b[i]; }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define swap(a, b) (a += b, b = a - b, a -= b)
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(M), b(M - 1);
  rep(i, M) { cin >> a[i]; }
  if (N >= M) {
    cout << 0 << endl;
  } else {
    sort(a.begin(), a.end());
    rep(i, M - 1) { b[i] = abs(a[i + 1] - a[i]); }
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());

    rep(i, N - 1) { b[i] = 0; }
    int sum = 0;
    rep(i, M - 1) { sum += b[i]; }
    cout << sum << endl;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define DescSort(a) sort(a.begin(), a.end(), std::greater<LL>())
#define Sort(a) sort(a.begin(), a.end())

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, M;
  LL ans = 0;

  cin >> N >> M;

  vector<LL> X((UI)M);

  for (int i = 0; i < M; i++) {
    cin >> X.at(i);
  }

  Sort(X);

  vector<LL> L((UI)M - 1, 0); // 差分

  for (int i = 1; i < M; i++) {
    L[i - 1] = X[i] - X[i - 1];
  }

  Sort(L);

  for (UI i = 0; i < L.size() - 1; i++) { // 最大のものだけ除く
    ans += L[i];
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define DescSort(a) sort(a.begin(), a.end(), std::greater<LL>())
#define Sort(a) sort(a.begin(), a.end())

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, M;
  LL ans = 0;

  cin >> N >> M;

  vector<LL> X((UI)M);

  for (int i = 0; i < M; i++) {
    cin >> X.at(i);
  }

  Sort(X);

  vector<LL> L((UI)M - 1, 0); // 差分

  for (int i = 1; i < M; i++) {
    L[i - 1] = X[i] - X[i - 1];
  }

  Sort(L);

  for (UI i = 0; i < M - N; i++) {
    ans += L[i];
  }

  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define rSort(a) sort(a.rbegin(), a.rend())
#define Sort(a) sort(a.begin(), a.end())
#define Sum(a) accumulate(a.begin(), a.end(), 0)
#define rep(i, n) for (UI i = 0; i < n; i++)

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, M;
  LL calc = 0;
  LL sum = 0;
  LL ans = 0;

  cin >> N >> M;

  vector<LL> X((UI)M);

  rep(i, M) { cin >> X[i]; }

  Sort(X);
  vector<LL> Y;

  rep(i, X.size() - 1) {
    Y.push_back(X[i + 1] - X[i]);
    sum += Y[i];
  }

  rSort(Y);

  if (N >= M) {
    ans = 0;
  } else {

    rep(i, i < N - 1) {
      calc += Y[i]; // 除去する分
    }
    ans = sum - calc;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define rSort(a) sort(a.rbegin(), a.rend())
#define Sort(a) sort(a.begin(), a.end())
#define Sum(a) accumulate(a.begin(), a.end(), 0)
#define rep(i, n) for (UI i = 0; i < n; i++)

int main(int argc, const char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, M;
  LL calc = 0;
  LL sum = 0;
  LL ans = 0;

  cin >> N >> M;

  vector<LL> X((UI)M);

  rep(i, M) { cin >> X[i]; }

  Sort(X);
  vector<LL> Y;

  rep(i, X.size() - 1) {
    Y.push_back(X[i + 1] - X[i]);
    sum += Y[i];
  }

  rSort(Y);

  if (N >= M) {
    ans = 0;
  } else {

    rep(i, N - 1) {
      calc += Y[i]; // 除去する分
    }
    ans = sum - calc;
  }

  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, t;
  long long res = 0;
  cin >> N >> M;

  if (M > 1) {
    vector<int> v(M, 0);
    vector<int> v2(M, 0);

    for (int i = 0; i < M; i++) {
      cin >> v[i];
    }
    sort(v.begin(), v.end());
    res = v[M - 1] - v[0];
    for (int i = 1; i < M; i++) {
      t = v[i] - v[i - 1];
      v2[i] = t;
    }
    sort(v2.rbegin(), v2.rend());

    for (int j = 0; j < N - 1; j++) {
      res -= v2[j];
    }
  }

  cout << res << endl;
}
// sort(v.rbegin(), v.rend(), [](auto& x, auto& y){return x[1] < y[1];});

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, t;
  long long res = 0;
  cin >> N >> M;

  if (M > 1) {
    vector<int> v(M, 0);
    vector<int> v2(M, 0);

    for (int i = 0; i < M; i++) {
      cin >> v[i];
    }
    sort(v.begin(), v.end());
    res = v[M - 1] - v[0];
    for (int i = 1; i < M; i++) {
      t = v[i] - v[i - 1];
      v2[i] = t;
    }
    sort(v2.rbegin(), v2.rend());

    t = min(N - 1, M);
    for (int j = 0; j < t; j++) {
      res -= v2[j];
    }
  }

  cout << res << endl;
}
// sort(v.rbegin(), v.rend(), [](auto& x, auto& y){return x[1] < y[1];});
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  if (M == 1) {
    cout << 1 << endl;
    return 0;
  }
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());

  vector<int> sub;
  for (int i = 1; i < X.size(); i++) {
    sub.push_back(abs(X[i] - X[i - 1]));
    // cout << abs(X[i] - X[i - 1]) << endl;
  }
  sort(sub.begin(), sub.end());

  cout << accumulate(sub.begin(), sub.end() - (N - 1), 0LL);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  if (M <= N) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());

  vector<int> sub;
  for (int i = 1; i < X.size(); i++) {
    sub.push_back(abs(X[i] - X[i - 1]));
    // cout << abs(X[i] - X[i - 1]) << endl;
  }
  sort(sub.begin(), sub.end());

  cout << accumulate(sub.begin(), sub.end() - (N - 1), 0LL);

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
signed main() {
  int N, M;
  cin >> N >> M;
  if (N >= M)
    cout << 0 << endl;
  else {
    vi A(M);
    rep(i, 0, M) cin >> A[i];
    sort(ALL(A));
    vector<pii> B(M - 1);
    rep(i, 0, M - 1) B[i] = make_pair(A[i + 1] - A[i], i);
    sort(ALL(B));
    vi C(N - 1);
    rep(i, 0, N - 1) C[i] = B[M - 2 - i].second;
    sort(ALL(C));
    int ans = A[C[0]] - A[0];
    rep(i, 1, N - 1) ans += A[C[i]] - A[C[i - 1] + 1];
    ans += A[M - 1] - A[C[N - 2] + 1];
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
signed main() {
  int N, M;
  cin >> N >> M;
  if (N >= M)
    cout << 0 << endl;
  else {
    vi A(M);
    rep(i, 0, M) cin >> A[i];
    sort(ALL(A));
    if (N == 1)
      cout << A[M - 1] - A[0] << endl;
    else {
      vector<pii> B(M - 1);
      rep(i, 0, M - 1) B[i] = make_pair(A[i + 1] - A[i], i);
      sort(ALL(B));
      vi C(N - 1);
      rep(i, 0, N - 1) C[i] = B[M - 2 - i].second;
      sort(ALL(C));
      int ans = A[C[0]] - A[0];
      rep(i, 1, N - 1) ans += A[C[i]] - A[C[i - 1] + 1];
      ans += A[M - 1] - A[C[N - 2] + 1];
      cout << ans << endl;
    }
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<long long> X(M);
  for (int i = 0; i < M; ++i)
    cin >> X[i];
  sort(X.begin(), X.end());

  vector<long long> diffs;
  for (int i = 1; i < X.size(); ++i)
    diffs.push_back(X[i] - X[i - 1]); // push_back=新たな要素を末尾に追加
  sort(diffs.begin(), diffs.end(), greater<long long>()); // 大きい順にソート

  long long res = X.back() - X[0]; // 座標の最大-最小
  for (int i = 0; i < N - 1; ++i)
    res -= diffs[i];
  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<long long> X(M);
  for (int i = 0; i < M; ++i)
    cin >> X[i];
  sort(X.begin(), X.end());

  vector<long long> diffs;
  for (int i = 1; i < X.size(); ++i)
    diffs.push_back(X[i] - X[i - 1]); // push_back=新たな要素を末尾に追加
  sort(diffs.begin(), diffs.end(), greater<long long>()); // 大きい順にソート

  long long res = X.back() - X[0]; // 座標の最大-最小
  for (int i = 0; i < min((int)diffs.size(),
                          N - 1) /*∵同じ座標に複数のコマを置いてもいい*/;
       ++i)
    res -= diffs[i];
  cout << res << endl;
}