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

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

int main(int argc, char const *argv[]) {
  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;
  } else {
    vector<int> y(m - 1);
    sort(x.begin(), x.end());
    for (int i = 0; i < m - 1; ++i) {
      y[i] = x[i + 1] - x[i];
    }
    sort(y.begin(), y.end());
    long long ans = accumulate(y.begin(), y.end() - n + 1, 0);
    cout << ans;
  }

  return 0;
}


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

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; ++i)
    cin >> x[i];
  if ((m == 1) || (n >= m)) {
    cout << 0;
  } else {
    vector<int> y(m - 1);
    sort(x.begin(), x.end());
    for (int i = 0; i < m - 1; ++i) {
      y[i] = x[i + 1] - x[i];
    }
    sort(y.begin(), y.end());
    long long ans = accumulate(y.begin(), y.end() - n + 1, 0);
    cout << ans;
  }

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
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];
  }
  sort(x.begin(), x.end());
  // Mが1のときの例外処理をいれるよう
  if (M == 1) {
    cout << "0" << endl;
    return 0;
  }

  vector<int> x_(M - 1);
  for (int i = 0; i < M - 1; i++) { // これで差分が代入される
    x_[i] = x[i + 1] - x[i];
  }
  // 差分をソート、小さい順
  sort(x_.begin(), x_.end());
  // 大きいほうの差分N-1個は無視できる
  int sum = accumulate(x_.begin(), x_.end() - (N - 1), 0);
  cout << sum;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
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];
  }
  sort(x.begin(), x.end());
  // Mが1のときの例外処理をいれるよう
  if (M == 1) {
    cout << "0" << endl;
    return 0;
  }

  vector<int> x_(M - 1);
  for (int i = 0; i < M - 1; i++) { // これで差分が代入される
    x_[i] = x[i + 1] - x[i];
  }
  // 差分をソート、小さい順
  sort(x_.begin(), x_.end());
  // 大きいほうの差分N-1個は無視できる
  // このときN>Mなら
  if (N > M) {
    cout << "0" << endl;
    return 0;
  }
  int sum = accumulate(x_.begin(), x_.end() - (N - 1), 0);
  cout << sum;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

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

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

  sort(x.begin(), x.end());
  for (int i = 0; i < m - 1; i++) {
    diff[i] = abs(x[i] - x[i + 1]);
  }

  sort(diff.begin(), diff.end());
  reverse(diff.begin(), diff.end());
  diff.erase(diff.begin(), diff.begin() + n - 1);

  int sum = 0;
  for (int i = 0; i < m - n; i++) {
    sum += diff[i];
  }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

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

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

  sort(x.begin(), x.end());
  for (int i = 0; i < m - 1; i++) {
    diff[i] = abs(x[i] - x[i + 1]);
  }

  sort(diff.begin(), diff.end());
  reverse(diff.begin(), diff.end());
  diff.erase(diff.begin(), diff.begin() + n - 1);

  int sum = 0;
  for (int i = 0; i < m - n; i++) {
    sum += diff[i];
  }
  cout << sum << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
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.begin(), x.end());
  vector<int> dis(m - 1);
  for (int i = 1; i < m; ++i)
    dis[i - 1] = x[i] - x[i - 1];
  sort(dis.rbegin(), dis.rend());
  int res = x.back() - x[0];
  for (int i = 0; i < min(m, n - 1); ++i)
    res -= dis[i];
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
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.begin(), x.end());
  vector<int> dis(m - 1);
  for (int i = 1; i < m; ++i)
    dis[i - 1] = x[i] - x[i - 1];
  sort(dis.rbegin(), dis.rend());
  int res = x.back() - x[0];
  for (int i = 0; i < min(m - 1, n - 1); ++i)
    res -= dis[i];
  cout << res << endl;
  return 0;
}
Problem p03137 (C++)

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

using namespace std;

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

  if (m != 1) {

    vector<long long> x(m);
    vector<long long> dx(m, 0);

    for (long long i = 0; i < m; i++) {
      cin >> x.at(i);
    }

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

    for (long long i = 1; i < m; i++) {
      dx.at(i) = abs(x.at(i) - x.at(i - 1));
    }

    sort(dx.begin(), dx.end(), greater<long long>());

    for (long long i = 0; i < (n - 1); i++) {
      dx.at(i) = 0;
    }

    long long sum = 0;
    for (long long i = 0; i < m; i++) {
      sum += dx.at(i);
    }

    cout << sum << endl;

  } else {
    cout << 0 << endl;
  }
}


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

