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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define mod ((ull)1e9 + 7)
#define MAX ((ull)1e9)

int main() {
  int n, m;
  cin >> n >> m;
  ll x[m], d[m - 1];
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }

  sort(x, x + m);
  for (int i = 0; i < m - 1; i++) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d, d + m - 1, greater<int>());
  ll ans = x[m - 1] - x[0];
  for (int i = 0; i < n - 1; i++) {
    ans -= d[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define mod ((ull)1e9 + 7)
#define MAX ((ull)1e9)

int main() {
  int n, m;
  cin >> n >> m;
  ll x[m], d[m - 1];
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  if (m == 1 || n >= m) {
    cout << 0 << endl;
    return 0;
  }

  sort(x, x + m);
  for (int i = 0; i < m - 1; i++) {
    d[i] = x[i + 1] - x[i];
  }
  sort(d, d + m - 1, greater<int>());
  ll ans = x[m - 1] - x[0];
  for (int i = 0; i < n - 1; i++) {
    ans -= d[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1 << 30;
const uint64_t MOD = 1000000000 + 7;

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

  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i);
  }

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

  vector<int> b(m);
  for (int i = 0; i < m - 1; i++) {
    b.at(i) = a.at(i + 1) - a.at(i);

    b.at(i) *= (b.at(i) < 0) ? -1 : 1;
  }

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

  cout << accumulate(b.begin(), b.end() - (n - 1), 0) << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

const int INF = 1 << 30;
const uint64_t MOD = 1000000000 + 7;

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

  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    cin >> a.at(i);
  }

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

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

  vector<int> b(m);
  for (int i = 0; i < m - 1; i++) {
    b.at(i) = a.at(i + 1) - a.at(i);

    b.at(i) *= (b.at(i) < 0) ? -1 : 1;
  }

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

  cout << accumulate(b.begin(), b.end() - (n - 1), 0) << endl;
}
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) {
    cout << "0";
    return 0;
  }
  vector<long long> x(m), a(m - 1);
  for (long long i = 0; i < m; i++) {
    cin >> x[i];
  }
  sort(x.begin(), x.end());

  for (long long i = m - 2; i >= 0; i--) {
    a[i] = x[i + 1] - x[i];
  }

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

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

  for (long long i = 0; i < n - 1; i++) {
    ans -= a[m - 2 - i];
  }
  cout << ans;
}

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

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

  for (long long i = m - 2; i >= 0; i--) {
    a[i] = x[i + 1] - x[i];
  }

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

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

  for (long long i = 0; i < n - 1; i++) {
    ans -= a[m - 2 - i];
  }
  cout << ans;
}
Problem p03137 (C++)

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

using namespace std;
typedef long long ll;

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

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

using namespace std;
typedef long long ll;

int main() {
  ll N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> x(M);
  vector<ll> l(M - 1);
  for (ll i = 0; i < M; i++) {
    cin >> x[i];
  }
  sort(x.begin(), x.end());
  for (ll i = 0; i < M - 1; i++) {
    l[i] = x[i + 1] - x[i];
  }
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());
  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    ans += l[i];
  }
  cout << x[M - 1] - x[0] - ans << endl;
  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
/* Accepted. */
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define MAXN 100006
int n, m;
int X[MAXN], d[MAXN], S;
int main() {
  // freopen( "input","r",stdin );
  cin >> m >> n;
  if (m <= n)
    return puts("0"), 0;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &X[i]);
  sort(X + 1, X + 1 + n);
  for (int i = 1; i <= n; ++i)
    d[i] = i != 1 ? X[i] - X[i - 1] : -0x3f3f3f3f;
  S = X[n] - X[1];
  sort(d + 1, d + 1 + n);
  for (int i = 0; i < m - 1; ++i)
    S -= d[n - i];
  cout << S;
} // qwq


