Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.

The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.

If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that:   the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company,  the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company,  the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,  Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement.

6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }
    for my $logo ( sort {$n} keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n++ >= scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        undef $banner;
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }
    for my $logo ( sort {$n} keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n++ >= scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -1;

while (1) {
    push @{$banner}, [ (undef) x $sqrt ] for 1 .. $sqrt;
    for my $logo ( sort {$n} keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep { !defined } map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n++ >= 1 ) {
            say -1;
            exit;
        }
        undef $banner;
        next;
    }
    last;
}

say $sqrt;
say q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if defined $matrix->[$y][ $x + $_ ];
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if defined $matrix->[ $y + $_ ][$x];
                }
                return 1;
            };

            if ( !defined $matrix->[$y][$x] ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

while(<>){

	$max = (sort {$b <=> $a} split)[0];
	$cmax = () = /\b$max\b/g;

	$area = eval join '+', map { eval join '*', split } @groups = /\d+ \d+/g;

	$area == $max ** 2 and $cmax =~ /[13]/ or do { print -1; next };

	$letter = 'A';

	print join "\n", $max,
		$cmax == 3 ?
		do {
			map { ( $letter ++ x $max ) x $_ } grep !/\b$max\b/, split
			}
		:
		do {
			($wide2) = grep $max != $_, split ' ', join '', grep /\b$max\b/, @groups;
			$wide1 = $max - $wide2;

			$pre = join "\n", map { s/ ?\b$wide1\b ?//; ( $letter ++ x $wide1 . 'C' x $wide2 ) x $_ }
				grep !/\b$max\b/, @groups;

			$groups[0] =~ /\b$max\b/ and $pre =~ y/ABC/BCA/;
			$groups[1] =~ /\b$max\b/ and $pre =~ y/ABC/ACB/;

			$pre
		}
}

$\ = $/;

while(<>){

	@s = sort {$b <=> $a} split;
	($max, $min) = @s[0,-1];
	$cmax = () = /\b$max\b/g;

	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;

	$area = eval join '+', map { eval join '*', split } /\d+ \d+/g;

	$area == $max ** 2 or do { print -1; next };

	%hash = ();
	map { ++ $hash{ $_ } } split;
	(join ' ', values %hash) =~ /[46]/ and do { print -1; next };

	$let = 'A';

	$cmax == 3 ?
	do {
		s/\b$max\b//g;
		(eval join '+', split) == $max or do { print -1; next };

		print join "\n", $max , map { ( $let ++ x $max ) x $_ } split;
		}
	:
	do {
		$cmax == 1 or do { print -1; next };

		($bemax = $_) =~ s/\b$max\b (\d+) ?//;
		$wide2 = $1;
		$wide1 = $max - $wide2;

		$pre = join "\n", map { s/ ?\b$wide1\b ?//; ( $let ++ x $wide1 ) x $_ } $bemax =~ /\d+ \d+/g;

		$pre =~ s/$/ 'C' x $wide2 /gem;

		/^$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ $max\b/ and $pre =~ y/ABC/ACB/;

		print join "\n", $max, $pre;

	}
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }
    for my $logo ( sort { $n++ } sort keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n >= scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

# my @input  = split / /, <>;
my @input = qw(1 2 2 4 3 2);
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt eval join q{+}, ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = 0;
    }
}

for my $logo ( keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;
    say $logo, $rotate//0;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    next if $matrix->[$y][ $x + $_ ] ne 0;
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        next if $matrix->[ $y + $_ ][$x] ne 0;
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }
                elsif ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt eval join q{+}, ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = 0;
    }
}

for my $logo ( keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    next if $matrix->[$y][ $x + $_ ] ne 0;
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        next if $matrix->[ $y + $_ ][$x] ne 0;
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }
                elsif ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}
my $sqrt = sqrt eval join q{+},
    ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

LOOP: while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }

    for my $logo ( sort { int( rand(3) ) - 1 } keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or next LOOP;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }

    last;
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    if ( $matrix->[$y][ $x + $_ ] ne '0' ) {
                        $width_success = 0;
                        next;
                    }
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        if ( $matrix->[ $y + $_ ][$x] ne '0' ) {
                            $success = 0;
                            next;
                        }
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq '0' ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }

    for my $logo ( sort { $n++ } keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n = scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = sort { $b <=> $a } ( $x, $y );
}
my $sqrt = sqrt eval join q{+},
    ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = '0';
    }
}

for my $logo (keys %{$logos}) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    if ( $matrix->[$y][ $x + $_ ] ne '0' ) {
                        $width_success = 0;
                        next;
                    }
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        if ( $matrix->[ $y + $_ ][$x] ne '0' ) {
                            $success = 0;
                            next;
                        }
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq '0' ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt eval join q{+},
    ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = '0';
    }
}

for my $logo ( keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    if ( $matrix->[$y][ $x + $_ ] ne '0' ) {
                        $width_success = 0;
                        next;
                    }
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        if ( $matrix->[ $y + $_ ][$x] ne '0' ) {
                            $success = 0;
                            next;
                        }
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt sum( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = 0;
    }
}

for my $logo ( sort keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub sum {
    my $sum;
    $sum += $_ for @_;
    return $sum;
}

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    next if $matrix->[$y][ $x + $_ ] ne 0;
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        next if $matrix->[ $y + $_ ][$x] ne 0;
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }
                elsif ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

$\ = $/;

