import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class AnonymousCache { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Map> contest = new HashMap<>(); Map registeredContestants = new HashMap<>(); String data; while (!"END".equals(data = scanner.nextLine())) { String[] info = data.split("\\s+->\\s+"); String contestant = info[1]; String country = info[0]; registeredContestants.putIfAbsent(contestant, country); if (!registeredContestants.get(contestant).equals(country)) { continue; } int points = Integer.parseInt(info[2]); contest.putIfAbsent(country, new LinkedHashMap<>()); contest.get(country).putIfAbsent(contestant, 0); contest.get(country).put(contestant, contest.get(country).get(contestant) + points); } AtomicInteger sum1 = new AtomicInteger(); AtomicInteger sum2 = new AtomicInteger(); contest.entrySet().stream().sorted((pair1, pair2) -> { sum1.set(0); sum2.set(0); pair1.getValue().forEach((key, value) -> sum1.addAndGet(value)); pair2.getValue().forEach((key, value) -> sum2.addAndGet(value)); return Integer.compare(sum2.get(), sum1.get()); }).forEach(pair -> { sum1.set(0); pair.getValue().forEach((key, value) -> sum1.addAndGet(value)); System.out.println(String.format("%s: %d", pair.getKey(), sum1.get())); pair.getValue().forEach((key, value) -> System.out.println(String.format("-- %s -> %d" , key, value))); }); } }