Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <cstdlib>

using namespace std;

using ll = long long;

bool comp(ll a, ll b) {
    return abs(a) < abs(b);
}

int main(){

  int N, K;
  cin >> N >> K;
  vector<ll> a(N);
  vector<ll> m;
  vector<ll> p;

  ll np = 0, nm = 0, nz =0;
  ll Np, Nm, Nz;

  for(int i = 0; i < N; ++i){
    cin >> a[i];
    if(a[i] > 0){
      ++np;
      p.push_back(a[i]);
    }
    else if(a[i] < 0){
      ++nm;
      m.push_back(a[i]);
    }
    else if(a[i] == 0) ++nz;
  }

  sort(a.begin(), a.end(), comp);
  
  Nm = np * nm;
  Nz = nz * (np + nm) + nz * (nz - 1) / 2;
  Np = N * (N-1) / 2 - Nm - Nz;

  vector<ll> minus_cnt(N);
  vector<ll> plus_cnt(N);
  ll np_bk = np;
  ll nm_bk = nm;

  for(ll = i ; i < N ; ++i){
    if(a[i] < 0){
      minus_cnt[i] = np;
      --np;
      --nm;
    }else if (a[i] > 0){
      minus_cnt[i] = nm;
      --np;
      --nm;
    }
  }

  np = np_bk;
  nm = nm_bk;

  for(ll = i ; i < N ; ++i){
    if(a[i] < 0){
      --nm;
      plus_cnt[i] = nm;
    }else if (a[i] > 0){
      --np;
      plus_cnt[i] = np;
    }
  }

  if(K <= Nm){
    // 負ゾーン
    for(ll i = 0; i < N; ++i){
      K -= minus_cnt[i];
      if(K == 0){
        // a[i]よりも大きい中で一番でかい符号の違う数
        for(ll j = N - 1; j > i; --j){
          if(a[i] * a[j] < 0){
            cout << a[i] * a[j] << endl;
            return 0;
          }
        }
      }else if(K < 0){
        // a[i]よりも大きい中で-K番目にでかい符号の違う数
        ll cnt = 0;
        for(ll j = i + 1; j < N; ++j){
          if(a[i] * a[j] < 0){
            ++cnt;
            if(cnt == K){
              cout << a[i] * a[j] << endl;
              return 0;
            }
          }
        }
      }
    }
  }else if(Nm < K && K <= Nz){
    cout << 0 << endl;
  }else{
    // 正ゾーン
    for(ll i = 0; i < N; ++i){
      K -= plus_cnt[i];
      if(K == 0){
        // a[i]よりも大きい中で一番小さい符号の同じ数
        for(ll j = i + 1; j < N; ++j){
          if(a[i] * a[j] > 0){
            cout << a[i] * a[j] << endl;
            return 0;
          }
        }
      }else if(K < 0){
        // a[i]よりも大きい中でK番目に小さい符号の違う数
        ll cnt = 0;
        for(ll j = N - 1; j > i; --j){
          if(a[i] * a[j] > 0){
            ++cnt;
            if(cnt == K){
              cout << a[i] * a[j] << endl;
              return 0;
            }
          }
        }
      }
    }
  }


  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:52:10: error: expected unqualified-id before '=' token
   52 |   for(ll = i ; i < N ; ++i){
      |          ^
a.cc:52:9: error: expected ';' before '=' token
   52 |   for(ll = i ; i < N ; ++i){
      |         ^~
      |         ;
a.cc:52:10: error: expected primary-expression before '=' token
   52 |   for(ll = i ; i < N ; ++i){
      |          ^
a.cc:52:12: error: 'i' was not declared in this scope
   52 |   for(ll = i ; i < N ; ++i){
      |            ^
a.cc:52:21: error: expected ')' before ';' token
   52 |   for(ll = i ; i < N ; ++i){
      |      ~              ^~
      |                     )
a.cc:52:26: error: 'i' was not declared in this scope
   52 |   for(ll = i ; i < N ; ++i){
      |                          ^
a.cc:67:10: error: expected unqualified-id before '=' token
   67 |   for(ll = i ; i < N ; ++i){
      |          ^
a.cc:67:9: error: expected ';' before '=' token
   67 |   for(ll = i ; i < N ; ++i){
      |         ^~
      |         ;
a.cc:67:10: error: expected primary-expression before '=' token
   67 |   for(ll = i ; i < N ; ++i){
      |          ^
a.cc:67:21: error: expected ')' before ';' token
   67 |   for(ll = i ; i < N ; ++i){
      |      ~              ^~
      |                     )