alexjercan/bugnet huggingface dataset:
Problem p01227 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX = INT_MAX / 20;
int main() {
  int t;
  cin >> t;
  REP(data, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    REP(i, n) cin >> x[i];

    vi d(n - 1);
    ll total = 0;
    REP(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      total += d[i];
    }

    sort(ALL(d), greater<int>());

    REP(i, min(k - 1, n - 1)) { total -= d[i]; }

    cout << total << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX = INT_MAX / 20;
int main() {
  int t;
  cin >> t;
  REP(data, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    REP(i, n) cin >> x[i];

    vvi dp(k, vi(n + 1, MAX));
    dp[0][0] = 0;

    REP(i, k) {
      FOR(j, 1, n + 1) {
        dp[i][j] = min(dp[i][j - 1] + (j == 1 ? 0 : x[j - 1] - x[j - 2]),
                       (i > 0 ? dp[i - 1][j - 1] : MAX));
      }
    }

    int best = MAX;
    REP(i, k) { best = min(best, dp[i][n]); }
    cout << best << endl;
  }
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[114514];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (k >= n) {
    cout << "0\\n";
    return;
  }
  int a1[114514];
  for (int i = 0; i < n - 1; i++)
    a1[i] = a[i + 1] - a[i];
  sort(a1, a1 + (n - 1));
  int ans = a[n - 1] - a[0];
  for (int i = 0; i < k - 1; i++) {
    ans -= a1[n - i - 2];
  }
  cout << ans << endl;
}
int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++)
    solve();
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[110];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (k >= n) {
    cout << "0\\n";
    return;
  }
  int a1[110];
  for (int i = 0; i < n - 1; i++)
    a1[i] = a[i + 1] - a[i];
  sort(a1, a1 + (n - 1));
  int ans = a[n - 1] - a[0];
  for (int i = 0; i < k - 1; i++) {
    ans -= a1[n - i - 2];
  }
  cout << ans << endl;
}
int main() {
  int d;
  cin >> d;
  for (int i = 0; i < d; i++)
    solve();
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX 100000
using namespace std;

int main() {
  int m, n, k;
  // vector<int> data;
  vector<int> x;
  vector<int> sum;
  int dis[MAX] = {0};
  cin >> m;
  sum.resize(m, 0);
  // data.resize(m);
  for (int i = 0; i < m; i++) {
    // sum=0;
    cin >> n >> k;
    x.resize(n, 0);

    for (int j = 0; j < n; j++) {
      cin >> x[j];
      if (j > 0)
        dis[j - 1] = x[j] - x[j - 1];
    }
    sort(dis, dis + n - 1);
    // int sum=0;
    for (int p = 0; p < n - k; p++) {
      sum[i] += dis[p];
    }
    // cout<<sum<<endl;
  }
  for (int i = 0; i < m; i++)
    cout << sum[i] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX 10000
using namespace std;

int main() {
  int m, n, k;
  vector<int> data;
  vector<int> x;
  vector<int> sum;
  int dis[MAX] = {0};
  cin >> m;
  data.resize(m);
  for (int i = 0; i < m; i++) {
    // sum=0;
    cin >> n >> k;
    x.resize(n);
    sum.resize(n, 0);
    // dis.resize(n-1);
    for (int j = 0; j < n; j++) {
      cin >> x[j];
      if (j > 0)
        dis[j - 1] = x[j] - x[j - 1];
    }
    sort(dis, dis + n - 1);
    // int sum=0;
    for (int p = 0; p < n - k; p++) {
      sum[i] += dis[p];
    }
    // cout<<sum<<endl;
  }
  for (int i = 0; i < m; i++)
    cout << sum[i] << endl;
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int c;
  cin >> c;
  for (int dataCount = 0; dataCount < c; dataCount++) {
    int n, k;
    cin >> n >> k;
    int x[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    if (n <= k) {
      cout << 0 << endl;
      continue;
    }
    int distance[n - 1];
    for (int i = 0; i < n - 1; i++) {
      distance[i] = x[i + 1] - x[i];
    }
    bool done[n - 1];
    fill(done, done + (n - 1), false);
    int sum = 0;
    sort(distance, distance + (n - 1), greater<>());
    for (int i = 0; i < k - 1; i++) {
      sum += distance[i];
    }
    cout << x[n - 1] - x[0] - sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int c;
  cin >> c;
  for (int dataCount = 0; dataCount < c; dataCount++) {
    int n, k;
    cin >> n >> k;
    int x[n];
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    if (n <= k) {
      cout << 0 << endl;
      continue;
    }
    int distance[n - 1];
    for (int i = 0; i < n - 1; i++) {
      distance[i] = x[i + 1] - x[i];
    }
    bool done[n - 1];
    fill(done, done + (n - 1), false);
    int sum = 0;
    for (int i = 0; i < k - 1; i++) {
      int maxVal = -1;
      int maxIndex = -1;
      for (int j = 0; j < n - 1; j++) {
        if (done[j])
          continue;
        if (maxVal < distance[j]) {
          maxVal = distance[j];
          maxIndex = j;
        }
      }
      sum += maxVal;
      done[maxIndex] = true;
    }
    cout << x[n - 1] - x[0] - sum << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <queue>
#include <stdio.h>
#include <string.h>
#define MAXNUM 100000

using namespace std;

int main(void) {
  int e[MAXNUM - 1], x[MAXNUM], t, n, k, i, j, l, max_i, sum;
  priority_queue<int> que;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d%d", &n, &k);
    k--;
    for (j = 0; j < n; j++)
      scanf("%d", &x[j]);
    memset(e, 0, sizeof(e));
    if (n <= k + 1) {
      printf("0\\n");
      continue;
    }
    for (j = 0; j < n - 1; j++)
      que.push(x[j + 1] - x[j]);
    sum = 0;
    for (j = 0; j < k; j++) {
      sum += que.top();
      que.pop();
    }
    while (que.size())
      que.pop();
    printf("%d\\n", x[n - 1] - x[0] - sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#define MAXNUM 1000000

int main(void) {
  int e[MAXNUM - 1], x[MAXNUM], t, n, k, i, j, l, max_i, sum;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    scanf("%d%d", &n, &k);
    k--;
    for (j = 0; j < n; j++)
      scanf("%d", &x[j]);
    memset(e, 0, sizeof(e));
    sum = 0;
    if (n <= k + 1) {
      printf("0\\n");
      continue;
    }
    for (j = 0; j < k; j++) {
      max_i = -1;
      for (l = 0; l < n - 1; l++)
        if (!e[l] && (max_i == -1 || x[max_i + 1] - x[max_i] < x[l + 1] - x[l]))
          max_i = l;
      e[max_i] = 1;
      sum += x[max_i + 1] - x[max_i];
    }
    printf("%d\\n", x[n - 1] - x[0] - sum);
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int t;

  cin >> t;

  for (int i = 0; i < t; ++i) {
    int n, k;
    vector<int> x;
    vector<int> distance;
    int temp;
    int total = 0;

    cin >> n >> k;

    for (int j = 0; j < n; ++j) {
      cin >> temp;
      x.push_back(temp);
    }

    if (n <= k)
      cout << 0 << endl;
    else {
      total = temp - x[0];

      for (int j = 0; j < n - 1; ++j)
        distance.push_back(x[j + 1] - x[j]);

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

      for (int j = 0; j < k - 1; ++j)
        total -= distance[j];

      cout << total << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int t;

  cin >> t;

  for (int i = 0; i < t; ++i) {
    int n, k;
    vector<int> x;
    vector<int> distance;
    int temp;
    int total = 0;

    cin >> n >> k;

    for (int j = 0; j < n; ++j) {
      cin >> temp;
      x.push_back(temp);
    }

    total = temp - x[0];

    for (int j = 0; j < n - 1; ++j)
      distance.push_back(x[j + 1] - x[j]);

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

    for (int j = 0; j < k - 1; ++j)
      total -= distance[j];

    cout << total << endl;
  }

  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

using namespace std;

typedef vector<int> vi;

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    rep(i, n) cin >> x[i];
    sort(all(x));
    vi d(n - 1);
    int s = 0;
    rep(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      s += d[i];
    }
    sort(rall(d));

    int end = min(k - 1, n - 1);

    rep(i, end) { s -= d[i]; }
    s = max(0, s);
    cout << s << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

using namespace std;

typedef vector<int> vi;

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    rep(i, n) cin >> x[i];
    sort(all(x));
    vi d(n - 1);
    int s = 0;
    rep(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      s += d[i];
    }
    sort(rall(d));

    rep(i, k - 1) { s -= d[i]; }
    s = max(0, s);
    cout << s << endl;
  }
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 100005
#define INF 1000000005LL

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

ll K;
ll T[SIZE];
ll S[SIZE];

void solve(int n, int k) {
  if (n <= k) {
    printf("0\\n");
    return;
  } else if (k == 1) {
    printf("%lld\\n", T[n - 1] - T[0]);
    return;
  }
  ll road = T[n - 1] - T[0];
  for (int i = 1; i < n; i++) {
    S[i - 1] = T[i] - T[i - 1];
  }
  sort(S, S + n - 1);
  for (int i = 0; i < (k - 1); i++) {
    road -= S[n - 2 - i];
  }
  printf("%lld\\n", road);
}

void imain() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &T[i]);
  solve(n, k);
}

int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    imain();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 1000
#define INF 1000000005LL

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

int n;
ll M;
ll T[SIZE];
ll K;

bool check(ll m) {
  printf("=%d=", m);
  for (int a = 0; a < n; a++) {
    if (T[a] > m)
      continue;
    for (int b = a + 1; b < n; b++) {
      if (T[a] + T[b] > m)
        continue;
      for (int c = b + 1; c < n; c++) {
        if (T[a] + T[b] + T[c] > m)
          continue;
        for (int d = c + 1; d < n; d++) {
          if (T[a] + T[b] + T[c] + T[d] == m)
            return true;
          printf("(%d)", T[a] + T[b] + T[c] + T[d]);
        }
      }
    }
  }
  return false;
}

bool solve() {
  ll l = 0, m, r;
  scanf("%d %lld", &n, &M);
  if (n == 0)
    return false;
  r = M + 1;
  for (int i = 0; i < n; i++)
    scanf("%lld", &T[i]);
  while (l + 1 < r) {
    m = (l + r) / 2;
    if (check(m))
      l = m;
    else
      r = m;
  }
  printf("%lld\\n", r);
  return true;
}

int main() {
  while (solve()) {
  };
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> dist;
    int pre;
    dist.push_back(0);
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (i)
        dist.push_back(tmp - pre);
      pre = tmp;
    }
    SORT(dist);
    REP(i, n - 1) dist[i + 1] += dist[i];
    cout << dist[max(n - k, 0)] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> dist;
    int pre;
    dist.push_back(0);
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (i)
        dist.push_back(tmp - pre);
      pre = tmp;
    }
    SORT(dist);
    REP(i, n - 1) dist[i + 1] += dist[i];
    cout << dist[n - k] << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;

int main() {
  int i, t;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    int s, n, k;
    scanf("%d %d", &n, &k);
    int x[n];
    int cnt;
    for (cnt = 1; cnt <= n; cnt++) {
      scanf("%d", &x[cnt]);
    }
    // データの取得

    // X[m]=x(m+1)-x(m) (m=1,,,,n-1)を取得する
    int m, X[n];
    X[0] = 0;
    for (m = 1; m <= n - 1; m++) {
      X[m] = x[m + 1] - x[m];
    }

    sort(X, X + n);
    // k台の発電機により、k-1のエリアと両端には電線を引かなくてよい
    // ゆえにもとめる電線の長さLは
    // L=(x(n)-x(1)) - (X(1)+X(2)+,,,X(k-1)) である。

    int l1, l2 = {0};
    l1 = x[n] - x[1];
    for (s = 1; s <= k - 1; s++) {
      l2 = l2 + X[n - s];
    }
    int L;
    L = l1 - l2;
    if (n <= k) {
      printf("0\\n");
    } else {
      printf("%d\\n", L);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
int main() {
  int i, t;
  scanf("%d", &t);
  for (i = 0; i < t; i++) {
    int s, n, k;
    scanf("%d %d", &n, &k);
    if (k >= n) {
      printf("0\\n");
    } else {
      int x[n + 1];
      int cnt;
      for (cnt = 1; cnt <= n; cnt++) {
        scanf("%d", &x[cnt]);
      }
      // データの取得

      // X[m]=x(m+1)-x(m) (m=1,,,,n-1)を取得する
      int m, X[n];
      for (m = 1; m <= n - 1; m++) {
        X[m] = x[m + 1] - x[m];
      }

      // X[m]を降順にソート
      int i, j;
      for (j = 0; j < n; j++) {
        for (i = 1; i <= n - 2; i++) {
          if (X[i + 1] > X[i]) {
            int temp = X[i];
            X[i] = X[i + 1];

            X[i + 1] = temp;
          }
        }
      }

      // k台の発電機により、k-1のエリアと両端には電線を引かなくてよい
      // ゆえにもとめる電線の長さLは
      // L=(x(n)-x(1)) - (X(1)+X(2)+,,,X(k-1)) である。

      int l1, l2 = {0};
      l1 = x[n] - x[1];
      for (s = 1; s <= k - 1; s++) {
        l2 = l2 + X[s];
      }
      int L;
      L = l1 - l2;

      printf("%d\\n", L);
    }
  }

  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int map[100001];

int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    --n;
    int prev, now;
    cin >> prev;
    for (int i = 0; i < n; ++i) {
      cin >> now;
      map[i] = now - prev;
      prev = now;
    }
    sort(map, map + n);
    n = n - k + 1;
    long sum = 0;
    for (int i = 0; i < n; ++i)
      sum += map[i];
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int map[50000], Min, n, k;

void search(int sum, int cnt, int i) {
  if (!sum) {
    Min = sum;
    return;
  }
  if (cnt == k) {
    if (Min > sum)
      Min = sum;
    return;
  }
  for (int j = i; j < n; ++j) {
    ::search(sum - map[j], cnt + 1, j + 1);
  }
}

int main(void) {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    --n;
    int prev, now, sum = 0;
    cin >> prev;
    for (int i = 0; i < n; ++i) {
      cin >> now;
      map[i] = now - prev;
      sum += map[i];
      prev = now;
    }
    Min = sum;
    search(sum, 1, 0);
    cout << Min << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (int a = 0; a < t; a++) {
    int n, k, x[100000], sum;
    vector<int> vec;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i)
        vec.push_back(x[i] - x[i - 1]);
    }
    sum = x[n - 1] - x[0];
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < min(n, k) - 1; i++) {
      sum -= vec[i];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (int a = 0; a < t; a++) {
    int n, k, x[100000], sum;
    vector<int> vec;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i)
        vec.push_back(x[i] - x[i - 1]);
    }
    sum = x[n - 1] - x[0];
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < k - 1; i++) {
      sum -= vec[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) // the number of dataset
  {
    int n, k;
    cin >> n >> k;
    int min_Length;
    vector<int> x(100001);
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    if (k >= n) {
      min_Length = 0;
    } else {
      vector<int> A(n);
      for (int i = 0; i < n - 1; i++) {
        A[i] = x[i + 1] - x[i];
      }
      sort(A.begin(), A.end());
      min_Length = x[n - 1] - x[0];
      for (int m = n - 1; m >= n - k + 1; m--) {
        min_Length -= A[m];
      }
    }
    cout << min_Length << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) // the number of dataset
  {
    int n, k;
    cin >> n >> k;
    int min_Length;
    if (k >= n) {
      min_Length = 0;
    } else {
      vector<int> x(100001);
      for (int i = 0; i < n; i++) {
        cin >> x[i];
      }
      vector<int> A(n);
      for (int i = 0; i < n - 1; i++) {
        A[i] = x[i + 1] - x[i];
      }
      sort(A.begin(), A.end());
      min_Length = x[n - 1] - x[0];
      for (int m = n - 2; m >= n - k; m--) {
        min_Length -= A[m];
      }
    }
    cout << min_Length << endl;
  }
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;

bool cmp(const P &p, const P &q) {
  if (p.S != q.S)
    return p.second > q.second;
  return p.F < q.F;
}

int main() {
  int n, k, T, ti[110000], Max, Min;
  cin >> T;
  while (T-- > 0) {
    vector<P> dif;
    cin >> n >> k;
    Max = 0;
    Min = 1000000000;
    for (int i = 0; i < n; i++) {
      cin >> ti[i];
      Max = max(Max, ti[i]);
      Min = min(Min, ti[i]);
      if (i != 0) {
        dif.push_back(P(i, ti[i] - ti[i - 1]));
      }
    }
    sort(dif.begin(), dif.end(), cmp);
    int sum = 0;

    if (n > k) {
      for (int i = 0; i < k - 1; i++)
        sum += dif[i].S;
      cout << Max - sum - Min << endl;
    } else
      cout << "0" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;

bool cmp(const P &p, const P &q) {
  if (p.S != q.S)
    return p.second > q.second;
  return p.F < q.F;
}

int main() {
  int n, k, T, ti[110000], Max, Min;
  cin >> T;
  while (T-- > 0) {
    vector<P> dif;
    cin >> n >> k;
    Max = 0;
    Min = 1000000000;
    for (int i = 0; i < n; i++) {
      cin >> ti[i];
      Max = max(Max, ti[i]);
      Min = min(Min, ti[i]);
      if (i != 0) {
        dif.push_back(P(i, ti[i] - ti[i - 1]));
      }
    }
    sort(dif.begin(), dif.end(), cmp);
    int sum = 0;

    for (int i = 0; i < k - 1; i++)
      sum += dif[i].S;
    if (n > k)
      cout << Max - sum - Min << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int T, N, K, X[100000 + 10], A[100000 + 10];

int main() {
  cin >> T; // ???????????????????????°???????????????
  for (int t = 0; t < T; ++t) {
    int length = 0;
    cin >> N >> K; // ???????????????????????°???????????????
    /*
    if (N<=K){
      cout << 0 << endl;
    }
    //??????????????¶?????°??\\????????´???????????????????????????????????§?????????0
    */

    for (int i = 0; i < N; ++i)
      cin >> X[i]; // ??¶??????????????\\???
    for (int i = 0; i < N - 1; ++i)
      A[i] = X[i + 1] - X[i]; // ??¶??¨??¶??????
    sort(A, A + N - 1);       // ??????A?????´?????????

    for (int i = 0; i < (N - 1) - (K - 1); ++i) {
      // ??????A?????????max(0,N-1-(K-1))????????????????????????
      length += A[i];
    }
    cout << length << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int T, N, K, X[100000 + 10], A[100000 + 10];
int main() {
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int tot = 0;
    int cut = 0;
    cin >> N >> K;
    for (int i = 0; i < N; ++i)
      cin >> X[i];
    for (int i = 0; i + 1 < N; ++i) {
      A[i] = X[i + 1] - X[i];
      tot += A[i];
    }
    sort(A, A + N - 2);    //????????????????????????
    reverse(A, A + N - 2); //???????????\\????????????
    if (N >= K)            //??¶?????°>=?????????
      for (int i = 0; i < K; ++i)
        cut += A[i]; //????????§????????????????????????????¨?
    else
      for (int i = 0; i < N; ++i)
        cut += A[i];
    cout << tot - cut << endl;
  }
  return 0;
}