<?php
// Staring straight up into the sky ... oh my my
function calculateDelivery(&$banknotes, $amount){
$amountSave = $amount;
$INF = 1000000000;
$minBanknotes[0][0] = 0;
for($m=0; $m<=$amount; $m++){
$minBanknotes[0][$m] = $m==0?0:$INF;
$p=1;
foreach ($banknotes as $key => $value) {
if($m <= $key-1){
$minBanknotes[$p][$m] = $minBanknotes[$p-1][$m];
}else{
$minBanknotes[$p][$m] = min($minBanknotes[$p-1][$m], $minBanknotes[$p][$m-$key] + 1); }
$p++;
}
}
for($i=count($banknotes);$i>=1;$i--){ $amount = $amountSave;
if ($minBanknotes[$i][$amount]==$INF){
exit("Данным набором банкнот сумму выдать невозможно"); }else{
while($amount>0){
foreach ($minBanknotes[$i] as $key => $value) {
if ($minBanknotes[$i][$amount-$key] == $minBanknotes[$i][$amount]-1){
$result[$key]++;
$amount -= $key;
break;
}
}
}
}
$resultStr = "";
foreach ($result as $key => $value) {
if($value != 0){
$resultStr = $resultStr."{$value}x{$key} ";
}
}
echo $resultStr."\n";
}
}
$amount = 66;
2 => 3,
5 => 1,
20 => 4,
50 => 1
);
echo "Сумма: {$amount}\n";
calculateDelivery($banknotes, $amount);
?>
PD9waHAKCS8vIFN0YXJpbmcgc3RyYWlnaHQgdXAgaW50byB0aGUgc2t5IC4uLiBvaCBteSBteQoJZXJyb3JfcmVwb3J0aW5nKC0xKTsKCW1iX2ludGVybmFsX2VuY29kaW5nKCd1dGYtOCcpOwoKCWZ1bmN0aW9uIGNhbGN1bGF0ZURlbGl2ZXJ5KCYkYmFua25vdGVzLCAkYW1vdW50KXsKCQlrc29ydCgkYmFua25vdGVzKTsKCQkKCQkkYW1vdW50U2F2ZSA9ICRhbW91bnQ7CgkJJElORiA9IDEwMDAwMDAwMDA7CgkJJG1pbkJhbmtub3RlcyA9IGFycmF5KCk7CgkJJG1pbkJhbmtub3Rlc1swXVswXSA9IDA7CgkJCgkJZm9yKCRtPTA7ICRtPD0kYW1vdW50OyAkbSsrKXsKCQkJJG1pbkJhbmtub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