Language: C++

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;

ll INF = (1LL << 60);

int main() {
    ll N, K;
    cin >> N >> K;
    ll a[N];
    for(int i = 0; i < N; i++) cin >> a[i];

    vector<ll> m, p;
    ll cnt_zero = 0;
    for(int i = 0; i < N; i++) {
        if(a[i] == 0) cnt_zero++;
        else if(a[i] > 0) p.push_back(a[i]);
        else m.push_back(-a[i]);
    }

    ll P = p.size();
    ll M = m.size();

    sort(p.begin(), p.end());
    sort(m.begin(), m.end());

    //cout << "hoge" << endl;

    ll l = -INF;
    ll r = INF;


    while(l + 1 != r) {
        ll piv = (l + r) / 2;
        ll k = 0;

        if(piv == 0) {
            k = P * M + cnt_zero * (P + M) + cnt_zero * (cnt_zero - 1) / 2;
        }
        if(piv < 0) {
            for(int i = 0; i < M; i++) {
                ll v = ((-piv + m[i] - 1) / m[i]); //Pからはv以上の個数
                ll cnt = p.end() - lower_bound(p.begin(), p.end(), v);
                k += cnt;
            }
        }
        else if(piv > 0) {
            ll cnt = 0;
            for(int i = 0; i < M; i++) {
                ll v = piv / m[i];
                cnt += max(0, upper_bound(m.begin(), m.end(), v) - m.begin() - (i + 1));
            }
            for(int i = 0; i < P; i++) {
                ll v = piv / p[i];
                cnt += max(0, upper_bound(p.begin(), p.end(), v) - p.begin() - (i + 1));
            }
            cnt += cnt_zero * (cnt_zero - 1) / 2 + cnt_zero * (P + M);
            k = P * M + cnt;
        }

        if(k < K) l = piv;
        else r = piv;
    }

    cout << r << endl;

    //=============================
    /*
    for(int piv = -10; piv <= 20; piv++) {
        cout << piv << " " ;
        ll k = 0;

        if(piv == 0) {
            k = P * M + cnt_zero * (P + M) + cnt_zero * (cnt_zero - 1);
        }
        if(piv < 0) {
            for(int i = 0; i < M; i++) {
                ll v = ((-piv + m[i] - 1) / m[i]); //Pからはv以上の個数
                ll cnt = p.end() - lower_bound(p.begin(), p.end(), v);
                k += cnt;
            }
        }
        else if(piv > 0) {
            ll cnt = 0;
            for(int i = 0; i < M; i++) {
                ll v = piv / m[i];
                cnt += max(0, upper_bound(m.begin(), m.end(), v) - m.begin() - (i + 1));
            }
            for(int i = 0; i < P; i++) {
                ll v = piv / p[i];
                cnt += max(0, upper_bound(p.begin(), p.end(), v) - p.begin() - (i + 1));
            }
            cnt += cnt_zero * (cnt_zero - 1) + cnt_zero * (P + M);
            k = P * M + cnt;
        }
        cout << k << endl;
    }
    */

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:61:27: error: no matching function for call to 'max(int, __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >::difference_type)'
   61 |                 cnt += max(0, upper_bound(m.begin(), m.end(), v) - m.begin() - (i + 1));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:61:27: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >::difference_type' {aka 'long int'})
   61 |                 cnt += max(0, upper_bound(m.begin(), m.end(), v) - m.begin() - (i + 1));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303: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:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:61:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   61 |                 cnt += max(0, upper_bound(m.begin(), m.end(), v) - m.begin() - (i + 1));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:65:27: error: no matching function for call to 'max(int, __gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >::difference_type)'
   65 |                 cnt += max(0, upper_bound(p.begin(), p.end(), v) - p.begin() - (i + 1));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:65:27: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >::difference_type' {aka 'long int'})
   65 |                 cnt += max(0, upper_bound(p.begin(), p.end(), v) - p.begin() - (i + 1));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:65:27: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   65 |                 cnt += max(0, upper_bound(p.begin(), p.end(), v) - p.begin() - (i + 1));
      |                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~