chomp($s = <>);
chomp($k = <>);
($i, $mark) = (0, 1);
$len = length $s;
$mark = 0 if $len % $k != 0;
while ($mark == 1 && $i < $len) {
	$su = substr($s, $i, $len / $k);
	$mark = 0 unless ($su eq reverse $su);
	$i += $len / $k;
}
print qw(NO YES)[$mark == 1];

use integer;
while(<>){
	chomp;
	$n = <>;
	$m = (length) / $n;
	$re = '.' x $m;
	$rq = qr/$re/;
	$i = $j = 0;
	while(/$rq/g){
		$i ++;
		$j += $& eq reverse $&;
		}
	print qw(NO YES)[$i == $n && $j == $n && $m * $n == length]
	}

#!/usr/bin/perl
use v5.10;

chomp($line = <>);
$len = length $line;
@a = split //, $line;
chomp($k = <>);
$len%$k>0 and say "NO" and exit;
$l = $len / $k;
for ($b=0,$e=$l; $b<$len; $b=$e,$e+=$l) {
	for ($i=$b,$j=$e-1; $i<$j; ++$i,--$j) {
		$a[$i] ne $a[$j] and say "NO" and exit;
	}
}
say "YES";

chomp($s = <>);
chomp($k = <>);
$i = 0;
$mark = 1;
$len = length $s;
$mark = 0 if $len % $k != 0;
while ($mark == 1 && $i < $len)
{
	$su = substr($s, $i, $len / $k);
	$mark = 0 unless ($su eq reverse $su);
	$i += $len / $k;
}
$mark == 1 ? print "YES" : print "NO";
print "\n";

chomp($s = <>);
chomp($k = <>);
$i = 0;
$mark = 1;
$len = length $s;
$mark = 0 if $len % $k != 0;
while ($mark == 1 && $i < length $s)
{
	$su = substr($s, 0, (length $s) / $k);
	$mark = 0 unless ($su eq reverse $su);
	$i += length $s / $k;
}
$mark == 1 ? print "YES" : print "NO";
print "\n";

chomp($s = <>);
chomp($k = <>);
$i = 0;
$mark = 1;
while ($i < length $s)
{
	$su = substr($s, 0, (length $s) / $k);
	$mark = 0 unless ($su eq reverse $su);
	$i += length $s / $k;
}
$mark == 1 ? print "YES" : print "NO";
print "\n";


use strict;
use warnings;

my ($n,$m)=split / /,<>;
chomp($n,$m);
my @d=split / /,<>;
chomp(@d);
my @sums;
my $ans=0;
while($m--)
{
  my ($s,$e)=split / /,<>;
  chomp($s,$e);
  push(@sums,sum($s,$e,\@d));
}

for my $val (@sums)
{
  $ans=max_($ans,$ans+$val);
}

print $ans . "\n";

sub sum{
  my $si=shift;
  my $ei=shift;
  my $arr=shift;
  my $aa=0;
  for(my $i=$si-1;$i<$ei;$i++)
  {
    $aa+=$arr->[$i];
  }
  return $aa;
}

sub min_{
	return $_[0] > $_[1] ? $_[1] : $_[0];
}

sub max_{
	return $_[1] > $_[0] ? $_[1] : $_[0];
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){

	my( $n, $m ) = split;

	@_ = split ' ', <>;

	print 0 + eval join '+', grep !/-/, map {
			0 + eval join '+', map "($_)",
			@_[ eval join '..', map $_ - 1, split ' ', <> ]
		} 1 .. $m;

	}


chomp,
(substr $_, substr $_, 0, (length) / 2,
(substr $_, 0, (length) / 2) =~ y/bdpq/dbqp/r),
( (length) % 2 and (substr $_, (length) / 2, 1) =~ /[bdpq]/ and $_ = 'a' ),
print qw(NIE TAK)[ !/[^ovwxAHIMOT-Ybdpq]/ * ($_ eq reverse) ]
for <>

chomp, print qw(NIE TAK)[ !/[^ovwxAHIMOT-Y]/ * ($_ eq reverse) ] for <>

chomp,
(substr $_, substr $_, 0, (length) / 2,
(substr $_, 0, (length) / 2) =~ y/bdpq/dbqp/r),
print qw(NIE TAK)[ !/[^ovwxAHIMOT-Ybdpq]/ * ($_ eq reverse) ] for <>

chomp,
(substr $_, substr $_, 0, (length) / 2,
(substr $_, 0, (length) / 2) =~ y/bdpq/dbqp/r),
( (length) % 2 and (substr $_, 0, (length) / 2 + 1) =~ /[bdpq]/ and $_ = 'a' ),
print qw(NIE TAK)[ !/[^ovwxAHIMOT-Ybdpq]/ * ($_ eq reverse) ]
for <>


#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';

my $last = 'x';
my $number = 0;

my $n = <>;
foreach (1..$n)
{
    my $piece = <>;
    chomp $piece;
    if ($last eq 'x')
    {
        $number++;
    }
    elsif ($last eq substr($piece, 0, 1))
    {
        $number++;
    }
    $last = substr($piece, -1);
}

print $number;

#!/usr/bin/perl

use 5.012;
use warnings;

chomp(my $n = <>);
my $last;
my $groups = 1;
while ($n--) {
	use integer;
	chomp(my $m = <>);
	my $first = $m / 10;
	$groups++ if defined $last && $last eq $first;
	$last = $m % 10;
}
print $groups;

#!/usr/bin/perl

$n = <> + 0;
$groups = 1;
$previous = <>;

for ($i = 0; $i < ($n-1); $i++) {
    $magnet = <>;
    if ($magnet ne $previous) {
        $groups++;
    }

    $previous = $magnet;
}

print "$groups\n";

chomp($n=<>);
my $pre = "";
while (<>) {
	chomp;
	++$cnt if $_ != $pre;
	$pre = $_;
}
print "$cnt\n";

#!/usr/bin/perl
# your code goes here
use v5.10;

chomp($t = <>);
my $ans = 0;
while (<>) {
	chomp;
	$_ ne $s and $ans++;
	$s = $_;
}
say $ans;

#!/usr/bin/perl -w

use strict;

my $n = <>;
my $cur = '00';
my $g = 0;

for (1..$n) {
    my $s = <>;
    if ($s ne $cur) {
        $g++;
        $cur = $s;
    }
}

print $g, "\n";

#!/usr/bin/perl

#  Copyright 2013 Anton Leontiev <bunder@t-25.ru>
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following disclaimer
#    in the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of the  nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

use v5.12;
use warnings;
use English qw(-no_match_vars);

$OUTPUT_FIELD_SEPARATOR = " ";

my $num = <>;
my $last = '';
my $islands;

map { $islands++, $last = $_ if $last ne $_ } <>;

say $islands;

#!/usr/bin/env perl

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

# essential
my @tokens = ();

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

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;
}

# solve

my $n = read_token;

my $latest_token = read_token;

my $answer = 1;

for (1..$n-1) {
    my $token = read_token;
    if ($latest_token ne $token) {
        $answer++;
        $latest_token = $token;
    }
}

say $answer;

$n = <>;
while ($n--) {
  $now = <>;
  $groups += $now != $last;
  $last = $now;
}
print "$groups\n";

#!/usr/bin/perl

use strict;
chomp(my $tc = <>);

my $ans = 1;
chomp(my $now = <>);
my $temp = $now;

for( my $i = 1; $i<$tc ;$i++){
	chomp($now = <>);
	if($now != $temp){
		$ans++;
	}
	$temp = $now
}
print $ans;

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

$t=<>;
while (<>){
	$t!=$_ and $c++;
	$t=$_;
	}
print $c+1;

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

my $num = <>;
chomp $num;
my $count = 1;
my $magnet = <>;
for (2..$num) {
    $next = <>;
    if (!($magnet eq $next)) {
        $count++;
        $magnet = $next;
    }
}
print $count;

chomp($n=<>);
my $pre = "";
while (<>) {
	chomp;
	$cnt++ if $_ == $pre;
	$pre = $_;
}
print "$cnt\n";

#!/usr/bin/perl

use strict;
chomp(my $tc = <>);

my $ans = 1;
chomp(my $now = <>);
my @arr = split("", $now);
my $temp = $arr[1];

for( my $i = 1; $i<$tc ;$i++){
	chomp(my $now = <>);
	@arr = split("", $now);
	if($arr[0] == $temp){
		$ans++;
	}
	else{
		$temp = $arr[1];
	}
}
print $ans;

#!/usr/bin/perl

use strict;
chomp(my $tc = <>);

my $ans = 1;
chomp(my $now = <>);
my $temp = $now;

for( my $i = 1; $i<$tc ;$i++){
	chomp(my $now = <>);
	if($now == $temp){
		$ans++;
	}
	else{
		$temp = $now;
	}
}
print $ans;


#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

<>;
$_=<>;
$_[20]=0;
s/-?\d+/$_[$&]++/eg;
for (1..10){
	$ats += $_[-$_]*$_[$_]
	}
$ats += $_[0]*($_[0]-1)/2;
print $ats;

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

<>;
$_=<>;
s/-?\d+/$_[$&+10]++/eg;
for (1..10){
	$ats += $_[10-$_]*$_[10+$_]
	}
$ats += $_[10]*($_[10]-1)/2;
print $ats;

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}


$_ = <>; # readline
s/(^|\*)\K/ $pre_match = $`, ($post_match = $') =~ s!(?=\*)|$! $hash{eval "$pre_match($`)$'"} = "$pre_match($`)$'", '' !ge, '' /ge;

use bigint;
print eval $hash{ (sort {$b <=> $a} keys %hash)[0] }


#!/usr/bin/env perl
package Codeforces::Task::_;
use v5.10;
use strict;
use warnings;

sub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}
sub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}
sub out{foreach my$c(@_){print"$c ";}print("\n");}

sub solve {
    my $s = <>;
    $s =~ s/( )+/ /g;
    $s =~ s/ ?([\.,!\?]) ?/$1 /g;
    say $s;
}

sub main {
    my $t = 1;
#    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main(@ARGV));

#!/usr/bin/perl
use strict;
use warnings;
use utf8;

my $str = <>;

# Clear text
$str =~ s/\s+/ /g;
$str =~ s/\s*([?,.!])\s*/$1 /g;

print $str;

#!/usr/bin/perl
use strict;
use warnings;

my $str = <>;

# Clear text
$str =~ s/\s+/ /g;
$str =~ s/\s([.,!?:;])/$1/g;
$str =~ s/([.,!?:;])/$1 /g;

print $str, "\n";

#!/usr/bin/perl -w
# Почему не работает-то?
while (<STDIN>)
{
    s/\s+/ /g;
    s/\s*([?,.!])\s*/$1 /g;

    print $_, "\n";
}

#!/usr/bin/perl
use strict;
use warnings;

my $str = <>;

# Clear text
$str =~ s/\s+/ /g;
$str =~ s/\s([.,!?:;])/$1/g;
$str =~ s/([.,!?:;])/$1 /g;
$str =~ s/\s+/ /g;

print $str, "\n";

#!/usr/bin/perl
use strict;
use warnings;
use utf8;

my $str = <>;

# Clear text
$str =~ s/\s+/ /g;
$str =~ s/\s*([?,.!])\s*/$1 /g;

print $str, "\n";


#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

sub min {
    my ($a, $b) = @_;
    if ($a < $b) {
		return $a;
	} else {
        return $b;
    }
}
sub max {
    my ($a, $b) = @_;
    if ($a > $b) {
        return $a;
    } else {
        return $b;
    }
}

sub solve {
    my ($h, $m) = map(int, split(" ", <>));
    print 24 * 60 - $m - 60 * $h, "\n";
}

sub main {
    my $t = 1;
    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main(@ARGV));

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $h, $min ) = split;

	print 24 * 60 - ( $h * 60 + $min );
	}


use List::Util qw(first max maxstr min minstr reduce shuffle sum);

chomp (my $n = <>);

my @names = (q/junk/);
while (<>) {
    chomp;
    push @names, $_;
}
my @order = split /\s+/, pop @names;

my $rsp = q/YES/;

my @h;
my $lH = '-';
foreach (@order) {
    @h = sort {$a cmp $b} split /\s+/, $names[$_];
    if ($lH lt $h[0]) {
        $lH = $h[0]; next;
    } elsif ($lH lt $h[1]) {
        $lH = $h[1]; next;
    } else {
        $rsp = q/NO/; last;
    }
}

printf "$rsp\n";


$len = <STDIN>;
$hashMap;

for my $val (1 .. $len) {
	$input = <STDIN>;
	$input =~ s/\s//g;

	if ($hashMap->{$input}) {
		print $input.$hashMap->{$input}."\n";
		++$hashMap->{$input};
	} else {
		$hashMap->{$input} = 1;
		print "OK\n";
	}
}

#!/usr/bin/perl

main();

sub main {
    my $n = int <>;
    my %hash = ();
    for(1..$n) {
        my $name = <>;
        $name =~ s/[\W]//;
        my $add = "";
        if (exists $hash{$name}) {
            $add = $hash{$name};
            print("$name$add\n");
            $hash{$name} = $add + 1;
        } else {
            print("OK\n");
            $hash{$name} = 1;
        }
    }
}

chomp($count = <STDIN>);
for ($i = 0; $i < $count; ++$i)
{
    chomp($newName = <STDIN>);
    push @names, $newName;
}
foreach (@names)
{
    if ( ! exists $logins{$_} )
    {
        $logins{$_}++;
        print "OK\n";
    }
    else
    {
        print "$_$logins{$_}\n";
        $logins{$_}++;
    }
}

my %hash;
for my $i(1..int<>) {
    chomp($_ = <>);
    if (not exists $hash{$_}) {
        print "OK\n";
        $hash{$_} = 1;
        next;
    }
    print $_ . $hash{$_} . "\n";
    $hash{$_}++;
}

#!/usr/bin/perl
use v5.10;

chomp($n = <>);
foreach (1 .. $n) {
	chomp($line = <>);
	defined $tb{$line} and say $line,$tb{$line} or say "OK";
	++$tb{$line};
}

my $n = <>;
my %names;

for (1..$n) {
    my $name = <>;
    chomp $name;
    if (exists $names{$name}) {
        print $name, $names{$name}++, "\n";
    } else {
        $names{$name} = 1;
        print "OK\n";
    }
}

#!perl

for (1..<STDIN>) {
	chomp ($name = <STDIN>);
	if (!defined($names{$name})) {
		print "OK\n";
		$names{$name} = 0;
	} else {
		print $name, ++$names{$name}, "\n";
	}
}

