Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes. Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≤ l1, i ≤ r1, i ≤ 109) — the i-th variant of a period of time when Anton can attend chess classes. The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes. Each of the following m lines of the input contains two integers l2, i and r2, i (1 ≤ l2, i ≤ r2, i ≤ 109) — the i-th variant of a period of time when Anton can attend programming classes.

Output one integer — the maximal possible distance between time periods.

NoteIn the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(!eof){
	my $left = 1e9;
	my $right = 0;
	my $left_2 = 1e9;
	my $right_2 = 0;
	my $n = <>;
	for my $i (1 .. $n){
		my( $l, $r )= split ' ', <>;
		$r < $left and $left = $r;
		$l > $right_2 and $right_2 = $l;
		}
	my $m = <>;
	for my $i (1 .. $m){
		my( $l, $r )= split ' ', <>;
		$r < $left_2 and $left_2 = $r;
		$l > $right and $right = $l;
		}

	print +( sort { $a <=> $b } 0, $right - $left, $right_2 - $left_2 )[ 2 ];

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(!eof){
	my $left = 3e5;
	my $right = 0;
	my $left_2 = 3e5;
	my $right_2 = 0;
	my $n = <>;
	for my $i (1 .. $n){
		my( $l, $r )= split ' ', <>;
		$r < $left and $left = $r;
		$l > $right_2 and $right_2 = $l;
		}
	my $m = <>;
	for my $i (1 .. $m){
		my( $l, $r )= split ' ', <>;
		$r < $left_2 and $left_2 = $r;
		$l > $right and $right = $l;
		}

	print +( sort { $a <=> $b } 0, $right - $left, $right_2 - $left_2 )[ 2 ];

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(!eof){
	my $left = 3e5;
	my $right = 0;
	my $n = <>;
	for my $i (1 .. $n){
		my $r = ( split ' ', <> )[ 1 ];
		$r < $left and $left = $r;
		}
	my $m = <>;
	for my $i (1 .. $m){
		my $l = ( split ' ', <> )[ 0 ];
		$l > $right and $right = $l;
		}

	print +( sort { $a <=> $b } 0, $right - $left )[ 1 ];

	}


We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $$$a$$$ yuan for $$$b$$$ kilos (You don't need to care about what "yuan" is), the same as $$$a/b$$$ yuan for a kilo.Now imagine you'd like to buy $$$m$$$ kilos of apples. You've asked $$$n$$$ supermarkets and got the prices. Find the minimum cost for those apples.You can assume that there are enough apples in all supermarkets.

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 5\,000$$$, $$$1 \leq m \leq 100$$$), denoting that there are $$$n$$$ supermarkets and you want to buy $$$m$$$ kilos of apples. The following $$$n$$$ lines describe the information of the supermarkets. Each line contains two positive integers $$$a, b$$$ ($$$1 \leq a, b \leq 100$$$), denoting that in this supermarket, you are supposed to pay $$$a$$$ yuan for $$$b$$$ kilos of apples.

The only line, denoting the minimum cost for $$$m$$$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $$$10^{-6}$$$. Formally, let your answer be $$$x$$$, and the jury's answer be $$$y$$$. Your answer is considered correct if $$$\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}$$$.

NoteIn the first sample, you are supposed to buy $$$5$$$ kilos of apples in supermarket $$$3$$$. The cost is $$$5/3$$$ yuan.In the second sample, you are supposed to buy $$$1$$$ kilo of apples in supermarket $$$2$$$. The cost is $$$98/99$$$ yuan.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	@_ = map { eval join '/', split } map ~~<>, 1 .. $n;

	print $m * ( sort { $a <=> $b } @_ )[ 0 ];
	}

print +( split )[ 1 ] * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] while <>

print +( split ) * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] for <>

print +( split ) * ( sort { $b <=> $a } map { eval s! !/!r } <> ) while <>


The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.

