Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll n, k, a[N];
vector<ll> v1, v2, v0;
bool check(ll val) {
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) {
      if (val >= 0)
        cnt += n - 1;
    } else if (a[i] > 0) {
      if (val >= 0) {
        ll l = -1e9 - 10;
        ll r = val / a[i];
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      } else {
        ll l = -1e9 - 10;
        ll r = val / a[i];
        if (val % a[i] != 0)
          r--;
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      }
    } else if (a[i] < 0) {
      if (val >= 0) {
        ll l = val / a[i];
        ll r = 1e9 + 10;
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      } else {
        ll l = val / a[i];
        ll r = 1e9 + 10;
        if (val % a[i] != 0)
          l++;
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      }
    }
  }
  cnt /= 2;
  return cnt >= k;
}
int main() {
  //	cout<<(-9)/(-4)<<endl;
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 0)
      v0.push_back(a[i]);
    else if (a[i] > 0)
      v1.push_back(a[i]);
    else
      v2.push_back(a[i]);
  }
  sort(a + 1, a + n + 1);
  ll l = -1e18 - 10, r = 1e18 + 10;
  while (l < r) {
    ll mid = (l + r) >> 1;
    bool flag = check(mid);
    if (flag)
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", r);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, k, a[N];
vector<ll> v1, v2, v0;
bool check(ll val) {
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) {
      if (val >= 0)
        cnt += n - 1;
    } else if (a[i] > 0) {
      if (val >= 0) {
        ll l = -1e9 - 10;
        ll r = val / a[i];
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      } else {
        ll l = -1e9 - 10;
        ll r = val / a[i];
        if (val % a[i] != 0)
          r--;
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      }
    } else if (a[i] < 0) {
      if (val >= 0) {
        ll l = val / a[i];
        ll r = 1e9 + 10;
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      } else {
        ll l = val / a[i];
        ll r = 1e9 + 10;
        if (val % a[i] != 0)
          l++;
        int pos1 = lower_bound(a + 1, a + n + 1, l) - a;
        int pos2 = upper_bound(a + 1, a + n + 1, r) - a;
        pos2--;
        cnt += pos2 - pos1 + 1;
        if (l <= a[i] && a[i] <= r)
          cnt--;
      }
    }
  }
  cnt /= 2;
  return cnt >= k;
}
int main() {
  //	cout<<(-9)/(-4)<<endl;
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] == 0)
      v0.push_back(a[i]);
    else if (a[i] > 0)
      v1.push_back(a[i]);
    else
      v2.push_back(a[i]);
  }
  sort(a + 1, a + n + 1);
  ll l = -1e18 - 10, r = 1e18 + 10;
  while (l < r) {
    ll mid = (l + r) >> 1;
    bool flag = check(mid);
    if (flag)
      r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", r);
}