%h=@h=(M,1000,CM,900,D,500,CD,400,C,100,XC,90,L,50,XL,40,X,10,IX,9,V,5,IV,4,I,1); $r=join'|',sort{$b=~/../-$a=~/../}keys%h; <>; <>=~s/$r/$s+=$h{$&}/ger; exit!print"ERROR"if$s>3999; while($s){ ($_)=grep$h{$_}&&$s>=$h{$_},@h; print; $s-=$h{$_}; }