The first line contains an integer number n (1  ≤  n  ≤  1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.

my $n = <>;
my (@name, @point);
my @table = [];
my ($ans, $max) = (0, 0);
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    push @table, [$name[$i], $hash{$name[$i]}];
}
$max = $_ > $max ? $_ : $max foreach(values %hash);
# print "\n-----------\n";
for my $i (1..$n) {
    if ($hash{$name[$i]} == $max && $table[$i][1] >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

use strict;
use warnings;

my $players = {};
my $score_log = [];

my $n_inputs = <>;

for ( 1 .. $n_inputs ) {
  my $line = <>;
  my ( $name, $score, @rest ) = split /\s+/s, $line;

  if ( not exists $players->{$name} ) {
    $players->{$name} = 0;
  }
  my $new_score = $players->{$name} + $score;
  push @{ $score_log }, { name => $name, score => $new_score };
  $players->{ $name } = $new_score;
}
my %winning_players;
my ( $winning_score, ) = sort { $b <=> $a } values %{ $players };
for ( grep { $winning_score == $players->{$_} } keys %{ $players } ) {
  $winning_players{ $_ } = 1;
}
for my $entry ( @{ $score_log } ) {
  next unless exists $winning_players{ $entry->{name} };
  next unless $entry->{score} >= $winning_score;
  printf "%s\n", $entry->{name};
  exit 0;
}

#!/usr/bin/perl

use strict;
use warnings;

my $n = <>;
chomp $n;

my @round;
my %score;

for (my $i = 0; $i < $n; $i++) {
    my $line = <>;
    chomp $line;
    push @round, $line;
    my @tuple = split ' ', $round[$i];
    if (not defined($score{$tuple[0]})) {
        $score{$tuple[0]} = $tuple[1];
    } else {
        $score{$tuple[0]} += $tuple[1];
    }
}

my $max = 0;
foreach (keys(%score)) {
    $max = $score{$_} if ($score{$_} > $max);
}

my %t;
for (my $i = 0; $i < $n; $i++) {
    my @tuple = split ' ', $round[$i];
    if (not defined($t{$tuple[0]})) {
        $t{$tuple[0]} = $tuple[1];
    } else {
        $t{$tuple[0]} += $tuple[1];
    }

    if ($score{$tuple[0]} == $max and $t{$tuple[0]} >= $max) {
        print $tuple[0];
        last;
    }
}

$n=<STDIN>;
%hash;
@list;
$i=0;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
        $list[$i++]=$name;
        $list[$i++]=$hash{$name};
}
#print $max,"\n";
$flag=1;
foreach $key (%hash)
        {
    #        print $hash{$key},"\n";
            if($hash{$key}>$max)
            {
                $max=$hash{$key};
                $winner=$key;
            }
        }

for($j=0;$j<$i && $flag;$j+=2)
{
    #print "$list[$j] $list[$j+1] $hash{$list[$j]} $max\n";
    if($list[$j+1]>=$max && $hash{$list[$j]}==$max && $flag)
    {
        print $list[$j];
        $flag=0;
    }
}

use strict;
use warnings;

use Data::Dumper;

my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
#    if (! exists $hisResults{$inputs[0]}){
#        $hisResults{$inputs[0]} = [$inputs[1]];
#    }
#    else{
#        my $arrayRef = $hisResults{$inputs[0]};
#        if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){
#            push @$arrayRef, $finResults{$inputs[0]};
#        }
#
#    }
}

my @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;

if (scalar @finals == 1){
    return $finals[0];
}
my $finalScore = $finResults{ $finals[scalar @finals - 1] };
my %candidateWiners;
$candidateWiners{$finals [scalar @finals - 1]} = 1;
for(my $i = scalar @finals -2; $i >= 0; $i--){
    if($finResults{ $finals[$i] } < $finalScore){
#        print "UUUUUUUUUU: $i,  NNNNN: $finResults{ $finals[$i]}\n";
        last;
    }
    else{
        $candidateWiners{$finals[$i] } = 1;
    }
}
#print Dumper \@finals ;
#
#
#
#print Dumper \%candidateWiners;
#print "HHHHHH: finalScore $finalScore \n";
#print Dumper \%finResults ;
#print Dumper \@hisResults;

foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore &&  exists($candidateWiners{$hisRes->[0]})){
        print $hisRes->[0];
        last;
    }
}

#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }
#           keys %finResults)
#{
#     print "$candidate $finResults{$candidate} \n";
#}

#print Dumper \%hisResults;

use strict;
use warnings;
my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;

#calc final score and keep historic record.
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
}
#Sort based on final score to find candidate winners.
my @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;
if (scalar @finals == 1){  #if there is only one candidate, he is the winner.
    return $finals[0];
}
my $finalScore = $finResults{ $finals[scalar @finals - 1] };
my %candidateWiners;
$candidateWiners{$finals [scalar @finals - 1]} = 1;
for(my $i = scalar @finals -2; $i >= 0; $i--){
    if($finResults{ $finals[$i] } < $finalScore){
        last;
    }
    else{
        $candidateWiners{$finals[$i] } = 1;
    }
}

