#!/usr/local/bin/perl -w

open IN , "<".$ARGV[0] or die "Cannot open $ARGV[0]:$!";
chomp ($nmachin = <IN>);
for (1..$nmachin){
    chomp ($s = <IN>);
    ($x , $y) = split /\s+/, $s;
    $name{$y}=$x;
}
%busy = %name;
chomp ($njobs = <IN>);
for $i (0..$njobs){
    $count=0;
    for (;;){
        $s = <IN>;
        chomp $s;
        ($x, $y) = split /\s+/, $s;
        if ($x !~ /^\d/){
	    $named[$i+1]=$x;
	    last;
        }
        $count++;
        $secuen[$i][$count]=$y;
        $seq[$i]{$y}=$x;
    }
    last if ($x eq "END");
}
$s = <IN>;
chomp $s;
($arraivalmean, $tmaxh) = split /\s+/, $s;
$s = <IN>;
chomp $s;
@s = split /\s+/, $s;
$count=0;
$prob[0]=0;
while (@s){
    $count++;                             
    $temp = shift @s;
    $prob[$count] = $prob[$count-1]+$temp;
   shift @s;
}
# print "\n@prob\n";
close IN;

$jobs=0;
$time=0;
$lastev=-$arraivalmean;
for (1..$njobs){
    $c[$_]=0;
}

for $hour (1..$tmaxh){
    for $minu (0..59){
	if ($time>=$lastev+$arraivalmean){
	    $r = rand($prob[$njobs]);
#	    print "$r\n";
	    for $n (1..$njobs){
		if ($r <= $prob[$n] && $r > $prob[$n-1] ){
		    $c[$n]++; 
		    event($n, $c[$n]);
		    $lastev=$time;
		    last;
		}			
	    }						
	}
	for $n (1..$njobs){
	    for $z (1..$c[$n]){
		if (defined $startevent[$n][$z][1]){
		    if (!defined $timejob[$n][$z]){
			event($n,$z);
		    }
		}
	    }
	}
	$time += 1/60;
    }
}
report();


sub event{
    my $i = shift;
    my $c = shift;			
    if (!defined $startevent[$i][$c][1]){ # start a job
	$j[$i][$c] = 1;
	$startevent[$i][$c][$j[$i][$c]]=$time;
	
	$machine[$i][$c]=$secuen[$i][$j[$i][$c]];
	$busy{$machine[$i][$c]}--;
	if ($busy{$machine[$i][$c]}<0){
	    $busy{$machine[$i][$c]}=-1;
	    $queue{$machine[$i][$c]}[0]+=1/60;
	    $queue{$machine[$i][$c]}[$c]+=1/60;
	    $totalqueue+=1/60;
	}else{
	    $stopevent[$i][$c][$j[$i][$c]]=$time+$seq[$i]{$machine[$i][$c]};
	    $stat[$i][$j[$i][$c]]+= $stopevent[$i][$c][$j[$i][$c]]-$startevent[$i][$c][$j[$i][$c]];
	}
    }elsif (defined $stopevent[$i][$c][$j[$i][$c]]){ # start a subprocess
	if ($time >= $stopevent[$i][$c][$j[$i][$c]]){
	    $busy{$machine[$i][$c]}++; 
	    if ($busy{$machine[$i][$c]}>$name{$machine[$i][$c]}){
		$busy{$machine[$i][$c]}=$name{$machine[$i][$c]};
	    }
	    $j[$i][$c]++;
	    $startevent[$i][$c][$j[$i][$c]]=$time;
	    $machine[$i][$c]=$secuen[$i][$j[$i][$c]];
	    if (!defined $machine[$i][$c]){
                $timejob[$i][$c]=$time-$startevent[$i][$c][1];
		$job[$i]++;
		$jobs++;
		return ;
	    }
	    $busy{$machine[$i][$c]}--;
	    if ($busy{$machine[$i][$c]}<0){
		$busy{$machine[$i][$c]}=-1;
		$queue{$machine[$i][$c]}[0]+=1/60;
		$queue{$machine[$i][$c]}[$c]+=1/60;
		$totalqueue+=1/60;
	    }else{
		$stopevent[$i][$c][$j[$i][$c]]=$time+$seq[$i]{$machine[$i][$c]};
		$stat[$i][$j[$i][$c]]+= $stopevent[$i][$c][$j[$i][$c]]-$startevent[$i][$c][$j[$i][$c]];
	    }
	}else{ # processing, do nothing.
	    return; 
	}
    }else{  # wait for a machine
	if ($busy{$machine[$i][$c]}<0){
	    $queue{$machine[$i][$c]}[0]+=1/60;
	    $queue{$machine[$i][$c]}[$c]+=1/60;
	    $totalqueue+=1/60;
	}else{
	    $stopevent[$i][$c][$j[$i][$c]]=$time;
	}
	return ;
    }
}

sub report{
    print "Elapsed time is $time\n";
    print "Number of jobs completed is $jobs\n";
    print "DISTRIBUTED MEAN TIME PER MACHINE:\n";
    for $i (1..$njobs){
	print "$named[$i]:\n";
	$count=0;
	for (;;){
	    $count++;
	    last if (!defined $secuen[$i][$count]);
	    print "\t$secuen[$i][$count]\t\t";    
	    print $stat[$i][$count]/$name{$secuen[$i][$count]},"\n";
	}
    }
    print "QUEUE STATUS:\n";
    for my $i (keys %name){
	if (!defined $queue{$i}[0]) { $queue{$i}[0]=0; }
	print "average queue $i ", $queue{$i}[0], "\n";
    }
    print "Total average queue time is $totalqueue\n";
    print "JOB STATUS:\n";
    for $i (1..$njobs){
	print "Job $i $named[$i]: $job[$i] done\n";	
    }
    print "JOBS MEAN TIME:\n";
    for $i (1..$njobs){
        for $z (1..$c[$i]){
	    if (defined $timejob[$i][$z]){
                $mean[$i]+= $timejob[$i][$z];
	    }
	}
        $mean[$i] /= $c[$i];
    	print "Job $i: $mean[$i] hours\n";
    }			
    print "\n";
}


__END__
