Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 	1,000,000 -> Loop
// 	100,000	-> vector(int:400KB, long:800KB)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (ll i = 0; i < n; i++)
#define Rep1(i, n) for (ll i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

ll FP(const vector<ll> &P, const vector<ll> &M, const ll K) {
  ll r = 0;
  if (0 != M.size()) // No positive number
    r = max(r, M.back() * M.back() + 1);
  if (0 != P.size()) // No negative number
    r = max(r, P.back() * P.back() + 1);
  ll l = 0;           // Less than minimum value
  while (r - l > 1) { // Loop
    ll m = (l + r) / 2;
    ll s = 0; // Count pairs equal or more then m
    Rep0(i, P.size() - 1) {
      ll d = (m + P.at(i) - 1) / P.at(i);
      // minimum value for the other for m
      ll c = lower_bound(P.begin(), P.end(), d) - P.begin();
      // index for the minimum P
      c = max(c, i + 1); // count only the ones more than P[i]
      s += P.size() - c;
    }
    Rep0(i, M.size() - 1) {
      ll d = (m + M.at(i) - 1) / M.at(i);
      // minimum value for the other for m
      ll c = lower_bound(M.begin(), M.end(), d) - M.begin();
      // index for the minimum M
      c = max(c, i + 1); // count only the ones more than M[i]
      s += M.size() - c;
    }
    if (s >= K)
      l = m;
    else
      r = m;
  }
  return l;
}

ll FM(const vector<ll> &P, const vector<ll> &M, const ll K) {
  ll r = P.back() * M.back() + 1; // More then maximum value
  ll l = 0;                       // Less than minimum value
  while (r - l > 1) {             // Loop
    ll m = (l + r) / 2;
    ll s = 0; // Count pairs equal or more then m
    Rep0(i, P.size()) {
      ll d = (m + P.at(i) - 1) / P.at(i);
      // minimum value for the other for m
      ll c = lower_bound(M.begin(), M.end(), d) - M.begin();
      // index for the minimum M
      s += M.size() - c;
    }
    if (s >= K)
      l = m;
    else
      r = m;
  }
  return l;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P, M, Z;
  Rep0(i, N) {
    ll A;
    cin >> A;
    if (A < 0)
      M.push_back(-A);
    else if (A == 0)
      Z.push_back(A);
    else
      P.push_back(A);
  }
  Sort(P);
  Sort(M);
  ll NM = P.size() * M.size();
  ll NZ = Z.size() * (P.size() + M.size()) + Z.size() * (Z.size() - 1) / 2;
  if (K <= NM) { // Minus
    cout << -FM(P, M, K) << endl;
  } else if (K <= NM + NZ) { // Zero
    cout << 0 << endl;
  } else { // Plus
    cout << FP(P, M, N * (N - 1) / 2 - K + 1) << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

//
// Notes
//
// When specified less than N, try full scan.
// RE: empty vector
// Check Qs
// long/int
// xor -> count each bit
// Check values
// 	1,000,000 -> Loop
// 	100,000	-> vector(int:400KB, long:800KB)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (ll i = 0; i < n; i++)
#define Rep1(i, n) for (ll i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

ll FP(const vector<ll> &P, const vector<ll> &M, const ll K) {
  ll r = 0;
  if (0 != M.size()) // No positive number
    r = max(r, M.back() * M.back() + 1);
  if (0 != P.size()) // No negative number
    r = max(r, P.back() * P.back() + 1);
  ll l = 0;           // Less than minimum value
  while (r - l > 1) { // Loop
    ll m = (l + r) / 2;
    ll s = 0; // Count pairs equal or more then m
    if (0 < P.size())
      Rep0(i, P.size() - 1) {
        ll d = (m + P.at(i) - 1) / P.at(i);
        // minimum value for the other for m
        ll c = lower_bound(P.begin(), P.end(), d) - P.begin();
        // index for the minimum P
        c = max(c, i + 1); // count only the ones more than P[i]
        s += P.size() - c;
      }
    if (0 < M.size())
      Rep0(i, M.size() - 1) {
        ll d = (m + M.at(i) - 1) / M.at(i);
        // minimum value for the other for m
        ll c = lower_bound(M.begin(), M.end(), d) - M.begin();
        // index for the minimum M
        c = max(c, i + 1); // count only the ones more than M[i]
        s += M.size() - c;
      }
    if (s >= K)
      l = m;
    else
      r = m;
  }
  return l;
}

ll FM(const vector<ll> &P, const vector<ll> &M, const ll K) {
  ll r = P.back() * M.back() + 1; // More then maximum value
  ll l = 0;                       // Less than minimum value
  while (r - l > 1) {             // Loop
    ll m = (l + r) / 2;
    ll s = 0; // Count pairs equal or more then m
    Rep0(i, P.size()) {
      ll d = (m + P.at(i) - 1) / P.at(i);
      // minimum value for the other for m
      ll c = lower_bound(M.begin(), M.end(), d) - M.begin();
      // index for the minimum M
      s += M.size() - c;
    }
    if (s >= K)
      l = m;
    else
      r = m;
  }
  return l;
}

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> P, M, Z;
  Rep0(i, N) {
    ll A;
    cin >> A;
    if (A < 0)
      M.push_back(-A);
    else if (A == 0)
      Z.push_back(A);
    else
      P.push_back(A);
  }
  Sort(P);
  Sort(M);
  ll NM = P.size() * M.size();
  ll NZ = Z.size() * (P.size() + M.size()) + Z.size() * (Z.size() - 1) / 2;
  if (K <= NM) { // Minus
    cout << -FM(P, M, K) << endl;
  } else if (K <= NM + NZ) { // Zero
    cout << 0 << endl;
  } else { // Plus
    cout << FP(P, M, N * (N - 1) / 2 - K + 1) << endl;
  }
  return 0;
}