alexjercan/bugnet huggingface dataset:
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int t, n, k;
int house_position[100000 + 10];
int bitween_house[100000 + 10];
int min_long;

int main() {

  cin >> t;

  for (int i = 0; i < t; ++i) {

    cin >> n >> k;

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

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

    sort(bitween_house, bitween_house + n - 1);

    for (int i = 0; i < n - k; ++i) {
      min_long += bitween_house[i];
    }

    cout << min_long << endl;

    min_long = 0;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n, k;
int house_position[100000];
int bitween_house[100000];
int min_long;

int main() {

  while (cin >> n) {
    cin >> k;

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

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

    sort(bitween_house, bitween_house + n - 1);

    for (int i = 0; i < n - k - 2; ++i) {
      min_long += bitween_house[i];
    }

    cout << min_long << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

/* global variables */

int xs[MAX_N], dxs[MAX_N];

/* subroutines */

/* main */

int main() {
  int tn;
  cin >> tn;

  while (tn--) {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
      cin >> xs[i];
    sort(xs, xs + n);

    for (int i = 0; i < n - 1; i++)
      dxs[i] = xs[i + 1] - xs[i];
    sort(dxs, dxs + n - 1);

    int sum = 0;
    for (int i = 0; i < n - k; i++)
      sum += dxs[i];

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;

/* typedef */

/* global variables */

int xs[MAX_N], dxs[MAX_N];

/* subroutines */

/* main */

int main() {
  int tn;
  cin >> tn;

  while (tn--) {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
      cin >> xs[i];
    sort(xs, xs + n);

    for (int i = 0; i < n - 1; i++)
      dxs[i] = xs[i + 1] - xs[i];
    sort(dxs, dxs + n - 1);

    int sum = 0;
    for (int i = 0; i < n - k; i++)
      sum += dxs[i];

    cout << sum << endl;
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MP make_pair
#define PB push_back
#define ALL(a) (a).begin(), (a).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
int main() {
  int times;
  cin >> times;
  REP(i, times) {
    int n, k;
    cin >> n >> k;
    int x[100100];
    REP(j, n) cin >> x[j];
    vector<int> v;
    REP(j, n - 1) { v.PB(x[j + 1] - x[j]); }
    sort(ALL(v));
    reverse(ALL(v));
    int btw = x[n - 1] - x[0];
    REP(j, min(n - 1, k - 1)) { btw -= v[j]; }
    cout << max(0, btw) << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MP make_pair
#define PB push_back
#define ALL(a) (a).begin(), (a).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
int main() {
  int times;
  cin >> times;
  REP(i, times) {
    int n, k;
    cin >> n >> k;
    int x[100100];
    REP(j, n) cin >> x[j];
    vector<int> v;
    REP(j, n - 1) { v.PB(x[j + 1] - x[j]); }
    sort(ALL(v));
    reverse(ALL(v));
    int btw = x[n - 1] - x[0];
    REP(j, k - 1) { btw -= v[j]; }
    cout << max(0, btw) << endl;
  }
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

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

long long n, k, m, x[100010], d[100010], result, l = 0;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    result = 0;
    cin >> n >> k;
    for (int j = 0; j < n; j++) {
      cin >> x[j];
    }
    for (int j = 0; j + 1 < n; j++) {
      d[j] = x[j + 1] - x[j];
    }
    sort(d, d + n - 1);
    m = max(l, n - k);

    for (int j = 0; j < m; j++)
      result += d[j];
    cout << result << endl;
  }
}

Error: WA

Fixed submission:

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

long long n, k, m, x[100010], d[100010], result, l = 0;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    for (int j = 0; j < n - 1; j++) {
      cin >> x[j];
      if (j >= 1)
        d[j - 1] = x[j] - x[j - 1];
    }
    sort(d, d + n - 1);
    m = max(l, n - k);
    for (int j = 0; j < m; j++)
      result += d[j];
    cout << result << endl;
  }
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int A[200000];
int B[200000];

int deleteNmin(int n, int k) {
  int all = A[n - 1] - A[0];
  int j = 0;
  for (int i = n - 1; i > 0; i--) {
    B[j++] = A[i] - A[i - 1];
  }
  sort(B, B + j);
  if (k >= n)
    return 0;
  if (k > 1)
    for (int i = 1; i < k; i++) {
      all -= B[j - i];
    }

  return all;
}

int main() {
  int times;
  int n;
  int k;
  cin >> times;
  while (times--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> A[i];

    int ans = deleteNmin(n, k);

    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int A[20000];
int B[20000];

int deleteNmin(int n, int k) {
  int all = A[n - 1] - A[0];
  int j = 0;
  for (int i = n - 1; i > 0; i--) {
    B[j++] = A[i] - A[i - 1];
  }
  sort(B, B + j);
  if (k >= n)
    return 0;
  if (k > 1)
    for (int i = 1; i < k; i++) {
      all -= B[j - i];
    }

  return all;
}

int main() {
  int times;
  int n;
  int k;
  cin >> times;
  while (times--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
      cin >> A[i];

    int ans = deleteNmin(n, k);
  }
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int i, j, t, n, m, x, y, sum;
  vector<int> data;

  cin >> t;

  for (i = 0; i < t; ++i) {
    cin >> n >> m;

    for (j = 0; j < n; ++j) {
      cin >> x;
      if (j != 0)
        data.push_back(x - y);
      y = x;
    }

    sum = 0;
    if (n > m) {
      sort(data.begin(), data.end());
      for (j = 0; n > m; ++j, --n)
        sum += data[j];
    }

    cout << sum << endl;
    data.clear();
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int i, j, t, n, m, x, y, min, sum;
  vector<int> data;

  cin >> t;

  for (i = 0; i < t; ++i) {
    cin >> n >> m;

    for (j = 0; j < n; ++j) {
      cin >> x;
      if (j != 0)
        data.push_back(x - y);
      y = x;
    }

    sum = 0;
    while (n > m) {
      for (j = 1, min = 0; j < data.size(); ++j) {
        if (data[min] > data[j])
          min = j;
      }
      sum += data[min];
      data.erase(data.begin() + min);
      --n;
    }

    cout << sum << endl;
    data.clear();
  }

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  int t;
  std::cin >> t;
  std::vector<int> space;
  space.reserve(100000);

  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    if (k >= n) {
      // テゥツ?ゥテ・ツスツ禿」ツ?ォテ、ツクツ?・ツ崢榲・ツ?・テ・ツ環崚」ツ?凖」ツつ?
      for (int j = 0; j < n; ++j)
        std::cin >> k;
      std::cout << 0 << std::endl;
      continue;
    }

    space.resize(n - 1);
    int before_pos, road_size, first_pos;
    std::cin >> before_pos;
    first_pos = before_pos;
    for (int j = 1; j < n; ++j) {
      int new_pos;
      std::cin >> new_pos;

      if (j == n - 1)
        road_size = new_pos;

      space[j - 1] = new_pos - before_pos;
      before_pos = new_pos;
    }

    std::sort(space.begin(), space.end(), std::greater<int>());

    std::cout << road_size - first_pos -
                     std::accumulate(space.begin(), space.begin() + k - 1, 0)
              << std::endl;

    space.clear();
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  int t;
  std::cin >> t;
  std::vector<int> space;
  space.reserve(100000);

  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    if (k >= n) {
      // テゥツ?ゥテ・ツスツ禿」ツ?ォテ、ツクツ?・ツ崢榲・ツ?・テ・ツ環崚」ツ?凖」ツつ?
      std::cin >> n;
      std::cout << 0 << std::endl;
      continue;
    }

    space.resize(n - 1);
    int before_pos, road_size, first_pos;
    std::cin >> before_pos;
    first_pos = before_pos;
    for (int j = 1; j < n; ++j) {
      int new_pos;
      std::cin >> new_pos;

      if (j == n - 1)
        road_size = new_pos;

      space[j - 1] = new_pos - before_pos;
      before_pos = new_pos;
    }

    std::sort(space.begin(), space.end(), std::greater<int>());

    std::cout << road_size - first_pos -
                     std::accumulate(space.begin(), space.begin() + k - 1, 0)
              << std::endl;

    space.clear();
  }
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    int x[n], gap[n - 1];
    for (int j = 0; j < n; j++)
      cin >> x[j];
    for (int j = 0; j < n - 1; j++)
      gap[j] = x[j + 1] - x[j];
    sort(&gap[0], &gap[n - 1]);
    int sum = 0;
    if ((n - 1) - (k - 1) > 0) {
      for (int j = 0; j < (n - 1) - (k - 1); j++)
        sum += gap[j];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; t++) {
    int n, k;
    cin >> n >> k;
    int x[n], gap[n - 1];
    for (int j = 0; j < n; j++)
      cin >> x[j];
    for (int j = 0; j < n - 1; j++)
      gap[j] = x[j + 1] - x[j];
    sort(&gap[0], &gap[n - 1]);
    int sum = 0;
    if ((n - 1) - (k - 1) > 0) {
      for (int j = 0; j < (n - 1) - (k - 1); j++)
        sum += gap[j];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    int sum = 0;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> b(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> a.at(j);
    }
    for (int m = 0; m < n - 1; m++) {
      b.at(m) = a.at(m + 1) - a.at(m);
    }
    sort(b.begin(), b.end());
    int c = n - k;
    for (int l = 0; l < c; l++) {
      sum += b.at(l);
    }
    cout << sum << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 2)


Fixed submission:

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    int sum = 0;
    cin >> n >> k;
    vector<int> a(n);
    vector<int> b(n - 1);
    for (int j = 0; j < n; j++) {
      cin >> a.at(j);
    }
    for (int m = 0; m < n - 1; m++) {
      b.at(m) = a.at(i + 1) - a.at(i);
    }
    sort(b.begin(), b.end());
    int c = n - k;
    for (int l = 0; l < c; l++) {
      sum += b.at(l);
      cout << sum << endl;
    }
  }
}
Problem p01227 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int n, k;
    cin >> n >> k;
    int dist;
    int aida[n - 1];
    int bfrD;
    for (int i = 0; i < n; i++) {
      int d;
      cin >> d;
      if (i == 0)
        dist = d;
      if (i + 1 == n)
        dist = d - dist;

      if (i != 0) {
        aida[i - 1] = d - bfrD;
      }

      bfrD = d;
    }

    if (k >= n)
      cout << 0 << endl;
    else {
      sort(aida, &aida[n - 1]);
      for (int i = 0; i < k - 1; i++) {
        dist -= aida[n - 2 - i];
      }

      cout << dist << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int k;
int n;
int m;

void rec(int dist[], int nk, int i, int l) {
  if (nk == k - 1 && i == n - 1) {
    if (l > m)
      m = l;
    return;
  }
  for (int j = i; j + 1 < n; j++) {
    if (nk != k - 1)
      rec(dist, nk + 1, j + 1, l + (dist[j + 1] - dist[j]));
    rec(dist, nk, j + 1, l);
  }
}

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    cin >> n >> k;
    int dist[n];
    for (int i = 0; i < n; i++) {
      cin >> dist[i];
    }

    int ans;
    if (k >= n) {
      ans = 0;
    } else {
      m = -1;
      rec(dist, 0, 0, 0);
      ans = dist[n - 1] - dist[0] - m;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int set, i, j, p, q, n, k, input[100100], st, en, sa[100100], cont;

  scanf("%d", &set);
  for (q = 0; q < set; q++) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++) {
      scanf("%d", &input[i]);
    }
    st = input[0];
    en = input[n - 1];
    cont = en - st;
    for (i = 1; i < n; i++) {
      sa[i - 1] = input[i] - input[i - 1];
    }
    sort(sa, sa + n - 1, greater<int>());
    for (i = 0; i < k - 1; i++) {
      cont -= sa[i];
    }
    if (cont < 0) {
      cont = 0;
    }
    printf("%d\\n", cont);
  }
}

Error: 0

Fixed submission:

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

int main() {
  int set, i, j, p, q, n, k, input[10010], st, en, sa[10010], cont;

  scanf("%d", &set);
  for (q = 0; q < set; q++) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++) {
      scanf("%d", &input[i]);
    }
    st = input[0];
    en = input[n - 1];
    cont = en - st;
    for (i = 1; i < n; i++) {
      sa[i - 1] = input[i] - input[i - 1];
    }
    sort(sa, sa + n - 1, greater<int>());
    for (i = 0; i < k - 1; i++) {
      cont -= sa[i];
    }
    if (cont < 0) {
      cont = 0;
    }
    printf("%d\\n", cont);
  }
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  int t;
  std::cin >> t;
  std::vector<int> v(t);
  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> x(n);
    int denki = 0;
    for (int j = 0; j < n; ++j) {
      std::cin >> x[j];
    }
    if (n > k) {
      std::vector<int> l(n - 1);
      for (int j = 0; j < n - 1; ++j) {
        l[j] = x[j + 1] - x[j];
      }
      std::sort(l.begin(), l.end());
      for (int j = n - 2; j > n - 1 - k; --j) {
        denki += l[j];
      }
      v[i] = x[n - 1] - x[0] - denki;
    } else {
      v[i] = 0;
    }
  }
  for (int i = 0; i < t; ++i) {
    std::cout << v[i] << std::endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  int t;
  std::cin >> t;
  std::vector<int> v(t);
  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> x(n);
    int denki = 0;
    for (int j = 0; j < n; ++j) {
      std::cin >> x[j];
    }
    if (n > k) {
      std::vector<int> l(n - 1);
      for (int j = 0; j < n; ++j) {
        l[j] = -x[j + 1] - x[j];
      }
      std::sort(l.begin(), l.end());
      for (int j = 0; j < k; ++j) {
        denki += x[j];
      }
      v[i] = denki;
    } else {
      v[i] = 0;
    }
  }
  for (int i = 0; i < t; ++i) {
    std::cout << v[i] << std::endl;
  }
  return 0;
}
Problem p01227 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

void do_country(int &n, int &k, vector<int> &x);

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x;
    for (int j = 0; j < n; j++) {
      int input_x;
      cin >> input_x;
      x.push_back(input_x);
    }
    do_country(n, k, x);
  }
}

void do_country(int &n, int &k, vector<int> &x) {
  sort(x.begin(), x.end());
  // reverse(x.begin(), x.end());
  // cout << x.at(0) << x.at(n - 1) << endl;
  int ans = x[n - 1] - x[0];
  vector<int> distance;
  for (int i = 0; i < n - 1; i++) {
    distance.push_back(x[n - 1 - i] - x[n - 2 - i]);
  }
  sort(distance.begin(), distance.end());
  // reverse(distance.begin(), distance.end());
  // cout << ans << endl;
  for (int i = 1; i < (n > k ? k : n); i++) {
    ans -= distance[n - i - 1];
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

using namespace std;

void do_country(int &n, int &k, vector<int> &x);

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    vector<int> x;
    for (int j = 0; j < n; j++) {
      int input_x;
      cin >> input_x;
      x.push_back(input_x);
    }
    do_country(n, k, x);
  }
}

void do_country(int &n, int &k, vector<int> &x) {
  sort(x.begin(), x.end());
  reverse(x.begin(), x.end());
  // cout << x.at(0) << x.at(n - 1) << endl;
  int ans = x[0] - x[n - 1];
  vector<int> distance;
  for (int i = 0; i < n - 1; i++) {
    distance.push_back(x[i] - x[i + 1]);
  }
  sort(distance.begin(), distance.end());
  reverse(distance.begin(), distance.end());
  // cout << ans << endl;
  for (int i = 1; i < k; i++) {
    ans -= distance[i - 1];
  }
  cout << ans << endl;
}
Problem p01981 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

void solve() {
  int y, m, d;
  cin >> y >> m >> d;
  vector<int> h = {31, 4, 30};
  if (h < vector<int>{y, m, d}) {
    cout << "? " << y - 30 << " " << m << " " << d << endl;
  } else
    cout << "HEISEI " << y << " " << m << " " << d << endl;
}
int main() {
  string s;
  while (cin >> s, s != "#")
    solve();
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int num(string s, int &i);
int calc(string s, int &i);
int x;

bool isnum(char c) {
  int y = c - 'a';
  return (y >= 0) && (y <= 3);
}
int calc(string s, int &i) {
  if (isnum(s[i]))
    return num(s, i);
  i++;
  char op = s[i];
  i++;
  int ret = calc(s, i);
  int res = calc(s, i);
  i++;
  if (op == '+')
    return ret | res;
  if (op == '*')
    return ret & res;
  if (op == '^')
    return ret ^ res;
  return 0;
}

int num(string s, int &i) {
  i++;
  if (s[i - 1] == 'a')
    return x / 1000;
  if (s[i - 1] == 'b')
    return (x / 100) % 10;
  if (s[i - 1] == 'c')
    return (x / 10) % 10;
  if (s[i - 1] == 'd')
    return x % 10;
  return 0;
}
int main() {
  string s;
  while (cin >> s, s != ".") {
    map<int, int> mp;
    rep(i, 10000) {
      x = i;
      int j = 0;
      mp[calc(s, j)]++;
    }
    int n;
    cin >> n;
    x = n;
    int j = 0;
    int ans = calc(s, j);
    cout << ans << " " << mp[ans] << endl;
  }
  return 0;
}
Problem p02099 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, o, n) for (int i = o; i < n; ++i)
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define all(X) X.begin(), X.end()

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

int main() {
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<pair<string, int>> ps(n);
  rep(i, n) cin >> ps[i].X, ps[i].Y = i;
  sort(all(ps));
  vector<int> re(n);
  for (auto p : ps) {
    int l = lower_bound(all(ps), make_pair(p.X, 0)) - ps.begin();
    int r = lower_bound(all(ps), make_pair(p.X, n)) - ps.begin();
    re[p.Y] = l * 2 + r - 1;
  }
  rep(i, n) cout << re[i] << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, i, j;
  double a[112345] = {};
  int p[112345] = {};
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++)
      if (a[i] > a[j])
        p[i] += 3;
      else if (a[i] < a[j])
        p[j] += 3;
      else {
        p[i]++;
        p[j]++;
      }
  for (i = 0; i < n; i++)
    cout << p[i] << endl;
}
Problem p02099 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  double gpa1[100000], list1[4001] = {};
  int N, win[100000] = {}, list2[4001] = {}, gpa2[100000];
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lf", &gpa1[i]);
    //	cout << gpa1[i]<<"    ";
    gpa1[i] += 0.0001;
    gpa2[i] = gpa1[i] * 1000;
    //	cout << gpa2[i] << "\\n";
  }
  for (int i = 0; i < N; i++) {
    if (list1[gpa2[i]] == 1) {
      win[i] = list2[gpa2[i]];
    } else {
      for (int j = 0; j < N; j++) {
        if (gpa2[i] > gpa2[j])
          win[i] += 3;
        else if (gpa2[i] == gpa2[j])
          win[i]++;
      }

      list1[gpa2[i]] = 1;
      list2[gpa2[i]] = win[i];
    }
    cout << (win[i] - 1) << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  double gpa[100000];
  int N, win[100000] = {};
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> gpa[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (gpa[i] > gpa[j])
        win[i] += 3;
      else if (gpa[i] == gpa[j])
        win[i]++;
    }
    cout << (win[i] - 1) << "\\n";
  }
  return 0;
}