Fixed submission:
/* Accepted. */
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define MAXN 100006
int n, m;
int X[MAXN], d[MAXN], S;
int main() {
  // freopen( "input","r",stdin );
  cin >> m >> n;
  if (n <= m)
    return puts("0"), 0;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &X[i]);
  sort(X + 1, X + 1 + n);
  for (int i = 1; i <= n; ++i)
    d[i] = i != 1 ? X[i] - X[i - 1] : -0x3f3f3f3f;
  S = X[n] - X[1];
  sort(d + 1, d + 1 + n);
  for (int i = 0; i < m - 1; ++i)
    S -= d[n - i];
  cout << S;
} // qwq
Problem p03137 (C++)

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

using namespace std;

long long X[10010];
long long subX[10010];
int main(void) {
  long long N, M;
  cin >> N >> M;
  for (long long i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X, X + M);

  for (long long i = 0; i < M - 1; i++) {
    subX[i] = X[i + 1] - X[i];
  }

  sort(subX, subX + M - 1);
  long long cost = 0;
  for (long long i = 0; i < M - 1 - (N - 1); i++) {
    cost += subX[i];
  }

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


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

using namespace std;

long long X[100100];
long long subX[100100];
int main(void) {
  long long N, M;
  cin >> N >> M;
  for (long long i = 0; i < M; i++) {
    cin >> X[i];
  }
  sort(X, X + M);

  for (long long i = 0; i < M - 1; i++) {
    subX[i] = X[i + 1] - X[i];
  }

  sort(subX, subX + M - 1);
  long long cost = 0;
  for (long long i = 0; i < M - 1 - (N - 1); i++) {
    cost += subX[i];
  }

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

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

int main() {
  int N, M;
  cin >> N >> M;
  int ans = 0;
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  sort(X.begin(), X.end());
  int length = X[M - 1] - X[0];
  vector<int> dif(M - 1);
  for (int i = 0; i < M; 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++) {
    length = length - dif[i];
  }
  cout << length << endl;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  int ans = 0;
  vector<int> X(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  sort(X.begin(), X.end());
  int length = X[M - 1] - X[0];
  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++) {
    length = length - dif[i];
  }
  cout << length << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

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

  vector<ll> A(M);
  vector<ll> gap(M - 1);

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

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

  ans = A[M - 1] - A[0];

  rep(i, M - 1) { gap[i] = A[i + 1] - A[i]; }

  sort(gap.begin(), gap.end(), greater<>());

  rep(i, N - 1) { ans -= gap[i]; }

  if (M == 1) {
    ans = 0;
  }

  cout << ans;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

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

  vector<ll> A(M);
  vector<ll> gap(M - 1);

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

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

  ans = A[M - 1] - A[0];

  rep(i, M - 1) { gap[i] = A[i + 1] - A[i]; }

  sort(gap.begin(), gap.end(), greater<>());

  rep(i, min(N - 1, M - 1)) { ans -= gap[i]; }

  cout << ans;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define fort(i, n) for (int i = 1; i <= n; ++i)
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz size()
#define er erase
#define fr first
#define sc second
#define rc(x) return cout << x, 0
using namespace std;

ll n, m, x[100005], a[100005], s;

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> n >> m;
  fort(i, m) cin >> x[i];
  sort(x + 1, x + m + 1);
  fort(i, m - 1) a[i] = x[i + 1] - x[i], s += a[i];
  sort(a + 1, a + m);
  fort(i, n - 1) s -= a[m - i];
  cout << max(s, 0ll);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define fort(i, n) for (int i = 1; i <= n; ++i)
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz size()
#define er erase
#define fr first
#define sc second
#define rc(x) return cout << x, 0
using namespace std;

ll n, m, x[100005], a[100005], s;

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> n >> m;
  if (n >= m)
    rc(0);
  fort(i, m) cin >> x[i];
  sort(x + 1, x + m + 1);
  fort(i, m - 1) a[i] = x[i + 1] - x[i], s += a[i];
  sort(a + 1, a + m);
  fort(i, n - 1) s -= a[m - i];
  cout << max(s, 0ll);
}
Problem p03137 (C++)

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

int main() {
  long long int n, m;
  vector<long long int> m_v;

  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    long long int mi;
    cin >> mi;
    m_v.push_back(mi);
  }

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

  vector<long long int> diffs;
  for (int i = 0; i < m - 1; i++) {
    diffs.push_back(abs(m_v[i] - m_v[i + 1]));
  }

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

  int ans = 0;
  for (int i = 0; i < diffs.size() - n + 1; i++) {
    ans += diffs[i];
  }
  cout << ans << endl;
}

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

int main() {
  long long int n, m;
  vector<long long int> m_v;

  cin >> n >> m;

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

  for (int i = 0; i < m; i++) {
    long long int mi;
    cin >> mi;
    m_v.push_back(mi);
  }

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

  vector<long long int> diffs;
  for (int i = 0; i < m - 1; i++) {
    diffs.push_back(abs(m_v[i] - m_v[i + 1]));
  }

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

  int ans = 0;
  for (int i = 0; i < diffs.size() - n + 1; i++) {
    ans += diffs[i];
  }
  cout << ans << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdio.h>
#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];

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

  vector<int> defs(m - 1);
  for (int i = 0; i < m - 1; i++)
    defs[i] = x[i + 1] - x[i];
  int res = x[m - 1] - x[0];

  sort(defs.begin(), defs.end(), greater<>());
  for (int i = 0; i < n - 1; i++)
    res -= defs[i];
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdio.h>
#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 (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(x.begin(), x.end());

  vector<int> defs(m - 1);
  for (int i = 0; i < m - 1; i++)
    defs[i] = x[i + 1] - x[i];
  int res = x[m - 1] - x[0];

  sort(defs.begin(), defs.end(), greater<>());
  for (int i = 0; i < n - 1; i++)
    res -= defs[i];
  cout << res << endl;
  return 0;
}
Problem p03137 (C++)

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

int main() {
  int N, M;
  int d[10000];
  int dis[10000];
  int counter = 0;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> d[i];
  }

  sort(d, d + M);

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

  sort(dis, dis + M - 1, greater<int>());

  for (int i = N - 1; i < M - 1; i++) {
    counter += dis[i];
  }

  cout << counter << endl;
}

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

