sub f{ local@a; local$s=0; "@_"=~/([1-9])(.*)(?!\1)(.)(.*)\3(?{push@a,[$1.$3x2,"$`$2$4$'"]})^/; #$s+=$-=$$_[0]-$s+f($$_[1])for@a; #$s<($x=$$_[0]+f($$_[1]))&&($s=$x)for@a; $s<($x=$$_[0]+0+f($$_[1].''))&&($s=$x)for@a; $s; } #print f<> $_=<>;chomp;print f$_