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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int a[20005];
int main() {
  int num = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + 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++) {
        if (a[i] + a[j] > a[k] && a[i] + a[k] > a[j] && a[j] + a[k] > a[i]) {
          if (a[k] - a[j] >= a[i]) {
            break;
          }
          num++;
        }
      }
    }
  }
  cout << num << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[20005];
int main() {
  int num = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + 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++) {
        if (a[i] + a[j] > a[k] && a[i] + a[k] > a[j]) {
          num++;
        }
      }
    }
  }
  cout << num << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int l[1010];
  static int memo[1010][2020] = {};
  for (int i = 0; i < n; i++)
    cin >> l[i];
  for (int i = 0; i < n; i++)
    memo[i][l[i]]++;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j + 1 < 2020; j++)
      memo[i][j + 1] += memo[i][j];
  }

  for (int j = 0; j < 2020; j++) {
    for (int i = 0; i + 1 < n; i++)
      memo[i + 1][j] += memo[i][j];
  }

  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int index_lmax = l[i] + l[j] - 1;
      int index_lmin = max(0, max(l[i] - l[j], l[j] - l[i]));
      int count = memo[n - 1][index_lmax] - memo[n - 1][index_lmin] -
                  memo[j][index_lmax] + memo[j][index_lmin];
      if (count > 0)
        res += count;
    }
  }

  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int l[2020];
  static int memo[2020][2020] = {};
  for (int i = 0; i < n; i++)
    cin >> l[i];
  for (int i = 0; i < n; i++)
    memo[i][l[i]]++;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j + 1 < 2020; j++)
      memo[i][j + 1] += memo[i][j];
  }

  for (int j = 0; j < 2020; j++) {
    for (int i = 0; i + 1 < n; i++)
      memo[i + 1][j] += memo[i][j];
  }

  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int index_lmax = l[i] + l[j] - 1;
      int index_lmin = max(0, max(l[i] - l[j], l[j] - l[i]));
      int count = memo[n - 1][index_lmax] - memo[n - 1][index_lmin] -
                  memo[j][index_lmax] + memo[j][index_lmin];
      if (count > 0)
        res += count;
    }
  }

  cout << res << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> L(n, 0);
  rep(i, n) { cin >> L[i]; }

  int ans = 0;
  for (int a = 0; a < n - 2; a++) {
    for (int b = a + 1; b < n - 1; b++) {
      for (int c = b + 1; c < n; c++) {
        if ((L[a] < L[b] + L[c]) && (L[b] < L[c] + L[a]) &&
            (L[c] < L[a] + L[b])) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> L(n, 0);
  rep(i, n) { cin >> L[i]; }
  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++) {
      for (int c = b + 1; c < n; c++) {
        if ((L[a] < L[b] + L[c]) && (L[b] < L[c] + L[a]) &&
            (L[c] < L[a] + L[b])) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = (1e9) + 7;
void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int main() {
  fastIO();
  int n;
  cin >> n;
  vector<int> sticks(n);
  for (int i = 0; i < n; i++) {
    cin >> sticks[i];
  }
  sort(sticks.begin(), sticks.end());
  int tot = 0;
  for (int A = 0; A < n; A++) {
    for (int B = A; B < n; B++) {
      for (int C = B; C < n; C++) {
        int a = sticks[A], b = sticks[B], c = sticks[C];
        if (a >= b + c) {
          cout << tot << endl;
          return 0;
        }
        if ((a < b + c) && (b < a + c) && (c < b + a) &&
            (a != b && a != c && b != c)) {
          tot++;
        }
      }
    }
  }
  cout << tot << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = (1e9) + 7;
void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int main() {
  fastIO();
  int n;
  cin >> n;
  vector<int> sticks(n);
  for (int i = 0; i < n; i++) {
    cin >> sticks[i];
  }
  sort(sticks.begin(), sticks.end());
  int tot = 0;
  for (int A = 0; A < n; A++) {
    for (int B = A + 1; B < n; B++) {
      int a = sticks[A], b = sticks[B];
      int x = lower_bound(sticks.begin(), sticks.end(), a + b) - sticks.begin();
      tot += x - B;
      tot--;
    }
  }
  cout << tot << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  size_t n;
  cin >> n;
  vector<size_t> l(n, 0);
  for (size_t i = 0; i < n; ++i)
    cin >> l[i];
  size_t d = 0;
  for (size_t i = 0; i < l.size(); ++i) {
    auto a = l[i];
    for (size_t j = i + 1; j < l.size(); ++j) {
      auto b = l[j];
      for (size_t k = j + 1; k < l.size(); ++k) {
        auto c = l[k];
        if (a < b + c && b < a + c && c < a + b)
          ++d;
      }
    }
  }
  cout << d << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  size_t n;
  cin >> n;
  vector<size_t> l(n, 0);
  for (size_t i = 0; i < n; ++i)
    cin >> l[i];
  sort(begin(l), end(l), greater<size_t>());
  size_t d = 0;
  for (size_t i = 0; i < l.size(); ++i) {
    auto a = l[i];
    for (size_t j = i + 1; j < l.size(); ++j) {
      auto b = l[j];
      for (size_t k = j + 1; k < l.size(); ++k) {
        auto c = l[k];
        if (a < b + c && b < a + c && c < a + b)
          ++d;
      }
    }
  }
  cout << d << endl;
  return 0;
}
Problem p02888 (C++)

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

int main() {
  int64_t n, a = 0;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v.at(i);
  sort(v.begin(), v.end());
  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 (v.at(i) + v.at(j) > v.at(k) && v.at(i) + v.at(k) > v.at(j) &&
            v.at(k) + v.at(j) > v.at(i))
          a++;
        else
          break;
      }
    }
  }
  cout << a << endl;
}


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

int main() {
  int64_t n, a = 0;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v.at(i);
  sort(v.begin(), v.end());
  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 (v.at(i) + v.at(j) > v.at(k))
          a++;
        else
          break;
      }
    }
  }
  cout << a << 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, 0);
  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }

  int cnt = 0;
  sort(L.begin(), L.end());

  /*
      if(N==3){
          if((L.at(0) + L.at(1) > L.at(2)) && (L.at(0) < L.at(1) + L.at(2)) &&
     (L.at(0) + L.at(2) > L.at(0))){ cnt++;
          }
          cout << cnt << endl;
          return 0;
      }
  */
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      for (int k = j; k < N; k++) {
        if (i != j && j != k && i != k) {
          if ((L.at(i) + L.at(j)) - L.at(k) > 0) {
            cnt++;
          } else {
            break;
          }
        }
      }
    }
  }

  cout << cnt << endl;
}


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

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

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

  int cnt = 0;
  sort(L.begin(), L.end());

  /*
      if(N==3){
          if((L.at(0) + L.at(1) > L.at(2)) && (L.at(0) < L.at(1) + L.at(2)) &&
     (L.at(0) + L.at(2) > L.at(0))){ cnt++;
          }
          cout << cnt << endl;
          return 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 ((L.at(i) + L.at(j)) - L.at(k) > 0) {
          cnt++;
        } else {
          break;
        }
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include "bits/stdc++.h"

using namespace std;

//------------------------------- Libraries --------------------------------//

//------------------------------- Type Names -------------------------------//

using i64 = int_fast64_t;

using seika = string;
// akari : 1D, yukari : 2D, maki : 3D vector
template <class kizuna> using akari = vector<kizuna>;
template <class yuzuki> using yukari = akari<akari<yuzuki>>;
template <class tsurumaki> using maki = akari<yukari<tsurumaki>>;
// akane : ascending order, aoi : decending order
template <class kotonoha>
using akane = priority_queue<kotonoha, akari<kotonoha>, greater<kotonoha>>;
template <class kotonoha> using aoi = priority_queue<kotonoha>;

//------------------------------- Dubug Functions ---------------------------//
inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}
//------------------------------- Solver ------------------------------------//

void solve() {
  int n;
  cin >> n;
  akari<int> ls(n);
  for (int i = 0; i < n; i++) {
    cin >> ls[i];
  }
  sort(ls.begin(), ls.end());
  long ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      ans += distance(ls.begin(),
                      lower_bound(ls.begin(), ls.end(), ls[i] + ls[j])) -
             j - 1;
    }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include "bits/stdc++.h"

using namespace std;

//------------------------------- Libraries --------------------------------//

//------------------------------- Type Names -------------------------------//

using i64 = int_fast64_t;

using seika = string;
// akari : 1D, yukari : 2D, maki : 3D vector
template <class kizuna> using akari = vector<kizuna>;
template <class yuzuki> using yukari = akari<akari<yuzuki>>;
template <class tsurumaki> using maki = akari<yukari<tsurumaki>>;
// akane : ascending order, aoi : decending order
template <class kotonoha>
using akane = priority_queue<kotonoha, akari<kotonoha>, greater<kotonoha>>;
template <class kotonoha> using aoi = priority_queue<kotonoha>;

//------------------------------- Dubug Functions ---------------------------//
inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}
//------------------------------- Solver ------------------------------------//

void solve() {
  int n;
  cin >> n;
  akari<int> ls(n);
  for (int i = 0; i < n; i++) {
    cin >> ls[i];
  }
  sort(ls.begin(), ls.end());
  long ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      ans += distance(ls.begin(),
                      lower_bound(ls.begin(), ls.end(), ls[i] + ls[j])) -
             j - 1;
    }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02888 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  int l[1005];
  for (int i = 0; i < n; ++i) {
    cin >> l[i];
  }
  sort(l, l + n);

  int res = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int tmp = l[i] + l[j];
      int *pos = lower_bound(l, l + n, tmp);
      res += max(0, (int)(pos - (l + j) - 1));
    }
  }
  cout << res << endl;

  return 0;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  int l[2005];
  for (int i = 0; i < n; ++i) {
    cin >> l[i];
  }
  sort(l, l + n);

  int res = 0;
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      int tmp = l[i] + l[j];
      int *pos = lower_bound(l, l + n, tmp);
      res += max(0, (int)(pos - (l + j) - 1));
    }
  }
  cout << res << endl;

  return 0;
}
Problem p02888 (C++)

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

int main(void) {
  int n;
  cin >> n;
  vector<int> l(n);

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

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

  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 (l[i] < l[j] + l[k] && l[j] < l[k] + l[i] && l[k] < l[i] + l[j]) {
          count++;
        } else if (l[k] >= l[i] + l[j]) {
          break;
        }
      }
    }
  }

  cout << count << endl;
}

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

int main(void) {
  int n;
  cin >> n;
  vector<int> l(n);

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

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

  int count = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (l[n - 1] < l[i] + l[j]) {
        count += n - j - 1;
      } else {
        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]) {
            count++;
          } else if (l[k] >= l[i] + l[j]) {
            break;
          }
        }
      }
    }
  }

  cout << count << 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);

  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }

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

  int count = 0;

  for (int i = 0; i < N; i++) {
    int a = L.at(i);
    for (int j = 0; j < N; j++) {
      if (i >= j) {
        continue;
      }
      int b = L.at(j);
      for (int k = 0; k < N; k++) {
        if (j >= k) {
          continue;
        }
        int c = L.at(k);
        if (abs(b - c) >= a) {
          break;
        } else {
          count++;
        }
      }
    }
  }

  cout << count << endl;

  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> L(N);

  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }

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

  int count = 0;

  for (int i = 0; i < N; i++) {
    int a = L.at(i);
    for (int j = 1; i + j < N; j++) {
      int b = L.at(i + j);
      for (int k = 1; i + j + k < N; k++) {
        int c = L.at(i + j + k);
        if (abs(b - c) >= a) {
          break;
        } else {
          count++;
        }
      }
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << #x << ": " << x << '\n'
#define INF 999999999
typedef long long int Int;
typedef pair<int, int> P;
using ll = long long;
using VI = vector<int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int x, y, z; // a,b,c
  int ans = 0;
  for (auto &i : a)
    cin >> i;
  for (int i = 0; i <= n - 3; i++) {
    for (int j = i + 1; j <= n - 2; j++) {
      for (int k = j + 1; k <= n - 1; k++) {
        if (a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j])
          ans++;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define SZ(x) ((int)(x).size())
#define debug(x) cerr << #x << ": " << x << '\n'
#define INF 999999999
typedef long long int Int;
typedef pair<int, int> P;
using ll = long long;
using VI = vector<int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int x, y, z; // a,b,c
  int ans = 0;
  for (auto &i : a)
    cin >> i;
  sort(a.begin(), a.end());
  for (int i = 0; i <= n - 3; i++) {
    for (int j = i + 1; j <= n - 2; j++) {
      for (int k = j + 1; k <= n - 1; k++) {
        if (a[i] < a[j] + a[k] && a[j] < a[i] + a[k] && a[k] < a[i] + a[j])
          ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long inf = 100000000000000;
typedef pair<int, int> P;
int main() { // 全探索できるか
  int n;
  cin >> n;
  int a[1100], i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  stable_sort(a, a + n);
  int j, k;
  int ans = 0;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      for (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;
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long inf = 100000000000000;
typedef pair<int, int> P;
int main() { // 全探索できるか
  int n;
  cin >> n;
  int a[2100], i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  stable_sort(a, a + n);
  int j, k;
  int ans = 0;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      for (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;
  return (0);
}
Problem p02888 (Python)

Buggy submission (status: Runtime Error):
import bisect

N = int(input())
L = [int(i) for i in input().split()]

SL = sorted(L)
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        right = bisect.bisect_left(SL, SL[i] + SL[j])
        left = j + 1
        ans += max(right - left)

print(ans)


Fixed submission:
import bisect

N = int(input())
L = [int(i) for i in input().split()]

SL = sorted(L)
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        right = bisect.bisect_left(SL, SL[i] + SL[j])
        left = j + 1
        ans += right - left

print(ans)
Problem p02888 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

N = int(input())
L = list(map(int, input().split()))
assert len(L) == N

L.sort()

n = 0

# Choose two edges
for i in range(N):
    for j in range(i + 1, N):
        a = L[i] + L[j]
        k = bisect.bisect_left(L, a, lo=j + 1)
        #   assert all(v < a for v in L[:k]) and all(v >= a for v in L[k:])
        # the number of candidates of the third edge
        n += k - (j + 1)

print(n)


Fixed submission:
import bisect

N = int(input())
L = list(map(int, input().split()))
assert len(L) == N

L.sort()

n = 0
# Choose two edges
for i in range(N):
    for j in range(i + 1, N):
        a = L[i] + L[j]
        k = bisect.bisect_left(L, a, lo=j + 1)
        #   assert all(v < a for v in L[:k]) and all(v >= a for v in L[k:])
        # the number of candidates of the third edge
        n += k - (j + 1)

print(n)
Problem p02888 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

n = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(n - 2):
    a = L[i]
    for j in range(i + 1, n - 1):
        b = L[j]
        k = bisect.bisect_left(L[j + 1 :], a + b)
        ans += k
print(ans)


Fixed submission:
import bisect

n = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(n - 2):
    a = L[i]
    for j in range(i + 1, n - 1):
        b = L[j]
        k = bisect.bisect_left(L, a + b)
        ans += k - j - 1
print(ans)
Problem p02888 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

N = int(input())
L = sorted(map(int, input().split(" ")))

ans = 0
for i in range(len(L)):
    for j in range(i + 1, len(L)):
        a = L[i]
        b = L[j]
        ans += bisect.bisect_right(L[j + 1 :], a + b - 1)

print(ans)


Fixed submission:
import bisect

N = int(input())
L = sorted(map(int, input().split(" ")))

ans = 0
for i in range(len(L)):
    for j in range(i + 1, len(L)):
        a = L[i]
        b = L[j]
        ans += max(0, bisect.bisect_right(L, a + b - 1) - (j + 1))

print(ans)
Problem p02888 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
N = int(input())
L = sorted([int(s) for s in input().split()])

MAX_SEARCH = max(L) + 1


def check_search(middle, idx_short2, l_lng, l_lows):
    return l_lows[middle] + l_lows[idx_short2] > l_lng


l_lows = L[:2]
triangles = 0


for l_lng in L[2:]:
    for idx, l_low in enumerate(l_lows):
        left = 0
        right = len(l_lows) - 1 - idx
        end = right
        while left < right:
            middle = (left + right) // 2
            if check_search(middle, len(l_lows) - 1 - idx, l_lng, l_lows):
                right = middle
            else:
                left = middle + 1
        triangles += end - right
    l_lows.append(l_lng)

print(triangles)


Fixed submission:
#!/usr/bin/env python3
N = int(input())
L = sorted([int(s) for s in input().split()])


def check_search(middle, idx_short2, l_lng, l_lows):
    return l_lows[middle] + l_lows[idx_short2] > l_lng


l_lows = L[:2]
triangles = 0


for l_lng in L[2:]:
    for idx, l_low in enumerate(l_lows):
        left = 0
        right = len(l_lows) - 1 - idx
        end = right
        while left < right:
            middle = (left + right) // 2
            if check_search(middle, len(l_lows) - 1 - idx, l_lng, l_lows):
                right = middle
            else:
                left = middle + 1
        triangles += end - right
    l_lows.append(l_lng)

print(triangles)
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++)

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> l(n, 0);
  REP(i, n) cin >> l.at(i);
  sort(l.begin(), l.end());

  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++) {
        bool f = 1;
        if (l.at(i) >= l.at(j) + l.at(k))
          f = 0;
        if (l.at(j) >= l.at(k) + l.at(i))
          f = 0;
        if (l.at(k) >= l.at(i) + l.at(j))
          f = 0;
        if (f)
          ans++;
        else
          break;
      }
    }
  }

  cout << ans;
}

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

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> l(n, 0);
  REP(i, n) cin >> l.at(i);
  sort(l.begin(), l.end());

  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.at(k) < l.at(i) + l.at(j))
          ans++;
        else
          break;
      }
    }
  }

  cout << ans;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) cout << fixed << setprecision(x)
#define endl '\n'
const ll inf = LLONG_MAX;
const long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

ll binary_search2(ll min, ll max, ll i, ll j, vll l) {
  if (min + 1 == max) { // 解近傍の処理
    if (l[max] < l[i] + l[j])
      return max;
    else
      return min;
  }

  ll med = (min + max) / 2;
  if (l[med] < l[i] + l[j])
    return binary_search2(med, max, i, j, l);
  else
    return binary_search2(min, med, i, j, l);
}

int main() {
  ll n;
  cin >> n;
  vll l(n);
  for (ll i = 0; i < n; i++)
    cin >> l[i];
  sort(all(l));
  ll ans = 0;
  for (ll i = 0; i < n - 2; i++) {
    for (ll j = i + 1; j < n - 1; j++) {
      ll x;
      if (l[j + 1] >= l[i] + l[j])
        continue;
      else if (l[n - 1] < l[i] + l[j])
        x = n - 1;
      else
        x = binary_search2(j + 1, n - 1, i, j, l);
      ans += x - (j + 1) + 1;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) cout << fixed << setprecision(x)
#define endl '\n'
const ll inf = LLONG_MAX;
const long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

ll binary_search2(ll min, ll max, ll i, ll j, vll &l) {
  if (min + 1 == max) { // 解近傍の処理
    if (l[max] < l[i] + l[j])
      return max;
    else
      return min;
  }

  ll med = (min + max) / 2;
  if (l[med] < l[i] + l[j])
    return binary_search2(med, max, i, j, l);
  else
    return binary_search2(min, med, i, j, l);
}

int main() {
  ll n;
  cin >> n;
  vll l(n);
  for (ll i = 0; i < n; i++)
    cin >> l[i];
  sort(all(l));
  ll ans = 0;
  for (ll i = 0; i < n - 2; i++) {
    for (ll j = i + 1; j < n - 1; j++) {
      ll x;
      if (l[j + 1] >= l[i] + l[j])
        continue;
      else if (l[n - 1] < l[i] + l[j])
        x = n - 1;
      else
        x = binary_search2(j + 1, n - 1, i, j, l);
      ans += x - (j + 1) + 1;
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 5;
int arr[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  ll res = 0;
  sort(arr, arr + n);
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j + 1 < n; ++j) {
      auto ptr = lower_bound(arr + j + 1, arr + n, arr[i] + arr[j]);
      res += ptr - (arr + j + 1);
    }
  cout << res << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2000 + 5;
int arr[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  ll res = 0;
  sort(arr, arr + n);
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j + 1 < n; ++j) {
      auto ptr = lower_bound(arr + j + 1, arr + n, arr[i] + arr[j]);
      res += ptr - (arr + j + 1);
    }
  cout << res << '\n';
  return 0;
}
Problem p02888 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;
//	std::cout<<std::fixed<<std::setprecision(10);

int main() {
  ll N;
  cin >> N;
  vector<int> L(N);
  rep(i, N) cin >> L[i];
  sort(L.begin(), L.end());

  vector<ll> dp; // dp[i]: L[i+2] までの棒を使用する．

  // dp[i+1] = (i+1番目を必ず使用) + dp[i];
  if (L[0] + L[1] > L[2])
    dp.push_back(1);
  else
    dp.push_back(0);

  for (int i = 1; i < N - 2; i++) {
    int cnt = 0;
    for (int j = i - 1 + 2; j > 0; j--) {
      for (int k = j - 1; k >= 0; k--) {
        if (L[j] + L[k] > L[i + 2])
          cnt++;
        // cout << "cnt " << cnt << ' ' << L[i+2] << ' '<< L[j] << ' '<< L[k] <<
        // endl;
      }
    }

    dp[i] = dp[i - 1] + cnt;
    // cout << "dp[i]: " << dp[i] << endl;
  }

  cout << dp[N - 3] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;
//	std::cout<<std::fixed<<std::setprecision(10);

int main() {
  ll N;
  cin >> N;
  vector<int> L(N);
  rep(i, N) cin >> L[i];
  sort(L.begin(), L.end());

  vector<ll> dp; // dp[i]: L[i+2] までの棒を使用する．

  // dp[i+1] = (i+1番目を必ず使用) + dp[i];
  if (L[0] + L[1] > L[2])
    dp.push_back(1);
  else
    dp.push_back(0);

  for (int i = 1; i < N - 2; i++) {
    int cnt = 0;
    for (int j = i - 1 + 2; j > 0; j--) {
      for (int k = j - 1; k >= 0; k--) {
        if (L[j] + L[k] > L[i + 2])
          cnt++;
        // cout << "cnt " << cnt << ' ' << L[i+2] << ' '<< L[j] << ' '<< L[k] <<
        // endl;
      }
    }

    dp.push_back(dp[i - 1] + cnt);
    // cout << "dp[i]: " << dp[i] << endl;
  }

  cout << dp[N - 3] << endl;

  return 0;
}
Problem p02888 (C++)

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

#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 2e3 + 1;
int arr[N];

int main() {
#ifdef LOCAL
  input_from_file;
  output_to_file;
#endif
  fastio;
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  sort(arr + 1, arr + n + 1);
  int ans = 0, x;
  for (int i = 1; i < n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      x = lower_bound(arr + 1, arr + n + 1, arr[i] + arr[j]) - arr;
      cerr << i << " " << j << " " << arr[i] + arr[j] << " " << x << endl;
      if (x - 1 > j)
        ans += x - j - 1;
    }
  }
  cout << ans;

  return 0;
}


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

#define show(x) cerr << #x << " = " << x << endl;
#define input_from_file freopen("input.txt", "r", stdin)
#define output_to_file freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'

const int N = 2e3 + 1;
int arr[N];

int main() {
#ifdef LOCAL
  input_from_file;
  output_to_file;
#endif
  fastio;
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  sort(arr + 1, arr + n + 1);
  int ans = 0, x;
  for (int i = 1; i < n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      x = lower_bound(arr + 1, arr + n + 1, arr[i] + arr[j]) - arr;
      if (x - 1 > j)
        ans += x - j - 1;
    }
  }
  cout << ans;

  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;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];

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

  return 0;
}


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

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(l.begin(), l.end());
  int result = 0;
  for (int i = 0; i < n - 2; ++i) {
    int a = l[i];
    for (int j = i + 1; j < n - 1; ++j) {
      int b = l[j];
      for (int k = j + 1; k < n; k++) {
        int c = l[k];
        if ((a < b + c) && (b < c + a) && (c < a + b)) {
          result++;
        }
      }
    }
  }
  cout << result << endl;

  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, tmp, ans = 0;
std::vector<int> data;

void solve() {
  sort(data.begin(), data.end());
  for (int i = n - 1; i >= 2; i--) {
    for (int j = i - 1; j >= 1; j--) {
      for (int k = j - 1; k >= 0; k--) {
        if (data.at(i) < data.at(j) + data.at(k)) {
          ans++;
        } else {
          break;
        }
      }
    }
  }

  cout << ans << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    data.push_back(tmp);
  }

  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, tmp, ans = 0;
std::vector<int> data;

void solve() {
  sort(data.begin(), data.end());
  reverse(data.begin(), data.end());
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      if (data.at(i) > data.at(j) * 2) {
        break;
      }
      for (int k = j + 1; k < n; k++) {
        if (data.at(i) < data.at(j) + data.at(k)) {
          ans++;
        } else {
          break;
        }
      }
    }
  }

  cout << ans << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    data.push_back(tmp);
  }

  solve();
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; }

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

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

  std::qsort(l, N, 4, compare_int);

  int j, k, m;
  int sum;
  int count = 0;
  for (j = N - 1; j > 1; j--) {

    for (k = j - 1; k > 0; k--) {
      if (l[j] > 2 * l[k])
        break;

      for (m = k - 1; m > -1; m--) {
        sum = l[k] + l[m];

        if (l[j] < sum && sum / 2 <= l[j]) {
          count++;
        }
      }
    }
  }
  cout << count << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int compare_int(const void *a, const void *b) { return *(int *)a - *(int *)b; }

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

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

  std::qsort(l, N, 4, compare_int);

  int j, k, m;
  int sum;
  int count = 0;
  for (j = N - 1; j > 1; j--) {

    for (k = j - 1; k > 0; k--) {
      if (l[j] > 2 * l[k])
        break;

      for (m = k - 1; m > -1; m--) {
        sum = l[k] + l[m];

        if (l[j] < sum && sum <= 2 * l[j]) {
          count++;
        }
      }
    }
  }
  cout << count << endl;

  return 0;
}
Problem p02888 (C++)

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

int main() {
  int N;
  int L[1002];
  long long ans = 0;

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> L[i];
  sort(L, L + N);
  // for(int i=0;i<N;i++) cout << L[i] << " ";
  // cout << endl;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      auto k = lower_bound(L + j + 1, L + N, L[i] + L[j]);
      long long temp = (k - L) - (j + 1);
      ans += temp;
      // cout << L[i] << " " <<  L[j] << " " << temp << endl;
      // cout << max(0LL, temp) << endl;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N;
  int L[10002];
  long long ans = 0;

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> L[i];
  sort(L, L + N);
  // for(int i=0;i<N;i++) cout << L[i] << " ";
  // cout << endl;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      auto k = lower_bound(L + j + 1, L + N, L[i] + L[j]);
      long long temp = (k - L) - (j + 1);
      ans += temp;
      // cout << L[i] << " " <<  L[j] << " " << temp << endl;
      // cout << max(0LL, temp) << endl;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> L(N);
  for (int i = 0; i < N; i++)
    cin >> L.at(i);
  sort(L.begin(), L.end());
  int ans = 0;
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; i < N - 1; j++) {
      int c = L.at(i) + L.at(j);
      auto itr = lower_bound(L.begin() + j + 1, L.end(), c);
      int k = distance(L.begin() + j + 1, itr);
      ans += k;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> L(N);
  for (int i = 0; i < N; i++)
    cin >> L.at(i);
  sort(L.begin(), L.end());
  int ans = 0;
  for (int i = 0; i < N - 2; i++) {
    for (int j = i + 1; j < N - 1; j++) {
      int c = L.at(i) + L.at(j);
      auto itr = lower_bound(L.begin() + j + 1, L.end(), c);
      int k = distance(L.begin() + j + 1, itr);
      ans += k;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02888 (C++)

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

using namespace std;

int solve(const vector<int> &L, int i, int j) {
  const auto ab = L[i] + L[j];
  const auto len = L.size();
  int left = 0, right = len - 1;
  int count = 0;
  for (;;) {
    const auto index = (left + right + 1) / 2;
    // printf("count=%d, left=%d, right=%d, index = %d\n", count, left, right,
    // index);
    if (L[index] < ab) {
      left = index;
    } else {
      right = index;
    }
    if (index == (left + right + 1) / 2) {
      break;
    }
  }

  // printf("left=%d, right=%d\n", left, right);

  int ans = 0;
  for (int k = j + 1; k <= left; ++k) {
    if (k == i || k == j) {
      continue;
    }
    if (L[i] >= L[j] + L[k]) {
      continue;
    }
    if (L[j] >= L[i] + L[k]) {
      continue;
    }
    if (L[k] >= L[i] + L[j]) {
      continue;
    }
    // printf("a=%d, b=%d, c=%d\n", L[i], L[j], L[k]);
    ++ans;
  }

  return ans;
}

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

  vector<int> L;
  L.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> L[i];
  }

  sort(L.begin(), L.end());
  // for (int i = 0; i < N; ++i) {
  //     cout << L[i] << endl;
  // }
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      const auto a = L[i];
      const auto b = L[j];
      const auto ab = a + b;
      ans += solve(L, i, j);
      // printf("a=%d, b=%d, ab=%d, ans=%d\n", a, b, ab, ans);
    }
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

int solve(const vector<int> &L, int i, int j) {
  const auto ab = L[i] + L[j];
  const auto len = L.size();
  int left = 0, right = len - 1;
  int count = 0;
  for (;;) {
    const auto index = (left + right + 1) / 2;
    // printf("count=%d, left=%d, right=%d, index = %d\n", count, left, right,
    // index);
    if (L[index] < ab) {
      left = index;
    } else {
      right = index;
    }
    if (index == (left + right + 1) / 2) {
      break;
    }
  }

  // printf("left=%d, right=%d\n", left, right);

  return left - j;
}

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

  vector<int> L;
  L.resize(N);
  for (int i = 0; i < N; ++i) {
    cin >> L[i];
  }

  sort(L.begin(), L.end());
  // for (int i = 0; i < N; ++i) {
  //     cout << L[i] << endl;
  // }
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      const auto a = L[i];
      const auto b = L[j];
      const auto ab = a + b;
      ans += solve(L, i, j);
      // printf("a=%d, b=%d, ab=%d, ans=%d\n", a, b, ab, ans);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02888 (C++)

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

int my_lower(vector<int> vec, int key, int nimin, int nimax) {
  int nimid = nimin + (nimax - nimin) / 2;
  if (vec.at(nimid) > key) {
    if (vec.at(nimid - 1) < key) {
      return nimid;
    }
    return my_lower(vec, key, nimin, nimid - 1);
  }
  if (vec.at(nimid) < key) {
    if (vec.at(nimid + 1) > key) {
      return nimid + 1;
    }
    return my_lower(vec, key, nimid + 1, nimax);
  }
  if (vec.at(nimid) == key) {
    for (int i = nimid; i >= nimin; i--) {
      if (vec.at(nimid) == vec.at(i)) {
        continue;
      }
      return i + 1;
    }
  }
}

int main() {
  int N;
  int ans = 0;
  cin >> N;
  vector<int> bou(N);
  for (int i = 0; i < N; i++) {
    cin >> bou.at(i);
  }
  sort(bou.begin(), bou.end());
  // a<=b<=cとすると、三角不等式はc<a+bと同値。
  // そこで、bouの中で添字が小さい順に見ていき、最初にa+b以上となる場所を二分探索で探す。
  for (int i = 0; i < N - 2; i++) {
    int bi = bou.at(i);
    for (int j = i + 1; j < N - 1; j++) {
      int di;
      if (bi + bou.at(j) > bou.at(N - 1)) {
        di = N - 1 - j;
      } else {
        di = my_lower(bou, bi + bou.at(j), 0, N - 1) - 1 - j;
      }
      ans += max(di, 0);
    }
  }
  cout << ans << endl;
}


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

int my_lower(vector<int> &vec, int key, int nimin, int nimax) {
  int nimid = nimin + (nimax - nimin) / 2;
  if (vec.at(nimid) > key) {
    if (vec.at(nimid - 1) < key) {
      return nimid;
    }
    return my_lower(vec, key, nimin, nimid - 1);
  }
  if (vec.at(nimid) < key) {
    if (vec.at(nimid + 1) > key) {
      return nimid + 1;
    }
    return my_lower(vec, key, nimid + 1, nimax);
  }
  if (vec.at(nimid) == key) {
    for (int i = nimid; i >= nimin; i--) {
      if (vec.at(nimid) == vec.at(i)) {
        continue;
      }
      return i + 1;
    }
  }
}

int main() {
  int N;
  int ans = 0;
  cin >> N;
  vector<int> bou(N);
  for (int i = 0; i < N; i++) {
    cin >> bou.at(i);
  }
  sort(bou.begin(), bou.end());
  // a<=b<=cとすると、三角不等式はc<a+bと同値。
  // そこで、bouの中で添字が小さい順に見ていき、最初にa+b以上となる場所を二分探索で探す。
  for (int i = 0; i < N - 2; i++) {
    int bi = bou.at(i);
    for (int j = i + 1; j < N - 1; j++) {
      int di;
      if (bi + bou.at(j) > bou.at(N - 1)) {
        di = N - 1 - j;
      } else {
        di = my_lower(bou, bi + bou.at(j), 0, N - 1) - 1 - j;
      }
      ans += max(di, 0);
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr long double EPS = 1e-15;
const long double PI = acos(-1);
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9 + 7;
constexpr ll MOD1 = 998244353;
typedef pair<ll, ll> P;
// #define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
// #define mp make_pair

void solve() {
  int n;
  int a[1010];
  cin >> n;
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  int 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]) {
          ans++;
        } else {
          break;
        }
      }
    }
  }
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  // /* Regular */
  // int case_num = 1;
  // if (argc > 1 && stoi(argv[1])) cin >> case_num;
  // REP(case_no, case_num) {
  //   cerr << endl << "case " << case_no + 1 << endl;
  //   solve();
  // }
  /* AOJ */
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr long double EPS = 1e-15;
const long double PI = acos(-1);
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9 + 7;
constexpr ll MOD1 = 998244353;
typedef pair<ll, ll> P;
// #define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
// #define mp make_pair