#!usr/bin/perl
use strict;
use warnings;

my $n=<STDIN>;
my %list;my $p=0;
my @output;
for my $i(0..$n-1){
    my $name=<STDIN>;
    chomp($name);
    if(!defined($list{$name})){
	$list{$name}=$name;
	$output[$i]="OK\n";
    }
    else{
	$p=$list{$name}=~s/[a-z]+/0/r+1;
	$list{$name}=$name.$p;
	$output[$i]=$list{$name}."\n";
    }
}
print @output;

#!/usr/bin/perl

$n=<STDIN>;
$n=int($n);

%db=();

for($i=0;$i<$n;$i++) {
	$name=<STDIN>;
	chomp($name);
	if($db{$name}) {
		$name2=$name . $db{$name};
		$db{$name}=$db{$name}+1;
		print "$name2\n";
	} else {
		$db{$name}=1;
		print "OK\n";
	}
}

chomp($n = <STDIN>);

%dic = {};
@userlist = [];
pop @userlist;

for( $i = 0; $i < $n; $i++){
    chomp($name = <STDIN>);
    if (exists $dic{$name}) {
        #$dic{$name} = $dic{$name} + 1;
        $dic{$name}++;
        $string = $name.$dic{$name};
        push @userlist , ($string);
    }
    else
    {
        $dic{$name} = 0;
        push @userlist, "OK";
    }
}

foreach $name(@userlist){
	print "$name\n";
}

#!perl

for (1..<STDIN>) {
	chomp ($name = <STDIN>);
	if (!defined($names{$name})) {
		print "OK";
		$names{$name} = 0;
	} else {
		print $name, ++$names{$name}, "\n";
	}
}

#!perl

for (1..<STDIN>) {
	chomp ($name = <STDIN>);
	if (!defined($names{$name})) {
		print "OK";
		$names{$name} = 0;
	} else {
		print $name, ++$names{$name};
	}
}

#!usr/bin/perl
use strict;
use warnings;

my $n=<STDIN>;
my %list;
my @output;
for my $i(0..$n-1){
    my $name=<STDIN>;
    chomp($name);
    if(!defined($list{$name})){
	$list{$name}=$name;
	$output[$i]="OK\n";
    }
    else{
	my $p=$name=~s/\w+/0/r;
	$list{$name}=$name.($p+1);
	$output[$i]=$list{$name}."\n";
    }
}
print @output;


use strict;

use List::Util qw(first max maxstr min minstr reduce shuffle sum);

my ($n, $m) = split / /, <>;
my $b;
my @bs;
$#bs = $n-1;

my @i = split / /, <>;
$b = shift @i;
for(@i)
{
    $bs[$_] = 1;
}
my @gs;
$#gs = $m-1;
@i = split / /, <>;

my $g = shift @i;
for(@i)
{
    $gs[$_] = 1;
}
if($b == 0 and $g == 0){ print "No";exit 0; }

my $nod = nod($m, $n);

my @t; $#t = $nod-1;

for (0..max ($n, $m))
{
    $t[$_ % $nod] = $bs[$_ % $n] || $gs[$_ % $m] || $t[$_ % $nod];
}
my $flag = 1;
for(@t)
{
    $flag *= $_;
}

print "Yes" if ($flag);
print "No" unless ($flag);

sub nod
 {
 return  $_[0] != 0  ?  nod ( ( $_[1] % $_[0] ), $_[0] )  :  $_[1];
 };

sub gcf {
  my ($x, $y) = @_;
  ($x, $y) = ($y, $x % $y) while $y;
  return $x;
}

sub lcm {
  return($_[0] * $_[1] / gcf($_[0], $_[1]));
}

chomp (my ($n, $m) = split /\s+/, <>);
my @boys  = (0) x $n;
my @girls = (0) x $m;
chomp (my @hb = split /\s+/, <>);
chomp (my @hg = split /\s+/, <>);

my $hcnt = 0;
$hcnt += shift @hb;
$hcnt += shift @hg;

$boys[$_]   = 1 foreach (@hb);
$girls[$_]  = 1 foreach (@hg);

my $prevhcnt;
my ($i, $j);

repeat:

$prevhcnt = $hcnt;
($i, $j) = (0, 0);
foreach my $x (0 .. &lcm($n, $m) - 1) {
  $i++;
  $j++;
  $i = ($n > $i) ? $i : 0;
  $j = ($m > $j) ? $j : 0;
  if ($boys[$i] ^ $girls[$j]) {
    $boys[$i] = 1;
    $girls[$j] = 1;
    $hcnt++;
  }
}
# printf "@boys\n@girls\n$hcnt\n$prevhcnt\n";
goto repeat if ($hcnt > $prevhcnt);

if ($hcnt == ($m + $n)) {
  printf "YES\n";
} else {
  printf "NO\n";
}

chomp (my ($n, $m) = split /\s+/, <>);
my @boys  = (0) x $n;
my @girls = (0) x $m;
chomp (my @hb = split /\s+/, <>);
chomp (my @hg = split /\s+/, <>);

my $hcnt = 0;
$hcnt += shift @hb;
$hcnt += shift @hg;

$boys[$_]   = 1 foreach (@hb);
$girls[$_]  = 1 foreach (@hg);

my $prevhcnt;

$n--;
$m--;

repeat:
$prevhcnt = $hcnt;
foreach my $i (0 .. $n) {
  foreach my $j (0 .. $m) {
    if ($boys[$i] ^ $girls[$j]) {
      $girls[$j] = 1;
      $boys[$i] = 1;
      $hcnt++;
    }
  }
}
# printf "@boys\n@girls\n$hcnt\n$prevhcnt\n";
goto repeat if ($hcnt > $prevhcnt);

if ($hcnt == ($m + $n + 2)) {
  printf "YES\n";
} else {
  printf "NO\n";
}

sub gcf {
  my ($x, $y) = @_;
  ($x, $y) = ($y, $x % $y) while $y;
  return $x;
}

sub lcm {
  return($_[0] * $_[1] / gcf($_[0], $_[1]));
}

chomp (my ($n, $m) = split /\s+/, <>);
my @boys  = (0) x $n;
my @girls = (0) x $m;
chomp (my @hb = split /\s+/, <>);
chomp (my @hg = split /\s+/, <>);

my $hcnt = 0;
$hcnt += shift @hb;
$hcnt += shift @hg;

$boys[$_]   = 1 foreach (@hb);
$girls[$_]  = 1 foreach (@hg);

my $prevhcnt;
my ($i, $j);

repeat:

$prevhcnt = $hcnt;
($i, $j) = (0, 0);
foreach my $x (0 .. &lcm($n, $m) - 1) {
  $i++;
  $j++;
  $i = ($n > $i) ? $i : 0;
  $j = ($m > $j) ? $j : 0;
  if ($boys[$i] ^ $girls[$j]) {
    $boys[$i] = 1;
    $girls[$j] = 1;
    $hcnt++;
  }
}
printf "@boys\n@girls\n$hcnt\n$prevhcnt\n";
goto repeat if ($hcnt > $prevhcnt);

if ($hcnt == ($m + $n + 2)) {
  printf "YES\n";
} else {
  printf "NO\n";
}


# author : Gabriel Hofer
use warnings;
use strict;
use constant NL => qq/\n/;

my ($n, $m) = ( <STDIN> =~ m /(\d+)\s(\d+)/ );
my @one = split qq/ /, <STDIN>;
my @two = split qq/ /, <STDIN>;
my @three = @one;
for ( my $i = 1; $i <= $#three; $i += 1 ) {
    $three [ $i ] += $three [ $i - 1 ] }

AA: for my $i ( @two ) {
    my ($left, $right, $mid);
    $left = 0;
    $right = $#three;
    AB: while ( $left < $right ) {
        $mid = int ( ( $left + $right ) / 2 );
        if ( $i < $three [ $mid ] ) { $right = $mid }
        elsif ( $i > $three [ $mid ] ) { $left = $mid + 1 }
        else { last AB }
    }
    $mid = int ( ( $left + $right ) / 2 );
    print ( $mid + 1, qq/ / );
    if ( $mid == 0 ) { print qq/$i/ }
    else { print ( $i - $three [ $mid - 1 ] ) }
    print NL;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $house = 1;
	my $accum = 0;

	for my $i ( split ' ', <> ){

		while( @_ and $_[ 0 ] + $accum < $i ){
			$house ++;
			$accum += $_[ 0 ];
			shift @_;
			}

		print $house . ' ' . ( $i - $accum );
		}

	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	@_ = split ' ', <>;

	my %h;

	map $h{ $_ } ++, @_;

	for my $key ( sort { $b <=> $a } keys %h ){
		if( $key % $x == 0 and exists $h{ $key / $x } ){
			while( $h{ $key } > 0 and $h{ $key / $x } > 0 ){
				$h{ $key } --;
				$h{ $key / $x } --;
				}
			}
		}

	print 0 + eval join '+', values %h;
	}


#!/usr/bin/perl
use strict;
use warnings;

sub min{
    return $_[0] < $_[1]? $_[0]:$_[1];
}
my $n;my $m;
($n, $m) = split(' ', <>);

my @arr = split(' ', <>);
@arr = sort {$a <=> $b}@arr;

my $res = 0;
for(my $i = 0; $i < min($n,$m); $i++){
    if($arr[$i] < 0){$res += -$arr[$i];}
    else {last;}
}
print $res;

#!/usr/bin/perl
use strict;
use warnings;

sub min{
    return $_[0] < $_[1]? $_[0]:$_[1];
}
my $n;my $m;
($n, $m) = split(' ', <>);

my @arr = split(' ', <>);
@arr = sort @arr;

my $res = 0;
for(my $i = 0; $i < min($n,$m); $i++){
    if($arr[$i] < 0){$res += -$arr[$i];}
    else {last;}
}
print $res;


use strict;

