use List::Util 'reduce'; print reduce{$a+$b*($b+1<=>$a)}map{/./;$|--&&(M999D499C99L49X9V4I=~$&+$')*y///c}reverse<>=~/((.)\2*)/g