N = gets.to_i L = gets.split.map(&:to_i) T = gets.to_i def calc_score(type, counter) counter[type] += 1 (50 * L[type] + 50 * L[type] / (0.8 + 0.2 * counter[type])).to_i end data = Hash.new { |h, k| h[k] = Hash.new(0) } counter = Hash.new(0) fast_bonus = Hash.new(0) penalty = Hash.new(0) scores = Hash.new(0) T.times do |t| name, p_type = gets.chomp.split p_type = p_type.ord - 'A'.ord score = calc_score(p_type, counter) scores[name] += score fast_bonus[scores[name]] += 1 penalty[name] = fast_bonus[scores[name]] data[name][p_type] = score end ranking = [] data.each do |name, d| row = [] row << name sum = 0 N.times do |i| sum += d[i] row << d[i] end row << sum ranking << row end ranking.sort_by { |row| row.last - 0.01 * penalty[row.first] }.reverse_each.with_index(1) do |row, rank| puts "#{rank} #{row.join(' ')}" end