import java.util.*; import java.io.*; public class Medals { public static int ALL_MEDALS = 7; public static int[] PICK_TWO = {3,5,6}; public static int[] PICK_ONE = {1,2,4}; Scanner in = new Scanner(System.in); ArrayList countries = new ArrayList(); Country canada; int num_medals; public class Country { public int[] medals; public String name; public Country(String n,int g, int s, int b) { medals = new int[] {g,s,b}; name = n; } public int total(int mask) { int tot = 0; for(int i=0; i<3; i++) { if((mask&(1< { public int medal_mask; public CountrySorter(int mask) { medal_mask = mask; } // reverse order public int compare(Country c1, Country c2) { return c2.total(medal_mask) - c1.total(medal_mask); } } public static void main(String[] args) { new Medals().solve(); } public void solve() { //in = new Scanner("3\nCanada 2 2 0\nUSA 3 0 1\nKenya 0 3 1\n0"); //in = new Scanner("3\nCanada 2 2 2\nUSA 3 2 1\nKenya 1 2 2\n0"); while(true) { // read the number of countries int num_countries = Integer.parseInt(in.nextLine()); if(num_countries == 0) return; // for each, make a new country and keep track of medal count countries.clear(); num_medals = 0; for(int i=0; i country_list, int medals_to_check) { // see if canada has the most in any one medal category included in medals_to_check; if so, win! // if tie, add to our lists ArrayList> c1 = new ArrayList>(); for(int curr_medal=0; curr_medal < 3; curr_medal++) { c1.add(new ArrayList()); int curr_mask = 1 << curr_medal; if((curr_mask & medals_to_check) != 0) { Collections.sort(country_list,new CountrySorter(curr_mask)); int best_total = country_list.get(0).total(curr_mask); int canada_total = canada.total(curr_mask); if(canada_total > best_total) return true; if(canada_total == best_total) { // keep a list of ties in each medal category for(int i=0; i canada_total && c1.get(0).size() == 0 && c1.get(1).size() == 0 && c1.get(2).size()==0) {return false;} // go through each set of ties, and try to break them by considering other two medals for(int m=0; m<3; m++) { int mask = 1 << m; if(c1.get(m).size() > 0) { // we have tied with these guys int new_mask = medals_to_check & (~mask); // all the medals to check except the one we tied on if(new_mask != 0 && checkWin(c1.get(m),new_mask)) return true; } } return false; } }