<?php
function constructGraph($bills, $amount){
global $topsOut, $topsIncluded, $weights;
foreach($bills as $nomination => $quantity){
do{
if(($amount-$nomination)>=0 && ($bills[$nomination] - 1)>=0){
$amount = $amount - $nomination;
$bills[$nomination]--;
if($amount==0){
return;
}
}
}while($bills[$nomination]>0 && ($amount-$nomination)>=0);
}
}
// $amount = 54500;
// $bills = array(
// 5000 => 200,
// 1000 => 0,
// 500 => 5,
// 100 => 23
// );
$amount = 6600;
5000 => 1,
2000 => 4,
500 => 1,
200 => 3
);
// $amount = 24;
// $bills = array(
// 10 => 1,
// 5 => 5,
// 1 => 5
// );
// $amount = 8;
// $bills = array(
// 9 => 1,
// 4 => 1,
// 2 => 5
// );
$amountSave = $amount;
foreach($bills as $nomination => $quantity){
if(($amount-$nomination)>=0 && ($bills[$nomination] - 1)>=0){
$bills[$nomination]--;
constructGraph($bills, $amount-$nomination);
}
}
$result[$weights[$num]]++;
if($num){
$n = $topsOut[$num];
while($topsOut[$num]!=$amountSave){
$num--;
$result[$weights[$num]]++;
}
}
$resultStr = "";
foreach ($result as $key => $value) {
if($value != 0){
$resultStr = $resultStr."{$value}x{$key} ";
}
}
echo $resultStr."\n";
?>
PD9waHAKCWVycm9yX3JlcG9ydGluZygtMSk7CgltYl9pbnRlcm5hbF9lbmNvZGluZygndXRmLTgnKTsKCQoJZnVuY3Rpb24gY29uc3RydWN0R3JhcGgoJGJpbGxzLCAkYW1vdW50KXsKCQlnbG9iYWwgJHRvcHNPdXQsICR0b3BzSW5jbHVkZWQsICR3ZWlnaHRzOwoJCWZvcmVhY2goJGJpbGxzIGFzICRub21pbmF0aW9uID0+ICRxdWFudGl0eSl7CgkJCWRvewoJCQkJaWYoKCRhbW91bnQtJG5vbWluYXRpb24pPj0wICYmICgkYmlsbHNbJG5vbWluYXRpb25dIC0gMSk+PTApewoJCQkJCWFycmF5X3B1c2goJHRvcHNPdXQsICRhbW91bnQpOwoJCQkJCWFycmF5X3B1c2goJHRvcHNJbmNsdWRlZCwgJG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