prithivMLmods/Coder-Stat huggingface dataset:
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++)

typedef long long ll;

#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 INF 1000000000005 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  ll N;
  cin >> N;
  vector<int> L(N);
  rep(i, N) cin >> L[i];
  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 (L[i] + L[j] > L[k] && L[k] + L[i] > L[j] && L[j] + L[k] > L[i])
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

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

typedef long long ll;

#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 INF 1000000000005 // 10^9:極めて大きい値,∞
// 略記
#define PB push_back // vectorヘの挿入
#define MP make_pair // pairのコンストラクタ
#define F first      // pairの一つ目の要素
#define S second     // pairの二つ目の要素

int main() {
  ll N;
  cin >> N;
  vector<int> L(N);
  rep(i, N) cin >> L[i];
  ll ans = 0;
  sort(L.begin(), L.end());
  for (int a = 2; a < N; a++) {
    for (int b = 1; b < a; b++) {
      int c = upper_bound(L.begin(), L.begin() + b, L[a] - L[b]) - L.begin();
      ans += max(b - c, 0);
    }
  }
  cout << ans << 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;
int main(void) {
  int n;
  int cnt = 0;
  cin >> n;
  vector<int> L(n, 0);
  rep(i, n) cin >> L[i];
  sort(L.begin(), L.end());
  // rep(i, n) cout << L[i];
  rep(i, n) {
    rep(j, n) {
      rep(k, n) {
        if (i != j and i != k and j != k) {
          if (i < j and j < k) {
            if (L[i] + L[j] > L[k])
              cnt += 1;
          }
        }
      }
    }
  }
  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  int n;
  int cnt = 0;
  cin >> n;
  vector<int> L(n, 0);
  rep(i, n) cin >> L[i];
  sort(L.begin(), L.end());
  // rep(i, n) cout << L[i];
  rep(i, n) {
    rep(j, n) {
      if (i != j) {
        if (i < j) {
          for (int k = j + 1; k < n; k++) {
            if (L[i] + L[j] > L[k])
              cnt += 1;
          }
        }
      }
    }
  }
  cout << cnt;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
typedef long long ll;
#define PB push_back
#define FOR(n) for (int i = 0; i < n; ++i)
#define RFOR(n) for (int i = n; i >= 0; --i)
#define FORR(start, end) for (int i = start; i < end; ++i)
#define COUT(n) cout << n << " " << flush
#define vi(v) vector<int> v
#define vii(v) vector<pair<int, int>> v
#define mkp(a, b) make_pair(a, b)
#define ALL(obj) obj.begin(), obj.end()
using namespace std;

int main() {
  int n;
  vector<ll> L(1000);
  ll cou = 0;
  cin >> n;
  FOR(n)
  cin >> L[i];

  sort(ALL(L), greater<ll>());
  FOR(n - 2) {
    ll longest = L[i];
    for (int j = i + 1; j < n - 1; ++j) {
      for (int k = j + 1; k < n; ++k)
        if (longest < L[j] + L[k]) {
          ++cou;
        } else {
          break;
        }
    }
  }

  cout << cou << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
typedef long long ll;
#define PB push_back
#define FOR(n) for (int i = 0; i < n; ++i)
#define RFOR(n) for (int i = n; i >= 0; --i)
#define FORR(start, end) for (int i = start; i < end; ++i)
#define COUT(n) cout << n << " " << flush
#define vi(v) vector<int> v
#define vii(v) vector<pair<int, int>> v
#define mkp(a, b) make_pair(a, b)
#define ALL(obj) obj.begin(), obj.end()
using namespace std;

int main() {
  int n;
  vector<ll> L(2000);
  ll cou = 0;
  cin >> n;
  FOR(n)
  cin >> L[i];

  sort(ALL(L), greater<ll>());
  FOR(n - 2) {
    ll longest = L[i];
    for (int j = i + 1; j < n - 1; ++j) {
      for (int k = j + 1; k < n; ++k)
        if (longest < L[j] + L[k]) {
          ++cou;
        } else {
          break;
        }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  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++) {
    int a = L[i];
    for (int j = i + 1; j < N - 1; j++) {
      int b = L[j];

      int left = b - a > a - b ? b - a + 1 : a - b + 1;
      int right = a + b;

      int l = lower_bound(L.begin() + j + 1, L.end(), left) - L.begin();
      /*
      if(l == 0) {
          l = j + 1;
      }*/
      int r = lower_bound(L.begin() + j + 1, L.end(), right) - L.begin();
      /*
      if(r == 0) {
          r = N + 1;
      }*/
      cerr << left << " " << right << endl;
      cerr << l << " " << r << endl;

      //            if(r > l) {
      count += r - l;
      //            }
    }
  }

  cout << count << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  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++) {
    int a = L[i];
    for (int j = i + 1; j < N - 1; j++) {
      int b = L[j];

      int left = b - a > a - b ? b - a + 1 : a - b + 1;
      int right = a + b;

      int l = lower_bound(L.begin() + j + 1, L.end(), left) - L.begin();
      /*
      if(l == 0) {
          l = j + 1;
      }*/
      int r = lower_bound(L.begin() + j + 1, L.end(), right) - L.begin();
      /*
      if(r == 0) {
          r = N + 1;
      }*/
      // cerr << left << " " << right << endl;
      // cerr << l << " " << r << endl;

      //            if(r > l) {
      count += r - l;
      //            }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
int main() {
  int n;
  std::cin >> n;
  std::vector<int> sticks(n);
  for (auto &s : sticks)
    std::cin >> s;
  std::sort(sticks.begin(), sticks.end());
  int count = 0;
  for (auto i = 0; i < n; ++i)
    for (auto j = 0; j < n; ++j)
      for (auto k = 0; k < n; ++k) {
        if (i < j && j < k && sticks[i] + sticks[j] > sticks[k]) {
          ++count;
        }
      }
  std::cout << count << std::endl;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
int main() {
  int n;
  std::cin >> n;
  std::vector<int> sticks(n);
  for (auto &s : sticks)
    std::cin >> s;
  std::sort(sticks.begin(), sticks.end());
  int count = 0;
  for (auto i = 0; i < n; ++i)
    for (auto j = i + 1; j < n; ++j)
      for (auto k = j + 1; k < n; ++k) {
        if (i < j && j < k && sticks[i] + sticks[j] > sticks[k]) {
          ++count;
        }
      }
  std::cout << count << std::endl;
}
Problem p02888 (C++)

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

int main() {
  int N;
  cin >> N;
  int L[1001];
  for (int i = 0; i < N; ++i)
    cin >> L[i];

  sort(L, L + N);

  int ans = 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) {
        int a = L[i];
        int b = L[j];
        int c = L[k];
        if ((a < (b + c)) && (b < (c + a)) && (c < (a + b))) {
          ans++;
        }
      }
    }
  }

  cout << ans << endl;
}


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

int main() {
  int N;
  cin >> N;
  int L[2001];
  for (int i = 0; i < N; ++i)
    cin >> L[i];

  sort(L, L + N);

  int ans = 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) {
        int a = L[i];
        int b = L[j];
        int c = L[k];
        if ((a < (b + c)) && (b < (c + a)) && (c < (a + b))) {
          ans++;
        }
      }
    }
  }

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

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

int main() {
  int N, R = 0, flg = 0;
  cin >> N;
  vector<int> L(N);
  for (int i = 0; i < N; i++) {
    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++) {
      flg = 0;
      for (int k = j + 1; k < N; k++) {
        if (L.at(k) < L.at(i) + L.at(j)) {
          R++;
        } else {
          flg = 1;
        }
        if ((flg == 1) && (L.at(k) != L.at(k + 1))) {
          break;
        }
      }
    }
  }

  cout << R << endl;
}

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

int main() {
  int N, R = 0, flg = 0;
  cin >> N;
  vector<int> L(N);
  for (int i = 0; i < N; i++) {
    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++) {
      flg = 0;
      for (int k = j + 1; k < N; k++) {
        if (L.at(k) < L.at(i) + L.at(j)) {
          R++;
        } else {
          flg = 1;
        }
        if ((flg == 1) && (k != N - 1)) {
          if (L.at(k) != L.at(k + 1)) {
            break;
          }
        }
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ROOP(i, N) for (int i = 0; i++; i < N)
#define RVROOP(i, N) for (int i = N; i--; i >= 0)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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 = N - 1; i > 0; i--) {
    for (int j = i - 1; j > 0; j--) {
      for (int k = j - 1; k >= 0; k--) {
        if (L.at(k) + L.at(j) > L.at(i)) {
          ans++;
        } else {
          break;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ROOP(i, N) for (int i = 0; i++; i < N)
#define RVROOP(i, N) for (int i = N; i--; i >= 0)
#define INF 1e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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 = N - 1; i >= 2; i--) {
    if (L.at(0) + L.at(1) > L.at(i)) {
      ans += i * (i - 1) / 2;
    } else {
      for (int j = i - 1; j > 0; j--) {
        for (int k = j - 1; k >= 0; k--) {
          if (L.at(k) + L.at(j) > L.at(i)) {
            ans++;
          } else {
            break;
          }
        }
      }
    }
  }

  cout << ans << endl;

  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> L(N);
  for (int i = 0; i < N; i++) {
    cin >> L[i];
  }
  int ans = 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[i] + L[k] && L[k] < L[i] + L[j]) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}


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[i];
  }
  int ans = 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 (max({L[i], L[j], L[k]}) * 2 < L[i] + L[j] + L[k]) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> /*sort()を使用するのに必要*/
#include <iostream>
#include <vector>

using namespace std;

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

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

  // 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++) {

      // 二分探索で3つめを絞る
      /*nt start = j + 1;
      int end = N - 1;
      int middle = (start + end) / 2;
      int k;
      while(start != end){
          if(L[middle] >= L[i] + L[j]){
              end = middle - 1;
          }else{
              continue;
          }
      }
      */

      for (int k = j + 1; k < N; k++) {
        if (L[i] >= L[j] + L[k])
          continue;
        if (L[j] >= L[i] + L[k])
          continue;
        if (L[k] >= L[i] + L[j])
          continue; // すでにやっている
        count++;
      }
    }
  }

  cout << count;

  return 0;
}