#Check the historic data to find the final winner.
foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore &&  exists($candidateWiners{$hisRes->[0]})){
        print $hisRes->[0];
        last;
    }
}

use strict;
use warnings;

my $n = <>;
my $name;
my $point;
my $ans;
my $max = 0;
my %hash;
for (1..$n) {
    ($name, $point) = split " ", <>;
    if (exists $hash{$name}) {
        $hash{$name} += $point;
    }
    else {
        $hash{$name} = $point;
    }
    if ($hash{$name} > $max) {
        $max = $hash{$name};
        $ans = $name;
    }
}
print $ans . "\n";

my $n = <>;
my (@name, @point);
my @table = [];
my ($ans, $max) = (0, 0);
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    push @table, [$name[$i], $point[$i]];
}
$max = $_ > $max ? $_ : $max foreach(values %hash);
# print "\n-----------\n";
for my $i (1..$n) {
    if ($hash{$name[$i]} == $max && $table[$i][1] >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

my $n = <>;
my @name;
my @point;
my $ans;
my $max = 0;
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    next if ($point[$i] < 0);
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    $max = $hash{$name[$i]} if ($hash{$name[$i]} > $max);
}
for my $i (1..$n) {
    if ($point[$i] >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

my $n = <>;
my @name;
my @point;
my $ans;
my $max = 0;
my %hash;
my %table;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
}
$max = $_ > $max ? $_ : $max foreach(values %hash);
# print "\n-----------\n";
for my $i (1..$n) {
    next if ($point[$i] < 0);
    $table{$name[$i]} = 0 if(not exists $table{$name[$i]});
    $table{$name[$i]} += $point[$i];
    # print "$hash{$name[$i]} $table{$name[$i]}\n";
    if ($hash{$name[$i]} == $max && $table{$name[$i]} >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

my $n = <>;
my $name;
my $point;
my $ans;
my $max = 0;
my %hash;
for (1..$n) {
    ($name, $point) = split " ", <>;
    if ($point < 0) {
        $hash{$name} = -1000;
        next;
    }
    if (exists $hash{$name} and $hash{$name} > 0) {
        $hash{$name} += $point;
    }
    else {
        $hash{$name} = $point;
    }
    if ($hash{$name} > $max) {
        $max = $hash{$name};
        $ans = $name;
    }
}
print $ans . "\n";

my $n = <>;
my @name;
my @point;
my $ans;
my $max = 0;
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    next if ($point[$i] < 0);
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    $max = $hash{$name[$i]} if ($hash{$name[$i]} > $max);
}

undef %hash;
for my $i (1..$n) {
    next if ($point[$i] < 0);
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    if ($hash{$name[$i]} >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

use strict;
use warnings;

my $players = {};
my $score_log = [];

my $n_inputs = <>;

for ( 1 .. $n_inputs ) {
  my $line = <>;
  my ( $name, $score, @rest ) = split /\s+/s, $line;

  if ( not exists $players->{$name} ) {
    $players->{$name} = 0;
  }
  my $new_score = $players->{$name} + $score;
  push @{ $score_log }, { name => $name, score => $new_score };
  $players->{ $name } = $new_score;
}
my %winning_players;
my ( $winning_score, ) = sort { $b <=> $a } values %{ $players };
for ( grep { $winning_score == $players->{$_} } keys %{ $players } ) {
  $winning_players{ $_ } = 1;
}
for my $entry ( @{ $score_log } ) {
  next unless exists $winning_players{ $entry->{name} };
  next unless $entry->{score} > $winning_score;
  printf "%s\n", $entry->{name};
  exit 0;
}

use strict;
use warnings;

my $scores = {};
my $players = {};

my $n_inputs = <>;

for ( 1 .. $n_inputs ) {
  my $line = <>;
  my ( $name, $score, @rest ) = split /\s+/s, $line;

  if ( not exists $players->{$name} ) {
    $players->{$name} = 0;
  }
  my $new_score = $players->{$name} + $score;
  $scores->{ $new_score } = $name unless exists $scores->{ $new_score };
  $players->{ $name } = $new_score;
}
my ( $winning_score, ) = sort { $b <=> $a } values %{ $players };

printf "%s\n", $scores->{ $winning_score };

$n=<STDIN>;
%hash;
$max=0;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        $hash{$name}+=$score;
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
print $winner;

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
}
#print $max,"\n";
$flag=1;
foreach $key (%hash)
        {
  #          print $hash{$key},"\n";
            if($hash{$key}>$max)
            {
                $max=$hash{$key};
                $winner=$key;
            }
        }
print $winner;

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
print $winner;

$n=<STDIN>;
%hash;
@list;
$i=0;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
        $list[$i++]=$name;
        $list[$i++]=$hash{$name};
}
#print $max,"\n";
$flag=1;
foreach $key (%hash)
        {
    #        print $hash{$key},"\n";
            if($hash{$key}>$max)
            {
                $max=$hash{$key};
                $winner=$key;
            }
        }

for($j=0;$j<$i;$j+=2)
{
   # print "$list[$j] $list[$j+1] $max\n";
    if($list[$j+1]==$max && $flag)
    {
        print $list[$j];
        $flag=0;
    }
}

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        $hash{$name}+=$score;
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
print $winner;

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
$flag=1;
foreach $key (%hash)
        {
            if($hash{$key}>=$max && $key==$winner && $flag)
            {
                print $key,"\n";
                $flag=0;
            }
        }
#print $winner;

use strict;
use warnings;
use Data::Dumper;
my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;
my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
}
my @finals = sort {$a <=> $b} values %finResults;
my $finalScore = $finals[scalar @finals - 1];
foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore){
        print $hisRes->[0];
        last;
    }
}