void solve() {
  int n;
  int a[2010];
  cin >> n;
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  int 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]) {
          ans++;
        } else {
          break;
        }
      }
    }
  }
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  // /* Regular */
  // int case_num = 1;
  // if (argc > 1 && stoi(argv[1])) cin >> case_num;
  // REP(case_no, case_num) {
  //   cerr << endl << "case " << case_no + 1 << endl;
  //   solve();
  // }
  /* AOJ */
  solve();
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int l[1000];
  for (int i = 0; i < n; ++i)
    cin >> l[i];
  sort(l, l + n);

  int cnt = 0;
  for (int i = 0; i < n - 2; ++i)
    for (int j = i + 1; j < n - 1; ++j)
      for (int k = n - 1; k > j; --k)
        if (l[i] + l[j] > l[k]) {
          cnt += k - j;
          break;
        }

  cout << cnt;
}


Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int l[2000];
  for (int i = 0; i < n; ++i)
    cin >> l[i];
  sort(l, l + n);

  int cnt = 0;
  for (int i = 0; i < n - 2; ++i)
    for (int j = i + 1; j < n - 1; ++j)
      for (int k = n - 1; k > j; --k)
        if (l[i] + l[j] > l[k]) {
          cnt += k - j;
          break;
        }

  cout << cnt;
}