Scanf.scanf "%d" (fun n -> let module M = Map.Make (struct type t = string let compare = compare end) in let rec loop i acc = if i = n then acc else let acc = Scanf.scanf " %d %d %d" (fun no m s -> let rec loop_j j acc = if j = m then acc else let acc = Scanf.scanf " %s" (fun key -> M.add key (s + try M.find key acc with _ -> 0) acc) in loop_j (j + 1) acc in loop_j 0 acc ) in loop (i + 1) acc in let map = loop 0 M.empty in let a = Array.of_list @@ M.bindings map in Array.sort (fun (a1, b1) (a2, b2) -> compare (-b1, a1) (-b2, a2)) a; for i = 0 to min 9 (Array.length a - 1) do let (tag, sc) = a.(i) in Printf.printf "%s %d\n" tag sc done )