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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (long long i = a; i < b; i += 1)
#define repr(i, a, b) for (long long i = a; i <= b; i += 1)
#define vec vector<ll>
#define map map<string, int>
#define repa(p, A) for (auto p : A)
#define pb push_back
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
const double PI = acos(-1);

using namespace std;
int main() {
  ll N, K;
  cin >> K >> N;
  vec A(N), B(N);
  rep(i, 0, N) { cin >> A[i]; }
  sort(A);
  rep(i, 0, N - 1) { B[i] = A[i + 1] - A[i]; }
  sort(B);
  reverse(B);
  ll count = 0;
  rep(i, 0, K - 1) {
    count += B[i];
    // cout<<B[i]<<endl;
  }
  ll a = A[N - 1] - A[0];

  a -= count;
  cout << a << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (long long i = a; i < b; i += 1)
#define repr(i, a, b) for (long long i = a; i <= b; i += 1)
#define vec vector<ll>
#define map map<string, int>
#define repa(p, A) for (auto p : A)
#define pb push_back
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
const double PI = acos(-1);

using namespace std;
int main() {
  ll N, K;
  cin >> K >> N;
  vec A(N), B(N);
  rep(i, 0, N) { cin >> A[i]; }
  sort(A);
  rep(i, 0, N - 1) { B[i] = A[i + 1] - A[i]; }
  sort(B);
  reverse(B);
  ll count = 0;

  rep(i, 0, min(K - 1, N - 1)) {
    count += B[i];
    // cout<<B[i]<<endl;
  }
  ll a = A[N - 1] - A[0];

  a -= count;
  cout << a << endl;
}
Problem p03137 (C++)

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

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> X(M);

  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  if (N >= M) {
    cout << 0 << endl;
    return 1;
  }

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

  int sum_difference = 0;
  vector<int> difference(M - 1);
  for (int i = 0; i < M - 1; i++) {
    difference[i] = abs(X[i + 1] - X[i]);
    sum_difference += difference[i];
  }
  sort(difference.begin(), difference.end());

  // for (int i = 0; i < M - 1; i++) {
  //	cout << i << ":" << difference[i] << endl;
  // }

  int answer = sum_difference;
  for (int i = 0; i < N - 1; i++) {
    answer -= difference[(M - 1) - 1 - i];
  }
  cout << answer << endl;

  return 0;
}

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

int main() {

  int N, M;
  cin >> N >> M;
  vector<int> X(M);

  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }

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

  int sum_difference = 0;
  vector<int> difference(M - 1);
  for (int i = 0; i < M - 1; i++) {
    difference[i] = abs(X[i + 1] - X[i]);
    sum_difference += difference[i];
  }
  sort(difference.begin(), difference.end());

  // for (int i = 0; i < M - 1; i++) {
  //	cout << i << ":" << difference[i] << endl;
  // }

  int answer = sum_difference;
  for (int i = 0; i < N - 1; i++) {
    answer -= difference[(M - 1) - 1 - i];
  }
  cout << answer << endl;

  return 0;
}
Problem p03137 (C++)

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

using namespace std;

typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, start, end) for (int i = start; i < end; i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << (x) << endl;
#define INF (1e9)
#define add_mod(a, b, x) ((ll)(a) + (ll)(b)) % (ll)(x)
#define mul_mod(a, b, x) (((ll)(a) % (ll)(x)) * ((ll)(b) % (ll)(x))) % (ll)(x)

ll sub_mod(ll a, ll b, ll x) {
  ll tmp = (a - b) % x;
  if (tmp < 0)
    tmp += x;
  return tmp;
}

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

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

  int n, m;
  cin >> n >> m;

  vector<int> nums(m), dist(m);
  rep(i, m) { cin >> nums[i]; }

  sort(all(nums));
  rep(i, m) {
    if (i != 0)
      dist[i] = nums[i] - nums[i - 1];
  }
  sort(all(dist));
  rep(i, n - 1) { dist[m - 1 - i] = 0; }

  cout << accumulate(all(dist), 0) << endl;
}

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

using namespace std;

typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define repi(i, start, end) for (int i = start; i < end; i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << (x) << endl;
#define INF (1e9)
#define add_mod(a, b, x) ((ll)(a) + (ll)(b)) % (ll)(x)
#define mul_mod(a, b, x) (((ll)(a) % (ll)(x)) * ((ll)(b) % (ll)(x))) % (ll)(x)

ll sub_mod(ll a, ll b, ll x) {
  ll tmp = (a - b) % x;
  if (tmp < 0)
    tmp += x;
  return tmp;
}

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

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

  int n, m;
  cin >> n >> m;

  vector<int> nums(m), dist(m);
  rep(i, m) { cin >> nums[i]; }

  sort(all(nums));
  rep(i, m) {
    if (i != 0)
      dist[i] = nums[i] - nums[i - 1];
  }
  sort(all(dist));
  rep(i, n - 1) {
    if (m - 1 - i > 0)
      dist[m - 1 - i] = 0;
  }

  cout << accumulate(all(dist), 0) << endl;
}
Problem p03137 (Python)

Buggy submission (status: Runtime Error):
import sys

n, m = map(int, input().split())
x = list(map(int, input().split()))
if m == 1:
    print(0)
    sys.exit()
if n == 1:
    print(max(x) - min(x))
    sys.exit()
x.sort()
a = []
for i in range(m - 1):
    a.append(abs(x[i] - x[i + 1]))
ans = sum(a)
a.sort(reverse=True)
for i in range(n - 1):
    ans -= a[i]
print(max(0, ans))


Fixed submission:
import sys

n, m = map(int, input().split())
x = list(map(int, input().split()))
if m == 1:
    print(0)
    sys.exit()
if n == 1:
    print(max(x) - min(x))
    sys.exit()
if n >= m:
    print(0)
    sys.exit()
x.sort()
a = []
for i in range(m - 1):
    a.append(abs(x[i] - x[i + 1]))
ans = sum(a)
a.sort(reverse=True)
for i in range(n - 1):
    ans -= a[i]
print(max(0, ans))
Problem p03137 (Python)

Buggy submission (status: Runtime Error):
def main():
    N, M = map(int, input().split())
    X = sorted(list(map(int, input().split())))

    if M == 1:
        print(0)
    elif N == 1:
        print(max(X) - min(X))
    else:
        dx = [abs(X[m] - X[m + 1]) for m in range(M - 1)]
        for _ in range(N - 1):
            dx.pop(dx.index(max(dx)))
        print(sum(dx))


main()


Fixed submission:
def main():
    N, M = map(int, input().split())
    X = sorted(list(map(int, input().split())))

    if M == 1:
        print(0)
    elif N == 1:
        print(max(X) - min(X))
    else:
        if N > M:
            N = M
        dx = sorted([abs(X[m] - X[m + 1]) for m in range(M - 1)])
        print(sum(dx[: M - N]))


main()
Problem p03137 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = map(int, input().split())
X = list(map(int, input().split()))

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [X[i + 1] - X[i] for i in range(len(X) - 1)]
    for _ in range(N - 1):
        x_diff.remove(max(x_diff))
    ans = sum(x_diff)

print(ans)


Fixed submission:
N, M = map(int, input().split())
X = list(map(int, input().split()))

X.sort()
if N >= M:
    ans = 0
else:
    x_diff = [
        xn - x for xn, x in zip(X[1:], X[:-1])
    ]  # [X[i + 1] - X[i] for i in range(len(X) - 1)]
    # for _ in range(N - 1):
    #     x_diff.remove(max(x_diff))
    x_diff.sort(reverse=True)
    ans = sum(x_diff[N - 1 :])

print(ans)
Problem p03137 (Python)

Buggy submission (status: Runtime Error):
def solve():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    x.sort()
    diff = [x[i + 1] - x[i] for i in range(m)]
    diff.sort(reverse=True)
    return sum(diff[n - 1 :])


if __name__ == "__main__":
    print(solve())


Fixed submission:
def solve():
    n, m = map(int, input().split())
    x = sorted(list(map(int, input().split())))
    diff = sorted([x[i + 1] - x[i] for i in range(m - 1)], reverse=True)
    return sum(diff[n - 1 :])


if __name__ == "__main__":
    print(solve())
Problem p03137 (Python)

Buggy submission (status: Runtime Error):
N, M = map(int, input().split())
x = list(map(int, input().split()))

x.sort()
s = [0] * (M - 1)
for i in range(M - 1):
    s[i] = x[i + 1] - x[i]

s.sort(reverse=True)

res = sum(s)
if M == 1:
    print(0)
    quit()
else:
    for i in range(N - 1):
        res -= s[i]

print(res)


Fixed submission:
N, M = map(int, input().split())
x = list(map(int, input().split()))
if N >= M:
    print(0)
    quit()


x.sort()
s = [0] * (M - 1)
for i in range(M - 1):
    s[i] = x[i + 1] - x[i]

s.sort(reverse=True)

