import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; public class footballStats { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); HashMap> stats = new HashMap<>(); HashMap totalScore = new HashMap<>(); String team1 = ""; String team2 = ""; String score1 = ""; String score2 = ""; String input = ""; while (!"Season End".equals(input = reader.readLine())) { String[] teams = input.split(" - "); team1 = teams[0]; String[] teamStats = teams[1].split("\\s+"); team2 = teamStats[0]; String[] scores = teamStats[2].split(":"); score1 = scores[0]; score2 = scores[1]; stats.putIfAbsent(team1, new ArrayList<>()); stats.get(team1).add(team2 + " -> " + teamStats[2]); totalScore.putIfAbsent(team1,0L); totalScore.replace(team1,totalScore.get(team1)+Long.parseLong(score1)); stats.putIfAbsent(team2, new ArrayList<>()); stats.get(team2).add(team1 + " -> " + score2+":"+score1); totalScore.putIfAbsent(team2,0L); totalScore.replace(team2,totalScore.get(team2)+Long.parseLong(score2)); } LinkedHashMap sorted = totalScore.entrySet().stream() .sorted(Map.Entry.comparingByValue(Collections.reverseOrder())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,e2) -> e1 ,LinkedHashMap::new)); List searchedTeams = Arrays.stream(reader.readLine().split(", ")).collect(Collectors.toList()); for(Map.Entry team : sorted.entrySet()){ if(searchedTeams.contains(team.getKey())){ stats.get(team.getKey()).stream() .sorted((t1,t2) -> t1.substring(0,t1.indexOf(" ")) .compareTo(t2.substring(0,t2.indexOf(" ")))) .forEach(t -> System.out.printf("%s - %s\n",team.getKey(),t)); } } } }