Fixed submission:
#include <algorithm> /*sort()を使用するのに必要*/
#include <iostream>
#include <vector>

using namespace std;

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

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

  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++) {

      // 二分探索で3つめを絞る
      /*nt start = j + 1;
      int end = N - 1;
      int middle = (start + end) / 2;
      int k;
      while(start != end){
          if(L[middle] >= L[i] + L[j]){
              end = middle - 1;
          }else{
              continue;
          }
      }
      */

      for (int k = j + 1; k < N; k++) {
        if (L[i] >= L[j] + L[k])
          continue;
        if (L[j] >= L[i] + L[k])
          continue;
        if (L[k] >= L[i] + L[j])
          continue; // すでにやっている
        count++;
      }
    }
  }

  cout << count;

  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define re return
#define vi vector<int>
#define pb push_back
#define si set<int>
#define in insert
#define fl float
#define db double
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
using namespace std;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
int main() {
  int n;
  int a[1111];
  cin >> n;
  REP(i, n)
  cin >> a[i];
  sort(a, a + n);
  ll ans = 0;
  REP(i, n)
  for (int j = i + 1; j < n; j++) {
    int l = j + 1;
    int r = n - 1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (a[i] + a[j] > a[mid])
        l = mid + 1;
      else
        r = mid - 1;
    }
    ans += l - j - 1;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--)
