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

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

typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n - 1); (i) >= 0; i--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (int(i) = (m - 1); (i) >= (n); i--)
#define PRINT(x)                                                               \
  { cout << (x) << endl; }

int main() {
  int n;
  cin >> n;
  vector<int> L;
  REP(i, n) {
    int a;
    cin >> a;
    L.push_back(a);
  }
  int ans = 0;
  REP(i, n - 2) {
    FOR(j, i + 1, n - 1) {
      FOR(k, j + 1, n) {
        int a = L[i], b = L[j], c = L[k];
        if (a < b + c and b < a + c and c < a + b)
          ans++;
      }
    }
  }
  PRINT(ans)
}

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

typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n - 1); (i) >= 0; i--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (int(i) = (m - 1); (i) >= (n); i--)
#define PRINT(x)                                                               \
  { cout << (x) << endl; }

int main() {
  int n;
  cin >> n;
  vector<int> L;
  REP(i, n) {
    int a;
    cin >> a;
    L.push_back(a);
  }
  sort(L.begin(), L.end());
  int ans = 0;
  REP(i, n - 2) {
    FOR(j, i + 1, n - 1) {
      FOR(k, j + 1, n) {
        int a = L[i], b = L[j], c = L[k];
        if (a < b + c and b < a + c and c < a + b)
          ans++;
      }
    }
  }
  PRINT(ans)
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  int counter = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (a[i] + a[j] > a[k] && a[j] + a[k] > a[i] && a[k] + a[i] > a[j])
          counter++;
      }
    }
  }
  cout << counter << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  int counter = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  // int o,p,mid;
  int i = 0;
  int j = 1;
  int k;
  while (i < n - 2) {
    k = j + 1;
    while (k < n && a[i] + a[j] > a[k]) {
      k++;
    }
    counter += k - j - 1;
    j++;
    if (j > n - 2) {
      i++;
      j = i + 1;
    }
  }
  cout << counter << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
#define out(a) cout << a << " "

int main() {
  int N, cnt = 0;
  cin >> N;
  vector<int> ls(N);
  rep(i, N) cin >> ls[i];
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++)
      for (int k = j + 1; k < N; k++) {
        if (ls[i] < ls[j] + ls[k] && ls[j] < ls[k] + ls[i] &&
            ls[k] < ls[i] + ls[j])
          cnt++;
      }
  cout << cnt << endl;
  return 0;
}

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

int main() {
  int N, cnt = 0;
  cin >> N;
  vector<int> ls(N);
  rep(i, N) cin >> ls[i];
  sort(ls.begin(), ls.end());
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++)
      for (int k = j + 1; k < N; k++) {
        if (ls[i] < ls[j] + ls[k] && ls[j] < ls[k] + ls[i] &&
            ls[k] < ls[i] + ls[j])
          cnt++;
      }
  cout << cnt << endl;
  return 0;
}
Problem p02888 (C++)

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

int main() {
  int n;
  cin >> n;
  int a[1000];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int s = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      int x = lower_bound(a, a + n, a[i] + a[j]) - a - j - 1;
      s += x;
      // cout << x << endl;
    }
  }
  cout << s;
}

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

int main() {
  int n;
  cin >> n;
  int a[2000];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int s = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      int x = lower_bound(a, a + n, a[i] + a[j]) - a - j - 1;
      s += x;
      // cout << x << endl;
    }
  }
  cout << s;
}
Problem p02888 (C++)

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

int a[1000 + 5];

int binary(int l, int r, int x) {
  int ans = l - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (a[mid] < x) {
      ans = mid, l = mid + 1;
    } else
      r = mid - 1;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int cont = 0;
  sort(a + 1, a + n + 1);
  for (int i = 1; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int p = a[i] + a[j];
      int q = binary(j + 1, n, p);
      cont += (q - j);
    }
  }
  cout << cont << endl;
  return 0;
}

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

int a[2000 + 5];