while(<>){

	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ > 3 and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	%h = ();
	map { ++ $h{ $_ } } split;
	$_ > 3 and do { print -1; next } for values %h;
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ > 3 and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ > 3 and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\d+).{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
	$max = (sort {$b <=> $a} split)[0];
	$sum == $max ** 2 or do { print -1; next };

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ =~ /[46]/ and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	$g = 0;
	$_ == 5 and $g ++  for values %h;
	$g and do {
		$max = (sort {$b <=> $a} split)[0];
		/\b$max $max\b/ and do { print -1; next };
		$min = (sort {$b <=> $a} split)[1];
		$min * 2 == $max or do { print -1; next };

		$let = 'A';
		$pre = join "\n", ($let x $min) x $min , (++ $let x $min) x $min;
		$pre =~ s/$/ 'C' x $min /gems;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		next;
		};

	/\b(\d+)\b.{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ =~ /[46]/ and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	$g = 0;
	$_ == 5 and $g ++  for values %h;
	$g and do {
		$max = (sort {$b <=> $a} split)[0];
		/\b$max $max\b/ and do { print -1; next };
		$min = (sort {$b <=> $a} split)[1];
		$min * 2 == $max or do { print -1; next };

		$let = 'A';
		$pre = join "\n", ($let x $min) x $min , (++ $let x $min) x $min;
		$pre =~ s/$/ 'C' x $min /gems;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		next;
		};

	/(\d+).{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ =~ /[46]/ and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	$g = 0;
	$_ == 5 and $g ++  for values %h;
	$g and do {
		$max = (sort {$b <=> $a} split)[0];
		/\b$max $max\b/ and do { print -1; next };
		$min = (sort {$b <=> $a} split)[1];
		$min * 2 == $max or do { print -1; next };

		$let = 'A';
		$pre = join "\n", ($let x $min) x $min , (++ $let x $min) x $min;
		$pre =~ s/$/ 'C' x $min /gems;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		next;
		};

	/\b(\d+)\b.{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	@s = sort {$b <=> $a} split;
	($max, $min) = @s[0,-1];
	$cmax = () = /\b$max\b/g;

	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;

	$area = eval join '+', map { eval join '*', split } /\d+ \d+/g;

	$area == $max ** 2 or do { print -1; next };

	%hash = ();
	map { ++ $hash{ $_ } } split;
	(join ' ', values %hash) =~ /[46]/ and do { print -1; next };

	$let = 'A';

	$cmax == 3 ?
	do {
		s/\b$max\b//g;
		(eval join '+', split) == $max or do { print -1; next };

		print join "\n", $max , map { ( $let ++ x $max ) x $_ } split;
		}
	:
	do {
		$cmax == 1 or do { print -1; next };

		($bemax = $_) =~ s/\b$max\b (\d+) ?//;
		$wide2 = $1;
		$wide1 = $max - $wide2;

		$bemax =~ s/\b$wide1\b// for 1 .. 2;

		$pre = join "\n", map { ( $let ++ x $wide1 ) x $_ } split ' ', $bemax;

		$pre =~ s/$/ 'C' x $wide2 /gem;

		/^$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ $max\b/ and $pre =~ y/ABC/ACB/;

		print join "\n", $max, $pre;

	}
}


3R2 - Standby for ActionOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show "1 vs. $$$n$$$"!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).For each question JOE answers, if there are $$$s$$$ ($$$s > 0$$$) opponents remaining and $$$t$$$ ($$$0 \le t \le s$$$) of them make a mistake on it, JOE receives $$$\displaystyle\frac{t}{s}$$$ dollars, and consequently there will be $$$s - t$$$ opponents left for the next question.JOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?

The first and single line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), denoting the number of JOE's opponents in the show.

Print a number denoting the maximum prize (in dollars) JOE could have. Your answer will be considered correct if it's absolute or relative error won't exceed $$$10^{-4}$$$. In other words, if your answer is $$$a$$$ and the jury answer is $$$b$$$, then it must hold that $$$\frac{|a - b|}{max(1, b)} \le 10^{-4}$$$.

NoteIn the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be $$$\displaystyle \frac{1}{2} + \frac{1}{1} = 1.5$$$ dollars.


You are given three strings $$$a$$$, $$$b$$$ and $$$c$$$ of the same length $$$n$$$. The strings consist of lowercase English letters only. The $$$i$$$-th letter of $$$a$$$ is $$$a_i$$$, the $$$i$$$-th letter of $$$b$$$ is $$$b_i$$$, the $$$i$$$-th letter of $$$c$$$ is $$$c_i$$$.For every $$$i$$$ ($$$1 \leq i \leq n$$$) you must swap (i.e. exchange) $$$c_i$$$ with either $$$a_i$$$ or $$$b_i$$$. So in total you'll perform exactly $$$n$$$ swap operations, each of them either $$$c_i \leftrightarrow a_i$$$ or $$$c_i \leftrightarrow b_i$$$ ($$$i$$$ iterates over all integers between $$$1$$$ and $$$n$$$, inclusive).For example, if $$$a$$$ is "code", $$$b$$$ is "true", and $$$c$$$ is "help", you can make $$$c$$$ equal to "crue" taking the $$$1$$$-st and the $$$4$$$-th letters from $$$a$$$ and the others from $$$b$$$. In this way $$$a$$$ becomes "hodp" and $$$b$$$ becomes "tele".Is it possible that after these swaps the string $$$a$$$ becomes exactly the same as the string $$$b$$$?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a string of lowercase English letters $$$a$$$. The second line of each test case contains a string of lowercase English letters $$$b$$$. The third line of each test case contains a string of lowercase English letters $$$c$$$. It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $$$100$$$.

Print $$$t$$$ lines with answers for all test cases. For each test case: If it is possible to make string $$$a$$$ equal to string $$$b$$$ print "YES" (without quotes), otherwise print "NO" (without quotes). You can print either lowercase or uppercase letters in the answers.

4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim

NoteIn the first test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$.In the second test case, you should swap $$$c_i$$$ with $$$a_i$$$ for all possible $$$i$$$. After the swaps $$$a$$$ becomes "bca", $$$b$$$ becomes "bca" and $$$c$$$ becomes "abc". Here the strings $$$a$$$ and $$$b$$$ are equal.In the third test case, you should swap $$$c_1$$$ with $$$a_1$$$, $$$c_2$$$ with $$$b_2$$$, $$$c_3$$$ with $$$b_3$$$ and $$$c_4$$$ with $$$a_4$$$. Then string $$$a$$$ becomes "baba", string $$$b$$$ becomes "baba" and string $$$c$$$ becomes "abab". Here the strings $$$a$$$ and $$$b$$$ are equal.In the fourth test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$.

use 5.020;
use warnings;
use utf8;

sub readNext {
    my $line = <STDIN>;
    chomp($line);
    return $line;
}

my $count = int( &readNext() );

READ: while ($count) {

    my @a = split //, &readNext();
    my @b = split //, &readNext();
    my @c = split //, &readNext();

    $count--;

    foreach ( 0 .. $#a ) {
        if ( $a[$_] eq $c[$_] || $b[$_] eq $c[$_] ) {
            next;
        }
        else {
            say "NO";
            next READ;
        }
    }

    say "YES";
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my $A = $_;
	my $B = <>;
	my $C = <>;

	chomp for $A, $B, $C;

	my $fail = 0;

	for my $i ( 0 .. -1 + length $A ){
		my $cA = substr $A, $i, 1;
		my $cB = substr $B, $i, 1;
		my $cC = substr $C, $i, 1;

		if( not( $cA eq $cC || $cB eq $cC ) ){
			$fail = 1;
			last;
			}
		}

	print $fail ? "NO" : "YES";
	}


An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: "0124:5678:90ab:cdef:0124:5678:90ab:cdef". We'll call such format of recording an IPv6-address full.Besides the full record of an IPv6 address there is a short record format. The record of an IPv6 address can be shortened by removing one or more leading zeroes at the beginning of each block. However, each block should contain at least one digit in the short format. For example, the leading zeroes can be removed like that: "a56f:00d3:0000:0124:0001:f19a:1000:0000"  →  "a56f:d3:0:0124:01:f19a:1000:00". There are more ways to shorten zeroes in this IPv6 address.Some IPv6 addresses contain long sequences of zeroes. Continuous sequences of 16-bit zero blocks can be shortened to "::". A sequence can consist of one or several consecutive blocks, with all 16 bits equal to 0. You can see examples of zero block shortenings below:  "a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::";  "a56f:0000:0000:0124:0001:0000:1234:0ff0"  →  "a56f::0124:0001:0000:1234:0ff0";  "a56f:0000:0000:0000:0001:0000:1234:0ff0"  →  "a56f:0000::0000:0001:0000:1234:0ff0";  "a56f:00d3:0000:0124:0001:0000:0000:0000"  →  "a56f:00d3:0000:0124:0001::0000";  "0000:0000:0000:0000:0000:0000:0000:0000"  →  "::". It is not allowed to shorten zero blocks in the address more than once. This means that the short record can't contain the sequence of characters "::" more than once. Otherwise, it will sometimes be impossible to determine the number of zero blocks, each represented by a double colon.The format of the record of the IPv6 address after removing the leading zeroes and shortening the zero blocks is called short.You've got several short records of IPv6 addresses. Restore their full record.

The first line contains a single integer n — the number of records to restore (1 ≤ n ≤ 100). Each of the following n lines contains a string — the short IPv6 addresses. Each string only consists of string characters "0123456789abcdef:". It is guaranteed that each short address is obtained by the way that is described in the statement from some full IPv6 address.

For each short IPv6 address from the input print its full record on a separate line. Print the full records for the short IPv6 addresses in the order, in which the short records follow in the input.


Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters.Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.

The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length.

Print "YES", if the dwarves belong to the same race. Otherwise, print "NO".

Note  First example: you can simply swap two letters in string "ab". So we get "ba".  Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".

$a=<>;
$b=<>;
@x=sort split '',$a;
@y=sort split '',$b;
$flag=0;
if((join '',@x)eq(join '',@y)){
  @x=split '',$a;
  @y=split '',$b;
  for($i=0;$i<@x;++$i){
    ++$cnt if $x[$i]ne$y[$i];
  }
  $flag=1 if $cnt>2;
} else {
  $flag=1;
}
print qw/YES NO/[$flag];

$a=<>;
$b=<>;
@x=sort split '',$a;
@y=sort split '',$b;
$flag=0;
if((join '',@x)eq(join '',@y)){
  @x=split '',$a;
  @y=split '',$b;
  for($i=0;$i<@x;++$i){
    ++$cnt if $x[$i]ne$y[$i];
  }
  if($cnt>2){
    $flag=1;
    exit;
  }
} else {
  $flag=1;
}
print qw/YES NO/[$flag];


Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.String a is the divisor of string b if and only if there exists a positive integer x such that if we write out string a consecutively x times, we get string b. For example, string "abab" has two divisors — "ab" and "abab".Now Vasya wants to write a program that calculates the number of common divisors of two strings. Please help him.

The first input line contains a non-empty string s1. The second input line contains a non-empty string s2. Lengths of strings s1 and s2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters.

Print the number of common divisors of strings s1 and s2.

NoteIn first sample the common divisors are strings "abcd" and "abcdabcd".In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa".


Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length.Over the years Emuskald has cultivated n plants in his greenhouse, of m different plant species numbered from 1 to m. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange m - 1 borders that would divide the greenhouse into m sections numbered from 1 to m from left to right with each section housing a single species. He is free to place the borders, but in the end all of the i-th species plants must reside in i-th section from the left.Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders.

The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 5000, n ≥ m), the number of plants and the number of different species. Each of the following n lines contain two space-separated numbers: one integer number si (1 ≤ si ≤ m), and one real number xi (0 ≤ xi ≤ 109), the species and position of the i-th plant. Each xi will contain no more than 6 digits after the decimal point. It is guaranteed that all xi are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their xi coordinates (xi < xi + 1, 1 ≤ i < n).