#define REP(i, n) for (int i = 0, _n = n; i < n; i++)
#define ll long long
#define pii pair<int, int>
#define re return
#define vi vector<int>
#define pb push_back
#define si set<int>
#define in insert
#define fl float
#define db double
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
using namespace std;
inline void read(int &x) {
  short negative = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      negative = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  x *= negative;
}
int main() {
  int n;
  int a[3111];
  cin >> n;
  REP(i, n)
  cin >> a[i];
  sort(a, a + n);
  ll ans = 0;
  REP(i, n)
  for (int j = i + 1; j < n; j++) {
    int l = j + 1;
    int r = n - 1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (a[i] + a[j] > a[mid])
        l = mid + 1;
      else
        r = mid - 1;
    }
    ans += l - j - 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02888 (C++)

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

using namespace std;

const int L = 1000;

int main() {
  ios::sync_with_stdio(false);

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

  int ps[L + 1];
  for (int i = 0; i <= L; i++) {
    ps[i] = i == 0 ? 0 : ps[i - 1] + cnt[i];
  }

  int ans = 0;
  for (int i = 1; i <= L; i++) {
    ans += 1ll * cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6;
  }

  for (int i = 1; i <= L; i++) {
    ans += cnt[i] * (cnt[i] - 1) / 2 * (ps[2 * i - 1] - cnt[i]);
  }

  for (int i = 1; i <= L; i++) {
    for (int j = i + 1; j <= L; j++) {
      ans += cnt[i] * cnt[j] * (ps[min(i + j - 1, L)] - ps[j]);
    }
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

const int L = 1000;

int main() {
  ios::sync_with_stdio(false);

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

  int ps[L + 1];
  for (int i = 0; i <= L; i++) {
    ps[i] = i == 0 ? 0 : ps[i - 1] + cnt[i];
  }

  int ans = 0;
  for (int i = 1; i <= L; i++) {
    ans += 1ll * cnt[i] * (cnt[i] - 1) * (cnt[i] - 2) / 6;
  }

  for (int i = 1; i <= L; i++) {
    ans += cnt[i] * (cnt[i] - 1) / 2 * (ps[min(2 * i - 1, L)] - cnt[i]);
  }

  for (int i = 1; i <= L; i++) {
    for (int j = i + 1; j <= L; j++) {
      ans += cnt[i] * cnt[j] * (ps[min(i + j - 1, L)] - ps[j]);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Runtime Error):
/* Author: Dhruv Rastogi */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define all(sdk) sdk.begin(), sdk.end()
#define mll map<int, int>
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define mp make_pair
#define hell 1000000007
#define endl '\n'
#define vvll vector<vector<int>>
#define vll vector<int>
#define mint map<int, int>
#define sz(x) (int)x.size()
#define sll set<int>
#define pll pair<int, int>
#define F first
#define S second
const long double PI = acos(-1.0);

int a[1005];

void solve() {
  int n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  sort(a, a + n);
  int ans = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      int idx = lower_bound(a, a + n, a[i] + a[j]) - a - 1;
      ans += idx - j;
    }
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
/* Author: Dhruv Rastogi */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define all(sdk) sdk.begin(), sdk.end()
#define mll map<int, int>
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define mp make_pair
#define hell 1000000007
#define endl '\n'
#define vvll vector<vector<int>>
#define vll vector<int>
#define mint map<int, int>
#define sz(x) (int)x.size()
#define sll set<int>
#define pll pair<int, int>
#define F first
#define S second
const long double PI = acos(-1.0);

int a[2005];

void solve() {
  int n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  sort(a, a + n);
  int ans = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      int idx = lower_bound(a, a + n, a[i] + a[j]) - a - 1;
      ans += idx - j;
    }
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define vint vector<int>
#define rep(x, y) for (int i = x; i < y; i++)
#define REP(x, y) for (int j = x; j < y; j++)
#define all(x) x.begin(), x.end()
#define pb() push_back()
#define mod 100000007
#define elif else if
#define str string
// main
signed main() {
  int N, M, O, Q, count = 0, count2 = 0, count3 = 0;
  cin >> N;
  vint L(N);
  rep(0, N) cin >> L[i];
  sort(all(L));
  rep(0, N - 2) {
    REP(i + 1, N - 1) {
      for (int k = j + 1; k < N; k++) {
        if (i != j || i != k || j != k) {
          if (L[i] < L[j] + L[k] && L[j] < L[i] + L[k] && L[k] < L[i] + L[j])
            count += 1;
        }
      }
    }
  }
  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define vint vector<int>
#define rep(x, y) for (int i = x; i < y; i++)
#define REP(x, y) for (int j = x; j < y; j++)
#define all(x) x.begin(), x.end()
#define pb() push_back()
#define mod 100000007
#define elif else if
#define str string
// main
signed main() {
  int N, M, O, Q, count = 0, count2 = 0, count3 = 0;
  cin >> N;
  vint L(N);
  rep(0, N) cin >> L[i];
  sort(all(L));
  rep(0, N - 2) {
    REP(i + 1, N - 1) {
      for (int k = j + 1; k < N; k++) {
        if (i != j || i != k || j != k) {
          if (L[k] < L[i] + L[j])
            count += 1;
        }
      }
    }
  }
  cout << count << endl;
}
Problem p02888 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <stdlib.h>

int ascSort(const void *v1, const void *v2);

int ascSort(const void *v1, const void *v2) {
  const int _v1 = *((const int *)v1);
  const int _v2 = *((const int *)v2);

  if (_v1 > _v2) {
    return 1;
  } else if (_v1 < _v2) {
    return -1;
  } else {
    return 0;
  }
}

int main(void) {
  int N, L[2 * 1000];
  int a, b, c, i;
  int ans = 0;

  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &L[i]);
  }

  qsort(L, N, sizeof(L[0]), ascSort);

  for (a = 0; a < N; a++) {
    for (b = a + 1; b < N; b++) {
      for (c = b + 1; (c < N) || (L[a] + L[b] < L[c]); c++) {
        if ((L[a] < L[b] + L[c]) && (L[b] < L[a] + L[c]) &&
            (L[c] < L[a] + L[b])) {
          ans++;
        }
      }
    }
  }
  printf("%d", ans);

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>

int ascSort(const void *v1, const void *v2);

int ascSort(const void *v1, const void *v2) {
  const int _v1 = *((const int *)v1);
  const int _v2 = *((const int *)v2);

  if (_v1 > _v2) {
    return 1;
  } else if (_v1 < _v2) {
    return -1;
  } else {
    return 0;
  }
}

int main(void) {
  int N, L[2 * 1000];
  int a, b, c, i;
  int ans = 0;

  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &L[i]);
  }

  qsort(L, N, sizeof(L[0]), ascSort);

  for (a = 0; a < N; a++) {
    for (b = a + 1; b < N; b++) {
      for (c = b + 1; (c < N) && (L[c] < L[a] + L[b]); c++) {
        if ((L[a] < L[b] + L[c]) && (L[b] < L[a] + L[c]) &&
            (L[c] < L[a] + L[b])) {
          ans++;
        }
      }
    }
  }
  printf("%d", ans);

  return 0;
}
Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> Pl;
const ll INF = 1000000000000000;
const int MAX = 310;
vector<pair<ll, ll>> G[MAX];                     // （行き先,cost）
priority_queue<Pl, vector<Pl>, greater<Pl>> que; //(count,energy,位置)
ll ans[310][310];
void dijkstra(int s, int l) {
  que.push({{0, -l}, s});
  while (!que.empty()) {
    Pl p = que.top();
    que.pop();
    ll x = p.second;
    ll cnt = p.first.first, en = -(p.first.second);
    if (ans[s][x] != INF)
      continue;
    ans[s][x] = cnt;
    for (P e : G[x]) {
      if (en >= e.second) {
        que.push({{cnt, -(en - e.second)}, e.first});
      } else {
        que.push({{cnt + 1, -(l - e.second)}, e.first});
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, n, m, q;
  ll l;
  cin >> n >> m >> l;
  for (i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c > l)
      continue;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      ans[i][j] = INF;
    }
  }
  for (i = 0; i < n; i++) {
    dijkstra(i, l);
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (ans[s][t] < INF)
      cout << ans[s][t] << endl;
    else
      cout << -1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> Pl;
const ll INF = 1000000000000000;
const int MAX = 310;
vector<pair<ll, ll>> G[MAX];                     // （行き先,cost）
priority_queue<Pl, vector<Pl>, greater<Pl>> que; //(count,energy,位置)
ll ans[310][310];
void dijkstra(int s, int l) {
  que.push({{0, -l}, s});
  while (!que.empty()) {
    Pl p = que.top();
    que.pop();
    ll x = p.second;
    ll cnt = p.first.first, en = -(p.first.second);
    if (ans[s][x] != INF)
      continue;
    ans[s][x] = cnt;
    for (P e : G[x]) {
      if (ans[s][e.first] != INF)
        continue;
      if (en >= e.second) {
        que.push({{cnt, -(en - e.second)}, e.first});
      } else {
        que.push({{cnt + 1, -(l - e.second)}, e.first});
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, n, m, q;
  ll l;
  cin >> n >> m >> l;
  for (i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c > l)
      continue;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      ans[i][j] = INF;
    }
  }
  for (i = 0; i < n; i++) {
    dijkstra(i, l);
  }
  cin >> q;
  for (i = 0; i < q; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (ans[s][t] < INF)
      cout << ans[s][t] << endl;
    else
      cout << -1 << endl;
  }
}
Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <forward_list>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 62) - 1;

int main(void) {
  int n, m, l;
  int a, b, c;
  int q;
  vector<int> s(10010), t(10010);
  ll d[500][500], count[500][500];
  int i, j, k;

  cin >> n >> m >> l;

  rep(i, 0, n) {
    rep(j, 0, n) {
      if (i == j) {
        d[i][j] = 0;
      } else {
        d[i][j] = INF;
      }
    }
  }

  rep(i, 0, m) {
    cin >> a >> b >> c;

    d[a - 1][b - 1] = c;
    d[b - 1][a - 1] = c;
  }

  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);

        if (j == k) {
          count[j][k] = 0;
        } else if (d[j][k] <= l) {
          count[j][k] = 1;
        } else {
          count[j][k] = INF;
        }
      }
    }
  }

  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        count[j][k] = min(count[j][k], count[j][i] + count[i][k]);
      }
    }
  }

  cin >> q;

  rep(i, 0, q) { cin >> s[i] >> t[i]; }

  rep(i, 0, q) {
    if (count[s[i] - 1][t[i] - 1] == INF) {
      cout << -1 << endl;
    } else {
      cout << count[s[i] - 1][t[i] - 1] - 1 << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <forward_list>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 62) - 1;

int main(void) {
  int n, m, l;
  int a, b, c;
  int q;
  vector<int> s(100010), t(100010);
  ll d[500][500], count[500][500];
  int i, j, k;

  cin >> n >> m >> l;

  rep(i, 0, n) {
    rep(j, 0, n) {
      if (i == j) {
        d[i][j] = 0;
      } else {
        d[i][j] = INF;
      }
    }
  }

  rep(i, 0, m) {
    cin >> a >> b >> c;

    d[a - 1][b - 1] = c;
    d[b - 1][a - 1] = c;
  }

  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);

        if (j == k) {
          count[j][k] = 0;
        } else if (d[j][k] <= l) {
          count[j][k] = 1;
        } else {
          count[j][k] = INF;
        }
      }
    }
  }

  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        count[j][k] = min(count[j][k], count[j][i] + count[i][k]);
      }
    }
  }

  cin >> q;

  rep(i, 0, q) { cin >> s[i] >> t[i]; }

  rep(i, 0, q) {
    if (count[s[i] - 1][t[i] - 1] == INF) {
      cout << -1 << endl;
    } else {
      cout << count[s[i] - 1][t[i] - 1] - 1 << endl;
    }
  }
}
Problem p02889 (C++)

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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri();
  int m = ri();
  int l = ri();
  std::vector<std::pair<int, int>> hen[n];
  for (int i = 0; i < m; i++) {
    int a = ri() - 1, b = ri() - 1, c = ri();
    hen[a].push_back({b, c});
    hen[b].push_back({a, c});
  }
  int res[n][n];
  for (int i = 0; i < n; i++) {
    std::vector<std::pair<int, int>> dist(n, {1000000001, 0});
    dist[i] = {0, 0};
    using T = std::pair<std::pair<int, int>, int>;
    std::priority_queue<T, std::vector<T>, std::greater<T>> que;
    que.push({{0, 0}, i});
    while (que.size()) {
      auto cur = que.top();
      que.pop();
      int i = cur.second;
      int cnt = cur.first.first;
      int left = l - cur.first.second;
      for (auto j : hen[i]) {
        if (j.second > l)
          continue;
        std::pair<int, int> new_cost;
        if (j.second > left) {
          new_cost = {cnt + 1, j.second};
        } else
          new_cost = {cnt, cur.first.second + j.second};
        if (new_cost < dist[j.first])
          dist[j.first] = new_cost, que.push({new_cost, j.first});
      }
    }
    for (int j = 0; j < n; j++)
      res[i][j] = dist[j].first == 1000000001 ? -1 : dist[j].first;
  }
  int q = ri();
  for (int i = 0; i < q; i++) {
    int a = ri() - 1, b = ri() - 1;
    std::cout << res[a][b] << std::endl;
  }
  return 0;
}


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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri();
  int m = ri();
  int l = ri();
  std::vector<std::pair<int, int>> hen[n];
  for (int i = 0; i < m; i++) {
    int a = ri() - 1, b = ri() - 1, c = ri();
    hen[a].push_back({b, c});
    hen[b].push_back({a, c});
  }
  int res[n][n];
  for (int i = 0; i < n; i++) {
    std::vector<std::pair<int, int>> dist(n, {1000000001, 0});
    dist[i] = {0, 0};
    using T = std::pair<std::pair<int, int>, int>;
    std::priority_queue<T, std::vector<T>, std::greater<T>> que;
    que.push({{0, 0}, i});
    while (que.size()) {
      auto cur = que.top();
      que.pop();
      if (cur.first != dist[cur.second])
        continue;
      int i = cur.second;
      int cnt = cur.first.first;
      int left = l - cur.first.second;
      for (auto j : hen[i]) {
        if (j.second > l)
          continue;
        std::pair<int, int> new_cost;
        if (j.second > left) {
          new_cost = {cnt + 1, j.second};
        } else
          new_cost = {cnt, cur.first.second + j.second};
        if (new_cost < dist[j.first])
          dist[j.first] = new_cost, que.push({new_cost, j.first});
      }
    }
    for (int j = 0; j < n; j++)
      res[i][j] = dist[j].first == 1000000001 ? -1 : dist[j].first;
  }
  int q = ri();
  for (int i = 0; i < q; i++) {
    int a = ri() - 1, b = ri() - 1;
    std::cout << res[a][b] << std::endl;
  }
  return 0;
}
Problem p02889 (C++)

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

