Problem p02774 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
const int INF = (int)(1e18) + 3ll;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> b = a;
  reverse(b.begin(), b.end());
  auto cnt = [&](int x, int y, vector<int> v) {
    // x*v[i] < yとなるiの個数
    int bot = -1, top = n;
    while (top - bot > 1) {
      int mid = (top + bot) / 2;
      if (v[mid] * x < y)
        bot = mid;
      else
        top = mid;
    }
    return top;
  };
  auto ok = [&](int x) {
    // x未満がkこ未満か
    int ret = 0;
    rep(i, n) {
      if (a[i] >= 0)
        ret += cnt(a[i], x, a);
      else
        ret += cnt(a[i], x, b);
      if (a[i] * a[i] < x)
        ret--;
    }
    ret /= 2;
    return ret < k;
  };
  int top = INF;
  int bot = -INF;
  while (top - bot > 1) {
    int mid = (top + bot) / 2;
    if (ok(mid))
      bot = mid;
    else
      top = mid;
  }
  cout << bot << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
const int INF = (int)(1e18) + 3ll;
signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  vector<int> b = a;
  reverse(b.begin(), b.end());
  auto cnt = [&](int x, int y, vector<int> &v) {
    // x*v[i] < yとなるiの個数
    int bot = -1, top = n;
    while (top - bot > 1) {
      int mid = (top + bot) / 2;
      if (v[mid] * x < y)
        bot = mid;
      else
        top = mid;
    }
    return top;
  };
  auto ok = [&](int x) {
    // x未満がkこ未満か
    int ret = 0;
    rep(i, n) {
      if (a[i] >= 0)
        ret += cnt(a[i], x, a);
      else
        ret += cnt(a[i], x, b);
      if (a[i] * a[i] < x)
        ret--;
    }
    ret /= 2;
    return ret < k;
  };
  int top = INF;
  int bot = -INF;
  while (top - bot > 1) {
    int mid = (top + bot) / 2;
    if (ok(mid))
      bot = mid;
    else
      top = mid;
  }
  cout << bot << endl;
  return 0;
}