Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define oneforall                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define ini(...)                                                               \
  int __VA_ARGS__;                                                             \
  in(__VA_ARGS__)
#define inl(...)                                                               \
  long long __VA_ARGS__;                                                       \
  in(__VA_ARGS__)
#define ins(...)                                                               \
  string __VA_ARGS__;                                                          \
  in(__VA_ARGS__)
const long long INF = 1e18;
void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U> void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u))
    cout << " ";
  out(u...);
}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;
int main() {
  oneforall oneforall oneforall oneforall oneforall oneforall oneforall
      oneforall oneforall oneforall oneforall oneforall oneforall oneforall inl(
          n);
  inl(k);
  ll a[10000];
  FOR(i, 0, n) { in(a[i]); }
  sort(a, a + n);
  ll l = -INF;
  ll r = INF;
  while (r - l > 1) {
    ll mid = (l + r) >> 1;
    ll tmp = 0;
    FOR(i, 0, n) {
      if (a[i] >= 0) {
        if (a[i] * a[0] > mid)
          continue;
        ll l1 = i;
        ll r1 = n;
        while (r1 - l1 > 1) {
          ll mid1 = (r1 + l1) >> 1;
          if (a[i] * a[mid1] > mid)
            r1 = mid1;
          else
            l1 = mid1;
        }
        tmp += l1 - i;
      }
      if (a[i] < 0) {
        if (a[i] * a[n - 1] > mid)
          continue;
        ll l1 = i;
        ll r1 = n;
        while (r1 - l1 > 1) {
          ll mid1 = (r1 + l1) >> 1;
          if (a[i] * a[mid1] > mid)
            l1 = mid1;
          else
            r1 = mid1;
        }
        tmp += n - r1;
      }
    }
    if (tmp >= k)
      r = mid;
    else
      l = mid;
  }
  out(r);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (long long i = o; i < n; i++)
#define oneforall                                                              \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define all(v) (v).begin(), (v).end()
#define ini(...)                                                               \
  int __VA_ARGS__;                                                             \
  in(__VA_ARGS__)
#define inl(...)                                                               \
  long long __VA_ARGS__;                                                       \
  in(__VA_ARGS__)
#define ins(...)                                                               \
  string __VA_ARGS__;                                                          \
  in(__VA_ARGS__)
const long long INF = 1e18;
void in() {}
template <typename T, class... U> void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U> void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u))
    cout << " ";
  out(u...);
}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long>> vpll;
typedef vector<pair<int, int>> vpii;
int main() {
  oneforall oneforall oneforall oneforall oneforall oneforall oneforall
      oneforall oneforall oneforall oneforall oneforall oneforall oneforall inl(
          n);
  inl(k);
  ll a[10000010];
  FOR(i, 0, n) { in(a[i]); }
  sort(a, a + n);
  ll l = -INF;
  ll r = INF;
  while (r - l > 1) {
    ll mid = (l + r) >> 1;
    ll tmp = 0;
    FOR(i, 0, n) {
      if (a[i] >= 0) {
        if (a[i] * a[0] > mid)
          continue;
        ll l1 = i;
        ll r1 = n;
        while (r1 - l1 > 1) {
          ll mid1 = (r1 + l1) >> 1;
          if (a[i] * a[mid1] > mid)
            r1 = mid1;
          else
            l1 = mid1;
        }
        tmp += l1 - i;
      }
      if (a[i] < 0) {
        if (a[i] * a[n - 1] > mid)
          continue;
        ll l1 = i;
        ll r1 = n;
        while (r1 - l1 > 1) {
          ll mid1 = (r1 + l1) >> 1;
          if (a[i] * a[mid1] > mid)
            l1 = mid1;
          else
            r1 = mid1;
        }
        tmp += n - r1;
      }
    }
    if (tmp >= k)
      r = mid;
    else
      l = mid;
  }
  out(r);
  return 0;
}