Output a single integer — the minimum number of plants to be replanted.

NoteIn the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.In the second test case, the species are already in the correct order, so no replanting is needed.

#!perl
use v5.18;
# time complexity: O(Nlog(N))
while(<>){
    my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>; push @a,$s0;
    }
    $length = 1; $b[0]=$a[0];
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i], \@b);
        if ($pos == @b) {push @b, $a[$i];}
        else {$b[$pos] = $a[$i];}
    }
    say $n-@b;
}

#find the first more than $num;
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $array->[$high]<=$num ? scalar @$array : $high;
}

#!perl
use v5.18;
# time complexity: O(Nlog(N))
# got Accepted
while(<>){
    my(@b,@a,$i,$n,$m,$s0,$pos);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$_)=split ' ',<>; push @a,$s0;
    }
    $b[0]=$a[0];
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i], \@b);
        if ($pos == @b) {push @b, $a[$i];}
        else {$b[$pos] = $a[$i];}
    }
    say $n-@b;
}

#find the first more than $num;
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $array->[$high]<=$num ? scalar @$array : $high;
}

#!perl
use strict;
use warnings;
while(<>){
    my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>;
        push @a,$s0;
    }
    $length = 1;
    $b[0]=$a[0];
    for $i ( 1..$#a ){
        if ($a[$i] < $b[0]) {
            $b[0] = $a[$i];
        }
        elsif ($a[$i] >= $b[$length-1]) {
            $b[$length++] = $a[$i];
        }
        else {
            $pos = lower_bound($a[$i], \@b);
            $b[$pos] = $a[$i];
        }
    }
    #print "length=$length\n";
    print $n-$length,"\n";
}
#find the first more than or equal to $num;
sub lower_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1;
    $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {
            $high=$mid;
        } else {
            $low=$mid;
        }
    }
    return $high;
}