res = sum(s)
if M == 1:
    print(0)
    quit()
else:
    for i in range(N - 1):
        res -= s[i]

print(res)
Problem p03137 (C++)

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

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

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

  vector<long long> Y(M - 1);
  for (int i = 0; i < M - 1; i++)
    Y[i] = X[i + 1] - X[i];

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

  long long ans = 0;
  while (M > N) {

    for (int i = 0; i < Y.size() - (N - 1); i++)
      ans += Y[i];
    cout << ans << endl;
  }
  cout << "0" << endl;
  return 0;
}

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

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

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

  vector<long long> Y(M - 1);
  for (int i = 0; i < M - 1; i++)
    Y[i] = X[i + 1] - X[i];

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

  long long ans = 0;
  while (M > N) {

    for (int i = 0; i < Y.size() - (N - 1); i++)
      ans += Y[i];
    cout << ans << endl;
    return 0;
  }
  cout << "0" << 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), diff(m);
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  sort(x.begin(), x.end());
  adjacent_difference(x.begin(), x.end(), diff.begin());
  diff[0] = -100001;
  sort(diff.begin(), diff.end());
  int ans = accumulate(diff.begin() + 1, diff.end() - n + 1, 0);
  cout << ans << endl;
}


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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

#define INF 100100100
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;