use strict;
use warnings;

use Data::Dumper;

my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
#    if (! exists $hisResults{$inputs[0]}){
#        $hisResults{$inputs[0]} = [$inputs[1]];
#    }
#    else{
#        my $arrayRef = $hisResults{$inputs[0]};
#        if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){
#            push @$arrayRef, $finResults{$inputs[0]};
#        }
#
#    }
}

my @finals = sort {$a <=> $b} values %finResults;

#print Dumper \@finals ;

my $finalScore = $finals[scalar @finals - 1];
#print "HHHHHH: finalScore $finalScore \n";
#print Dumper \%finResults ;
#print Dumper \@hisResults;

foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore){
        print $hisRes->[0];
        last;
    }
}

#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }
#           keys %finResults)
#{
#     print "$candidate $finResults{$candidate} \n";
#}

#print Dumper \%hisResults;

use strict;
use warnings;

use Data::Dumper;

my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
#    if (! exists $hisResults{$inputs[0]}){
#        $hisResults{$inputs[0]} = [$inputs[1]];
#    }
#    else{
#        my $arrayRef = $hisResults{$inputs[0]};
#        if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){
#            push @$arrayRef, $finResults{$inputs[0]};
#        }
#
#    }
}

my @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;

#print Dumper \@finals ;

my $finalScore = $finals[scalar @finals - 1];
#print "HHHHHH: finalScore $finalScore \n";
#print Dumper \%finResults ;
#print Dumper \@hisResults;

foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finResults{$finalScore} && $hisRes->[0] eq $finalScore){
        print $hisRes->[0];
        last;
    }
}

#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }
#           keys %finResults)
#{
#     print "$candidate $finResults{$candidate} \n";
#}

#print Dumper \%hisResults;


The 2050 volunteers are organizing the "Run! Chase the Rising Sun" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are $$$n+1$$$ checkpoints on the trail. They are numbered by $$$0$$$, $$$1$$$, ..., $$$n$$$. A runner must start at checkpoint $$$0$$$ and finish at checkpoint $$$n$$$. No checkpoint is skippable — he must run from checkpoint $$$0$$$ to checkpoint $$$1$$$, then from checkpoint $$$1$$$ to checkpoint $$$2$$$ and so on. Look at the picture in notes section for clarification.Between any two adjacent checkpoints, there are $$$m$$$ different paths to choose. For any $$$1\le i\le n$$$, to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$, a runner can choose exactly one from the $$$m$$$ possible paths. The length of the $$$j$$$-th path between checkpoint $$$i-1$$$ and $$$i$$$ is $$$b_{i,j}$$$ for any $$$1\le j\le m$$$ and $$$1\le i\le n$$$.To test the trail, we have $$$m$$$ runners. Each runner must run from the checkpoint $$$0$$$ to the checkpoint $$$n$$$ once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length $$$l_i$$$ between checkpoint $$$i-1$$$ and $$$i$$$ ($$$1\le i\le n$$$), his tiredness is $$$$$$\min_{i=1}^n l_i,$$$$$$ i. e. the minimum length of the paths he takes.Please arrange the paths of the $$$m$$$ runners to minimize the sum of tiredness of them.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 100$$$). The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i,1}$$$, $$$b_{i,2}$$$, ..., $$$b_{i,m}$$$ ($$$1 \le b_{i,j} \le 10^9$$$). It is guaranteed that the sum of $$$n\cdot m$$$ over all test cases does not exceed $$$10^4$$$.

