#!/usr/bin/env perl
my %twin = (
')' => '(',
'}' => '{',
']' => '[',
);
COLLECT:
while (<>) {
my ($max, @stack) = 0;
for my $char ( split // ) { if ( $char =~ / [[({] /x ) {
( $max < 1+ $#stack ) && ( $max = 1+ $#stack );
}
elsif ( $char =~ / [])}] /x ) {
unless ( pop @stack eq $twin{$char} ) { next COLLECT;
}
}
}
}
IyEvdXNyL2Jpbi9lbnYgcGVybApteSAldHdpbiA9ICgKICAgICcpJyA9PiAnKCcsCiAgICAnfScgPT4gJ3snLAogICAgJ10nID0+ICdbJywKKTsKCkNPTExFQ1Q6CndoaWxlICg8PikgewogICAgbXkgKCRtYXgsIEBzdGFjaykgPSAwOwogICAgZm9yIG15ICRjaGFyICggc3BsaXQgLy8gKSB7CiAgICAgICAgaWYgKCAkY2hhciA9fiAvIFtbKHtdIC94ICkgewogICAgICAgICAgICBwdXNoIEBzdGFjaywgJGNoYXI7CiAgICAgICAgICAgICggJG1heCA8IDErICQjc3RhY2sgKSAmJiAoICRtYXggPSAxKyAkI3N0YWNrICk7CiAgICAgICAgfQogICAgICAgIGVsc2lmICggJGNoYXIgPX4gLyBbXSl9XSAveCApIHsKICAgICAgICAgICAgdW5sZXNzICggcG9wIEBzdGFjayBlcSAkdHdpbnskY2hhcn0gKSB7CiAgICAgICAgICAgICAgICBwcmludCAiLTFcbiI7CiAgICAgICAgICAgICAgICBuZXh0IENPTExFQ1Q7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBwcmludCAiJG1heFxuIjsKfQ==