#!perl
use v5.18;
use strict;
use warnings;
# time complexity: O(Nlog(N))
# got Accepted
while(<>){
    my(@b,@a,$i,$n,$m,$s0,$pos);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$_)=split ' ',<>; push @a,$s0;
    }
    $b[0]=$a[0];
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i], \@b);
        if ($pos == @b) {push @b, $a[$i];}
        else {$b[$pos] = $a[$i];}
    }
    say $n-@b;
}

#find the first more than $num;
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $array->[$high]<=$num ? scalar @$array : $high;
}

#!perl
use v5.20;
# time complexity: O(Nlog(N))
while(<>){
    my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>; push @a,$s0;
    }
    $length = 1; $b[0]=$a[0];
    for $i ( 1..$#a ){
        if ($a[$i] < $b[0]) {$b[0] = $a[$i];}
        elsif ($a[$i] >= $b[$length-1]) {$b[$length++] = $a[$i];}
        else {$b[lower_bound($a[$i], \@b)] = $a[$i];}
    }
    say $n-$length;
}

#find the first more than $num;
sub lower_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $high;
}

#!perl
use strict;
use warnings;
while(<>){
    my(@b,@a,@len,$i,$x0,$n,$m,$s0,$pos,$result);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>;
        push @a,$s0;
    }
    $b[0]=$a[0];$len[0]=1;
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i],\@b);
        if($pos==@b){
            $len[$pos]=$len[$pos-1]+1;
            $b[$pos]=$a[$i];
        } elsif ($pos==0 && $a[$i] < $b[$pos]) {
            $b[$pos] = $a[$i];
        }
        else{
            if($a[$i]==$b[$pos]){
                $len[$pos]++;
            } else{
                $b[$pos+1]=$a[$i];
            }
        }
    }
    $result=1;
    for $i ( 0.. $#len ) { $result = $len[$i] if ($result < $len[$i]); }
    print $n-$result,"\n";
}
#find the last less or equal to $num; if all of the element
#in array are less than num. return the length of the array
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low=0;$high=$#{$array};
    while($low<=$high){
        $mid=int(($low+$high)/2);
        if($array->[$mid] > $num){
            $high=$mid-1;
        } else{
            $low=$mid+1;
        }
    }
    if($high==$#{$array} &&
       $num > $array->[$high]){
        return $high+1;
    }
    return -1!=$high ? $high : 0;
}


Boboniu gives you  $$$r$$$ red balls,  $$$g$$$ green balls,  $$$b$$$ blue balls,  $$$w$$$ white balls. He allows you to do the following operation as many times as you want:   Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations.

The first line contains one integer $$$T$$$ ($$$1\le T\le 100$$$) denoting the number of test cases. For each of the next $$$T$$$ cases, the first line contains four integers $$$r$$$, $$$g$$$, $$$b$$$ and $$$w$$$ ($$$0\le r,g,b,w\le 10^9$$$).

For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No".

NoteIn the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.In the second test case, after doing one operation, changing $$$(8,1,9,3)$$$ to $$$(7,0,8,6)$$$, one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr".A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.


This is an easy version of the problem. The only difference between an easy and a hard version is the constraints on $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$.You are given $$$4$$$ positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ with $$$a < c$$$ and $$$b < d$$$. Find any pair of numbers $$$x$$$ and $$$y$$$ that satisfies the following conditions: $$$a < x \leq c$$$, $$$b < y \leq d$$$, $$$x \cdot y$$$ is divisible by $$$a \cdot b$$$.Note that required $$$x$$$ and $$$y$$$ may not exist.

The first line of the input contains a single integer $$$t$$$ $$$(1 \leq t \leq 10$$$), the number of test cases. The descriptions of the test cases follow. The only line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$d$$$ ($$$1 \leq a < c \leq 10^5$$$, $$$1 \leq b < d \leq 10^5$$$).

For each test case print a pair of numbers $$$a < x \leq c$$$ and $$$b < y \leq d$$$ such that $$$x \cdot y$$$ is divisible by $$$a \cdot b$$$. If there are multiple answers, print any of them. If there is no such pair of numbers, then print -1 -1.

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

OUTER:
while(<>){
	$debug and print '-' x 15;
	my( $A, $B, $C, $D ) = split;

	my $cA = $A;
	my $cB = $B;

	my %h;

	while( $cA > 1 ){
		my $i = 2;
		while( 1 ){
			if( $cA % $i == 0 ){
				$cA /= $i;
				$h{ $i } ++;
				last;
				}
			$i ++;
			}
		}

	while( $cB > 1 ){
		my $i = 2;
		while( 1 ){
			if( $cB % $i == 0 ){
				$cB /= $i;
				$h{ $i } ++;
				last;
				}
			$i ++;
			}
		}

	$debug and print 'cAB:', join ', ', map { "$_ -> $h{ $_ }" } sort keys %h;

	for my $i ( reverse $A + 1 .. $C ){
		my $ii = $i;
		my %g;
		for my $j ( sort keys %h ){
			my $k = 0;
			while( $i % $j == 0 ){
				$i /= $j;
				$k ++;
				}
			$g{ $j } = $h{ $j } - $k if $h{ $j } - $k > 0;
			}

		my $x = 1;
		$x *= $_ ** $g{ $_ } for sort keys %g;

		$debug and print "x[$x]";

		my $times = int $B / $x;

		if( $times * $x + $x <= $D ){
			print $ii, " ", $times * $x + $x;
			next OUTER;
			}
		}

	print "-1 -1";
	}


Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is that each of them uses at most two distinct letters. Andrew decided to send an article to the magazine, but as he hasn't written any article, he just decided to take a random one from magazine 26Char. However, before sending it to the magazine 2Char, he needs to adapt the text to the format of the journal. To do so, he removes some words from the chosen article, in such a way that the remaining text can be written using no more than two distinct letters.Since the payment depends from the number of non-space characters in the article, Andrew wants to keep the words with the maximum total length.

The first line of the input contains number n (1 ≤ n ≤ 100) — the number of words in the article chosen by Andrew. Following are n lines, each of them contains one word. All the words consist only of small English letters and their total length doesn't exceed 1000. The words are not guaranteed to be distinct, in this case you are allowed to use a word in the article as many times as it appears in the input.

Print a single integer — the maximum possible total length of words in Andrew's article.

NoteIn the first sample the optimal way to choose words is {'abb', 'aaa', 'bbb'}.In the second sample the word 'cdecdecdecdecdecde' consists of three distinct letters, and thus cannot be used in the article. The optimal answer is {'a', 'a', 'aaaa'}.

#!/usr/bin/perl
# your code here
$/ = $\;
$s = <>;

