There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

NoteIn the first sample, the optimal order is: 10, 20, 30, 40, 50.In the second sample, the optimal order is: 100, 200, 100, 200.

#!/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;

#!/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:
#      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;
#    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;

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


As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes. The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO». You can output any letter in lower case or in upper case.

NoteIn first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){

	@_ = split ' ', <>;

	my $i = 0;

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

#	print "$_ -> $h{$_}" for sort keys %h;

	my %g = map { $_ => 1 } @_;
	my @g = sort keys %g;

#	print "[@g]";

	my $ok = 0;

	while( @g ){
		my $try = shift @g;
		if( $h{ $h{ $h{ $try } } } == $try ){
			$ok = 1;
			}
		}

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

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){

	@_ = split ' ', <>;

	my $i = 0;

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

#	print "$_ -> $h{$_}" for sort keys %h;

	my %g = map { $_ => 1 } @_;
	my @g = sort keys %g;

#	print "[@g]";

	my $ok = 0;

	while( @g ){
		my $try = shift @g;
		if( $h{ $h{ $h{ $try } } } == $try ){
			$ok = 1;
			}
		}

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


You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You can perform operations on it.In one operation you can replace any element of the array $$$a_i$$$ with $$$\lfloor \frac{a_i}{2} \rfloor$$$, that is, by an integer part of dividing $$$a_i$$$ by $$$2$$$ (rounding down).See if you can apply the operation some number of times (possible $$$0$$$) to make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$ —that is, so that it contains all numbers from $$$1$$$ to $$$n$$$, each exactly once.For example, if $$$a = [1, 8, 25, 2]$$$, $$$n = 4$$$, then the answer is yes. You could do the following:  Replace $$$8$$$ with $$$\lfloor \frac{8}{2} \rfloor = 4$$$, then $$$a = [1, 4, 25, 2]$$$.  Replace $$$25$$$ with $$$\lfloor \frac{25}{2} \rfloor = 12$$$, then $$$a = [1, 4, 12, 2]$$$.  Replace $$$12$$$ with $$$\lfloor \frac{12}{2} \rfloor = 6$$$, then $$$a = [1, 4, 6, 2]$$$.  Replace $$$6$$$ with $$$\lfloor \frac{6}{2} \rfloor = 3$$$, then $$$a = [1, 4, 3, 2]$$$.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) —the number of test cases. Each test case contains exactly two lines. The first one contains an integer $$$n$$$ ($$$1 \le n \le 50$$$), the second one contains integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

For each test case, output on a separate line:   YES if you can make the array $$$a$$$ become a permutation of numbers from $$$1$$$ to $$$n$$$,  NO otherwise.  You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

NoteThe first test case is explained in the text of the problem statement.In the second test case, it is not possible to get a permutation.

#!/usr/bin/perl

use warnings;
use strict;

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

<>;

while(<>){
	chomp;

	$debug and print '-' x 15;

	my $n = $_;

	@_ = split ' ', <>;

	for( @_ ){
		while( $_ > $n ){
			$_ >>= 1;
			}
		}

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

	$debug and print "@_";

	my $fail = 0;

	for my $i ( reverse 1 .. $n ){

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

		$debug and print "$i: @_";

		if( $_[ 0 ] == $i ){
			shift @_;

			my $j = 0;

			while( $_[ $j ] and $_[ $j ] == $i ){
				$_[ $j ] >>= 1;
				$j ++;
				}
			}
		else{
			$fail = 1;
			last;
			}
		}

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


You are given a string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in it no more than twice.Your task is to rearrange the letters in the string in such a way that for each pair of letters that appear exactly twice, the distance between the letters in the pair is the same. You are not allowed to add or remove letters.It can be shown that the answer always exists. If there are multiple answers, print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of testcases. Each testcase consists of a non-empty string $$$s$$$, consisting of lowercase Latin letters. Every letter appears in the string no more than twice. The length of the string doesn't exceed $$$52$$$.

For each testcase, print a single string. Every letter should appear in it the same number of times as it appears in string $$$s$$$. For each pair of letters that appear exactly twice, the distance between the letters in the pair should be the same. If there are multiple answers, print any of them.

NoteIn the first testcase of the example, the only letter that appears exactly twice is letter 'l'. You can rearrange the letters arbitrarily, since there are no distances to compare.In the second testcase of the example, the letters that appear exactly twice are 'a', 'b' and 'c'. Initially, letters 'a' are distance $$$6$$$ apart, letters 'b' are distance $$$4$$$ apart and letters 'c' are distance $$$2$$$ apart. They are not the same, so we have to rearrange the letters. After rearrangement, letters 'a' are distance $$$2$$$ apart, letters 'b' are distance $$$2$$$ apart and letters 'c' are distance $$$2$$$ apart. They are all the same, so the answer is valid.In the third testcase of the example, there are no letters that appear exactly twice. Thus, any rearrangement is valid. Including not changing the string at all.

$\ = $/;

<>;

print sort m/./g while <>

#!/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;
	}

