#include #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; const int log = 31; struct BinaryTrie { vector par, cnt, b0, b1; explicit BinaryTrie(void) { par.push_back(-1); b0.push_back(-1); b1.push_back(-1); cnt.push_back(0); } inline int __append(int p, int nxt_bit) { int idx = par.size(); par.push_back(p); b0.push_back(-1); b1.push_back(-1); cnt.push_back(0); if(nxt_bit == 0) b0[p] = idx; if(nxt_bit == 1) b1[p] = idx; return idx; } inline int move(int p, int nxt_bit) { if(nxt_bit == 0) { if(b0[p] == -1) return __append(p, nxt_bit); else return b0[p]; } else { if(b1[p] == -1) return __append(p, nxt_bit); else return b1[p]; } } void add_value(int x) { int root = 0; cnt[root]++; for(int i = log; i >= 0; i--) { root = move(root, (x>>i)&1); cnt[root]++; } } inline int subtree_cnt(int r) { if(r == -1) return 0; return cnt[r]; } int lower_bound(int value, int x=0) { int lb = 0; int root = 0; for(int i = log; i >= 0; i--) { int nxt_bit = (value>>i)&1; int flip = (x>>i)&1; if(nxt_bit == 1) { if(flip) lb += subtree_cnt(b1[root]); else lb += subtree_cnt(b0[root]); } root = move(root, nxt_bit^flip); } return lb; } int upper_bound(int value, int x=0) { int ub = 0; int root = 0; for(int i = log; i >= 0; i--) { int nxt_bit = (value>>i)&1; int flip = (x>>i)&1; if(nxt_bit) { if(flip) ub += subtree_cnt(b1[root]); else ub += subtree_cnt(b0[root]); } root = move(root, nxt_bit^flip); } ub += subtree_cnt(root); return ub; } }; int main() { BinaryTrie bt; long long n, k; cin >> n >> k; vector a(n); for(int i = 0; i < n; ++i) { cin >> a[i]; bt.add_value(a[i]); } // Find N + 2 * K kth number const long long pos = n + 2 * k - 1; int ng = -1, ok = 1e9; while(ok - ng > 1) { int x = (ok + ng) >> 1; long long count = 0; for(int i = 0; i < n; ++i) { count += bt.upper_bound(x, a[i]); } if(count >= pos) ok = x; else ng = x; } cout << ok << endl; }