class Calc0059 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n, @k = args.shift.map(&:to_i) @ws = args.map(&:first).map(&:to_i) end def run stock = [] @ws.each do |w| if w > 0 if stock.size < @k || stock[-@k] < w add(stock, w) end else remove(stock, -w) end end stock.size end def add(stock, w) i = stock.bsearch_index { |x| x >= w } if i stock.insert(i, w) else stock.push(w) end end def remove(stock, w) i = stock.bsearch_index { |x| x >= w } if i if stock[i] == w stock.delete_at(i) end else if stock[0] == w stock.shift end end end end puts Calc0059.new(STDIN.readlines).run if __FILE__ == $0