Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>

using ll = long long;

ll pp(ll p)
{
	return p * (p - 1) / 2;
}

struct MyMul
{
	ll mul;
	bool visited;
};

struct Point
{
	int x, y;
};

template <class T>
class Array2D
{
	size_t d1_size, d2_size;
	std::vector<T> buf;

	size_t getIndex(size_t d1, size_t d2)
	{
		return d1 * d1_size + d2;
	}

public:
	using value_type = T;

	Array2D(size_t d1_size, size_t d2_size)
		: d1_size(d1_size)
		, d2_size(d2_size)
		, buf(d1_size* d2_size)
	{}

	value_type get(size_t d1, size_t d2) const 
	{
		return buf[getIndex(d1, d2)];
	}

	value_type &get(size_t d1, size_t d2) 
	{
		return buf[getIndex(d1, d2)];
	}

	void set(size_t d1, size_t d2, value_type v)
	{
		buf[getIndex(d1, d2)] = v;
	}
};


ll search_min_plus(const std::vector<ll>& p, const std::vector<ll>& m, ll K)
{
	Array2D<MyMul> tableP(p.size(), p.size());
	for (size_t i = 0; i < p.size(); i++)
	{
		for (size_t j = 0; j < i; j++)
		{
			tableP.set(i, j, MyMul{ p[i] * p[j], false });
		}
		tableP.set(i, i, MyMul{ 0, true });
	}

	Array2D<MyMul> tableM(m.size(), m.size());
	for (size_t i = 0; i < m.size(); i++)
	{
		for (size_t j = 0; j < i; j++)
		{
			tableM.set(i, j, MyMul{ m[i] * m[j], false });
		}
		tableM.set(i, i, MyMul{ 0, true });
	}

	std::vector<Point> nextP;
	ll PMul = LLONG_MAX;
	nextP.push_back(Point{ 1,0 });

	std::vector<Point> nextM;
	ll MMul = LLONG_MAX;
	nextM.push_back(Point{ 1,0 });

	for (ll i = 0ll; i < K; i++)
	{
		int flagMP = 0;

		if (nextP.size() == 0 || p.size() <= 1)
		{
			flagMP = -1;
		}
		else if (nextM.size() == 0 || m.size() <= 1)
		{
			flagMP = 1;
		}

		decltype(nextP)::iterator min_it_p;
		decltype(nextM)::iterator min_it_m;

		if (flagMP >= 0)
		{
			min_it_p = std::min_element(begin(nextP), end(nextP),
				[&tableP](Point l, Point r)
				{
					return tableP.get(l.x, l.y).mul < tableP.get(r.x, r.y).mul;
				});

			PMul = tableP.get(min_it_p->x, min_it_p->y).mul;
		}

		if (flagMP <= 0)
		{
			min_it_m = std::min_element(begin(nextM), end(nextM),
				[&tableM](Point l, Point r)
				{
					return tableM.get(l.x, l.y).mul < tableM.get(r.x, r.y).mul;
				});
			MMul = tableM.get(min_it_m->x, min_it_m->y).mul;
		}

		if (flagMP == 0)
		{
			flagMP = PMul < MMul ? 1 : -1;
		}

		if (flagMP > 0)
		{
			int mpx = min_it_p->x, mpy = min_it_p->y;
			nextP.erase(min_it_p);

			if (mpx < p.size() - 1 && !tableP.get(mpx + 1, mpy).visited)
			{
				nextP.push_back(Point{ mpx + 1, mpy });
				tableP.get(mpx + 1, mpy).visited = true;
			}
			if (mpy < p.size() - 1 && !tableP.get(mpx, mpy + 1).visited)
			{
				nextP.push_back(Point{ mpx, mpy + 1 });
				tableP.get(mpx, mpy + 1).visited = true;
			}
		}
		else
		{
			int mpx = min_it_m->x, mpy = min_it_m->y;
			nextM.erase(min_it_m);

			if (mpx < m.size() - 1 && !tableM.get(mpx + 1, mpy).visited)
			{
				nextM.push_back(Point{ mpx + 1, mpy });
				tableM.get(mpx + 1, mpy).visited = true;
			}
			if (mpy < m.size() - 1 && !tableM.get(mpx, mpy + 1).visited)
			{
				nextM.push_back(Point{ mpx, mpy + 1 });
				tableM.get(mpx, mpy + 1).visited = true;
			}
		}
	}

	return std::min(PMul, MMul);
}

ll search_min_minus(const std::vector<ll> &p, const std::vector<ll> &m, ll K)
{
	Array2D<MyMul> table(p.size(), m.size());
	for (size_t i = 0; i < p.size(); i++)
	{
		for (size_t j = 0; j < m.size(); j++)
		{
			table.set(i, j, MyMul{ p[i] *m[j], false });
		}
	}

	std::vector<Point> next;
	Point min_point{ 0, 0 };
	next.push_back(min_point);

	for (ll i = 0ll; i < K; i++)
	{
		auto min_it = std::min_element(begin(next), end(next),
			[&table](Point l, Point r)
			{
				return table.get(l.x, l.y).mul < table.get(r.x, r.y).mul;
			});
		min_point = *min_it;
		int mpx = min_point.x, mpy = min_point.y;
		next.erase(min_it);

		if (mpx < p.size() - 1 && !table.get(mpx + 1, mpy).visited)
		{
			next.push_back(Point{ mpx + 1, mpy });
			table.get(mpx + 1, mpy).visited = true;
		}
		if (mpy < m.size() - 1 && !table.get(mpx, mpy + 1).visited)
		{
			next.push_back(Point{ mpx, mpy + 1 });
			table.get(mpx, mpy + 1).visited = true;
		}
	}

	return table.get(min_point.x, min_point.y).mul;
}

ll f(const std::vector<ll> &A, int N, ll K)
{
	std::vector<ll> plus, minus;
	ll zero = 0;
	plus.reserve(N);
	minus.reserve(N);

	for (int i = 0; i < N; i++)
	{
		if (A[i] > 0ll)
		{
			plus.push_back(A[i]);
		}
		else if (A[i] < 0ll)
		{
			minus.push_back(A[i]);
		}
		else
		{
			zero++;
		}
	}

	ll pp_minus = (ll)plus.size() * minus.size();
	ll pp_zero = pp(zero) + zero * ((ll)plus.size() + minus.size());
	//ll pp_plus = pp(plus.size()) * pp(minus.size());

	std::sort(begin(plus), end(plus), std::greater<ll>());
	std::sort(begin(minus), end(minus), std::less<ll>());
	
	if (K <= pp_minus)
	{
		return search_min_minus(plus, minus, K);
	}
	K -= pp_minus;
	if (K <= pp_zero)
	{
		return 0ll;
	}
	K -= pp_zero;

	std::reverse(begin(plus), end(plus));
	std::reverse(begin(minus), end(minus));
	return search_min_plus(plus, minus, K);
}

int main()
{
	int N;
	ll K;
	std::cin >> N >> K;
	std::vector<ll> A(N);
	for (int i = 0; i < N; i++)
	{
		std::cin >> A[i];
	}

	std::cout << f(A, N, K);
	
}

Compiler output:
a.cc: In function 'll search_min_plus(const std::vector<long long int>&, const std::vector<long long int>&, ll)':
a.cc:85:19: error: 'LLONG_MAX' was not declared in this scope
   85 |         ll PMul = LLONG_MAX;
      |                   ^~~~~~~~~
a.cc:6:1: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    5 | #include <map>
  +++ |+#include <climits>
    6 |