int main() {
  int N, M;
  int d[110000];
  int dis[110000];
  int counter = 0;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> d[i];
  }

  sort(d, d + M);

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

  sort(dis, dis + M - 1, greater<int>());

  for (int i = N - 1; i < M - 1; i++) {
    counter += dis[i];
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  vector<int> xbet(m - 1);

  rep(i, m) cin >> x[i];
  sort(all(x));

  rep(i, m - 1) xbet[i] = x[i + 1] - x[i];
  sort(all(xbet));
  reverse(all(xbet));

  rep(i, n - 1) xbet[i] = 0;

  ll ans = 0;

  rep(i, m - 1) ans += xbet[i];

  dunk(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

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

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

  vector<int> x(m);
  vector<int> xbet(m - 1);

  rep(i, m) cin >> x[i];
  sort(all(x));

  rep(i, m - 1) xbet[i] = x[i + 1] - x[i];
  sort(all(xbet));
  reverse(all(xbet));

  rep(i, n - 1) xbet[i] = 0;

  ll ans = 0;

  rep(i, m - 1) ans += xbet[i];

  dunk(ans);
}
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;
  } else {
    vector<int> X(M);
    for (int i = 0; i < M; i++) {
      cin >> X.at(i);
    }
    sort(X.begin(), X.end());
    int a = X.at(M - 1) - X.at(0);
    vector<int> Y(M - 1);
    Y.at(0) = 0;
    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());
    int b = 0;
    for (int i = 0; i < N - 1; i++) {
      b = b + Y.at(i);
    }
    cout << a - b << endl;
  }
}


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

