cnt = Hash.new { |h, k| h[k] = 0 } gets.to_i.times do gets n, s = gets.split.map &:to_i gets.split.each do |w| puts w cnt[w] -= s end end cnt.to_a.map(&:reverse).sort.first(10).each do |u| puts "#{u[-1]} #{-u[0]}" end