//This program reads any number of track times (in mins and secs)
//    and allocates them to a CD with maximum time 74 minutes.
//The list of times is terminated by end of file
//e.g. three tracks with times 2:30, 1:52 and 4:15:
//
//2:30
//1:52
//4:15
//
//It prints a track list of selected tracks and the total time for the CD:
//
//Track 1 ( 2:30 )
//Track 2 ( 1:52 )
//Track 3 ( 4:15 )
//Total time: 8:37
//WARNING: All tracks are allocated
//This last message only occurs if the total time of all tracks is
//    less than 74 mins.

import java.io.*;

class Track {
    private int time;
    private boolean used;
    public Track(int t) {
	time = t; used = false; 
    }
    public void print() {
	System.out.println("(" + time / 60 + ":"
			   + time % 60 + ")");
    }
    public void setTime(int t) { time = t;}
    public int getTime() { return time; }
    public boolean getUsed() { return used; }
    public void setUsed() { used = true; }
};

class CD {
    final static public int MAXTRACKS = 100;
    private Track tracks[];
    private int nTracks;
    private int totalTime;
    public  CD() {
	nTracks = 0;
	totalTime = 0;
	tracks = new Track[MAXTRACKS];
    }
    public void readTrackTimes() throws java.io.IOException {
	String fn = "";
	System.out.print("File name? ");
	DataInputStream con = new DataInputStream(System.in);
	fn = con.readLine();
	DataInputStream in = null;
	try {
	    in = new DataInputStream(new
		FileInputStream(fn));
	}
	catch (IOException e) {
	    System.out.println("File not found");
	    System.exit(1);
	}
	int mins, secs;
	String l, lmins, lsecs;
	l = in.readLine();
	lmins = l.substring(0, l.indexOf(':'));
	lsecs = l.substring(l.lastIndexOf(':') + 1);
	mins = Integer.parseInt(lmins);
	secs = Integer.parseInt(lsecs);
	while (true) {
	    tracks[nTracks++] = new Track(mins * 60 + secs);
	    l = in.readLine();
	    if (l == null)
		break;
	    lmins = l.substring(0, l.indexOf(':'));
	    lsecs = l.substring(l.lastIndexOf(':') + 1);
	    mins = Integer.parseInt(lmins);
	    secs = Integer.parseInt(lsecs);
	}
    }
    public int findNextTrack() {
	int longest = 0;
	int index = -1;
	
	for (int t = 0; t < nTracks; t++) {
	    if (tracks[t].getTime() > longest) {
		longest = tracks[t].getTime();
		index = t;
	    }
	}
	return index;
    }
    public void setUsed(int t) { tracks[t].setUsed(); }
    public void addTime(int t) { totalTime += tracks[t].getTime(); }
    public int getTotalTime() { return totalTime; }
    public int getTrackTime(int t) { return tracks[t].getTime(); }
    public void printAllocation() {
	int tracksUsed = 0;
	for (int t = 0; t < nTracks; t++)
	    if (tracks[t].getUsed()) {
		++tracksUsed;
		System.out.print("Track " + tracksUsed  + " ");
		tracks[t].print();
	    }
	int hrs = totalTime / 60;
	int mins = totalTime % 60;
	System.out.println("Total time: " + hrs + ":" + mins);
	if (nTracks == tracksUsed)
	    System.out.println("WARNING: All files allocated");
    }
}

class Allocate {
    private static CD cd = new CD();
    public static void main(String args[]) throws java.io.IOException {
	cd.readTrackTimes();
	int track;
	while ((track = cd.findNextTrack()) != -1) {
	    if (cd.getTotalTime() + cd.getTrackTime(track) > 74 * 60)
		break;
	    cd.setUsed(track);
	    cd.addTime(track);
	    ++track;
	}

	cd.printAllocation();
    }
}











