<?php

error_reporting(-1);

$bills = [500, 100];
$totalBills = [5, 5, 5, 5];
$rndCombination = [1, 5, 5, 5];

krsort($rndCombination);
print_r($rndCombination);

function nextCombination($total, $combination){
	if($total == $combination){
		return 'Максимальная комбинация.';
	}
	krsort($combination);
	foreach($combination as $key => &$value){
		if($value < $total[$key]){
			$value++;
			break;
		}
		else{
			$value = 0;
			if($combination[$key-1] < $total[$key-1]){
				$combination[$key-1]++;	
			}
			else{
				$combination[$key-1] = 0;
				$combination[$key-2]++;	
			}
			break;
		}
	}
	ksort($combination);
	return $combination;
}

print_r(nextCombination($totalBills, $rndCombination));