//	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 selcted 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.

#include <iostream>
#include <fstream>

using namespace std;

class Track {
private:
	int time;
	bool used;
public:
	Track(int t = 0) :
		time(t), used(false)
			{}
		void print();
		void setTime(int t) { time = t;	}
		int getTime() { return time; }
		bool getUsed() { return used; }
		void setUsed() { used = true; }
};

class CD {
private:
	enum {MAXTRACKS = 100};
	Track tracks[MAXTRACKS];
	int nTracks;
	int totalTime;
public:
	CD() : nTracks(0), totalTime(0) {}
	void readTrackTimes();
	int findNextTrack();
	void setUsed(int t) { tracks[t].setUsed(); }
	void addTime(int t) { totalTime += tracks[t].getTime(); }
	int getTotalTime() { return totalTime; }
	int getTrackTime(int t) { return tracks[t].getTime(); }
	void printAllocation(); 
};

class Allocate {
private:
	CD cd;
public:
	void run();
};

void Track::print() {
	cout << "(" << time / 60 << ":"
		 << time % 60 << ")" << endl;
}

void CD::printAllocation() {
	int tracksUsed = 0;
	for (int t = 0; t < nTracks; t++)
		if (tracks[t].getUsed()) {
			++tracksUsed;
			cout << "Track " << tracksUsed << " ";
			tracks[t].print();
		}
	cout << "Total time: " << totalTime / 60 << ":"
		 << totalTime % 60 << endl;
	if (nTracks == tracksUsed)
		cout << "WARNING: All files allocated" << endl;
}

void CD::readTrackTimes() {
	char fn[256];
	cout << "File name? ";
	cin >> fn;
	ifstream in(fn);
	if (in.fail()) {
		cout << "File not found" << endl;
		exit(1);
	}
	int mins, secs;
	char colon;
	while (!in.eof()) {
		in >> mins;
		in.get(colon);
		in >> secs;
		tracks[nTracks++].setTime(mins * 60 + secs);
	}
}

int CD::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;
}



void Allocate::run() {
	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();
}


int main() {
	Allocate allocate;
	allocate.run();
	return 0;
}

