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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define int ll

int n, m;
int a[1000000];
int l[1000000];
int ans;

signed main() {
  cin >> n >> m;
  rep(i, m) { cin >> a[i]; }

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

  sort(a, a + m);
  ans = a[m - 1] - a[0];
  rep(i, m - 1) { l[i] = a[i + 1] - a[i]; }
  sort(l, l + m - 1);
  rep(i, n) { ans -= l[m - 1 - i]; }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(a, n)                                                               \
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define int ll

int n, m;
int a[1000000];
int l[1000000];
int ans;

signed main() {
  cin >> n >> m;
  rep(i, m) { cin >> a[i]; }

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

  sort(a, a + m);
  ans = a[m - 1] - a[0];
  rep(i, m - 1) { l[i] = a[i + 1] - a[i]; }
  sort(l, l + m - 1);
  rep(i, n) { ans -= l[m - 1 - i]; }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << 0;
    return 0;
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << 0;
    return 0;
  }

  vector<int> X(M, 0);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());
  vector<int> sa(M - 1, 0);
  for (int i = 0; i < M - 1; i++) {
    sa[i] = X[i + 1] - X[i];
  }
  sort(sa.begin(), sa.end());
  int ans = 0;
  for (int i = 0; i < M - N; i++) {
    ans = ans + sa[i];
  }
  cout << ans;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  ll n, m;
  cin >> n >> m;
  vl x(m);
  rep(i, m) cin >> x[i];
  sort(all(x));
  if (m == 1)
    cout << 0 << endl;
  else {
    vl y(m - 1);
    rep(i, m - 1) y[i] = x[i + 1] - x[i];
    sort(y.rbegin(), y.rend());
    rep(i, min(n - 1, m)) y[i] = 0;
    ll sum = 0;
    rep(i, m - 1) sum += y[i];
    cout << sum << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define INF 1 << 30
#define LINF (ll)1 << 62
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main() {
  ll n, m;
  cin >> n >> m;
  vl x(m);
  rep(i, m) cin >> x[i];
  sort(all(x));
  if (m == 1)
    cout << 0 << endl;
  else {
    vl y(m - 1);
    rep(i, m - 1) y[i] = x[i + 1] - x[i];
    sort(y.rbegin(), y.rend());
    rep(i, min(n - 1, m - 1)) y[i] = 0;
    ll sum = 0;
    rep(i, m - 1) sum += y[i];
    cout << sum << 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;
  cin >> N >> M;
  if (M == 1) {
    cout << "0\n";
    return 0;
  }
  vector<int> L(M);
  for (int i = 0; i < M; i++) {
    cin >> L[i];
  }
  sort(L.begin(), L.end());
  vector<int> Width(M - 1);
  for (int i = 0; i < M - 1; i++) {
    Width[i] = L[i + 1] - L[i];
  }
  sort(Width.begin(), Width.end());
  for (int i = 0; i < N - 1; i++) {
    Width.pop_back();
  }
  cout << accumulate(Width.begin(), Width.end(), 0) << "\n";
}


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

int main() {
  int N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << "0\n";
    return 0;
  }
  if (M == 1) {
    cout << "0\n";
    return 0;
  }
  vector<int> L(M);
  for (int i = 0; i < M; i++) {
    cin >> L[i];
  }
  sort(L.begin(), L.end());
  vector<int> Width(M - 1);
  for (int i = 0; i < M - 1; i++) {
    Width[i] = L[i + 1] - L[i];
  }
  sort(Width.begin(), Width.end());
  for (int i = 0; i < N - 1; i++) {
    Width.pop_back();
  }
  cout << accumulate(Width.begin(), Width.end(), 0) << "\n";
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#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];

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

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

  vector<long long> Y(M - 1);
  for (int i = 0; i < M - 1; i++)
    Y[i] = X[i + 1] - X[i];
  sort(Y.rbegin(), Y.rend());
  long long ans = 0;
  for (int i = 0; i < N - 1; i++)
    ans += Y[i];

  cout << X.back() - X.front() - ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#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];

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

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

  vector<long long> Y(M - 1);
  for (int i = 0; i < M - 1; i++)
    Y[i] = X[i + 1] - X[i];
  sort(Y.rbegin(), Y.rend());
  long long ans = 0;
  for (int i = 0; i < min(M - 1, N - 1); i++)
    ans += Y[i];

  cout << X.back() - X.front() - 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;
  if (m == 1) {
    cout << "0" << endl;
    return 0;
  }
  vector<int> x(m);
  vector<int> dist(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++) {
    dist[i] = x[i + 1] - x[i];
  }
  sort(dist.begin(), dist.end());
  for (int i = 0; i < n - 1; i++) {
    dist.pop_back();
  }
  int ans = 0;
  for (int i = 0; i < dist.size(); i++) {
    ans += dist[i];
  }
  cout << ans << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  if (m == 1) {
    cout << "0" << endl;
    return 0;
  }
  vector<int> x(m);
  vector<int> dist(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++) {
    dist[i] = x[i + 1] - x[i];
  }
  sort(dist.begin(), dist.end());
  for (int i = 0; i < n - 1; i++) {
    dist.pop_back();
  }
  int ans = 0;
  int size = dist.size();
  for (int i = 0; i < size; i++) {
    ans += dist[i];
  }
  cout << ans << endl;
}
Problem p03137 (C++)

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

int const static n_max = 100000;
int n, m, x[n_max + 10], l[n_max + 10];

int main() {
  cin >> n >> m;
  rep(i, m) cin >> x[i];

  sort(x, x + m);

  for (int i = 1; i < m; i++) {
    l[i - 1] = x[i] - x[i - 1];
  }

  sort(l, l + m - 1);

  int sum = 0;
  for (int i = m - 1 - 1; i > m - n - 1; i--) {
    sum += l[i];
  }
  cout << ((x[m - 1] - x[0]) - sum) << endl;
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int const static n_max = 100000;
int n, m, x[n_max + 10], l[n_max + 10];

int main() {
  cin >> n >> m;
  rep(i, m) cin >> x[i];

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

  sort(x, x + m);

  for (int i = 1; i < m; i++) {
    l[i - 1] = x[i] - x[i - 1];
  }

  sort(l, l + m - 1);

  int sum = 0;
  for (int i = m - 1 - 1; i > m - n - 1; i--) {
    sum += l[i];
  }
  cout << ((x[m - 1] - x[0]) - sum) << endl;
}
Problem p03137 (C++)

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

using ll = long long;
using P = pair<int, int>;

const int INF = 1e9;
const int MOD = 1e9 + 7;

// 4近傍、8近傍
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  // 入力
  int N, M;
  cin >> N >> M;
  ll x[M];
  for (int i = 0; i < M; i++)
    scanf("%lld", x + i);

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

  sort(x, x + M);
  ll d[M - 1];
  for (int i = 0; i < M - 1; i++) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d, d + (M - 1));

  cout << accumulate(d, d + (M - N), 0) << endl;
  return 0;
}


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