print -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];


Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products.There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that:  the cashier needs 5 seconds to scan one item;  after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers.

The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has.

Print a single integer — the minimum number of seconds Vasya needs to get to the cashier.

NoteIn the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.

use strict ;
use warnings ;

my $noCounters = <STDIN>;
my $noPeople = <STDIN>;

my $min = 0;

for(my $i=0;$i<$noCounters;$i++){
    my $Items = <STDIN>;
    my @noItems = split(/ /,$Items);
    my $sum = 0;
    for(my $j=0;$j<=$#noItems;$j++){
        $sum += 5*$noItems[$j];
    }
    $sum += 15*($#noItems+1);
    #print "$sum";
    if($i==0){
        $min=$sum;
    }else{
        if($sum<=$min){
            $min=$sum;
        }
    }
}

print "$min" ;


There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0, y0).Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location.

The first line contains three integers n, x0 и y0 (1 ≤ n ≤ 1000,  - 104 ≤ x0, y0 ≤ 104) — the number of stormtroopers on the battle field and the coordinates of your gun. Next n lines contain two integers each xi, yi ( - 104 ≤ xi, yi ≤ 104) — the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point.

Print a single integer — the minimum number of shots Han Solo needs to destroy all the stormtroopers.

NoteExplanation to the first and second samples from the statement, respectively:


You are given a sequence of integers of length $$$n$$$ and integer number $$$k$$$. You should print any integer number $$$x$$$ in the range of $$$[1; 10^9]$$$ (i.e. $$$1 \le x \le 10^9$$$) such that exactly $$$k$$$ elements of given sequence are less than or equal to $$$x$$$.Note that the sequence can contain equal elements.If there is no such $$$x$$$, print "-1" (without quotes).

The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k \le n$$$). The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sequence itself.

Print any integer number $$$x$$$ from range $$$[1; 10^9]$$$ such that exactly $$$k$$$ elements of given sequence is less or equal to $$$x$$$. If there is no such $$$x$$$, print "-1" (without quotes).

NoteIn the first example $$$5$$$ is also a valid answer because the elements with indices $$$[1, 3, 4, 6]$$$ is less than or equal to $$$5$$$ and obviously less than or equal to $$$6$$$.In the second example you cannot choose any number that only $$$2$$$ elements of the given sequence will be less than or equal to this number because $$$3$$$ elements of the given sequence will be also less than or equal to this number.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $k ) = split;

	my %h;

	map { $h{ $_ } ++ } split ' ', <>;

	if( $k == 0 ){
		print $h{ 1 } ? -1 : 1;
		next;
		}

	my $sum = 0;

	my $x = 0;

	for my $i ( sort { $a <=> $b } keys %h ){
		$sum += $h{ $i };

		if( $sum == $k ){
			$x = $i;
			}
		}

	print $x || -1;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $k ) = split;

	my %h;

	map { $h{ $_ } ++ } split ' ', <>;

	my $sum = 0;

	my $x = 0;

	for my $i ( sort { $a <=> $b } keys %h ){
		$sum += $h{ $i };

		if( $sum == $k ){
			$x = $i;
			}
		}

	print $x || -1;
	}


Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons:   Tetrahedron. Tetrahedron has 4 triangular faces.  Cube. Cube has 6 square faces.  Octahedron. Octahedron has 8 triangular faces.  Dodecahedron. Dodecahedron has 12 pentagonal faces.  Icosahedron. Icosahedron has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below:  Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of polyhedrons in Anton's collection. Each of the following n lines of the input contains a string si — the name of the i-th polyhedron in Anton's collection. The string can look like this:   "Tetrahedron" (without quotes), if the i-th polyhedron in Anton's collection is a tetrahedron.  "Cube" (without quotes), if the i-th polyhedron in Anton's collection is a cube.  "Octahedron" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron.  "Dodecahedron" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron.  "Icosahedron" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.

Output one number — the total number of faces in all the polyhedrons in Anton's collection.

4
Icosahedron
Cube
Tetrahedron
Dodecahedron

NoteIn the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.

use 5.016;

my %sides = (
  Tetrahedron  => 4,
  Cube         => 6,
  Octahedron   => 8,
  Dodecahedron => 12,
  Icosahedron  => 20,
);

my $n = <>;
my $s = 0;
for (1..$n) {
  chomp (my $shape = <>);
  $s += $sides{$shape};
}
say $s;

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

# essential
my @tokens = ();

my %poly = (
    Tetrahedron => 4,
    Cube        => 6,
    Octahedron  => 8,
    Dodecahedron=> 12,
    Icosahedron => 20,
);

my $answer = 0;

my $n = read_token();

for ( 1..$n ) {
    $answer += $poly{read_token()};
}

say $answer;

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

$n = <STDIN>;
$ans = 0;
for($i=0;$i<$n;$i++)
{
	$str = <STDIN>;
	chomp($str);
	if($str =~ /Tetrahedron/)
	{
		$ans += 4;
	}
	elsif($str =~ /Cube/)
	{
		$ans += 6;
	}
	elsif($str =~ /Octahedron/)
	{
		$ans += 8;
	}
	elsif($str =~ /Dodecahedron/)
	{
		$ans += 12;
	}
	else
	{
		$ans += 20;
	}
}
print("$ans\n");

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my %h;
map { /(\w+).*?(\d+)/; $h{ $1 } = $2 } <DATA>;

while(<>){

	my $sum = 0;

	for (1 .. $_){
		$_ = <>, chomp;
		$sum += $h{ $_ };
		}

	print $sum;
	}

__DATA__
    Tetrahedron. Tetrahedron has 4 triangular faces.
    Cube. Cube has 6 square faces.
    Octahedron. Octahedron has 8 triangular faces.
    Dodecahedron. Dodecahedron has 12 pentagonal faces.
    Icosahedron. Icosahedron has 20 triangular faces.


Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1, x2, ..., xk (k > 1) is such maximum element xj, that the following inequality holds: .The lucky number of the sequence of distinct positive integers x1, x2, ..., xk (k > 1) is the number that is equal to the bitwise excluding OR of the maximum element of the sequence and the second maximum element of the sequence.You've got a sequence of distinct positive integers s1, s2, ..., sn (n > 1). Let's denote sequence sl, sl + 1, ..., sr as s[l..r] (1 ≤ l < r ≤ n). Your task is to find the maximum number among all lucky numbers of sequences s[l..r].Note that as all numbers in sequence s are distinct, all the given definitions make sence.