typedef long long ll;

ll n, m, l;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> l;

  ll adj[n][n];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      adj[i][j] = (i == j) ? 0 : 1e13;

  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    adj[a][b] = adj[b][a] = c;
  }

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      adj[i][j] = adj[i][j] <= l ? 1 : 1e13;
      if (i == j)
        adj[i][j] = 0;
    }
  }

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }

  int q;
  cin >> q;

  while (q--) {
    int s, t;
    cin >> s >> t;
    s--, t--;
    ll ans = adj[s][t];

    if (ans >= 1e13)
      cout << -1 << "\n";
    else
      cout << (ans - 1) << "\n";
  }
}

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

typedef long long ll;

ll n, m, l;

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> l;

  ll adj[n][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      adj[i][j] = (i == j) ? 0 : 1e13;

  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    adj[a][b] = adj[b][a] = c;
  }

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      adj[i][j] = adj[i][j] <= l ? 1 : 1e13;
      if (i == j)
        adj[i][j] = 0;
    }
  }

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
      }
    }
  }

  int q;
  cin >> q;

  while (q--) {
    int s, t;
    cin >> s >> t;
    s--, t--;
    ll ans = adj[s][t];

    if (ans >= 1e13)
      cout << -1 << "\n";
    else
      cout << (ans - 1) << "\n";
  }
}
Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define uqv(v) v.erase(unique(all(v)), v.end());
#define INF 1e9
#define LINF 1e18
#define gt(tp, x) get<x>(tp)

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