#!/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;
		}
	}


Given simple (without self-intersections) n-gon. It is not necessary convex. Also you are given m lines. For each line find the length of common part of the line and the n-gon.The boundary of n-gon belongs to polygon. It is possible that n-gon contains 180-degree angles.

The first line contains integers n and m (3 ≤ n ≤ 1000;1 ≤ m ≤ 100). The following n lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct. The following m lines contain line descriptions. Each of them contains two distict points of a line by their coordinates. All given in the input coordinates are real numbers, given with at most two digits after decimal point. They do not exceed 105 by absolute values.

Print m lines, the i-th line should contain the length of common part of the given n-gon and the i-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

use strict;

package calc;

sub new {
	my $self = { len => 0, start => undef, in_border => 0, inside => 0 };
	bless $self;
}

sub enter {
	my ($calc, $coord) = @_;
	$calc->{start} = $coord;
    $calc->{inside} = 1;
}

sub leave {
	my ($calc, $coord) = @_;
	$calc->{len} += abs($coord - $calc->{start});
    $calc->{start} = undef;
    $calc->{inside} = 0;
}

sub enter_leave {
	my ($calc, $coord) = @_;
	if ($calc->{inside}) {
		$calc->leave($coord);
	} else {
		$calc->enter($coord);
	}
}

sub enter_border {
	my ($calc, $coord, $level) = @_;
	$calc->{border_level} = $level;
	$calc->{border_inside} = $calc->{inside};
	$calc->{in_border} = 1;
	$calc->enter($coord) unless $calc->{inside};
}