For each test case, output $$$n$$$ lines. The $$$j$$$-th number in the $$$i$$$-th line should contain the length of the path that runner $$$j$$$ chooses to run from checkpoint $$$i-1$$$ to checkpoint $$$i$$$. There should be exactly $$$m$$$ integers in the $$$i$$$-th line and these integers should form a permuatation of $$$b_{i, 1}$$$, ..., $$$b_{i, m}$$$ for all $$$1\le i\le n$$$. If there are multiple answers, print any.

NoteIn the first case, the sum of tiredness is $$$\min(2,5) + \min(3,3) + \min(4,1) = 6$$$.  In the second case, the sum of tiredness is $$$\min(2,4,3) + \min(3,1,5) = 3$$$.


In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string $$$s$$$. He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size $$$x$$$ ($$$1 \leq x \leq 26$$$) contains only the first $$$x$$$ Latin letters. For example an alphabet of size $$$4$$$ contains only the characters $$$\texttt{a}$$$, $$$\texttt{b}$$$, $$$\texttt{c}$$$ and $$$\texttt{d}$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters.

For each test case, output a single integer — the minimum alphabet size required to so that Atilla can write his message $$$s$$$.

5

1

a

4

down

10

codeforces

3

bcf

5

zzzzz

NoteFor the first test case, Atilla needs to know only the character $$$\texttt{a}$$$, so the alphabet of size $$$1$$$ which only contains $$$\texttt{a}$$$ is enough.For the second test case, Atilla needs to know the characters $$$\texttt{d}$$$, $$$\texttt{o}$$$, $$$\texttt{w}$$$, $$$\texttt{n}$$$. The smallest alphabet size that contains all of them is $$$23$$$ (such alphabet can be represented as the string $$$\texttt{abcdefghijklmnopqrstuvw}$$$).

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my( $L ) = reverse sort split '';

	my $A = 'a';

	my $ans;

	for my $i ( 1 .. 26 ){
		if( $A eq $L ){
			$ans = $i;
			last;
			}
		$A ++;
		}

	print $ans;
	}


Ohana Matsumae is trying to clean a room, which is divided up into an n by n grid of squares. Each square is initially either clean or dirty. Ohana can sweep her broom over columns of the grid. Her broom is very strange: if she sweeps over a clean square, it will become dirty, and if she sweeps over a dirty square, it will become clean. She wants to sweep some columns of the room to maximize the number of rows that are completely clean. It is not allowed to sweep over the part of the column, Ohana can only sweep the whole column.Return the maximum number of rows that she can make completely clean.

The first line of input will be a single integer n (1 ≤ n ≤ 100). The next n lines will describe the state of the room. The i-th line will contain a binary string with n characters denoting the state of the i-th row of the room. The j-th character on this line is '1' if the j-th square in the i-th row is clean, and '0' if it is dirty.

The output should be a single line containing an integer equal to a maximum possible number of rows that are completely clean.

NoteIn the first sample, Ohana can sweep the 1st and 3rd columns. This will make the 1st and 4th row be completely clean.In the second sample, everything is already clean, so Ohana doesn't need to do anything.


Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.Help Yaroslav.

The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the array elements.

In the single line print "YES" (without the quotes) if Yaroslav can obtain the array he needs, and "NO" (without the quotes) otherwise.

NoteIn the first sample the initial array fits well.In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.In the third sample Yarosav can't get the array he needs.