int ans[303][303];
bool chk[303][303];
vector<pii> g[303];

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

  int n, m, l;
  cin >> n >> m >> l;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (w > l)
      continue;
    g[u].eb(v, w), g[v].eb(u, w);
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ans[i][j] = -1;
    }
  }

  for (int i = 1; i <= n; i++) {
    priority_queue<tp3> pq;
    pq.push(make_tuple(0, l, i));
    while (!pq.empty()) {
      int x = gt(pq.top(), 2), f = gt(pq.top(), 1), dg = -gt(pq.top(), 0);
      pq.pop();
      if (chk[i][x])
        continue;
      chk[i][x] = true;
      ans[i][x] = dg;
      for (auto j : g[x]) {
        if (j.se > f)
          pq.push(make_tuple(-dg - 1, l - j.se, j.fi));
        else
          pq.push(make_tuple(-dg, f - j.se, j.fi));
      }
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int s, t;
    cin >> s >> t;
    cout << ans[s][t] << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define uqv(v) v.erase(unique(all(v)), v.end());
#define INF 1e9
#define LINF 1e18
#define gt(tp, x) get<x>(tp)

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

int ans[303][303];
bool chk[303][303];
vector<pii> g[303];

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

  int n, m, l;
  cin >> n >> m >> l;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (w > l)
      continue;
    g[u].eb(v, w), g[v].eb(u, w);
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ans[i][j] = -1;
    }
  }

  for (int i = 1; i <= n; i++) {
    priority_queue<tp3> pq;
    pq.push(make_tuple(0, l, i));
    while (!pq.empty()) {
      int x = gt(pq.top(), 2), f = gt(pq.top(), 1), dg = -gt(pq.top(), 0);
      pq.pop();
      if (chk[i][x])
        continue;
      chk[i][x] = true;
      ans[i][x] = dg;
      for (auto j : g[x]) {
        if (chk[i][j.fi])
          continue;
        if (j.se > f)
          pq.push(make_tuple(-dg - 1, l - j.se, j.fi));
        else
          pq.push(make_tuple(-dg, f - j.se, j.fi));
      }
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int s, t;
    cin >> s >> t;
    cout << ans[s][t] << '\n';
  }
}
Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB push_back
#define PF push_front
#define FILL(a, b) memset(a, b, sizeof(a))
using namespace std;
P<int, int> dis[305][305];
V<P<int, int>> v[305];
int n;
#define PP pair<pair<int, int>, int>
priority_queue<PP, vector<PP>, greater<PP>> q;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int m, l;
  cin >> n >> m >> l;
  int x, y, z;
  FOR(i, 1, m) {
    cin >> x >> y >> z;
    if (z > l)
      continue;
    v[x].PB(MP(y, z));
    v[y].PB(MP(x, z));
  }
  FOR(i, 1, n) { FOR(j, 1, n) dis[i][j] = MP(-1, 0); }
  FOR(i, 1, n) {
    q.push(MP(MP(0, 0), i));
    while (!q.empty()) {
      PP t = q.top();
      q.pop();
      if (dis[i][t.S].F != -1)
        continue;
      dis[i][t.S] = t.F;
      for (int j = 0; j < v[t.S].size(); j++) {
        if (v[t.S][j].S + t.F.S > l) {
          q.push(MP(MP(t.F.F + 1, v[t.S][j].S), v[t.S][j].F));
        } else {
          q.push(MP(MP(t.F.F, t.F.S + v[t.S][j].S), v[t.S][j].F));
        }
      }
    }
  }
  int q, s1, s2;
  cin >> q;
  FOR(i, 1, q) {
    cin >> s1 >> s2;
    cout << dis[s1][s2].F << '\n';
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB push_back
#define PF push_front
#define FILL(a, b) memset(a, b, sizeof(a))
using namespace std;
P<int, int> dis[305][305];
V<P<int, int>> v[305];
int n;
#define PP pair<pair<int, int>, int>
priority_queue<PP, vector<PP>, greater<PP>> q;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int m, l;
  cin >> n >> m >> l;
  int x, y, z;
  FOR(i, 1, m) {
    cin >> x >> y >> z;
    if (z > l)
      continue;
    v[x].PB(MP(y, z));
    v[y].PB(MP(x, z));
  }
  FOR(i, 1, n) { FOR(j, 1, n) dis[i][j] = MP(-1, 0); }
  FOR(i, 1, n) {
    q.push(MP(MP(0, 0), i));
    while (!q.empty()) {
      PP t = q.top();
      q.pop();
      if (dis[i][t.S].F != -1)
        continue;
      dis[i][t.S] = t.F;
      for (int j = 0; j < v[t.S].size(); j++) {
        if (dis[i][v[t.S][j].F].F != -1)
          continue;
        if (v[t.S][j].S + t.F.S > l) {
          q.push(MP(MP(t.F.F + 1, v[t.S][j].S), v[t.S][j].F));
        } else {
          q.push(MP(MP(t.F.F, t.F.S + v[t.S][j].S), v[t.S][j].F));
        }
      }
    }
  }
  int q, s1, s2;
  cin >> q;
  FOR(i, 1, q) {
    cin >> s1 >> s2;
    cout << dis[s1][s2].F << '\n';
  }
  return 0;
}
Problem p02889 (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 dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  vector<vector<ll>> dist1(n, vector<ll>(n, LINF));
  rep(i, n) dist1[i][i] = 0;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    dist1[a][b] = c;
    dist1[b][a] = c;
  }
  int q;
  cin >> q;
  vector<pii> v(q);
  rep(i, q) {
    cin >> v[i].first >> v[i].second;
    v[i].first--, v[i].second--;
  }

  rep(i, m) rep(k, n) {
    rep(i, n) {
      rep(j, n) { dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]); }
    }
  }

  vector<vector<ll>> dist2(n, vector<ll>(n, LINF));
  rep(i, n) {
    rep(j, n) { dist2[i][j] = (dist1[i][j] <= l ? 1 : LINF); }
  }
  rep(k, n) {
    rep(i, n) {
      rep(j, n) { dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]); }
    }
  }
  rep(i, q) {
    if (dist2[v[i].first][v[i].second] == LINF)
      cout << -1 << endl;
    else
      cout << dist2[v[i].first][v[i].second] - 1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dup(x, y) (((x) + (y)-1) / (y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  vector<vector<ll>> dist1(n, vector<ll>(n, LINF));
  rep(i, n) dist1[i][i] = 0;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    dist1[a][b] = c;
    dist1[b][a] = c;
  }
  int q;
  cin >> q;
  vector<pii> v(q);
  rep(i, q) {
    cin >> v[i].first >> v[i].second;
    v[i].first--, v[i].second--;
  }

  rep(k, n) {
    rep(i, n) {
      rep(j, n) { dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]); }
    }
  }

  vector<vector<ll>> dist2(n, vector<ll>(n, LINF));
  rep(i, n) {
    rep(j, n) { dist2[i][j] = (dist1[i][j] <= l ? 1 : LINF); }
  }
  rep(k, n) {
    rep(i, n) {
      rep(j, n) { dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]); }
    }
  }
  rep(i, q) {
    if (dist2[v[i].first][v[i].second] == LINF)
      cout << -1 << endl;
    else
      cout << dist2[v[i].first][v[i].second] - 1 << endl;
  }
}
Problem p02889 (C++)

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

