Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \
  for (ll i = (m); i <= n; i++) {                                              \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;

ll n, k, a[100008];

ll f(ll i, ll mid) {
  ll res;
  if (a[i] < 0) {
    if (a[i] * a[0] <= mid) {
      return n - 1;
    }
    if (a[i] * a[n - 1] > mid) {
      return 0;
    }

    ll yes = n - 1;
    ll no = 0;
    while (yes - no > 1) {
      ll m = (yes + no) / 2;
      if (a[i] * a[m] <= mid) {
        yes = m;
      } else {
        no = m;
      }
    }

    res = n - yes;
    if (yes <= i) {
      res--;
    }
  } else {
    if (a[i] * a[0] > mid) {
      return 0;
    }
    if (a[i] * a[n - 1] <= mid) {
      return n - 1;
    }

    ll yes = 0;
    ll no = n - 1;
    while (no - yes > 1) {
      ll m = (yes + no) / 2;

      if (a[i] * a[m] <= mid) {
        yes = m;
      } else {
        no = m;
      }
    }

    res = yes + 1;
    if (yes >= i) {
      res--;
    }
  }
  return res;
}

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

  cin >> n >> k;
  zep(i, 0, n) { cin >> a[i]; }
  sort(a, a + n);

  ll ok = 1e18;
  ll ng = -1e18 - 1;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;

    ll sm = 0;
    zep(i, 0, n) { sm += f(i, mid); }
    sm /= 2;

    if (sm >= k) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  print(ok)
      // print(f(3, -12))
      return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \
  for (ll i = (m); i <= n; i++) {                                              \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;

ll n, k, a[200008];

ll f(ll i, ll mid) {
  ll res;
  if (a[i] < 0) {
    if (a[i] * a[0] <= mid) {
      return n - 1;
    }
    if (a[i] * a[n - 1] > mid) {
      return 0;
    }

    ll yes = n - 1;
    ll no = 0;
    while (yes - no > 1) {
      ll m = (yes + no) / 2;
      if (a[i] * a[m] <= mid) {
        yes = m;
      } else {
        no = m;
      }
    }

    res = n - yes;
    if (yes <= i) {
      res--;
    }
  } else {
    if (a[i] * a[0] > mid) {
      return 0;
    }
    if (a[i] * a[n - 1] <= mid) {
      return n - 1;
    }

    ll yes = 0;
    ll no = n - 1;
    while (no - yes > 1) {
      ll m = (yes + no) / 2;

      if (a[i] * a[m] <= mid) {
        yes = m;
      } else {
        no = m;
      }
    }

    res = yes + 1;
    if (yes >= i) {
      res--;
    }
  }
  return res;
}

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

  cin >> n >> k;
  zep(i, 0, n) { cin >> a[i]; }
  sort(a, a + n);

  ll ok = 1e18;
  ll ng = -1e18 - 1;
  while (ok - ng > 1) {
    ll mid = (ok + ng) / 2;

    ll sm = 0;
    zep(i, 0, n) { sm += f(i, mid); }
    sm /= 2;

    if (sm >= k) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  print(ok)
      // print(f(3, -12))
      return 0;
}