import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class prog { public class Team { private int points; private int goals; private String name; public Team(String name) { this.name = name; points = 0; goals = 0; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Team team = (Team) obj; return team.getName().equals(name); } public int getPoints() { return points; } public void setPoints(int points) { this.points = points; } public int getGoals() { return goals; } public void setGoals(int goals) { this.goals = goals; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList teams = new ArrayList<>(); prog main = new prog(); for (int k = 0; k < 6; k++) { String line = sc.nextLine(); String[] data = line.split(" "); String team1 = data[0]; String team2 = data[1]; int goals1 = Integer.parseInt(data[2]); int goals2 = Integer.parseInt(data[3]); int points1 = 0; int points2 = 0; if (goals1 == goals2) { points1 = points2 = 1; } else if (goals1 > goals2) { points1 = 3; } else { points2 = 3; } Team newTeam = main.new Team(team1); if (teams.contains(newTeam)) { for (int i = 0; i < teams.size(); i++) { Team t = teams.get(i); if (t.getName().equals(team1)) { t.setGoals(t.getGoals() + goals1); t.setPoints(t.getPoints() + points1); } } } else { newTeam.setGoals(goals1); newTeam.setPoints(points1); teams.add(newTeam); } newTeam = main.new Team(team2); if (teams.contains(newTeam)) { for (int i = 0; i < teams.size(); i++) { Team t = teams.get(i); if (t.getName().equals(team2)) { t.setGoals(t.getGoals() + goals2); t.setPoints(t.getPoints() + points2); } } } else { newTeam.setGoals(goals2); newTeam.setPoints(points2); teams.add(newTeam); } } sc.close(); Collections.sort(teams, new Comparator() { @Override public int compare(Team team1, Team team2) { if (team1.getPoints() > team2.getPoints()) { return -1; } else if (team1.getPoints() < team2.getPoints()) { return 1; } else if (team1.getGoals() > team2.getGoals()) { return -1; } else if (team1.getGoals() < team2.getGoals()) { return 1; } else return team1.getName().compareToIgnoreCase(team2.getName()); } }); for (Team t : teams) { System.out.println(t.getName()); } } }