c	This program reads any number of track times (in mins and secs)
c       and allocates them to a CD with maximum time 74 minutes.
c	The list of times is terminated by '0 0'
c	e.g. three tracks with times 2:30, 1:52 and 4:15:
c
c	2 30
c	1 52
c	4 15
c	0 0
c
c	It prints a track list of selcted tracks and the total time for the CD:
c 
c	Track 1 ( 2:30 )
c	Track 2 ( 1:52 )
c	Track 3 ( 4:15 )
c	Total time: 8:37
c	WARNING: All tracks are allocated
c	This last message only occurs if the total time of all tracks is
c       less than 74 mins.

	subroutine readTrackTimes(times)
	integer times(*)
	integer mins, secs, track
	
		track = 1
 10		read (*,*) mins, secs
	if (mins.eq.0.and.secs.eq.0) return
	times(track) = mins * 60 + secs;
	track = track + 1
	goto 10
	end

	integer function  findNextTrack(times)
	integer times(*), track, longest, index

	track = 1
	longest = 0
	index = -1

 10	if (times(track).eq.0) goto 20
	if (times(track).gt.longest) then
	   longest = times(track)
	   index = track
	end if
	track = track + 1
	goto 10
 20	findNextTrack = index
	return
	end
	


	subroutine printAllocation(times, used, total)
	integer times(*), used(*), total, track, trackNum, mins, secs
	track = 1
	trackNum = 0
	
 10	if (times(track).eq.0) goto 20
	if (used(track).eq.1) then
	   trackNum = trackNum + 1
	   mins = times(track) / 60
	   secs = mod(times(track), 60)
	   print *, 'Track ', trackNum, ' (', mins, ':', secs, ')'
	end if
	track = track + 1
	goto 10
 20	print *, 'Total time: ', total / 60, ':', mod(total, 60)
	if (track - 1.eq.trackNum) then
	   write (*,*) 'WARNING: All files allocated'
	end if
        return
	end
	
	program allocateTracks
	integer trackTimes(100), trackUsed(100)
        integer track, timeUsed
        integer findNextTrack
        
	do 10 i = 1, 100
	   trackTimes(i) = 0
	   trackUsed(i) = 0
 10	continue
        
	timeUsed = 0
	
	call readTrackTimes(trackTimes)

 100	track = findNextTrack(trackTimes)
	if (track.eq.-1) goto 200
	if (timeUsed + trackTimes(track).gt.74 * 60) goto 200
	trackUsed(track) = 1
	timeUsed = timeUsed + trackTimes(track)
	track = track + 1
	goto 100
 200	call printAllocation(trackTimes, trackUsed, timeUsed)
	stop
	end