The first line contains integer n (1 < n ≤ 105). The second line contains n distinct integers s1, s2, ..., sn (1 ≤ si ≤ 109).

Print a single integer — the maximum lucky number among all lucky numbers of sequences s[l..r].

NoteFor the first sample you can choose s[4..5] = {4, 3} and its lucky number is (4 xor 3) = 7. You can also choose s[1..2].For the second sample you must choose s[2..5] = {8, 3, 5, 7}.


Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.Mahmoud knows that the i-th word can be sent with cost ai. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message?The cost of sending the message is the sum of the costs of sending every word in it.

The first line of input contains integers n, k and m (1 ≤ k ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively. The second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct. The third line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) where ai is the cost of sending the i-th word. The next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 ≤ x ≤ n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group. The next line contains m space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words.

The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.

5 4 4
i loser am the second
100 1 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

5 4 4
i loser am the second
100 20 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

NoteIn the first sample, Mahmoud should replace the word "second" with the word "loser" because it has less cost so the cost will be 100+1+5+1=107.In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $k, $m ) = split;

	my @dict = split ' ', <>;

	my @w = split ' ', <>;

	my @groups = map ~~<>, 1 .. $k;

	my @orig = split ' ', <>;

	for( @groups ){
		my( undef, @set ) = split;

		my $min = ( sort { $a <=> $b } map { $w[ $_ - 1 ] } @set )[ 0 ];

		map { $w[ $_ - 1 ] = $min } @set;
		}

	my $i = 0;

	my %h = map { $_ => $i ++ } @dict;

	print 0 + eval join ' + ', map { $w[ $h{ $_ } ] } @orig;
	}


Let's call some square matrix with integer values in its cells palindromic if it doesn't change after the order of rows is reversed and it doesn't change after the order of columns is reversed.For example, the following matrices are palindromic:  The following matrices are not palindromic because they change after the order of rows is reversed:  The following matrices are not palindromic because they change after the order of columns is reversed:  You are given $$$n^2$$$ integers. Put them into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print "NO".

The first line contains one integer $$$n$$$ ($$$1 \le n \le 20$$$). The second line contains $$$n^2$$$ integers $$$a_1, a_2, \dots, a_{n^2}$$$ ($$$1 \le a_i \le 1000$$$) — the numbers to put into a matrix of $$$n$$$ rows and $$$n$$$ columns.

If it is possible to put all of the $$$n^2$$$ numbers into a matrix of $$$n$$$ rows and $$$n$$$ columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print "YES". Then print $$$n$$$ lines with $$$n$$$ space-separated numbers — the resulting matrix. If it's impossible to construct any matrix, then print "NO". You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

