#use strict;
use warnings;
local $SIG{__WARN__} = sub {$w = shift};
use Time::HiRes;
$|++;

srand;
my @lines = <>;
my $n     = 50000;
my $long  = '';
   $long .= qw{( )}[ int(rand() * 2) ] for 1 .. $n;
   
push @lines, $long;

my @lines2 = @lines;

my $start = Time::HiRes::time();

my $depth = 40;
my $pre_regex2 = '(?:\((?:\d+,)*(?:\)|' x $depth . '(?!)' . '\)))+' x $depth;
#	(?:
#		\(
#			(?:\d+,)*	(?:
#							\)
#							|
#							(?!)\)
#						)
#	)+
my $regex2 = qr/$pre_regex2/;
my $regex3 = qr/(?:\d+,){2,}/;

for my $string (@lines2){
	
	while ( 
		$string =~ s/$regex2/
			$a=$&,
			$b=()=$a=~m{[)(]}g,
			$a=~s{\d+,}{$b+=$&,''}eg,
			"$b,"
			/eg 
		or
		$string =~ s/$regex3/
			$a=$&,
			$b=0,
			$a=~s{\d+,}{$b+=$&,''}eg,
			"$b,"
			/eg 
		)
		{}
	my @groups = $string =~ m/\d+/g;
	my $max=0;
	for my $group (@groups){
		$max < $group and $max = $group
		}
	my $occ = 0;
	for my $group (@groups){
		$occ += $max == $group
		}
	$max or $occ=1;
	print "$max $occ\n";
	}
	
print Time::HiRes::time() - $start, $/;

$start = Time::HiRes::time();

my $regex =
qr{
    \(                  # match an opening parenthesis
         (?0)*          # recurse to capture full pattern
    \)                  # match a closing parenthesis
}x;

for my $string (@lines)
{
    my $max    = 0;
    my $occ    = 1;
 	my @groups = ();
    my $pred = -1;
    my $i = 0;
    while ($string =~ m/$regex/g){
    	$pred == $-[0] or $i++;
    	$groups[$i] += $+[0] - $-[0];
    	$pred = $+[0];
    	}

    if (@groups)
    {
        @groups = sort {    $a <=>    $b } @groups;
        $max =    $groups[-1];

        for my $i (reverse 0 .. $#groups - 1)
        {
            if (    $groups[$i] ==     $groups[-1])
            {
                ++$occ;
            }
            else
            {
                last;
            }
        }
    }

    print "$max $occ\n";
}

print Time::HiRes::time() - $start, $/;

__DATA__
)((())))(()())
))(
()(())()
((((()(((
(()())()(())()()())())()((()(()(())()()())((()(())()(()()()()))()(())()(((()())()(()((())()(())(()))
