Language: C++

#include <iostream>
#include <vector>

#define rep(i, N) for(int i = 0; i < N; ++i)

using namespace std;
typedef long long ll;

int main()
{
    ll n,k;
    cin >> n >> k;

    vector<ll> plus;
    vector<ll> minus;
    int countZero = 0;
    int countPlus = 0;
    int countMinus = 0;
    rep(i, n)
    {
        int a;
        cin >> a;
        if (a == 0)
        {
            countZero++;
        }
        else if (a < 0)
        {
            countMinus++;
            minus.push_back(a);
        }
        else
        {
            countPlus++;
            plus.push_back(a);
        }
    }
    sort(plus.begin(), plus.end());
    sort(minus.begin(), minus.end());

    // cout << countZero << " "<<countPlus << " "<<countMinus << endl;

    ll totalSquareCount = n * (n - 1) / 2;

    ll tempPlus = countPlus > 1 ? (countPlus * (countPlus - 1) / 2) : 0;
    ll tempMinus = countMinus > 1 ? (countMinus * (countMinus - 1) / 2) : 0;

    ll plusSquareCount = tempPlus + tempMinus;
    ll minusSquareCount = countPlus * countMinus;
    ll zeroSquareCount = totalSquareCount - plusSquareCount - minusSquareCount;

    // cout << "Square: "<< minusSquareCount << " "<< zeroSquareCount << " " << plusSquareCount << endl;

    ll ans = 0;
    if (k < minusSquareCount)
    {
        // minus
        ll order = min(k, minusSquareCount - k);
        if (order == k)
        {
            // from top
            vector<ll> minusTop;
            for (auto i = 0; i < order; i++)
            {
                for (auto j = 0; j < order; j++)
                {
                    if (i < minus.size() && j < plus.size())
                    {
                        minusTop.push_back(minus[i] * plus[j]);
                    }
                }
            }
            sort(minusTop.begin(), minusTop.end());
            ans = minusTop[order-1];
        }
        else
        {
            // from end
            sort(plus.begin(), plus.end(), std::greater<int>());
            sort(minus.begin(), minus.end(), std::greater<int>());
            vector<ll> minusEnd;
            for (auto i = 0; i < order; i++)
            {
                for (auto j = 0; j < order; j++)
                {
                    if (i < minus.size() && j < plus.size())
                    {
                        minusEnd.push_back(minus[i] * plus[j]);
                    }
                    //cout << minus[i] * plus[j] << endl;
                }
            }
            sort(minusEnd.begin(), minusEnd.end(), std::greater<int>());
            ans = minusEnd[order-1];
        }
    }
    else if (k < minusSquareCount + zeroSquareCount)
    {
        // zero
    }
    else
    {
        // plus
        ll order = min(totalSquareCount - k, k - minusSquareCount - zeroSquareCount); 
        if (order == totalSquareCount - k)
        {
            // from end
            sort(plus.begin(), plus.end(), std::greater<int>());
            sort(minus.begin(), minus.end(), std::greater<int>());
            vector<ll> plusEnd;
            for (auto i = 0; i < order; i++)
            {
                for (auto j = i + 1; j < order+1; j++)
                {
                    if (j < minus.size())
                    {
                        plusEnd.push_back(minus[i] * minus[j]);
                        //cout << minus[i] * minus[j] << endl;
                    }
                    if (j < plus.size())
                    {
                        plusEnd.push_back(plus[i] * plus[j]);
                        //cout << plus[i] * plus[j] << endl;
                    }
                }
            }
            sort(plusEnd.begin(), plusEnd.end(), std::greater<int>());
            ans = plusEnd[order-1];
        }
        else
        {
            // from top
            vector<ll> plusTop;
            for (auto i = 0; i < order; i++)
            {
                for (auto j = i + 1; j < order+1; j++)
                {
                    if (j < minus.size())
                    {
                        plusTop.push_back(minus[i] * minus[j]);
                        //cout << minus[i] * minus[j] << endl;
                    }
                    if (j < plus.size())
                    {
                        plusTop.push_back(plus[i] * plus[j]);
                        //cout << plus[i] * plus[j] << endl;
                    }
                }
            }
            sort(plusTop.begin(), plusTop.end());
            ans = plusTop[order-1];
        }
    }
    cout << ans << endl;
    return 0;
}


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