int main() {
  int N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << "0" << endl;
  } else {
    vector<int> X(M);
    for (int i = 0; i < M; i++) {
      cin >> X.at(i);
    }
    sort(X.begin(), X.end());
    int a = X.at(M - 1) - X.at(0);
    vector<int> Y(M - 1);
    Y.at(0) = 0;
    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());
    int b = 0;
    for (int i = 0; i < N - 1; i++) {
      b = b + Y.at(i);
    }
    cout << a - b << endl;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<int> yop, yoc;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    yop.push_back(x);
  }
  sort(yop.begin(), yop.end());
  int ans = yop[m - 1] - yop[0];
  for (int i = 0; i < m - 1; i++)
    yoc.push_back(yop[i + 1] - yop[i]);
  sort(yoc.begin(), yoc.end(), greater<int>());
  for (int i = 0; i < n - 1; i++)
    ans -= yoc[i];
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<int> yop, yoc;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    yop.push_back(x);
  }
  sort(yop.begin(), yop.end());
  int ans = yop[m - 1] - yop[0];
  for (int i = 0; i < m - 1; i++)
    yoc.push_back(yop[i + 1] - yop[i]);
  sort(yoc.begin(), yoc.end(), greater<int>());
  for (int i = 0; i < n - 1 && i < yoc.size(); i++)
    ans -= yoc[i];
  cout << ans << endl;
}
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++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;
using Graph = vector<vector<ll>>;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> X(m);
  rep(i, m) cin >> X[i];

  sort(X.begin(), X.end());
  ll moto = X[m - 1] - X[0];
  vector<ll> sa(m - 1);
  rep(i, m - 1) sa[i] = X[i + 1] - X[i];
  ll hiku = 0;
  sort(sa.begin(), sa.end(), greater<ll>());
  rep(i, n - 1) hiku += sa[i];

  cout << moto - hiku << endl;
}

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++)
#define agewari(a, b) ((ll)a + ((ll)b - 1)) / b
const int MOD = 1000000007;
const long long INF = 1LL << 60;
using Graph = vector<vector<ll>>;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> X(m);
  rep(i, m) cin >> X[i];

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

  sort(X.begin(), X.end());
  ll moto = X[m - 1] - X[0];
  vector<ll> sa(m - 1);
  rep(i, m - 1) sa[i] = X[i + 1] - X[i];
  ll hiku = 0;
  sort(sa.begin(), sa.end(), greater<ll>());
  rep(i, n - 1) hiku += sa[i];

  cout << moto - hiku << endl;
}
Problem p03137 (C++)

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

using namespace std;
typedef unsigned int uint;
typedef long long ll;
// typedef pair<int, int> P;
const int INF = 111111111;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const int DIV = 1000000007;                       // 10^9 + 7
const double PI = 3.14159265358979323846264338327950288;

int main() {

  //==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  //==================================

  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());

  if (M != 1) {
    vector<int> aida(M - 1);
    for (int i = 0; i < M - 1; i++) {
      aida[i] = X[i + 1] - X[i];
    }

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

    ll none = 0;
    for (int i = 0; i < N - 1; i++) {
      none += aida[i];
    }

    cout << X[M - 1] - X[0] - none;
  } else {
    cout << 0;
  }
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef unsigned int uint;
typedef long long ll;
// typedef pair<int, int> P;
const int INF = 111111111;
const ll LLINF = 1e17 + 1;
const int DX[9] = {0, 0, 1, -1, 1, 1, -1, -1, 0}; // 4;４近傍
const int DY[9] = {1, -1, 0, 0, 1, -1, 1, -1, 0}; // 8:８近傍 9:(0,0)を含む
const int DIV = 1000000007;                       // 10^9 + 7
const double PI = 3.14159265358979323846264338327950288;

