module LaboratoryAssignment def solve(n, m, aa, at) lab = Array.new(n) val = Array.new(n) (0 ... m).each do |i| (0 ... n).each do |j| next if val[j] if aa[at[j][i]] > 0 lab[j] = at[j][i] aa[at[j][i]] -= 1 val[j] = true end end end lab end module_function :solve end n, m = gets.chomp.split(/\s+/).map(&:to_i) aa = gets.chomp.split(/\s+/).map(&:to_i) at = n.times.map{ gets.chomp.split(/\s+/).map(&:to_i) } puts LaboratoryAssignment.solve(n, m, aa, at).join(" ")