a={}\x0d while gets\x0d i=[$_.scan(/\d+$/)[0],$`]\x0d a[i[0]]?a[i[0]]<<i[1]:a[i[0]]=[i[1]]\x0d end\x0d c=1\x0d a.sort_by{|i,j|-i.to_i}.each{|i|(v=i.reverse)[0].each{|o|puts"#{c} #{o}#{v[1]}"};c+=v[0].size}
Note that non-ascii characters in the above source code will be escaped (such as \x9f).