int main() {

  //==================================
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  //==================================

  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());

  if (M != 1 && N < M) {
    vector<int> aida(M - 1);
    for (int i = 0; i < M - 1; i++) {
      aida[i] = X[i + 1] - X[i];
    }

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

    ll none = 0;
    for (int i = 0; i < N - 1; i++) {
      none += aida[i];
    }

    cout << X[M - 1] - X[0] - none;
  } else {
    cout << 0;
  }
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, x[N], ans;
priority_queue<int> q;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i)
    scanf("%d", &x[i]);
  sort(x + 1, x + m + 1);
  ans = x[m] - x[1];
  for (int i = 1; i < m; ++i)
    q.push(x[i + 1] - x[i]);
  for (int i = 1; i <= n - 1; ++i) {
    ans -= q.top();
    q.pop();
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, x[N], ans;
priority_queue<int> q;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i)
    scanf("%d", &x[i]);
  sort(x + 1, x + m + 1);
  ans = x[m] - x[1];
  for (int i = 1; i < m; ++i)
    q.push(x[i + 1] - x[i]);
  for (int i = 1; i <= n - 1; ++i) {
    if (q.empty())
      break;
    ans -= q.top();
    q.pop();
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03137 (C++)

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

int main() {
  long long n, m;

  cin >> n >> m;

  vector<long long> v(m);

  for (long long i = 0; i < m; i++) {
    cin >> v[i];
  }

  if (m == 1) {
    printf("0\n");
    return 0;
  }

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

  /*for(int i = 0; i < m; i++){
      printf("v[i] %d\n", v[i]);
  }*/

  long long sum = v[m - 1] - v[0];

  if (n == 1) {
    printf("%lld\n", sum);
    return 0;
  }

  // printf("sum %d\n", sum);

  vector<long long> distance(m - 1);

  for (long long i = 0; i < m - 1; i++) {
    distance[i] = v[i + 1] - v[i];
    // printf("distance[i] %d\n", distance[i]);
  }

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

  /*for(int i = 0; i < m-1; i++){
      //printf("sort distance[i] %d\n", distance[i]);
  }*/

  long long avoid = 0;
  for (long long i = 0; i < n - 1; i++) {
    avoid += distance[i];
    // printf("avoid %d\n", avoid);
  }

  printf("%lld\n", sum - avoid);

  return 0;
}

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

int main() {
  long long n, m;

  cin >> n >> m;

  vector<long long> v(m);

  for (long long i = 0; i < m; i++) {
    cin >> v[i];
  }

  if (m == 1) {
    printf("0\n");
    return 0;
  }

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

  /*for(int i = 0; i < m; i++){
      printf("v[i] %d\n", v[i]);
  }*/

  long long sum = v[m - 1] - v[0];

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

  // printf("sum %d\n", sum);

  vector<long long> distance(m - 1);

  for (long long i = 0; i < m - 1; i++) {
    distance[i] = v[i + 1] - v[i];
    // printf("distance[i] %d\n", distance[i]);
  }

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

  /*for(int i = 0; i < m-1; i++){
      //printf("sort distance[i] %d\n", distance[i]);
  }*/

  long long avoid = 0;
  for (long long i = 0; i < n - 1; i++) {
    avoid += distance[i];
    // printf("avoid %d\n", avoid);
  }

  printf("%lld\n", sum - avoid);

  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> x(m), len(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++) {
    len[i] = abs(x[i] - x[i + 1]);
    // cout << "len:" << len[i] << endl;
  }
  sort(len.begin(), len.end());

  cout << accumulate(len.begin(), len.end() - (n - 1), 0) << endl;

  return 0;
}

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

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

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

  if (n > m) {
    n = m;
  }

  sort(x.begin(), x.end());
  for (int i = 0; i < m - 1; i++) {
    len[i] = abs(x[i] - x[i + 1]);
    // cout << "len:" << len[i] << endl;
  }
  sort(len.begin(), len.end());

  cout << accumulate(len.begin(), len.end() - (n - 1), 0) << endl;

  return 0;
}
Problem p03137 (C++)

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

int main(int argc, char const *argv[]) {
  int N, M, len = 0;
  scanf("%d %d", &N, &M);

  int dest[M], space[M - 1];
  for (int i = 0; i < M; ++i)
    scanf("%d", &(dest[i]));
  std::sort(dest, dest + M);

  for (int i = 0; i < M - 1; ++i)
    space[i] = dest[i + 1] - dest[i];
  std::sort(space, space + M - 1);
  std::reverse(space, space + M - 1);

  for (int i = 0; i < N - 1; ++i)
    len += space[i];
  printf("%d\n", dest[M - 1] - dest[0] - len);

  return 0;
}


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