my $try = <>;
while($try-- > 0)
{
    my $val = <>;
    if($val < 31){
        print "NO\n";
    } elsif($val == 36) {
        print "YES\n";
        printf("15 10 6 %d\n", $val-31);
    } elsif($val == 40 || $val == 44) {
        print "YES\n";
        printf("15 10 14 %d\n", $val-39);
    } else {
        print "YES\n";
        printf("6 10 14 %d\n", $val-30);
    }
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

while(<>){
	@_ = map ~~<>, 1 .. $_;
	my $start = 0;

	for( @_ ){
		my( $s, $d ) = split;
		$start ++;

		my $n = ( $start - $s ) / $d;
		$debug and print '  ', $n;
		$start = $s + $d * ( $n =~ s/-.*/0/r =~ s/(\d+)\..*/ $1 + 1 /er );
		$debug and print '       ', $start;
		}

	print $start;
	}


#!/usr/local/bin/perl

my @ttt = split(/\s+/, <>);
my @arr = split(/\s+/, <>);

$odd = 0;
$even = 0;

foreach $a (@arr) {

    $c = int($a) % 2;

    if ($c == 1) {
        $odd = $odd + 1;
    } else {
        $even = $even + 1;
    }

}

if ($odd <= $even)
{
    print($odd)
}
else
{
    print($even + int(($odd - $even) / 3 + 1e-12))
}

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

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

my @splits = split / /, $line;

my $odd = 0;
my $even = 0;
for my $split (@splits) {
    if (int($split) % 2 == 0) {
        $even += 1;
    } else {
        $odd += 1;
    }
}

my $pair;

if ($even < $odd) {
    $pair = $even;
} else {
    $pair = $odd;
}

$odd -= $pair;
$even -= $pair;

my $ans = $pair + int($odd / 3);

print $ans . "\n";

$_ = <STDIN>; my ($n) = split;
$_ = <STDIN>; my @A = split;

$odd = 0;
$even = 0;
for($i=0; $i < $n ; $i=$i+1){

	if( $A[$i] %2 == 0){
	    $even = $even+1;
	}else{
	    $odd = $odd+1;
	}
}

$mn = $odd;
if ($even < $mn) {
    $mn = $even;
}

$df = $odd - $mn;
$kk = $df / 3;
$qq = int($kk);
$ans = $mn + $qq;

print "$ans\n"

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

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

my @splits = split / /, $line;

my $odd = 0;
my $even = 0;
for my $split (@splits) {
    if (int($split) % 2 == 0) {
        $even += 1;
    } else {
        $odd += 1;
    }
}

if ($even < $odd) {
    print $even . "\n";
} else {
    print $odd . "\n";
}

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

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

my @splits = split /,/, $line;

my $odd = 0;
my $even = 0;
for my $split (@splits) {
    if (int($split) % 2 == 0) {
        $even += 1;
    } else {
        $odd += 1;
    }
}

if ($even < $odd) {
    print $even . "\n";
} else {
    print $odd . "\n";
}


print qw(YES NO)[$_ = <> xor /AB.*BA/ || /BA.*AB/ ]

$_ = <>;
if (/AB.*BA/ || /BA.*AB/) {
	print 'YES';
} else {
	print 'NO';
}

$_=<>;
print qw/NO YES/[ 0+(/AB.*BA/ or /BA.*AB/) ]

$_=<>;
if(/AB.*BA/ or /BA.*AB/) {
  print "YES";
} else {
  print "NO";
}

$_ = <>;
my $ans = "NO\n";
$ans = "YES" if (/AB.*BA/ || /BA.*AB/);
print $ans;

$a = <>;
if ($a =~ m[AB.*BA]||$a =~ m[BA.*AB])
{
    print "YES";
}
else
{
    print "NO";
}

$_=<>;
print qw/NO YES/[ /AB.*BA/ or /BA.*AB/ ]


my $n = <>;

# my @m = split " ", scalar <>;

my @A = map {my @tmp = split " "; shift (@tmp) == @tmp or die "???"; [@tmp];} <>;

# (scalar @A) == (scalar @m) or die ((scalar @A) . ' ' . (scalar @m));

my @B = map{
    my @tmp = ();
    $tmp[100] = undef;
    @tmp[@$_] = (1) x @$_;
    \@tmp;
    } @A;

foreach my $curr (@B){
    my $good = 2;
    foreach my $chk (@A){
        # continue if $_ == $curr;
        $good-- unless grep {not $_} @$curr[@$chk];
        }
    print $good > 0 ? "YES\n" : "NO\n";
    }
1;

$n = <>;
@A = map {@tmp = split " "; shift (@tmp) == @tmp or die "???"; [@tmp];} <>;

@B = map{
    @tmp = ();
    $tmp[100] = undef;
    @tmp[@$_] = (1) x @$_;
    \@tmp;
    } @A;

foreach $curr (@B){
    $good = 2;
    foreach $chk (@A){
        $good-- unless grep {not $_} @$curr[@$chk];
        }
    print $good > 0 ? "YES\n" : "NO\n";
    }


use feature ':all';

while(<>){
	@_ = ('+');
	/0/ and say "@_" and next;
	for $i (1 .. $_){
		@new = ();
		for $line (@_){
			@syms = split //, $line;
			$doubled = '';
			for $k (@syms){ $doubled .= $k . $k };
			$mixed = '';
			for $k (@syms){ $mixed .= $k . ($k eq '+' ? '*' : '+') };
			push @new, $doubled, $mixed;
			}
		@_ = @new;
		}
	say join "\n", @_;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	@_ = split ' ', <>;

	my $sum = 0;

	$sum += $_ for @_;

	my $cnt = 0;

	my $i = 0;

	while( @_ ){
		if( 0 < int $t / $sum ){
			$cnt += @_ * ( int $t / $sum );
			$t -= $sum * ( int $t / $sum );
			}
		my $this = shift @_;
		if( $this > $t ){
			$sum -= $this;
			next;
			}
		else{
			$t -= $this;
			$cnt ++;
			push @_, $this;
			next;
			}
		}

	print $cnt;
	}


#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

#    int ans = 0, cur = -1e5;
#    for (char c : $s) {
#        if (c == 'A') {ans = max(ans, cur);cur = 0;}else {++cur;}
#    }
#    answer(max(ans,cur));

sub solve {
    my ($ans, $n) = (0, int(<>));
    my @s = split("", <>);
    my $p = $n - 1;
    for (my $i = $n - 1; $i > -1; $i--) {
        if ($s[$i] eq "A") {
            if ($ans < $p - $i)
                { $ans = $p - $i; }
            $p = $i - 1;
		}
	}
    print "$ans\n";
}

sub main {
    my $t = 1;
    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main());

#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

sub solve {
    my ($ans, $n) = (0, int(<>));
    my @s = split("", <>);
    my $p = $n - 1;
    for (my $i = $n - 1; $i > -1; $i--) {
        if ($s[$i] eq "A") {
            if ($ans < $p - $i)
                { $ans = $p - $i; }
            $p = $i - 1;
		}
	}
    print "$ans\n";
}

sub main {
    my $t = 1;
    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main());

use strict;
use warnings;

use Data::Dumper;

sub main {

    my $in= *STDIN;
    my $q= <$in>; chomp($q);

    while($q > 0) {
        $q--;
        my $s= <$in>;
        my $z= <$in>;
        my @l= split('', $z);

        my $i= -1;
        for my $stud (@l) {
            last if($stud eq 'A');
            if($i==-1) {
                $i= 0;
            } else {
                $i++;
            }
        }

        $i++;

        if($i == -1) {
            print "0\n";
            next;
        }

        my $c= 0;
        my $max= 0;
        for(my $j=$i; $j < $#l; $j++) {
            if($l[$j] eq 'A') {
                $c= 0;
            } else {
                $c++;
                if($max < $c) {
                    $max = $c;
                }
            }

        }

        print "$max\n";

    }

}

main() unless caller();

#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

sub solve {
    my ($ans, $n) = (0, int(<>));
    my @s = split("", <>);
    my $f = 0;
    for (my $i = $n - 1; $i > -1; $i--) {
		if ($s[$i] eq 'A') {
            $f = 1;
		    last;
		}
        $ans++;
	}
	$ans *= $f;
    print "$ans\n";
}

sub main {
    my $t = 1;
    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main());

#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

sub solve {
    my ($ans, $n) = (0, int(<>));
    my @s = split("", <>);
    for (my $i = $n - 1; $i > -1; $i--) {
		if ($s[$i] eq 'A') {
		    last;
		}
        $ans++;
	}
    print "$ans\n";
}

sub main {
    my $t = 1;
    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main());

#!/usr/bin/env perl
package Codeforces::Task::A;
use v5.10;
use strict;
use warnings;

#    int ans = 0, cur = -1e5;
#    for (char c : $s) {
#        if (c == 'A') {ans = max(ans, cur);cur = 0;}else {++cur;}
#    }
#    answer(max(ans,cur));

sub solve {
    my ($ans, $n) = (0, int(<>));
    my @s = split("", <>);
    push(@s, "A");
    my $p = $n - 1;
    for (my $i = $n - 1; $i > -1; $i--) {
        if ($s[$i] eq "A") {
            if ($ans < $p - $i)
                { $ans = $p - $i; }
            $p = $i;
		}
	}
    print "$ans\n";
}

sub main {
    my $t = 1;
    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main());


#!/bin/perl
use strict;
use warnings;

my ($n, $k) = map int, split '\D', <>;
my $p = 0;
for my $i (1..$n) {
  my ($f, $t) = map int, split '\D', <>;
  my $np = ($k < $t)? $f - $t + $k: $f;
  if ( ($i == 1) || ($p < $np) ) {
    $p = $np;
  }
}
print $p;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	$_ .= <>;

	my $max_len = 0;

	m/
		(.*) .*
		\n
		.* \1
		(?{
			$max_len < length $1 and $max_len = length $1;
			})
		(*FAIL)
		/x;

	print +( length ) - 2 - $max_len * 2;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

while(<>){

	my %h;

	for my $i (2 .. $_){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ v }{ $v } ++;
		$h{ $v }{ v }{ $u } ++;
		}

	my $i = 0;
	map { $i ++; $h{ $i }{ c } = $_ } split ' ', <>;

	my @sorted = grep { keys %{ $h{ $_ }{ v } } == 1 }
		sort { keys %{ $h{ $a }{ v } } <=> keys %{ $h{ $b }{ v } } } keys %h;
	$debug and print "@sorted";

	my $i;

	while( @sorted ){
		$i = shift @sorted;
		my( $key ) = keys %{ $h{ $i }{ v } };
		if( $h{ $i }{ c } != $h{ $key }{ c } ){
			next;
			}

		delete $h{ $key }{ v }{ $i };
		keys %{ $h{ $key }{ v } } == 1 and unshift @sorted, $key;
		delete $h{ $i }{ v };
		}

	$debug and print "keys: ", map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;
	my @g = grep $_ > 1, map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;
	$debug and print "[@sorted]";

	if( @g > 1 ){
		print "NO";
		}
	elsif( @g == 0 ){
		print "YES";
		print $i;
		}
	else{
		print "YES";
		my $j;
		for my $k (keys %h){
			keys %{ $h{ $k }{ v } } > 1 and $j = $k;
			}
		print $j;
		}
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

while(<>){

	my %h;

	for my $i (2 .. $_){
		my( $u, $v ) = split ' ', <>;

		$h{ $u }{ v }{ $v } ++;
		$h{ $v }{ v }{ $u } ++;
		}

	my $i = 0;
	map { $i ++; $h{ $i }{ c } = $_ } split ' ', <>;

	my @sorted = grep { keys %{ $h{ $_ }{ v } } == 1 }
		sort { keys %{ $h{ $a }{ v } } <=> keys %{ $h{ $b }{ v } } } keys %h;
	$debug and print "@sorted";

	while( @sorted ){
		my $i = shift @sorted;
		my( $key ) = keys %{ $h{ $i }{ v } };
		if( $h{ $i }{ c } != $h{ $key }{ c } ){
			next;
			}

		delete $h{ $key }{ v }{ $i };
		keys %{ $h{ $key }{ v } } == 1 and unshift @sorted, $key;
		delete $h{ $i }{ v };
		}

	$debug and print "keys: ", map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;
	my @g = grep $_ > 1, map { ~~ keys %{ $h{ $_ }{ v } } } keys %h;
	$debug and print "[@sorted]";

	if( @g > 1 ){
		print "NO";
		}
	elsif( @g == 0 ){
		print "YES";
		print 1;
		}
	else{
		print "YES";
		my $j;
		for my $k (keys %h){
			keys %{ $h{ $k }{ v } } > 1 and $j = $k;
			}
		print $j;
		}
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = split ' ', <>;

	my $knife = 0;
	my $alive = 0;

	for my $i ( reverse 0 .. @_ - 1 ){
		$knife --;
		$knife < 0 and $alive ++;
		$_[ $i ] > $knife and $knife = $_[ $i ];
		}

	print $alive;
	}


#!/usr/bin/perl

while (<>) {
    my @nums = split(/ /, <>);
    my @odds;
    my $sum = 0;

    $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;

    if (@odds & 1) {
        @odds = sort {$a <=> $b} @odds;
        shift @odds;
    }
    $sum += $_ for @odds;

    print "$sum";
}

#!/usr/bin/perl

while (<>) {
    my @nums = split(/ /, <>);
    my @odds;
    my $sum = 0;

    $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;

    if (@odds & 1) {
        @odds = sort {$a <=> $b} @odds;
        $sum += $_ for @odds[1..$#odds];
    } else {
        $sum += $_ for @odds;
    }

    print "$sum";
}

#!/usr/bin/perl

while (<>) {
    my @nums = split(/ /, <>);
    my @odds;
    my $sum = 0;

    $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;

    if (@odds & 1) {
        @odds = sort {$a <=> $b} @odds;
        $sum += $_ for @odds[-$#odds..-1];
    } else {
        $sum += $_ for @odds;
    }

    print "$sum";
}

#!/usr/bin/perl

my $n = <>;
my $line = <>;
chomp($line);

my @nums = split(/ /, $line);
my @odds;
my $sum = 0;

$_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;

if (@odds % 2 == 0) {
    $sum += $_ for @odds;
} else {
    @odds = sort {$a <=> $b} @odds;
    shift @odds;
    $sum += $_ for @odds;
}

print "$sum";

#!/usr/bin/perl

my $n = <>;
my $line = <>;
chomp($line);

my @nums = split(/ /, $line);

my @odds;
my $sum = 0;
for (@nums) {
    if ($_ % 2 == 0) {
        $sum += $_;
    } else {
        push @odds, $_;
    }
}

if (@odds % 2 == 0) {
    for (@odds) {
        $sum += $_;
    }
} else {
    @odds = sort {$a <=> $b} @odds;
    shift @odds;
    for (@odds) {
        $sum += $_;
    }
}

print "$sum";

#!/usr/bin/perl

my $n = <>;
my $line = <>;
chomp($line);

my @nums = split(/ /, $line);

my (@evens, @odds);

for (@nums) {
    if ($_ % 2 == 0) {
        push @evens, $_;
    } else {
        push @odds, $_;
    }
}

my $sum = 0;

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

if (@odds % 2 == 0) {
    for (@odds) {
        $sum += $_;
    }
} else {
    @odds = reverse sort @odds;
    for (@odds[-$#odds..-1]) {
        $sum += $_;
    }
}

print "$sum";

#!/usr/bin/perl

while (<>) {
    my @nums = split(/ /, <>);
    my @odds;
    my $sum = 0;

    $_ & 1 ? push @odds, $_ : ($sum += $_) for @nums;

    if (@odds & 1) {
        @odds = sort @odds;
        $sum += $_ for @odds[1..$#odds];
    } else {
        $sum += $_ for @odds;
    }

    print "$sum";
}

#!/usr/bin/perl

my $n = <>;
my $line = <>;
chomp($line);

my @nums = split(/ /, $line);

my (@evens, @odds);

for (@nums) {
    if ($_ % 2 == 0) {
        push @evens, $_;
    } else {
        push @odds, $_;
    }
}

my $sum = 0;

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

if (@odds % 2 == 0) {
    for (@odds) {
        $sum += $_;
    }
} else {
    @odds = sort @odds;
    for (@odds[-$#odds..-1]) {
        $sum += $_;
    }
}

print "$sum";


#!/usr/bin/perl -w
use strict;
use warnings;
use List::Util qw{sum min};

my $n = int <>;
my @d = map int, split /\D/, <>;
my ($s, $t) = map int, split /\D/, <>;
if ($s > $t) {
    my $tmp = $t;
    $t = $s;
    $s = $tmp;
}
my $s1 = 0;
for ($s-1 .. $t-2) {
    $s1 += $d[$_];
}
my $s2 = (sum @d) - $s1;
print min ($s1, $s2);

#!usr/bin/perl
#use warnings;
#use strict;
#use autodie;
#use diagnostics;
use utf8;
use 5.010;
use 5.012;
use 5.014;
my($n,$ss,@arr,$s,$t,$count,$max);
chomp($n=<STDIN>);
chomp($ss=<STDIN>);
@arr=split / /,$ss;
chomp($ss=<STDIN>);
($s,$t)=split / /,$ss;
$count=$s;
$max=999999999;
$ss=0;
while($count ne $t)
{
    $ss+=$arr[$count-1];
    $count=0 if $count eq $n;
    $count++;
}
$max=$ss if $max > $ss;
($ss,$count)=(0,$s);
while($count ne $t)
{
    $count=$n+1 if $count eq 1;
    $count--;
    $ss+=$arr[$count-1];
}
$max=$ss if $max > $ss;
say $max;

my $num = <>;
chomp $num;
my @dist = grep /\w/, split ' ', <>;
my ($s, $t) = sort {$a <=> $b} split ' ', <>;

my $r = 0;
$r += $dist[$_-1] for $s..$t-1;
my $total;
$total += $dist[$_-1] for 1..$num;
my $l = $total - $r;
print $r < $l ? $r : $l;


#!/perl -w

use strict ;

chomp ( my $n = <STDIN> );
my($gmax,$gmin) = (2*10**9,-2*10**9) ;
while($n-- ){
    my($symbol,$guess,$answer) = split /\s+/s , <STDIN> ;
    if($answer eq 'N'){
        my $temp = $symbol ;
        $symbol = '<'  if $temp eq '>=' ;
        $symbol = '<=' if $temp eq '>'  ;
        $symbol = '>'  if $temp eq '<=' ;
        $symbol = '>=' if $temp eq '<' ;
    }

    $gmin = ($guess > $gmin)? $guess : $gmin  if $symbol eq '>=' ;
    $gmin = ($guess + 1 > $gmin)? $guess +1 : $gmin  if $symbol eq '>' ;
    $gmax = ($guess < $gmax)? $guess : $gmax  if $symbol eq '<=' ;
    $gmax = ($guess - 1 < $gmax)? $guess -1 : $gmax  if $symbol eq '<' ;
}

if($gmax < $gmin){
    print "Impossible\n" ;
}else{
    print "$gmin\n";
}


#!/usr/bin/perl
# your code goes here
use v5.10;

my %st;
chomp($line = <>);
$st{$_}++ foreach(split ", ", substr $line, 1, length($line)-2);
say scalar(keys %st);

#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';

my $s = <>;
chomp $s;

$s = substr(substr($s, 0, -1), 1);

my %set;

foreach my $letter (split(', ', $s))
{
    $set{$letter} = 1;
}

my $result = keys %set;

print $result

#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'uninitialized';

my $line = <>;

$line =~ s/[^a-z]//g;

my %seen;
foreach my $c (split(//, $line))
{
   $seen{$c} = 1
}

print scalar(keys %seen), "\n"

my $s = <>;
$s = substr ($s, 1, (length $s) - 3);
%k = ();
grep { $k{$_} = $_ } split ", ", $s;
print scalar (keys %k);

#! usr/bin/perl

use warnings;
use strict;

my $line = readline();

my $count = {};

while ($line =~ m/(\w),/g) {
    $count->{$1} = 1;
#   print $1 . "\n";
}
if ($line =~ m/(\w)\}/g) {
    $count->{$1} = 1;
}

my @keys = keys %$count;

my $size = @keys;

print $size . "\n";

my $str = <>;
my $reg = "{([^{}]*)}\n";
$str =~ s/$reg/$1/;
my %hash = map { $_, 1 } split ", ", $str;
print scalar keys %hash;

my $str = <>;
$str =~ s/{([^{}]*)}\n/$1/;
my %hash = map { $_, 1 } split ", ", $str;
print scalar keys %hash;


#!/usr/bin/perl

use warnings;
use strict;

<>;

while(<>){
	$_ = <>;

	s/(.)\K \1//g;

	print m/ / ? s/ //r : s/^/a/r;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $r ) = split;
	my( $x1, @x ) = split ' ', <>;

	my %y;
	$y{ $x1 } = $r;
	my @y;
	push @y, $r;

	my $sqr = ( $r * 2 ) ** 2;

	for my $x ( @x ){

		my $Y = 0;

		for my $xkey ( keys %y ){

			my $B = $sqr - ( $x - $xkey ) ** 2;
			next if $B < 0;

			my $y = $y{ $xkey } + sqrt( $B );

			if( $y >= $Y ){
				$Y = $y;
				}

			}

		$Y ||= $r;
		$y{ $x } = $Y;
		push @y, $Y;
		}

	print "@y";
	}

my $n, $r;
my $f = <STDIN>;
chomp ( $f );
($n, $r) = split m/ /, $f;
$f = <STDIN>;
chomp ( $f );
my @disks = split m/ /, $f;

my @placed = ();
my $elem;
my $found;
my @yy = ();
my $val;
my $height;

my $j = 0;
my $max;
while ( @disks > 0 ) {

    $elem = shift @disks;
    $found = 0;

    $max = 0;
    for ( my $i = 0; $i < @placed; $i += 1 ) {

	if ( abs ( $elem - $placed[ $i ][ 0 ] ) <= (2 * $r) ) { # if there's a collision
	    $found = 1;

	    $height = sqrt (abs( ((2 * $r) ** 2) - (abs ( $elem - $placed[ $i ][ 0 ] ) ** 2)));
	    $val = $placed[ $i ][ 1 ] + $height;
	    if ( $val > $max ) {
		$max = $val;}
	}
    }

    if ( $found == 1 ) {
	unshift @placed, [ $elem, $max, $j ];
    }

    if ( $found == 0 ) {	# if there's no collision
	unshift @placed, [ $elem, $r, $j ];
    }

    @placed = sort { ${ $a }[ 1 ] <=> ${ $b }[ 1 ] } @placed;
    @placed = reverse @placed;

    $j += 1;
}

my @answer = sort { ${ $a }[ 2 ] <=> ${ $b }[ 2 ] } @placed;

for ( my $i = 0; $i < @answer; $i += 1 ) {
    print $answer[ $i ][ 1 ];
    print " ";}
print "\n";

use 5.016;
use strict;
use warnings;

my (undef, $r) = split ' ', <>;
my @x = split ' ', <>;

my %y;
my @y;

for my $x (@x) {
  my $max = $r;
  while (my ($x2, $y2s) = each %y) {
    for my $y2 (@$y2s) {
      if (abs($x2 - $x) <= 2 * $r) {
        # collision

        # dx^2 + dy^2 = (2r)^2
        my $dy = sqrt(4 * $r**2 - ($x2 - $x)**2);
        my $y = $y2 + $dy;

        $max = $y if $y > $max;
      }
    }
  }

  push @y, $max;
  push @{$y{$x}}, $max;
}

say join ' ', @y;

my $n, $r;
my $f = <STDIN>;
chomp ( $f );
($n, $r) = split m/ /, $f;
$f = <STDIN>;
chomp ( $f );
my @disks = split m/ /, $f;

my @placed = ();
my $elem;
my $found;
my @yy = ();
my $val;
my $height;

my $j = 0;
while ( @disks > 0 ) {

    $elem = shift @disks;
    $found = 0;

    for ( my $i = 0; not $found and $i < @placed; $i += 1 ) {
	if ( abs ( $elem - $placed[ $i ][ 0 ] ) <= (2 * $r) ) { # if there's a collision
	    $found = 1;

	    $height = sqrt (abs( ((2 * $r) ** 2) - (abs ( $elem - $placed[ $i ][ 0 ] ) ** 2)));
	    $val = $placed[ $i ][ 1 ] + $height;
	    unshift @placed, [ $elem, $val, $j ];
	}
    }

    if ( $found == 0 ) {	# if there's no collision
	unshift @placed, [ $elem, $r, $j ];
    }

    @placed = sort { ${ $a }[ 1 ] <=> ${ $b }[ 1 ] } @placed;
    @placed = reverse @placed;
    $j += 1;
}

my @answer = sort { ${ $a }[ 2 ] <=> ${ $b }[ 2 ] } @placed;

for ( my $i = 0; $i < @answer; $i += 1 ) {
    print $answer[ $i ][ 1 ];
    print " ";}
print "\n";

my $n, $r;
my $f = <STDIN>;
chomp ( $f );
($n, $r) = split m/ /, $f;
$f = <STDIN>;
chomp ( $f );
my @disks = split m/ /, $f;

my @placed = ();
my $elem;
my $found;
my @yy = ();
my $val;
my $height;

while ( @disks > 0 ) {

    $elem = shift @disks;
    $found = 0;

    for ( my $i = 0; not $found and $i < @placed; $i += 1 ) {
	if ( abs ( $elem - $placed[ $i ][ 0 ] ) <= (2 * $r) ) { # if there's a collision
	    $found = 1;

	    $height = sqrt (abs( ((2 * $r) ** 2) - (abs ( $elem - $placed[ $i ][ 0 ] ) ** 2)));
	    $val = $placed[ $i ][ 1 ] + $height;
	    unshift @placed, [ $elem, $val ];
	}
    }

    if ( $found == 0 ) {	# if there's no collision
	unshift @placed, [ $elem, $r ];
    }
}

my @answer = reverse @placed;

for ( my $i = 0; $i < @answer; $i += 1 ) {
    print $answer[ $i ][ 1 ];
    print " "; }
print "\n";


my ($n, $x) = split / /, <>;
my @arr = split / /, <>;
my $sum;
$sum += $_ for(@arr);
print int((abs($sum)+$x - 1) / $x);

#!/usr/bin/perl
use v5.10;
use integer;

($n, $x) = split / /, <>;
$tot+=$_ foreach (split / /, <>);
say ((abs($tot) + $x-1)/$x);

use integer;

while(<>){

	($n,$x)=split/ /;
	$_=<>;
	$sum=0;
	s/-?\d+/$sum+=$&/ge;
#	print "$sum\n";

	$sum=abs $sum;
#	print "$sum\n";
	$answer=$sum/$x + ($sum%$x? 1:0) ;

	print "$answer\n";

	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

while(<>){
	my @A = split ' ', <>;

	my @cand;
	push @cand, $A[1] - $A[0], $A[2] - $A[1], ($A[2] - $A[0]) / 2;
	$debug and print "cand:[@cand]";

	my $ok = 0;

	for my $cand ( @cand ){

		my $jump;
		my $f = 0;

		for( my $i = 0; $i < @A - 1; $i ++ ){
			my $diff = $A[ $i+1 ] - $A[ $i ];

			if( $diff != $cand ){
				if( not defined $jump ){
					$jump = $diff - $cand;
					}
				elsif( -$jump == $diff - $cand ){
					$jump *= -1;
					#OK
					}
				else{
					$f ++;
					last;
					}
				}
			}

		if( $f == 0 and defined $jump ){
			$ok = 1;
			last;
			}
		}

	print $ok ? "YES" : "NO";

	}


#!/usr/bin/perl

my $test_count = <STDIN>;
chomp $test_count;
my @final_results;

for ($i = 0; $i < $test_count; ++$i) {
  my $seq_length = <STDIN>;
  chomp($seq_length);
  my $line = <STDIN>;
  chomp($line);
  my @elements = split(/ /,$line);
  my %seen;
  my @result;
  foreach (@elements) {
    unless ($seen{$_}) {
      $seen{$_} = true;
      push(@result,$_);
    }
  }
  push(@final_results,join(' ',@result));
}

print join("\n", @final_results)."\n";


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	print eval join '+', 0, map $_ * ( $_ + 1 ) / 2, map length, split /[^$ok]/, $_;
	}


#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

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

	my( $n, $q ) = split;
	@_ = split ' ', <>;
	$debug and print "[@_]";

	my $sum = 0;
	$sum += $_ for @_;

	my $LYG = grep $_ % 2 == 0, @_;
	my $NELYG = @_ - $LYG;
	$debug and print "LYG[$LYG], NELYG:[$NELYG]";

	for my $qq ( 1 .. $q ){
		my( $t, $x ) = split ' ', <>;

		if( $t == 0 ){
			$sum += $LYG * $x;
			if( $x % 2 == 1 ){
				$LYG = 0;
				$NELYG = @_;
				}
			}
		if( $t == 1 ){
			$sum += $NELYG * $x;
			if( $x % 2 == 1 ){
				$LYG = @_;
				$NELYG = 0;
				}
			}
		print $sum;
		}
	}


#!/bin/perl -w
use strict;
use warnings;

my ($n, $m, $color) = (split / /, <>);
chomp $color;
my ($ps, $ns) = ('', '');
my @s;
for (1..$n) {
    my $rs = <>;
    chomp $rs;
    if ($rs =~ /$color/) {
	push @s, $rs;
    } elsif (@s) {
	$ns = $rs;
	last;
    } else {
	$ps = $rs;
    }
}

my %tables;
# left and right tables
for (@s) {
    if (/([^$color])$color/) {
	$tables{$1} = 1 if $1 ne '.';
    }
    if (/$color([^$color])/) {
	$tables{$1} = 1 if $1 ne '.';
    }
}

# tables in previous row
if ($ps ne '') {
    my @ps = split //, $ps;
    my @fs = split //, $s[0];
    for (0..$#ps) {
	if ($fs[$_] eq $color and $ps[$_] ne '.') {
	    $tables{$ps[$_]} = 1;
	}
    }
}
# tables in next row
if ($ns ne '') {
    my @ns = split //, $ns;
    my @ls = split //, $s[$#s];
    for (0..$#ns) {
	if ($ls[$_] eq $color and $ns[$_] ne '.') {
	    $tables{$ns[$_]} = 1;
	}
    }
}
# all tables

my $sum = keys %tables;
print "$sum\n";

#!/bin/perl -w
use strict;
use warnings;

my ($n, $m, $color) = (split / /, <>);
chomp $color;
my ($ps, $s, $ns) = ('', '', '');
for (1..$n) {
    $s = <>;
    chomp $s;
    if ($s =~ /$color/) {
	$ns = <> if $_ < $n;
	chomp $ns;
	last;
    } else {
	$ps = $s;
    }
}

my %tables;
# left table
if ($s =~ /([^$color])$color/) {
    $tables{$1} = 1 if $1 ne '.';
}
# right table
if ($s =~ /$color([^$color])/) {
    $tables{$1} = 1 if $1 ne '.';
}

# tables in previous row
if ($ps ne '') {
    my @ps = split //, $ps;
    my @s = split //, $s;
    for (0..$#ps) {
	if ($s[$_] eq $color and $ps[$_] ne '.') {
	    $tables{$ps[$_]} = 1;
	}
    }
}
# tables in next row
if ($ns ne '') {
    my @ns = split //, $ns;
    my @s = split //, $s;
    for (0..$#ns) {
	if ($s[$_] eq $color and $ns[$_] ne '.') {
	    $tables{$ns[$_]} = 1;
	}
    }
}
# all tables

my $sum = keys %tables;
print "$sum\n";

#!/bin/perl -w
use strict;
use warnings;

my ($n, $m, $color) = (split / /, <>);
chomp $color;
my ($ps, $ns) = ('', '');
my @s;
for (1..$n) {
    my $rs = <>;
    chomp $rs;
    if ($rs =~ /$color/) {
	push @s, $rs;
    } elsif (@s) {
	$ns = $rs;
	last;
    } else {
	$ps = $rs;
    }
}

my %tables;
# left and right tables
for (@s) {
    if (/([^$color])$color/) {
	$tables{$1} = 1 if $1 ne '.';
    }
    if (/$color(^$color])/) {
	$tables{$1} = 1 if $1 ne '.';
    }
}

# tables in previous row
if ($ps ne '') {
    my @ps = split //, $ps;
    my @fs = split //, $s[0];
    for (0..$#ps) {
	if ($fs[$_] eq $color and $ps[$_] ne '.') {
	    $tables{$ps[$_]} = 1;
	}
    }
}
# tables in next row
if ($ns ne '') {
    my @ns = split //, $ns;
    my @ls = split //, $s[$#s];
    for (0..$#ns) {
	if ($ls[$_] eq $color and $ns[$_] ne '.') {
	    $tables{$ns[$_]} = 1;
	}
    }
}
# all tables

my $sum = keys %tables;
print "$sum\n";


<>; print abs eval <> =~ s/\s/+/gr . 0, $/ while <>


use 5.016;
use strict;
use warnings;

<>;
my ($k, $m) = split ' ', <>;
my @x = split ' ', <>;
my @y = split ' ', <>;

my $x = $x[$k - 1];
my $y = $y[-$m];

say $x < $y ? 'YES' : 'NO';


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	my @A = split ' ', <>;
	my @B = split ' ', <>;

	$debug and print "\@A: [@A]";
	$debug and print "\@B: [@B]";

	my %h;

	my $i = 0;

	map $h{ $_ } = ++ $i, @A;

	$_ = $h{ $_ } for @A;
	$_ = $h{ $_ } for @B;

	$debug and print "\@A: [@A]";
	$debug and print "\@B: [@B]";

	my $cnt = 0;

	@B = reverse @B;

	my $min = $B[ 0 ];

	for( @B ){
		if( $_ > $min ){
			$cnt ++;
			}
		if( $_ < $min ){
			$min = $_;
			}
		}

	print $cnt;
	}


use warnings;
use strict;
chomp ( my $n = <STDIN> );
my @a = ();
my $s;
for ( 1 .. $n ) {
    $s = <STDIN>;
    chomp ( $s );
    push @a, $s;
}
$, = qq/\n/, $\ = qq/\n/;
@a = sort { $a =~ m/$b/ ? 1 : -1 } @a;
for my $i ( 1 .. $#a ) { if ( not $a[$i] =~ m/$a[$i-1]/ ) { print "NO\n"; exit; }}
print "YES";
print @a;

#!/usr/bin/perl

$input_line = <STDIN>;
chop($input_line);
$n=$input_line;
@s=();
for ($counter = 0; $counter <$n; $counter++){
	$input_line = <STDIN>;
	chop($input_line);
	$s[$counter] = $input_line;
}
$counter = $n;
while ($counter > 0){
	$i = 1;
	while ($i < $counter){
		if (length($s[$i])< length($s[$i-1])){($s[$i],$s[$i-1]) = ($s[$i-1], $s[$i])}
		$i++;
	}
	$counter--;
}
$check = 1;
for ($i = 1; $i <$n; $i++){
	if ($s[$i] !~ /${s[$i-1]}/){$check = 0}
}
if ($check == 0){print ("NO\n");}
else{
	print("YES\n");
	for ($i = 0; $i <$n; $i++){
		print ("$s[$i]\n");}
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = map ~~<>, 1 .. $_;
	chomp @_;

	@_ = sort { length $a <=> length $b } @_;

	my $f = 0;

	for my $i ( 0 .. @_ - 2 ){
		$_[ $i + 1 ] =~ $_[ $i ] or $f ++;
		}

	next if $f and print "NO";

	print for "YES", @_;
	}

use warnings;
use strict;
chomp ( my $n = <STDIN> );
my @a = ();
my $s;
for ( 1 .. $n ) {
    $s = <STDIN>;
    chomp ( $s );
    push @a, $s;
}
$, = qq/\n/, $\ = qq/\n/;
@a = sort { $a =~ m/$b/ ? 1 : -1 } @a;
for my $i ( 1 .. $#a ) { if ( not $a[$i] =~ m/$a[$i-1]/ ) { print "NO\n"; exit; }}
print @a;

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = map ~~<>, 1 .. $_;
	chomp @_;

	@_ = sort { length $a <=> length $b } @_;

	my $f = 0;

	for my $i ( 0 .. @_ - 2 ){
		$_[ $i + 1 ] =~ $_[ $i ] or $f ++;
		}

	next if $f and print "NO";

	print for @_;
	}


#!/usr/bin/perl -l
use warnings;
use strict;
chomp(my $n=<STDIN>);
my %mapp=('purple'=>'Power','green'=>'Time','blue'=>'Space','orange'=>'Soul','red'=>'Reality','yellow'=>'Mind');
for(my $i=0;$i<$n;$i++)
{
	chomp(my $s=<STDIN>);
        delete $mapp{$s};
}
my $ss=keys %mapp;
print $ss;
foreach my $value(values %mapp)
{
	print $value;
}

use warnings;
use strict;
use integer;
use constant NL => qq/\n/;
$\ = qq/\n/, $, = qq/\n/;
chomp ( my $n = <STDIN> );
my %gems = (
    'purple' => 'Power',
    'green' => 'Time',
    'blue' => 'Space',
    'orange' => 'Soul',
    'red' => 'Reality',
    'yellow' => 'Mind'
);
for my $val ( 1 .. $n ) {
    chomp ( my $str = <STDIN> );
    delete $gems { $str };
}
print scalar values %gems;
print values %gems;

#!/usr/bin/perl -l
use warnings;
use strict;
chomp(my $n=<STDIN>);
my %mapp=('purple'=>'Power','green'=>'Time','blue'=>'Space','orange'=>'Soul','red'=>'Reality','yellow'=>'Mind');
for(my $i=0;$i<$n;$i++)
{
	chomp(my $s=<STDIN>);
        delete $mapp{$s};
}
foreach my $value(values %mapp)
{
	print $value;
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	$_ = sprintf "%b", $_;

	my $ones = () = /1/g;

	print $ones == 1 ? "NO" : "YES";
	}


use strict;

$,=" ";
my ($n,$ax,$bx)=split / /,<>;
chomp($n,$ax,$bx);
$ax--;
$bx--;
$n--;
my $str=<>;
chomp($str);
my @aa=split //,$str;

if($aa[$ax]==$aa[$bx]){
	print "0 \n";
}else{
	print "1 \n";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $A, $B ) = split;
	$_ = <>, chomp;

	my $CA = substr $_, $A - 1, 1;
	my $CB = substr $_, $B - 1, 1;

	print do {
		if( abs( $CA - $CB ) == 1 ){
			1;
			}
		else{
			0;
			}
		};
	}

#!/usr/bin/perl

use strict;
use warnings;

my $tmp = <>;
$tmp =~ s/\s+$//;
my ($n, $fs, $sn) = split /\s+/, $tmp;

my $s = <>;
$s =~ s/\s+$//;

if (substr($s, $fs - 1, 1) eq substr($s, $sn - 1, 1)) {
    print "0";
} else {
    print "1";
}

use strict;
use Data::Dumper;
use warnings;

my ($n,$ax,$bx)=split / /,<>;
chomp($n,$ax,$bx);
$ax--;
$bx--;
$n--;
my $str=<>;
chomp($str);
my @aa=split //,$str;
my %b_cost;
my %a_cost;
for my $k(0..$n){
    $b_cost{$k}->[0]=$aa[$k]-$aa[$bx];
	$b_cost{$k}->[1]=abs($bx-$k);
}

if($b_cost{$ax}->[0]==0){
	print "0\n";
}
else{

for my $k (sort { $b_cost{$a}->[1] <=> $b_cost{$b}->[1]  } keys %b_cost){
if($b_cost{$k}->[0]==1){
	print $b_cost{$k}->[1] . "\n";
	last;
}
}
}

use strict;
use Data::Dumper;
use warnings;

my ($n,$ax,$bx)=split / /,<>;
chomp($n,$ax,$bx);
$ax--;
$bx--;
$n--;
my $str=<>;
chomp($str);
my @aa=split //,$str;
my %b_cost;
my %a_cost;
for my $k(0..$n){
    $b_cost{$k}->[0]=abs($aa[$k]-$aa[$bx]);
	$b_cost{$k}->[1]=abs($bx-$k);
}

if($b_cost{$ax}->[0]==0){
	print "0\n";
}
else{
for my $k (sort { $b_cost{$a}->[1] <=> $b_cost{$b}->[1]  } keys %b_cost){
if($b_cost{$k}->[0]==1){
	print $b_cost{$k}->[1] . "\n";
	last;
}
}
}

use strict;

$,=" ";
my ($n,$ax,$bx)=split / /,<>;
chomp($n,$ax,$bx);
$ax--;
$bx--;
$n--;
my $str=<>;
chomp($str);
my @aa=split //,$str;

my $b_cost=0;
my $ans=10000000000;
if($aa[$ax]==$aa[$bx])
{
	print "0\n";
}
else
{
for my $k(0..$n){
$b_cost=0;
my $a_to_vertx=0;
my $b_to_vertx=0;
 if($aa[$k]==$aa[$ax]){
 	$a_to_vertx=0;
 }else{
 	$a_to_vertx=abs($ax-$k);
 }
 if($aa[$k]==$aa[$bx]){
 	$b_to_vertx=0;
 }else{
 	$b_to_vertx=abs($bx-$k);
 }
 #print $a_to_vertx,$b_to_vertx . "\n";
    $b_cost=$a_to_vertx+$b_to_vertx;
    $ans=min($b_cost,$ans);
}
print $ans . "\n";
}

sub min{
	return $_[0]>$_[1] ? $_[1] : $_[0];
}


#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

$_=<>;
chomp;
@_=split//;
$_ eq'x'and $x++ for@_;
$y=@_-$x;
print (($x>$y?'x':'y')x($x+$y-min($x,$y)*2));

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

@_=split//,<>;
$_ eq'x'and $x++ for@_;
$y=@_-$x;
print (($x>$y?'x':'y')x($x+$y-min($x,$y)*2));

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

@_=split//,<>;chomp$_[@_-1];
$_ eq'x'and $x++ for@_;
$y=@_-$x;
print (($x>$y?'x':'y')x($x+$y-min($x,$y)*2));

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my @m = map { length s/.*?(0*)$/$1/r }
		map { eval sprintf "%0b", $_ } $w, $h;

	for( @m ){
		$_ > 13 and $_ = 13;
		}

	my $m = eval join '*', map 2 ** $_, @m;

	print $m >= $n ? "YES" : "NO";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my @m = map { length s/.*?(0*)$/$1/r }
		map { eval sprintf "%0b", $_ } $w, $h;

	for( @m ){
		$_ > 11 and $_ = 11;
		}

	my $m = eval join '*', map 2 ** $_, @m;

	print $m >= $n ? "YES" : "NO";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my $m = length join '', map { s/.*?(0*)$/$1/r }
		map { eval sprintf "%0b", $_ } $w, $h;

	if( $m < 11 ){
		$m = 2 ** $m;
		}

	print $m >= $n ? "YES" : "NO";
	}


#!/usr/bin/perl
use v5.10;
use integer;

($n, $k) = split / /, <>;
($tot, $k) = (0, 5 - $k);
$_<=$k and ++$tot foreach (split / /, <>);
say $tot/3;

my ($n, $k) = split / /, <>;
my @arr = split / /, <>;
my $count = 0;
$_ += $k for(@arr);
$count = $count + ($_ <=5 ? 1:0) for(@arr);
print int($count / 3);

my ($n, $k) = split / /, <>;
my $count = 0;
$_ <= 5 - $k && $count++ for(split / /, <>);
print int($count / 3);

my ($n, $k) = split ' ', <>;
my @people = split ' ', <>;
my @rem = map { 5 - $_ } @people;

my $ok = int((scalar grep { $_ >= $k } @rem) / 3);

print "$ok\n";

my @firstline = split (' ', <>);
my @secondline = split (' ', <>);
my @goodguys;
my $n = $firstline[0];
my $k = $firstline[1];
my $goodguys = 0;
foreach (@secondline){
    if (($k+$_) <= 5){
        $goodguys++;

}
}
print int($goodguys / 3);


# function returns true if the current
# character is the smallest in the rest
# otherwise, it returns false
# of string s
# parameters are the character and
# the hash
sub is_small {
    my $chr = shift @_;
    my $h_ref = shift @_;
    for my $i ( keys %{ $h_ref } ) {
	if ( ($i lt $chr) and ${ $h_ref }{ $i } > 0 ) {
	    return 0;}}		# return false if character is less than
    return 1;			# chr and there are still chrs in the string
}

# initialize hash
my $h;
for my $i (a..z) {
    $h{ $i } = 0; }

# read input
my $f = <STDIN>;
chomp ( $f );
# put the string into an array
my @s = split m//, $f;

# calculate frequencies for each character
# in the string and store in the hash
for (@s) {
    $h{ $_ } += 1;}

# strings described in problem statement
my @t;
my @u;

# temporary varaibles for pushing and poping and shifting
my $front;
my $back;

# main algorithm with two operations
while ( @s > 0 ) {

    # remove character from front of string
    $front = shift @s;

    # decrement value for $front character
    $h { $front } -= 1;

    # remove key - value pair if value become zero
    if ( $h { $front } == 0 ) {
	delete $h { $front }; }

    # push character to the end of @t string
    push @t, $front;

    # take advantage of short circuiting
    while ( @t > 0 and &is_small ( $t[ @t-1 ], \%h ) ) {

	$back = pop @t;
	push @u, $back;}
}

# put the remainder of @t onto @u
while ( @t > 0 ) {
    $back = pop @t;
    push @u, $back; }

# print the resuling string
print @u;
print "\n";

# function returns true if the current
# character is the smallest in the rest
# otherwise, it returns false
# of string s
# parameters are the character and
# the hash
sub is_small {
    my $chr = shift @_;
    my $h_ref = shift @_;
    for my $i ( keys %{ $h_ref } ) {
	if ( ($i lt $chr) and ${ $h_ref }{ $i } > 0 ) {
	    return 0;}}		# return false if character is less than
    return 1;			# chr and there are still chrs in the string
}

# initialize hash
my $h;
for my $i (a..z) {
    $h{ $i } = 0; }

# read input
my $f = <STDIN>;
chomp ( $f );
# put the string into an array
my @s = split m//, $f;

# calculate frequencies for each character
# in the string and store in the hash
for (@s) {
    $h{ $_ } += 1;}

# strings described in problem statement
my @t;
my @u;

# temporary varaibles for pushing and poping and shifting
my $front;
my $back;

# main algorithm with two operations
while ( @s > 0 ) {

    $front = shift @s;
    push @t, $front;
    if ( &is_small ( $front, \%h ) ) {
	$back = pop @t;
	push @u, $back;}
}

# put the remainder of @t onto @u
while ( @t > 0 ) {
    $back = pop @t;
    push @u, $back; }

# print the resuling string
print @u;
print "\n";


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $A, $B, $C ) = sort { $a <=> $b } split;

	$A ++ if $C - $A > 1;
	$C -- if $C - $A > 0;

	print 2 * ( $C - $A );
	}

my $tests = <>;
#printf("tests:$tests\n");
chomp ($tests);
foreach my $t (1..$tests) {
 my $in = <>;
 chomp($in);
 my @arr = split(" ",$in);
 my @sorted = sort { $a <=> $b } @arr;
 my $a = $sorted[0];
 my $b = $sorted[1];
 my $c = $sorted[2];
 #print "a,b,c:$a,$b,$c\n";
 if ($a != $b) {
     $a++;
     if ($b != $c) {
      $c--;
     } else {
         $c--;
         $b--;
     }
 }
 #print "a,b,c:$a,$b,$c\n";
 my $d = ($c - $b) + ($b - $a) + ($c - $a);
 print "$d\n";
}

my $tests = <>;
#printf("tests:$tests\n");
chomp ($tests);
foreach my $t (1..$tests) {
 my $in = <>;
 chomp($in);
 my @arr = split(" ",$in);
 my @sorted = sort { $a <=> $b } @arr;
 my $a = $sorted[0];
 my $b = $sorted[1];
 my $c = $sorted[2];
 #print "a,b,c:$a,$b,$c\n";
 if ($a != $b) {
     $a++;
     if ($b != $c) {
      $c--;
     } else {
         $c--;
         $b--;
     }
 } elsif ($b != $c) {
     $c--;
 }

 #print "a,b,c:$a,$b,$c\n";
 my $d = ($c - $b) + ($b - $a) + ($c - $a);
 print "$d\n";
}

my $tests = <>;
#printf("tests:$tests\n");
chomp ($tests);
foreach my $t (1..$tests) {
 my $in = <>;
 chomp($in);
 my @arr = split(" ",$in);
 my @sorted = sort { $a <=> $b } @arr;
 my $a = $sorted[0];
 my $b = $sorted[1];
 my $c = $sorted[2];
 #print "a,b,c:$a,$b,$c\n";
 if ($a != $b) {
     $a++;
     if ($b != $c) {
      $c--;
     } elsif ($a != $b) {
         $c--;
         $b--;
     }
 } elsif ($b != $c) {
     $c--;
 }

 #print "a,b,c:$a,$b,$c\n";
 my $d = ($c - $b) + ($b - $a) + ($c - $a);
 print "$d\n";
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	$debug and print "-" x 15;

	my( $n, $m ) = split;

	$_ = join '', map ~~<>, 1 .. $n;
	y/ //d;
	chomp;

	$debug and print;

	my $gap = $m - 2 + 1;

	0 while
		s/
			[1,]{2} (.{$gap})
			[1,]{2}
		/
			":," . $1 .
			",,"
		/xse;

	$debug and print;

	/1/ and print -1 and next;

	@_ = split "\n", $_;

	my @coords;

	for my $i ( 0 .. $n - 1 ){
		while( $_[ $i ] =~ /:/g ){
			push @coords, join " ", $i + 1, pos $_[ $i ];
			}
		}

	print for ~~ @coords, @coords;
	}


 my $n = <STDIN>;

  my $found = 0;

  my $level = 0;

  my $throwLevel = -1;

  my $e = "";

  foreach (1 .. $n) {

      my $line = <STDIN>;

      if ($line =~ /^\s*try/) {

          $level ++;

      } elsif ($line =~ /^\s*throw\s*\(\s*(\w+)\s*\)/) {

          $e = $1;

          $throwLevel = $level;

          # print "throw $e at level $throwLevel\n";

      } elsif ($line =~ /^\s*catch\s*\(\s*(\w+)\s*,\s*"(.+)"\s*\)/) {

          my $ce = $1;

          my $msg = $2;

          if ($ce eq $e && $level <= $throwLevel) {

              $found = 1;

              print "$msg\n";

              last;

          }

          $level --;

          if ($throwLevel > $level) {

              $throwLevel = $level;

              # print "decrease throwLevel to $throwLevel\n";

          }

      }

  }

  if (!$found) {

      print "Unhandled Exception";

  }

#!/usr/local/bin/perl
$n              = <>;
$openBlocks     = 0;
$needlessBlocks =
  0;    #это блоки, открытые после эксепшена TODO
$wasThrow = 0;
$eType;
for ( 1 .. $n ) {
    my $line = <>;

    if ( $line =~ /\s*throw\s*\(\s*(\w+)\s*\)/ ) {
        $wasThrow = 1;
        $eType    = $1;
        maybeError();
        next;
    }
    if ( $line =~ /\s*catch\s*\(\s*(\w+)\s*,\s*"(.+)"\s*\)/ ) {
        if ( $needlessBlocks > 0 ) {
            $needlessBlocks--;
            next;
        }
        $openBlocks--;
        if ( $wasThrow != 0 && $eType eq $1 ) {
            print $2. "\n";
            exit;
        }
        if ( $wasThrow != 0 ) {
            maybeError();
        }
        next;
    }
    if ( $line =~ /\s*try\s*/ ) {
        if ( $wasThrow == 0 ) {
            $openBlocks++;
        }
        else {
            $needlessBlocks++;
        }
        next;
    }
}
print "Unhandled Exception\n";

sub maybeError {
    if ( $openBlocks == 0 ) {
        print "Unhandled Exception\n";
        exit;
    }
}

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;

sub getMessage {
    my $t = shift;
    my $s = $t;
    $s =~ s/^[\w (,]+\"//g;
    $s =~ s/\"[\w) ]+$//g;
    $s =~ s/^[\s]+//g;
    $s =~ s/[\s]+$//g;
    return $s;
}

sub getException {
    my $t = shift;
    my $s = $t;
    $s =~ s/^[\w ]+\(//g;
    $s =~ s/,[\w )\"]+$//g;
    $s =~ s/^[\s]+//g;
    $s =~ s/[\s]+$//g;
    return $s;
}

sub startsWith {
    my $t = shift;
    my $tok = shift;
    my $s = $t;
    $s =~ s/^[\s]+//g;
    return 1 if ($s =~ /^$tok/);
    return 0;
}

sub getThrowExc {
    my $t = shift;
    my $s = $t;
    $s =~ s/^[\w ]+\(//g;
    $s =~ s/[)]//g;
    $s =~ s/^[\s]+//g;
    $s =~ s/[\s]+$//g;
    return $s;
}

sub blank {
    my $t = shift;
    my $s = $t;
    $s =~ s/[\s]+//g;
    return length($s) == 0;
}

my $n = <>;
my $line;
my $level = 0;
my $throwLevel = -1;
my $left = 0;
my $haveAnswer = 0;
my $exc;
while ($line = <>) {
    next if (blank($line));
    if ($throwLevel == -1) {
        if (startsWith($line, "try")) {
            $level++;
            next;
        }
        if (startsWith($line, "catch")) {
            $level--;
            next;
        }
        $exc = getThrowExc($line);
        $throwLevel = $level;
    } else {
        if (startsWith($line, "try")) {
            $left++;
            next;
        }
        my $e = getException($line);
        if ($left == 0 && $exc eq $e) {
            $haveAnswer = 1;
            print getMessage($line);
            last;
        }
        if ($left > 0) {
            $left--;
        }

    }
}

if ($haveAnswer != 1) {
    print "Unhandled Exception";
}

#!perl
use strict;
use warnings;

main();

sub main {
    my $n = int <>;
    my ($tries, $ltries, $type, $message) = (0, 0, "", 'Unhandled Exception');
tc: for (1..$n) {
        my $str = <>;
        chomp($str);
        $str =~ s/[(),]/ /g;
        $str =~ s/^[ \t]*//g;
        $str =~ s/ +/ /g;
        my ($cmd, $ttype, $cmessage) = split / /, $str, 3;
        if (! $cmd) { next tc; }
        if ($cmd eq 'try') {
            if ($type) {
                $ltries++;
            } else {
                $tries++;
            }
        } elsif ($cmd eq 'catch') {
            if ($ltries) {
                $ltries--;
            } else {
                if ($type) {
                    if ($type eq $ttype) {
                        $message = $cmessage;
                        $message =~ s/ +$//g;
                        $message =~ s/"//g;
                        last tc;
                    }
                } else {
                    $tries--;
                }
            }
        } elsif ($cmd eq 'throw') {
            $type = $ttype;
        }
    }
    print $message;
}

__END__

8
try
    try
        throw ( AE )
    catch ( BE, "BE in line 3")

    try
    catch(AE, "AE in line 5")
catch(AE,"AE somewhere")

#!perl
use strict;
use warnings;

main();

sub main {
    my $n = int <>;
    my ($tries, $ltries, $type, $message) = (0, 0, "", 'Unhandled Exception');
tc: for (1..$n) {
        my $str = <>;
        chomp($str);
        $str =~ s/[(),]/ /g;
        $str =~ s/^[ \t]*//g;
        $str =~ s/ +/ /g;
        my ($cmd, $ttype, $cmessage) = split / /, $str, 3;
        if (! $cmd) { next tc; }
        if ($cmd eq 'try') {
            if ($type) {
                $ltries++;
            } else {
                $tries++;
            }
        } elsif ($cmd eq 'catch') {
            if ($ltries) {
                $ltries--;
            } else {
                if ($type) {
                    if ($type eq $ttype) {
                        $message = $cmessage;
                        last tc;
                    }
                } else {
                    $tries--;
                }
            }
        } elsif ($cmd eq 'throw') {
            $type = $ttype;
        }
    }
    print $message;
}

__END__

#!perl
use strict;
use warnings;

main();

sub main {
    my $n = int <>;
    my ($tries, $ltries, $type, $message) = (0, 0, "", 'Unhandled Exception');
tc: for (1..$n) {
        my $str = <>;
        chomp($str);
        $str =~ s/[(),]/ /g;
        $str =~ s/^[ \t]*//g;
        $str =~ s/ +/ /g;
        my ($cmd, $ttype, $cmessage) = split / /, $str, 3;
        if (! $cmd) { next tc; }
        if ($cmd eq 'try') {
            if ($type) {
                $ltries++;
            } else {
                $tries++;
            }
        } elsif ($cmd eq 'catch') {
            if ($ltries) {
                $ltries--;
            } else {
                if ($type) {
                    if ($type eq $ttype) {
                        $message = $cmessage;
                        $message =~ s/"//g;
                        last tc;
                    }
                } else {
                    $tries--;
                }
            }
        } elsif ($cmd eq 'throw') {
            $type = $ttype;
        }
    }
    print $message;
}

__END__

#!/usr/local/bin/perl
$n              = <>;
$openBlocks     = 0;
$needlessBlocks =
  0;    #это блоки, открытые после эксепшена TODO
$wasThrow = 0;
$eType;
for ( 1 .. $n ) {
    my $line = <>;

    if ( $line =~ /\s*try\s*/ ) {
        if ( $wasThrow == 0 ) {
            $openBlocks++;
        }
        else {
            $needlessBlocks++;
        }
        next;
    }
    if ( $line =~ /\s*throw\s*\(\s*(\w+)\s*\)/ ) {
        $wasThrow = 1;
        $eType    = $1;
        maybeError();
        next;
    }
    if ( $line =~ /\s*catch\s*\(\s*(\w+)\s*,\s*"(.+)"\s*\)/ ) {
        if ( $needlessBlocks > 0 ) {
            $needlessBlocks--;
            next;
        }
        $openBlocks--;
        if ( $wasThrow != 0 && $eType eq $1 ) {
            print $2. "\n";
            exit;
        }
        if ($wasThrow != 0){
        maybeError();
        }

    }
}
print "Unhandled Exception\n";

sub maybeError {
    if ( $openBlocks == 0 ) {
        print "Unhandled Exception\n";
        exit;
    }
}

#!/usr/local/bin/perl
$n              = <>;
$openBlocks     = 0;
$needlessBlocks =
  0;    #это блоки, открытые после эксепшена TODO
$wasThrow = 0;
$eType;
for ( 1 .. $n ) {
    my $line = <>;

    if ( $line =~ /\s*try\s*/ ) {
        if ( $wasThrow == 0 ) {
            $openBlocks++;
        }
        else {
            $needlessBlocks++;
        }
        next;
    }
    if ( $line =~ /\s*throw\s*\(\s*(\w+)\s*\)/ ) {
        $wasThrow = 1;
        $eType    = $1;
        maybeError();
        next;
    }
    if ( $line =~ /\s*catch\s*\(\s*(\w+)\s*,\s*"(.+)"\s*\)/ ) {
        if ( $needlessBlocks > 0 ) {
            $needlessBlocks--;
            next;
        }
        $openBlocks--;
        if ( $wasThrow != 0 && $eType eq $1 ) {
            print $2. "\n";
            exit;
        }
        $wasThrow != 0 && maybeError();
        next;

    }
}

sub maybeError {
    if ( $openBlocks == 0 ) {
        print "Unhandled Exception\n";
        exit;
    }
}


$_ = <>, chomp;

$o = 'O';

$o =~ s/o*O?o*O$/ $& . reverse ucfirst lc $& /e for 1 .. 14;

print 'O' . substr $o, 0, $_ - 1

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my $n = $_;
	$_ = 'o' x 1e3;

	s/o/O/;

	my $i = 1;
	my $j = 1;

	while( $j < 1 + length ){
		substr $_, $j - 1, 1, 'O';
		my $tmp = $j;
		$j += $i;
		$i = $tmp;
		}

	print substr $_, 0, $n;
	}


my $n = <STDIN>;
my @a = split(/\s/, <STDIN>);
my $total = 0;
map{ $total += $_ } @a;
printf("%.12f", $total/$n);

#!/usr/bin/perl
use v5.10;

chomp($n = <>);
$tot+=$_ foreach (split / /, <>);
$ans = $tot / $n;
say $ans;

#!perl
use strict;

main();

sub main {
    my $n = int <>;
    my @arr = map int, split /\D/, <>;
    my $res = 0;
    for (@arr) {
        $res += $_;
    }
    $res /= $n;
    print $res;
}

__END__

#!/usr/bin/env perl

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

# essential
my @tokens = ();

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

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;
}

# solve

my $n = read_token;

my @percents = split q{ }, read_line;

my @proportions = map { $_ / 100 } @percents;

my $sums = sum(@proportions);

say $sums / $n * 100;


#!perl
$\ = "\n";

$n = <>;

@a = split /\s/, <>;

push @result, (abs @a[0] - @a[1]).' '.(abs @a[0] - @a[$#a]);

for my $i (1..$#a - 1)
{
    my $minLeft = abs @a[$i] - @a[$i - 1];
    my $minRight = abs @a[$i] - @a[$i + 1];

    my $maxLeft = abs @a[$i] - @a[0];
    my $maxRight = abs @a[$i] - @a[$#a];

    my $min = $minLeft < $minRight ? $minLeft : $minRight;
    my $max = $maxLeft > $maxRight ? $maxLeft : $maxRight;

    push @result, $min.' '.$max;
}

push @result, (abs @a[$#a - 1] - @a[$#a]).' '.(abs @a[0] - @a[$#a]);

print join "\n", @result;

use 5.016;
use strict;
use warnings;

my %min;
my %max;

<>;
my @cities = split ' ', <>;

for my $i (1..$#cities) {
  my $d = abs($cities[$i - 1] - $cities[$i]);
  $min{$i - 1} = $d if !exists $min{$i - 1} || $d < $min{$i - 1};
  $min{$i}     = $d if !exists $min{$i}     || $d < $min{$i};
}

for my $i (0..$#cities) {
  my $left = $cities[$i] - $cities[0];
  my $right = $cities[-1] - $cities[$i];
  $max{$i} = $left > $right ? $left
                            : $right;
}

for my $i (0..$#cities) {
  say "$min{$i} $max{$i}"
}

use 5.016;
use strict;
use warnings;

my %min;
my %max;

<>;
my @cities = split ' ', <>;

for my $i (1..$#cities) {
  my $d = abs($cities[$i - 1] - $cities[$i]);
  $min{$i - 1} = $d if !exists $min{$i - 1} || $d < $min{$i - 1};
  $min{$i}     = $d if !exists $min{$i}     || $d < $min{$i};
}

for my $i (0..$#cities) {
  my $left = $cities[$i] - $cities[0];
  my $right = $cities[-1] - $cities[0];
  $max{$i} = $left > $right ? $left
                            : $right;
}

for my $i (0..$#cities) {
  say "$min{$i} $max{$i}"
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $A, $B ) = split;
	$_ = <>, chomp;

	my $reverse = reverse;

	for my $i ( 0 .. -1 + length ){
		my $c1 = substr $_, $i, 1;
		my $c2 = substr $reverse, $i, 1;
		if( 0 ){ ; }
		elsif( $c1 eq '?' and $c2 eq '?' ){
			;
			}
		elsif( $c1 eq '?' and $c2 eq '0' ){
			substr $_, $i, 1, 0;
			}
		elsif( $c1 eq '0' and $c2 eq '?' ){
			substr $reverse, $i, 1, 0;
			}
		elsif( $c1 eq '?' and $c2 eq '1' ){
			substr $_, $i, 1, 1;
			}
		elsif( $c1 eq '1' and $c2 eq '?' ){
			substr $reverse, $i, 1, 1;
			}
		}

	if( $_ ne reverse ){
		print -1;
		next;
		}

	my $_0 = () = m/0/g;
	my $_1 = () = m/1/g;

	$A -= $_0;
	$B -= $_1;

	if( $A < 0 or $B < 0 ){
		print -1;
		next;
		}

	my @change;

	if( ( length ) % 2 == 1 ){
		if( $A % 2 == 1 ){
			$A --;
			push @change, 0;
			}
		elsif( $B % 2 == 1 ){
			$B --;
			push @change, 1;
			}
		}

	if( $A % 2 == 1 or $B % 2 == 1 ){
		print -1;
		next;
		}

	push @change, ( 0 ) x ( $A / 2 ), ( 1 ) x ( $B / 2 );
	unshift @change, ( 1 ) x ( $B / 2 ), ( 0 ) x ( $A / 2 );

	s/\?/shift @change/ge;

	print;
	}


my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my @RINDEX;
my @DINDEX;

for(my $i=0;$i<length($s);$i++)
{
	if(substr($s,$i,1) eq "R")
	{
	push(@RINDEX,$i)
	}
	if(substr($s,$i,1) eq "D")
	{
	push(@DINDEX,$i)
	}
}
my $j=0;
while(scalar(@RINDEX) and scalar(@DINDEX)){

	if($RINDEX[0]==$j)
	   {
	   		shift @DINDEX;
	   		push(@RINDEX,shift @RINDEX);
		}elsif($DINDEX[0]==$j){
	   		shift @RINDEX;
	   		push(@DINDEX,shift @DINDEX);
		}
$j++;
if($j>=length($s)){
	$j=0;
}
}

if(scalar(@RINDEX)){
	print "R \n";
}
if(scalar(@DINDEX)){
	print "D \n";
}

use strict;
my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my $i=0;
my @RINDEX;
my @DINDEX;

for(my $i=0;$i<length($s);$i++)
{
	if(substr($s,$i,1) eq "R")
	{
	push(@RINDEX,$i)
	}
	if(substr($s,$i,1) eq "D")
	{
	push(@DINDEX,$i)
	}
}
my $j=0;
while(scalar(@RINDEX)>0 and scalar(@DINDEX)>0 ){

	if($RINDEX[0]==$j)
	   {
	   		shift @DINDEX;
	   		push(@RINDEX,shift @RINDEX);
		}elsif($DINDEX[0]==$j){
	   		shift @RINDEX;
	   		push(@DINDEX,shift @DINDEX);
		}
$j++;
if($j>=length($s)){
	$j=0;
}
}

if(scalar(@RINDEX)>0){
	print "R \n";
}
if(scalar(@DINDEX)>0){
	print "D \n";
}

use strict;
use warnings;
my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my $i=0;

while(index($s,"D") >=0 and index($s,"R") >=0)
{

	if(substr($s,$i,1) eq "R")
	{
		my $ind=index($s,"D");
		substr($s,$ind,1,"0");
	}
	elsif(substr($s,$i,1) eq "D")
	{
		my $ind=index($s,"R");
		substr($s,$ind,1,"0");
	}
	$i++;
	$s =~ s/0//g;
	if($i>=length($s)){
		$i=0;
	}

}

if(index($s,"D")>=0){
	print "D\n";
}
if(index($s,"R")>=0){
	print "R\n";
}

use strict;
my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my $i=0;
my @RINDEX;
my @DINDEX;
my $j=0;
my $k=0;
for(my $i=0;$i<length($s);$i++)
{
	if(substr($s,$i,1) eq "R")
	{
	$RINDEX[$j++]=$i;
	}
	if(substr($s,$i,1) eq "D")
	{
	$DINDEX[$k++]=$i;
	}
}

$i=0;
$j=0;
$k=0;
while(scalar(@RINDEX)>0 and scalar(@DINDEX)>0 ){
if($RINDEX[$i] == $j ){
	shift @DINDEX;
	if($k>0){
		$k--;
	}
    $i++;
}elsif( $DINDEX[$k] == $j){
	shift @RINDEX;
	if($i>0){
		$i--;
	}
	$k++;
}
$j++;
if($j>=length($s)){
$i=0;
$j=0;
$k=0;
}
}

if(scalar(@RINDEX)>0){
	print "R \n";
}
if(scalar(@DINDEX)>0){
	print "D \n";
}

use strict;
use warnings;
my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my $i=0;
my @RINDEX;
my @DINDEX;
my $j=0;
my $k=0;
for(my $i=0;$i<length($s);$i++)
{
	if(substr($s,$i,1) eq "R")
	{
	$RINDEX[$j++]=$i;
	}
	if(substr($s,$i,1) eq "D")
	{
	$DINDEX[$k++]=$i;
	}
}

$i=0;
while(scalar(@RINDEX)>0 and scalar(@DINDEX)>0){
if($RINDEX[$i] > $DINDEX[$i]){
	shift @DINDEX;
}else{
	shift @RINDEX;
}
}

if(scalar(@RINDEX)>0){
	print "R \n";
}
if(scalar(@DINDEX)>0){
	print "D \n";
}

use strict;
use warnings;
my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my $i=0;
my @RINDEX;
my @DINDEX;
my $j=0;
my $k=0;
for(my $i=0;$i<length($s);$i++)
{
	if(substr($s,$i,1) eq "R")
	{
	$RINDEX[$j++]=$i;
	}
	if(substr($s,$i,1) eq "D")
	{
	$DINDEX[$k++]=$i;
	}
}

$i=0;
while(scalar(@RINDEX)>0 and scalar(@DINDEX)>0){
if($RINDEX[$i] < $DINDEX[$i]){
	shift @DINDEX;
}elsif($RINDEX[$i] > $DINDEX[$i]){
	shift @RINDEX;
}
}

if(scalar(@RINDEX)>0){
	print "R \n";
}
if(scalar(@DINDEX)>0){
	print "D \n";
}

use strict;
use warnings;
my $n=<>;
chomp($n);
my $s=<>;
chomp($s);

my $i=0;
my @RINDEX;
my @DINDEX;
my $j=0;
my $k=0;
for(my $i=0;$i<length($s);$i++)
{
	if(substr($s,$i,1) eq "R")
	{
	$RINDEX[$j++]=$i;
	}
	if(substr($s,$i,1) eq "D")
	{
	$DINDEX[$k++]=$i;
	}
}

$i=0;
while(scalar(@RINDEX)>0 and scalar(@DINDEX)>0){
print "@RINDEX \n";
print "@DINDEX \n";
if($RINDEX[$i] < $DINDEX[$i]){
	shift @DINDEX;
}elsif($RINDEX[$i] > $DINDEX[$i]){
	shift @RINDEX;
}
}

if(scalar(@RINDEX)>0){
	print "R \n";
}
if(scalar(@DINDEX)>0){
	print "D \n";
}


use strict;
use warnings;

my $n = <>;

sub isPrime{
        my $num = shift;
        my $count = 0;
        for(my $i = 1 ; $i <= int(sqrt($num)) ; $i++){
                if($num % $i == 0){
                        $count++;
                }
        }
        if($count > 1){
                return 0;
        }else{
                return 1;
        }
}

for(my $j = 2 ;;$j++){
        my $p = $n - $j;
        if(!isPrime($p) && !isPrime($j)){
                print "$j $p";
                last;
        }
}

#!/usr/bin/perl

my $n = <>;

sub isPrime($) {
	my $arg = $_[0];
	my $res = 1;
	for (my $i = 2; $i <= sqrt($arg); $i++) {
		unless ($arg % $i) {
			$res = 0;
		}
	}

	return $res;
}

for (my $i = 4; $i <= ($n / 2); $i++) {
	if (!isPrime($i) && !isPrime($n - $i)) {
		print($i . " " . ($n - $i));
		exit;
	}
}

#!/usr/bin/perl
# your code goes here
use v5.10;

chomp($n = <>);
$n%2==0 and say 4,' ',$n-4 or say 9,' ',$n-9;

#!/usr/bin/perl -w

use strict;

sub primeq {
    my $n = shift;
    return '' if $n < 2;
    for (2 .. sqrt($n) + 1) {
        return '' if $n % $_ == 0;
    }
    return 1;
}

my $n = <>;
for (4 .. $n / 2) {
    if (!primeq($_) && !primeq($n - $_)) {
        print "$_ ", $n - $_, "\n";
        last;
    }
}


<>;

print <> =~ s/\n//r eq (join ' ', map y/yaeiou//d, <>) ? "YES" : "NO"

my $n = <> + 0;
my $P = <>;
my @pp = split ' ', $P;

for (my $i = 0; $i < $n; ++$i) {
	my $line = <>;
	my @lets = split //, $line;
	foreach (@lets) {
		--$pp[$i] if /a|e|i|o|u|y/;
	}
	if ($pp[$i] != 0){
		print "NO\n";
		exit 0;
	}
}
print "YES\n";

<>;

@_ = split ' ', <>;

y/yaeiou//d == shift @_ or $f++ while <>;

print $f ? "NO" : "YES"

#!/usr/bin/perl
use strict;
use warnings;

my $n = <>;
my @arr = split " ", <>;
# print "@arr";
my $ans = "YES";
for (0..$n-1) {
  my $count = 0;
  chomp(my $line = <>);
  $count = () = $line =~ /[aeiouy]/g;
  if ($count != $arr[$_]) {
    $ans = "NO";
    last;
  }
}
print "$ans\n";

$n = int(<STDIN>);
@k = split(/ /, <STDIN>);
my $i = 0;
while(<STDIN>) {
    if(($_ =~ tr/[aeiouy]//) != $k[$i]) {
        print "NO\n";
        exit 0;
    }
    $i++;
}
print "YES\n";


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my @n = map ~~<>, 1 .. $n;
	chomp @n;
	my %n = map { $_, undef } @n;

	my @m = map ~~<>, 1 .. $m;
	chomp @m;
	my %m = map { $_, undef } @m;

	my $i = 0;

	for (@m){
		exists $n{$_} or next;
		$i ++;
		if( $i % 2 ){
			delete $m{$_}
			}
		else {
			delete $n{$_}
			}
		}

	print qw(NO YES)[ (~~ keys %n) > (~~ keys %m) ];
	}


my $poke_nr = <>;
my %pokemons;
my @numbers = split(" ", <>);
my $biggest_poke = 0;
my $max_poke = 0;
my $current_max = 0;
foreach(@numbers)
{
	if(exists($pokemons{$_}))
	{
		$pokemons{$_} = $pokemons{$_} + 1;
	}
	else
	{
		$pokemons{$_} = 1;
		if($_ > $biggest_poke)
		{
			$biggest_poke = $_;
		}
	}
}
foreach my $i(2 .. $biggest_poke)
{
	$current_max = 0;
	for(my $j = $i; $j <= $biggest_poke; $j += $i)
	{
		if(exists($pokemons{$j}))
		{
			$current_max += $pokemons{$j};
		}
	}
	if($current_max > $max_poke)
	{
		$max_poke = $current_max;
	}
}
if($max_poke > 0)
{
	print $max_poke;
}
else
{
	if(exists($pokemons{1}))
	{
		print 1;
	}
	else
	{
		print 0;
	}

}

my $poke_nr = <>;
my %pokemons;
my @numbers = split(" ", <>);
my $biggest_poke = 0;
my $max_poke = 0;
my $current_max = 0;
foreach(@numbers)
{
	if(exists($pokemons{$_}))
	{
		$pokemons{$_} = $pokemons{$_} + 1;
	}
	else
	{
		$pokemons{$_} = 1;
		if($_ > $biggest_poke)
		{
			$biggest_poke = $_;
		}
	}
}
foreach my $i(1 .. $biggest_poke)
{
	$current_max = 0;
	for(my $j = $i; $j <= $biggest_poke; $j += $i)
	{
		if(exists($pokemons{$j}))
		{
			$current_max += $pokemons{$j};
		}
	}
	if($current_max > $max_poke)
	{
		$max_poke = $current_max;
	}
}
print $max_poke;

my $poke_nr = <>;
my %pokemons;
my @numbers = split(" ", <>);
my $biggest_poke = 0;
my $max_poke = 0;
my $current_max = 0;
foreach(@numbers)
{
	if(exists($pokemons{$_}))
	{
		$pokemons{$_} = $pokemons{$_} + 1;
	}
	else
	{
		$pokemons{$_} = 1;
		if($_ > $biggest_poke)
		{
			$biggest_poke = $_;
		}
	}
}
foreach my $i(2 .. $biggest_poke)
{
	$current_max = 0;
	for(my $j = $i; $j <= $biggest_poke; $j += $i)
	{
		if(exists($pokemons{$j}))
		{
			$current_max += $pokemons{$j};
		}
	}
	if($current_max > $max_poke)
	{
		$max_poke = $current_max;
	}
}
print $max_poke;


<>;
$_=<>;
s/\s//g;
while(/1/g){push @a, pos}
while(/2/g){push @b, pos}
while(/3/g){push @c, pos}
$min = (sort {$a <=> $b} (0+@a, 0+@b, 0+@c) )[0];
print $min, $/;
while($min--){
	printf "%d %d %d\n", shift @a, shift @b, shift @c
	}

use strict;
use warnings;

<>;
my @kids = split ' ', <>;
my @teams;
my %kinds;

for my $i (0..$#kids) {
    push @{$kinds{$kids[$i]}}, $i + 1;
}

OUTER: for (;;) {
    my @line;
    for (1..3) {
        my $m = shift @{$kinds{$_}};
        last OUTER unless defined $m;
        push @line, $m;
    }
    push @teams, \@line;
}

$\ = "\n";
$, = ' ';

print scalar @teams;
print @$_ for @teams;

#!/usr/bin/perl
#use warnings;
#use strict;

<>;
my @students = split(' ', <>);
my @pro; my @ma; my @pre;
my $idx;
for my $el(@students){
	++$idx;
	if($el == 1){push (@pro, $idx);}
	elsif($el == 2){push (@ma, $idx);}
	else{push (@pre, $idx);}
}

$idx = $#pro;	if($#ma < $idx){$idx = $#ma;} if($#pre < $idx){$idx = $#pre;} $idx ++;
print "$idx\n";
while($#pro >= 0 && $#ma >= 0 && $#pre >= 0){
	print ((pop @pro), " ", (pop @ma), " ", (pop @pre), "\n");
}

#!/usr/bin/perl
use strict;
use warnings;

sub min{
    return (sort {$a <=> $b} @_)[0];
}
sub len{
    return scalar @{$_[0]};
}

my $n = <>;
my @list = split ' ',<>;
my @arr;
push @arr,[] for 0..3;
for my $i(0..$n-1){
    my $val = $list[$i];
    push @{$arr[$val]}, $i+1;
}
my $min = min(len($arr[1]), len($arr[2]), len($arr[3]));
print $min, "\n";
for my $i(0..$min-1){
    print "$arr[1][$i] $arr[2][$i] $arr[3][$i]\n";
}

#!/usr/bin/perl
#use warnings;
#use strict;

<>;
my @students = split(' ', <>);
my @pro; my @ma; my @pre;
my $idx;
for my $el(@students){
	++$idx;
	if($el == 1){push (@pro, $idx);}
	elsif($el == 2){push (@ma, $idx);}
	else{push (@pre, $idx);}
}

while($#pro >= 0 && $#ma >= 0 && $#pre >= 0){
	print ((pop @pro), " ", (pop @ma), " ", (pop @pre), "\n");
}

#!/usr/bin/perl
#use warnings;
#use strict;

<>;
my @students = split(' ', <>);
my @pro; my @ma; my @pre;
my $idx;
for my $el(@students){
	++$idx;
	if($el == 1){push (@pro, $idx);}
	elsif($el == 2){push (@ma, $idx);}
	else{push (@pre, $idx);}
}
$idx = scalar @pro; $idx < scalar @ma ? $idx : $idx = scalar @ma;  $idx < scalar @pre ? $idx : $idx = scalar @pre;
print "$idx\n";
while($#pro >= 0 && $#ma >= 0 && $#pre >= 0){
	print ((pop @pro), " ", (pop @ma), " ", (pop @pre), "\n");
}

#!/usr/bin/perl
#use warnings;
#use strict;

<>;
my @students = split(' ', <>);
my @pro; my @ma; my @pre;
my $idx;
for my $el(@students){
	++$idx;
	if($el == 1){push (@pro, $idx);}
	elsif($el == 2){push (@ma, $idx);}
	else{push (@pre, $idx);}
}
$idx = $#pro; $idx < $#ma ? $idx : $idx = $#ma;  $idx < $#pre ? $idx : $idx = $#pre; $idx++;
print "$idx\n";
while($#pro >= 0 && $#ma >= 0 && $#pre >= 0){
	print ((pop @pro), " ", (pop @ma), " ", (pop @pre), "\n");
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

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

	$_ = <>, chomp;
	@_ = split //;

	my %h;

	map $h{ $_ } ++, @_;

	my @az = sort keys %h;

	my( $min, $max ) = @az[ 0, -1 ];

	my $az = join '', @az;

	$debug and print "[$min]";

	if( $k > $n ){
		$debug and print '>';
		print $_ . $min x ( $k - $n );
		}
	else{
		for my $i ( reverse 0 .. $k - 1 ){
			if( $_[ $i ] ne $max ){
				print @_[ 0 .. $i - 1 ],
					( grep $_ gt $_[ $i ], @az )[ 0 ],
					( $min ) x ( $k - $i - 1 );
				last;
				}
			}
		}
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

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

	$_ = <>, chomp;

	my %h;

	map $h{ $_ } ++, split //;

	my @az = sort keys %h;

	my( $min, $max ) = @az[ 0, -1 ];

	$debug and print "[$min|$max]";

	my $pos = -1;

	/
		(?= (.)(.) )
		(?{
			$1 lt $2 and $pos = pos;
		})
		(*FAIL)
	/x;

	my $next = 0;
	my $ans = '';

	if( $k > $n ){
		$debug and print '>';
		print $_ . $az[ 0 ] x ( $k - $n );
		}
	else{
		for my $lett ( reverse( ( split // )[ 0 .. $k - 1 ] ) ){
			$debug and print $lett;
			$next and do {
				$ans .= $lett;
				next;
				};

			if( $lett eq $max ){
				$ans .= $min;
				}
			else{
				$ans .= $max;
				$next = 1;
				}
			}
		print scalar reverse $ans;
		}
	}


use 5.016;
use strict;
use warnings;

sub max {
  my $max = shift;
  local $_;

  for (@_) {
    $max = $_ if $_ > $max;
  }
  $max;
}

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

<>;
my @welfare = split ' ', <>;
my $max = max(@welfare);

say sum map { $max - $_ } @welfare;

#!/usr/bin/env perl

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

# send argument as array, not reference, to function requiring list

# essential
my @tokens = ();

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

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;
}

# solve

my $n = read_token;

my @welfares = split q{ }, read_line;

my $max = max(@welfares);

my $answer = 0;

for (@welfares) {
    $answer += $max - $_;
}

say $answer;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	@_ = split ' ', <>;

	$debug and print "[@_]";

	my $sum = 0;

	$sum += $_ for @_;

	my $xor = 0;

	$xor ^= $_ for @_;

	$debug and print "sum:[$sum]";
	$debug and print "xor:[$xor]";

	my $Bsum = sprintf "%0b", $sum;
	my $Bxor = sprintf "%0b", $xor;

	$debug and print "Bsum:[$Bsum]";
	$debug and print "Bxor:[$Bxor]";

	my @ans;

	push @ans, $xor;

	$sum += $xor;
	$xor ^= $xor;

	$Bsum = sprintf "%0b", $sum;
	$Bxor = sprintf "%0b", $xor;

	$debug and print "Bsum:[$Bsum]";
	$debug and print "Bxor:[$Bxor]";

	push @ans, $sum;

	print for ~~ @ans, "@ans";
	}


#!/usr/bin/perl

main();

sub main {
    my %hash = ();
    my $line;
    my $ans = 0;
    while ($line = <>) {
        if ($line =~ /^\+/) {
            $line =~ s/^\+//;
            $hash{$line} = 1;
            next;
        }
        if ($line =~ /^-/) {
            $line =~ s/^-//;
            delete $hash{$line};
            next;
        }
        my $cnt = keys (%hash);
        $line =~ s/^[\S]+://g;
        $line =~ s/[^\w ]+//g;
        $ans += length ($line) * $cnt;
    }
    print ("$ans\n");
}

#!/usr/bin/perl

my @users;
my $total = 0;
my $len;

while (<>) {
    chomp $_;
    if (/\+/) {
	$_ =~ s/\+//;
	push @users, $_;
    } elsif (/\-/) {
	$_ =~ s/\-//;
	for my $i (0 .. $#users){
	    if ($users[$i] eq $_) {
		splice(@users, $i, 1, ());
	    }
	}
    } else {
	$_ =~ s/([\w]*:)//;
	$total += (scalar @users) * (length $_);
    }
}

print $total

#!usr/bin/perl
use strict;
use warnings;

my %ch;my $snd;my $traf=0;my $com;my $msg;my $come;
for(1..100){
    $come=<STDIN>;
    chomp($come);
    if($come eq ""){ last;}
    if($come=~/\A[\+]/){$com=$come=~s/[\+]//r; $ch{$com}=1;}
    elsif($come=~/\A[\-]/){$com=$come=~s/[\-]//r; $ch{$com}=0;}
    elsif($come=~/:/){
	($snd,$msg)=split/:/,$come;chomp($msg);
	foreach(keys %ch){ if($ch{$_}==1){$traf+=length($msg);}}
    }
}
print $traf."\n";


#!/usr/bin/perl

use strict;
use warnings;

chomp(my $n = <>);
chomp($_ = <>);
my @a = split;
my @pos  = grep { $_ > 0 } @a;
my @neg  = grep { $_ < 0 } @a;
my @zero = grep { $_ == 0 } @a;

my @set1 = ();
my @set2 = ();
my @set3 = ();

if (@pos) {
    push @set1, shift @neg;
    push @set2, shift @pos;
    push @set3, @pos;
    push @set3, @neg;
    push @set3, @zero;
} else {
    push @set1, shift @neg;
    push @set2, shift @neg;
    push @set2, shift @neg;
    push @set3, @pos;
    push @set3, @neg;
    push @set3, @zero;
}

unshift @set1, scalar @set1;
unshift @set2, scalar @set2;
unshift @set3, scalar @set3;
print "@set1\n@set2\n@set3\n";

#!/usr/bin/perl
#

while ($eil=<>) {

$num = <>;
@num = split/ /,$num;
chomp @num;

$i=0;
@a1 = @a2 = @a3 = ();
foreach (@num){
    if ($_==0) {push @a3, $_}
    elsif ($_<0 and $i==0) {push @a1, $_; $i++}
    else {push @a2, $_}
    }
print "@num\n";
print "1 @a1\n";
print scalar @a2, " @a2\n";
print scalar @a3, " @a3\n";

}

#!/usr/bin/perl
#

while ($eil=<>) {

$num = <>;
@num = split/ /,$num;
chomp @num;

$i=0;
@a1 = @a2 = @a3 = ();
foreach (@num){
    if ($_==0) {push @a3, $_}
    elsif ($_<0 and $i==0) {push @a1, $_; $i++}
    else {push @a2, $_}
    }
# print "@num\n";
print "1 @a1\n";
print scalar @a2, " @a2\n";
print scalar @a3, " @a3\n";

}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	@_ = split ' ', <>;

	my( $max, $smax ) = sort { $b <=> $a } @_;

	$_ -= $max for @_;

	$_ ||= $max - $smax for @_;

	print "@_";
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $h1, $a1, $c ) = split;
	my( $h2, $a2 ) = split ' ', <>;

	my @moves;

	while( 1 ){
		if( $h1 - $a2 > 0 || $a1 >= $h2 ){
			push @moves, 'STRIKE';
			$h2 -= $a1;
			$h2 <= 0 and last;
			}
		else{
			push @moves, 'HEAL';
			$h1 += $c;
			}
		$h1 -= $a2;
		}

	print ~~ @moves;
	print for @moves;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $h1, $a1, $c ) = split;
	my( $h2, $a2 ) = split ' ', <>;

	my @moves;

	while( 1 ){
		if( $h1 - $a2 > 0 ){
			push @moves, 'STRIKE';
			$h2 -= $a1;
			$h2 <= 0 and last;
			}
		else{
			push @moves, 'HEAL';
			$h1 += $c;
			}
		$h1 -= $a2;
		}

	print ~~ @moves;
	print for @moves;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $h1, $a1, $c ) = split;
	my( $h2, $a2 ) = split ' ', <>;

	my @moves;

	while( 1 ){
		if( $h1 - $a2 > 0 ){
			push @moves, 'STRIKE';
			$h2 -= $a1;
			$h2 <= 0 and last;
			$h1 -= $a2;
			}
		else{
			push @moves, 'HEAL';
			$h1 += $c;
			}
		}

	print ~~ @moves;
	print for @moves;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){

	my( $n, $m ) = split;

	my $min = (sort {$a <=> $b} map { 0 - eval join '-', split ' ', <> } 1 .. $m )[ 0 ];

	print 1 + $min;
	print join " ", ( (0 .. $min) x (1 + $n / ( $min || 1 ) ) )[ 0 .. $n - 1 ];

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){

	my( $n, $m ) = split;

	my $min = (sort {$a <=> $b} map { 0 - eval join '-', split ' ', <> } 1 .. $m )[ 0 ];

	print 1 + $min;
	print do { local $, = " "; ( (0 .. $min) x (1 + $n / $min) )[ 0 .. $n - 1 ] };

	}


my $n = int<>;
my @arr = split //, <>;
my @stack = ();
my $ans = $n;
push @stack, shift @arr;
for (2..$n) {
    push @stack, shift @arr;
    if (scalar @stack > 1 && $stack[-1] != $stack[-2]) {
        pop @stack;
        pop @stack;
    }
}
print scalar @stack;

#!/usr/bin/perl
use v5.10;

my ($n, $line, $on, $zn) = (<>, <>, 0, 0);
chomp $line;
$_>0 and ++$on or ++$zn foreach (split //, $line);
say abs($zn-$on);

use 5.016;
use strict;
use warnings;

sub abs {
  my ($x) = @_;
  $x < 0 ? -$x : $x;
}

<>;
chomp (my $bits = <>);

my $zero = $bits =~ tr/0//d;
my $one = $bits =~ tr/1//d;
say abs($one - $zero);

use strict;
use warnings;
#!/usr/bin/perl
#$, = " ";

$a = <>;
$a = <>;
chomp($a);
my ($zero, $one) = (0,0);
foreach my $i (0..length($a)-1) {
	$zero += (substr($a,$i,1)=='0');
	$one += (substr($a,$i,1)=='1');
}
print abs($zero-$one);


#!/usr/bin/perl
use v5.10;

chomp($n = <>);
$ans = 0;
$ans=$_>$ans ? $_:$ans foreach (split / /, <>);
say $ans;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $k ) = split;
	$k = ( sort { $a <=> $b } $n - $k, $k )[ 0 ];

	my $L = 0;
	my $sect = 1;
	my $plus = 1;
	my @sect;

	for my $i (1 .. $n){
		$L += $k;
		if( $L > $n ){
			$plus ++;
			push @sect, $sect += $plus;
			$plus ++;
			$L %= $n;
			}
		else{
			push @sect, $sect += $plus;
			}

		}

	print "@sect";
	}