#!/usr/bin/perl -w

my %tasks = ();
while (<>) {        
  chomp;
  next if /^\s*$/;
  my ($task, $duration, @prereqs) = split /\s+/;
  @{$tasks{$task}}{'duration', 'prereq', 'painted'} =
    ($duration, \@prereqs, 0);
}              
        
my ($duration, @path) = CritPath('FINAL');
#print "Critical Path: ($duration days)\n",
#      map(" $_\t$tasks{$_}{duration}\n", @path);

print "$_\n" for @path, '';
          
sub CritPath {      
  my ($node) = $tasks{$_[0]} ||  die "Bad prereq $_[0]\n";
  $node->{painted}++         and die "Tasks must be acyclic.\n";
  my @maxpath = (0);
  for my $req (@{$node->{prereq}}) {  
    my @path = CritPath($req);
    @maxpath = @path if $path[0] > $maxpath[0];      
  }
  $node->{painted}--;
  $maxpath[0] += $node->{duration};
  return (@maxpath, $_[0]);
}