NoteNote that there exist multiple answers for the first two examples.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while( my $n = <>){

	my $blue_area = ( $n - $n % 2 ) ** 2;

	$_ = join ' ', sort split ' ', <>;

	my @BLUE;
	my @YELLOW;

	s/\b(\d+)(\s+\1){3}\b/ '' x push @BLUE  , $1 /ge;
	s/\b(\d+)(\s+\1){1}\b/ '' x push @YELLOW, $1 /ge;

	my @GREEN = split;

	push @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;

	if( $blue_area != @BLUE * 4 or
		$n % 2 and 1 != @GREEN ){
		print "NO";
		next;
		}

	my @MATRIX;

	$n % 2 and @MATRIX = [ grep { defined } ( reverse @YELLOW[ 0 .. $n / 2 - 1 ] ), @GREEN, @YELLOW[ 0 .. $n / 2 - 1 ] ];

	while( @BLUE ){
		my @half = splice @BLUE, 0, $n / 2, ();

		my $row = [ grep { defined } @half, pop @YELLOW, reverse @half ];
		@MATRIX = ( $row, @MATRIX, $row );
		}

	print "YES";
	print "@{ $_ }" for @MATRIX;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while( my $n = <>){

	my $blue_area = ( $n - $n % 2 ) ** 2;

	my %count;

	map { $count{ $_ } ++ } split ' ', <>;

	my @BLUE;
	my @YELLOW;
	my @GREEN;

	for my $key ( keys %count ){
		push @BLUE, ( $key ) x ( $count{ $key } / 4 );
		push @YELLOW, $key if $count{ $key } % 4 >= 2;
		push @GREEN, $key if $count{ $key } % 2 == 1;
		}

	push @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;

	if( $blue_area != @BLUE * 4 or
		$n % 2 and 1 != @GREEN ){
		print "NO";
		next;
		}

	my @MATRIX;

	while( @BLUE ){
		push @MATRIX, [ splice @BLUE, 0, $n / 2, () ];
		}

	for( @MATRIX ){
		push @{ $_ }, grep { defined } pop @YELLOW, reverse @{ $_ };
		}

	@GREEN and push @MATRIX, [ @YELLOW, @GREEN, reverse @YELLOW ];

	push @MATRIX, reverse @MATRIX[ 0 .. ( $n >> 1 ) - 1 ];

	print "YES";
	print "@{ $_ }" for @MATRIX;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	$debug and print '-' x 20;

	chomp;
	my $n = $_;

	my %h;

	map { $h{ $_ } ++ } split ' ', <>;

	my @odd_cross;

	if( $n % 2 == 1 ){

		for my $key ( sort keys %h ){
			if( $h{ $key } % 2 == 1 ){
				$h{ $key } -= 1;
				push @odd_cross, $key;
				last;
				}
			}

		my $require = $n - 1;

		for my $key ( sort keys %h ){
			if( $require > 0 and $h{ $key } % 4 == 2 ){
				$h{ $key } -= 2;
				push @odd_cross, $key;
				$require --;
				}
			}

		for my $key ( sort keys %h ){
			while( $require > 0 and $h{ $key } % 4 == 0 and $h{ $key } > 0 ){
				$h{ $key } -= 4;
				push @odd_cross, ( $key ) x 2;
				$require -= 2;
				}
			}
		}

	$debug and print "odd_cross:[@odd_cross]";

	my $sum = eval join '+', map { $h{ $_ } } keys %h;
	my $not_tetra = grep { $h{ $_ } % 4 != 0 } keys %h;

	$debug and print "n:[$n], sum:[$sum], not_tetra:[$not_tetra]";

	if( $not_tetra or $sum != ( $n - ( $n % 2 ? 1 : 0 ) ) ** 2 ){
		print "NO";
		next;
		}

	my @fill = map { ( $_ ) x ( $h{ $_ } / 4 ) } sort keys %h;

	$debug and print "fill:[@fill]";

	my @palindrome;

	@odd_cross and @palindrome = [ ( reverse @odd_cross[ 0 .. $n / 2 ] ), @odd_cross[ 1 .. $n / 2 ] ];

	while( @fill ){
		my @line = splice @fill, 0, $n / 2, ();

		my $unshift_push = [ @line, @odd_cross ? pop @odd_cross : (), reverse @line ];
		unshift @palindrome, $unshift_push;
		push @palindrome, $unshift_push;
		}

	print "YES";
	print "@{ $_ }" for @palindrome;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while( my $n = <>){

	my $blue_area = ( $n - $n % 2 ) ** 2;

	my %count;

	map { $count{ $_ } ++ } split ' ', <>;

	my @BLUE;
	my @YELLOW;
	my @GREEN;

	for my $key ( keys %count ){
		push @BLUE, ( $key ) x ( $count{ $key } / 4 );
		push @YELLOW, $key if $count{ $key } % 4 == 2;
		push @GREEN, $key if $count{ $key } % 2 == 1;
		}

	push @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;

	if( $blue_area != @BLUE * 4 or
		$n % 2 and 1 != @GREEN ){
		print "NO";
		next;
		}

	my @MATRIX;

	while( @BLUE ){
		push @MATRIX, [ splice @BLUE, 0, $n / 2, () ];
		}

	for( @MATRIX ){
		push @{ $_ }, grep { defined } pop @YELLOW, reverse @{ $_ };
		}

	@GREEN and push @MATRIX, [ @YELLOW, @GREEN, reverse @YELLOW ];

	push @MATRIX, reverse @MATRIX[ 0 .. ( $n >> 1 ) - 1 ];

	print "YES";
	print "@{ $_ }" for @MATRIX;
	}


Codeforces user' handle color depends on his rating — it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance.Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it.Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same?

The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the number of participants Anton has outscored in this contest . The next n lines describe participants results: the i-th of them consists of a participant handle namei and two integers beforei and afteri ( - 4000 ≤ beforei, afteri ≤ 4000) — participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct.

Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise.

3
Burunduk1 2526 2537
BudAlNik 2084 2214
subscriber 2833 2749

3
Applejack 2400 2400
Fluttershy 2390 2431
Pinkie_Pie -2500 -2450

NoteIn the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest.In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest.


Ksenia has an array $$$a$$$ consisting of $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$. In one operation she can do the following:   choose three distinct indices $$$i$$$, $$$j$$$, $$$k$$$, and then  change all of $$$a_i, a_j, a_k$$$ to $$$a_i \oplus a_j \oplus a_k$$$ simultaneously, where $$$\oplus$$$ denotes the bitwise XOR operation. She wants to make all $$$a_i$$$ equal in at most $$$n$$$ operations, or to determine that it is impossible to do so. She wouldn't ask for your help, but please, help her!

The first line contains one integer $$$n$$$ ($$$3 \leq n \leq 10^5$$$) — the length of $$$a$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — elements of $$$a$$$.

Print YES or NO in the first line depending on whether it is possible to make all elements equal in at most $$$n$$$ operations. If it is possible, print an integer $$$m$$$ ($$$0 \leq m \leq n$$$), which denotes the number of operations you do. In each of the next $$$m$$$ lines, print three distinct integers $$$i, j, k$$$, representing one operation.  If there are many such operation sequences possible, print any. Note that you do not have to minimize the number of operations.

NoteIn the first example, the array becomes $$$[4 \oplus 1 \oplus 7, 2, 4 \oplus 1 \oplus 7, 4 \oplus 1 \oplus 7, 2] = [2, 2, 2, 2, 2]$$$.


You have $$$n$$$ rectangular wooden blocks, which are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th block is $$$1$$$ unit high and $$$\lceil \frac{i}{2} \rceil$$$ units long.Here, $$$\lceil \frac{x}{2} \rceil$$$ denotes the result of division of $$$x$$$ by $$$2$$$, rounded up. For example, $$$\lceil \frac{4}{2} \rceil = 2$$$ and $$$\lceil \frac{5}{2} \rceil = \lceil 2.5 \rceil = 3$$$.For example, if $$$n=5$$$, then the blocks have the following sizes: $$$1 \times 1$$$, $$$1 \times 1$$$, $$$1 \times 2$$$, $$$1 \times 2$$$, $$$1 \times 3$$$.  The available blocks for $$$n=5$$$ Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks.  One of the ways to create $$$3 \times 3$$$ square using blocks $$$1$$$ through $$$5$$$

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^9$$$) — the number of blocks.

For each test case, print one integer — the maximum possible side length of a square you can create.

NoteIn the first test case, you can create a $$$1 \times 1$$$ square using only one of the blocks.In the second test case, one of the possible ways to create a $$$3 \times 3$$$ square is shown in the statement. It is impossible to create a $$$4 \times 4$$$ or larger square, so the answer is $$$3$$$.

#!/usr/bin/perl

use POSIX qw/ceil/;
my $ntests = <>;
while (<>) {
  chomp(my $n = $_);
  print ceil($n/2), "\n";
}


You are given a special jigsaw puzzle consisting of $$$n\cdot m$$$ identical pieces. Every piece has three tabs and one blank, as pictured below.  The jigsaw puzzle is considered solved if the following conditions hold:  The pieces are arranged into a grid with $$$n$$$ rows and $$$m$$$ columns.  For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.

The test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^5$$$).

For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower).

NoteFor the first test case, this is an example solution:   For the second test case, we can show that no solution exists.For the third test case, this is an example solution:

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

use Carp;

# essential
my @tokens = ();

sub ceil {
    my $n = shift;
    my $in = int($n);
    return $n if $in == $n;
    return $in + 1;
}

sub say_all {
    my @args = @_;
    say join ' ', @args;
}

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

sub ref_ref_scalar {
    my $ref = shift;
    return 1 if ref($ref) eq 'SCALAR';
}

sub toggle {
    my $ref = shift;
    croak "$ref does not reference to scalar" if !ref_ref_scalar($ref);

    $$ref = !$$ref;
}

sub odd {
    my $num = shift;
    return $num % 2 == 1;
}

sub even {
    my $num = shift;
    return $num % 2 == 0;
}

sub sum_of_digits {
    my $n = shift;
    my @numbers = split q{}, $n;

    my $sum = 0;

    for (@numbers) {
        $sum += $_;
    }

    return $sum;
}

# solution

my $n = read_line;

