<>;@b=@a=sort<>=~/\S/g; $y=0; while($a[0]eq'+'){ ++$p; shift@a; } while($a[0]eq'-'){ ++$m; shift@a; } while($m){ $y-=shift@a; --$m; } while($p){ $y+=shift@a; --$p; } $y+=join'',reverse@a; $z=$p=$m=0; while($b[0]eq'+'){ ++$p; shift@b; } while($b[0]eq'-'){ ++$m; shift@b; } if($m){ while($p>=0){ $z+=shift@b; --$p; } while($m>1){ $z-=shift@b; --$m; } $z-=join'',reverse@b; }else{ $c[$i++%($p+1)].=$_ for@b; $z=eval join'+',@c; } print"$y $z"