int binary(int l, int r, int x) {
  int ans = l - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (a[mid] < x) {
      ans = mid, l = mid + 1;
    } else
      r = mid - 1;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int cont = 0;
  sort(a + 1, a + n + 1);
  for (int i = 1; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int p = a[i] + a[j];
      int q = binary(j + 1, n, p);
      cont += (q - j);
    }
  }
  cout << cont << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[i] + l[j]) {
          ans += 1;
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  sort(l.begin(), l.end());
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int k = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin();
      ans += max(k - (j + 1), 0);
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int ans = 0;
  sort(a, a + n);
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = j + 1; k < n; ++k) {
        int len = a[i] + a[j] + a[k];
        int ma = max(a[i], max(a[j], a[k]));
        int rest = len - ma;
        if (ma < rest)
          ++ans;
        else
          break;
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int ans = 0;
  sort(a, a + n);
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = j + 1; k < n; ++k) {
        if (a[k] < a[i] + a[j])
          ++ans;
        else
          break;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  long long ans = 0;
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++)
      for (int k = j + 1; k < N; k++)
        if (A[i] + A[j] > A[k] && A[i] + A[k] > A[j] && A[k] + A[j] > A[i])
          ans++;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  long long ans = 0;
  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++)
      for (int k = j + 1; k < N; k++)
        if (A[i] + A[j] > A[k] && A[i] + A[k] > A[j] && A[k] + A[j] > A[i])
          ans++;
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(l.begin(), l.end());
  int res = 0;
  for (int i = 0; i < n; i) {
    for (int j = i + 1; j < n; j) {
      auto itr = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin();
      int k = itr - (j + 1);
      res += max(k, 0);
    }
  }
  cout << res << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(l.begin(), l.end());
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      auto itr = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin();
      int k = itr - (j + 1);
      res += max(k, 0);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)

const ll INF = 1LL << 60;

signed main() {

  ll n;
  cin >> n;

  vector<ll> a(n);

  rep(i, n) cin >> a[i];
  sort(a.rbegin(), a.rend());
  ll ans = 0;
  rep(i, n - 2) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j]) {
          ans++;
        } else {
          break;
        }
      }
    }
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)

const ll INF = 1LL << 60;