for (1..$n) {
    my ($a, $b) = split q{ }, read_line;
    if ($a == 1 || $b == 1) {
        say 'YES';
        next;
    }
    if ($a == 2 && $b == 2) {
        say 'YES';
        next;
    }
    say 'NO';
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $m ) = split;

	print $n == 2 && $m == 2 || $n == 1 || $m == 1 ? "YES" : "NO";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $m ) = split;

	print $n == 2 && $n == 2 || $n == 1 || $m == 1 ? "YES" : "NO";
	}


Once Bob needed to find the second order statistics of a sequence of integer numbers. Lets choose each number from the sequence exactly once and sort them. The value on the second position is the second order statistics of the given sequence. In other words it is the smallest element strictly greater than the minimum. Help Bob solve this problem.

The first input line contains integer n (1 ≤ n ≤ 100) — amount of numbers in the sequence. The second line contains n space-separated integer numbers — elements of the sequence. These numbers don't exceed 100 in absolute value.

If the given sequence has the second order statistics, output this order statistics, otherwise output NO.

my $n = <STDIN>;
chomp $n;

my $int_list = <STDIN>;
chomp $int_list;

my @array = split(" ",$int_list);

my $min = $array[0];

my $sec = "NO";

for my $elem (@array){
    if ($elem < $min){
        $sec = $min;
        $min = $elem;
    }
    elsif (($elem > $min) && (($sec eq "NO") ||($sec > $elem))){
        $sec = $elem;
    }
}

print $sec;

#!/usr/bin/perl

use strict;
use warnings;
<>;
my $str = <>;
chomp($str);
my @arr = split(/ /, $str);
my $min = $arr[0];
my $mmin;
for my $i (@arr) {
    if ($i < $min) {
        $mmin = $min;
        $min = $i;
    } elsif ($i > $min) {
        if ( (! defined $mmin) || ($mmin > $i) ) {
             $mmin = $i;
        }
    }
}
if (!defined $mmin) {
    print "NO";
} else {
    print $mmin;
}

$n = <>;
@a = split(' ', <>);
$m = 123;
foreach(@a){
	$m = $_ if $_ < $m;
}
foreach(@a){
	print $_ and exit
	if $_ > $m;
}
print "NO";

$n = <>;
@a = split(' ', <>);
unshift @a, $m = 123;
foreach(@a){
	$m = $_ if $_ < $m;
}
foreach(@a){
	print $_ and exit
	if $_ > $m;
}
print "NO";


There are n cows playing poker at a table. For the current betting phase, each player's status is either "ALLIN", "IN", or "FOLDED", and does not change throughout the phase. To increase the suspense, a player whose current status is not "FOLDED" may show his/her hand to the table. However, so as not to affect any betting decisions, he/she may only do so if all other players have a status of either "ALLIN" or "FOLDED". The player's own status may be either "ALLIN" or "IN".Find the number of cows that can currently show their hands without affecting any betting decisions.

The first line contains a single integer, n (2 ≤ n ≤ 2·105). The second line contains n characters, each either "A", "I", or "F". The i-th character is "A" if the i-th player's status is "ALLIN", "I" if the i-th player's status is "IN", or "F" if the i-th player's status is "FOLDED".

The first line should contain a single integer denoting the number of players that can currently show their hands.

NoteIn the first sample, cows 1, 4, 5, and 6 can show their hands. In the second sample, only cow 3 can show her hand.


Vasya's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation.We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value .Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears.In the problem, fraction  denotes division in real numbers, that is, no rounding is performed.

The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing. The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing. It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.

Print the number of "integer" gears with the maximum ratio among all "integer" gears.

NoteIn the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.

chomp($n=<STDIN>);
@pedal = split /\s+/, <STDIN>;
chomp($m=<STDIN>);
@wheel = split /\s+/, <STDIN>;;

@ratios = (0) x 2000;

$count = 0;
$largestr = -1;
foreach $p (@pedal) {
	foreach $w (@wheel) {
		if($w % $p == 0) {
			$r = $w / $p;
			$largestr = $r if($r > $largestr);
#			print "wheel $w pedal $p has ratio $r\n";
			$ratios[$r] += 1;
#			print "This is combination #".$ratios[$r]."\n";
		}
	}
}
print $ratios[$largestr]."\n";

chomp($n=<STDIN>);
@pedal = split /\s+/, <STDIN>;
chomp($m=<STDIN>);
@wheel = split /\s+/, <STDIN>;;

%ratios = { };
$count = 0;
$largestr = -1;
foreach $p (@pedal) {
	foreach $w (@wheel) {
		if($w % $p == 0) {
			$r = $w / $p;
			$largestr = $r if($r > $largestr);
#			print "wheel $w pedal $p has ratio $r\n";
			defined $ratios{$r} ? $ratios{$r} += 1 : $ratios{$r} = 1;
		}
	}
}
print $ratios{$largestr}."\n";


DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter c DZY knows its value wc. For each special string s = s1s2... s|s| (|s| is the length of the string) he represents its value with a function f(s), where Now DZY has a string s. He wants to insert k lowercase letters into this string in order to get the largest possible value of the resulting string. Can you help him calculate the largest possible value he could get?

The first line contains a single string s (1 ≤ |s| ≤ 103). The second line contains a single integer k (0 ≤ k ≤ 103). The third line contains twenty-six integers from wa to wz. Each such number is non-negative and doesn't exceed 1000.

Print a single integer — the largest possible value of the resulting string DZY could get.

NoteIn the test sample DZY can obtain "abcbbc", value = 1·1 + 2·2 + 3·2 + 4·2 + 5·2 + 6·2 = 41.

#!/usr/bin/env perl
#===============================================================================
#         STDIN: dzy_string.pl
#       AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com
# ORGANIZATION: Hunan University
#      CREATED: 05/26/2015 09:44:35 AM
#===============================================================================

use strict;
use warnings;
use utf8;
use 5.012;

#open STDIN, "in.txt";
chomp(my $str = <STDIN>);
my @st = split //, $str;
chomp(my $cnt = <STDIN>);
chomp($_ = <STDIN>);
my @alph = split;
my %value;
while (my ($inde, $val) = each @alph){
    $value{chr($inde + ord('a'))} = $val;
}
my $sum = 0;
while (my ($inde, $val) = each @st){
    $sum += ($inde + 1) * $value{$val};
}
my @sor_alph = sort {$b <=> $a} @alph;
my $len = @st;
while ($cnt) {
    $sum += $sor_alph[0] * ($len + $cnt);
    $cnt--;
}
print $sum . "\n";