void main() { auto N = readln.chomp.to!int; alias Entry = Tuple!(string, "s", long, "x"); auto S = new Entry[N]; foreach (i; 0 .. N) { S[i].s = readln.chomp; S[i].x = cast(long)(i + 1) * (N - i); } S.sort; for (int i = 0; i < N; ) { int j = i; long sum; while (j < N && S[i].s == S[j].s) sum += S[j++].x; writeln(sum, ' ', S[i].s); i = j; } } import std, core.bitop; bool chmin(T)(ref T A, T B) { if (A > B) { A = B; return true; } else { return false; } } bool chmax(T)(ref T A, T B) { if (A < B) { A = B; return true; } else { return false; } } int lowerBound(T)(T[] ar, T x) { int l = -1, r = cast(int)(ar.length); while (r - l > 1) { int mid = l + r >> 1; (ar[mid] < x ? l : r) = mid; } return r; } int upperBound(T)(T[] ar, T x) { int l = -1, r = cast(int)(ar.length); while (r - l > 1) { int mid = l + r >> 1; (ar[mid] > x ? r : l) = mid; } return r; }