using namespace std;

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

  if (m != 1) {

    vector<long long> x(m);
    vector<long long> dx(m, 0);

    for (long long i = 0; i < m; i++) {
      cin >> x.at(i);
    }

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

    for (long long i = 1; i < m; i++) {
      dx.at(i) = abs(x.at(i) - x.at(i - 1));
    }

    sort(dx.begin(), dx.end(), greater<long long>());

    if (m >= (n - 1)) {
      for (long long i = 0; i < (n - 1); i++) {
        dx.at(i) = 0;
      }
    } else {
      for (long long i = 0; i < m; i++) {
        dx.at(i) = 0;
      }
    }

    long long sum = 0;
    for (long long i = 0; i < m; i++) {
      sum += dx.at(i);
    }

    cout << sum << endl;

  } else {
    cout << 0 << endl;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
// #include <numeric>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>

using namespace std;
#define n_max 100000

#define rep(i, n) for (int i = 0; i < n; i++)
#define in(a) cin >> a
#define out(a) cout << a << endl
typedef long long ll;

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

  vector<int> x;

  rep(i, m) {
    int tmp;
    cin >> tmp;
    x.push_back(tmp);
  }
  sort(x.begin(), x.end());
  vector<ll> d(m);

  rep(i, m - 1) { d[i] = x[i + 1] - x[i]; }

  ll ans = x.back() - x.front();
  sort(d.rbegin(), d.rend());

  rep(i, n - 1) { ans -= d[i]; }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <math.h>
// #include <numeric>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>

using namespace std;
#define n_max 100000

#define rep(i, n) for (int i = 0; i < n; i++)
#define in(a) cin >> a
#define out(a) cout << a << endl
typedef long long ll;

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

  vector<int> x;

  rep(i, m) {
    int tmp;
    cin >> tmp;
    x.push_back(tmp);
  }
  sort(x.begin(), x.end());
  vector<ll> d(m);

  rep(i, m - 1) { d[i] = x[i + 1] - x[i]; }

  ll ans = x.back() - x.front();
  sort(d.rbegin(), d.rend());

  if (n >= m) {
    ans = 0;
  } else {
    rep(i, n - 1) { ans -= d[i]; }
  }

  cout << ans << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc117/tasks/abc117_c

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int main() {
  int n, m, tmp;
  vector<int> v, w;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    v.push_back(tmp);
  }
  sort(v.begin(), v.end());

  // 座標の差分を計算
  for (int i = 1; i < m; i++) {
    w.push_back(v[i] - v[i - 1]);
  }

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

  int sum = 0;
  for (int i = 0; i < w.size() - (n - 1); i++) {
    sum += w[i];
  }

  cout << sum << endl;

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/abc117/tasks/abc117_c

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))

