Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 5;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline int gcd(int a, int b) {
  while (b) {
    int c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int mypow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int modpow(int a, int b, int m = mod) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  int memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
int n, k, a[100010], b, c;
vector<int> vec[2];
signed main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0) {
      b++;
      vec[0].push_back(-a[i]);
    }
    if (a[i] > 0) {
      c++;
      vec[1].push_back(a[i]);
    }
  }
  sort(all(vec[0]));
  sort(all(vec[1]));
  if (b * c >= k) {
    int mi = 0, ma = INF;
    while (mi + 1 < ma) {
      int md = (mi + ma) / 2;
      int cnt = 0;
      for (int i : vec[0]) {
        cnt += vec[1].end() - lower_bound(all(vec[1]), (md + i - 1) / i);
      }
      if (cnt < k)
        ma = md;
      else
        mi = md;
    }
    cout << -mi << endl;
  } else if (b * c + (n - b - c) * (b + c) +
                 (n - b - c) * (n - b - c - 1) / 2 >=
             k) {
    cout << 0 << endl;
  } else {
    k -= b * c + (n - b - c) * (b + c) + (n - b - c) * (n - b - c - 1) / 2;
    int mi = 0, ma = INF;
    while (mi + 1 < ma) {
      int md = (mi + ma) / 2;
      int cnt = 0;
      rep(i, vec[0].size()) {
        cnt +=
            upper_bound(vec[0].begin() + i + 1, vec[0].end(), md / vec[0][i]) -
            vec[0].begin() - i - 1;
      }
      rep(i, vec[1].size()) {
        cnt +=
            upper_bound(vec[1].begin() + i + 1, vec[1].end(), md / vec[1][i]) -
            vec[1].begin() - i - 1;
      }
      if (cnt < k)
        mi = md;
      else
        ma = md;
    }
    cout << ma << endl;
  }
  return 0;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 5;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline int gcd(int a, int b) {
  while (b) {
    int c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int mypow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int modpow(int a, int b, int m = mod) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  int memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
int n, k, a[200010], b, c;
vector<int> vec[2];
signed main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    if (a[i] < 0) {
      b++;
      vec[0].push_back(-a[i]);
    }
    if (a[i] > 0) {
      c++;
      vec[1].push_back(a[i]);
    }
  }
  sort(all(vec[0]));
  sort(all(vec[1]));
  if (b * c >= k) {
    int mi = 0, ma = INF;
    while (mi + 1 < ma) {
      int md = (mi + ma) / 2;
      int cnt = 0;
      for (int i : vec[0]) {
        cnt += vec[1].end() - lower_bound(all(vec[1]), (md + i - 1) / i);
      }
      if (cnt < k)
        ma = md;
      else
        mi = md;
    }
    cout << -mi << endl;
  } else if (b * c + (n - b - c) * (b + c) +
                 (n - b - c) * (n - b - c - 1) / 2 >=
             k) {
    cout << 0 << endl;
  } else {
    k -= b * c + (n - b - c) * (b + c) + (n - b - c) * (n - b - c - 1) / 2;
    int mi = 0, ma = INF;
    while (mi + 1 < ma) {
      int md = (mi + ma) / 2;
      int cnt = 0;
      rep(i, vec[0].size()) {
        cnt +=
            upper_bound(vec[0].begin() + i + 1, vec[0].end(), md / vec[0][i]) -
            vec[0].begin() - i - 1;
      }
      rep(i, vec[1].size()) {
        cnt +=
            upper_bound(vec[1].begin() + i + 1, vec[1].end(), md / vec[1][i]) -
            vec[1].begin() - i - 1;
      }
      if (cnt < k)
        mi = md;
      else
        ma = md;
    }
    cout << ma << endl;
  }
  return 0;
}