The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to $$$1$$$); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to $$$0$$$).In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead.  In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the $$$i$$$-th monster is selected, and the health values of the hero and the $$$i$$$-th monster are $$$x$$$ and $$$y$$$ before the fight, respectively. After the fight, the health values of the hero and the $$$i$$$-th monster become $$$x-a_i$$$ and $$$y-A$$$, respectively. Note that the hero can fight the same monster more than once.For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster).

Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains three integers $$$A$$$ ($$$1 \leq A \leq 10^6$$$), $$$B$$$ ($$$1 \leq B \leq 10^6$$$) and $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the attack power of the great hero, the initial health value of the great hero, and the number of monsters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$), where $$$a_i$$$ denotes the attack power of the $$$i$$$-th monster. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \leq b_i \leq 10^6$$$), where $$$b_i$$$ denotes the initial health value of the $$$i$$$-th monster. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print the answer: "YES" (without quotes) if the great hero can kill all the monsters. Otherwise, print "NO" (without quotes).

NoteIn the first example: There will be $$$6$$$ fights between the hero and the only monster. After that, the monster is dead and the health value of the hero becomes $$$17 - 6 \times 2 = 5 > 0$$$. So the answer is "YES", and moreover, the hero is still living.In the second example: After all monsters are dead, the health value of the hero will become $$$709$$$, regardless of the order of all fights. So the answer is "YES".In the third example: A possible order is to fight with the $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd and $$$4$$$-th monsters. After all fights, the health value of the hero becomes $$$-400$$$. Unfortunately, the hero is dead, but all monsters are also dead. So the answer is "YES".In the fourth example: The hero becomes dead but the monster is still living with health value $$$1000 - 999 = 1$$$. So the answer is "NO".


Bob is playing with $$$6$$$-sided dice. A net of such standard cube is shown below.He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.For example, the number of visible pips on the tower below is $$$29$$$ — the number visible on the top is $$$1$$$, from the south $$$5$$$ and $$$3$$$, from the west $$$4$$$ and $$$2$$$, from the north $$$2$$$ and $$$4$$$ and from the east $$$3$$$ and $$$5$$$.The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.Bob also has $$$t$$$ favourite integers $$$x_i$$$, and for every such integer his goal is to build such a tower that the number of visible pips is exactly $$$x_i$$$. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of favourite integers of Bob.  The second line contains $$$t$$$ space-separated integers $$$x_i$$$ ($$$1 \leq x_i \leq 10^{18}$$$) — Bob's favourite integers.

For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).

NoteThe first example is mentioned in the problem statement.In the second example, one can build the tower by flipping the top dice from the previous tower.In the third example, one can use a single die that has $$$5$$$ on top.The fourth example is impossible.

#!/usr/bin/perl

use warnings;
use strict;
use bigint;

$\ = $/;

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

	for( @_ ){
		my $d = int $_ / 14;

		$_ -= $d * 14;

		print $_ <= 6 || $d == 0 ? "YES" : "NO";
		}
	}

#!/usr/bin/perl

use warnings;
use strict;
use bigint;

$\ = $/;

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

	for( @_ ){
		my $d = int $_ / 14;

		$_ -= $d * 14;

		print $_ > 6 ? "NO" : "YES";
		}
	}

#!/usr/bin/perl

use warnings;
use strict;
use bigint;

$\ = $/;

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

	for( @_ ){
		my $d = int $_ / 14;

		$_ -= $d * 14;

		print $_ <= 6 && $d ? "YES" : "NO";
		}
	}


A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".

The first line contains integer $$$n$$$ ($$$1 \le n \le 100$$$), denoting the number of strings to process. The following $$$n$$$ lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between $$$1$$$ and $$$100$$$, inclusive.

Print $$$n$$$ lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $a_to_z = join '', 'a' .. 'z';

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

	for( @_ ){
		my $re = join '', sort split //;
		print $a_to_z =~ /$re/ ? "Yes" : "No";
		}

	}


Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$.

Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them.

NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$.  $$$|a_1 - a_2| = |6 - 6| = 0$$$;  $$$|a_2 - a_3| = |6 - 9| = 3$$$;  $$$|a_3 - a_4| = |9 - 6| = 3$$$;  $$$|a_4 - a_5| = |6 - 3| = 3$$$;  $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$.

use 5.020;
use warnings;
use utf8;

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

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