int main(int argc, char const *argv[]) {
  int N, M, len = 0;
  scanf("%d %d", &N, &M);
  if (N > M) {
    printf("0\n");
    exit(0);
  }

  int dest[M], space[M - 1];
  for (int i = 0; i < M; ++i)
    scanf("%d", &(dest[i]));
  std::sort(dest, dest + M);

  for (int i = 0; i < M - 1; ++i)
    space[i] = dest[i + 1] - dest[i];
  std::sort(space, space + M - 1);
  std::reverse(space, space + M - 1);

  for (int i = 0; i < N - 1; ++i)
    len += space[i];
  printf("%d\n", dest[M - 1] - dest[0] - len);

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll INF = 1LL << 60;

// n個のコマの担当範囲をどう区切るかの問題に帰着できる
// 範囲を決めることは、その右端から次の担当コマまでの区間をカウントしなくてよくすることと同義
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x{m};
  rep(i, m) cin >> x[i];

  int ans = 0;
  vector<int> d(m - 1);
  sort(all(x));
  rep(i, m - 1) d[i] = x[i + 1] - x[i];
  sort(all(d));
  // 距離dが大きい上位n-1個のdはカウントしなくて良い
  rep(i, m - n) ans += d[i];

  cout << ans << endl;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll INF = 1LL << 60;

// n個のコマの担当範囲をどう区切るかの問題に帰着できる
// 範囲を決めることは、その右端から次の担当コマまでの区間をカウントしなくてよくすることと同義
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];

  int ans = 0;
  vector<int> d(m - 1);
  sort(all(x));
  rep(i, m - 1) d[i] = x[i + 1] - x[i];
  sort(all(d));
  // 距離dが大きい上位n-1個のdはカウントしなくて良い
  rep(i, m - n) ans += d[i];

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

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <type_traits>
#include <vector>

using namespace std;

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


Fixed submission:


#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <type_traits>
#include <vector>

using namespace std;

int main(int argc, const char *argv[]) {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  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());
  int ans = 0;
  for (int i = 0; i < d.size() - n + 1; i++) {
    ans += d[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03137 (C++)

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

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

#define FOR(i, start, end)                                                     \
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)

#define ALL(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \
  })(c))

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> d;
  FOR(i, 0, m) {
    int x;
    cin >> x;
    d.push_back(x);
  }
  std::sort(d.begin(), d.end());
  vector<int> dn;
  FOR(i, 0, m - 1) dn.push_back(d[i + 1] - d[i]);
  std::sort(dn.begin(), dn.end());
  int ans = 0;
  if (m != 1) {
    FOR(i, 0, n + 1) ans = ans + dn[i];
  }
  cout << ans << endl;
}

signed main() {
  solve();
  return 0;
}

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

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

#define FOR(i, start, end)                                                     \
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)

#define ALL(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \
  })(c))

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> d;
  FOR(i, 0, m) {
    int x;
    cin >> x;
    d.push_back(x);
  }
  std::sort(d.begin(), d.end());
  vector<int> dn;
  FOR(i, 0, m - 1) dn.push_back(d[i + 1] - d[i]);
  std::sort(dn.begin(), dn.end());
  int ans = 0;
  FOR(i, 0, m - n) ans += dn[i];
  cout << ans << endl;
}

signed main() {
  solve();
  return 0;
}
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(m), d(m);

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

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

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

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

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

  cout << ans << endl;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m), d(m);

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

  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(x.begin(), x.end());

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

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

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

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

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

#define all(a) (a).begin(), (a).end()
#define pb push_back

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;

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

  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(x, x + m);

  int total = 0;
  for (int i = 1; i < m; ++i) {
    total += abs(x[i] - x[i - 1]);
    d.push_back(abs(x[i] - x[i - 1]));
  }

  sort(all(d), greater<int>());
  for (int i = 0; i < n - 1; ++i) {
    total -= d[i];
  }

  cout << total << endl;
}

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

#define all(a) (a).begin(), (a).end()
#define pb push_back

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;

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

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

  int total = 0;
  for (int i = 1; i < m; ++i) {
    total += abs(x[i] - x[i - 1]);
    d.push_back(abs(x[i] - x[i - 1]));
  }

  sort(all(d), greater<int>());
  for (int i = 0; i < n - 1; ++i) {
    total -= d[i];
  }

  cout << total << 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;

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

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

  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());

  int ans = 0;
  for (int i = 0; i < d.size() - (n - 1); i++) {
    ans += d[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> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }

  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());

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

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

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

