It's quite slow, but that must be enough...
#!perl $_=1;for($m=pop;$m;){s/([^0]{1,5})0*$/$1*$m--/e}/.$/;print"$& "