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

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int r, c, q;

#define MAX_R 50000
#define MAX_C 50000

struct Q {
  int t;
  int index;
  int order;
  Q(int t, int index, int order) : t(t), index(index), order(order) {}
};
int main() {
  while (cin >> r >> c >> q) {
    if (r == 0 && c == 0 && q == 0)
      break;
    vector<Q> qs;
    for (int i = 0; i < q; i++) {
      int A, B, order;
      cin >> A >> B >> order;
      qs.push_back(Q(A, B, order));
    }

    bool dupR[MAX_R];
    bool dupC[MAX_C];
    memset(dupR, 0, sizeof(dupR));
    memset(dupC, 0, sizeof(dupC));
    int cnt = 0;
    vector<Q>::reverse_iterator ri;
    for (ri = qs.rbegin(); ri != qs.rend(); ri++) {
      if (ri->t == 1) {
        if (dupC[ri->index])
          continue;
        if (ri->order == 1) {
          cnt += r;
          c--;
        } else {
          c--;
        }
        dupC[ri->index] = true;
      } else {
        if (dupR[ri->index])
          continue;
        if (ri->order == 1) {
          cnt += c;
          r--;
        } else {
          r--;
        }
        dupR[ri->index] = true;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const bool debug = false;

int r, c, q;
vector<vector<bool>> T;
int main() {
  while (cin >> r >> c >> q) {
    if (debug)
      cout << r << ' ' << c << ' ' << q << endl;
    if (r == 0 && c == 0 && q == 0)
      break;
    T.clear();
    T.resize(r);
    for (int i = 0; i < r; i++) {
      T[i].clear();
      T[i].resize(c, 0);
    }

    int A, B, order;
    for (int i = 0; i < q; i++) {
      cin >> A >> B >> order;
      if (A == 0) {
        int y = B;
        for (int i = 0; i < c; i++) {
          T[y][i] = order;
        }
      } else {
        int x = B;
        for (int i = 0; i < r; i++) {
          T[i][x] = order;
        }
      }
    }

    int cnt = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        if (T[i][j] == 1)
          cnt++;
      }
    }

    cout << cnt << endl;

    if (debug) {
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
          cout << T[i][j] << ' ';
        }
        cout << endl;
      }
    }
  }
  return 0;
}
Problem p00664 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 50001
using namespace std;
typedef long long ll;