int main() {

  int N, M;
  std::cin >> N >> M;

  std::vector<int> x(M);
  for (auto i = x.begin(); i != x.end(); i++) {
    std::cin >> *i;
  }

  if (N >= M) {
    std::cout << 0 << std::endl;
  } else {
    std::sort(x.begin(), x.end());
    int sum = *(x.end() - 1) - *(x.begin());
    for (auto i = x.begin(); i != x.end(); i++) {
      if (i != x.end() - 1) {
        *i = *(i + 1) - *i;
      } else {
        *i = 0;
      }
    }
    std::sort(x.begin(), x.end());
    int minus = 0;
    for (auto i = x.crbegin(); x.crbegin() - i != N - 1; i++) {
      minus += *i;
    }
    std::cout << sum - minus << std::endl;
  }
  return 0;
}


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

int main() {

  int N, M;
  std::cin >> N >> M;

  std::vector<int> x(M);
  for (auto i = x.begin(); i != x.end(); i++) {
    std::cin >> *i;
  }

  if (N >= M) {
    std::cout << 0 << std::endl;
  } else {
    std::sort(x.begin(), x.end());
    int sum = *(x.end() - 1) - *(x.begin());
    for (auto i = x.begin(); i != x.end(); i++) {
      if (i != x.end() - 1) {
        *i = *(i + 1) - *i;
      } else {
        *i = 0;
      }
    }
    std::sort(x.begin(), x.end());
    int minus = 0;
    for (auto i = x.crbegin(); i - x.crbegin() != N - 1; i++) {
      minus += *i;
    }
    std::cout << sum - minus << std::endl;
  }
  return 0;
}
Problem p03137 (C++)

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

using namespace std;

#define all(x) begin(x), end(x)

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

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

  int n, m;
  cin >> n >> m;
  vi a(m);
  for (int i = 0; i < m; ++i)
    cin >> a[i];

  sort(all(a));
  a.erase(unique(all(a)), end(a));

  // can skip n - 1 gaps
  vi b;
  for (int i = 1; i < a.size(); ++i) {
    b.push_back(a[i] - a[i - 1]);
  }
  sort(all(b));
  for (int i = 0; i < n - 1; ++i)
    b.pop_back();
  ll ans = 0LL;
  for (int x : b)
    ans += x;

  cout << ans << '\n';

  return 0;
}


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

using namespace std;

#define all(x) begin(x), end(x)

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

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

  int n, m;
  cin >> n >> m;
  vi a(m);
  for (int i = 0; i < m; ++i)
    cin >> a[i];

  sort(all(a));
  a.erase(unique(all(a)), end(a));

  // can skip n - 1 gaps
  vi b;
  for (int i = 1; i < a.size(); ++i) {
    b.push_back(a[i] - a[i - 1]);
  }
  sort(all(b));
  for (int i = 0; !b.empty() and i < n - 1; ++i)
    b.pop_back();
  ll ans = 0LL;
  for (int x : b)
    ans += x;

  cout << ans << '\n';

  return 0;
}
Problem p03137 (C++)

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

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

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

using namespace std;
int a[123456], b[123456];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m);
  for (int i = 1; i < m; i++) {
    b[i - 1] = a[i] - a[i - 1];
  }
  sort(b, b + m - 1);
  cout << accumulate(b, max(b + m - n, b + 0), 0LL) << 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 argc, char const *argv[]) {
  int N, M;
  cin >> N >> M;
  vector<int> X;
  X.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }

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

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

  vector<int> d;
  d.resize(M - 1);
  for (int i = 0; i < M; i++) {
    d[i] = X[i + 1] - X[i];
  }

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

  int sum = accumulate(d.begin(), d.end() - (N - 1), 0);

  cout << sum << endl;

  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;
  X.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }

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

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

  vector<int> d;
  d.resize(M - 1);
  for (int i = 0; i < M - 1; i++) {
    d[i] = X[i + 1] - X[i];
  }

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

  int sum = accumulate(d.begin(), d.end() - (N - 1), 0);

  cout << sum << endl;

  return 0;
}
Problem p03137 (C++)

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

