using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); Dictionary tagScores = new Dictionary(); for(int i = 0; i < N; i++) { // データの読み取り Console.ReadLine(); // 入力データの番号が邪魔(使わないデータ) string[] ss = Console.ReadLine().Split(); int M = int.Parse(ss[0]); // 配列の1番目がタグの数 int S = int.Parse(ss[1]); // 配列の2番目がスコア string[] Tag = Console.ReadLine().Split(); // タグの読み取り // データをまとめる for (int tagNum = 0; tagNum < M; tagNum++) { if (tagScores.ContainsKey(Tag[tagNum])) { // Dictionaryにtags[m]と同文字列のキーが既に存在する場合 tagScores[Tag[tagNum]] += S; } else { // キーが無い場合 tagScores[Tag[tagNum]] = S; } } } // タグとスコアのM分配列を用意する string[] tags = new string[tagScores.Count]; int[] score = new int[tagScores.Count]; // Dictionaryから配列に入れ直す int n = 0; foreach(string key in tagScores.Keys) { tags[n] = key; score[n] = tagScores[key]; n++; } // 並べ替え for(int i = 0; i < score.Length; i++) { for(int k = i; k < score.Length - i - 1; k++) { if (score[k] < score[k + 1] || score[k] == score[k + 1] && tags[k].CompareTo(tags[k + 1]) == 1) { int tmp = score[k]; score[k] = score[k + 1]; score[k + 1] = tmp; string tmp2 = tags[k]; tags[k] = tags[k + 1]; tags[k + 1] = tmp2; } } } // 出力処理 int count = 10; if (count > tags.Length) { count = tags.Length; } for (int i = 0; i < count; i++) { Console.WriteLine(tags[i] + " " + score[i]); } } } }