#define mod 998244353

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  rep(i, m) cin >> x[i];
  sort(x.begin(), x.end());
  ll ans = x[m - 1] - x[0];
  priority_queue<int> q;

  for (int i = 0; i < m - 1; i++) {
    q.push(abs(x[i + 1] - x[i]));
  }
  while (n > 1) {
    ans -= q.top();
    q.pop();

    n--;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

#define INF 100100100
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;

#define mod 998244353

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

  for (int i = 0; i < m - 1; i++) {
    q.push(abs(x[i + 1] - x[i]));
  }
  while (n > 1) {
    ans -= q.top();
    q.pop();

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

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

int main() {
  int N, M;
  int X[10005];
  int diff[10005];
  cin >> N >> M;
  for (int i = 0; i < M; ++i)
    cin >> X[i];

  sort(X, X + M);
  for (int i = 0; i < M - 1; ++i)
    diff[i] = X[i + 1] - X[i];
  sort(diff, diff + (M - 1));

  int ans = 0;
  for (int i = 0; i < M - N; ++i)
    ans += diff[i];
  cout << ans << endl;

  return 0;
}


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

int main() {
  int N, M;
  int X[100005];
  int diff[100005];
  cin >> N >> M;
  for (int i = 0; i < M; ++i)
    cin >> X[i];

  sort(X, X + M);
  for (int i = 0; i < M - 1; ++i)
    diff[i] = X[i + 1] - X[i];
  sort(diff, diff + (M - 1));

  int ans = 0;
  for (int i = 0; i < M - N; ++i)
    ans += diff[i];
  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
ll mod = 1000000007;

int numsplit(vector<tuple<int, int, int>> &vt, vector<int> &vpos) {
  int cntsplit = 1;
  auto tmp = vt;
  sort(all(tmp), [](tuple<int, int, int> &a, tuple<int, int, int> &b) {
    return get<0>(a) < get<0>(b);
  });
  int pos = vpos[0];
  for (int l = 0; l < tmp.size(); ++l) {
    cntsplit += distance(find(all(vpos), pos), find(all(vpos), get<0>(tmp[l])));
    pos = get<1>(tmp[l]);
  }
  cntsplit +=
      distance(find(all(vpos), pos), find(all(vpos), vpos[vpos.size() - 1]));
  return cntsplit;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  for (int i = 0; i < m; ++i) {
    cin >> v[i];
  }
  sort(all(v));
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  vector<tuple<int, int, int>> vt;
  for (int j = 1; j < m; ++j) {
    vt.push_back(make_tuple(v[j - 1], v[j], v[j] - v[j - 1]));
  }
  sort(all(vt), [](tuple<int, int, int> &a, tuple<int, int, int> &b) {
    return get<2>(a) < get<2>(b);
  });
  while (numsplit(vt, v) < n) {
    vt.pop_back();
  }
  int out = 0;
  for (int k = 0; k < vt.size(); ++k) {
    out += get<2>(vt[k]);
  }
  cout << out << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
ll mod = 1000000007;

int numsplit(vector<tuple<int, int, int>> &vt, vector<int> &vpos) {
  int cntsplit = 1;
  auto tmp = vt;
  sort(all(tmp), [](tuple<int, int, int> &a, tuple<int, int, int> &b) {
    return get<0>(a) < get<0>(b);
  });
  int pos = vpos[0];
  for (int l = 0; l < tmp.size(); ++l) {
    cntsplit += distance(find(all(vpos), pos), find(all(vpos), get<0>(tmp[l])));
    pos = get<1>(tmp[l]);
  }
  cntsplit +=
      distance(find(all(vpos), pos), find(all(vpos), vpos[vpos.size() - 1]));
  return cntsplit;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  for (int i = 0; i < m; ++i) {
    cin >> v[i];
  }
  sort(all(v));
  if (n >= m) {
    cout << 0 << endl;
    return 0;
  }
  vector<tuple<int, int, int>> vt;
  for (int j = 1; j < m; ++j) {
    vt.push_back(make_tuple(v[j - 1], v[j], v[j] - v[j - 1]));
  }
  sort(all(vt), [](tuple<int, int, int> &a, tuple<int, int, int> &b) {
    return get<2>(a) < get<2>(b);
  });
  // while(numsplit(vt, v) < n){
  //   vt.pop_back();
  // }
  vt.resize(vt.size() - n + 1);
  int out = 0;
  for (int k = 0; k < vt.size(); ++k) {
    out += get<2>(vt[k]);
  }
  cout << out << endl;
  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;
  if (N >= M) {
    cout << 0 << endl;
    return 1;
  }
  vector<int> X(M);
  for (int i; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());
  vector<int> Y(M - 1);
  for (int i = 0; i < M - 1; i++) {
    Y[i] = X[i + 1] - X[i];
  }
  sort(Y.begin(), Y.end());
  long long XX = X[X.size() - 1] - X[0], sumY = 0;
  for (int i = 0; i < N - 1; i++) {
    sumY += Y[Y.size() - 1 - i];
  }
  cout << XX - sumY << endl;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> X(M);
  for (int i; i < M; i++) {
    cin >> X[i];
  }
  sort(X.begin(), X.end());
  vector<int> Y(M - 1);
  for (int i = 0; i < M - 1; i++) {
    Y[i] = X[i + 1] - X[i];
  }
  sort(Y.begin(), Y.end());
  long long XX = X[X.size() - 1] - X[0], sumY = 0;
  for (int i = 0; i < N - 1; i++) {
    sumY += Y[Y.size() - 1 - i];
  }
  cout << XX - sumY << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  if (m == 1)
    cout << 0;
  else {
    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());
    for (int i = 0; i < n - 1; i++)
      d.pop_back();
    int ans = 0;
    for (int i = 0; i < d.size(); i++)
      ans += d[i];
    cout << ans;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++)
    cin >> x[i];
  sort(x.begin(), x.end());
  vector<int> d(m - 1);
  for (int i = 0; i < m - 1; i++)
    d[i] = x[i + 1] - x[i];
  sort(d.begin(), d.end());
  for (int i = 0; i < n - 1; i++)
    d.pop_back();
  int ans = 0;
  for (int i = 0; i < m - n; i++)
    ans += d[i];
  cout << ans;
}
Problem p03137 (C++)

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

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


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

int main() {
  int n, m, k = 0;
  cin >> n >> m;
  vector<int> x(m);
  for (int i = 0; i < m; i++) {
    cin >> x.at(i);
  }
  sort(x.begin(), x.end());
  if (n >= m) {
    k = 0;
  } else {
    vector<int> y(m - 1);
    for (int i = 0; i < m - 1; i++) {
      y.at(i) = x.at(i + 1) - x.at(i);
    }
    sort(y.begin(), y.end());
    for (int i = 0; i < m - n; i++) {
      k += y.at(i);
    }
  }
  cout << k << endl;
  return 0;
}
Problem p03137 (C++)

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

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> diffs(m);
  for (int i = 1; i < m; i++) {
    diffs.push_back(x[i] - x[i - 1]);
  }
  sort(diffs.begin(), diffs.end(), greater<int>());

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


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

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> diffs(m);
  for (int i = 1; i < m; i++) {
    diffs.push_back(x[i] - x[i - 1]);
  }
  sort(diffs.begin(), diffs.end(), greater<int>());

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repf(i, a, b) for (int i = (a); i < (b); i++)
#define repi(i, a, b) for (int i = (a); i <= (b); i++)
#define repr(i, a, b) for (int i = (a); i >= (b); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << " = " << (x) << endl;
#define SORT(x) sort(all(x));
#define RSORT(x) sort(rall(x));
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vb = vector<bool>;
using pii = pair<int, int>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using tiii = tuple<int, int, int>;
const ll inf = 1ll << 29;
const ll mod = 1000000007;
const double eps = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// cout << fixed << setprecision(10);

int main() {
  int N, M;
  cin >> N >> M;
  vi X(M);
  rep(i, M) cin >> X.at(i);

  SORT(X);

  vi d(M - 1);
  rep(i, M - 1) { d.at(i) = X.at(i + 1) - X.at(i); }

  SORT(d);

  rep(i, N - 1) { d.pop_back(); }

  cout << accumulate(all(d), 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repf(i, a, b) for (int i = (a); i < (b); i++)
#define repi(i, a, b) for (int i = (a); i <= (b); i++)
#define repr(i, a, b) for (int i = (a); i >= (b); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << " = " << (x) << endl;
#define SORT(x) sort(all(x));
#define RSORT(x) sort(rall(x));
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vd = vector<double>;
using vb = vector<bool>;
using pii = pair<int, int>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using tiii = tuple<int, int, int>;
const ll inf = 1ll << 29;
const ll mod = 1000000007;
const double eps = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// cout << fixed << setprecision(10);

int main() {
  int N, M;
  cin >> N >> M;
  vi X(M);
  rep(i, M) cin >> X.at(i);

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

  SORT(X);

  vi d(M - 1);
  rep(i, M - 1) { d.at(i) = X.at(i + 1) - X.at(i); }

  SORT(d);

  rep(i, N - 1) { d.pop_back(); }

  cout << accumulate(all(d), 0) << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define mod 1000000007
#define INF2 9999999999
#define INF (1 << 30)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define P pair<int, int>

using namespace std;
using ll = int64_t;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};

void solve() {
  int n, m, res = 0;
  vector<int> x, cost;
  cin >> n >> m;
  rep(i, m) {
    int temp;
    cin >> temp;
    x.push_back(temp);
  }
  sort(x.begin(), x.end());
  for (auto i = x.begin(); i != x.end(); i++) {
    if (i == x.end() - 1)
      break;
    int c = (*(i + 1)) - (*i);
    //        cout << "cost: " << c << endl;
    cost.push_back(c);
    res += c;
  }
  //    cout << "res: " << res << endl;
  sort(cost.begin(), cost.end());
  reverse(cost.begin(), cost.end());
  if (m == 1) {
    cout << 0 << endl;
    return;
  } else {
    for (int i = 0; i < n - 1; i++) {
      res -= cost[i];
    }
  }
  cout << res << endl;
}

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

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define mod 1000000007
#define INF2 9999999999
#define INF (1 << 30)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define P pair<int, int>

using namespace std;
using ll = int64_t;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int DX[] = {1, 1, 0, -1, -1, -1, 0, 1};
int DY[] = {0, -1, -1, -1, 0, 1, 1, 1};

void solve() {
  int n, m, res = 0;
  vector<int> x, cost;
  cin >> n >> m;
  rep(i, m) {
    int temp;
    cin >> temp;
    x.push_back(temp);
  }
  sort(x.begin(), x.end());
  for (auto i = x.begin(); i != x.end(); i++) {
    if (i == x.end() - 1)
      break;
    int c = (*(i + 1)) - (*i);
    //        cout << "cost: " << c << endl;
    cost.push_back(c);
    res += c;
  }
  //    cout << "res: " << res << endl;
  sort(cost.begin(), cost.end());
  reverse(cost.begin(), cost.end());
  if (n >= m) {
    cout << 0 << endl;
    return;
  } else {
    for (int i = 0; i < n - 1; i++) {
      res -= cost[i];
    }
  }
  cout << res << endl;
}

int main() {
  solve();
  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++)

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

  if (m == 1) { // m==1のとき
    cout << 0 << endl;
    return 0;
  }

  vector<int> x(m);
  rep(i, m) { cin >> x.at(i); }
  sort(x.begin(), x.end());

  vector<int> d(m - 1);
  rep(i, m - 1) { d.at(i) = x.at(i + 1) - x.at(i); }

  sort(d.begin(), d.end());
  int res = x.at(m - 1) - x.at(0);
  rep(i, n - 1) { res -= d.at(m - i - 2); }

  cout << res << endl;
  return 0;
}


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

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

  if (n >= m) { // n>=mのとき０
    cout << 0 << endl;
    return 0;
  }

  vector<int> x(m);
  rep(i, m) { cin >> x.at(i); }
  sort(x.begin(), x.end());

  vector<int> d(m - 1);
  rep(i, m - 1) { d.at(i) = x.at(i + 1) - x.at(i); }

  sort(d.begin(), d.end());
  int res = x.at(m - 1) - x.at(0);
  rep(i, n - 1) { res -= d.at(m - i - 2); }

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

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

#define int long long
// #define uint __int128_t

#define pb push_back
#define fi first
#define se second
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
#define low(a, b) lower_bound(all(a), (b)) - (a).begin()
#define upp(a, b) upper_bound(all(a), (b)) - (a).begin()
#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;

signed main() {
  IOS();
  //(1ll<<N)シフト演算のオーバーフローに気をつける
  // MOD同士の引き算に注意
  int N, M;
  cin >> N >> M;
  if (M == 1) {
    cout << 0 << endl;
    return 0;
  }
  vint v(M);
  rep(i, 0, M) cin >> v[i];
  sort(all(v));
  vint a(M - 1);
  int ans = 0;
  rep(i, 0, M - 1) {
    a[i] = v[i + 1] - v[i];
    ans += a[i];
  }
  sort(all(a));
  reverse(all(a));
  rep(i, 0, N - 1) ans -= a[i];
  cout << ans << endl;

  return 0;
}

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

#define int long long
// #define uint __int128_t

#define pb push_back
#define fi first
#define se second
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
#define low(a, b) lower_bound(all(a), (b)) - (a).begin()
#define upp(a, b) upper_bound(all(a), (b)) - (a).begin()
#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;

signed main() {
  IOS();
  //(1ll<<N)シフト演算のオーバーフローに気をつける
  // MOD同士の引き算に注意
  int N, M;
  cin >> N >> M;
  if (M == 1 || N >= M) {
    cout << 0 << endl;
    return 0;
  }
  vint v(M);
  rep(i, 0, M) cin >> v[i];
  sort(all(v));
  vint a(M - 1);
  int ans = 0;
  rep(i, 0, M - 1) {
    a[i] = v[i + 1] - v[i];
    ans += a[i];
  }
  sort(all(a));
  reverse(all(a));
  rep(i, 0, N - 1) ans -= a[i];
  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

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

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

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

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

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

using namespace std;

typedef long long ll;

const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 7;

signed main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  for (auto &x : a)
    cin >> x;
  sort(a.begin(), a.end());
  vector<ll> dist;
  for (ll i = 0; i < m - 1; ++i)
    dist.emplace_back(a[i + 1] - a[i]);
  sort(dist.begin(), dist.end(), greater<ll>());
  ll ans = a.back() - a.front();
  for (ll i = 0; i < n - 1; ++i) {
    ans -= dist[i];
  }
  cout << ans << endl;
}


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

using namespace std;

typedef long long ll;

const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 7;

signed main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(m);
  for (auto &x : a)
    cin >> x;
  sort(a.begin(), a.end());
  vector<ll> dist;
  for (ll i = 0; i < m - 1; ++i)
    dist.emplace_back(a[i + 1] - a[i]);
  sort(dist.begin(), dist.end(), greater<ll>());
  ll ans = a.back() - a.front();
  for (ll i = 0; i < min((ll)dist.size(), n - 1); ++i) {
    ans -= dist[i];
  }
  cout << ans << endl;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
  int N, M, X[10000];
  int d[10000], ans;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  if (N < M) {
    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>());
    ans = X[M - 1] - X[0];
    for (int i = 0; i < N - 1; i++) {
      ans -= d[i];
    }
  } else {
    ans = 0;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int main() {
  int N, M, X[100000];
  int d[100000], ans;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    cin >> X[i];
  }
  if (N < M) {
    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>());
    ans = X[M - 1] - X[0];
    for (int i = 0; i < N - 1; i++) {
      ans -= d[i];
    }
  } else {
    ans = 0;
  }
  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(m), xs(m - 1);
  rep(i, m) cin >> x[i];
  sort(all(x));
  rep(i, m - 1) { xs[i] = x[i + 1] - x[i]; }
  sort(rall(xs));
  cout << accumulate(xs.begin() + n - 1, xs.end(), 0) << endl;
  ;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
  int n, m;
  cin >> n >> m;
  if (m <= n) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> x(m), xs(m - 1);
  rep(i, m) cin >> x[i];
  sort(all(x));
  rep(i, m - 1) { xs[i] = x[i + 1] - x[i]; }
  sort(rall(xs));
  cout << accumulate(xs.begin() + n - 1, xs.end(), 0) << 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> A(M), B(M - 1);
  long long V;
  for (int X = 0; X < M; X++) {
    cin >> A[X];
  }
  sort(A.begin(), A.end());
  V = A[M - 1] - A[0];
  for (int X = 0; X < M; X++) {
    if (X != 0) {
      B[X - 1] = A[X] - A[X - 1];
    }
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  for (int X = 0; X < N - 1; X++) {
    V -= B[X];
  }
  cout << V << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M - 1);
  long long V = 0;
  if (N >= M) {
    cout << 0 << endl;
    return 0;
  }
  for (int X = 0; X < M; X++) {
    cin >> A[X];
  }
  sort(A.begin(), A.end());
  V = A[M - 1] - A[0];
  for (int X = 0; X < M; X++) {
    if (X != 0) {
      B[X - 1] = A[X] - A[X - 1];
    }
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  for (int X = 0; X < N - 1; X++) {
    V -= B[X];
  }
  cout << V << endl;
}
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.at(i);
  sort(X.begin(), X.end());
  int kyori = X.at(M - 1) - X.at(0);

  if (M == 1) {
    cout << 0 << endl;
  } else {
    vector<int> sa(M - 1);
    for (int i = 0; i < M - 1; i++) {
      sa.at(i) = X.at(i + 1) - X.at(i);
    }
    sort(sa.begin(), sa.end());
    int minos = 0;
    for (int i = 0; i < N - 1; i++) {
      minos += sa.at(M - 2 - i);
    }
    cout << kyori - minos << 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.at(i);
  sort(X.begin(), X.end());
  int kyori = X.at(M - 1) - X.at(0);

  if (M == 1 || N >= M) {
    cout << 0 << endl;
  } else {
    vector<int> sa(M - 1);
    for (int i = 0; i < M - 1; i++) {
      sa.at(i) = X.at(i + 1) - X.at(i);
    }
    sort(sa.begin(), sa.end());
    int minos = 0;
    for (int i = 0; i < N - 1; i++) {
      minos += sa.at(M - 2 - i);
    }
    cout << kyori - minos << endl;
  }
}
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;
  for (int i = 0; i < M; i++) {
    int t;
    std::cin >> t;
    X.push_back(t);
  }

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

  int pos_min = X.at(0);
  int pos_max = X.at(X.size() - 1);
  int ans = pos_max - pos_min;

  std::vector<int> margins;
  for (int i = 1; i < X.size(); i++)
    margins.push_back(X.at(i) - X.at(i - 1));

  std::sort(margins.begin(), margins.end());
  std::reverse(margins.begin(), margins.end());

  for (int i = 0; i < N - 1; i++)
    ans -= margins.at(i);

  std::cout << ans << 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;
  for (int i = 0; i < M; i++) {
    int t;
    std::cin >> t;
    X.push_back(t);
  }

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

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

  int pos_min = X.at(0);
  int pos_max = X.at(X.size() - 1);
  int ans = pos_max - pos_min;

  std::vector<int> margins;
  for (int i = 1; i < X.size(); i++)
    margins.push_back(X.at(i) - X.at(i - 1));

  std::sort(margins.begin(), margins.end());
  std::reverse(margins.begin(), margins.end());

  for (int i = 0; i < N - 1; i++)
    ans -= margins.at(i);

  std::cout << ans << std::endl;

  return 0;
}
Problem p03137 (C++)

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

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

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

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

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

  vector<int> D(M - 1);
  for (int i = 0; i < M - 1; i++)
    D.at(i) = X.at(i + 1) - X.at(i);
  sort(D.begin(), D.end());

  int ans = X.at(M - 1) - X.at(0);
  for (int i = 0; i < N - 1; i++)
    if (i < M + 2)
      ans -= D.at(M - 2 - i);

  cout << ans << endl;

  return 0;
}

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

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

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

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

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

  vector<int> D(M - 1);
  for (int i = 0; i < M - 1; i++)
    D.at(i) = X.at(i + 1) - X.at(i);
  sort(D.begin(), D.end());

  int ans = X.at(M - 1) - X.at(0);
  for (int i = 0; i < N - 1; i++)
    if (i <= M - 2)
      ans -= D.at(M - 2 - i);

  cout << ans << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int x[m], dx[m + 1];
  REP(i, m) cin >> x[i];
  sort(x, x + m);

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

  FOR(i, 1, m) dx[i] = x[i] - x[i - 1];

  sort(dx + 1, dx + m, greater<int>());

  ll sum = x[m - 1] - x[0];
  FOR(i, 1, n) { sum -= dx[i]; }

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int x[m], dx[m + 1];
  REP(i, m) cin >> x[i];
  sort(x, x + m);

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

  FOR(i, 1, m) dx[i] = x[i] - x[i - 1];

  sort(dx + 1, dx + m, greater<int>());

  ll sum = x[m - 1] - x[0];
  FOR(i, 1, n) { sum -= dx[i]; }

  cout << sum << endl;

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define ll long long
using namespace std;
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> xl(m);
  rep(i, m) { cin >> xl[i]; }
  sort(xl.begin(), xl.end());
  vector<ll> dist;
  rep(i, m) {
    if (i + 1 >= xl.size())
      break;
    dist.push_back(abs(xl[i + 1] - xl[i]));
  }
  sort(dist.begin(), dist.end());
  ll ans = 0;
  if (dist.size() >= 1) {
    rep(i, dist.size() - (n - 1)) { ans += dist[i]; }
  }
  cout << ans << endl;
  // cout << fixed << setprecision(10) << ans << endl;
  system("pause");
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define ll long long
using namespace std;
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

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

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> xl(m);
  rep(i, m) { cin >> xl[i]; }
  sort(xl.begin(), xl.end());
  vector<ll> dist;
  rep(i, m) {
    if (i + 1 >= xl.size())
      break;
    dist.push_back(abs(xl[i + 1] - xl[i]));
  }
  sort(dist.begin(), dist.end());
  ll ans = 0;
  if (dist.size() >= 1) {
    rep(i, dist.size()) {
      ll last = dist.size() - (n - 1);
      if (last < 0)
        break;
      if (i >= last)
        break;
      ans += dist[i];
    }
  }
  cout << ans << endl;
  // cout << fixed << setprecision(10) << ans << endl;
  system("pause");
}
Problem p03137 (C++)

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

