n, m = gets.chomp.split.map(&:to_i) a = gets.to_i as = (2..n).map { gets.to_i }.sort.reverse def ok(a, m, as, ignore_index) t = [] s = [] as.each_with_index do |b, idx| next if idx == ignore_index s << b if s.size == 2 t << s.inject(:+) s = [] end break if t.size == m end a >= t[-1] end ans = -1 left = 0 right = n - 2 if ok(a + as[left], m, as, left) if ok(a + as[right], m, as, right) p as[right] else # bisearch 100.times do mid = (left + right) / 2 if ok(a + as[mid], m, as, mid) left = mid else right = mid end end p as[left] end else p -1 end