signed main() {

  ll n;
  cin >> n;

  vector<ll> a(n);

  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  ll ans = 0;
  rep(i, n - 2) {
    for (int j = i + 1; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j]) {
          ans++;
        } else {
          break;
        }
      }
    }
  }
  cout << ans;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int d[N];
  vector<int> L;
  for (int i = 0; i < N; i++) {
    cin >> d[i];
    L.push_back(d[i]);
  }
  sort(L.begin(), L.end());
  long long int ans = 0;
  for (vector<int>::iterator e = L.end(); e - 1 != L.begin(); e--) {
    for (vector<int>::iterator f = L.end() - 1; *f * 2 > *e; f--) {
      for (vector<int>::iterator g = f - 1; *g + *f > *e; g--) {
        if (abs(*g - *f) < *e)
          ans++;
      }
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int d[N];
  vector<int> L;
  for (int i = 0; i < N; i++) {
    cin >> d[i];
    L.push_back(d[i]);
  }
  sort(L.begin(), L.end());
  long long int ans = 0;
  for (vector<int>::iterator e = L.end() - 1; e - 1 != L.begin(); e--) {
    for (vector<int>::iterator f = e - 1; *f * 2 > *e; f--) {
      for (vector<int>::iterator g = f - 1; *g + *f > *e; g--) {
        if (abs(*g - *f) < *e)
          ans++;
      }
    }
  }
  cout << ans;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

ll l[1010];

void solve() {

  ll n;
  cin >> n;
  rep(i, n) { cin >> l[i]; }
  l[n] = 2000;
  sort(l, l + n);
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      // l[0]~l[j-1]までの間で, l[i]-l[j]より大きいもの
      auto itr = upper_bound(l, l + n, l[i] - l[j]);
      // cout << "i: " << i << ", j:" << j << endl;
      // cout << *itr << endl;
      // cout << max(int(0), int(distance(itr,l+j))) << endl;
      ans += max(int(0), int(distance(itr, l + j)));
    }
  }

  cout << ans << endl;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

ll l[2010];

void solve() {

  ll n;
  cin >> n;
  rep(i, n) { cin >> l[i]; }
  l[n] = 2000;
  sort(l, l + n);
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      // l[0]~l[j-1]までの間で, l[i]-l[j]より大きいもの
      auto itr = upper_bound(l, l + n, l[i] - l[j]);
      // cout << "i: " << i << ", j:" << j << endl;
      // cout << *itr << endl;
      // cout << max(int(0), int(distance(itr,l+j))) << endl;
      ans += max(int(0), int(distance(itr, l + j)));
    }
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define mod 1000000007
#define INF LLONG_MAX / 5
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
int x[1000];
signed main() {
  int n, ans = 0;
  cin >> n;
  f(i, n) cin >> x[i];
  sort(x, x + n);
  f(i, n) {
    for (int j = i + 1; j < n; j++) {
      ans += lower_bound(x, x + n, x[i] + x[j]) - x - j - 1;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define mod 1000000007
#define INF LLONG_MAX / 5
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
int x[2000];
signed main() {
  int n, ans = 0;
  cin >> n;
  f(i, n) cin >> x[i];
  sort(x, x + n);
  f(i, n) {
    for (int j = i + 1; j < n; j++) {
      ans += lower_bound(x, x + n, x[i] + x[j]) - x - j - 1;
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

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

using namespace std;

int l[1111];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> l[i];
  sort(l, l + n);
  int r = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int p = lower_bound(l, l + n, l[i] + l[j]) - l;
      r += max(0, p - j - 1);
    }
  }
  cout << r << endl;
  return 0;
}


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

using namespace std;

int l[2222];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> l[i];
  sort(l, l + n);
  int r = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int p = lower_bound(l, l + n, l[i] + l[j]) - l;
      r += max(0, p - j - 1);
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02888 (C++)

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

int n;
vector<int> l;

bool isok(int a, int b, int c) {
  if (l[a] + l[b] > l[c])
    return true;
  else
    return false;
}

int nibutan(int a, int b) {
  int ok = b, ng = n;
  while (ng - ok > 1) {
    int naka = (ng + ok) / 2;
    if (isok(a, b, naka))
      ok = naka;
    else {
      ng = naka;
    }
  }
  return ok - b;
}

int main() {
  cin >> n;
  rep(i, n) { cin >> l[i]; }
  ll ans = 0;
  sort(l.begin(), l.end());
  for (int a = 0; a < n; a++) {
    for (int b = a + 1; b < n; b++) {
      ans += nibutan(a, b);
    }
  }
  cout << ans << endl;
}


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

int n;
vector<int> l;

bool isok(int a, int b, int c) {
  if (l[a] + l[b] > l[c])
    return true;
  else
    return false;
}

int nibutan(int a, int b) {
  int ok = b, ng = n;
  while (ng - ok > 1) {
    int naka = (ng + ok) / 2;
    if (isok(a, b, naka))
      ok = naka;
    else {
      ng = naka;
    }
  }
  return ok - b;
}

int main() {
  cin >> n;
  l.resize(n);
  rep(i, n) { cin >> l[i]; }
  ll ans = 0;
  sort(l.begin(), l.end());
  for (int a = 0; a < n; a++) {
    for (int b = a + 1; b < n; b++) {
      ans += nibutan(a, b);
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, count = 0;
  cin >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = j + 1; k < n; k++) {
        if (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[j] + l[i]) {
          count++;
        }
      }
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, count = 0;
  cin >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  sort(l.begin(), l.end());
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = j + 1; k < n; k++) {
        if (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[j] + l[i]) {
          count++;
        }
      }
    }
  }
  cout << count << endl;
}
Problem p02888 (C++)

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

#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;

using namespace std;

int main(void) {
  ll N, ans;
  scanf("%lld", &N);
  vector<ll> L(N);
  REP(int, i, 0, N) scanf("%lld", &L.at(i));
  // sort(L.begin(),L.end());
  ans = 0;
  REP(int, i, 0, N - 2) {
    REP(int, j, i + 1, N - 1) {
      REP(int, k, j + 1, N) {
        // cout << i << j << k << endl;
        if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) {
          ans++;
        }
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}


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

#define REP(type, i, a, b) for (type i = a; i < b; i++)
typedef long long ll;

using namespace std;

int main(void) {
  ll N, ans;
  scanf("%lld", &N);
  vector<ll> L(N);
  REP(int, i, 0, N) scanf("%lld", &L.at(i));
  sort(L.begin(), L.end());
  ans = 0;
  REP(int, i, 0, N - 2) {
    REP(int, j, i + 1, N - 1) {
      REP(int, k, j + 1, N) {
        // cout << i << j << k << endl;
        if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j]) {
          ans++;
        }
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

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

  int n, l[3000];
  int cnt = 0;
  int i, j, k;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> l[i];

  // sort(l, l + n);

  for (i = 0; i < n - 2; i++) {
    for (j = i + 1; j < n - 1; j++) {
      for (k = j + 1; k < n; k++) {
        if (l[i] < l[j] + l[k] && l[k] < l[j] + l[i] && l[j] < l[i] + l[k])
          cnt++;
      }
    }
  }
  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

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

  int n, l[3000];
  int cnt = 0;
  int i, j, k;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> l[i];

  sort(l, l + n);

  for (i = 0; i < n - 2; i++) {
    for (j = i + 1; j < n - 1; j++) {
      for (k = j + 1; k < n; k++) {
        if (l[i] < l[j] + l[k] && l[k] < l[j] + l[i] && l[j] < l[i] + l[k])
          cnt++;
      }
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
int main() {
  int64_t N, b, e, m, count = 0;
  vector<int64_t> L(1024);
  cin >> N;
  rep(i, N) { cin >> L.at(i); }
  sort(L.begin(), L.end(), greater<int>());
  rep(i, N - 2) {
    for (int64_t j = i + 1; j < N - 1; j++) {
      b = j;
      e = N;
      while (e - b > 1) {
        m = (b + e) / 2;
        if (L.at(i) < L.at(j) + L.at(m))
          b = m;
        else
          e = m;
      }
      count += b - j;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
int main() {
  int64_t N, b, e, m, count = 0;
  vector<int64_t> L(pow(2, 16));
  cin >> N;
  rep(i, N) { cin >> L.at(i); }
  sort(L.begin(), L.end(), greater<int>());
  rep(i, N - 2) {
    for (int64_t j = i + 1; j < N - 1; j++) {
      b = j;
      e = N;
      while (e - b > 1) {
        m = (b + e) / 2;
        if (L.at(i) < L.at(j) + L.at(m))
          b = m;
        else
          e = m;
      }
      count += b - j;
    }
  }
  cout << count << endl;
}
Problem p02888 (C++)

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

int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> L(N);
  for (int n = 0; n < N; n++) {
    cin >> L.at(n);
  }

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

  for (int x = 0; x < N; x++) {
    for (int y = x + 1; y < N; y++) {
      for (int z = y + 1; z < N; z++) {
        if (L.at(x) < L.at(y) + L.at(z) && L.at(y) < L.at(x) + L.at(z) &&
            L.at(z) < L.at(y) + L.at(x))
          sum++;
        else
          break;
      }
    }
  }

  cout << sum << endl;
}

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

int main() {
  int N, sum = 0;
  cin >> N;
  vector<int> L(N);
  for (int n = 0; n < N; n++) {
    cin >> L.at(n);
  }

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

  for (int x = 0; x < N; x++) {
    for (int y = x + 1; y < N; y++) {
      for (int z = y + 1; z < N; z++) {
        if (L.at(z) < L.at(x) + L.at(y))
          sum++;
        else
          break;
      }
    }
  }

  cout << sum << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rip(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define lb long double
#define vec vector<int>
#define vec2 vector<vector<int>>
#define set setprecision
#define mod 1000000007
#define pi 3.14159265
#include <numeric>
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
ll pow(ll a, ll b) {
  ll k = b % 30000, h = b / 30000, s = 1;
  rip(i, 30000) s = (s * a) % mod;
  ll j = 1;
  if (h != 0)
    rip(i, h) j = j * s % mod;
  if (k != 0)
    rip(i, k) j = j * a % mod;
  return (j);
}
int main() {
  int n;
  cin >> n;
  vec a(n);
  rip(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = j + 1; j < n; k++)
        if (a[i] + a[j] > a[k])
          count++;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define rip(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define lb long double
#define vec vector<int>
#define vec2 vector<vector<int>>
#define set setprecision
#define mod 1000000007
#define pi 3.14159265
#include <numeric>
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
ll pow(ll a, ll b) {
  ll k = b % 30000, h = b / 30000, s = 1;
  rip(i, 30000) s = (s * a) % mod;
  ll j = 1;
  if (h != 0)
    rip(i, h) j = j * s % mod;
  if (k != 0)
    rip(i, k) j = j * a % mod;
  return (j);
}
int main() {
  int n;
  cin >> n;
  vec a(n);
  rip(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      for (int k = j + 1; k < n; k++)
        if (a[i] + a[j] > a[k])
          count++;
    }
  }
  cout << count << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<int> L(n);
  REP(i, n) cin >> L[i];
  // int n = 2000;
  // vector<int> L(n);
  // random_device rnd;
  // REP(i,n)L[i] = rnd() % 100;

  sort(L.begin(), L.end());
  int ans = 0;
  for (int a = 0; a < n - 2; a++) {
    for (int b = a + 1; b < n - 1; b++) {
      int sum_ab = L[a] + L[b];
      int left = lower_bound(L.begin(), L.end(), sum_ab) - L.begin();
      int right = b + 1;
      ans += left - right;
    }
  }
  cout << ans << endl;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> L(n);
  REP(i, n) cin >> L[i];
  // int n = 2000;
  // vector<int> L(n);
  // random_device rnd;
  // REP(i,n)L[i] = rnd() % 100;

  sort(L.begin(), L.end());
  int ans = 0;
  for (int a = 0; a < n - 2; a++) {
    for (int b = a + 1; b < n - 1; b++) {
      int sum_ab = L[a] + L[b];
      int left = lower_bound(L.begin(), L.end(), sum_ab) - L.begin();
      int right = b + 1;
      ans += left - right;
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define mp make_pair
#define bit(n) (1LL << (n))
#define F first
#define S second
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL << 60;
const ll mod = (int)1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(all(l));
  ll ans = 0;
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      int tem;
      auto itr1 = upper_bound(all(l), abs(l[i] - l[j]));
      auto itr2 = lower_bound(all(l), l[i] + l[j]);
      tem = itr2 - itr1;
      int it1 = itr1 - l.begin();
      int it2 = itr2 - l.begin();
      if (it1 <= i && i < it2)
        tem--;
      if (it1 <= j && j < it2)
        tem--;
      if (tem > 0) {
        ans += tem;
        cerr << i << ' ' << j << ' ' << tem << endl;
      }
    }
  }
  cout << ans / 6;
}

/*



 */


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define all(x) (x).begin(), (x).end()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define mp make_pair
#define bit(n) (1LL << (n))
#define F first
#define S second
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL << 60;
const ll mod = (int)1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(all(l));
  ll ans = 0;
  rep(i, n) {
    rep(j, n) {
      if (i == j)
        continue;
      int tem;
      auto itr1 = upper_bound(all(l), abs(l[i] - l[j]));
      auto itr2 = lower_bound(all(l), l[i] + l[j]);
      tem = itr2 - itr1;
      int it1 = itr1 - l.begin();
      int it2 = itr2 - l.begin();
      if (it1 <= i && i < it2)
        tem--;
      if (it1 <= j && j < it2)
        tem--;
      if (tem > 0)
        ans += (ll)tem;
    }
  }
  cout << ans / 6;
}

/*



 */
Problem p02888 (C++)

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

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define debug(x) cerr << "  - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \
  cerr << "  - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl;
#define debugv(v, n)                                                           \
  cerr << #v << " : "                                                          \
       << " ";                                                                 \
  for (int i = 0; i < n; i++)                                                  \
    cerr << "\t" << #v << "[" << i << "]"                                      \
         << " : " << v[i] << "\n";                                             \
  cerr << endl;
#define sep() cerr << "--------------------" << endl;

typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1234;

int a[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      int c = a[i] + a[j];
      int idx = lower_bound(a + j + 1, a + n, c) - a;
      idx--;
      sum += idx - j;
    }
  }
  cout << sum << endl;
  return 0;
}

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

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define debug(x) cerr << "  - " << #x << ": " << x << endl;
#define debugs(x, y)                                                           \
  cerr << "  - " << #x << ": " << x << "\t\t" << #y << ": " << y << endl;
#define debugv(v, n)                                                           \
  cerr << #v << " : "                                                          \
       << " ";                                                                 \
  for (int i = 0; i < n; i++)                                                  \
    cerr << "\t" << #v << "[" << i << "]"                                      \
         << " : " << v[i] << "\n";                                             \
  cerr << endl;
#define sep() cerr << "--------------------" << endl;

typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 2234;

int a[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      int c = a[i] + a[j];
      int idx = lower_bound(a + j + 1, a + n, c) - a;
      idx--;
      sum += idx - j;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int v[1004];

int caut_bin(int st, int fin, int sum) {
  int p = st;
  while (st <= fin) {
    int mij = (st + fin) / 2;
    if (v[mij] >= sum) {
      fin = mij - 1;
    } else {
      st = mij + 1;
    }
  }
  return st - p;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int sum = 0;
  sort(v, v + n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += caut_bin(j + 1, n - 1, v[i] + v[j]);
    }
  }
  cout << sum;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int v[2004];

int caut_bin(int st, int fin, int sum) {
  int p = st;
  while (st <= fin) {
    int mij = (st + fin) / 2;
    if (v[mij] >= sum) {
      fin = mij - 1;
    } else {
      st = mij + 1;
    }
  }
  return st - p;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int sum = 0;
  sort(v, v + n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      sum += caut_bin(j + 1, n - 1, v[i] + v[j]);
    }
  }
  cout << sum;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    author:  yuya1234
 *    created: 17.07.2020 09:17:47
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
long double eps = 1.0E-14;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) {
  return gcd(a, b) * (a / gcd(a, b)) * (b / gcd(a, b));
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

  int n;
  cin >> n;

  int l[n];
  REP(i, n) cin >> l[i];
  ll ans = 0;

  REP(i, n - 2) FOR(j, i + 1, n - 2) FOR(k, j + 1, n - 1) {
    if (l[i] < l[j] + l[k] && l[j] < l[i] + l[k] && l[k] < l[j] + l[i])
      ans++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
/**
 *    author:  yuya1234
 *    created: 17.07.2020 09:17:47
 **/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
long double eps = 1.0E-14;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)

#define SORT(s) sort((s).begin(), (s).end())
#define SORTD(s) sort((s).rbegin(), (s).rend())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) {
  return gcd(a, b) * (a / gcd(a, b)) * (b / gcd(a, b));
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15);

  int n;
  cin >> n;

  int l[n];
  REP(i, n) cin >> l[i];
  ll ans = 0;

  sort(l, l + n, greater<int>());

  REP(i, n - 2) {
    FOR(j, i + 1, n - 2) {
      FOR(k, j + 1, n - 1) {
        if (l[i] < l[j] + l[k] && l[j] < l[i] + l[k] && l[k] < l[j] + l[i])
          ans++;
        else
          break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
long long l[2010], cnt[1010][2010] = {}, ans = 0;
int main() {
  int i, j, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> l[i];
  }
  sort(l, l + n);
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= 1000; j++) {
      cnt[i][j] = cnt[i - 1][j];
    }
    for (j = 0; j <= l[i - 1]; j++) {
      cnt[i][j]++;
    }
  }
  for (i = n - 1; i >= 0; i--) {
    for (j = i - 1; j >= 1; j--) {
      int x = l[i] - l[j];
      ans += cnt[j][x + 1];
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
long long l[2010], cnt[2010][2010] = {}, ans = 0;
int main() {
  int i, j, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> l[i];
  }
  sort(l, l + n);
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= 1000; j++) {
      cnt[i][j] = cnt[i - 1][j];
    }
    for (j = 0; j <= l[i - 1]; j++) {
      cnt[i][j]++;
    }
  }
  for (i = n - 1; i >= 0; i--) {
    for (j = i - 1; j >= 1; j--) {
      int x = l[i] - l[j];
      ans += cnt[j][x + 1];
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9 + 7;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N, a, b, c;
  cin >> N;
  int L[N], count = 0;
  rep(i, N) cin >> L[i];
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      for (int k = j + 1; k < N; k++) {
        a = L[i];
        b = L[j];
        c = L[k];
        if (a < b + c && b < a + c && c < a + b) {
          count++;
        }
      }
    }
  }
  cout << count << endl;
  return 0;
}

// a=97,z=122
//  1,2,3,4,5   1,2,3 1,2,4 1,2,5, 1,3,4 1,3,5, 1,4,5, 2,3,4, 2,3,5 2,4,5 3,4,5

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9 + 7;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N, a, b, c;
  cin >> N;
  int L[N], count = 0;
  rep(i, N) cin >> L[i];
  sort(L, L + N);
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      for (int k = j + 1; k < N; k++) {
        a = L[i];
        b = L[j];
        c = L[k];
        if (a < b + c && b < a + c && c < a + b) {
          count++;
        }
      }
    }
  }
  cout << count << endl;
  return 0;
}

// a=97,z=122
//  1,2,3,4,5   1,2,3 1,2,4 1,2,5, 1,3,4 1,3,5, 1,4,5, 2,3,4, 2,3,5 2,4,5 3,4,5
Problem p02888 (C++)

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

bool flag(int a, int b, int c) {
  return ((a < b + c) && (b < a + c) && (c < b + a));
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  int count = 0;
  for (int i = 0; i < n - 2; ++i) {

    for (int j = i + 1; j < n - 1; ++j) {
      for (int k = j + 1; k < n; ++k) {
        if (flag(v[i], v[k], v[j]))
          count++;
      }
    }
  }
  cout << count << endl;
}

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

bool flag(int a, int b, int c) {
  return ((a < b + c) && (b < a + c) && (c < b + a));
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int v[n];
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v, v + n);
  int count = 0;
  for (int i = 0; i < n - 2; ++i) {

    for (int j = i + 1; j < n - 1; ++j) {
      for (int k = j + 1; k < n; ++k) {
        if (flag(v[i], v[k], v[j]))
          count++;
      }
    }
  }
  cout << count << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vi = vector<int>;
using vvi = vector<vi>;
#define ote(x) cout << (x) << endl
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
using PII = pair<int, int>;
void vprn(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << (i < v.size() - 1 ? ",\t" : "\n");
} //

ll solve(int N, vi &L) {
  ll ans = 0;

  rp(ia, 0, N - 2) {
    ll a = L[ia];
    rp(ib, ia + 1, N - 1) {
      ll b = L[ib];
      rp(ic, ib + 1, N) {
        ll c = L[ic];
        // printf("a %d b %d c %d\n",a,b,c);//
        if (a < b + c && b < c + a && c < a + b) {
          ans++;
        }
      }
    }
  }

  return ans;
}

int main() {
  int N;
  cin >> N;
  vi L(N);
  rp(i, 0, N) cin >> L[i];
  ote(solve(N, L));
}


Fixed submission:
//
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vi = vector<int>;
using vvi = vector<vi>;
#define ote(x) cout << (x) << endl
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
using PII = pair<int, int>;
void vprn(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << (i < v.size() - 1 ? ",\t" : "\n");
} //

ll solve(int N, vi &L) {
  ll ans = 0;

  rp(ia, 0, N - 2) {
    ll a = L[ia];
    rp(ib, ia + 1, N - 1) {
      ll b = L[ib];
      rp(ic, ib + 1, N) {
        ll c = L[ic];
        int minL = min({a, b, c});
        int maxL = max({a, b, c});
        int midL = a + b + c - minL - maxL;
        // printf("min %d mid %d max %d\n",minL,midL,maxL);//
        if (maxL < minL + midL) {
          ans++;
        }
      }
    }
  }

  return ans;
}

int main() {
  int N;
  cin >> N;
  vi L(N);
  rp(i, 0, N) cin >> L[i];
  ote(solve(N, L));
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void go();
int main() {
#ifdef tokitsukaze
  freopen("TEST.txt", "r", stdin);
#endif
  go();
  return 0;
}

/*********************************  head  *********************************/

int n, a[10];
int cmp(int a, int b) { return a > b; }
LL ans;
void go() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n, cmp);
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int t = lower_bound(a + j, a + n, a[i] - a[j], greater<int>()) - a;
      t -= 1;
      if (t > j)
        ans += t - j;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void go();
int main() {
#ifdef tokitsukaze
  freopen("TEST.txt", "r", stdin);
#endif
  go();
  return 0;
}

/*********************************  head  *********************************/

int n, a[100100];
int cmp(int a, int b) { return a > b; }
LL ans;
void go() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n, cmp);
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int t = lower_bound(a + j, a + n, a[i] - a[j], greater<int>()) - a;
      t -= 1;
      if (t > j)
        ans += t - j;
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  vector<int> a(1001);
  for (int i = 0; i < n; i++) {
    cin >> l.at(i);
    a.at(l.at(i))++;
  }
  int64_t ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i != j) {
        for (int k = abs(l.at(i) - l.at(j)) + 1;
             k < min(1001, (l.at(i) + l.at(j))); k++) {
          ans += a.at(k);
          if (k == l.at(i))
            ans--;
          if (k == l.at(j))
            ans--;
        }
      }
    }
  }
  cout << ans / 3 << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  vector<int> a(1001);
  for (int i = 0; i < n; i++) {
    cin >> l.at(i);
    a.at(l.at(i))++;
  }
  int64_t ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i != j) {
        for (int k = abs(l.at(i) - l.at(j)) + 1,
                 x = min(1001, (l.at(i) + l.at(j)));
             k < x; k++) {
          ans += a.at(k);
          if (k == l.at(i))
            ans--;
          if (k == l.at(j))
            ans--;
        }
      }
    }
  }
  cout << ans / 3 << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

int n, l[2020];

int main() {
  cin >> n;
  rep(i, n) cin >> l[i];
  sort(l, l + n);
  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int a = min(l[i], l[j]), b = l[i] + l[j] - a;
      auto sentou = l + j + 1;
      auto ub = upper_bound(sentou, l + n, a + b - 1);
      int num = ub - sentou;
      cerr << a << " " << b << " " << *ub << " " << num << endl;
      sum += num;
    }
  }
  cout << sum << endl;
  return 0;
}


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

int n, l[2020];

int main() {
  cin >> n;
  rep(i, n) cin >> l[i];
  sort(l, l + n);
  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int a = min(l[i], l[j]), b = l[i] + l[j] - a;
      auto sentou = l + j + 1;
      auto ub = upper_bound(sentou, l + n, a + b - 1);
      int num = ub - sentou;
      // cerr << a << " " << b << " " << *ub << " " << num << endl;
      sum += num;
    }
  }
  cout << sum << endl;
  return 0;
}