sub leave_border {
	my ($calc, $coord, $level) = @_;
	my $s = $calc->{border_level} * $level;
	my $i = $calc->{border_inside};
	if (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {
		$calc->leave($coord);
	}
	$calc->{in_border} = 0;
}

sub in_border {
	shift->{in_border};
}

sub ans {
	shift->{len};
}

package main;

sub fz {
	my $f = shift;
	abs($f) < 1E-10? 0: $f;
}

sub line {
	my ($x1, $y1, $x2, $y2) = @_;
	my ($k, $b, $v, $x, $sin, $cos);
	$v = ($x1 == $x2);
	if ($v) {
		$x = $x1;
		$sin = 1;
		$cos = 0;
	} else {
		my $dx = ($x2 - $x1);
		my $dy = ($y2 - $y1);
		if ($dx < 0) {
			$dx = -$dx;
			$dy = -$dy;
		}
		$k = $dy / $dx;
		$b = $y1 - $k * $x1;
		my $l = sqrt($dx * $dx + $dy * $dy);
		$sin = $dy / $l;
		$cos = $dx / $l;
	}
	{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }
}

sub newxy {
	my ($m, $nref, $xref, $yref) = @_;
	my $n = @$nref;
	my ($xshift, $yshift);
	if ($m->{v}) {
		$xshift = $m->{x};
	} else {
		$yshift = $m->{b};
	}
	my $sin = $m->{sin};
	my $cos = $m->{cos};
	for (my $i = 0; $i < $n; $i++) {
		my $x = $$nref[$i][0] - $xshift;
		my $y = $$nref[$i][1] - $yshift;
		$$xref[$i] = $x * $cos + $y * $sin;
		$$yref[$i] = fz($y * $cos - $x * $sin);
	}
}

sub entry_points {
	my ($xref, $yref) = @_;
	my $n = @$xref;
	my @i;
	for (my $i = 0; $i < $n; $i++) {
		my $y = $yref->[$i];
		my $yp = $yref->[$i - 1];
		my $yn = $yref->[($i + 1) % $n];
		if ($y == 0) {
			if ($yp * $yn < 0 || ($yp * $yn == 0 && $yp + $yn != 0)) {
				push(@i, [$i, $xref->[$i], $y, $yp, $yn]);
			}
		} elsif ($y * $yp < 0) {
			my $x = $xref->[$i] + ($xref->[$i - 1] - $xref->[$i])/(1 - $yp / $y);
    		push(@i, [$i, $x, $y, $yp, $yn]);
		}
	}
	@i = sort { $a->[1] <=> $b->[1] } @i;
	@i;
}

sub calc_line {
	my ($m, $nref) = @_;
	my $n = @$nref;
	my $c = calc->new();

	# new (x, y)
	my (@x, @y);
	newxy($m, $nref, \@x, \@y);

	# entry points
	my @i = entry_points(\@x, \@y);

	# enter, leave, calc
	for my $i (@i) {
		my (undef, $x, $y, $yp, $yn) = @$i;
		if ($y == 0) {
			if ($c->in_border) {
				$c->leave_border($x, $yp + $yn);
			} elsif ($yp * $yn == 0) {
				$c->enter_border($x, $yp + $yn);
			} elsif ($yp * $yn < 0) {
				$c->enter_leave($x)
			}
		} else {
			# y <> 0
			$c->enter_leave($x);
		}
	}
	$c->ans();
}

sub input {
	my ($fh, $nref, $mref) = @_;
	my $input = sub { split(' ', <$fh>) };
	my ($n, $m) = &$input();
	# n: x0 y0
	$nref->[$_] = [ &$input() ] for 0 .. $n - 1;
	# m: x0 y0 x1 y1
	$mref->[$_] = [ &$input() ] for 0 .. $m - 1;
}

sub go {
	my (@n, @m);
	input(\*STDIN, \@n, \@m);
	for my $m (@m) {
		my $ans = calc_line(line(@$m), \@n);
		print $ans, " ";
	}
	print "\n";
}

go() unless exists $INC{"Test/More.pm"};

1;

use strict;

package calc;

sub new {
	my $self = { len => 0, start => undef, in_border => 0, inside => 0 };
	bless $self;
}

sub enter {
	my ($calc, $coord) = @_;
	$calc->{start} = $coord;
    $calc->{inside} = 1;
}

sub leave {
	my ($calc, $coord) = @_;
	$calc->{len} += abs($coord - $calc->{start});
    $calc->{start} = undef;
    $calc->{inside} = 0;
}

sub enter_leave {
	my ($calc, $coord) = @_;
	if ($calc->{inside}) {
		$calc->leave($coord);
	} else {
		$calc->enter($coord);
	}
}

sub enter_border {
	my ($calc, $coord, $level) = @_;
	$calc->{border_level} = $level;
	$calc->{border_inside} = $calc->{inside};
	$calc->{in_border} = 1;
	$calc->enter($coord) unless $calc->{inside};
}

sub leave_border {
	my ($calc, $coord, $level) = @_;
	my $s = $calc->{border_level} * $level;
	my $i = $calc->{border_inside};
	if (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {
		$calc->leave($coord);
	}
	$calc->{in_border} = 0;
}

sub in_border {
	shift->{in_border};
}

sub ans {
	shift->{len};
}

package main;

sub fz {
	my $f = shift;
	abs($f) < 1E-12? 0: $f;
}

sub line {
	my ($x1, $y1, $x2, $y2) = @_;
	my ($k, $b, $v, $x, $sin, $cos);
	$v = ($x1 == $x2);
	if ($v) {
		$x = $x1;
		$sin = 1;
		$cos = 0;
	} else {
		my $dx = ($x2 - $x1);
		my $dy = ($y2 - $y1);
		if ($dx < 0) {
			$dx = -$dx;
			$dy = -$dy;
		}
		$k = $dy / $dx;
		$b = $y1 - $k * $x1;
		my $l = sqrt($dx * $dx + $dy * $dy);
		$sin = $dy / $l;
		$cos = $dx / $l;
	}
	{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }
}

sub newxy {
	my ($m, $nref, $xref, $yref) = @_;
	my $n = @$nref;
	my ($xshift, $yshift);
	if ($m->{v}) {
		$xshift = $m->{x};
	} else {
		$yshift = $m->{b};
	}
	my $sin = $m->{sin};
	my $cos = $m->{cos};
	for (my $i = 0; $i < $n; $i++) {
		my $x = $$nref[$i][0] - $xshift;
		my $y = $$nref[$i][1] - $yshift;
		$$xref[$i] = $x * $cos + $y * $sin;
		$$yref[$i] = fz($y * $cos - $x * $sin);
	}
}

sub entry_points {
	my ($xref, $yref) = @_;
	my $n = @$xref;
	my @i;
	for (my $i = 0; $i < $n; $i++) {
		my $y = $yref->[$i];
		my $yp = $yref->[$i - 1];
		my $yn = $yref->[($i + 1) % $n];
		if ($y == 0 && $yp * $yn <= 0) {
			push(@i, [$i, $xref->[$i], $y, $yp, $yn]);
		} elsif ($y * $yp < 0) {
			my $x = $xref->[$i] + ($xref->[$i - 1] - $xref->[$i])/(1 - $yp / $y);
    		push(@i, [$i, $x, $y, $yp, $yn]);
		}
	}
	@i = sort { $a->[1] <=> $b->[1] } @i;
	@i;
}

sub calc_line {
	my ($m, $nref) = @_;
	my $n = @$nref;
	my $c = calc->new();

	# new (x, y)
	my (@x, @y);
	newxy($m, $nref, \@x, \@y);

	# entry points
	my @i = entry_points(\@x, \@y);

	# enter, leave, calc
	for my $i (@i) {
		my (undef, $x, $y, $yp, $yn) = @$i;
		if ($y == 0) {
			if ($c->in_border) {
				$c->leave_border($x, $yp + $yn);
			} elsif ($yp * $yn == 0) {
				$c->enter_border($x, $yp + $yn);
			} elsif ($yp * $yn < 0) {
				$c->enter_leave($x)
			}
		} else {
			# y <> 0
			$c->enter_leave($x);
		}
	}
	$c->ans();
}

sub input {
	my ($fh, $nref, $mref) = @_;
	my $input = sub { split(' ', <$fh>) };
	my ($n, $m) = &$input();
	# n: x0 y0
	$nref->[$_] = [ &$input() ] for 0 .. $n - 1;
	# m: x0 y0 x1 y1
	$mref->[$_] = [ &$input() ] for 0 .. $m - 1;
}

sub go {
	my (@n, @m);
	input(\*STDIN, \@n, \@m);
	for my $m (@m) {
		my $ans = calc_line(line(@$m), \@n);
		print $ans, " ";
	}
	print "\n";
}

go() unless exists $INC{"Test/More.pm"};

1;

use strict;

package calc;

sub new {
	my $self = { len => 0, start => undef, in_border => 0, inside => 0 };
	bless $self;
}

sub enter {
	my ($calc, $coord) = @_;
	$calc->{start} = $coord;
    $calc->{inside} = 1;
}

sub leave {
	my ($calc, $coord) = @_;
	$calc->{len} += abs($coord - $calc->{start});
    $calc->{start} = undef;
    $calc->{inside} = 0;
}

sub enter_leave {
	my ($calc, $coord) = @_;
	if ($calc->{inside}) {
		$calc->leave($coord);
	} else {
		$calc->enter($coord);
	}
}

sub enter_border {
	my ($calc, $coord, $level) = @_;
	$calc->{border_level} = $level;
	$calc->{border_inside} = $calc->{inside};
	$calc->{in_border} = 1;
	$calc->enter($coord) unless $calc->{inside};
}

sub leave_border {
	my ($calc, $coord, $level) = @_;
	my $s = $calc->{border_level} * $level;
	my $i = $calc->{border_inside};
	if (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {
		$calc->leave($coord);
	}
	$calc->{in_border} = 0;
}

sub in_border {
	shift->{in_border};
}

sub ans {
	shift->{len};
}

package main;

sub fz {
	my $f = shift;
	abs($f) < 1E-12? 0: $f;
}

sub line {
	my ($x1, $y1, $x2, $y2) = @_;
	my ($k, $b, $v, $x, $sin, $cos);
	$v = ($x1 == $x2);
	if ($v) {
		$x = $x1;
		$sin = 1;
		$cos = 0;
	} else {
		my $dx = ($x2 - $x1);
		my $dy = ($y2 - $y1);
		if ($dx < 0) {
			$dx = -$dx;
			$dy = -$dy;
		}
		$k = $dy / $dx;
		$b = $y1 - $k * $x1;
		my $l = sqrt($dx * $dx + $dy * $dy);
		$sin = $dy / $l;
		$cos = $dx / $l;
	}
	{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }
}

sub newxy {
	my ($m, $nref, $xref, $yref) = @_;
	my $n = @$nref;
	my ($xshift, $yshift);
	if ($m->{v}) {
		$xshift = $m->{x};
	} else {
		$yshift = $m->{b};
	}
	my $sin = $m->{sin};
	my $cos = $m->{cos};
	for (my $i = 0; $i < $n; $i++) {
		my $x = $$nref[$i][0] - $xshift;
		my $y = $$nref[$i][1] - $yshift;
		$$xref[$i] = $x * $cos + $y * $sin;
		$$yref[$i] = fz($y * $cos - $x * $sin);
	}
}

sub entry_points {
	my ($xref, $yref) = @_;
	my $n = @$xref;
	my @i;
	for (my $i = 0; $i < $n; $i++) {
		my $y = $yref->[$i];
		my $yp = $yref->[$i - 1];
		my $yn = $yref->[($i + 1) % $n];
		if ($y == 0) {
			if ($yp * $yn < 0 || ($yp * $yn == 0 && $yp + $yn != 0)) {
				push(@i, [$i, $xref->[$i], $y, $yp, $yn]);
			}
		} elsif ($y * $yp < 0) {
			my $x = $xref->[$i] + ($xref->[$i - 1] - $xref->[$i])/(1 - $yp / $y);
    		push(@i, [$i, $x, $y, $yp, $yn]);
		}
	}
	@i = sort { $a->[1] <=> $b->[1] } @i;
	@i;
}

sub calc_line {
	my ($m, $nref) = @_;
	my $n = @$nref;
	my $c = calc->new();

	# new (x, y)
	my (@x, @y);
	newxy($m, $nref, \@x, \@y);

	# entry points
	my @i = entry_points(\@x, \@y);

	# enter, leave, calc
	for my $i (@i) {
		my (undef, $x, $y, $yp, $yn) = @$i;
		if ($y == 0) {
			if ($c->in_border) {
				$c->leave_border($x, $yp + $yn);
			} elsif ($yp * $yn == 0) {
				$c->enter_border($x, $yp + $yn);
			} elsif ($yp * $yn < 0) {
				$c->enter_leave($x)
			}
		} else {
			# y <> 0
			$c->enter_leave($x);
		}
	}
	$c->ans();
}

sub input {
	my ($fh, $nref, $mref) = @_;
	my $input = sub { split(' ', <$fh>) };
	my ($n, $m) = &$input();
	# n: x0 y0
	$nref->[$_] = [ &$input() ] for 0 .. $n - 1;
	# m: x0 y0 x1 y1
	$mref->[$_] = [ &$input() ] for 0 .. $m - 1;
}

sub go {
	my (@n, @m);
	input(\*STDIN, \@n, \@m);
	for my $m (@m) {
		my $ans = calc_line(line(@$m), \@n);
		print $ans, " ";
	}
	print "\n";
}

go() unless exists $INC{"Test/More.pm"};

1;

use strict;

package calc;

sub new {
	my $self = { len => 0, start => undef, in_border => 0, inside => 0 };
	bless $self;
}

sub enter {
	my ($calc, $coord) = @_;
	$calc->{start} = $coord;
    $calc->{inside} = 1;
}

sub leave {
	my ($calc, $coord) = @_;
	$calc->{len} += abs($coord - $calc->{start});
    $calc->{start} = undef;
    $calc->{inside} = 0;
}

sub enter_leave {
	my ($calc, $coord) = @_;
	if ($calc->{inside}) {
		$calc->leave($coord);
	} else {
		$calc->enter($coord);
	}
}

sub enter_border {
	my ($calc, $coord, $level) = @_;
	$calc->{border_level} = $level;
	$calc->{border_inside} = $calc->{inside};
	$calc->{in_border} = 1;
	$calc->enter($coord) unless $calc->{inside};
}

sub leave_border {
	my ($calc, $coord, $level) = @_;
	my $s = $calc->{border_level} * $level;
	my $i = $calc->{border_inside};
	if (($i == 0 && $s > 0) || ($i == 1 && $s < 0)) {
		$calc->leave($coord);
	}
	$calc->{in_border} = 0;
}

sub in_border {
	shift->{in_border};
}

sub ans {
	shift->{len};
}

package main;

sub fz {
	my $f = shift;
	abs($f) < 1E-12? 0: $f;
}

sub line {
	my ($x1, $y1, $x2, $y2) = @_;
	my ($k, $b, $v, $x, $sin, $cos);
	$v = ($x1 == $x2);
	if ($v) {
		$x = $x1;
		$sin = 1;
		$cos = 0;
	} else {
		my $dx = ($x2 - $x1);
		my $dy = ($y2 - $y1);
		if ($dx < 0) {
			$dx = -$dx;
			$dy = -$dy;
		}
		$k = $dy / $dx;
		$b = $y1 - $k * $x1;
		my $l = sqrt($dx * $dx + $dy * $dy);
		$sin = $dy / $l;
		$cos = $dx / $l;
	}
	{ k => $k, b => $b, v => $v, x => $x, sin => $sin, cos => $cos }
}

sub newxy {
	my ($m, $nref, $xref, $yref) = @_;
	my $n = @$nref;
	my ($xshift, $yshift);
	if ($m->{v}) {
		$xshift = $m->{x};
	} else {
		$yshift = $m->{b};
	}
	my $sin = $m->{sin};
	my $cos = $m->{cos};
	for (my $i = 0; $i < $n; $i++) {
		my $x = $$nref[$i][0] - $xshift;
		my $y = $$nref[$i][1] - $yshift;
		$$xref[$i] = $x * $cos + $y * $sin;
		$$yref[$i] = fz($y * $cos - $x * $sin);
	}
}

sub entry_points {
	my ($xref, $yref) = @_;
	my $n = @$xref;
	my @i;
	for (my $i = 0; $i < $n; $i++) {
		if ($yref->[$i] == 0 && $yref->[$i - 1] * $yref->[($i + 1) % $n] <= 0) {
			push(@i, $i);
		} elsif ($yref->[$i] * $yref->[$i - 1] < 0) {
			push(@i, $i);
		}
	}
	@i = sort { $xref->[$a] <=> $xref->[$b] } @i;
	@i;
}

sub calc_line {
	my ($m, $nref) = @_;
	my $n = @$nref;
	my $c = calc->new();

	# new (x, y)
	my (@x, @y);
	newxy($m, $nref, \@x, \@y);

	# entry points
	my @i = entry_points(\@x, \@y);

	# enter, leave, calc
	for my $i (@i) {
		my $y = $y[$i];
		my $yp = $y[$i - 1];
		my $yn = $y[($i + 1) % $n];
		if ($y == 0) {
			if ($c->in_border) {
				$c->leave_border($x[$i], $yp + $yn);
			} elsif ($yp * $yn == 0) {
				$c->enter_border($x[$i], $yp + $yn);
			} elsif ($yp * $yn < 0) {
				$c->enter_leave($x[$i])
			}
		} else {
			# y <> 0
			my $k = $yp / $y;
			if ($k < 0) {
				my $x = $x[$i] + ($x[$i - 1] - $x[$i])/(1 - $k);
				$c->enter_leave($x);
			}
		}
	}
	$c->ans();
}

sub input {
	my ($fh, $nref, $mref) = @_;
	my $input = sub { split(' ', <$fh>) };
	my ($n, $m) = &$input();
	# n: x0 y0
	$nref->[$_] = [ &$input() ] for 0 .. $n - 1;
	# m: x0 y0 x1 y1
	$mref->[$_] = [ &$input() ] for 0 .. $m - 1;
}

sub go {
	my (@n, @m);
	input(\*STDIN, \@n, \@m);
	for my $m (@m) {
		my $ans = calc_line(line(@$m), \@n);
		print $ans, " ";
	}
	print "\n";
}

go() unless exists $INC{"Test/More.pm"};

1;


You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj.

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b. The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109). The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).

Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.


Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'!To compose a story, Stephen wrote out $$$n$$$ words consisting of the first $$$5$$$ lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story.Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together.For example, the story consisting of three words "bac", "aaada", "e" is interesting (the letter 'a' occurs $$$5$$$ times, all other letters occur $$$4$$$ times in total). But the story consisting of two words "aba", "abcde" is not (no such letter that it occurs more than all other letters in total).You are given a sequence of $$$n$$$ words consisting of letters 'a', 'b', 'c', 'd' and 'e'. Your task is to choose the maximum number of them to make an interesting story. If there's no way to make a non-empty story, output $$$0$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of the words in the sequence. Then $$$n$$$ lines follow, each of them contains a word — a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i. e. duplicates are allowed). Only the letters 'a', 'b', 'c', 'd' and 'e' may occur in the words. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$; the sum of the lengths of all words over all test cases doesn't exceed $$$4 \cdot 10^5$$$.