int main() {
  int n, m, tmp;
  vector<int> v, w;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    v.push_back(tmp);
  }
  sort(v.begin(), v.end());

  // 座標の差分を計算
  for (int i = 1; i < m; i++) {
    w.push_back(v[i] - v[i - 1]);
  }

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

  int sum = 0, wsize = w.size();
  for (int i = 0; i < wsize - (n - 1); i++) {
    sum += w[i];
  }

  cout << sum << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define INF 100000000
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());
  vector<int> space(M - 1);
  for (int i = 0; i < M - 1; i++) {
    space[i] = X[i + 1] - X[i];
  }
  sort(space.begin(), space.end());
  for (int i = 0; i < N - 1; i++) {
    space.pop_back();
  }
  cout << accumulate(space.begin(), space.end(), 0);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define INF 100000000
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
int main() {
  int N, M;
  cin >> N >> M;
  if (N > M) {
    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> space(M - 1);
  for (int i = 0; i < M - 1; i++) {
    space[i] = X[i + 1] - X[i];
  }
  sort(space.begin(), space.end());
  for (int i = 0; i < N - 1; i++) {
    space.pop_back();
  }
  cout << accumulate(space.begin(), space.end(), 0);
}
Problem p03137 (C++)

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

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

  swap(n, m);

  vector<ll> arr(n), delta;

  for (int i = 0; i < n; i++)
    cin >> arr[i];

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

  for (int i = 1; i < n; i++) {
    delta.push_back(arr[i] - arr[i - 1]);
  }

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

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

  ll max_x = arr.back() - arr.front(), max_delta = 0;
  for (int i = 0; i < m - 1; i++) {
    max_delta += delta.back();
    delta.pop_back();
  }

  cout << max_x - max_delta << endl;
  return 0;
}


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

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

  swap(n, m);

  vector<ll> arr(n), delta;

  for (int i = 0; i < n; i++)
    cin >> arr[i];

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

  for (int i = 1; i < n; i++) {
    delta.push_back(arr[i] - arr[i - 1]);
  }

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

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

  ll max_x = arr.back() - arr.front(), max_delta = 0;
  for (int i = 0; i < m - 1; i++) {
    max_delta += delta.back();
    delta.pop_back();
  }

  cout << max_x - max_delta << endl;
  return 0;
}
Problem p03137 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int maxN = 5e2 + 5;
const ll INF = 1ll << 60;
int n, m;
ll a[maxN];
ll b[maxN];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + m);
  b[0] = 0;
  for (int i = 2; i <= m; i++) {
    ll x = a[i] - a[i - 1];
    b[i] = x;
  }
  sort(b + 1, b + 1 + m);
  reverse(b + 1, b + 1 + m);
  ll ans = 0;
  for (int i = n; i <= m; i++) {
    ans += b[i];
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<int, ll> pil;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int maxN = 5e5 + 5;
const ll INF = 1ll << 60;
int n, m;
ll a[maxN];
ll b[maxN];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + m);
  b[0] = 0;
  for (int i = 2; i <= m; i++) {
    ll x = a[i] - a[i - 1];
    b[i] = x;
  }
  sort(b + 1, b + 1 + m);
  reverse(b + 1, b + 1 + m);
  ll ans = 0;
  for (int i = n; i <= m; i++) {
    ans += b[i];
  }
  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 >> M;
  int X[M];
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X, X + M);
  vector<int> sub;
  int ans = 0;
  if (M > 1 && M < N) {
    for (int i = 0; i < M - 1; i++) {
      sub.push_back(X[i + 1] - X[i]);
    }
    std::sort(sub.begin(), sub.end());
    for (int i = 0; i < sub.size() - N + 1; i++) {
      ans += sub[i];
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
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);
  vector<int> sub;
  int ans = 0;
  if (M > 1 && M > N) {
    for (int i = 0; i < M - 1; i++) {
      sub.push_back(X[i + 1] - X[i]);
    }
    std::sort(sub.begin(), sub.end());
    for (int i = 0; i < sub.size() - N + 1; i++) {
      ans += sub[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

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

using namespace std;

int n, m, a[100005], b[100005], ans;

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + m + 1);
  for (int i = 1; i < m; i++) {
    b[i] = a[i + 1] - a[i];
  }
  sort(b + 1, b + m);
  ans = a[m] - a[1];
  for (int i = m - 1; i > m - n; i--) {
    ans -= b[i];
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int n, m, a[100005], b[100005], ans;

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
  }
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(a + 1, a + m + 1);
  for (int i = 1; i < m; i++) {
    b[i] = a[i + 1] - a[i];
  }
  sort(b + 1, b + m);
  ans = a[m] - a[1];
  for (int i = m - 1; i > m - n; i--) {
    ans -= b[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e8;

using namespace std;

#define vvint(vec, n, m, l) vector<vector<int>> vec(n, vector<int>(m, l));
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>;
#define pint pair<int, int>;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause")
#define ret return 0

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

  ll n, m;
  cin >> n >> m;
  vector<ll> x(m + 2);
  rep(i, m) { cin >> x[i]; }
  x[m] = INF;
  x[m + 1] = -INF;

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

  vector<ll> suki(m + 1);
  for (int i = 1; i <= m + 1; i++) {
    suki[i - 1] = x[i] - x[i - 1];
  }
  sort(suki.begin(), suki.end(), greater<int>());
  ll ans = 0;
  for (int i = 0; i < n + 1; i++) {
    ans += suki[i];
  }

  cout << 2 * INF - ans << endl;

  ret;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e8;

using namespace std;

#define vvint(vec, n, m, l) vector<vector<int>> vec(n, vector<int>(m, l));
#define vvll(vec, n, m, l) vector<vector<ll>> vec(n, vector<ll>(m, l));
#define vint vector<int>;
#define pint pair<int, int>;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define all(x) (x).begin(), (x).end()
#define debug system("pause")
#define ret return 0

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

  ll n, m;
  cin >> n >> m;
  vector<ll> x(m + 2);
  rep(i, m) { cin >> x[i]; }
  x[m] = INF;
  x[m + 1] = -INF;

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

  vector<ll> suki(m + 1);
  for (int i = 1; i <= m + 1; i++) {
    suki[i - 1] = x[i] - x[i - 1];
  }
  sort(suki.begin(), suki.end(), greater<int>());
  ll ans = 0;
  for (int i = 0; i < min(n + 1, m + 1); i++) {
    ans += suki[i];
  }

  cout << 2 * INF - ans << endl;

  ret;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define mREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int ans[6];
int main() {
  int n, m;
  cin >> n >> m;
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> v(m);
  vector<int> tmp(m - 1);
  rep(i, m) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  rep(i, m - 1) {
    ans += abs(v[i] - v[i + 1]);
    tmp[i] = abs(v[i] - v[i + 1]);
  }
  sort(all(tmp));
  reverse(all(tmp));
  rep(i, n - 1) { ans -= tmp[i]; }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define mREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int ans[6];
int main() {
  int n, m;
  cin >> n >> m;
  if (m < n)
    n = m;
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> v(m);
  vector<int> tmp(m - 1);
  rep(i, m) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  rep(i, m - 1) {
    ans += abs(v[i] - v[i + 1]);
    tmp[i] = abs(v[i] - v[i + 1]);
  }
  sort(all(tmp));
  reverse(all(tmp));
  rep(i, n - 1) { ans -= tmp[i]; }
  cout << ans << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
#define int long long
#define ll long long
#define mod 1000000007
#define MOD 1000000007
#define inf 1e17
#define rep(i, j, n) for (int i = j; i < n; i++)
#define P pair<int, int>
#define ps push_back
#define all(x) x.begin(), x.end()
double pi = 3.141592653589793;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// ここから始めよう
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, 0, m) {
    int a;
    cin >> a;
    a += 114514;
    v.ps(a);
  }
  sort(all(v));
  vector<int> key;
  int sum = v[m - 1] - v[0];
  rep(i, 1, m) { key.ps(v[i] - v[i - 1]); }
  sort(all(key));
  reverse(all(key));
  rep(i, 0, n - 1) { sum -= key[i]; }
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <type_traits>
#include <vector>
#define int long long
#define ll long long
#define mod 1000000007
#define MOD 1000000007
#define inf 1e17
#define rep(i, j, n) for (int i = j; i < n; i++)
#define P pair<int, int>
#define ps push_back
#define all(x) x.begin(), x.end()
double pi = 3.141592653589793;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// ここから始めよう
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, 0, m) {
    int a;
    cin >> a;
    a += 114514;
    v.ps(a);
  }
  sort(all(v));
  vector<int> key;
  int sum = v[m - 1] - v[0];
  rep(i, 1, m) { key.ps(v[i] - v[i - 1]); }
  sort(all(key));
  reverse(all(key));
  rep(i, 0, n - 1) { sum -= key[i]; }
  cout << sum << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define per(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define _GLIBCXX_DEBUG
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef long long ll;
ll MOD = 1000000007;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  ll n, m;
  cin >> n >> m;
  vector<ll> x(m), xd(m - 1);
  rep(i, m) cin >> x[i];
  sort(all(x));
  rep(i, m - 1) { xd[i] = abs(x[i + 1] - x[i]); }
  sort(all(xd));
  cout << accumulate(xd.begin(), xd.begin() + (xd.size() - n + 1), 0LL) << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define per(i, n) for (ll i = n - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define _GLIBCXX_DEBUG
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
typedef long long ll;
ll MOD = 1000000007;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  ll n, m;
  cin >> n >> m;
  vector<ll> x(m), xd(m - 1);
  rep(i, m) cin >> x[i];
  sort(all(x));
  rep(i, m - 1) { xd[i] = abs(x[i + 1] - x[i]); }
  sort(all(xd));
  ll sum = 0;
  rep(i, max(0LL, m - n)) sum += xd[i];
  cout << sum << endl;
}
Problem p03137 (C++)

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

int main() {
  int N, M, sum, S;
  cin >> N >> M;
  vector<int> X(M), Y(M - 1);
  for (int i = 0; i < M; ++i)
    cin >> X.at(i);
  sort(X.begin(), X.end());
  for (int i = 0; i < M - 1; ++i)
    Y.at(i) = X.at(i + 1) - X.at(i);
  sort(Y.begin(), Y.end());
  reverse(Y.begin(), Y.end());
  sum = X.at(M - 1) - X.at(0);
  for (int i = 0; i < N - 1; ++i)
    S += Y.at(i);
  cout << sum - S << endl;
}


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

int main() {
  int N, M, sum, S;
  cin >> N >> M;
  if (N >= M)
    cout << 0 << endl;
  else {
    vector<int> X(M), Y(M - 1);
    for (int i = 0; i < M; ++i)
      cin >> X.at(i);
    sort(X.begin(), X.end());
    for (int i = 0; i < M - 1; ++i)
      Y.at(i) = X.at(i + 1) - X.at(i);
    sort(Y.begin(), Y.end());
    reverse(Y.begin(), Y.end());
    sum = X.at(M - 1) - X.at(0);
    for (int i = 0; i < N - 1; ++i)
      S += Y.at(i);
    cout << sum - S << endl;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
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;
    return 0;
  }
  sort(x.begin(), x.end());
  vector<int> d(m - 1);
  for (int i = 0; i < m - 1; i++) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d.begin(), d.end());
  ll ans = 0;
  for (int i = 0; i < d.size() - (n - 1); i++) {
    ans += d[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
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;
    return 0;
  }
  sort(x.begin(), x.end());
  vector<int> d(m - 1);
  for (int i = 0; i < m - 1; i++) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d.begin(), d.end());
  ll ans = 0;
  for (int i = 0; i < (int)d.size() - (n - 1); i++) {
    ans += d[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(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> dist(m - 1);
  rep(i, m - 1) dist[i] = x[i + 1] - x[i];

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

  ll ans = accumulate(dist.begin(), dist.end(), 0);
  rep(i, n - 1) ans -= dist[i];

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(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> dist(m - 1);
  rep(i, m - 1) dist[i] = x[i + 1] - x[i];

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

  ll ans = accumulate(dist.begin(), dist.end(), 0);
  int cnt = min(n - 1, m - 1);
  rep(i, cnt) ans -= dist[i];

  cout << ans << endl;
  return 0;
}
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++)
#define rep1(i, n) for (int i = 0; i <= (int)(n); i++)
using P = pair<int, int>;
using pq = priority_queue<int>;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  int sum = 0;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  pq q;
  sort(x.begin(), x.end());
  rep(i, m - 1) {
    q.push(abs(x[i] - x[i + 1]));
    sum += abs(x[i] - x[i + 1]);
  }
  // cout << sum;
  rep(i, n - 1) {
    sum -= q.top();
    q.pop();
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 0; i <= (int)(n); i++)
using P = pair<int, int>;
using pq = priority_queue<int>;
typedef long long ll;

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

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

int getMinMove(int n, const std::vector<int> &positions) {
  if (static_cast<int>(positions.size()) <= n) {
    return 0;
  }
  auto pos_sorted = positions;
  std::sort(pos_sorted.begin(), pos_sorted.end());
  std::vector<int> diffs(positions.size() - 1);
  for (int i = 0; i < static_cast<int>(diffs.size()); ++i) {
    diffs[i] = pos_sorted[i + 1] - pos_sorted[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    auto iter = std::max_element(diffs.begin(), diffs.end());
    diffs.erase(iter);
  }
  return std::accumulate(diffs.begin(), diffs.end(), 0);
  return 0;
}

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> positions(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> positions[i];
  }
  std::cout << getMinMove(n, positions) << std::endl;
  return 0;
}

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

int getMinMove(int n, const std::vector<int> &positions) {
  if (static_cast<int>(positions.size()) <= n) {
    return 0;
  }
  auto pos_sorted = positions;
  std::sort(pos_sorted.begin(), pos_sorted.end());
  std::vector<int> diffs(positions.size() - 1);
  for (int i = 0; i < static_cast<int>(diffs.size()); ++i) {
    diffs[i] = pos_sorted[i + 1] - pos_sorted[i];
  }
  std::sort(diffs.begin(), diffs.end());
  return std::accumulate(diffs.begin(), diffs.end() - (n - 1), 0);
}

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> positions(m);
  for (int i = 0; i < m; ++i) {
    std::cin >> positions[i];
  }
  std::cout << getMinMove(n, positions) << std::endl;
  return 0;
}
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[n];
  for (int i = 0; i < m; ++i)
    cin >> x[i];
  if (n >= m) {
    cout << 0 << '\n';
    return 0;
  }
  sort(x, x + m);
  priority_queue<int> pq;
  for (int i = 0; i < m - 1; ++i) {
    pq.push(x[i + 1] - x[i]);
  }
  int ans = x[m - 1] - x[0];
  while (!pq.empty() && n != 1) {
    int diff = pq.top();
    pq.pop();
    ans -= diff;
    n--;
  }

  cout << ans << '\n';

  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];
  if (n >= m) {
    cout << 0 << '\n';
    return 0;
  }
  sort(x, x + m);
  priority_queue<int> pq;
  for (int i = 0; i < m - 1; ++i) {
    pq.push(x[i + 1] - x[i]);
  }
  int ans = x[m - 1] - x[0];
  while (!pq.empty() && n != 1) {
    int diff = pq.top();
    pq.pop();
    ans -= diff;
    n--;
  }

  cout << ans << '\n';

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie();                                                                   \
  cout.tie();
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll mininf = -922337203685477;
const ll nax = 1e5 + 5;
ll n, m, arr[nax], ans = 0;
vector<ll> v;

bool cmp(ll a, ll b) { return a > b; }

int main() {
  IOS cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + m);
  for (int i = 1; i < m; i++) {
    v.pb(arr[i] - arr[i - 1]);
    ans += arr[i] - arr[i - 1];
  }
  if (v.empty()) {
    cout << 0 << '\n';
    return 0;
  }
  sort(v.rbegin(), v.rend());
  for (int i = 0; i < n - 1; i++) {
    ans -= v[i];
  }
  if (ans < 0) {
    cout << 0 << '\n';
    return 0;
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie();                                                                   \
  cout.tie();
#define ff first
#define ss second
#define pb push_back
#define debug(val) cerr << "The value of " << #val << " is = " << val << '\n';
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll mininf = -922337203685477;
const ll nax = 1e5 + 5;
ll n, m, arr[nax], ans = 0;
vector<ll> v;

bool cmp(ll a, ll b) { return a > b; }

int main() {
  IOS cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> arr[i];
  }
  if (n >= m) {
    cout << 0 << '\n';
    return 0;
  }
  sort(arr, arr + m);
  for (int i = 1; i < m; i++) {
    v.pb(arr[i] - arr[i - 1]);
    ans += arr[i] - arr[i - 1];
  }
  if (v.empty()) {
    cout << 0 << '\n';
    return 0;
  }
  sort(v.rbegin(), v.rend());
  for (int i = 0; i < n - 1; i++) {
    ans -= v[i];
  }
  if (ans < 0) {
    cout << 0 << '\n';
    return 0;
  }
  cout << ans << '\n';
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int N, M;
int X[100000], D[99999];

void merge_sort(int *a, int n) {
  if (n == 1)
    return;
  int m = n / 2;
  merge_sort(a, m);
  merge_sort(a + m, n - m);
  static int a_[100000];
  for (int i = 0; i < n; i++) {
    a_[i] = a[i];
  }
  int i = 0;
  int j = m;
  int k = 0;
  while (i < m || j < n) {
    if (j == n || (i < m && a_[i] <= a_[j])) {
      a[k++] = a_[i++];
    } else {
      a[k++] = a_[j++];
    }
  }
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d", &X[i]);
  }
  merge_sort(X, M);
  int ans = 0;
  for (int i = 0; i + 1 < M; i++) {
    D[i] = X[i + 1] - X[i];
    ans += D[i];
  }
  merge_sort(D, M - 1);
  int k = M - 2;
  while (k >= 0 && N >= 2) {
    ans -= D[k--];
    N--;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <stdio.h>

int N, M;
int X[100000], D[99999];

void merge_sort(int *a, int n) {
  if (n <= 1)
    return;
  int m = n / 2;
  merge_sort(a, m);
  merge_sort(a + m, n - m);
  static int a_[100000];
  for (int i = 0; i < n; i++) {
    a_[i] = a[i];
  }
  int i = 0;
  int j = m;
  int k = 0;
  while (i < m || j < n) {
    if (j == n || (i < m && a_[i] <= a_[j])) {
      a[k++] = a_[i++];
    } else {
      a[k++] = a_[j++];
    }
  }
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d", &X[i]);
  }
  merge_sort(X, M);
  int ans = 0;
  for (int i = 0; i + 1 < M; i++) {
    D[i] = X[i + 1] - X[i];
    ans += D[i];
  }
  merge_sort(D, M - 1);
  int k = M - 2;
  while (k >= 0 && N >= 2) {
    ans -= D[k--];
    N--;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03137 (C++)

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

using namespace std;
using ll = long long;

int main() {
  int M = 0;
  int N;
  cin >> N;
  cin >> M;
  vector<int> A(M, 0);
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  ll ans = A.at(M - 1) - A.at(0);
  // cout << ans << endl;
  for (int i = 0; i < M - 1; i++) {
    A.at(i) = A.at(i + 1) - A.at(i);
  }
  A.at(M - 1) = 0;
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  for (int i = 0; i < N - 1; i++) {
    // cout << A.at(i) << endl;
    ans -= A.at(i);
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

int main() {
  int M = 0;
  int N;
  cin >> N;
  cin >> M;
  vector<int> A(M, 0);
  if (M == 1 || N >= M) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  ll ans = A.at(M - 1) - A.at(0);
  // cout << ans << endl;
  for (int i = 0; i < M - 1; i++) {
    A.at(i) = A.at(i + 1) - A.at(i);
  }
  A.at(M - 1) = 0;
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  for (int i = 0; i < N - 1; i++) {
    // cout << A.at(i) << endl;
    ans -= A.at(i);
  }

  cout << ans << endl;

  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)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);
#define chmin(x, y) x = min(x, y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  sort(x.begin(), x.end());
  priority_queue<int> q;
  rep(i, m - 1) {
    int a = x[i + 1] - x[i];
    q.push(a);
  }
  rep(i, n - 1) { q.pop(); }
  int ans = 0;
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  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;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y);
#define chmin(x, y) x = min(x, y);
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const int INF = 1001001001;

int main() {
  int n, m;
  cin >> n >> m;
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  sort(x.begin(), x.end());
  priority_queue<int> q;
  rep(i, m - 1) {
    int a = x[i + 1] - x[i];
    q.push(a);
  }
  rep(i, n - 1) { q.pop(); }
  int ans = 0;
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  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;
  cin >> n >> m;

  vector<int> x(m);
  for (int i = 0; i < m; i++)
    cin >> x[i];

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

  int ans = x[m - 1] - x[0];

  if (m == 1)
    cout << ans << endl;
  else {
    vector<int> dif(m - 1);
    for (int i = 0; i < m - 1; i++)
      dif[i] = x[i + 1] - x[i];

    sort(dif.begin(), dif.end());
    reverse(dif.begin(), dif.end());

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

    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
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];

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

  int ans = x[m - 1] - x[0];

  if (m == 1)
    cout << ans << endl;
  else {
    vector<int> dif(m - 1);
    for (int i = 0; i < m - 1; i++)
      dif[i] = x[i + 1] - x[i];

    sort(dif.begin(), dif.end());
    reverse(dif.begin(), dif.end());

    for (int i = 0; i < n - 1 && i < m; i++) {
      ans -= dif[i];
    }

    cout << ans << endl;
  }
}
Problem p03137 (C++)

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

using namespace std;
/*
int find(int x, vector<int> &p) {
if (p[x] == x)return x;
return p[x] = find(p[x], p);
}

void uni(int x, int y, vector<int> &p) {
x = find(x, p);
y = find(y, p);
p[x] = y;
}

int same(int x, int y, vector<int> p) {
if (find(x, p) == find(y, p))return 1;
return 0;
}
*/

// long long a[1002];

int main() {
  long long N, M, cnt = 0, tmp, ans = 0;
  cin >> N >> M;
  vector<long long> X(M), D(M - 1);

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

  sort(D.begin(), D.end());
  for (int i = 0; i < N - 1; i++) {
    sum -= D[M - 2 - i];
    if (sum <= 0) {
      sum = 0;
      break;
    }
  }

  cout << sum;

  char qq;
  cin >> qq;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
/*
int find(int x, vector<int> &p) {
if (p[x] == x)return x;
return p[x] = find(p[x], p);
}

void uni(int x, int y, vector<int> &p) {
x = find(x, p);
y = find(y, p);
p[x] = y;
}

int same(int x, int y, vector<int> p) {
if (find(x, p) == find(y, p))return 1;
return 0;
}
*/

// long long a[1002];

int main() {
  long long N, M, cnt = 0, tmp, ans = 0;
  cin >> N >> M;
  vector<long long> X(M), D(M - 1);

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

  sort(D.begin(), D.end());
  for (int i = 0; i < N - 1; i++) {
    if (M - 2 - i < 0) {
      sum = 0;
      break;
    }
    sum -= D[M - 2 - i];
    if (sum <= 0) {
      sum = 0;
      break;
    }
  }

  cout << sum;

  char qq;
  cin >> qq;
  return 0;
}
Problem p03137 (C++)

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

using namespace std;

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

  sort(xsub.begin(), xsub.end(), greater<int>());
  int length = x[M - 1] - x[0];
  for (int i = 0; i < N - 1; i++) {
    length -= xsub[i];
  }
  cout << length << endl;
  return 0;
}


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

using namespace std;

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

  sort(xsub.begin(), xsub.end(), greater<int>());
  int length = x[M - 1] - x[0];
  for (int i = 0; i < N - 1; i++) {
    length -= xsub[i];
  }
  cout << length << endl;
  return 0;
}
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;
  vector<int> destinations(M);
  for (int i = 0; i < M; ++i) {
    cin >> destinations[i];
  }
  sort(destinations.begin(), destinations.end());
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> distances(M - 1);
  for (int i = 0; i < M - 1; ++i) {
    distances[i] = destinations[i + 1] - destinations[i];
  }
  int sum = accumulate(distances.begin(), distances.end(), 0);
  sort(distances.rbegin(), distances.rend());
  for (int i = 0; i < N - 1; ++i) {
    sum -= distances[i];
  }
  cout << sum << endl;
}

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

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> destinations(M);
  for (int i = 0; i < M; ++i) {
    cin >> destinations[i];
  }
  sort(destinations.begin(), destinations.end());
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> distances(M - 1);
  for (int i = 0; i < M - 1; ++i) {
    distances[i] = destinations[i + 1] - destinations[i];
  }
  int sum = accumulate(distances.begin(), distances.end(), 0);
  sort(distances.rbegin(), distances.rend());
  for (int i = 0; i < min(N - 1, M - 1); ++i) {
    sum -= distances[i];
  }
  cout << sum << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int SZ = 10005;
priority_queue<int> pq;
int arr[SZ];
int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  n--;
  for (int i = 0; i < m; i++)
    scanf("%d", &arr[i]);
  sort(arr, arr + m);
  int ans = arr[m - 1] - arr[0];
  for (int i = 1; i < m; i++) {
    pq.push(arr[i] - arr[i - 1]);
  }
  while (!pq.empty() && n > 0) {
    int t = pq.top();
    pq.pop();
    ans -= t;
    n--;
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int SZ = 100005;
priority_queue<int> pq;
int arr[SZ];
int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);
  n--;
  for (int i = 0; i < m; i++)
    scanf("%d", &arr[i]);
  sort(arr, arr + m);
  int ans = arr[m - 1] - arr[0];
  for (int i = 1; i < m; i++) {
    pq.push(arr[i] - arr[i - 1]);
  }
  while (!pq.empty() && n > 0) {
    int t = pq.top();
    pq.pop();
    ans -= t;
    n--;
  }
  printf("%d", ans);
  return 0;
}
Problem p03137 (C++)

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

using namespace std;

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

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

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

  sort(x.begin(), x.end());
  for (int i = 0; i < m - 1; i++) {
    xx[i] = x[i + 1] - x[i];
  }

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

  long long summ = 0;
  for (int i = 0; i < n - 1; i++) {
    summ += xx.back();
    xx.pop_back();
  }
  cout << x[m - 1] - x[0] - summ << endl;
  return 0;
}

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

using namespace std;

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

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

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

  sort(x.begin(), x.end());
  for (int i = 0; i < m - 1; i++) {
    xx[i] = x[i + 1] - x[i];
  }

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

  long long summ = 0;
  for (int i = 0; i < n - 1; i++) {
    summ += xx.back();
    xx.pop_back();
    if (xx.empty() == true) {
      break;
    }
  }
  cout << x[m - 1] - x[0] - summ << endl;
  return 0;
}