def ok(score, pair) head = N.size - 1 count = 0 N.each_index.inject(head) do |big, small| break unless big > small next big if big == pair || small == pair oscore = N[big] + N[small] if oscore > score count += 1 big - 1 else big end end count < M end n, M = gets.split.map(&:to_i) N = n.times.map{ gets.to_i } K = N.shift N.sort! min_pair = N.bsearch_index do |pair| ok(N[pair] + K, pair) end if min_pair.nil? puts -1 else puts N[min_pair] end