For each test case, output the maximum number of words that compose an interesting story. Print 0 if there's no way to make a non-empty interesting story.

6
3
bac
aaada
e
3
aba
abcde
aba
2
baba
baba
4
ab
ab
c
bc
5
cbdca
d
a
d
e
3
b
c
ca

NoteIn the first test case of the example, all $$$3$$$ words can be used to make an interesting story. The interesting story is "bac aaada e".In the second test case of the example, the $$$1$$$-st and the $$$3$$$-rd words can be used to make an interesting story. The interesting story is "aba aba". Stephen can't use all three words at the same time.In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $$$0$$$.In the fourth test case of the example, Stephen can use the $$$3$$$-rd and the $$$4$$$-th words to make an interesting story. The interesting story is "c bc".

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

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

	@_ = map ~~<>, 1 .. $_;
	chomp @_;

	my @cand;

	for my $letter ( 'a' .. 'e' ){
		my @balance;

		for my $word ( @_ ){
			my $balance = 0;

			while( $word =~ /./g ){
				$balance += $& eq $letter ? 1 : -1;
				}

			$debug and print "  $letter:$balance";

			push @balance, $balance;
			}

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

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

		my $cand = 0;
		my $sum = 0;

		while( @balance ){
			my $i = shift @balance;

			if( $i > 0 ){
				$cand ++;
				$sum += $i;
				}
			elsif( $i == 0 and $sum > 0 ){
				$cand ++;
				}
			elsif( $sum + $i > 0 ){
				$cand ++;
				$sum += $i;
				}
			else{
				last;
				}
			}

		push @cand, $cand;
		}

	$debug and print "\@cand:[@cand]";

	print 0 + ( sort { $b <=> $a } @cand )[ 0 ];
	}


Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.