int main() {
  int n;
  int m;
  int zahyo[MAX];
  long long int ans = 0;
  bool ff = true;
  cin >> n >> m;

  if (n > m) {
    ff = false;
  }

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

  sort(zahyo, zahyo + m);

  int diff[MAX];
  for (int j = 0; j < m - 1; ++j) {
    diff[j] = zahyo[j + 1] - zahyo[j];
  }

  sort(diff, diff + m - 1);

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

  if (ff) {
    cout << ans;
  } else {
    cout << 0;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define MAX 100001
using namespace std;

int main() {
  int n;
  int m;
  int zahyo[MAX];
  long long int ans = 0;
  bool ff = true;
  cin >> n >> m;

  if (n > m) {
    ff = false;
  }

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

  sort(zahyo, zahyo + m);

  int diff[MAX];
  for (int j = 0; j < m - 1; ++j) {
    diff[j] = zahyo[j + 1] - zahyo[j];
  }

  sort(diff, diff + m - 1);

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

  if (ff) {
    cout << ans;
  } else {
    cout << 0;
  }
  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;
typedef long long ll;

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  vector<int> diff(m - 1);
  rep(i, m) { cin >> x[i]; }
  if (m == 1) {
    puts("0");
    return 0;
  }
  sort(x.begin(), x.end());
  ll ans = 0LL;
  rep(i, m - 1) {
    diff[i] = x[i + 1] - x[i];
    ans += diff[i];
  }

  sort(diff.begin(), diff.end(), greater<int>());
  rep(i, n - 1) { ans -= diff[i]; }
  cout << ans << endl;
  return 0;
}

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

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  vector<int> diff(m - 1);
  rep(i, m) { cin >> x[i]; }
  if (m <= n) {
    puts("0");
    return 0;
  }
  sort(x.begin(), x.end());
  ll ans = 0LL;
  rep(i, m - 1) {
    diff[i] = x[i + 1] - x[i];
    ans += diff[i];
  }

  sort(diff.begin(), diff.end(), greater<int>());
  rep(i, n - 1) { ans -= diff[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 (ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main() {
  ll n, m;
  in2(n, m);
  if (m == 1) {
    out(0);
    memi;
    return 0;
  }
  vector<ll> a(m), b(m - 1);
  rep(i, m) in(a[i]);
  sort(all(a));
  rep(i, m - 1) b[i] = a[i + 1] - a[i];
  sort(all(b));
  rep(i, n - 1) b[m - i - 2] = 0;
  n = 0;
  rep(i, b.size()) n += b[i];
  out(n);
  memi;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main() {
  ll n, m;
  in2(n, m);
  if (m == 1) {
    out(0);
    memi;
    return 0;
  }
  vector<ll> a(m), b(m - 1);
  rep(i, m) in(a[i]);
  sort(all(a));
  rep(i, m - 1) b[i] = a[i + 1] - a[i];
  sort(all(b));
  rep(i, n - 1) b[max(0ll, m - i - 2)] = 0;
  n = 0;
  rep(i, b.size()) n += b[i];
  out(n);
  memi;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
  long long n, m;
  cin >> n >> m;
  long long x[m];
  rep(i, m) cin >> x[i];
  if (n >= m) {
    cout << 0 << endl;
  }
  sort(x, x + m);
  vector<long long> diff;
  long long ans = 0;
  for (int i = 1; i < m; i++) {
    diff.push_back(x[i] - x[i - 1]);
    ans += x[i] - x[i - 1];
  }
  sort(all(diff));
  reverse(all(diff));
  for (int i = 0; i < n - 1; i++) {
    ans -= diff[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
inline long long mod(long long n, long long m) { return (n % m + m) % m; }
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main() {
  long long n, m;
  cin >> n >> m;
  long long x[m];
  rep(i, m) cin >> x[i];
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  sort(x, x + m);
  vector<long long> diff;
  long long ans = 0;
  for (int i = 1; i < m; i++) {
    diff.push_back(x[i] - x[i - 1]);
    ans += x[i] - x[i - 1];
  }
  sort(all(diff));
  reverse(all(diff));
  for (int i = 0; i < n - 1; i++) {
    ans -= diff[i];
  }
  cout << ans << endl;
  return 0;
}