import java.util.Scanner; import java.util.Arrays; public class BirthdayBoy_Helene { static int[] days = {31,28,31,30,31,30,31,31,30,31,30,31}; static int[] cumulative = new int[12]; // inner class to keep Dates public static class Date implements Comparable { public int mm, dd, hash; public Date(int mm, int dd) { this.mm = mm; this.dd = dd; this.hash = cumulative[mm-1] + dd; } // compute string representing the previous date public String prev(){ int ddp = dd; int mmp = mm; if (dd > 1) {ddp--;} else if (mm > 1) {mmp--; ddp = days[mmp-1];} else {mmp = 12; ddp = 31;} return "" + String.format("%02d", mmp) + "-" + String.format("%02d", ddp); } public int compareTo(Date d){ return this.hash - d.hash; } } // compute difference (in nb days) between two dates public static int diff(Date d1, Date d2) { return (365 - d1.hash + d2.hash) % 365; } public static void main(String[] args){ // fill cumulative for(int i = 0; i < 11; i++) cumulative[i+1] = cumulative[i] + days[i]; // read file Scanner reader = new Scanner(System.in).useDelimiter("[\\s-]");; int n = reader.nextInt(); Date[] date = new Date[n]; for(int i = 0; i < n; i++){ reader.next("[a-zA-Z]+"); date[i] = new Date(reader.nextInt(), reader.nextInt()); } reader.close(); // sorting following the hash (O(nlogn) basic sorting from java) Arrays.sort(date); // compute first date having smallest distance with previous one int k = 0; int duration = diff(date[n-1],date[0]); //System.err.println(date[n-1].prev() + " " + date[0].prev() + " " +duration); Date BAPCdate = new Date(10, 29); int fromBAPC = diff(BAPCdate,date[0]); for(int i = 1; i < n; i++){ int newduration = diff(date[i-1], date[i]); //System.err.println(date[i-1].prev() + " " + date[i].prev() + " " +newduration + " " + diff(BAPCdate, date[i])); if (newduration > duration){ k = i; duration = newduration; fromBAPC = diff(BAPCdate,date[i]); } else if (newduration == duration){ int newfromBAPC = diff(BAPCdate,date[i]); if (newfromBAPC < fromBAPC){ k = i; duration = newduration; fromBAPC = newfromBAPC; } } } // output best (previous of date having smallest distance) System.out.println(date[k].prev()); } }