Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
using namespace std;

int main() {
    int N;
    int K;
    cin >> N >> K;

    vector <long long> val(N);
    vector <long long> belowZ;
    vector <long long> rBelowZ;
    vector <long long> aboveZ;
    vector <long long> rAboveZ;

    for (int i = 0; i < N; ++i) {
        cin >> val[i];

        if (val[i] >= 0) {
            aboveZ.push_back(val[i]);
        } else {
            belowZ.push_back(val[i]);
        }
    }

    sort(val.begin(), val.end());
    sort(aboveZ.begin(), aboveZ.end());
    sort(belowZ.begin(), belowZ.end());

    rAboveZ = aboveZ;
    reverse(rAboveZ.begin(), rAboveZ.end());
    rBelowZ = belowZ;
    reverse(rBelowZ.begin(), rBelowZ.end());

    const long long INF = 1000000000000000000LL;
    long long low = -INF;
    long long up = INF;
    long long ans = INF;

    while (low <= up) {
        int rank = 0; // how many numbers <= mid
        long long mid = (low+up) / 2;
        // 3 3 -4 -2
        // 0 => 3 : -4 -2, 3 : -4 -2
        // -4 => 3 3
        // -2 => 3 3

        for (int i = 0; i < val.size(); ++i) {
            if (val[i] < 0) {
                int low1 = 0;
                int up1 = rAboveZ.size()-1;
                int greater = -1;

                while (low1 <= up1) {
                    int mid1 = (low1 + up1) / 2;
                    long long res = val[i] * rAboveZ[mid1];

                    if (res <= mid) {
                        greater = mid1;
                        low1 = mid1+1;
                    } else {
                        up1 = mid1-1;
                    }
                }

                rank += (greater+1);
                //cout << "MINUS CHECK " << val[i] << " " << greater << endl;

                // minus part
                low1 = 0;
                up1 = rBelowZ.size()-1;
                greater = -1;

                while (low1 <= up1) {
                    int mid1 = (low1 + up1) / 2;
                    long long res = val[i] * rBelowZ[mid1];

                    if (res <= mid) {
                        greater = mid1;
                        low1 = mid1+1;
                    } else {
                        up1 = mid1-1;
                    }
                }

                rank += (greater+1);
            } else {
                int low1 = 0;
                int up1 = aboveZ.size()-1;
                int greater = -1;

                while (low1 <= up1) {
                    int mid1 = (low1 + up1) / 2;
                    long long res = val[i] * aboveZ[mid1];

                    if (res <= mid) {
                        greater = mid1;
                        low1 = mid1+1;
                    } else {
                        up1 = mid1-1;
                    }
                }

                rank += (greater+1);

                // minus part
                low1 = 0;
                up1 = belowZ.size()-1;
                greater = -1;

                while (low1 <= up1) {
                    int mid1 = (low1 + up1) / 2;
                    long long res = val[i] * belowZ[mid1];

                    if (res <= mid) {
                        greater = mid1;
                        low1 = mid1+1;
                    } else {
                        up1 = mid1-1;
                    }
                }
                //cout << "POSITIVE CHECK " << val[i] << " " << greater << endl;

                rank += (greater+1);
            }
        }

        // cout << "DEBUG " << mid << " " << rank << endl;

        // cout << "DEBUG STEP2 " << mid << " " << rank << endl;

        for (int i = 0; i < val.size(); ++i) {
            if (val[i]*val[i] <= mid) { //avoid self*self
                --rank;
            }
        }

        rank /= 2;
        //cout << "AFTER DEBUG " << mid << " " << rank << endl;
        if (rank >= K) {
            up = mid-1;
            ans = mid;
        } else {
            low = mid+1;
        }
    }

    cout << ans << endl;


    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:28:5: error: 'sort' was not declared in this scope; did you mean 'short'?
   28 |     sort(val.begin(), val.end());
      |     ^~~~
      |     short
a.cc:33:5: error: 'reverse' was not declared in this scope
   33 |     reverse(rAboveZ.begin(), rAboveZ.end());
      |     ^~~~~~~