using namespace std;
using ll = long long;

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

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

using namespace std;
using ll = long long;

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

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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;

int n, m;
int x[maxn];

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  sort(x, x + m);
  vector<int> gap;
  for (int i = 0; i + 1 < m; i++) {
    gap.push_back(x[i + 1] - x[i]);
  }

  sort(gap.begin(), gap.end());
  for (int i = 0; i < n - 1; i++) {
    gap.pop_back();
  }

  int res = std::accumulate(gap.begin(), gap.end(), 0);
  out(res);

  return 0;
}


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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;

int n, m;
int x[maxn];

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> x[i];
  }
  sort(x, x + m);
  vector<int> gap;
  for (int i = 0; i + 1 < m; i++) {
    gap.push_back(x[i + 1] - x[i]);
  }

  sort(gap.begin(), gap.end());
  for (int i = 0; i < n - 1 && !gap.empty(); i++) {
    gap.pop_back();
  }

  int res = std::accumulate(gap.begin(), gap.end(), 0);
  out(res);

  return 0;
}
Problem p03137 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize "-O3"
#pragma GCC target("avx")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define inf (1ll << 60)
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())
signed gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
signed lcm(int a, int b) { return a * b / gcd(a, b); }

using namespace std;

const int mod = 1e9 + 7;

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int n, m;
  cin >> n >> m;
  if (m == 1) {
    cout << 0 << endl;
    return;
  }
  vi x(m);
  rep(i, m) cin >> x[i];
  RSORT(x);
  int ans = x[0] - x[m - 1];
  vi sa(m - 1);
  rep(i, m - 1) { sa[i] = x[i] - x[i + 1]; }
  RSORT(sa);
  int sum_sa = 0;
  rep(i, n - 1) { sum_sa += sa[i]; }
  cout << ans - sum_sa << endl;
}

Fixed submission:
#pragma GCC optimize "-O3"
#pragma GCC target("avx")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int, int>
#define vp vector<pair<int, int>>
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define inf (1ll << 60)
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())
signed gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
signed lcm(int a, int b) { return a * b / gcd(a, b); }

using namespace std;

const int mod = 1e9 + 7;

void run();

void init() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
}

signed main() {
  init();
  run();
  return 0;
}

void run() {
  int n, m;
  cin >> n >> m;
  if (n >= m) {
    cout << 0 << endl;
    return;
  }
  vi x(m);
  rep(i, m) cin >> x[i];
  RSORT(x);
  int ans = x[0] - x[m - 1];
  vi sa(m - 1);
  rep(i, m - 1) { sa[i] = x[i] - x[i + 1]; }
  RSORT(sa);
  int sum_sa = 0;
  rep(i, n - 1) { sum_sa += sa[i]; }
  cout << ans - sum_sa << endl;
}