Language: C++

#include <bits/stdc++.h>
using namespace std;

long count(vector<long> &A, long N, long now){
	long ans = 0;
	for (int i = 0; i < N; i++){
		long tmp = 0;
		bool isZero = false;
		if (A.at(i)) tmp = now / A.at(i);
		else isZero = true;
		if (isZero && now >= 0) ans +=  N - 1 - i;
		else if (!isZero){
			long min_i = 0;
			long max_i = N-1;
			while (1){
				long now_i = (min_i + max_i) / 2;
				if (A.at(i) > 0){
					if (A.at(now_i) < tmp) min_i = now_i;
					else max_i = now_i;
					if (max_i - min_i <= 1) break;
				}else{
					if (A.at(now_i) >= tmp) max_i = now_i;
					else min_i = now_i;
					if (max_i - min_i <= 1) break;
				}
			}
			if (A.at(max_i) <= tmp){
				if (A.at(i) > 0){
					ans += max((long)0, min(N-max_i, N-1-i));
				}else{
					ans += max((long)0, min(max_i, i-1));
				}
				// cout << "check max " << ans << endl;
			}
			else{
				if (A.at(i) > 0){
					ans += max((long)0, min(N-min_i, N-1-i));
				}else{
					ans += max((long)0, min(min_i, i-1));
				}
				// cout << "check min " << ans << endl;
			}
		}
	}
	return (ans);
}

long calc_ans(vector<long> &A, long N, long now){
	long ans = LONG_MIN;
	for (long i = 0; i < N; i++){
		long tmp = 0;
		bool isZero = false;
		if (A.at(i)) tmp = now / A.at(i);
		else isZero = true;
		if (isZero && now >= 0) ans = max(ans, (long)0);
		else if (!isZero){
			long min_i = 0;
			long max_i = N-1;
			while (1){
				long now_i = (min_i + max_i) / 2;
				if (A.at(now_i) < tmp) min_i = now_i;
				else max_i = now_i;
				if (max_i - min_i <= 1) break;
			}
			if (A.at(max_i) <= tmp){
				int tmp_i = max(max_i, i+1);
				if (tmp_i < N) ans = max(ans, A.at(i) * A.at(tmp_i));
				// cout << "check max " << ans << endl;
			}
			else{
				int tmp_i = max(min_i, i+1);
				if (tmp_i < N) ans = max(ans, A.at(i) * A.at(tmp_i));
				// cout << "check min " << ans << endl;
			}
		}
	}
	return (ans);
}

int main(){
	long N, K;
	cin >> N >> K;
	vector <long> A(N);
	for (int i = 0; i < N; i++) cin >> A.at(i);
	sort(A.begin(), A.end());
	long min_A = min(A.at(0)*A.at(1), A.at(0)*A.at(N-1));
	long max_A = max(A.at(0)*A.at(1), A.at(N-2)*A.at(N-1));
	// cout << min_A << "    " << max_A << endl;
	while (max_A - min_A > 10){
		long now = (max_A + min_A) / 2;
		long cnt = count(A, N, now);
		// cout << cnt << " " << now << endl;
		if (cnt == K) break;
		else if (cnt < K) min_A = now;
		else max_A = now;
	}
	for (int i = min_A; i < max_A+1; i++){
		long cnt = count(A, N, i);
		// cout << "i = " << i << ", cnt = " << cnt << endl;
		if (cnt < K) continue;
		else{
			// cout << "check" << endl;
			cout << calc_ans(A, N, i) << endl;
			break;
		}
	}
}


Compiler output:
a.cc: In function 'long int count(std::vector<long int>&, long int, long int)':
a.cc:31:64: error: no matching function for call to 'min(long int&, int)'
   31 |                                         ans += max((long)0, min(max_i, i-1));
      |                                                             ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:31:64: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   31 |                                         ans += max((long)0, min(max_i, i-1));
      |                                                             ~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:31:64: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   31 |                                         ans += max((long)0, min(max_i, i-1));
      |                                                             ~~~^~~~~~~~~~~~
a.cc:39:64: error: no matching function for call to 'min(long int&, int)'
   39 |                                         ans += max((long)0, min(min_i, i-1));
      |                                                             ~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:39:64: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   39 |                                         ans += max((long)0, min(min_i, i-1));
      |                                                             ~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:39:64: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   39 |                                         ans += max((long)0, min(min_i, i-1));
      |                                                             ~~~^~~~~~~~~~~~