using ll = long long;
using P = pair<int, int>;

const int INF = 1e9;
const int MOD = 1e9 + 7;

// 4近傍、8近傍
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  // 入力
  int N, M;
  cin >> N >> M;
  ll x[M];
  for (int i = 0; i < M; i++)
    scanf("%lld", x + i);

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

  sort(x, x + M);
  ll d[M - 1];
  for (int i = 0; i < M - 1; i++) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d, d + (M - 1));

  cout << accumulate(d, d + (M - N), 0) << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAX 10005
#define MOD 1000000007
#define INF 1e9
#define mp make_pair
using namespace std;

typedef long long ll;
int arr[MAX];
vector<int> diff;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  int n, k;

  cin >> k >> n;

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

  if (k >= n) {
    cout << 0 << endl;
    return 0;
  }
  sort(arr, arr + n);

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

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

  for (int i = 0; i < k - 1; i++) {
    sum -= diff[i];
  }

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MAX 100005
#define MOD 1000000007
#define INF 1e9
#define mp make_pair
using namespace std;

typedef long long ll;
int arr[MAX];
vector<int> diff;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  int n, k;

  cin >> k >> n;

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

  if (k >= n) {
    cout << 0 << endl;
    return 0;
  }
  sort(arr, arr + n);

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

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

  for (int i = 0; i < k - 1; i++) {
    sum -= diff[i];
  }

  cout << sum << endl;

  return 0;
}
Problem p03137 (C++)

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

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

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

