class L
  def initialize(r=true)
    @inf = r
    @sum = 0
    @ts = []
    @nums = []
  end
  def add(*ts)
    ts.each{ |t|
      @ts.push t[0]
      @nums.push t[1]
      @sum += t[1]
    }
  end
  def get_id(n,c,d)
    d < c ? n : get_id(n+1, c+@nums[n+1], d)
  end
  def draw
    return nil if @sum == 0
    id = get_id(0, @nums[0], rand(@sum))
    unless @inf
      @nums[id] -= 1
      @sum -= 1
    end
    @ts[id]
  end
end