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

#include <cstring>
#include <iostream>

long long h[1500001], w[1500001];
long long hh[1500], ww[1500];

int main() {
  int n, m;
  while (std::cin >> n >> m && (n || m)) {
    std::memset(h, 0, sizeof(h));
    std::memset(w, 0, sizeof(w));

    int numX = 0, numY = 0;
    for (int i = 0; i < n; i++) {
      std::cin >> hh[i];
    }
    for (int i = 0; i < m; i++) {
      std::cin >> ww[i];
    }

    for (int sy = 0; sy < n; sy++) {
      for (int y = 0; y < n - sy; y++) {
        long long num = 0;
        for (int i = 0; i <= sy; i++) {
          num += hh[i + y];
        }
        h[num]++;
      }
    }

    for (int sx = 0; sx < m; sx++) {
      for (int x = 0; x < m - sx; x++) {
        int num = 0;
        for (int i = 0; i <= sx; i++) {
          num += ww[i + x];
        }
        w[num]++;
      }
    }

    long long cnt = 0;
    for (int i = 0; i < 1500001; i++) {
      cnt += h[i] * w[i];
    }

    std::cout << cnt << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int h[1000], w[1000];

int main() {
  int n, m;
  while (std::cin >> n >> m && (n || m)) {
    for (int i = 0; i < n; i++)
      std::cin >> h[i];
    for (int i = 0; i < m; i++)
      std::cin >> w[i];

    int cnt = 0;
    for (int sy = 0; sy < n; sy++) {
      for (int sx = 0; sx < m; sx++) {
        for (int y = 0; y < n - sy; y++) {
          for (int x = 0; x < m - sx; x++) {
            int wSum = 0, hSum = 0;
            for (int j = x; j <= x + sx; j++) {
              wSum += w[j];
            }
            for (int i = y; i <= y + sy; i++) {
              hSum += h[i];
            }
            if (wSum == hSum)
              cnt++;
          }
        }
      }
    }
    std::cout << cnt << std::endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
int H, W;
int h[1500], w[1500];

void solve() {
  for (int i = 0; i < H; i++)
    scanf("%d", &h[i]);
  for (int i = 0; i < W; i++)
    scanf("%d", &w[i]);

  int mxh = accumulate(h, h + H, 0);
  int mxw = accumulate(w, w + W, 0);

  vector<int> th(mxh + 1, 0), tw(mxw + 1, 0);

  for (int i = 0; i < H; i++) {
    int sum = 0;
    for (int j = i; j < H; j++) {
      sum += h[j];
      th[sum]++;
    }
  }
  for (int i = 0; i < W; i++) {
    int sum = 0;
    for (int j = i; j < W; j++) {
      sum += w[j];
      tw[sum]++;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= min(mxh, mxw); i++) {
    ans += (long long)th[i] * tw[i];
  }

  printf("%d\\n", ans);
}
int main() {
  while (scanf("%d%d", &H, &W), H || W)
    solve();
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int i, j, k, s;
  int N, M;
  long long count = 0;
  while (1) {
    cin >> N >> M;
    if (N == 0)
      break;
    int h[N], w[M];
    for (i = 0; i < N; i++)
      cin >> h[i];
    for (i = 0; i < M; i++)
      cin >> w[i];
    int x = (N + 1) * N / 2;
    int y = (M + 1) * M / 2;
    int a[x];
    int b[y];
    k = 0;
    while (k < x) {
      for (i = 0; i < N; i++) {
        s = 0;
        for (j = i; j < N; j++) {
          s = s + h[j];
          a[k] = s;
          k++;
        }
      }
    }
    k = 0;
    while (k < y) {
      for (i = 0; i < N; i++) {
        s = 0;
        for (j = i; j < M; j++) {
          s = s + w[j];
          b[k] = s;
          k++;
        }
      }
    }
    for (i = 0; i < x; i++)
      for (j = 0; j < y; j++) {
        if (a[i] == b[j])
          count++;
      }
    cout << count << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> P;
typedef vector<P> vP;

int main() {
  while (true) {

    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    vi x(3000009, 0);
    vi y(3000009, 0);
    x[0] = 1;
    y[0] = 1;
    vi h(max(n, m) + 1, 0);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        x[h[j] - h[i]] += 1;
      }
    }
    sum = 0;
    for (int i = 1; i <= m; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        y[h[j] - h[i]] += 1;
      }
    }
    int ans = 0;
    for (int i = 1; i < 3000009; i++) {
      ans += x[i] * y[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> P;
typedef vector<P> vP;

int main() {
  while (true) {

    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    vi x(3009, 0);
    vi y(3009, 0);
    x[0] = 1;
    y[0] = 1;
    vi h(max(n, m) + 1, 0);
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        x[h[j] - h[i]] += 1;
      }
    }
    sum = 0;
    for (int i = 1; i <= m; i++) {
      int temp;
      cin >> temp;
      sum += temp;
      h[i] = sum;
    }
    for (int i = 0; i <= m; i++) {
      for (int j = i + 1; j <= m; j++) {
        y[h[j] - h[i]] += 1;
      }
    }
    int ans = 0;
    for (int i = 1; i < 3009; i++) {
      ans += x[i] * y[i];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01140 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n, m;
  while (cin >> n >> m and (n or m)) {

    int h[1501], w[1501];
    vector<int> hh(1500001, 0), ww(1500001, 0);

    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];

    rep(i, n) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += h[j];
        hh[s]++;
      }
    }

    rep(i, m) {
      int s = 0;
      for (int j = i; j < m; ++j) {
        s += w[j];
        ww[s]++;
      }
    }

    int ans = 0;
    rep(i, 1500001) { ans += hh[i] * ww[i]; }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  int n, m;
  while (cin >> n >> m and (n or m)) {

    int h[1001], w[1001];
    vector<int> hh(1500001, 0), ww(1500001, 0);

    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];

    rep(i, n) {
      int s = 0;
      for (int j = i; j < n; ++j) {
        s += h[j];
        hh[s]++;
      }
    }

    rep(i, m) {
      int s = 0;
      for (int j = i; j < m; ++j) {
        s += w[j];
        ww[s]++;
      }
    }

    int ans = 0;
    rep(i, 1500001) { ans += hh[i] * ww[i]; }

    cout << ans << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n[2];
ll cnt[2][1500010];
ll h[2][1500010];

int main() {
  while (cin >> n[0] >> n[1]) {
    if (n[0] + n[1] == 0) {
      return 0;
    }
    REP(i, 2) {
      fill(cnt[i], cnt[i] + 1500010, 0);
      fill(h[i], h[i] + 1500010, 0);
    }
    REP(i, 2) {
      FOR(j, 1, n[i] + 1) cin >> h[i][j];
      REP(j, n[i]) h[i][j + 1] += h[i][j];
    }
    REP(i, 2) {
      REP(j, n[i]) {
        FOR(k, j + 1, n[i] + 1) { cnt[i][h[i][k] - h[i][j]]++; }
      }
    }
    ll ans = 0;
    REP(i, 1500010) { ans += (cnt[0][i] * cnt[1][i]); }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n[2];
ll cnt[2][2000];
ll h[2][2000];

int main() {
  while (cin >> n[0] >> n[1]) {
    if (n[0] + n[1] == 0) {
      return 0;
    }
    REP(i, 2) {
      fill(cnt[i], cnt[i] + 2000, 0);
      fill(h[i], h[i] + 2000, 0);
    }
    REP(i, 2) {
      FOR(j, 1, n[i] + 1) cin >> h[i][j];
      REP(j, n[i]) h[i][j + 1] += h[i][j];
    }
    REP(i, 2) {
      REP(j, n[i]) {
        FOR(k, j + 1, n[i] + 1) { cnt[i][h[i][k] - h[i][j]]++; }
      }
    }
    ll ans = 0;
    REP(i, 1001) { ans += (cnt[0][i] * cnt[1][i]); }
    cout << ans << endl;
  }
}
Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

using namespace std;

int main() {
  int n, m, h[1501], w[1501], s, t;

  while (cin >> n >> m && (n + m)) {
    map<int, int> hmp1;
    map<int, bool> hmp2;

    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int j = 0; j < m; j++) {
      cin >> w[j];
    }

    for (int i = 0; i < n; i++) {
      s = h[i];
      if (hmp2[s]) {
        hmp1[s]++;
      } else {
        hmp2[s] = true;
        hmp1[s] = 1;
      }
      for (int j = i + 1; j < n; j++) {
        s += h[j];
        if (hmp2[s]) {
          hmp1[s]++;
        } else {
          hmp2[s] = true;
          hmp1[s] = 1;
        }
      }
    }

    t = 0;
    for (int i = 0; i < m; i++) {
      s = w[i];
      if (hmp2[s]) {
        t += hmp1[s];
      }
      for (int j = i + 1; j < m; j++) {
        s += w[j];
        if (hmp2[s]) {
          t += hmp1[s];
        }
      }
    }

    cout << t << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, m, h[1501], w[1501], s, t, u;

  while (cin >> n >> m && (n + m)) {
    t = 0;
    for (int i = 0; i < n; i++) {
      cin >> h[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> w[i];
    }

    for (int k = 0; k < n; k++) {
      u = 0;
      for (int i = k; i < n; i++) {
        s = 0;
        u += h[i];
        for (int j = 0; j < m; j++) {
          s += w[j];
          if (u == s) {
            t++;
            s = 0;
          } else if (u < s) {
            s = 0;
            if (u == w[j]) {
              t++;
            } else if (u > w[j]) {
              s = w[j];
            }
          }
        }
      }
    }

    cout << t << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {

  int N1, N2;

  cin >> N1;
  while (N1--) {
    int R;
    cin >> N2 >> R;
    int hm = 0;
    int y, x = 0;
    int n[100003] = {};
    for (int i = 0; i < N2; i++) {
      cin >> y;
      if (i > 0)
        n[i] = y - x;
      x = y;
    }
    /*
    for(int i=0;i<N2;i++)
            for(int j=N2-1;j>i;j--)
                    if(n[j]>n[i]){int t=n[j];n[j]=n[i];n[i]=t;}
            */
    sort(n, n + N2);
    long long int s = 0;
    int W = 0;
    for (int i = N2 - 1; i >= 0; i--) {
      if (W >= R - 1)
        s += n[i];
      W++;
    } // cout<<n[i]<<endl;W++;}
    cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {

  int N1, N2;

  cin >> N1;
  while (N1--) {
    int R;
    cin >> N2 >> R;
    int hm = 0;
    int y, x = 0;
    int n[100003] = {};
    for (int i = 0; i < N2; i++) {
      cin >> y;
      if (i > 0)
        n[i] = y - x;
      x = y;
    }

    for (int i = 0; i < N2; i++)
      for (int j = N2 - 1; j > i; j--)
        if (n[j] > n[i]) {
          int t = n[j];
          n[j] = n[i];
          n[i] = t;
        }

    long long int s = 0;
    for (int i = 0; i < N2 - 1; i++) {
      if (i >= R - 1)
        s += n[i];
    }
    cout << s << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

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

  int DataSetNum = 0;
  int HouseNum = 0;       // 家の数
  int GeneNum = 0;        // 発電機の数
  int *HousePos;          // 家の位置
  int *CableLength;       // 送電線長さ
  int AllCableLength = 0; // 全線距離

  cin >> DataSetNum; // データセット数取得
  for (int datanum = 0; datanum < DataSetNum; ++datanum) {
    // 各データ入力
    cin >> HouseNum;
    cin >> GeneNum;
    if (HouseNum <= GeneNum) { // 家より発電機が多ければ距離0
      int dummy = 0;           // データ読み捨て
      for (int i = 0; i < HouseNum; ++i) {
        cin >> dummy;
      }
      cout << 0 << endl;
      continue;
    }
    HousePos = new int[HouseNum];
    for (int i = 0; i < HouseNum; ++i) {
      cin >> HousePos[i];
    }
    CableLength = new int[HouseNum - 1];
    for (int i = 0; i < HouseNum - 1; ++i) {
      CableLength[i] = HousePos[i + 1] - HousePos[i];
    }
    AllCableLength = HousePos[HouseNum - 1] - HousePos[0]; // 全線距離

    // 距離ソート
    sort(&CableLength[0], &CableLength[0] + HouseNum - 1);
    // 長いほうから削る
    for (int i = 0; i < GeneNum - 1; ++i) {
      AllCableLength -= CableLength[HouseNum - i - 2];
      CableLength[HouseNum - i - 2] = 0;
    }
    // 出力
    cout << AllCableLength << endl;

    delete[] CableLength;
    delete[] HousePos;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

// 昇順挿入ソート(ALDS1_1_A)
void UpperSort(int DataNum, int *Data) {
  int v = 0, j = 0;
  for (int i = 1; i < DataNum; ++i) {
    v = Data[i]; // ソート
    j = i - 1;
    while (j >= 0 && Data[j] > v) {
      Data[j + 1] = Data[j];
      --j;
    }
    Data[j + 1] = v;
  }
}

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

  int DataSetNum = 0;
  int HouseNum = 0;       // 家の数
  int GeneNum = 0;        // 発電機の数
  int *HousePos;          // 家の位置
  int *CableLength;       // 送電線長さ
  int AllCableLength = 0; // 全線距離

  cin >> DataSetNum; // データセット数取得
  for (int datanum = 0; datanum < DataSetNum; ++datanum) {
    // 各データ入力
    cin >> HouseNum;
    cin >> GeneNum;
    if (HouseNum <= GeneNum) { // 家より発電機が多ければ距離0
      int dummy = 0;           // データ読み捨て
      for (int i = 0; i < HouseNum; ++i) {
        cin >> dummy;
      }
      cout << 0 << endl;
      continue;
    }
    HousePos = new int[HouseNum];
    for (int i = 0; i < HouseNum; ++i) {
      cin >> HousePos[i];
    }
    CableLength = new int[HouseNum - 1];
    for (int i = 0; i < HouseNum - 1; ++i) {
      CableLength[i] = HousePos[i + 1] - HousePos[i];
    }
    AllCableLength = HousePos[HouseNum - 1] - HousePos[0]; // 全線距離

    // 距離ソート
    UpperSort(HouseNum - 1, CableLength);
    // 長いほうから削る
    for (int i = 0; i < GeneNum - 1; ++i) {
      AllCableLength -= CableLength[HouseNum - i - 2];
      CableLength[HouseNum - i - 2] = 0;
    }
    // 出力
    cout << AllCableLength << endl;

    delete[] CableLength;
    delete[] HousePos;
  }

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



#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int T, N, K, X[1000000 + 10], A[1000000 + 10];
int main() {
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int sum = 0;
    cin >> N >> K;
    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);
    for (int j = 0; j < N - K; ++j) {
      sum = sum + A[j];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
int T, N, K, X[1000], A[1000];
int main() {
  cin >> T;
  for (int t = 0; t < T; ++t) {
    int sum = 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];
    }
    sort(A, A + N);
    for (int j = 0; j < N - K + 1; ++j) {
      sum = sum + A[j];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int l = 0; l < t; l++) {
    int n, k;
    cin >> n >> k;
    if (k < n) {
      int sum = 0;
      int home[100001];
      for (int j = 1; j <= n; j++) {
        cin >> home[j];
      }
      sum = home[n] - home[1];
      int dis[100000];
      for (int i = 1; i <= n - 1; i++) {
        dis[i] = home[i + 1] - home[i];
      }
      std::sort(dis + 1, dis + n, greater<int>());
      for (int i = 1; i <= k - 1; i++) {
        sum -= dis[i];
      }
      cout << sum << endl;
    } else {
      for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
      }
      cout << "0" << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int l = 0; l < t; l++) {
    int n, k;
    cin >> n >> k;
    if (k <= n) {
      cout << "n=" << n << endl;
      int sum = 0;
      int home[100001];
      for (int j = 1; j <= n; j++) {
        cin >> home[j];
      }
      sum = home[n] - home[1];
      int dis[100000];
      for (int i = 1; i <= n - 1; i++) {
        dis[i] = home[i + 1] - home[i];
      }
      std::sort(dis + 1, dis + n, greater<int>());
      for (int i = 1; i <= k - 1; i++) {
        sum -= dis[i];
      }
      cout << sum << endl;
    } else {
      cout << "0" << endl;
    }
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n);
    vector<int> y(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> x[j];
    }
    if (n <= k) {
      cout << 0 << endl;
      continue;
    }
    for (int j = 0; j < n - 1; j++) {
      y[j] = x[j + 1] - x[j];
    }
    sort(y.begin(), y.end());
    int ans = x[n - 1] - x[0];
    for (int j = 0; j < k - 1; j++) {
      ans -= y[n - j - 2];
    }
    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define MAX 500

using namespace std;

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n);
    vector<int> y(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> x[j];
    }
    /*if (n <= k) {
        cout << 0 << endl;
        continue;
    }*/
    for (int j = 0; j < n - 1; j++) {
      y[j] = x[j + 1] - x[j];
    }
    sort(y.begin(), y.end());
    int ans = x[n - 1] - x[0];
    for (int j = 0; j < k - 1; j++) {
      ans -= y[n - j - 2];
    }
    cout << ans << endl;
  }
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k, a[1000000];
vector<int> vec;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    vec.clear();
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int i = 0; i < n - 1; i++) {
      vec.push_back(a[i + 1] - a[i]);
    }
    sort(vec.begin(), vec.end());
    int sum = 0, cnt = 1;
    for (int i = vec.size() - 1; i >= 0; i--) {
      if (cnt >= k)
        sum += vec[i];
      cnt++;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, k, a[1000000];
vector<int> vec;
int main() {
  while (true) {
    vec.clear();
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int i = 0; i < n - 1; i++) {
      vec.push_back(a[i + 1] - a[i]);
    }
    sort(vec.begin(), vec.end());
    int sum = 0, cnt = 1;
    for (int i = vec.size() - 1; i >= 0; i--) {
      if (cnt >= k)
        sum += vec[i];
      cnt++;
    }
    cout << sum << endl;
  }
  return 0;
}