int main() {
  while (1) {
    ll r, c, q;
    cin >> r >> c >> q;
    if (!r && !c && !q)
      return 0;

    ll A[N], B[N], O[N];
    for (int i = 0; i < q; i++)
      cin >> A[i] >> B[i] >> O[i];

    ll ans = 0, cnt[2] = {}, rc[] = {r, c}, used[2][N] = {};
    for (int i = q - 1; i >= 0; i--) {
      ll f = A[i], RC = B[i];
      if (used[f][RC]++)
        continue;
      cnt[f]++;
      ans += (rc[!f] - cnt[!f]) * O[i];
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 50001
using namespace std;
typedef long long ll;

int main() {
  while (1) {
    ll r, c, q;
    cin >> r >> c >> q;
    if (!r && !c && !q)
      return 0;

    ll A[N], B[N], O[N];
    for (int i = 0; i < q; i++)
      cin >> A[i] >> B[i] >> O[i];

    ll ans = 0, cnt[2] = {}, rc[] = {r, c}, used[2][N] = {};
    for (int i = q - 1; i >= 0; i--) {
      ll f = A[i], RC = B[i];
      if (used[f][RC]++)
        continue;
      cnt[f]++;
      assert(rc[f] - cnt[!f] >= 0);
      ans += (rc[f] - cnt[!f]) * O[i];
    }
    cout << ans << endl;
  }

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

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

class chara {
public:
  string name;
  int vote;
  bool operator<(const chara &x) const {
    return (vote == x.vote) ? (name < x.name) : (vote > x.vote);
  }
};

int main() {
  int n, m, k, l;
  chara c[100000];

  while (scanf("%d %d %d %d", &n, &m, &k, &l) && (n || m || k || l)) {
    char tmp[12];
    int x;
    for (int i = 0; i < n; i++) {
      scanf("%s %d", tmp, &x);
      c[i].name = tmp;
      c[i].vote = x;
    }

    sort(c, c + n);

    map<string, bool> fav;
    for (int i = 0; i < m; i++) {
      scanf("%s", tmp);
      string s = tmp;
      fav[s] = true;
    }

    int ans = 0;
    int pos = k - 1;
    int now = k;
    for (;;) {
      while (0 <= pos && fav[c[pos].name])
        pos--;
      if (pos < 0)
        break;

      while (now < n && !fav[c[now].name])
        now++;
      if (now == n)
        break;

      swap(c[now], c[k]);
      for (int i = pos; i < k; i++) {
        l -= c[i].vote - c[i + 1].vote;
        c[i + 1].vote = c[i].vote;
        if (c[i].name < c[i + 1].name) {
          l--;
          c[i + 1].vote++;
        }
        if (l < 0)
          break;
        swap(c[i], c[i + 1]);
      }
      if (l < 0)
        break;
    }

    for (int i = 0; i < k; i++)
      if (fav[c[i].name])
        ans++;

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

Error: 0

Fixed submission:

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

class chara {
public:
  string name;
  int vote;
  bool operator<(const chara &x) const {
    return (vote == x.vote) ? name < x.name : vote > x.vote;
  }
};

int main() {
  int n, m, k, l;
  chara c[100000];

  while (scanf("%d %d %d %d", &n, &m, &k, &l) && (n || m || k || l)) {
    char tmp[12];
    int x;
    for (int i = 0; i < n; i++) {
      scanf("%s %d", tmp, &x);
      c[i].name = tmp;
      c[i].vote = x;
    }

    sort(c, c + n);

    map<string, bool> fav;
    for (int i = 0; i < m; i++) {
      scanf("%s", tmp);
      string s = tmp;
      fav[s] = true;
    }

    int ans = 0;
    int pos = k - 1;
    int now = k;
    while (0 <= pos) {
      while (fav[c[pos].name])
        pos--;
      while (now < n && !fav[c[now].name])
        now++;
      if (now == n)
        break;
      swap(c[now], c[k]);
      for (int i = pos; i < k; i++) {
        l -= c[i].vote - c[i + 1].vote;
        c[i + 1].vote = c[i].vote;
        if (c[i].name < c[i + 1].name) {
          l--;
          c[i + 1].vote++;
        }
        if (l < 0)
          break;
        swap(c[i], c[i + 1]);
      }
      if (l < 0)
        break;
    }
    for (int i = 0; i < k; i++)
      if (fav[c[i].name])
        ans++;

    printf("%d\\n", ans);
  }
}
Problem p00667 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

#define N 100000
#define M 100000007

long long int dp[N + 2][2] = {0};

void init() {
  int loop[2] = {5, 3};
  for (int t = 0; t < 2; t++) {
    dp[0][t] = 1;
    dp[1][t] = 1;
    for (int i = 2; i <= loop[t]; i++)
      dp[i][t] = dp[i - 1][t] * 2;
    for (int i = loop[t] + 1; i < N + 2; i++) {
      dp[i][t] = 1;
      for (int j = 1; j <= loop[t]; j++) {
        dp[i][t] += dp[i - j][t];
        dp[i][t] %= M;
      }
    }
  }
  return;
}

int main() {
  string s;
  long long int ans;
  int i, cnt;
  init();
  while (cin >> s, s != "#") {
    ans = 1;
    cnt = 1;
    for (i = 1; i < s.size(); i++) {
      if (s[i - 1] == s[i]) {
        cnt++;
      } else {
        int t;
        if (s[i - 1] == '8' || s[i - 1] == '0')
          t = 1;
        else
          t = 0;
        ans *= dp[cnt][t];
        ans %= M;
        cnt = 1;
      }
    }
    i = s.size() - 1;
    if (s[i - 1] == s[i]) {
      int t;
      if (s[i - 1] == '8' || s[i - 1] == '0')
        t = 1;
      else
        t = 0;
      ans *= dp[cnt][t];
      ans %= M;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

void rec(int n, string s, int l);

int cnt = 0;
map<string, bool> m;
int count(int n, int l) {
  cnt = 0;
  m.clear();
  rec(n, "", l);
  return cnt;
}
void rec(int n, string s, int l) {
  if (m.count(s))
    return;
  cnt = (cnt + 1) % 100000007;
  m[s] = true;
  for (int i = 1; i < n; i++) {
    char c = '0' + (i % l);
    rec(n - i, s + c, l);
  }
  return;
}

int main() {
  long long int ans;
  int c, l;
  string s;
  int i, j;
  while (cin >> s, s != "#") {
    ans = 1;
    c = 1;
    for (i = 1; i < s.size(); i++) {
      if (s[i - 1] == s[i]) {
        c++;
      } else {
        if (s[i - 1] == '0' || s[i - 1] == '8')
          l = 3;
        else
          l = 5;
        ans *= count(c, l);
        c = 1;
      }
    }
    if (s[s.size() - 1] == '0' || s[s.size() - 1] == '8')
      l = 3;
    else
      l = 5;
    if (s[s.size() - 1] == s[s.size() - 2])
      ans *= count(c, l);
    cout << (ans % 100000007) << endl;
  }
}
Problem p00668 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <map>

#define MAX_Q (400000)

using namespace std;

typedef long long lint;

int bit[MAX_Q + 1];
lint val[MAX_Q + 1];

void add(int pos, int val) {
  while (pos <= MAX_Q) {
    bit[pos] += val;
    pos += pos & -pos;
  }
}

int sum(int pos) {
  int ret = 0;

  while (pos) {
    ret += bit[pos];
    pos &= (pos - 1);
  }

  return (ret);
}

void print(int x) {
  int l = 0, r = MAX_Q;

  while (l != r) {
    int center = (l + r) / 2;

    if (sum(center) < x) {
      l = center + 1;
    } else {
      r = center;
    }
  }

  printf("%lld\\n", val[r]);
}

void erase(int x) {
  int l = 0, r = MAX_Q;

  while (l != r) {
    int center = (l + r) / 2;

    if (sum(center) < x) {
      l = center + 1;
    } else {
      r = center;
    }
  }

  add(r, -1);
}

int main() {
  int q, lim;

  while (scanf("%d %d", &q, &lim), lim) {
    memset(bit, 0, sizeof(bit));
    memset(val, 0, sizeof(val));

    map<lint, int> pos;

    int p = 1;

    while (q--) {
      int qtype, x;
      scanf("%d %lld", &qtype, &x);

      if (qtype == 0) {
        add(p, 1);
        pos[x] = p;
        val[p++] = x;
        if (sum(MAX_Q) > lim) {
          erase(1);
        }
      }
      if (qtype == 1) {
        erase(x);
      }
      if (qtype == 2) {
        print(x);
      }
      if (qtype == 3) {
        add(pos[x], -1);
      }
    }

    printf("end\\n");
  }

  return (0);
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <map>

#define MAX_Q (400000)

using namespace std;

typedef long long lint;

int bit[MAX_Q + 1];

void add(int pos, int val) {
  while (pos <= MAX_Q) {
    bit[pos] += val;
    pos += pos & -pos;
  }
}

int sum(int pos) {
  int ret = 0;

  while (pos) {
    ret += bit[pos];
    pos &= (pos - 1);
  }

  return (ret);
}

void print(int x, map<int, lint> &val) {
  int l = 0, r = MAX_Q;

  while (l != r) {
    int center = (l + r) / 2;

    if (sum(center) < x) {
      l = center + 1;
    } else {
      r = center;
    }
  }

  printf("%lld\\n", val[r]);
}

void erase(int x) {
  int l = 0, r = MAX_Q;

  while (l != r) {
    int center = (l + r) / 2;

    if (sum(center) < x) {
      l = center + 1;
    } else {
      r = center;
    }
  }

  add(r, -1);
}

int main() {
  int q, lim;

  while (scanf("%d %d", &q, &lim), lim) {
    memset(bit, 0, sizeof(bit));
    map<lint, int> pos;
    map<int, lint> val;

    int p = 1;

    while (q--) {
      int qtype, x;
      scanf("%d %lld", &qtype, &x);

      if (qtype == 0) {
        add(p, 1);
        pos[x] = p;
        val[p++] = x;
        if (sum(MAX_Q) > lim) {
          erase(1);
        }
      }
      if (qtype == 1) {
        erase(x);
      }
      if (qtype == 2) {
        print(x, val);
      }
      if (qtype == 3) {
        add(pos[x], -1);
      }
    }

    printf("end\\n");
  }

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

#include <iostream>
#include <map>

using namespace std;

const int MAX_N = 1 << 19;

int n, dat[2 * MAX_N - 1];
int num[MAX_N];

int at(int x, int k = 0) {
  if (k - (MAX_N - 1) >= 0)
    return k - (MAX_N - 1);
  if (x <= dat[k * 2 + 1])
    return at(x, k * 2 + 1);
  return at(x - dat[k * 2 + 1], k * 2 + 2);
}

void update(int k, int a) {
  k += MAX_N - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
  }
}

int main() {
  for (int q, lim; cin >> q >> lim, q | lim;) {
    fill(begin(dat), end(dat), 0);
    int serial = 0;
    int size = 0;
    map<int, int> m;
    while (q--) {
      int qi, x;
      cin >> qi >> x;
      if (qi == 0) {
        update(serial, 1);
        num[serial] = x;
        m[x] = serial;
        serial++;
        size++;
        if (size > lim) {
          update(at(1), 0);
          size--;
        }
      } else if (qi == 1) {
        update(at(x), 0);
        size--;
      } else if (qi == 2) {
        cout << num[at(x)] << endl;
      } else {
        update(m[x], 0);
        size--;
      }
    }
    cout << "end" << endl;
  }
}

Error: TLE

Fixed submission:

// 24
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

struct P {
  int n, x;
  bool operator<(P a) const { return x < a.x; }
};

int main() {
  for (int q, l; cin >> q >> l, q | l;) {
    vector<P> deq;
    map<int, int> m;
    for (int i = 0; i < q; i++) {
      int qu, x;
      cin >> qu >> x;
      if (qu == 0) {
        P p = {x, i};
        deq.push_back(p);
        m[x] = i;
        if (deq.size() > l) {
          m.erase(deq[0].n);
          deq.erase(deq.begin());
        }
      } else if (qu == 1) {
        x--;
        m.erase(deq[x].n);
        deq.erase(deq.begin() + x);
      } else if (qu == 2) {
        cout << deq[x - 1].n << endl;
      } else {
        P s = {0, m[x]};
        vector<P>::iterator d = lower_bound(deq.begin(), deq.end(), s);
        m.erase(d->n);
        deq.erase(d);
      }
    }
    cout << "end" << endl;
  }
  return 0;
}
Problem p00668 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int q, lim, query, x;

  while (1) {
    deque<int> l;
    cin >> q >> lim;
    if (!q && !lim)
      break;
    for (int i = 0; i < q; i++) {
      cin >> query >> x;

      if (query == 0) {
        l.push_back(x);
        if ((int)l.size() > lim)
          l.pop_front();
      }
      if (query == 1)
        l.erase(l.begin() + x - 1);
      if (query == 2)
        cout << *(l.begin() + x - 1) << endl;
      if (query == 3)
        l.erase(find(l.begin(), l.end(), x));
    }
    cout << "end" << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int q, lim, query, x;

  while (1) {
    deque<int> l;
    cin >> q >> lim;
    if (!q && !lim)
      break;
    for (int i = 0; i < q; i++) {
      cin >> query >> x;

      if (query == 0) {
        l.push_back(x);
        if ((int)l.size() > lim)
          l.pop_front();
      }
      if (query == 1)
        l.erase(l.begin() + x - 1);
      if (query == 2)
        cout << *(l.begin() + x - 1) << endl;
      if (query == 3) {
        for (int j = 0; j < (int)l.size(); j++) {
          if (*(l.begin() + j) == x)
            l.erase(l.begin() + j);
        }
      }
    }
    cout << "end" << endl;
  }
}
Problem p00669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int Max(int n, int K, int c[]) {
  int k, l, CK, M = 0;
  for (k = 0; k < n - K + 1; k++) {
    CK = 1;
    for (l = k; l < k + K; l++) {
      CK *= c[l];
    }
    if (CK > M)
      M = CK;
  }
  return M;
}

int main() {
  int n, K, M1, M2, tmp, cnt = 0;
  int i, j;
  int c[100];
  while (cnt < 100) {
    cnt++;
    cin >> n >> K;
    if (n == 0 && K == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> c[i];
    M1 = 0;
    M2 = 0;
    if (M1 < Max(n, K, c))
      M1 = Max(n, K, c);
    for (i = 0; i < n - 1; i++) {
      for (j = i + 1; j < n; j++) {
        tmp = c[i];
        c[i] = c[j];
        c[j] = tmp;
        if (M2 < Max(n, K, c))
          M2 = Max(n, K, c);
        tmp = c[i];
        c[i] = c[j];
        c[j] = tmp;
      }
    }
    if (M1 <= M2)
      cout << M2 - M1 << endl;
    else
      cout << "NO GAME" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, K, c[100], CK, M1, M2, tmp, CK2, cnt = 0;
  int i, j, k, l;
  while (cnt < 100) {
    cnt++;
    cin >> n >> K;
    if (n == 0 && K == 0)
      break;
    for (i = 0; i < n; i++)
      cin >> c[i];
    M1 = 0;
    M2 = 0;
    for (i = 0; i < n - K + 1; i++) {
      CK = 1;
      for (j = i; j < i + K; j++) {
        CK *= c[j];
      }
      if (CK > M1)
        M1 = CK;
    }
    for (i = 0; i < n - 1; i++) {
      for (j = i + 1; j < n; j++) {
        tmp = c[i];
        c[i] = c[j];
        c[j] = tmp;
        for (k = 0; k < n - K + 1; k++) {
          CK2 = 1;
          for (l = k; l < k + K; l++) {
            CK2 *= c[l];
          }
          if (CK2 > M2)
            M2 = CK2;
          cout << "M2：" << M2 << endl;
        }
        tmp = c[i];
        c[i] = c[j];
        c[j] = tmp;
      }
    }

    if (M1 <= M2)
      cout << M2 - M1 << endl;
    else
      cout << "NO GAME" << endl;
  }
  return 0;
}
Problem p00669 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef long long int LLI;

void swap(vector<LLI> &vs, int i, int j) {
  int val = vs[i];
  vs[i] = vs[j];
  vs[j] = val;
}

void print(vector<LLI> vs) {
  cerr << "[";
  for (LLI val : vs) {
    cerr << val << ", ";
  }
  cerr << "]" << endl;
}

int calc(vector<LLI> &vs, int k, int n) {
  LLI prod = 1;
  for (int i = 0; i < k; i++) {
    prod *= vs[i];
  }
  LLI ans = prod;
  for (int i = k; i < n; i++) {
    prod /= vs[i - k];
    prod *= vs[i];
    ans = max(prod, ans);
  }
  return ans;
}

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0 and k == 0)
      break;
    vector<LLI> vs(n);
    for (int i = 0; i < n; i++) {
      cin >> vs[i];
    }
    int init_score = calc(vs, k, n);
    int max_score = -10000;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        swap(vs, i, j);
        int score = calc(vs, k, n);
        max_score = max(max_score, score);
        swap(vs, i, j);
      }
    }
    if (max_score < init_score) {
      cout << "NO GAME" << endl;
    } else {
      cout << max_score - init_score << endl;
    }
  }
}

Error: 0
[3, 2, 7, 5, ]
[7, 3, 2, 5, ]
[5, 3, 7, 2, ]
[2, 7, 3, 5, ]
[2, 5, 7, 3, ]
[2, 3, 5, 7, ]


Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

typedef long long int LLI;

void swap(vector<LLI> &vs, int i, int j) {
  int val = vs[i];
  vs[i] = vs[j];
  vs[j] = val;
}

void print(vector<LLI> vs) {
  cerr << "[";
  for (LLI val : vs) {
    cerr << val << ", ";
  }
  cerr << "]" << endl;
}

int calc(vector<LLI> &vs, int k) {
  LLI prod = 1;
  for (int i = 0; i < k; i++) {
    prod *= vs[i];
  }
  LLI ans = prod;
  for (int i = k; i < vs.size(); i++) {
    prod /= vs[i - k];
    prod *= vs[i];
    ans = max(prod, ans);
  }
  return ans;
}

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (n == 0 and k == 0)
      break;
    vector<LLI> vs(n);
    for (int i = 0; i < n; i++) {
      cin >> vs[i];
    }
    int init_score = calc(vs, k);
    int max_score = -10000;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        swap(vs, i, j);
        int score = calc(vs, k);
        max_score = max(max_score, score);
        print(vs);
        swap(vs, i, j);
      }
    }
    if (max_score < init_score) {
      cout << "NO GAME" << endl;
    } else {
      cout << max_score - init_score << endl;
    }
  }
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 30
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
int counter[140], sum[140];
// this behaves as if this were counting sort

int calc(int c, int n) {
  int cnt = 0;
  n--;
  for (int i = c; i >= 1; i--) {
    cnt += n;
    n--;
  }
  return cnt;
}

int main() {
  int n, S, r, mex;
  while (true) {
    cin >> n >> S;
    if (n + S == 0)
      break;
    mex = 0;
    for (int i = 0; i < 140; i++) {
      counter[i] = sum[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      cin >> r;
      mex = mex < r ? r : mex;
      counter[r]++;
    }

    for (int i = mex; i >= 0; i--) {
      sum[i] = counter[i] + sum[i + 1];
    }

    int cnt = 0;
    for (int i = 0; i <= mex; i++) {
      if (counter[i] == 0)
        continue;
      cnt += S - i + 1 <= i ? calc(counter[i], sum[i])
                            : counter[i] * sum[1 + S - i];
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int r[100], S, n;
int douf(int s, int e) {
  if (s == e)
    return s;
  else if (max(s, e) == min(s, e) + 1) {
    if (r[n] + r[s] > S && r[n] + r[e] > s)
      return max(s, e);
    else if (r[n] + r[s] > S)
      return s;
    else if (r[n] + r[e] > S)
      return e;
    else
      return min(s, e) - 1;
  }
  if (r[n] + r[(s + e) / 2] <= S) {
    douf(s, (s + e) / 2);
  } else {
    douf((s + e) / 2, e);
  }
}

int main() {
  int cou, lm;
  while (true) {
    cin >> n >> S;
    if (n == 0 && S == 0)
      break;
    cou = 0;
    lm = n;
    for (int i = 0; i < n; i++)
      cin >> r[i];
    sort(r, r + n, greater<int>());
    for (int i = 0; i < n; i++) {
      if (r[i] > S) {
        cou += n - 1;
        continue;
      }
      if (r[i] + r[i + 1] <= S)
        break;
      lm = douf(i + 2, n - 1);
      // cout <<"lm =  " <<lm << endl;
      for (int j = i + 1; j < lm; j++) {
        if ((r[i] + r[j]) > S && i != j)
          cou++;
        else
          break;
      }
    }
    cout << cou << endl;
    cou = 0;
  }
}