use 5.020;
use warnings;
use utf8;

#下面列举出几个特别难搞的东西，这里出错的几率很大
# 6 6 3 -1 3 4 3 3
# 5 3 2 5 4 -1 10 9
# 1 -1 2 -1 3 -1 4 -1 5 -1 6 -1 7 -1 8 -1 24
# 1  2  3  4  5  6  7  8 -1 24
sub readNext {
    my $line = <STDIN>;
    chomp($line);
    return $line;
}

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

READ: while ($count) {

    $index++;

    &readNext();
    my @array = split /\s+/, &readNext();

    # if ( $index == 369 ) {
    #     say join( " ", @array );
    # }

    my @adjacentArray = ();
    my @presentArray  = ();

    my $lastNumber;
    my $lastIndex        = -1;
    my $lastPushedIndex  = -1;
    my $boolLastIsAbsent = 0;
    my $mForArray        = 0;

    for my $i ( 0 .. $#array ) {

        my $item = int( $array[$i] );

        if ( $item == -1 ) {

            if (   defined($lastNumber)
                && $lastNumber != -1
                && !$boolLastIsAbsent
                && $lastPushedIndex != $lastIndex )
            {
                push( @adjacentArray, $lastNumber );
                $lastPushedIndex = $lastIndex;

            }

            $boolLastIsAbsent = 1;
        }
        else {
            if ( $boolLastIsAbsent && $lastPushedIndex != $i ) {
                push( @adjacentArray, $item );
                $lastPushedIndex = $i;
            }
            $boolLastIsAbsent = 0;

            if ( defined($lastNumber) && $lastNumber != -1 ) {
                my $gap = abs( $lastNumber - $item );
                if ( $gap > $mForArray ) {
                    $mForArray = $gap;
                }
            }
        }

        $lastNumber = $item;
        $lastIndex  = $i;
    }

    if (@adjacentArray) {

        # say join( ",", @adjacentArray );

        @adjacentArray = sort { $a <=> $b } @adjacentArray;

        my $k =
          int(
            ( $adjacentArray[0] + $adjacentArray[$#adjacentArray] ) / 2 + 0.5 );

        # say "the k is $k";

        my $mforK = (
            (
                ( $k - $adjacentArray[0] ) >
                  ( $adjacentArray[$#adjacentArray] - $k )
            )
            ? ( $k - $adjacentArray[0] )
            : ( $adjacentArray[$#adjacentArray] - $k )
        );

        # say "mfork $mforK   mForArray $mForArray";

        my $m = $mforK > $mForArray ? $mforK : $mForArray;

        say $m. " " . $k;
    }
    else {
        say "0 0";
    }

    $count--;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	$_ = <>, chomp;

	$debug and print "[$_]";

	0 while s/-1\K( -1)+//g;

	$debug and print "[$_]";

	@_ = split;

	if( ! grep { $_ != -1 } @_ ){
		print "0 42";
		next;
		}

	my @A;

	for my $i ( 0 .. @_ - 1 ){
		next if $_[ $i ] != -1;

		push @A, $_[ $i - 1 ] if $i - 1 >= 0;
		push @A, $_[ $i + 1 ] if $i + 1 <= @_ - 1;
		}

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

	my $undef;

	if( @A ){
		my( $min, $max ) = ( sort { $a <=> $b } @A )[ 0, -1 ];
		my $diff = $max - $min;

		$undef = $min + ( $diff >> 1 );
		}

	if( not defined $undef ){
		( $undef ) = grep { $_ != -1 } @_;
		}

	$_ == -1 and $_ = $undef for @_;

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

	my @B;

	for my $i ( 0 .. @_ - 2 ){
		push @B, abs( $_[ $i ] - $_[ $i + 1 ] );
		}

	my $max_diff = ( sort { $b <=> $a } @B )[ 0 ] // 0;

	print "$max_diff $undef";
	}

use 5.020;
use warnings;
use utf8;

# 6 6 3 -1 3 4 3 3 这个情况很难，确定相邻的问题
sub readNext {
    my $line = <STDIN>;
    chomp($line);
    return $line;
}

my $count = int( &readNext() );
my $index = 0;
READ: while ($count) {

    $index++;

    &readNext();
    my @array = split / /, &readNext();

    if ( $index == 369 ) {
        say join( "-", @array );
    }

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

    my $lastNumber;
    my $lastGap;
    my $lastIndex;

    for my $i ( 0 .. $#array ) {

        my $item = int( $array[$i] );

        if ( $item == -1 ) {
            next;
        }

        if ( defined($lastIndex) ) {
            if ( ( $i - $lastIndex ) == 1 ) {
                my $gap = abs( $item - $lastNumber );
                if ( defined($lastGap) ) {
                    $lastGap = $gap > $lastGap ? $gap : $lastGap;
                }
                else {
                    $lastGap = $gap;
                }
            }
        }

        if ( defined($min) ) {

            if ( $item < $min ) {
                $min      = $item;
                $minIndex = $i;
            }

            if ( $item > $max ) {
                $max      = $item;
                $maxIndex = $i;
            }
        }
        else {
            $min = $item;
            $max = $item;

            $minIndex = $i;
            $maxIndex = $i;
        }

        $lastIndex  = $i;
        $lastNumber = $item;
    }

    # say("max gap :$lastGap, min:$minIndex, max:$maxIndex");

    if ( defined($min) ) {

        if ( $minIndex == $maxIndex ) {
            say "0 $min";
        }

        elsif ( abs( $maxIndex - $minIndex ) == 1 ) {
            say( $lastGap . " " . $min );
        }
        else {
            my $k = int( ( $max + $min ) / 2 + 0.999 );
            my $m =
                ( ( $max - $k ) > ( $k - $min ) )
              ? ( $max - $k )
              : ( $k - $min );
            if ( defined($lastGap) ) {
                say( ( ( $lastGap > $m ) ? $lastGap : $m ) . " " . $k );
            }
            else {
                say $m. " " . $k;
            }
        }
    }
    else {
        say "0 0";
    }

    $count--;
}

use 5.020;
use warnings;
use utf8;

# 6 6 3 -1 3 4 3 3 这个情况很难，确定相邻的问题
sub readNext {
    my $line = <STDIN>;
    chomp($line);
    return $line;
}

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

READ: while ($count) {
    &readNext();
    my @array = split / /, &readNext();

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

    my $lastNumber;
    my $lastGap;
    my $lastIndex;
    for my $i ( 0 .. $#array ) {

        my $item = int( $array[$i] );

        if ( $item == -1 ) {
            next;
        }

        if ( defined($lastIndex) ) {
            if ( ( $i - $lastIndex ) == 1 ) {
                my $gap = abs( $item - $lastNumber );
                if ( defined($lastGap) ) {
                    $lastGap = $gap > $lastGap ? $gap : $lastGap;
                }
                else {
                    $lastGap = $gap;
                }
            }
        }

        if ( defined($min) ) {

            if ( $item < $min ) {
                $min      = $item;
                $minIndex = $i;
            }

            if ( $item > $max ) {
                $max      = $item;
                $maxIndex = $i;
            }
        }
        else {
            $min = $item;
            $max = $item;

            $minIndex = $i;
            $maxIndex = $i;
        }

        $lastIndex  = $i;
        $lastNumber = $item;
    }

    # say("max gap :$lastGap, min:$minIndex, max:$maxIndex");

    if ( defined($min) ) {

        if ( $minIndex == $maxIndex ) {
            say "0 $min";
        }

        elsif ( abs( $maxIndex - $minIndex ) == 1 ) {
            say( $lastGap . " " . $min );
        }

        else {
            my $k = int( ( $max + $min ) / 2 + 0.9 );

            if ( defined($lastGap) ) {
                say $lastGap. " " . $k;
            }
            else {
                my $m =
                    ( ( $max - $k ) > ( $k - $min ) )
                  ? ( $max - $k )
                  : ( $k - $min );
                say $m. " " . $k;
            }
        }
    }
    else {
        say "0 0";
    }

    $count--;
}

use 5.020;
use warnings;
use utf8;

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

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

READ: while ($count) {
    &readNext();
    my @array = split / /, &readNext();

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

    for my $i ( 0 .. $#array ) {

        my $item = int( $array[$i] );

        if ( $item == -1 ) {
            next;
        }

        if ( defined($min) ) {

            if ( $item < $min ) {
                $min      = $item;
                $minIndex = $i;
            }

            if ( $item > $max ) {
                $max      = $item;
                $maxIndex = $i;
            }
        }
        else {
            $min = $item;
            $max = $item;

            $minIndex = $i;
            $maxIndex = $i;
        }
    }

    if ( defined($min) ) {
        if ( $minIndex == $maxIndex ) {
            say "0 $min";
        }
        elsif ( abs( $maxIndex - $minIndex ) == 1 ) {
            say( ( $max - $min ) . " " . $min );
        }
        else {
            my $k = int( ( $max + $min ) / 2 + 0.9 );
            my $m =
              ( ( $max - $k ) > ( $k - $min ) ) ? ( $max - $k ) : ( $k - $min );
            say $m. " " . $k;
        }
    }
    else {
        say "0 0";
    }

    $count--;
}

use 5.020;
use warnings;
use utf8;

# 6 6 3 -1 3 4 3 3 这个情况很难，确定相邻的问题
sub readNext {
    my $line = <STDIN>;
    chomp($line);
    return $line;
}

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

READ: while ($count) {
    &readNext();
    my @array = split / /, &readNext();

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

    my $lastNumber;
    my $lastGap;
    my $lastIndex;
    for my $i ( 0 .. $#array ) {

        my $item = int( $array[$i] );

        if ( $item == -1 ) {
            next;
        }

        if ( defined($lastIndex) ) {
            if ( ( $i - $lastIndex ) == 1 ) {
                my $gap = abs( $item - $lastNumber );
                if ( defined($lastGap) ) {
                    $lastGap = $gap > $lastGap ? $gap : $lastGap;
                }
                else {
                    $lastGap = $gap;
                }
            }
        }

        if ( defined($min) ) {

            if ( $item < $min ) {
                $min      = $item;
                $minIndex = $i;
            }

            if ( $item > $max ) {
                $max      = $item;
                $maxIndex = $i;
            }
        }
        else {
            $min = $item;
            $max = $item;

            $minIndex = $i;
            $maxIndex = $i;
        }

        $lastIndex  = $i;
        $lastNumber = $item;
    }

    # say("max gap :$lastGap, min:$minIndex, max:$maxIndex");

    if ( defined($min) ) {

        if ( $minIndex == $maxIndex ) {
            say "0 $min";
        }

        elsif ( abs( $maxIndex - $minIndex ) == 1 ) {
            say( $lastGap . " " . $min );
        }
        else {
            my $k = int( ( $max + $min ) / 2 + 0.9 );
            my $m =
                ( ( $max - $k ) > ( $k - $min ) )
              ? ( $max - $k )
              : ( $k - $min );
            if ( defined($lastGap) ) {
                say( ( ( $lastGap > $m ) ? $lastGap : $m ) . " " . $k );
            }
            else {
                say $m. " " . $k;
            }
        }
    }
    else {
        say "0 0";
    }

    $count--;
}


chomp (my @s = split /\s*/, <STDIN>);
chomp (my @t = split /\s*/, <STDIN>);

sub incs {
  foreach (reverse (0 .. $#s)) {
    $s[$_] = chr (1 + ord ($s[$_]));
    if ($s[$_] eq '{') {
      $s[$_] = 'a';
    } else {
      return;
    }
  }
}

&incs;

my $sum = 0;
foreach (0 .. $#s) {
  $sum += (ord ($t[$_])) - (ord ($s[$_]));
  last if ($sum > 0);
}

if ($sum > 0) {
  printf "%s\n", join "", @s;
} else {
  printf "No such string\n";
}


#use strict;
use warnings;

$n=<STDIN>;
if($n%2==1) {
    print "7";
}
print "1"x(($n-$n%2*3)/2);

  $amount=<STDIN>;
  $one = $amount/2;
  if ($amount%2!=0){
   print(7);
   $one--;
  }
  for($i= 1;$i<=$one;$i++){
    print (1);
  }

#use strict;
use warnings;

$n=<STDIN>;
if($n>=6) {
    print 9;
} else {
    if($n>=3) {
        print "7";
    } else {
        if($n>=2) {
            print "1";
        }
    }
}

#use strict;
use warnings;

$n=<STDIN>;
if($n>=6)
{
    print "9\n";
} else {
    if($n>=3) {
        print "7\n";
    } else {
        if($n>=2) {
            print "1\n";
        }
    }
}

#use strict;
use warnings;

$n=<STDIN>;
if($n>=6)
{
    print "9";
} else {
    if($n>=3) {
        print "7";
    } else {
        if($n>=2) {
            print "1";
        }
    }
}

  $amount=<STDIN>;
  $nine = $amount/6;
  $amount = $amount%6;
  $seven = $amount/3;
  $amount = $amount%3;
  $one = $amount/2;
  for($i= 1;$i<=$nine;$i++){
    print (9);
  }
  for($i= 1;$i<=$seven;$i++){
    print(7);
  }
  for($i= 1;$i<=$one;$i++){
    print (1);
  }


<STDIN>;
my @arr;
foreach $line ( <STDIN> ) {
    chomp( $line );
    my @values = split(':', $line);
    push(@arr, $values[0]*60 + $values[1]);
}

my @sorterArr = sort {$a <=> $b} @arr;

my $prev = 0;
my @diff;

foreach $time (@sorterArr) {
    push(@diff, $time - $prev);
    $prev = $time;
}

my $res = $diff[0] + 24*60-$prev-1;
foreach $x (@diff) {
    if ($res < $x-1) {
        $res = $x-1;
    }
}
my $hour = $res/60;
my $mins = $res%60;
printf("%02d:%02d\n",$hour,$mins);


chomp(my $n = <>);
my %procs;
for(1..$n) {
  my ($name, $params) = (<> =~ /void\s+(\w+)\s*\(([^\)]*)\)/);
  my (@params) = ($params =~ /\w+/g);
  $procs{$name}{join("\f", @params)} = undef;
}
chomp(my $m = <>);
my %vars;
for(1..$m) {
  my ($type, $name) = (<> =~ /(\w+)\s+(\w+)/);
  $vars{$name} = $type;
}
chomp(my $k = <>);
for(1..$k) {
  my ($name, $params) = (<> =~ /(\w+)\s*\(([^\)]*)\)/);
  my (@params) = map { $vars{$_} } ($params =~ /(\w+)/g);
  if (exists $procs{$name}) {
    my (@temps) = keys %{ $procs{$name} };
    my $res = 0;
    for my $temp (@temps) {
      my @temp_params = split /\f/, $temp;
      (scalar @params) eq (scalar @temp_params) or next;
      my $b = 1;
      for (my $i = 0; $i < @params; $i++) {
        ($temp_params[$i] eq 'T') || ($temp_params[$i] eq $params[$i]) or ($b = 0);
      }
      $res += $b;
    }
    print $res . "\n";
  } else {
    print "0\n";
  }
}


#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION:
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $n = $_;
    my  @array = sort split ' ', <>;
    my $len = @array;
    my @brrby;
    my $cnt = 0;
    for (1..$#array) {
        $brrby[$cnt]++;
        $cnt++ unless ($array[$_] == $array[$_-1]);
    }
    $brrby[$cnt]++;
    my $sum = 0;
    my $max = $brrby[0];
    for (1..$cnt) {
        if ($brrby[$_] > $max) {
            $sum += $max;
            $max = $brrby[$_];
        } else {$sum += $brrby[$_];}
    }
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $ar = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$ar;
    #@$ar = (1..1000);
    my @myvalues = sort {$b <=> $a} values %{$h};
    print @$ar - $myvalues[0];
#    if (@$ar == 1000) {
#        my @myvalues = sort {$b <=> $a} values %{$h};
#        print "print myvalues";
#        print "@myvalues";
#
#    } else {
#        print @$ar - (sort {$b <=> $a} values %{$h})[0];
#    }

#    my @array = split ' ', $_;
#    my $sum = 0;
#    $sum += $_ for @array;
#    print $sum;
#    print $sum - $array[0];
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $ar = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$ar;
#    my @myvalues = sort {$b <=> $a} values %{$h};
#    print @$ar - $myvalues[0];
#     print @$ar - (sort {$b <=> $a} values %{$h})[0];
#    my @array = (1..100);
#    my @brrby = (1..20);
#    $brrby[$#brrby] = 30;
#    print @array - (sort {$b <=> $a} @brrby)[0];
        print @$ar - (sort {$b <=> $a} values %{$h})[0];
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $ar = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$ar;
#    my @myvalues = sort {$b <=> $a} values %{$h};
#    print @$ar - $myvalues[0];
     print @$ar - (sort {$b <=> $a} values %{$h})[0];
#    my @array = (1..100);
#    my @brrby = (1..20);
#    $brrby[$#brrby] = 30;
#    print @array - (sort {$b <=> $a} @brrby)[0];
#        print @$ar - (sort {$b <=> $a} values %{$h})[0];
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION:
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;
use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $n = $_;
    my  @array = sort split ' ', <>;
    my $len = @array;
    my @brrby;# = map 0, (0..$len);
    my $cnt = 0;
    for (1..$#array) {
        $brrby[$cnt]++;
        $cnt++ unless ($array[$_] == $array[$_-1]);
    }
    $brrby[$cnt]++;
    my $sum = 0;
    my $max = $brrby[0];
    for (1..$cnt) {
        if ($brrby[$_] > $max) {
            $sum += $max;
            $max = $brrby[$_];
        } else {$sum += $brrby[$_];}
    }
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION:
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;
use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $n = $_;
    my  @array = sort split ' ', <>;
    my %ash;
    for (@array) {
        $ash{$_}++;
    }
    @array = uniq @array;
    my $sum = 0;
    my $max = $ash{$array[0]};
    shift @array;
    for (@array) {
        my $tmp = $ash{$_};
        if ($tmp > $max) {
            $sum += $max;
            $max = $tmp;
        } else {
            $sum += $tmp;
        }
    }
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION:
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $n = $_;
    my  @array = sort split ' ', <>;
    my %ash;
    for (@array) {
        $ash{$_}++;
    }
    @array = uniq @array;
    my $sum = 0;
    my $max = $ash{$array[0]};
    shift @array;
    for (@array) {
        my $tmp = $ash{$_};
        if ($tmp > $max) {
            $sum += $max;
            $max = $tmp;
        } else {
            $sum += $tmp;
        }
    }
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

<STDIN>;
my $A = [split ' ', <STDIN>];
my ($h, $ans) = ({}, undef);
$h->{$_} = 1 for @$A;
$ans = scalar(keys %$h);
print sprintf("%d\n", $ans);

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $a = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$a;
    print @$a - (sort {$a <=> $b} values %{$h})[0];
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $a = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$a;
    if (@a == 1000) {
        my @values = sort {$b <=> $a} values %{$h};
        print "@values";
    } else {
        print @$a - (sort {$b <=> $a} values %{$h})[0];
    }
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION:
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

use strict;
use warnings;
use utf8;
#use 5.020;
$\ = $/;

while (<>) {
    my $n = $_;
    my  @array = split ' ', <>;
    @array = sort @array;
    my ($cnt, $pos) = (0, 0);

    for (1..$n) {
        if ($array[$pos] < $array[$pos+1]) {
            ++$cnt; shift @array;
        } elsif ($array[$pos] == $array[$pos+1]) {
            if ($array[$pos] == $array[$#array]) {
                last;
            } else {
                ++$cnt; pop @array; shift @array;
            }
        }
        last if (@array == 1 or @array == 0);
    }
    print $cnt;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $a = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$a;
    print @$a - (sort {$b <=> $a} values %{$h})[0];
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $a = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$a;
    my @values = sort {$a <=> $b} values %{$h};
    pop @values;
    my $sum = 0;
    $sum += $_ for @values[0..$#values];
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $ar = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$ar;
    #@$ar = (1..1000);
    if (@$ar == 1000) {
        my @myvalues = sort {$b <=> $a} values %{$h};
        print "print myvalues";
        print "@myvalues";
    } else {
        print @$ar - (sort {$b <=> $a} values %{$h})[0];
    }
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $a = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$a;
    my @values = sort values %{$h};
    pop @values;
    my $sum = 0;
    $sum += $_ for @values[0..$#values];
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;

#!/usr/bin/env perl
#===============================================================================
#
#         FILE: 2.pl
#
#  DESCRIPTION: B. Beautiful Paintings
#
#        FILES: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Lpaste (yesterday2young.github.io), liuxueyang457@163.com
# ORGANIZATION: Solution 1.
#      VERSION: 1.0
#      CREATED: 2016/03/17 21时31分48秒
#     REVISION: ---
#===============================================================================

#use strict;
#use warnings;
#use utf8;
#use List::MoreUtils qw(uniq);
#use 5.020;
$\ = $/;

while (<>) {
    my $a = [split ' ', <>];
    my $h;
    $h->{$_}++ for @$a;
    my @values = sort values %{$h};
    shift @values;
    my $sum = 0;
    $sum += $_ for @values[0..$#values];
    print $sum;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of strings
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (str_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: num_array_in_array_of_array
#      PURPOSE: find if an array is equal to one of a array of arrays
#   PARAMETERS: two array reference
#      RETURNS: return 1 if find an equal array, 0 otherwise
#  DESCRIPTION: all arrays are array of numbers
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_in_array_of_array {
    my ($small_ar, $ar_ar) = @_;
    my $mark = 0;
    for (@{$ar_ar}) {
        $mark = 1;
        unless (num_array_cmp($small_ar, $_)) {
            $mark = 0;
            next;
        }
        last if $mark;
    }
    $mark;
}

#===  FUNCTION  ================================================================
#         NAME: str_array_cmp
#      PURPOSE: compare two array of strings
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub str_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] eq $_[1][$_]); }
    return 1;
} ## --- end sub str_array_cmp

#===  FUNCTION  ================================================================
#         NAME: num_array_cmp
#      PURPOSE: compare two array of numbers
#   PARAMETERS: two array reference
#      RETURNS: return 1 if two array are equal, 0 otherwise
#  DESCRIPTION: ????
#       THROWS: no exceptions
#     COMMENTS: none
#     SEE ALSO: n/a
#===============================================================================
sub num_array_cmp {
    return 0 if @{$_[0]} != @{$_[1]};
    my $len = @{$_[0]};
    for (0..$len-1) { return 0 unless ($_[0][$_] == $_[1][$_]); }
    return 1;
} ## --- end sub num_array_cmp

exit 0;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	my %h;

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

	$_ = join '', sort grep { $h{ $_ } == 1 } keys %h;

	$_ .= join '', sort grep { $h{ $_ } == 2 } keys %h;
	$_ .= join '', sort grep { $h{ $_ } == 2 } keys %h;

	print;
	}

$\ = $/;

<>;

print sort m/./g while <>

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

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

	my( $n, $m, $r, $c ) = split;

	$debug and print "[$n, $m, $r, $c]";

	my %row;
	my %col;

	my $ans;

	for my $i ( 0 .. $n - 1 ){
		$_ = <>, chomp;

		next if defined $ans;

		m/B/ and $row{ $i } = 1;

		while( m/B/g ){
			$col{ $-[ 0 ] } = 1;

			if( $r - 1 == $i and $-[ 0 ] == $c - 1 ){
				$ans = 0;
				last;
				}
			}
		}

	if( defined $ans ){
		print $ans;
		}
	elsif( %row or %col ){
		if( $row{ $r - 1 } or $col{ $c - 1 } ){
			print 1;
			}
		else{
			print 2;
			}
		}
	else{
		print -1;
		}
	}


$n = <>;
@n = sort {$a <=> $b} split ' ', <>;
$sum += $_ for @n;
$small = int($sum / $n);
$bigger = $sum % $n;
@w = ( ($small) x ($n - $bigger), ($small + 1) x $bigger );
$sum = 0;
for (@n){
$sum += abs($_ - shift @w)
}
print $sum >> 1

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

my $sum = 0;
for (@arr) {
    $sum += $_; }
@arr = sort { $a <=> $b } @arr;

my $lower = $n - ($sum % $n);
my $upper = ($sum % $n);
my $avg = int ($sum / $n);

my $seconds = 0;
for ( my $i = 0; $i < $lower; $i += 1 ) {
    $seconds += abs ((shift @arr) - $avg);}

for ( my $j = 0; $j < $upper; $j += 1 ) {
    $seconds += abs ((shift @arr) - ($avg+1));}

$seconds /= 2;
print "$seconds\n";

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

my $sum = 0;
for (@arr) {
    $sum += $_; }
@arr = sort @arr;

my $lower = $n - ($sum % $n);
my $upper = ($sum % $n);
my $avg = int ($sum / $n);

my $seconds = 0;
for (1..$lower) {
    $seconds += abs ((shift @arr) - $avg);}

for (1..$upper) {
    $seconds += abs ((shift @arr) - ($avg+1));}

$seconds /= 2;
print "$seconds\n";

$n = <>;
@n = sort {$a <=> $b} split ' ', <>;
$sum += $_ for @n;
$small = int($sum / $n);
$bigger = $sum % $n;
@w = ( ($small) x ($n - $bigger), ($small + 1) x $bigger );
for (@n){
$sum += abs($_ - shift @w)
}
print $sum >> 1


<>;
	print qw(YES NO)[
			!! join '',
				map {
					join '',
						map {
							map s/11//gr,
								map y/0//dr,
									split /11/
						}
							map { split /00/ }
								join '',
									map $_ % 2, split
				} split /\b0/, <>
		]

print qw(YES NO)[
			!! join '',
				map {
					join '',
						map {
							map s/11//gr,
								map y/0//dr,
									split /11(?!0)/
						}
							map { split /00(?!1)/ }
								join '',
									map $_ % 2, split
				} split /\b0/, <>
		]


#!/usr/bin/env perl

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

use Carp;

# essential
my @tokens = ();

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

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

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

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

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

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

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

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

    $$ref = !$$ref;
}

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

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

# solution

my $n = read_token;

for (1..$n) {
    my ($a, $b) = split q{ }, read_line;
    if ($a == $b) {
        say 0;
        next;
    }
    if ($a > $b) {
        if (even($a - $b)) {
            say 1;
        }
        else {
            say 2;
        }
    }
    else {
        if (odd($b - $a)) {
            say 1;
        }
        else {
            say 2;
        }
    }
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	if( $A == $B ){
		print 0;
		}

	elsif( $A % 2 == 0 and $B % 2 == 1 and $A < $B ){
		print 1;
		}
	elsif( $A % 2 == 0 and $B % 2 == 0 and $A < $B ){
		print 2;
		}
	elsif( $A % 2 == 1 and $B % 2 == 1 and $A < $B ){
		print 2;
		}
	elsif( $A % 2 == 1 and $B % 2 == 0 and $A < $B ){
		print 1;
		}

	elsif( $A % 2 == 0 and $B % 2 == 1 and $A > $B ){
		print 2;
		}
	elsif( $A % 2 == 0 and $B % 2 == 0 and $A > $B ){
		print 1;
		}
	elsif( $A % 2 == 1 and $B % 2 == 1 and $A > $B ){
		print 1;
		}
	elsif( $A % 2 == 1 and $B % 2 == 0 and $A > $B ){
		print 2;
		}

	}


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

#open (my $F1, "<", "a") or die "a: $!";
#my $num=0;
my %Q=();
my $num = <>;
chomp($num);
my $line2 = <>;
chomp($line2);
my @snacks = split (" ", $line2);
my $len = scalar @snacks;
if ($num != $len ) { print "Error: $num != $len\n";exit(1);}
#print "num=$num\n";
#print "snacks:@snacks\n";
#close($F1);
##############################################
my $wait_for = $num;
for (my $i=0;$i < $num;$i++) {
   if ($wait_for == $snacks[$i] ) {
      print "$wait_for";
      $wait_for--;
      #while (my $ele = pop (@Q)) {
      while ($Q{$wait_for}) {
         #if ($wait_for == $ele) {
            print " $wait_for";
            $wait_for--;

      }
      print "\n";
   } else {
      print "\n";
      $Q{$snacks[$i]} = 1;
      #push (@Q,$snacks[$i]);
   }
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	chomp;

	@_ = (-1, 1 .. $_);

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

	my $last = $_;

	for my $i ( @A ){

		$_[ $i ] = 0;

		my @out;

		while( $_[ $last ] == 0 ){
			push @out, $last --
			}

		print "@out";
		}

	$debug and print '-' x 10;
	}

use strict;
use warnings;

use feature qw/say/;
{
    my $n = <>;
    chomp $n;
    my %has;
    my $temp_heap = $n;
    my @values;
    {
        local $/ = ' ';
        for (my $i=1;$i<=$n;$i++){
            if ($i == $n){
                $/ = "\n";
            }
            my $str = <>;
            chomp $str;
            $has{$str} = 1;

            while (defined $has{$temp_heap} ){
                print $temp_heap . " ";
                $temp_heap--;
            }
            print "\n";
        }
    }
}

use strict;
use warnings;

use feature qw/say/;
{
    my $n = <>;
    chomp $n;

    my $temp_heap = $n+1;
    my @values;
    {
        local $/ = ' ';
        for (my $i=1;$i<=$n;$i++){
            if ($i == $n){
                $/ = "\n";
            }
            my $str = <>;
            chomp $str;
            push @values, $str;
            @values = reverse sort @values;
            while (defined $values[0] && $values[0]+1 == $temp_heap ){
                my $new_heap = shift @values;
                print $new_heap . " ";
                $temp_heap = $new_heap;
            }
            print "\n";
        }
    }
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	chomp;

	@_ = (-1, 1 .. $_);

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

	my $c = $_;
	my $last = $_;

	for my $i ( reverse 1 .. @_ - 1 ){
		my $num = shift @A;
		$_[ $num ] = 0;
		if( $num >= $i ){ $c-- }
		$debug and print "  $c";
		if( $num != $i and $_[ $i ] == 0 ){ $c-- }
		$debug and print "    $c";
		if( $c < $i ){
			print join ' ', reverse $c + 1 .. $last;
			$last = $c;
			}
		else{
			print ''
			}
		}

	$debug and print '-' x 10;
	}


#!/usr/bin/perl

($n, $m) = split /\s/, <>;

$name = <>;
chomp $name;

@name_letters = split //, $name;

for my $i (0..$#name_letters)
{
    $h{@name_letters[$i]} = [] if ! exists   $h{@name_letters[$i]};

    push @{$h{@name_letters[$i]}}, $i;
}

for (1..$m)
{
    my $xy = <>;
    chomp $xy;
    (my $x, my $y) = split /\s/, $xy;

    if($x ne $y)
    {
    if(exists $h{$x} and exists $h{$y})
        {
        my $ar_ref = $h{$x};
            $h{$x} = $h{$y};
            $h{$y} = $ar_ref;
    }
        elsif(exists $h{$x})
        {
        my $ar_ref = $h{$x};
            delete  $h{$x};
            $h{$y} = $ar_ref;
        }
        elsif(exists $h{$y})
        {
        my $ar_ref = $h{$y};
            delete  $h{$y};
            $h{$x} = $ar_ref;
        }
    }
}

for my $letter (keys %h)
{
    for my $i (@{$h{$letter}})
    {
    @name_letters[$i] = $letter;
    }
}

$name = join '', @name_letters;

print $name, "\n";


use strict;
use warnings;

my $case = <STDIN>;

for my $i(0..$case - 1){
	my $num = <STDIN>;
	my $sum = 0;
	my $exp = 0;

	$sum = ($num * ($num + 1)) >> 1;
	for my $j(0..$num - 1){
		if($num < (1 << $j)){
			$exp = $j;
			last;
		}
	}

	$sum -= (2 * ((1 << $exp) - 1));

	if($num == 1){
		print "-1\n";
	}elsif($num == 2){
		print "-3\n";
	}else{
		print $sum, "\n";
	}
}

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use bignum;

my $count=<>;
my $sum;
my $temp;

for (my $i=0;$i<$count;$i+=1)
{
	$temp=1;
	my $number=<>;
	$sum = (1 + $number) * ($number / 2);

	while ($temp <= $number)
	{
		$sum = $sum - ($temp * 2);
		$temp = $temp * 2;
	}
	print $sum ;
	print "\n" ;
}

#!/usr/bin/perl -w
use bigint;

sub doit {
    my $n = $_[0];
    my $next_power_of_two = 1;
    while ($next_power_of_two <= $n) {
        $next_power_of_two *= 2;
    }
    $n * ($n + 1) / 2 - 2 * ($next_power_of_two - 1);
}

my $t = <>;
for (1..$t) {
    my $n = <>;
    print doit($n), "\n";
}

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

my $count=<>;
my $sum;
my $temp;

for (my $i=0;$i<$count;$i+=1)
{
	$temp=1;
	my $number=<>;
	$sum = (1 + $number) * ($number / 2);

	while ($temp <= $number)
	{
		$sum = $sum - ($temp * 2);
		$temp = $temp * 2;
	}
	print $sum ;
	print "\n" ;
}

use strict;
use warnings;

my $case = <STDIN>;

for my $i(0..$case){
	my $num = <STDIN>;
	my $sum = 0;
	my $exp = 0;

	$sum = ($num * ($num + 1)) >> 1;
	for my $j(0..$num - 1){
		if($num < (1 << $j)){
			$exp = $j;
			last;
		}
	}

	$sum -= (2 * ((1 << $exp) - 1));

	if($num == 1){
		print "-1\n";
	}elsif($num == 2){
		print "-3\n";
	}else{
		print $sum, "\n";
	}
}

use strict;
use warnings;

my $case = <STDIN>;

for my $i(0..$case){
	my $num = <STDIN>;
	my $sum = 0;
	my $exp = 0;

	$sum = ($num * ($num + 1)) >> 1;
	for my $j(0..$num){
		if($num < (1 << $j)){
			$exp = $j;
			last;
		}
	}

	$sum -= (2 * ((1 << $exp) - 1));

	if($num == 1){
		print "-1\n";
	}elsif($num == 2){
		print "-3\n";
	}else{
		print $sum, "\n";
	}
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	chomp;

	my $del = 0;

	@_ = ();

	for( split // ){
		if( @_ > 0 and $_ eq $_[ -1 ] ){
			pop @_;
			$del ++;
			}
		else{
			push @_, $_;
			}
	}

	print $del % 2 ? "Yes" : "No";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	chomp;

	my $del = 0;

	@_ = ();

	for( split // ){
		push @_, $_;
		if( @_ > 1 and $_[ -2 ] eq $_[ -1 ] ){
			pop @_ for 1 .. 2;
			$del ++;
			}
	}

	print $del % 2 ? "Yes" : "No";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	chomp;

	my $del = 0;

	my $pos = pos;

	( pos ) = $pos < 0 ? 0 : $pos while s/\G.*?\K(.)\1(?{ $del ++; $pos = ( pos ) - 3; })//x;

	print $del % 2 ? "Yes" : "No";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	chomp;

	my $del = 0;

	my $pos = pos;

	( pos ) = $pos while s/\G.*?\K(.)\1(?{ $del ++; $pos = ( pos ) - 3; })//x;

	print $del % 2 ? "Yes" : "No";
	}


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

my $n; my $l;
my @num;
sub valid{
    my $d = shift;
    my $last = 0;
    for my $i(0..$n-1){
        return 0 if $num[$i]-$d > $last;
        $last = $last > $num[$i] + $d ? $last : $num[$i] + $d;
    }
    return $last >= $l;
}

($n,$l) = split ' ',<>;
@num = split ' ',<>;
@num = sort {$a <=> $b} @num;
my ($start,$len) = (0,$l+1);
while($len > 1e-11){
    $len /= 2.0;
    my $mid = $start + $len;
    $start = $mid if(!valid($mid)) ;
}

printf "%0.11f\n",$start;

#!/usr/bin/perl
use v5.10;
# use List::Util qw(first max maxstr min minstr reduce shuffle sum);

sub max {
    my ($x, $y) = (shift, shift);
    return $x>$y ? $x:$y;
}

($n, $l) = split / /, <>;
@a = sort {$a <=> $b} split / /, <>;
$ans = max ($a[0]*2, ($l-$a[-1])*2);
$ans = max $ans,$a[$_]-$a[$_-1] foreach (1..$n-1);
say $ans/2;

#!/usr/bin/perl
use v5.10;
#use List::Util qw(first max maxstr min minstr reduce shuffle sum);

sub max {
    my ($x, $y) = (shift, shift);
    return $x>$y ? $x:$y;
}

($n, $l) = split / /, <>;
@a = sort {$a <=> $b} split / /, <>;
$ans = max ($a[0]*2, (l-$a[-1])*2);
$ans = max ($ans,$a[$_]-$a[$_-1]) foreach (1..$n-1);
say $ans/2;

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

my $n; my $l;
my @num;
sub valid{
    my $d = shift;
    my $last = 0;
    for my $i(0..$n-1){
        return 0 if $num[$i]-$d > $last;
        $last = $last > $num[$i] + $d ? $last : $num[$i] + $d;
    }
    return $last >= $l;
}

($n,$l) = split ' ',<>;
@num = split ' ',<>;
@num = sort {$a <=> $b} @num;
my ($low,$high) = (0,$l+1);
while($high - $low > 1e-8){
    my $mid = ($low + $high) / 2;
    if(valid($mid)){
        $high = $mid;
    }
    else{
        $low = $mid;
    }
}

printf "%0.11   f\n",$low;


sub game($$){
    my $r=($_[0]-1)/$_[1];
    while($r>=1){
        if ($r == 1){
            return 1;
        }
        $r/=$_[1];
    }
    return 0;
}
$\=$/;
my $max=0;
$n=<STDIN>;
my $maxn=0;
@sculptures=(0,split(" ",<STDIN>));

$pointSTOP=$n/3;

for ($index=1;$index<$n+1;$index++){

    $max+=$sculptures[$index];

}
#print "pointSTOP=".$pointSTOP."   n=".$n."\n";
for ($delims=2;(($delims<=$pointSTOP));$delims++){
    if (($n%$delims==0)){
#       print "delims="."$delims";

        for ($index=1;(($index<=$delims));$index++){
            #if (game($index,$delims)==0){
                #print "        index="."$index";
                $maxn=0;
                for ($subindex=$index-1;$subindex<$index+$n-1;($subindex+=$delims)){
                #   print "         subindex=".$subindex."    delims=".$delims."    sculpturerank=".$sculptures[$subindex%($n)+1];
                    $maxn+=$sculptures[$subindex%($n)+1];
                #   print "         maxn=".$maxn;
                }
                #print "        max=".$max."  maxn=".$maxn;
                if ($max<$maxn){
                $max=$maxn;
                }
            #}
        }
    }

}

print $max;

#!perl
sub sum{
	my $step = shift;
	my $start = shift;
	my $s = 0;
	for(my $i = $start; $i <= $#_; $i += $step){
		$s += $_[$i];
	}
	$s;
}
my $n = <STDIN>;
if ($n <= 2) {
	die "wrong input\n";
}
my @possible;
$m = sqrt($n);
for (my $i = 2; $i <= $m; $i++) {
	if (0 == $n % $i) {
		$d = int $n / $i;
		push @possible, $i if $d >= 3;
		push @possible, $d if $i >= 3;
	}
}

my @statues = map {int $_} grep {/^-?\d+$/} split / +/, <STDIN>;
@statues = @statues[0..($n-1)];
my $max = sum 1, 0, @statues;
for my $k(@possible){
	for (0..($k-1)) {
		my $s = sum $k, $_, @statues;
		$max = $s if ($s > $max);
	}
}
print $max."\n";

#!/usr/bin/perl
# Codeforces Practice - 26 May 2012
use strict;
use warnings;
use Math::Complex;

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

my @tis = split(" ",<STDIN>);

my $tSum =0;
$tSum += $_ for @tis; #summing up the array

if($n<6) {
    print $tSum;
}

else {
    my @sums = ($tSum);
    my $count;

    #................ Calculate factors of $n

    my @factors;

    for(2..sqrt $n){
        if($n % $_ == 0){
            my $div = $n/$_;
            push(@factors,$_);
            unless($div == $_){
                push(@factors,$div);
    }}}

    @factors = sort {$a<=>$b} @factors;

    #................ Finished calculating factors of $n

    foreach my $factor (@factors){
        next if ($n / $factor < 3);
        my @factSums = ();
        my $count = 0;
        foreach my $t (@tis){
            @factSums[$count++ % $factor] += $t;
        }
        @sums = (@sums,@factSums);
    }

    my $max = $sums[0];
$_ > $max and $max = $_
    for @sums;

print $max;
}

#!/usr/bin/perl -w

use strict;
use List::Util qw(sum max);

my $n = <>;
my @t = split ' ', <>;

my @foos = ();

for my $m (1 .. $n / 3) {
    next unless $n % $m == 0;
    for my $i (0 .. $m - 1) {
        my @foo = map { $t[$i + $m * $_] } (0 .. $n / $m - 1);
        push @foos, sum @foo;
    }
}

print max @foos;

my ($n) = split(/\s/, <STDIN>);

my @t = split(/\s/, <STDIN>);

sub calc {
    my (@a) = @_;
    my $sum = 0;
    for my $e (@a) {
        $sum += $e;
    }
    if (scalar @a % 2 == 0 && scalar @a > 4) {
        my @b;
        my @c;
        for (my $i = 0; $i < scalar @a; $i++) {
            if ($i % 2 == 0) {
                push(@b, $a[$i]);
            } else {
                push(@c, $a[$i]);
            }
        }
        my $odd_sum = calc(@b);
        my $even_sum = calc(@c);
        $sum = $odd_sum if ($odd_sum > $sum);
        $sum = $even_sum if ($even_sum > $sum);
    }
    return $sum;
}

print calc(@t);

#!/usr/bin/perl
# Codeforces Practice - 26 May 2012
use strict;
use warnings;

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

my @tis = split(" ",<STDIN>);
my $tSum =0;
$tSum += $_ for @tis;
if($n%2==1 || $n<6) {print $tSum;}
else {
my ($sum1,$sum2) = (0,0);
my $toggle = 0;
foreach my $t (@tis) {
    if($toggle){
        $sum1 += $t;
    } else {
        $sum2 += $t;
    }
    $toggle = not $toggle;
}
if($sum1 >= $tSum && $sum1 >= $sum2){print $sum1;}
elsif($sum2 >= $tSum && $sum2 >= $sum1){print $sum2;}
else {print $tSum;}
}

#!/usr/bin/perl
# Codeforces Practice - 26 May 2012
use strict;
use warnings;

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

my @tis = split(" ",<STDIN>);
my $tSum =0;
$tSum += $_ for @tis;
if($n%2==1) {print $tSum;}
else {
my ($sum1,$sum2) = (0,0);
my $toggle = 0;
foreach my $t (@tis) {
    if($toggle){
        $sum1 += $t;
    } else {
        $sum2 += $t;
    }
    $toggle = not $toggle;
}
if($sum1 >= $tSum && $sum1 >= $sum2){print $sum1;}
elsif($sum2 >= $tSum && $sum2 >= $sum1){print $sum2;}
else {print $tSum;}
}

#!/usr/bin/perl -w

use strict;
use List::Util qw(sum max);

my $n = <>;
my @t = split ' ', <>;

my @foos = ();

for my $m (1 .. $n) {
    next unless $n % $m == 0;
    for my $i (0 .. $m - 1) {
        my @foo = map { $t[$i + $m * $_] } (0 .. $n / $m - 1);
        push @foos, sum @foo;
    }
}

print max @foos;

sub game($$){
    my $r=($_[0]-1)/$_[1];
    while($r>=1){
        if ($r == 1){
            return 1;
        }
        $r/=$_[1];
    }
    return 0;
}
$\=$/;
my $max=0;
$n=<STDIN>;
my $maxn=0;
@sculptures=(0,split(" ",<STDIN>));

$pointSTOP=$n/3;

for ($index=1;$index<$n+1;$index++){

    $max+=$sculptures[$index];

}
#print "pointSTOP=".$pointSTOP."   n=".$n."\n";
for ($delims=2;(($delims<$pointSTOP));$delims++){
    if (($n%$delims==0)){
#       print "delims="."$delims";

        for ($index=1;(($index<=$delims));$index++){
            #if (game($index,$delims)==0){
                #print "        index="."$index";
                $maxn=0;
                for ($subindex=$index-1;$subindex<$index+$n-1;($subindex+=$delims)){
                #   print "         subindex=".$subindex."    delims=".$delims."    sculpturerank=".$sculptures[$subindex%($n)+1];
                    $maxn+=$sculptures[$subindex%($n)+1];
                #   print "         maxn=".$maxn;
                }
                #print "        max=".$max."  maxn=".$maxn;
                if ($max<$maxn){
                $max=$maxn;
                }
            #}
        }
    }

}

print $max;

sub game($$){
    my $r=($_[0]-1)/$_[1];
    while($r>=1){
        if ($r == 1){
            return 1;
        }
        $r/=$_[1];
    }
    return 0;
}
$\=$/;
my $max=0;
$n=<STDIN>;
my $maxn=0;
@sculptures=(0,split(" ",<STDIN>));

$pointSTOP=$n/3+1;

for ($index=1;$index<$n+1;$index++){

    $max+=$sculptures[$index];

}
#print "pointSTOP=".$pointSTOP."   n=".$n."\n";
for ($delims=2;(($delims<$pointSTOP));$delims++){
    if (($n%$delims==0)){
        #print "delims="."$delims";

        for ($index=1;(($index<=$delims));$index++){
            #if (game($index,$delims)==0){
            #   print "     index="."$index";
                $maxn=0;
                for ($subindex=$index-1;$subindex<$index+$n-1;($subindex+=$delims)){
                    #print "            subindex=".$subindex."    delims=".$delims."    sculpturerank=".$sculptures[$subindex%($n)+1];
                    $maxn+=$sculptures[$subindex%($n)+1];
                    #print "            maxn=".$maxn;
                }
                #print "        max=".$max."  maxn=".$maxn;
                if ($max<$maxn){
                $max=$maxn;
                }
            #}
        }
    }

}

print $max;

use strict;

my ($n) = split(/\s/, <STDIN>);

my @t = split(/\s/, <STDIN>);

sub calc {
    my (@a) = @_;
    my $sum = 0;
    for my $e (@a) {
        $sum += $e;
    }
    if (scalar @a % 2 == 0) {
        my @b;
        my @c;
        for (my $i = 0; $i < scalar @a; $i++) {
            if ($i % 2 == 0) {
                push(@b, $a[$i]);
            } else {
                push(@c, $a[$i]);
            }
        }
        my $sum_even = calc(@b);
        my $sum_odd = calc(@c);
        $sum = $sum_even if ($sum < $sum_even);
        $sum = $sum_odd if ($sum < $sum_odd);
    }
    return $sum;
}

print calc(@t) . "\n";

my ($n) = split(/\s/, <STDIN>);

my @t = split(/\s/, <STDIN>);

my @res = ();

sub calc {
    my (@a) = @_;
    my $sum = 0;
    for my $e (@a) {
        $sum += $e;
    }
    push (@res, $sum);
    if (scalar @a % 2 == 0) {
        my @b;
        my @c;
        for (my $i = 0; $i < scalar @a; $i++) {
            if ($i % 2 == 0) {
                push(@b, $a[$i]);
            } else {
                push(@c, $a[$i]);
            }
        }
        calc(@b);
        calc(@c);
    }
}

calc(@t);

my $max = $res[0];
for $e (@res) {
    $max = $e if ($max < $e);
}

print $max;


#!/usr/bin/perl

$n = <STDIN>;
@b = split ' ', <STDIN>;
$ans = 0;
$delta = 0;
foreach (@b) {
    $ans += abs $_ - $delta;
    $delta += $_ - $delta;
}
print $ans, "\n";


<>;

$h{ $_ } ++ or $L = $_ for reverse split ' ', <>;

print $L

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my %h;
	my $last;

	for( reverse split ' ', <> ){
		not exists $h{ $_ } and $last = $_;
		$h{ $_ } ++;
		}

	print $last;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $m, $k ) = split;
	my @A = 0 .. 1e6;
	map { $A[ $_ ] = -1 } split ' ', <>;

	my $x = 1;

	for (1 .. $k){

		my( $u, $v ) = split ' ', <>;

		if( $A[ $x ] == -1 ){
			next;
			}

		$x == $u and $x = $v and next;
		$x == $v and $x = $u and next;
		}

	print $x;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $m, $k ) = split;
	my @A = 0 .. 1e6;
	map { $A[ $_ ] = -1 } split ' ', <>;

	my $x = 1;

	for (1 .. $k){

		my( $u, $v ) = split ' ', <>;

		if( $A[ $x ] == -1 ){
			last;
			}

		$x == $u and $x = $v and next;
		$x == $v and $x = $u and next;
		}

	print $x;
	}


use strict;

my ($n, $m, @pass, @fail);

($n,$m) = split / /, <>;
@pass = split / /, <>;
@fail = split / /, <>;
chomp(@pass), chomp(@fail);

@pass = sort {$a <=> $b} @pass;
@fail = sort {$a <=> $b} @fail;

(print "-1") and exit if (($pass[-1] >= $fail[0]) || (2 * $pass[0] >= $fail[0]));

print ((2 * $pass[0] > $pass[-1]) ? 2 * $pass[0] : $pass[-1]);

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

sub max
{
    my $max = $_[0];
    foreach (@_)
    {
        $max = $_ if $_ > $max;
    };
    return $max;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

$possible = 0 if max(@right) >= min(@wrong);

if ($possible)
{
    if (max(@right) == min(@right)) {
        print min(min(@wrong) - 1, $v)
    } else {
        print min(min(@wrong) - 1, max(@right, $v))
    }
}
else
{
    print -1;
}

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

@a=split/ /,<>;
@b=split/ /,<>;

$ib=min(@b);
$ia=min(@a);
$aa=max(@a);

$TL=max($ia*2,$aa);
print $TL<$ib?$TL:-1;
n();

}

#***********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
}

use strict;

my ($n, $m, @pass, @fail);

($n,$m) = split / /, <>;
@pass = split / /, <>;
@fail = split / /, <>;
chomp(@pass), chomp(@fail);

@pass = sort @pass;
@fail = sort @fail;

(print "-1") and exit if (($pass[-1] >= $fail[0]) || (2 * $pass[0] >= $fail[0]));

print ((2 * $pass[0] > $pass[-1]) ? 2 * $pass[0] : $pass[-1]);

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

sub max
{
    my $max = $_[0];
    foreach (@_)
    {
        $max = $_ if $_ > $max;
    };
    return $max;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

if ($possible)
{
    if (max(@right) == min(@right)) {
        print min(min(@wrong) - 1, $v)
    } else {
        print min(min(@wrong) - 1, max(@right))
    }
}
else
{
    print -1;
}

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

if ($possible)
{
    print min(@wrong) - 1;
}
else
{
    print -1;
}

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

sub max
{
    my $max = $_[0];
    foreach (@_)
    {
        $max = $_ if $_ > $max;
    };
    return $max;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

$possible = 0 if max(@right) >= max(@wrong);

if ($possible)
{
    if (max(@right) == min(@right)) {
        print min(min(@wrong) - 1, $v)
    } else {
        print min(min(@wrong) - 1, max(@right, $v))
    }
}
else
{
    print -1;
}

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

sub max
{
    my $max = $_[0];
    foreach (@_)
    {
        $max = $_ if $_ > $max;
    };
    return $max;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

if ($possible)
{
    if (max(@right) == max(@wrong)) {
        print min(min(@wrong) - 1, $v)
    } else {
        print min(min(@wrong) - 1, max(@right))
    }
}
else
{
    print -1;
}

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

sub max
{
    my $max = $_[0];
    foreach (@_)
    {
        $max = $_ if $_ > $max;
    };
    return $max;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

if ($possible)
{
    print min(min(@wrong) - 1, $v)
}
else
{
    print -1;
}

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

sub min
{
    my $min = $_[0];
    foreach (@_)
    {
        $min = $_ if $_ < $min;
    };
    return $min;
}

sub max
{
    my $max = $_[0];
    foreach (@_)
    {
        $max = $_ if $_ > $max;
    };
    return $max;
}

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

my @right = split(' ', <>);
my @wrong = split(' ', <>);

my $v = min(@right) * 2;

my $possible = 1;

foreach my $time (@wrong)
{
    if ($time <= $v)
    {
        $possible = 0;
        last;
    }
}

if ($possible)
{
    if (max(@right) == min(@right)) {
        print min(min(@wrong) - 1, $v)
    } else {
        print min(min(@wrong) - 1, max(@right, $v))
    }
}
else
{
    print -1;
}