The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively. The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.

Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.

NoteIn the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:1. Move to floor 5: takes 2 seconds.2. Pick up passenger 3.3. Move to floor 3: takes 2 seconds.4. Wait for passenger 2 to arrive: takes 4 seconds.5. Pick up passenger 2.6. Go to floor 2: takes 1 second.7. Pick up passenger 1.8. Go to floor 0: takes 2 seconds.This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.

use feature ':all';

while(<>){
	say( ( sort {$b <=> $a} (split)[1], map { eval join '+', split } <>)[ 0 ] )
	}

use feature ':all';

while(<>){
	say( ( sort {$b <=> $a} map { eval join '+', split } <>)[ 0 ] )
	}


Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously:   all numbers on the square are non-negative integers not exceeding $$$10^5$$$;  there are no prime numbers in the square;  sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.

For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.


You are given a number $$$n$$$ (divisible by $$$3$$$) and an array $$$a[1 \dots n]$$$. In one move, you can increase any of the array elements by one. Formally, you choose the index $$$i$$$ ($$$1 \le i \le n$$$) and replace $$$a_i$$$ with $$$a_i + 1$$$. You can choose the same index $$$i$$$ multiple times for different moves.Let's denote by $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ the number of numbers from the array $$$a$$$ that have remainders $$$0$$$, $$$1$$$ and $$$2$$$ when divided by the number $$$3$$$, respectively. Let's say that the array $$$a$$$ has balanced remainders if $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ are equal.For example, if $$$n = 6$$$ and $$$a = [0, 2, 5, 5, 4, 8]$$$, then the following sequence of moves is possible:   initially $$$c_0 = 1$$$, $$$c_1 = 1$$$ and $$$c_2 = 4$$$, these values are not equal to each other. Let's increase $$$a_3$$$, now the array $$$a = [0, 2, 6, 5, 4, 8]$$$;  $$$c_0 = 2$$$, $$$c_1 = 1$$$ and $$$c_2 = 3$$$, these values are not equal. Let's increase $$$a_6$$$, now the array $$$a = [0, 2, 6, 5, 4, 9]$$$;  $$$c_0 = 3$$$, $$$c_1 = 1$$$ and $$$c_2 = 2$$$, these values are not equal. Let's increase $$$a_1$$$, now the array $$$a = [1, 2, 6, 5, 4, 9]$$$;  $$$c_0 = 2$$$, $$$c_1 = 2$$$ and $$$c_2 = 2$$$, these values are equal to each other, which means that the array $$$a$$$ has balanced remainders. Find the minimum number of moves needed to make the array $$$a$$$ have balanced remainders.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^4$$$) — the length of the array $$$a$$$. It is guaranteed that the number $$$n$$$ is divisible by $$$3$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$150\,000$$$.

