@$_=glob<>for P..V;sub f{my($i,$c,$t)=@_;$i=="@P"?$\=$t:map$S[$_]-$i||f($T[$_],$c-$U[$_],$t+$V[$_]),0..@S if$c>=0&$t<$\<<0}f!$\--,@Q;print