#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 300 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320;
const ll INFLL = 1e17;
ll N, M, L, Q;
ll dis[MN][MN], f[MN][MN];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  cin >> N >> M >> L;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      dis[i][j] = f[i][j] = INFLL;
    }
    dis[i][i] = f[i][i] = 0;
  }
  for (int i = 0; i < M; i++) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    dis[u][v] = min(dis[u][v], w);
    dis[v][u] = min(dis[v][u], w);
  }

  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i == j)
        continue;
      if (dis[i][j] <= L)
        f[i][j] = min(f[i][j], 1LL);
    }
  }

  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
      }
    }
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int s, t;
    cin >> s >> t;
    if (f[s][t] < INFLL) {
      cout << f[s][t] - 1 << '\n';
    } else
      cout << -1 << '\n';
  }
  return 0;
}


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

#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 300 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f, BSZ = 320;
const ll INFLL = 1e17;
ll N, M, L, Q;
ll dis[MN][MN], f[MN][MN];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  cin >> N >> M >> L;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      dis[i][j] = f[i][j] = INFLL;
    }
    dis[i][i] = f[i][i] = 0;
  }
  for (int i = 0; i < M; i++) {
    int u, v;
    ll w;
    cin >> u >> v >> w;
    dis[u][v] = min(dis[u][v], w);
    dis[v][u] = min(dis[v][u], w);
  }

  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (i == j)
        continue;
      if (dis[i][j] <= L)
        f[i][j] = min(f[i][j], 1LL);
    }
  }

  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
      }
    }
  }

  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int s, t;
    cin >> s >> t;
    if (f[s][t] < INFLL) {
      cout << f[s][t] - 1 << '\n';
    } else
      cout << -1 << '\n';
  }
  return 0;
}