using System.Text; using System.Linq; using System.Collections.Generic; using System; public class Hello { static void Main() { var n = int.Parse(Console.ReadLine().Trim()); getAns(n); } static void getAns(int n) { var d = new Dictionary(); for (int i = 0; i < n; i++) { var s = Console.ReadLine().Trim(); if (d.ContainsKey(s)) d[s] += (long)(i + 1) * (n - i); else d[s] = (long)(i + 1) * (n - i); } var Lex = d.Keys.ToArray(); Array.Sort(Lex, string.CompareOrdinal); var sb = new StringBuilder(); foreach (var x in Lex) sb.Append(string.Format("{0} {1}\n", d[x], x)); Console.Write(sb); } }