def selection_sort ary ary_with_index = ary.zip(0...ary.length) (0...ary.length).each{|ix| min = ary_with_index[ix..-1].min_by{|num, ix| num} ary_with_index[ix][0], ary_with_index[min[1]][0] = min[0], ary_with_index[ix][0] } ary_with_index.map{|num, ix| num} end q, k = gets.split.map(&:to_i) s = [] q.times{ if v = gets.split.map(&:to_i)[1] then if s == [] || v >= s[-1] then s.push(v) elsif v <= s[0] then s.unshift(v) else s.unshift(v) s = selection_sort(s) end else puts s.size >= k ? s.delete_at(k - 1) : -1 end }