For each test case, output one integer — the minimum number of moves that must be made for the $$$a$$$ array to make it have balanced remainders.

NoteThe first test case is explained in the statements.In the second test case, you need to make one move for $$$i=2$$$.The third test case you need to make three moves:   the first move: $$$i=9$$$;  the second move: $$$i=9$$$;  the third move: $$$i=2$$$. In the fourth test case, the values $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ initially equal to each other, so the array $$$a$$$ already has balanced remainders.


In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.Write a program to find the minimum number of seconds needed to balance the load of servers.

The first line contains positive number n (1 ≤ n ≤ 105) — the number of the servers.  The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≤ mi ≤ 2·104), where mi is the number of tasks assigned to the i-th server.

Print the minimum number of seconds required to balance the load.

NoteIn the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.In the second example the load is already balanced.A possible sequence of task movements for the third example is:  move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5);  then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4);  then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3). The above sequence is one of several possible ways to balance the load of servers in three seconds.

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

$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 ( $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


Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player. Alex is a perfectionist, so he decided to get as many points as possible. Help him.

The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence.  The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).

Print a single integer — the maximum number of points that Alex can earn.

NoteConsider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points.


Is it rated?Here it is. The Ultimate Question of Competitive Programming, Codeforces, and Everything. And you are here to answer it.Another Codeforces round has been conducted. No two participants have the same number of points. For each participant, from the top to the bottom of the standings, their rating before and after the round is known.It's known that if at least one participant's rating has changed, then the round was rated for sure.It's also known that if the round was rated and a participant with lower rating took a better place in the standings than a participant with higher rating, then at least one round participant's rating has changed.In this problem, you should not make any other assumptions about the rating system.Determine if the current round is rated, unrated, or it's impossible to determine whether it is rated of not.