int main() {
  long long n, m, x[100009];
  long long int sm = 0;
  vector<long long> v1, v2;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i];
    v1.push_back(x[i]);
  }
  if (n >= m) {
    cout << 0 << endl;
  } else {
    sort(v1.begin(), v1.end());
    for (int i = 1; i < m; i++) {
      v2.push_back(v1[i] - v1[i - 1]);
      sm += v1[i] - v1[i - 1];
    }
    sort(v2.begin(), v2.end(), greater<long long>());
    for (int i = 0; i < n - 1; i++) {
      sm -= v2[i];
    }
    cout << sm << endl;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for (int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

int main() {
  ll N, M;
  cin >> N >> M;
  vl X(M);
  rep(i, M) cin >> X[i];

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

  stable_sort(X.begin(), X.end());

  vl Y(N - 1);
  ll ans = 0;
  rep(i, M - 1) {
    Y[i] = X[i + 1] - X[i];
    ans += Y[i];
  }

  stable_sort(Y.begin(), Y.end(), greater<ll>());

  rep(i, N - 1) ans -= Y[i];

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for (int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

int main() {
  ll N, M;
  cin >> N >> M;
  vl X(M);
  rep(i, M) cin >> X[i];

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

  stable_sort(X.begin(), X.end());

  vl Y(M - 1);
  ll ans = 0;
  rep(i, M - 1) {
    Y[i] = X[i + 1] - X[i];
    ans += Y[i];
  }

  stable_sort(Y.begin(), Y.end(), greater<ll>());

  rep(i, N - 1) ans -= Y[i];

  cout << ans << "\n";

  return 0;
}
Problem p03137 (C++)

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

using namespace std;

#define N 11000
#define INF 0x3f3f3f3f
#define _for(i, a, b) for (int i = (a); i < (b); ++i)

typedef long long ll;

int a[N], dif[N];

int main() {
  int n, m;
  while (scanf("%d%d", &m, &n) != EOF) {
    int res = 0;
    for (int i = 0; i < n; ++i)
      scanf("%d", &a[i]);
    sort(a, a + n);
    for (int i = 0; i < n - 1; ++i)
      dif[i] = a[i + 1] - a[i];
    n = n - 1;
    m = m - 1;
    sort(dif, dif + n);
    for (int i = 0; i < n - m; ++i)
      res += dif[i];
    printf("%d\n", res);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

#define N 110000
#define INF 0x3f3f3f3f
#define _for(i, a, b) for (int i = (a); i < (b); ++i)

typedef long long ll;

int a[N], dif[N];

int main() {
  int n, m;
  while (scanf("%d%d", &m, &n) != EOF) {
    int res = 0;
    for (int i = 0; i < n; ++i)
      scanf("%d", &a[i]);
    sort(a, a + n);
    for (int i = 0; i < n - 1; ++i)
      dif[i] = a[i + 1] - a[i];
    n = n - 1;
    m = m - 1;
    sort(dif, dif + n);
    for (int i = 0; i < n - m; ++i)
      res += dif[i];
    printf("%d\n", res);
  }
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
#define LOOP1(i, N) for (int i = 1; i <= N; i++)
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

int main() {
  int n, m;
  int x[10000] = {};
  int d[10000] = {};
  int ans = 0;

  cin >> n >> m;
  LOOP(i, m) { cin >> x[i]; }

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

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

  // LOOP(i,m){
  //   cout << x[i] << ":" << d[i] << endl;
  // }

  for (int i = n - 1; i < m; i++) {
    ans += d[i];
  }
  ans *= -1;
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
#define LOOP1(i, N) for (int i = 1; i <= N; i++)
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;

int main() {
  int n, m;
  int x[100000] = {};
  int d[100000] = {};
  int ans = 0;

  cin >> n >> m;
  LOOP(i, m) { cin >> x[i]; }

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

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

  // LOOP(i,m){
  //   cout << x[i] << ":" << d[i] << endl;
  // }

  for (int i = n - 1; i < m; i++) {
    ans += d[i];
  }
  ans *= -1;
  cout << ans;
}
Problem p03137 (C++)

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

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

using ll = long long;
using ull = unsigned long long;
const ll INF = LLONG_MAX / 2;
const ll MOD = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;
  int X[M];
  for (int i = 0; i < M; i++)
    cin >> X[i];
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(X, X + M);
  int difX[M - 1];
  for (int i = 0; i + 1 < M; i++) {
    difX[i] = X[i + 1] - X[i];
  }
  sort(difX, difX + M - 1, greater<int>());
  int ans = X[M - 1] - X[0];
  for (int i = 0; i < N - 1; i++) {
    ans -= difX[i];
  }
  cout << ans << endl;

  return 0;
}

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

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \
  for (long long hoge = 0; (hoge) < (n); ++(hoge))                             \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

using ll = long long;
using ull = unsigned long long;
const ll INF = LLONG_MAX / 2;
const ll MOD = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int N, M;
  cin >> N >> M;
  int X[M];
  for (int i = 0; i < M; i++)
    cin >> X[i];
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  sort(X, X + M);
  int difX[M - 1];
  for (int i = 0; i + 1 < M; i++) {
    difX[i] = X[i + 1] - X[i];
  }
  sort(difX, difX + M - 1, greater<int>());
  int ans = X[M - 1] - X[0];
  for (int i = 0; i < N - 1; i++) {
    ans -= difX[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

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

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

    return 0;
  }

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

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

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

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

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

  cout << ans;

  return 0;
}

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

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

    return 0;
  }

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

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

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

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

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

  cout << ans;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define INF 1LL << 60
#define MAX 1000000000
const int MOD = 1000000007;
int main(void) {
  int N, M;
  ll l;
  scanf("%d%d", &N, &M);
  if (M == 1) {
    puts("0");
    return 0;
  }
  vector<int> X(M), D(M - 1);
  rep(i, 0, M) { scanf("%d", &X[i]); }
  sort(all(X));
  l = X.back() - X.front();
  rep(i, 0, M - 1) D[i] = X[i + 1] - X[i];
  r_sort(D);
  rep(i, 0, N - 1) { l -= D[i]; }
  printf("%lld\n", l);
  return 0;
}
int gcd(int a, int b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
} /*
 int main() {
         int N, K;
         ll s = 0;
         scanf("%d%d", &N, &K);
         rep(l, 1, K + 1) {
                 s += (modpow(K - l + 1,N) - modpow(K - l, N)) % MOD + modpow(K
 / l, N - 1) * (l - 1); printf("%lld %lld \n",s, (modpow(K - l + 1, N) -
 modpow(K - l, N)) % MOD, N - 1); s %= MOD;
         }
         printf("%lld\n", s);
         return 0;
 }*/

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define INF 1LL << 60
#define MAX 1000000000
const int MOD = 1000000007;
int main(void) {
  int N, M;
  ll l;
  scanf("%d%d", &N, &M);
  if (M == 1 || N >= M) {
    puts("0");
    return 0;
  }
  vector<int> X(M), D(M - 1);
  rep(i, 0, M) { scanf("%d", &X[i]); }
  sort(all(X));
  l = X.back() - X.front();
  rep(i, 0, M - 1) D[i] = X[i + 1] - X[i];
  r_sort(D);
  rep(i, 0, N - 1) { l -= D[i]; }
  printf("%lld\n", l);
  return 0;
}
int gcd(int a, int b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
} /*
 int main() {
         int N, K;
         ll s = 0;
         scanf("%d%d", &N, &K);
         rep(l, 1, K + 1) {
                 s += (modpow(K - l + 1,N) - modpow(K - l, N)) % MOD + modpow(K
 / l, N - 1) * (l - 1); printf("%lld %lld \n",s, (modpow(K - l + 1, N) -
 modpow(K - l, N)) % MOD, N - 1); s %= MOD;
         }
         printf("%lld\n", s);
         return 0;
 }*/
Problem p03137 (C++)

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

using namespace std;

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

  vector<long long> v(k);
  vector<long long> dv(k - 1);
  for (int i = 0; i < k; i++) {
    cin >> v[i];
  }
  std::sort(v.begin(), v.end(), std::greater<int>());
  long long max = *std::max_element(v.begin(), v.end());
  long long min = *std::min_element(v.begin(), v.end());

  long long suml = max - min;
  long long suma = 0;

  if (k == 1) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < k - 1; i++) {
      dv[i] = v[i] - v[i + 1];
    }
    std::sort(dv.begin(), dv.end(), std::greater<int>());
    if (n == 1) {
      cout << suml << endl;
    } else {
      for (int i = 0; i < n - 1; i++) {
        suma += dv[i];
      }
      cout << suml - suma << endl;
    }
  }
  return 0;
}

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

using namespace std;

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

  vector<long long> v(k);
  vector<long long> dv(k - 1);
  for (int i = 0; i < k; i++) {
    cin >> v[i];
  }
  std::sort(v.begin(), v.end(), std::greater<int>());
  long long max = *std::max_element(v.begin(), v.end());
  long long min = *std::min_element(v.begin(), v.end());

  long long suml = max - min;
  long long suma = 0;

  if (k == 1 || k <= n) {
    cout << 0 << endl;
  } else {
    for (int i = 0; i < k - 1; i++) {
      dv[i] = v[i] - v[i + 1];
    }
    std::sort(dv.begin(), dv.end(), std::greater<int>());
    if (n == 1) {
      cout << suml << endl;
    } else {
      for (int i = 0; i < n - 1; i++) {
        suma += dv[i];
      }
      cout << suml - suma << endl;
    }
  }
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (long long i = (a); i >= (b); --i)
#define REPR(i, n) FORR(i, n, 0)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  ll n, m;
  vector<ll> x, y;
  cin >> n >> m;
  REP(i, m) {
    ll z;
    cin >> z;
    x.push_back(z);
  }
  sort(x.begin(), x.end());
  REP(i, x.size() - 1) {
    ll z = x[i + 1] - x[i];
    y.push_back(z);
  }
  sort(y.begin(), y.end());
  ll ans = 0;
  REP(i, y.size() - n + 1) { ans += y[i]; }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (long long i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (long long i = (a); i >= (b); --i)
#define REPR(i, n) FORR(i, n, 0)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  ll n, m;
  vector<ll> x, y;
  cin >> n >> m;
  REP(i, m) {
    ll z;
    cin >> z;
    x.push_back(z);
  }
  sort(x.begin(), x.end());
  REP(i, x.size() - 1) {
    ll z = x[i + 1] - x[i];
    y.push_back(z);
  }
  sort(y.begin(), y.end());
  ll ans = 0;

  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  REP(i, y.size() - n + 1) { ans += y[i]; }

  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  if (m == 1) {
    cout << 0 << '\n';
    return 0;
  }
  if (n == 1) {
    cout << a[m - 1] - a[0] << '\n';
    return 0;
  }
  vector<int> v;
  for (int i = 0; i < m - 1; i++) {
    v.push_back(a[i + 1] - a[i]);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  multiset<int> largest;
  for (int i = 0; i < n - 1; i++) {
    largest.insert(v[i]);
  }
  long long ans = 0;
  int start_point = 0;
  for (int i = 0; i < m - 1; i++) {
    if (!largest.empty()) {
      if (largest.find(a[i + 1] - a[i]) != largest.end()) {
        largest.erase(largest.find(a[i + 1] - a[i]));
        ans += a[i] - a[start_point];
        start_point = i + 1;
      }
    }
  }
  ans += a[m - 1] - a[start_point];
  cout << ans << '\n';
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  if (m == 1) {
    cout << 0 << '\n';
    return 0;
  }
  if (n == 1) {
    cout << a[m - 1] - a[0] << '\n';
    return 0;
  }
  vector<int> v;
  for (int i = 0; i < m - 1; i++) {
    v.push_back(a[i + 1] - a[i]);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  multiset<int> largest;
  for (int i = 0; i < min(n - 1, (int)v.size()); i++) {
    largest.insert(v[i]);
  }
  long long ans = 0;
  int start_point = 0;
  for (int i = 0; i < m - 1; i++) {
    if (!largest.empty()) {
      if (largest.find(a[i + 1] - a[i]) != largest.end()) {
        largest.erase(largest.find(a[i + 1] - a[i]));
        ans += a[i] - a[start_point];
        start_point = i + 1;
      }
    }
  }
  ans += a[m - 1] - a[start_point];
  cout << ans << '\n';
  return 0;
}
Problem p03137 (C++)

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

int main() {
  ll n, m;
  cin >> n >> m;
  ll a[n];
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  if (m <= n) {
    // cout<<"if"<<endl;
    cout << 0 << endl;
  } else {
    sort(a, a + m);
    vector<ll> v;
    for (int i = 1; i < m; i++) {
      v.push_back(abs(a[i] - a[i - 1]));
    }
    sort(v.begin(), v.end(), greater<ll>());

    for (int i = 0; i < n - 1; i++) {
      v[i] = 0;
    }
    /*for(int i=0;i<v.size();i++)
    {
    cout<<v[i]<<" ";
    }
    cout<<endl<<endl;;
    */
    ll sum = 0;
    for (int i = 0; i < v.size(); i++) {
      sum += v[i];
    }
    cout << sum << endl;
  }
}

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

int main() {
  ll n, m;
  cin >> n >> m;
  ll a[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  if (m <= n) {
    // cout<<"if"<<endl;
    cout << 0 << endl;
  } else {
    sort(a, a + m);
    vector<ll> v;
    for (int i = 1; i < m; i++) {
      v.push_back(abs(a[i] - a[i - 1]));
    }
    sort(v.begin(), v.end(), greater<ll>());

    for (int i = 0; i < n - 1; i++) {
      v[i] = 0;
    }
    /*for(int i=0;i<v.size();i++)
    {
    cout<<v[i]<<" ";
    }
    cout<<endl<<endl;;
    */
    ll sum = 0;
    for (int i = 0; i < v.size(); i++) {
      sum += v[i];
    }
    cout << sum << 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> A(M, 0);
  for (int i = 0; i < M; i++)
    cin >> A[i];

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

  vector<int> B(M - 1, 0);

  for (int i = 0; i < M - 1; i++) {
    B[i] = A[i + 1] - A[i];
  }

  sort(B.begin(), B.end(), greater<int>());
  int ans = A[M - 1] - A[0];
  for (int i = 0; i < N - 1; i++) {
    ans -= B[i];
  }
  cout << ans;
}


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

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;

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

  vector<int> A(M, 0);
  for (int i = 0; i < M; i++)
    cin >> A[i];

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

  vector<int> B(M - 1, 0);

  for (int i = 0; i < M - 1; i++) {
    B[i] = A[i + 1] - A[i];
  }

  sort(B.begin(), B.end(), greater<int>());
  int ans = A[M - 1] - A[0];
  for (int i = 0; i < N - 1; i++) {
    ans -= B[i];
  }
  cout << ans;
}
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> X, dis_X;
  for (int i = 0; i < M; i++) {
    int element;
    cin >> element;
    X.push_back(element);
  }
  if (M == 1) {
    cout << 0 << endl;
  } else if (N > M) {
    cout << 0 << endl;
  } else {
    sort(X.begin(), X.end());
    int first, last;
    first = X[0];
    last = X[X.size() - 1];
    int distance = last - first;
    for (int i = 1; i < M; i++) {
      int dis = X[i] - X[i - 1];
      dis_X.push_back(dis);
    }
    sort(dis_X.begin(), dis_X.end());
    int count = 0;
    for (int i = 0; i <= dis_X.size() - N; i++) {
      count += dis_X[i];
    }
    cout << count << endl;
  }
  return 0;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> X, dis_X;
  for (int i = 0; i < M; i++) {
    int element;
    cin >> element;
    X.push_back(element);
  }
  if (M == 1) {
    cout << 0 << endl;
  } else if (N >= M) {
    cout << 0 << endl;
  } else {
    sort(X.begin(), X.end());
    int first, last;
    first = X[0];
    last = X[X.size() - 1];
    int distance = last - first;
    for (int i = 1; i < M; i++) {
      int dis = X[i] - X[i - 1];
      dis_X.push_back(dis);
    }
    sort(dis_X.begin(), dis_X.end());
    int count = 0;
    for (int i = 0; i <= dis_X.size() - N; i++) {
      count += dis_X[i];
    }
    cout << count << endl;
  }
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int main() {
  int n, m;
  cin >> n >> m;

  vector<int> data(m);
  rep(i, 0, m) { cin >> data[i]; }
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(data.begin(), data.end());
  vector<int> distance(m - 1);
  rep(i, 0, m - 1) { distance[i] = data[i + 1] - data[i]; }
  sort(distance.begin(), distance.end());
  reverse(distance.begin(), distance.end());
  int total = data[m - 1] - data[0];
  rep(i, 0, n - 1) { total -= distance[i]; }
  cout << total << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int main() {
  int n, m;
  cin >> n >> m;

  vector<int> data(m);
  rep(i, 0, m) { cin >> data[i]; }
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(data.begin(), data.end());
  vector<int> distance(m - 1);
  rep(i, 0, m - 1) { distance[i] = data[i + 1] - data[i]; }
  sort(distance.begin(), distance.end());
  reverse(distance.begin(), distance.end());
  int total = data[m - 1] - data[0];
  rep(i, 0, n - 1) { total -= distance[i]; }
  cout << total << endl;

  return 0;
}
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;
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> a(m);
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> div(m - 1);
  for (int i = 0; i < m - 1; i++) {
    div[i] = a[i + 1] - a[i];
  }
  sort(div.begin(), div.end());
  ll ans = 0;
  for (int i = 0; i < div.size() - (n - 1); i++) {
    ans += div[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;
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> a(m);
  for (int i = 0; i < m; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> div(m - 1);
  for (int i = 0; i < m - 1; i++) {
    div[i] = a[i + 1] - a[i];
  }
  sort(div.begin(), div.end());
  ll ans = 0;
  for (int i = 0; i < max(0, (int)div.size() - (n - 1)); i++) {
    if (i < div.size())
      ans += div[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 PI 3.14159265358979323846
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

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

  int N, M;
  cin >> N >> M;
  int X[M];
  rep(i, M) cin >> X[i];

  sort(X, X + M);
  int x[M - 1];
  rep(i, M - 1) { x[i] = X[i + 1] - X[i]; }
  int ans = X[M - 1] - X[0];
  sort(x, x + M - 1);
  reverse(x, x + M - 1);
  rep(i, N - 1) { ans -= x[i]; }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

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

  int N, M;
  cin >> N >> M;
  int X[M];
  rep(i, M) cin >> X[i];

  int ans = 0;
  if (N >= M)
    ans = 0;
  else {
    sort(X, X + M);
    int x[M - 1];
    rep(i, M - 1) { x[i] = X[i + 1] - X[i]; }
    ans = X[M - 1] - X[0];
    sort(x, x + M - 1);
    reverse(x, x + M - 1);
    rep(i, N - 1) { ans -= x[i]; }
  }
  cout << ans << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i)
    cin >> v[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

lli n, m;
vector<lli> x;
int main() {
  cin >> n >> m;
  read_n(x, m);
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(begin(x), end(x));
  vector<lli> d(m - 1);
  for (lli i = 0; i < m; ++i) {
    d[i] = x[i + 1] - x[i];
  }
  sort(begin(d), end(d));
  lli ans = 0;
  for (lli i = 0; i < m - n; ++i)
    ans += d[i];
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using lli = long long int;
template <class T, class U> void init_n(vector<T> &v, size_t n, U x) {
  v = vector<T>(n, x);
}
template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); }
template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) {
  v = vector<T>(n + o);
  for (lli i = o; i < n + o; ++i)
    cin >> v[i];
}
template <class T> T gabs(const T &x) { return max(x, -x); }
#define abs gabs

lli n, m;
vector<lli> x;
int main() {
  cin >> n >> m;
  read_n(x, m);
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(begin(x), end(x));
  vector<lli> d(m - 1);
  for (lli i = 0; i < m - 1; ++i) {
    d[i] = x[i + 1] - x[i];
  }
  sort(begin(d), end(d));
  lli ans = 0;
  for (lli i = 0; i < m - n; ++i)
    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;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; ++i)
    cin >> x[i];

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

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

  int ans = 0;
  for (int i = 0; i < m - n; ++i)
    ans += diff[i];

  if (n < m)
    cout << ans << endl;
  else
    cout << 0 << 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());

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

  int ans = 0;
  for (int i = 0; i < m - n; ++i)
    ans += diff[i];

  if (n < m)
    cout << ans << endl;
  else
    cout << 0 << endl;
}
Problem p03137 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  if (M == 1) {
    cout << "0"
         << "\n";
    return 0;
  }
  vector<int> v(M);
  for (int i = 0; i < M; i++) {
    cin >> v.at(i);
  }
  sort(v.begin(), v.end());
  vector<int> sa(M - 1);
  for (int i = 1; i < M; i++) {
    sa.at(i - 1) = v.at(i) - v.at(i - 1);
  }
  sort(sa.begin(), sa.end());
  reverse(sa.begin(), sa.end());
  int soukyori = v.at(M - 1) - v.at(0);
  int yaranai = 0;
  for (int i = 0; i < N - 1; i++) {
    yaranai += sa.at(i);
  }
  cout << soukyori - yaranai << "\n";
}

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

int main() {
  int N, M;
  cin >> N >> M;
  if (M == 1 || N >= M) {
    cout << "0"
         << "\n";
    return 0;
  }
  vector<int> v(M);
  for (int i = 0; i < M; i++) {
    cin >> v.at(i);
  }
  sort(v.begin(), v.end());
  vector<int> sa(M - 1);
  for (int i = 1; i < M; i++) {
    sa.at(i - 1) = v.at(i) - v.at(i - 1);
  }
  sort(sa.begin(), sa.end());
  reverse(sa.begin(), sa.end());
  int soukyori = v.at(M - 1) - v.at(0);
  int yaranai = 0;
  for (int i = 0; i < N - 1; i++) {
    yaranai += sa.at(i);
  }
  cout << soukyori - yaranai << "\n";
}
Problem p03137 (C++)

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

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<int> x(m, 0), d(m - 1, 0);
  REP(i, m) cin >> x.at(i);
  sort(x.begin(), x.end());
  REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i);
  sort(d.begin(), d.end());
  cout << (m == 1 ? 0 : accumulate(d.begin(), d.begin() + m - n, 0));
}

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

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<int> x(m, 0), d(m - 1, 0);
  REP(i, m) cin >> x.at(i);
  sort(x.begin(), x.end());
  REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i);
  sort(d.begin(), d.end());
  cout << (m == 1 ? 0 : accumulate(d.begin(), d.begin() + m - (min(n, m)), 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 < (n); i++)

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  vector<int> x(m, 0), d(m - 1, 0);
  REP(i, m) cin >> x.at(i);
  sort(x.begin(), x.end());
  REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i);
  sort(d.begin(), d.end(), greater<int>());
  ans = accumulate(d.begin(), d.end(), 0);
  REP(i, n - 1) ans -= d.at(i);
  cout << ans;
}

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

int main() {
  int n, m, ans = 0;
  cin >> n >> m;
  if (m != 1) {
    vector<int> x(m, 0), d(m - 1, 0);
    REP(i, m) cin >> x.at(i);
    sort(x.begin(), x.end());
    REP(i, m - 1) d.at(i) = x.at(i + 1) - x.at(i);
    sort(d.begin(), d.end(), greater<int>());
    ans = accumulate(d.begin(), d.end(), 0);
    for (int i = 0; (i < n - 1) && (i < m - 1); i++)
      ans -= d.at(i);
  }
  cout << ans;
}
Problem p03137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long int n, m, x[100000], min, tmp, sum;
  cin >> n >> m;
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  cin >> x[0];
  for (int i = 1; i < m; i++) {
    cin >> x[i];
  }
  /*
  for(int i=0;i<m;i++){
      min = i;
      for(int k=i+1;k<m;k++){
          if(x[min]>x[k])min=k;
      }
      tmp = x[min];
      x[min] = x[i];
      x[i] = tmp;
  }
  */
  sort(x, x + m);
  sum = x[m - 1] - x[0];
  for (int i = 0; i < m - 1; i++) {
    x[i] = x[i + 1] - x[i];
  }
  for (int i = 0; i < m - 1; i++) {
    min = i;
    for (int k = i + 1; k < m - 1; k++) {
      if (x[min] > x[k])
        min = k;
    }
    tmp = x[min];
    x[min] = x[i];
    x[i] = tmp;
  }
  for (int i = 1; i <= n - 1; i++) {
    sum -= x[m - 1 - i];
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long int n, m, x[100000], min, tmp, sum;
  cin >> n >> m;
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  cin >> x[0];
  for (int i = 1; i < m; i++) {
    cin >> x[i];
  }
  /*
  for(int i=0;i<m;i++){
      min = i;
      for(int k=i+1;k<m;k++){
          if(x[min]>x[k])min=k;
      }
      tmp = x[min];
      x[min] = x[i];
      x[i] = tmp;
  }
  */
  sort(x, x + m);
  sum = x[m - 1] - x[0];
  for (int i = 0; i < m - 1; i++) {
    x[i] = x[i + 1] - x[i];
  }
  /*
  for(int i=0;i<m-1;i++){
      min = i;
      for(int k=i+1;k<m-1;k++){
          if(x[min]>x[k])min=k;
      }
      tmp = x[min];
      x[min] = x[i];
      x[i] = tmp;
  }*/
  sort(x, x + m - 1);
  for (int i = 1; i <= n - 1; i++) {
    sum -= x[m - 1 - i];
  }
  cout << sum << 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> l;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    l.push_back(a);
  }
  sort(l.begin(), l.end());
  vector<int> d_l;
  for (int i = 0; i < m - 1; i++) {
    d_l.push_back(l[i + 1] - l[i]);
  }
  sort(d_l.begin(), d_l.end());

  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += d_l[m - 2 - i];
  }
  int l_last = l[m - 1] - l[0];
  cout << l_last - sum << endl;
  return 0;
}

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

using namespace std;

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

  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += d_l[m - 2 - i];
  }
  int l_last = l[m - 1] - l[0];
  cout << l_last - sum << endl;
  return 0;
}