<?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++;
}
}
$amount = $amountSave;
krsort($banknotes, SORT_NUMERIC
); while($amount>0){
foreach ($banknotes as $key => $value) {
while((($result[$key]+1) <= $banknotes[$key]) & ($amount-$key)>=0){
if ($minBanknotes[$i][$amount-$key] == $minBanknotes[$i][$amount]-1){
$result[$key]++;
$amount -= $key;
}else{
echo $amount."\n";
}
}
$i--;
if($i<0){
exit("Все пошло по пизде"); }
}
}
$resultStr = "";
foreach ($result as $key => $value) {
if($value != 0){
$resultStr = $resultStr."{$value}x{$key} ";
}
}
echo $resultStr."\n";
}
$amount = 34;
2 => 3,
5 => 10,
10 => 1
);
echo "Сумма: {$amount}\n";
$rez = calculateDelivery($banknotes, $amount);
?>
PD9waHAKCS8vIFN0YXJpbmcgc3RyYWlnaHQgdXAgaW50byB0aGUgc2t5IC4uLiBvaCBteSBteQoJZXJyb3JfcmVwb3J0aW5nKC0xKTsKCW1iX2ludGVybmFsX2VuY29kaW5nKCd1dGYtOCcpOwoKCWZ1bmN0aW9uIGNhbGN1bGF0ZURlbGl2ZXJ5KCYkYmFua25vdGVzLCAkYW1vdW50KXsKCQlrc29ydCgkYmFua25vdGVzKTsKCQkkYW1vdW50U2F2ZSA9ICRhbW91bnQ7CgkJJElORiA9IDEwMDAwMDAwMDA7CgkJJG1pbkJhbmtub3RlcyA9IGFycmF5KCk7CgkJJG1pbkJhbmtub3Rlc1swXVswXSA9IDA7CgkJCgkJZm9yKCRtPTA7ICRtPD0kYW1vdW50OyAkbSsrKXsKCQkJJG1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