The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of round participants. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 4126) — the rating of the i-th participant before and after the round, respectively. The participants are listed in order from the top to the bottom of the standings.

If the round is rated for sure, print "rated". If the round is unrated for sure, print "unrated". If it's impossible to determine whether the round is rated or not, print "maybe".

NoteIn the first example, the ratings of the participants in the third and fifth places have changed, therefore, the round was rated.In the second example, no one's rating has changed, but the participant in the second place has lower rating than the participant in the fourth place. Therefore, if the round was rated, someone's rating would've changed for sure.In the third example, no one's rating has changed, and the participants took places in non-increasing order of their rating. Therefore, it's impossible to determine whether the round is rated or not.


Screen resolution of Polycarp's monitor is $$$a \times b$$$ pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates $$$(x, y)$$$ ($$$0 \le x < a, 0 \le y < b$$$). You can consider columns of pixels to be numbered from $$$0$$$ to $$$a-1$$$, and rows — from $$$0$$$ to $$$b-1$$$.Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.

In the first line you are given an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. In the next lines you are given descriptions of $$$t$$$ test cases. Each test case contains a single line which consists of $$$4$$$ integers $$$a, b, x$$$ and $$$y$$$ ($$$1 \le a, b \le 10^4$$$; $$$0 \le x < a$$$; $$$0 \le y < b$$$) — the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that $$$a+b>2$$$ (e.g. $$$a=b=1$$$ is impossible).

Print $$$t$$$ integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.

NoteIn the first test case, the screen resolution is $$$8 \times 8$$$, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.


The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.

The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.

If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).

NoteIn the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.

<>;
	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/, <>
		]