READ: while ($count) {

    $index++;

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

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

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

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

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

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

        if ( $item == -1 ) {

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

            }

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

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

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

    if (@adjacentArray) {

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

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

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

        # say "the k is $k";

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

        # say "mfork $mforK   mForArray $mForArray";

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

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

    $count--;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	$_ = <>, chomp;

	$debug and print "[$_]";

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

	$debug and print "[$_]";

	@_ = split;

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

	my @A;

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

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

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

	my $undef;

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

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

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

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

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

	my @B;

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

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

	print "$max_diff $undef";
	}

use 5.020;
use warnings;
use utf8;

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

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

    $index++;

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

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

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

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

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

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

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

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

        if ( defined($min) ) {

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

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

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

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

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

    if ( defined($min) ) {

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

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

    $count--;
}

use 5.020;
use warnings;
use utf8;

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

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

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

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

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

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

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

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

        if ( defined($min) ) {

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

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

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

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

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

    if ( defined($min) ) {

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

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

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

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

    $count--;
}

use 5.020;
use warnings;
use utf8;

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

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

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

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

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

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

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

        if ( defined($min) ) {

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

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

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

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

    $count--;
}

use 5.020;
use warnings;
use utf8;

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

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

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

    my $min;
    my $max;

    my $minIndex;
    my $maxIndex;

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

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

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

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

        if ( defined($min) ) {

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

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

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

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

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

    if ( defined($min) ) {

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

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

    $count--;
}


This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.

Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.

NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.


Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The newspaper contains string t, consisting of uppercase and lowercase English letters. We know that the length of string t greater or equal to the length of the string s.The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some n letters out of the newspaper and make a message of length exactly n, so that it looked as much as possible like s. If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS".Tanya wants to make such message that lets her shout "YAY!" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says "WHOOPS". Your task is to help Tanya make the message.

The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message. The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper. Here |a| means the length of the string a.

Print two integers separated by a space:   the first number is the number of times Tanya shouts "YAY!" while making the message,  the second number is the number of times Tanya says "WHOOPS" while making the message.

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

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

&incs;

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

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


Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below.  So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most n sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer.

The first line contains the integer n (2 ≤ n ≤ 100 000) — the maximum number of sections which can be highlighted on the display.

Print the maximum integer which can be shown on the display of Stepan's newest device.

#use strict;
use warnings;

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

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

#use strict;
use warnings;

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

#use strict;
use warnings;

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

#use strict;
use warnings;

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

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


Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute.Determine the longest time segment when Polycarp can sleep, i. e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in another.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of alarms. Each of the next n lines contains a description of one alarm. Each description has a format "hh:mm", where hh is the hour when the alarm rings, and mm is the minute of that hour when the alarm rings. The number of hours is between 0 and 23, and the number of minutes is between 0 and 59. All alarm times are distinct. The order of the alarms is arbitrary. Each alarm starts ringing in the beginning of the corresponding minute and rings for exactly one minute (i. e. stops ringing in the beginning of the next minute). Polycarp can start sleeping instantly when no alarm is ringing, and he wakes up at the moment when some alarm starts ringing.

Print a line in format "hh:mm", denoting the maximum time Polycarp can sleep continuously. hh denotes the number of hours, and mm denotes the number of minutes. The number of minutes should be between 0 and 59. Look through examples to understand the format better.

NoteIn the first example there is only one alarm which rings during one minute of a day, and then rings again on the next day, 23 hours and 59 minutes later. Polycarp can sleep all this time.

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

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

my $prev = 0;
my @diff;

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

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


Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays. The second line contains n integers ai (0 ≤ ai ≤ 109). The third line contains n integers bi (0 ≤ bi ≤ 109).

Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

NoteBitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.In the second sample, the maximum value is obtained for l = 1 and r = 9.


Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.Help Amr by choosing the smallest subsegment possible.

The first line contains one number n (1 ≤ n ≤ 105), the size of the array. The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them.

NoteA subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1


Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, default values).Valery decided to examine template procedures in this language in more detail. The description of a template procedure consists of the procedure name and the list of its parameter types. The generic type T parameters can be used as parameters of template procedures.A procedure call consists of a procedure name and a list of variable parameters. Let's call a procedure suitable for this call if the following conditions are fulfilled:   its name equals to the name of the called procedure;  the number of its parameters equals to the number of parameters of the procedure call;  the types of variables in the procedure call match the corresponding types of its parameters. The variable type matches the type of a parameter if the parameter has a generic type T or the type of the variable and the parameter are the same. You are given a description of some set of template procedures. You are also given a list of variables used in the program, as well as direct procedure calls that use the described variables. For each call you need to count the number of procedures that are suitable for this call.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of template procedures. The next n lines contain the description of the procedures specified in the following format: "void procedureName (type_1, type_2, ..., type_t)" (1 ≤ t ≤ 5), where void is the keyword, procedureName is the procedure name, type_i is the type of the next parameter. Types of language parameters can be "int", "string", "double", and the keyword "T", which denotes the generic type. The next line contains a single integer m (1 ≤ m ≤ 1000) — the number of used variables. Next m lines specify the description of the variables in the following format: "type variableName", where type is the type of variable that can take values "int", "string", "double", variableName — the name of the variable. The next line contains a single integer k (1 ≤ k ≤ 1000) — the number of procedure calls. Next k lines specify the procedure calls in the following format: "procedureName (var_1, var_2, ..., var_t)" (1 ≤ t ≤ 5), where procedureName is the name of the procedure, var_i is the name of a variable. The lines describing the variables, template procedures and their calls may contain spaces at the beginning of the line and at the end of the line, before and after the brackets and commas. Spaces may be before and after keyword void. The length of each input line does not exceed 100 characters. The names of variables and procedures are non-empty strings of lowercase English letters and numbers with lengths of not more than 10 characters. Note that this is the only condition at the names. Only the specified variables are used in procedure calls. The names of the variables are distinct. No two procedures are the same. Two procedures are the same, if they have identical names and identical ordered sets of types of their parameters.

On each of k lines print a single number, where the i-th number stands for the number of suitable template procedures for the i-th call.

4
void f(int,T)
void  f(T, T)
 void foo123   ( int,  double,  string,string  )
  void  p(T,double)
3
int a
 string    s
double x123
5
f(a,  a)
  f(s,a   )
foo   (a,s,s)
 f  (  s  ,x123)
proc(a)

6
void f(string,double,int)
void f(int)
   void f  ( T  )
void procedure(int,double)
void f  (T, double,int)
void f(string, T,T)
4
 int a
 int x
string  t
double  val
5
f(t, a, a)
f(t,val,a)
f(val,a, val)
 solve300(val, val)
f  (x)

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


Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water.It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:  Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;  Pasha pours the same amount of water to each girl;  Pasha pours the same amount of water to each boy;  if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does.Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends.

The first line of the input contains two integers, n and w (1 ≤ n ≤ 105, 1 ≤ w ≤ 109) — the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≤ ai ≤ 109, 1 ≤ i ≤ 2n) — the capacities of Pasha's tea cups in milliliters.

Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

NotePasha also has candies that he is going to give to girls but that is another task...


Vasya has a sequence of cubes and exactly one integer is written on each cube. Vasya exhibited all his cubes in a row. So the sequence of numbers written on the cubes in the order from the left to the right equals to a1, a2, ..., an.While Vasya was walking, his little brother Stepan played with Vasya's cubes and changed their order, so now the sequence of numbers written on the cubes became equal to b1, b2, ..., bn. Stepan said that he swapped only cubes which where on the positions between l and r, inclusive, and did not remove or add any other cubes (i. e. he said that he reordered cubes between positions l and r, inclusive, in some way).Your task is to determine if it is possible that Stepan said the truth, or it is guaranteed that Stepan deceived his brother.

The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ n) — the number of Vasya's cubes and the positions told by Stepan. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of integers written on cubes in the Vasya's order. The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of integers written on cubes after Stepan rearranged their order. It is guaranteed that Stepan did not remove or add other cubes, he only rearranged Vasya's cubes.

Print "LIE" (without quotes) if it is guaranteed that Stepan deceived his brother. In the other case, print "TRUTH" (without quotes).

NoteIn the first example there is a situation when Stepan said the truth. Initially the sequence of integers on the cubes was equal to [3, 4, 2, 3, 1]. Stepan could at first swap cubes on positions 2 and 3 (after that the sequence of integers on cubes became equal to [3, 2, 4, 3, 1]), and then swap cubes in positions 3 and 4 (after that the sequence of integers on cubes became equal to [3, 2, 3, 4, 1]).In the second example it is not possible that Stepan said truth because he said that he swapped cubes only between positions 1 and 2, but we can see that it is guaranteed that he changed the position of the cube which was on the position 3 at first. So it is guaranteed that Stepan deceived his brother.In the third example for any values l and r there is a situation when Stepan said the truth.