You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.

The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.

Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.

5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba

use POSIX;
my $line;
my @input1; my @input2;
my $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;
my $s = 0; my $c; my $result; my $i; my %h;

$line = <STDIN>;
chomp($line);
@input1 = split(' ', $line);

#$line = <STDIN>;
#chomp($line);
#@input2 = split(' ', $line);

my $line2;
for(1..@input1[0]) {
        $line = <STDIN>;
        chomp($line);
        @input2 = split('', $line);
        $line2 = <STDIN>;
        chomp($line2);
        my $prev = index($line, substr($line2, 0, 1));
        $s = 0;
        foreach $c (split('', $line2)) {
                $s += abs($prev - index($line, $c));
                $prev = index($line, $c);
        }
        print($s,"\n");
}
exit;

use POSIX;
my $line;
my @input1; my @input2;
my $key1 = 0; my $key2 = 0; my $key3 = 0; my $key4 = 0;
my $s = 0; my $c; my $result; my $i; my %h;

$line = <STDIN>;
chomp($line);
@input1 = split(' ', $line);

#$line = <STDIN>;
#chomp($line);
#@input2 = split(' ', $line);

my $line2;
for(1..@input1[0]) {
        $line = <STDIN>;
        chomp($line);
        @input2 = split('', $line);
        $line2 = <STDIN>;
        chomp($line2);
        my $prev = index($line, substr($line2, 0, 1));
        $s = 0;
        foreach $c (split('', $line2)) {
                $s += abs($prev - index($line, $c));
                $prev = index($line, $c);
        }
        print $s;
}
exit;


The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.In the Pindows operating system a strings are the lexemes of the command line — the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command " run.exe one, two . ", we give four lexemes to the Pindows command line: "run.exe", "one,", "two", ".". More formally, if we run a command that can be represented as string s (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string s that contain no spaces.To send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited — that is, for each occurrence of character """ we should be able to say clearly that the quotes are opening or closing. For example, as we run the command ""run.exe o" "" " ne, " two . " " ", we give six lexemes to the Pindows command line: "run.exe o", "" (an empty string), " ne, ", "two", ".", " " (a single space).It is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.You have a string that consists of uppercase and lowercase English letters, digits, characters ".,?!"" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character """ to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters.

The single line contains a non-empty string s. String s consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the ".,?!"" signs, or a space. It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme.

In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the "<" (less) character to the left of your lexemes and the ">" (more) character to the right. Print the lexemes in the order in which they occur in the command. Please, follow the given output format strictly. For more clarifications on the output format see the test samples.

"RUn.exe O" "" "   2ne, " two! . " "
firstarg   second   ""


Indian summer is such a beautiful time of the year! A girl named Alyona is walking in the forest and picking a bouquet from fallen leaves. Alyona is very choosy — she doesn't take a leaf if it matches the color and the species of the tree of one of the leaves she already has. Find out how many leaves Alyona has picked.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of leaves Alyona has found. The next n lines contain the leaves' descriptions. Each leaf is characterized by the species of the tree it has fallen from and by the color. The species of the trees and colors are given in names, consisting of no more than 10 lowercase Latin letters. A name can not be an empty string. The species of a tree and the color are given in each line separated by a space.

Output the single number — the number of Alyona's leaves.

5
birch yellow
maple red
birch yellow
maple yellow
maple green

$n = <>; chomp $n;
while($n--){
    $_=<>;chomp;
    $h{$_} = 1;
}
print (scalar keys %h);


There are many sunflowers in the Garden of the Sun.Garden of the Sun is a rectangular table with $$$n$$$ rows and $$$m$$$ columns, where the cells of the table are farmlands. All of the cells grow a sunflower on it. Unfortunately, one night, the lightning stroke some (possibly zero) cells, and sunflowers on those cells were burned into ashes. In other words, those cells struck by the lightning became empty. Magically, any two empty cells have no common points (neither edges nor corners).Now the owner wants to remove some (possibly zero) sunflowers to reach the following two goals:    When you are on an empty cell, you can walk to any other empty cell. In other words, those empty cells are connected.   There is exactly one simple path between any two empty cells. In other words, there is no cycle among the empty cells. You can walk from an empty cell to another if they share a common edge.Could you please give the owner a solution that meets all her requirements?Note that you are not allowed to plant sunflowers. You don't need to minimize the number of sunflowers you remove. It can be shown that the answer always exists.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 \le n,m \le 500$$$) — the number of rows and columns.  Each of the next $$$n$$$ lines contains $$$m$$$ characters. Each character is either 'X' or '.', representing an empty cell and a cell that grows a sunflower, respectively. It is guaranteed that the sum of $$$n \cdot m$$$ for all test cases does not exceed $$$250\,000$$$.

For each test case, print $$$n$$$ lines. Each should contain $$$m$$$ characters, representing one row of the table. Each character should be either 'X' or '.', representing an empty cell and a cell with a sunflower, respectively. If there are multiple answers, you can print any. It can be shown that the answer always exists.

XXX
..X
XXX
XXXX
.X.X
.X..
.XXX
.X...
.XXXX
.X...
.X...
XXXXX
XXXXXXXXXX
..
..

NoteLet's use $$$(x,y)$$$ to describe the cell on $$$x$$$-th row and $$$y$$$-th column.In the following pictures white, yellow, and blue cells stand for the cells that grow a sunflower, the cells lightning stroke, and the cells sunflower on which are removed, respectively.In the first test case, one possible solution is to remove sunflowers on $$$(1,2)$$$, $$$(2,3)$$$ and $$$(3 ,2)$$$. Another acceptable solution is to remove sunflowers on $$$(1,2)$$$, $$$(2,2)$$$ and $$$(3,2)$$$. This output is considered wrong because there are 2 simple paths between any pair of cells (there is a cycle). For example, there are 2 simple paths between $$$(1,1)$$$ and $$$(3,3)$$$.  $$$(1,1)\to (1,2)\to (1,3)\to (2,3)\to (3,3)$$$  $$$(1,1)\to (2,1)\to (3,1)\to (3,2)\to (3,3)$$$ This output is considered wrong because you can't walk from $$$(1,1)$$$ to $$$(3,3)$$$.


You have $$$r$$$ red and $$$b$$$ blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet:   has at least one red bean (or the number of red beans $$$r_i \ge 1$$$);  has at least one blue bean (or the number of blue beans $$$b_i \ge 1$$$);  the number of red and blue beans should differ in no more than $$$d$$$ (or $$$|r_i - b_i| \le d$$$) Can you distribute all beans?

The first line contains the single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first and only line of each test case contains three integers $$$r$$$, $$$b$$$, and $$$d$$$ ($$$1 \le r, b \le 10^9$$$; $$$0 \le d \le 10^9$$$) — the number of red and blue beans and the maximum absolute difference in each packet.

For each test case, if you can distribute all beans, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

NoteIn the first test case, you can form one packet with $$$1$$$ red and $$$1$$$ blue bean. The absolute difference $$$|1 - 1| = 0 \le d$$$.In the second test case, you can form two packets: $$$1$$$ red and $$$4$$$ blue beans in the first packet and $$$1$$$ red and $$$3$$$ blue beans in the second one.In the third test case, since $$$b = 1$$$, you can form only one packet with $$$6$$$ red and $$$1$$$ blue beans. The absolute difference $$$|6 - 1| = 5 > d$$$.In the fourth test case, since $$$d = 0$$$ so each packet should contain the same number of red and blue beans, but $$$r \neq b$$$.

#!/usr/bin/perl
use Data::Dumper;
use integer;

my ($test_case) = map { $_ - 0 } split(/\s+/,<STDIN>);

while( $test_case -- > 0 ){
  my ($r,$b,$d) = map { $_ - 0 } split(/\s+/,<STDIN>);
  if( $d == 0 ){
    print ( $r == $b ? "YES\n" : "NO\n" ); next;
  }
  ($r,$b) = ($b,$r) if $r < $b;
  my $di = $r - $b;
  my $dv = ($di+($d-1))/$d;
  if( $b < $dv ){
    print "NO\n";
  } else {
    print "YES\n";
  }

}

exit(0);


There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have $$$b$$$ buns, $$$p$$$ beef patties and $$$f$$$ chicken cutlets in your restaurant. You can sell one hamburger for $$$h$$$ dollars and one chicken burger for $$$c$$$ dollars. Calculate the maximum profit you can achieve.You have to answer $$$t$$$ independent queries.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) – the number of queries. The first line of each query contains three integers $$$b$$$, $$$p$$$ and $$$f$$$ ($$$1 \le b, ~p, ~f \le 100$$$) — the number of buns, beef patties and chicken cutlets in your restaurant. The second line of each query contains two integers $$$h$$$ and $$$c$$$ ($$$1 \le h, ~c \le 100$$$) — the hamburger and chicken burger prices in your restaurant.

For each query print one integer — the maximum profit you can achieve.

NoteIn first query you have to sell two hamburgers and three chicken burgers. Your income is $$$2 \cdot 5 + 3 \cdot 10 = 40$$$.In second query you have to ell one hamburgers and two chicken burgers. Your income is $$$1 \cdot 10 + 2 \cdot 12 = 34$$$.In third query you can not create any type of burgers because because you have only one bun. So your income is zero.

use strict;
use warnings;

use Data::Dumper;

sub main {

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

    for(my $i=0; $i<$q; $i++) {
        my $i= <$in>; chomp($i);
        my ($b, $p, $f)= split(' ', $i);

        $i= <$in>; chomp($i);
        my ($h, $c)= split(' ', $i);

        my $profit= 0;
        if($h > $c) { # make hamburgers as much as u can
            while($b>=2 && $p>0) {
                $profit += $h;
                $b-= 2;
                $p--;
            }

            while($b>=2 && $f>0) {
                $profit += $c;
                $b-= 2;
                $f--;
            }
        } else {
            while($b>=2 && $f>0) {
                $profit += $c;
                $b-= 2;
                $f--;
            }

            while($b>=2 && $p>0) {
                $profit += $h;
                $b-= 2;
                $p--;
            }
        }

        print $profit;
        print $/;
    }

}

main() unless caller();

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);
my $t = $line;

for (1..$t) {
    $line = <STDIN>;
    my ($b, $p, $f) = split q{ }, $line;
    $b = $b - 1 if $b % 2 == 1;
    $line = <STDIN>;
    my ($h, $c) = split q{ }, $line;

    my $price = 0;

    if ( $h > $c ) {
        $price += $h * make(\$b, \$p);
        $price += $c * make(\$b, \$f);
    }
    else {
        $price += $c * make(\$b, \$f);
        $price += $h * make(\$b, \$p);
    }
    say $price;
}

sub make {
    my ($b, $m) = @_;
    my $count = 0;
    if ( 2*$$m > $$b ) {
        $count = $$b/2;
        $$b = 0;
        $$m -= $count;
    }
    else {
        $count = $$m;
        $$m = 0;
        $$b -= 2 * $count;
    }
    return $count;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	my( $b, $p, $f ) = split;
	my( $h, $c ) = split ' ', <>;

	$b /= 2;

	my $middle = 'p' x $p . 'f' x $f;

	if( $h < $c ){
		$middle = reverse $middle;
		}

	my $L = substr $middle, 0, $b;

	$debug and print $L;

	my $cp = () = $L =~ /p/g;
	my $cf = () = $L =~ /f/g;

	$debug and print "$cp $cf";

	print $cp * $h + $cf * $c;
	}

use strict;
use warnings;

use Data::Dumper;

sub main {

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

    for(my $i=0; $i<$q; $i++) {
        my $i= <$in>; chomp($i);
        my ($b, $p, $f)= split(' ', $i);

        $i= <$in>; chomp($i);
        my ($h, $c)= split(' ', $i);

        my $profit= 0;
        if($h > $c) { # make hamburgers as much as u can
            while($b>=2 && $p>0) {
                $profit += $h;
                $b-= 2;
                $p--;
            }

            while($b>=2 && $f>0) {
                $profit += $c;
                $b-= 2;
                $f--;
            }
        } else {
            while($b>=2 && $f>0) {
                $profit += $c;
                $b-= 2;
                $f--;
            }

            while($b>=2 && $p>0) {
                $profit += $h;
                $b-= 2;
                $p--;
            }
        }

        print $profit;
    }

}

main() unless caller();


The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, "101101" is a palindrome, while "0101" is not.Alice and Bob are playing a game on a string $$$s$$$ (which is initially a palindrome in this version) of length $$$n$$$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.In each turn, the player can perform one of the following operations:   Choose any $$$i$$$ ($$$1 \le i \le n$$$), where $$$s[i] =$$$ '0' and change $$$s[i]$$$ to '1'. Pay 1 dollar.  Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, "01001" becomes "10010" after reversing.The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^3$$$). The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting of the characters '0' and '1'. It is guaranteed that the string $$$s$$$ is a palindrome and contains at least one '0'.  Note that there is no limit on the sum of $$$n$$$ over test cases.

For each test case print a single word in a new line:    "ALICE", if Alice will win the game,  "BOB", if Bob will win the game,  "DRAW", if the game ends in a draw.

NoteIn the first test case of the example,   in the $$$1$$$-st move Alice has to perform the $$$1$$$-st operation, since the string is currently a palindrome.  in the $$$2$$$-nd move Bob reverses the string.  in the $$$3$$$-rd move Alice again has to perform the $$$1$$$-st operation. All characters of the string are '1', game over.  Alice spends $$$2$$$ dollars while Bob spends $$$0$$$ dollars. Hence, Bob always wins.

chomp($t = <STDIN>);
foreach (1..$t)
{
    <STDIN>;
    chomp($a = <STDIN>);
    $cnt = $a =~ tr/0//;
    if ($cnt == 1)
    {
        print "BOB\n";
    }
    elsif ($cnt % 2)
    {
        print "ALICE\n";
    }
    else
    {
        print "BOB\n";
    }
}


You are given array $$$a_1, a_2, \dots, a_n$$$. Find the subsegment $$$a_l, a_{l+1}, \dots, a_r$$$ ($$$1 \le l \le r \le n$$$) with maximum arithmetic mean $$$\frac{1}{r - l + 1}\sum\limits_{i=l}^{r}{a_i}$$$ (in floating-point numbers, i.e. without any rounding).If there are many such subsegments find the longest one.

The first line contains single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the array $$$a$$$.

Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.

NoteThe subsegment $$$[3, 4]$$$ is the longest among all subsegments with maximum arithmetic mean.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $max = ( sort { $b <=> $a } @_ )[ 0 ];

	my $cnt = 0;

	my $m = -1;

	for( @_ ){
		if( $_ == $max ){
			$cnt ++;
			$cnt > $m and $m = $cnt;
			}
		else{
			$cnt = 0;
			}
		}

	print $m;
	}


You are given $$$n$$$ elements numbered from $$$1$$$ to $$$n$$$, the element $$$i$$$ has value $$$a_i$$$ and color $$$c_i$$$, initially, $$$c_i = 0$$$ for all $$$i$$$.The following operation can be applied:  Select three elements $$$i$$$, $$$j$$$ and $$$k$$$ ($$$1 \leq i < j < k \leq n$$$), such that $$$c_i$$$, $$$c_j$$$ and $$$c_k$$$ are all equal to $$$0$$$ and $$$a_i = a_k$$$, then set $$$c_j = 1$$$. Find the maximum value of $$$\sum\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.

The first line contains an integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$) — the number of elements. The second line consists of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq n$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element.

Print a single integer in a line — the maximum value of $$$\sum\limits_{i=1}^n{c_i}$$$ that can be obtained after applying the given operation any number of times.

NoteIn the first test, it is possible to apply the following operations in order:

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my $A = 'a' x 4;

	my %h;

	$_ = join ' ', reverse
		map { $h{ $_ } or $h{ $_ } = ++ $A and "$A," } split ' ', <>;

	my $ans = 0;
	my $min;
	my $new_min;

	m/
		\b(\w+)\b(,)?+
		(?{
			if( not defined $min ){
				$min = $1;
				}
			elsif( $min lt $1 or $min eq $1 and !$2 ){
				$ans ++;
				}
			elsif( $min gt $1 ){
				if( not defined $new_min ){
					$new_min = $1;
					}
				else{
					$new_min gt $1 and $new_min = $1;
					$ans ++;
					}
				}

			if( $min eq $1 and $2 ){
				if( defined $new_min ){
					$min = $new_min;
					undef $new_min;
					$ans ++;
					}
				else{
					undef $min;
					}
				}
			})
		(*FAIL)
		/x;

	print $ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my @ABC;
my $abc = 'a';

for my $i ( 0 .. 2e5 ){
	push @ABC, $abc ++;
	}

while(<>){
	@_ = map $ABC[ $_ ], split ' ', <>;

	$_ = join ' ', @_;

	my %B;

	for my $i ( 0 .. @_ - 1 ){
		$B{ $_[ $i ] } = exists $B{ $_[ $i ] } ? $i : -1;
		}

	my $ans = 0;
	my $under = 0;
	my $cover;
	my $far_cover;
	my $far_dist;

	my $i = 0;

	m/
		\b\w+\b
		(?{
			$i ++;

			if( $under ){
				if( $& eq $cover ){
					if( $B{ $& } + 1 != $i ){
						$ans ++;
						}
					elsif( defined $far_dist and $far_dist >= $i ){
						$cover = $far_cover;
						undef $far_cover;
						undef $far_dist;
						}
					else{
						$under = 0;
						undef $cover;
						undef $far_dist;
						undef $far_cover;
						}
					}
				else{
					$ans ++;
					if( $B{ $& } > -1 and not
						( defined $far_dist and $B{ $& } <= $far_dist ) ){
						$far_dist = $B{ $& };
						$far_cover = $&;
						}
					}
				}
			else{
				if( $B{ $& } > -1 ){
					$cover = $&;
					$under = 1;
					}
				}
		})
		(*FAIL)
		/x;

	print $ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

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

	@_ = split ' ', <>;

	$debug and print "[@_]";

	my @B = ( -2 ) x ( @_ + 1 );

	for my $i ( 0 .. @_ - 1 ){
		if( $B[ $_[ $i ] ] == -2 ){
			$B[ $_[ $i ] ] = -1;
			}
		elsif( $B[ $_[ $i ] ] > -2 ){
			$B[ $_[ $i ] ] = $i;
			}
		}

	my $ans = 0;
	my $under = 0;
	my $cover;
	my $far_cover;
	my $far_dist;

	my $i = 0;

	while( @_ ){
		$_ = shift @_;
		$i ++;
		$debug and print "$i:[$_]";

		if( $under ){
			if( $_ == $cover ){
				$debug and print " 1)";
				if( $B[ $_ ] + 1 != $i ){
					$debug and print "  1.1) $B[$_],$i";
					$ans ++;
					}
				elsif( defined $far_dist and $far_dist >= $i ){
					$cover = $far_cover;
					undef $far_cover;
					undef $far_dist;
					}
				else{
					$under = 0;
					undef $cover;
					undef $far_dist;
					undef $far_cover;
					}
				}
			else{
				$debug and print " 2)";
				$ans ++;
				$debug and print " $cover,$_";
				if( $B[ $_ ] > -1 ){
					if( defined $far_dist ) {
						if( $B[ $_ ] > $far_dist ){
							$far_dist = $B[ $_ ];
							$far_cover = $_;
							}
						}
					else{
						$far_dist = $B[ $_ ];
						$far_cover = $_;
						}
					}
				}
			}
		else{
			$debug and print " 3)";
			if( $B[ $_ ] > -1 ){
				$cover = $_;
				$under = 1;
				}
			}

		}

	print $ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my $A = 'a' x 4;

	my %h;

	$_ = join ' ', reverse
		map { $h{ $_ } or $h{ $_ } = ++ $A and "$A," } split ' ', <>;

	my $ans = 0;
	my $q = 0;
	my $min = 'z';

	m/
		\b(\w+)\b(,)?+
		(?{
			if( defined $2 and $min ge $1 ){
				$min eq $1 and -- $ans;
				$q > 1 and -- $ans;
				$q = 0;
				}
			else{
				++ $ans;
				$min gt $1 and $min = $1 and ++ $q;
				}
			})
		(*FAIL)
		/x;

	print $ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my $A = 'a' x 4;

	my %h;

	$_ = join ' ', reverse
		map { $h{ $_ } or $h{ $_ } = ++ $A and "$A," } split ' ', <>;

	my $ans = 0;
	my $min = 'z';

	m/
		\b(\w+)\b(,)?+
		(?{
			if( $min lt $1 or $min eq $1 and !$2 ){
				$ans ++;
				}
			$1 lt $min and $min = $1;
			})
		(*FAIL)
		/x;

	print $ans;
	}


You are given $$$k$$$ sequences of integers. The length of the $$$i$$$-th sequence equals to $$$n_i$$$.You have to choose exactly two sequences $$$i$$$ and $$$j$$$ ($$$i \ne j$$$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $$$i$$$ (its length will be equal to $$$n_i - 1$$$) equals to the sum of the changed sequence $$$j$$$ (its length will be equal to $$$n_j - 1$$$).Note that it's required to remove exactly one element in each of the two chosen sequences.Assume that the sum of the empty (of the length equals $$$0$$$) sequence is $$$0$$$.

The first line contains an integer $$$k$$$ ($$$2 \le k \le 2 \cdot 10^5$$$) — the number of sequences. Then $$$k$$$ pairs of lines follow, each pair containing a sequence. The first line in the $$$i$$$-th pair contains one integer $$$n_i$$$ ($$$1 \le n_i < 2 \cdot 10^5$$$) — the length of the $$$i$$$-th sequence. The second line of the $$$i$$$-th pair contains a sequence of $$$n_i$$$ integers $$$a_{i, 1}, a_{i, 2}, \dots, a_{i, n_i}$$$. The elements of sequences are integer numbers from $$$-10^4$$$ to $$$10^4$$$. The sum of lengths of all given sequences don't exceed $$$2 \cdot 10^5$$$, i.e. $$$n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5$$$.

If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers $$$i$$$, $$$x$$$ ($$$1 \le i \le k, 1 \le x \le n_i$$$), in the third line — two integers $$$j$$$, $$$y$$$ ($$$1 \le j \le k, 1 \le y \le n_j$$$). It means that the sum of the elements of the $$$i$$$-th sequence without the element with index $$$x$$$ equals to the sum of the elements of the $$$j$$$-th sequence without the element with index $$$y$$$. Two chosen sequences must be distinct, i.e. $$$i \ne j$$$. You can print them in any order. If there are multiple possible answers, print any of them.

NoteIn the first example there are two sequences $$$[2, 3, 1, 3, 2]$$$ and $$$[1, 1, 2, 2, 2, 1]$$$. You can remove the second element from the first sequence to get $$$[2, 1, 3, 2]$$$ and you can remove the sixth element from the second sequence to get $$$[1, 1, 2, 2, 2]$$$. The sums of the both resulting sequences equal to $$$8$$$, i.e. the sums are equal.

use warnings;
use strict;
$\ = qq/\n/, $, = qq/ /;
sub sum {
    my $result = 0;
    $result += $_ for @{ $_ [ 0 ] };
    $result;
}
my $k = <STDIN>;
chomp ( $k );
my %h = ();
for my $i ( 1 .. $k ) {
    chomp ( my $n = <STDIN> );
    my @a = split qq/ /, <STDIN>;
    my $s = sum ( \@a );
    my $index = 1;
    for my $j ( @a ) {
        if ( exists $h { $s - $j } and $h { $s - $j } -> [ 0 ] != $i ) {
            print "YES";
            print + $i, $index;
            print + $h { $s - $j } -> [ 0 ], $h { $s - $j } -> [ 1 ];
            exit;
        } else { $h { $s - $j } = [ $i, $index ] }
        $index += 1;
    }
}
print "NO";

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my %h;

	for my $i ( 1 .. $_ ){
		<>;
		@_ = split ' ', <>;
		my $sum = 0;
		$sum += $_ for @_;

		for my $j ( 0 .. @_ - 1 ){
			$h{ $sum - $_[ $j ] }{ $i } = $j + 1;
			}
		}

	my $ok = 0;

	for my $i ( keys %h ){
		my @keys = keys %{ $h{ $i } };
		next if @keys < 2;
		print "YES";
		print $keys[ 0 ], ' ', $h{ $i }{ $keys[ 0 ] };
		print $keys[ 1 ], ' ', $h{ $i }{ $keys[ 1 ] };
		$ok = 1;
		last;
		}

	$ok or print "NO";
	}

use warnings;
use strict;
$\ = qq/\n/, $, = qq/ /;
sub sum {
    my $result = 0;
    $result += $_ for @{ $_ [ 0 ] };
    $result;
}
my $k = <STDIN>;
chomp ( $k );
my %h = ();
for my $i ( 1 .. $k ) {
    chomp ( my $n = <STDIN> );
    my @a = split qq/ /, <STDIN>;
    my $s = sum ( \@a );
    my $index = 1;
    for my $j ( @a ) {
        if ( exists $h { $s - $j } and $h { $s - $j } -> [ 0 ] != $i ) {
            print "YES";
            print + $i, $j;
            print + $h { $s - $j } -> [ 0 ], $h { $s - $j } -> [ 1 ];
            exit;
        } else { $h { $s - $j } = [ $i, $index ] }
        $index += 1;
    }
}
print "NO";


Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.

The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50). Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.

Print a single number — the minimum number of operations Simon needs to carry out his idea.

NoteIn the first sample, the sequence of operations can be like this:    For the first time you need to choose cell (2, 2) and corner (1, 1).  For the second time you need to choose cell (2, 2) and corner (3, 3).  For the third time you need to choose cell (2, 2) and corner (3, 1).  For the fourth time you need to choose cell (2, 2) and corner (1, 3). In the second sample the sequence of operations can be like this:    For the first time you need to choose cell (3, 1) and corner (4, 3).  For the second time you need to choose cell (2, 3) and corner (1, 1).

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

<>=~/1/ and $F++;
while(<>){
	/^1|1$/ and $F++;
	 $a=$_;
	}
$a=~/1/ and $F++;

print $F?2:4;

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

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

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

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

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

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

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

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

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

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

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

<>=~/1/ and $F++;
while($a=<>){
	$a=~/^1|1$/ and $F++;
	}
$a=~/1/ and $F++;

print $F?2:4;

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

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

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

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

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

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

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

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

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

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

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

<>=~/1/ and $F++;
while(<>){
	/^1|1$/ and $F++;
	}
/1/ and $F++;

print $F?2:4;

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

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

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

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

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

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

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

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

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

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


A permutation is a sequence of $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all the numbers occur exactly once. For example, $$$[1]$$$, $$$[3, 5, 2, 1, 4]$$$, $$$[1, 3, 2]$$$ are permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$ are not.Polycarp was given four integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n)$$$ and $$$s$$$ ($$$1 \le s \le \frac{n (n+1)}{2}$$$) and asked to find a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$ that satisfies the following condition:   $$$s = p_l + p_{l+1} + \ldots + p_r$$$. For example, for $$$n=5$$$, $$$l=3$$$, $$$r=5$$$, and $$$s=8$$$, the following permutations are suitable (not all options are listed):   $$$p = [3, 4, 5, 2, 1]$$$;  $$$p = [5, 2, 4, 3, 1]$$$;  $$$p = [5, 2, 1, 3, 4]$$$.  But, for example, there is no permutation suitable for the condition above for $$$n=4$$$, $$$l=1$$$, $$$r=1$$$, and $$$s=5$$$.Help Polycarp, for the given $$$n$$$, $$$l$$$, $$$r$$$, and $$$s$$$, find a permutation of numbers from $$$1$$$ to $$$n$$$ that fits the condition above. If there are several suitable permutations, print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$). Then $$$t$$$ test cases follow. Each test case consist of one line with four integers $$$n$$$ ($$$1 \le n \le 500$$$), $$$l$$$ ($$$1 \le l \le n$$$), $$$r$$$ ($$$l \le r \le n$$$), $$$s$$$ ($$$1 \le s \le \frac{n (n+1)}{2}$$$). It is guaranteed that the sum of $$$n$$$ for all input data sets does not exceed $$$500$$$.

For each test case, output on a separate line:    $$$n$$$ integers — a permutation of length $$$n$$$ that fits the condition above if such a permutation exists;  -1, otherwise.  If there are several suitable permutations, print any of them.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $L, $R, $s ) = split;

	my $diff = $R - $L + 1;

	my @arr = 1 .. $diff;

	my $sum = 0;

	$sum += $_ for @arr;

	if( $s < $sum ){
		print -1;
		next;
		}

	my $i = @arr - 1;

	while( $s != $sum ){
		$arr[ $i ] ++;
		$sum ++;
		$i --;
		$i < 0 and $i = @arr - 1;
		}

	if( $arr[ @arr - 1 ] > $n ){
		print -1;
		next;
		}

	my %h;

	$h{ $_ } ++ for @arr;

	for( 1 .. $n ){
		next if exists $h{ $_ };

		push @arr, $_;
		}

	for( 1 .. $L - 1 ){
		my $tmp = pop @arr;
		unshift @arr, $tmp;
		}

	print "@arr";
	}


Polycarp came up with a new programming language. There are only two types of statements in it:   "x := s": assign the variable named x the value s (where s is a string). For example, the statement var := hello assigns the variable named var the value hello. Note that s is the value of a string, not the name of a variable. Between the variable name, the := operator and the string contains exactly one space each. "x = a + b": assign the variable named x the concatenation of values of two variables a and b. For example, if the program consists of three statements a := hello, b := world, c = a + b, then the variable c will contain the string helloworld. It is guaranteed that the program is correct and the variables a and b were previously defined. There is exactly one space between the variable names and the = and + operators. All variable names and strings only consist of lowercase letters of the English alphabet and do not exceed $$$5$$$ characters.The result of the program is the number of occurrences of string haha in the string that was written to the variable in the last statement.Polycarp was very tired while inventing that language. He asks you to implement it. Your task is — for given program statements calculate the number of occurrences of string haha in the last assigned variable.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of statements in the program. All variable names and strings are guaranteed to consist only of lowercase letters of the English alphabet and do not exceed $$$5$$$ characters. This is followed by $$$n$$$ lines describing the statements in the format described above. It is guaranteed that the program is correct.

For each set of input data, output the number of occurrences of the haha substring in the string that was written to the variable in the last statement.

4
6
a := h
b := aha
c = a + b
c = c + c
e = c + c
d = a + c
15
x := haha
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
1
haha := hah
5
haahh := aaaha
ahhhh = haahh + haahh
haahh = haahh + haahh
ahhhh = ahhhh + haahh
ahhaa = haahh + ahhhh

NoteIn the first test case the resulting value of d is hhahahaha.


In the Bus of Characters there are $$$n$$$ rows of seat, each having $$$2$$$ seats. The width of both seats in the $$$i$$$-th row is $$$w_i$$$ centimeters. All integers $$$w_i$$$ are distinct.Initially the bus is empty. On each of $$$2n$$$ stops one passenger enters the bus. There are two types of passengers:   an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it;  an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the number of rows in the bus. The second line contains the sequence of integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^{9}$$$), where $$$w_i$$$ is the width of each of the seats in the $$$i$$$-th row. It is guaranteed that all $$$w_i$$$ are distinct. The third line contains a string of length $$$2n$$$, consisting of digits '0' and '1' — the description of the order the passengers enter the bus. If the $$$j$$$-th character is '0', then the passenger that enters the bus on the $$$j$$$-th stop is an introvert. If the $$$j$$$-th character is '1', the the passenger that enters the bus on the $$$j$$$-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i. e. both numbers equal $$$n$$$), and for each extrovert there always is a suitable row.

Print $$$2n$$$ integers — the rows the passengers will take. The order of passengers should be the same as in input.

NoteIn the first example the first passenger (introvert) chooses the row $$$2$$$, because it has the seats with smallest width. The second passenger (introvert) chooses the row $$$1$$$, because it is the only empty row now. The third passenger (extrovert) chooses the row $$$1$$$, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row $$$2$$$, because it is the only row with an empty place.

my $n = <>;
my @sizes = split ' ', <>;
my $line = <>; chomp $line;
my (@is, @es);
@is = sort { @sizes[$b - 1] <=> @sizes[$a - 1] } (1..$n);
for my $i (split '', $line) {
    if ($i eq '0') {
        $_ = pop @is;
        push @es, $_;
    } else {
        $_ = pop @es;
    }
    print $_ . ' ';
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my %h;

	my $i = 0;

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

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

	$_ = <>, chomp;

	my @ans;
	my @stack;

	for( split // ){
		if( $_ ){
			push @ans, $h{ pop @stack };
			}
		else{
			my $w = shift @_;
			push @ans, $h{ $w };
			push @stack, $w;
			}
		}

	print "@ans";
	}


You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$.There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$.You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room.The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).You can't rotate any of the tables, but you can move the first table inside the room.   Example of how you may move the first table. What is the minimum distance you should move the first table to free enough space for the second one?

The first line contains the single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of the test cases. The first line of each test case contains two integers $$$W$$$ and $$$H$$$ ($$$1 \le W, H \le 10^8$$$) — the width and the height of the room. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$0 \le x_1 < x_2 \le W$$$; $$$0 \le y_1 < y_2 \le H$$$) — the coordinates of the corners of the first table. The third line contains two integers $$$w$$$ and $$$h$$$ ($$$1 \le w \le W$$$; $$$1 \le h \le H$$$) — the width and the height of the second table.

For each test case, print the minimum distance you should move the first table, or $$$-1$$$ if there is no way to free enough space for the second table. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.

NoteThe configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by $$$(0, -1)$$$, so the lower left corner will move from $$$(2, 1)$$$ to $$$(2, 0)$$$. Then you can place the second table at $$$(0, 3)-(4, 5)$$$.In the second test case, there is no way to fit both tables in the room without intersecting.In the third test case, you can move the first table by $$$(0, 2)$$$, so the lower left corner will move from $$$(0, 3)$$$ to $$$(0, 5)$$$.


We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:  The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers.  The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$.  If you sort the array in increasing order (such that $$$a_1 < a_2 < \ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x < y \le 50$$$) — the length of the array and two elements that are present in the array, respectively.

For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.


You may have already known that a standard ICPC team consists of exactly three members. The perfect team however has more restrictions. A student can have some specialization: coder or mathematician. She/he can have no specialization, but can't have both at the same time.So the team is considered perfect if it includes at least one coder, at least one mathematician and it consists of exactly three members.You are a coach at a very large university and you know that $$$c$$$ of your students are coders, $$$m$$$ are mathematicians and $$$x$$$ have no specialization.What is the maximum number of full perfect teams you can distribute them into? Note that some students can be left without a team and each student can be a part of no more than one team.You are also asked to answer $$$q$$$ independent queries.

The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of queries.  Each of the next $$$q$$$ lines contains three integers $$$c$$$, $$$m$$$ and $$$x$$$ ($$$0 \le c, m, x \le 10^8$$$) — the number of coders, mathematicians and students without any specialization in the university, respectively. Note that the no student is both coder and mathematician at the same time.

Print $$$q$$$ integers — the $$$i$$$-th of them should be the answer to the $$$i$$$ query in the order they are given in the input. The answer is the maximum number of full perfect teams you can distribute your students into.

NoteIn the first example here are how teams are formed:  the only team of 1 coder, 1 mathematician and 1 without specialization;  all three teams consist of 1 coder and 2 mathematicians;  no teams can be formed;  no teams can be formed;  one team consists of 1 coder, 1 mathematician and 1 without specialization, the rest aren't able to form any team;  one team consists of 1 coder, 1 mathematician and 1 without specialization, one consists of 2 coders and 1 mathematician and one consists of 1 coder and 2 mathematicians.

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

for ( 1..$line ) {
    chomp ($line = <STDIN>);
    my ($c, $m, $x) = split q{ }, $line;
    my $answer = 0;

    $answer = min(min($c, $m), int(($c+$m+$x)/3));

    say $answer;
}

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

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	my( $c, $m, $x ) = split;

	( $c, $m ) = sort { $b <=> $a } $c, $m;

	my( $min ) = sort { $a <=> $b } $c, $m, $x;

	$debug and print "$c, $m, $x";

	map $_ -= $min, $c, $m, $x;

	$debug and print "$c, $m, $x";

	print $min + do {
		if( $x == 0 ){
			( sort { $a <=> $b } int( ( $c + $m ) / 3 ), $m )[ 0 ];
			}
		else{
			0;
			}
		};
	}

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

for ( 1..$line ) {
    chomp ($line = <STDIN>);
    my ($c, $m, $x) = split q{ }, $line;
    my $answer = 0;

    if ( $c >= 1 and $m >= 1 and $x >= 1 ) {
        my $min = min($c, $m, $x);
        $c -= $min, $m -= $min, $x -= $min;
        $answer += $min;
    }

    if ( $c >= 3 and $m >= 3 ) {
        my $min = min(int($c/3), int($m/3));
        $c -= 3*$min, $m -= 3*$min;
        $answer += $min*2;
    }

    if ( $c < $m ) {
        ($c, $m) = ($m, $c);
    }

    if ( $c >= 2 and $m >= 1 ) {
        $c -= 2, $m -= 1;
        $answer += 1;
        next;
    }
    last;

    say $answer;
}

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

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

for ( 1..$line ) {
    chomp ($line = <STDIN>);
    my ($c, $m, $x) = split q{ }, $line;
    my $answer = 0;

    if ( $c >= 1 and $m >= 1 and $x >= 1 ) {
        my $min = min($c, $m, $x);
        $c -= $min, $m -= $min, $x -= $min;
        $answer += $min;
    }

    while ( 1 ) {

        if ( $c < $m ) {
            ($c, $m) = ($m, $c);
        }

        if ( $c >= 2 and $m >= 1 ) {
            my $min = min(int($c/2), $m);
            $c -= 2*$min, $m -= $min;
            $answer += $min;
            next;
        }
        last;
    }

    say $answer;
}

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

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	my( $c, $m, $x ) = split;

	( $c, $m ) = sort { $b <=> $a } $c, $m;

	my( $min ) = sort { $a <=> $b } $c, $m, $x;

	$debug and print "$c, $m, $x";

	map $_ -= $min, $c, $m, $x;

	$debug and print "$c, $m, $x";

	print $min + do {
		if( $x == 0 ){
			int( ( $c + $m ) / 3 );
			}
		else{
			0;
			}
		};
	}


Recall that a permutation of length $$$n$$$ is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once.For a fixed positive integer $$$d$$$, let's define the cost of the permutation $$$p$$$ of length $$$n$$$ as the number of indices $$$i$$$ $$$(1 \le i < n)$$$ such that $$$p_i \cdot d = p_{i + 1}$$$.For example, if $$$d = 3$$$ and $$$p = [5, 2, 6, 7, 1, 3, 4]$$$, then the cost of such a permutation is $$$2$$$, because $$$p_2 \cdot 3 = p_3$$$ and $$$p_5 \cdot 3 = p_6$$$.Your task is the following one: for a given value $$$n$$$, find the permutation of length $$$n$$$ and the value $$$d$$$ with maximum possible cost (over all ways to choose the permutation and $$$d$$$). If there are multiple answers, then print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print the value $$$d$$$ in the first line, and $$$n$$$ integers in the second line — the permutation itself. If there are multiple answers, then print any of them.


Recently, Masha was presented with a chessboard with a height of $$$n$$$ and a width of $$$m$$$.The rows on the chessboard are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Therefore, each cell can be specified with the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number, and $$$y$$$ is the row number (do not mix up).Let us call a rectangle with coordinates $$$(a,b,c,d)$$$ a rectangle lower left point of which has coordinates $$$(a,b)$$$, and the upper right one — $$$(c,d)$$$.The chessboard is painted black and white as follows:   An example of a chessboard. Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat — they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle $$$(x_1,y_1,x_2,y_2)$$$. Then after him Denis spilled black paint on the rectangle $$$(x_3,y_3,x_4,y_4)$$$.To spill paint of color $$$color$$$ onto a certain rectangle means that all the cells that belong to the given rectangle become $$$color$$$. The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each of them is described in the following format: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^9$$$) — the size of the board. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \le x_1 \le x_2 \le m, 1 \le y_1 \le y_2 \le n$$$) — the coordinates of the rectangle, the white paint was spilled on. The third line contains four integers $$$x_3$$$, $$$y_3$$$, $$$x_4$$$, $$$y_4$$$ ($$$1 \le x_3 \le x_4 \le m, 1 \le y_3 \le y_4 \le n$$$) — the coordinates of the rectangle, the black paint was spilled on.

Output $$$t$$$ lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.

NoteExplanation for examples:The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).In the first test, the paint on the field changed as follows:  In the second test, the paint on the field changed as follows:  In the third test, the paint on the field changed as follows:  In the fourth test, the paint on the field changed as follows:  In the fifth test, the paint on the field changed as follows:

use strict;
use warnings;
use integer;

my $q = <>;
for (my $i = 0; $i < $q; $i++) {core();};

sub core {
	my ($n, $m) = readpos(); # board sizes
	my ($wx1, $wy1, $wx2, $wy2) = readrect(); # spill white rectangle
	my ($bx1, $by1, $bx2, $by2) = readrect(); # spill black rectangle

	my ($white, $black) = aftercolors($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);
	print "$white $black\n";
}

sub readpos {
	<> =~ /(\d+)\s+(\d+)/;
	return ($1, $2);
}

sub readrect {
	<> =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
	return ($1, $2, $3, $4);
}

sub aftercolors {
	my ($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;
	my ($boardwhite, $boardblack) = colors(1, 1, $m, $n);

	my ($whiteunderwhitespillarea, $blackunderwhitespillarea) = colors($wx1, $wy1, $wx2, $wy2);
	my ($whiteunderblackspillarea, $blackunderblackspillarea) = colors($bx1, $by1, $bx2, $by2);

	my ($cx1, $cy1, $cx2, $cy2) = commonargs($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);
	my ($whiteundercommon, $blackundercommon) = colors($cx1, $cy1, $cx2, $cy2);

	my $white = $boardwhite + $blackunderwhitespillarea - $blackundercommon - $whiteunderblackspillarea;
	my $black = $boardblack - $blackunderwhitespillarea + $blackundercommon + $whiteunderblackspillarea;

	return ($white, $black);
}

sub valid {
	my ($x1, $y1, $x2, $y2) = @_;
	return $x1 <= $x2 && $y1 <= $y2;
}

sub commonargs {
	my ($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;
	return (max($wx1, $bx1), max($wy1, $by1), min($wx2, $bx2), min($wy2, $by2));
}

sub colors {
	my ($x1, $y1, $x2, $y2) = @_;
	my $area = area($x1, $y1, $x2, $y2);

	if ($area % 2 == 0) {
		my $halfarea = $area / 2;
		return ($halfarea, $halfarea);
	} else {
		my ($more, $less) = (($area + 1) / 2, ($area - 1) / 2);
		return colorpos("white", $x1, $y1) ? ($more, $less) : ($less, $more);
	}
}

sub colorpos {
	my ($color, $x, $y) = @_;
	my $diagonal = $x % 2 == $y % 2;
	return $color eq "white" ? $diagonal : !$diagonal;
}

sub area {
	my ($x1, $y1, $x2, $y2) = @_;
	if (valid($x1, $y1, $x2, $y2)) {
		my $lx = $x2 - $x1 + 1;
		my $ly = $y2 - $y1 + 1;
		return $lx * $ly;
	} else {
		return 0;
	}
}

# use List::Util qw[min max];
sub min {
	my ($a, $b) = @_;
	return $a <= $b ? $a : $b;
}
sub max {
	my ($a, $b) = @_;
	return $a <= $b ? $b : $a;
}

use strict;
use warnings;
use integer;

my $q = <>;
for (my $i = 0; $i < $q; $i++) {core();};

sub core {
	my ($n, $m) = readpos(); # board sizes
	my ($wx1, $wy1, $wx2, $wy2) = readrect(); # spill white rectangle
	my ($bx1, $by1, $bx2, $by2) = readrect(); # spill black rectangle

	my ($white, $black) = aftercolors($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);
	print "$white $black\n";
}

sub readpos {
	<> =~ /(\d+)\s+(\d+)/;
	return ($1, $2);
}

sub readrect {
	<> =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
	return ($1, $2, $3, $4);
}

sub aftercolors {
	my ($m, $n, $wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;
	my $boardwhite    = white(1, 1, $m, $n);
	my $boardblack    = black(1, 1, $m, $n);

	my $whiteunderwhitespillarea = white($wx1, $wy1, $wx2, $wy2);
	my $blackunderwhitespillarea = black($wx1, $wy1, $wx2, $wy2);
	my $whiteunderblackspillarea = white($bx1, $by1, $bx2, $by2);
	my $blackunderblackspillarea = black($bx1, $by1, $bx2, $by2);

	my ($cx1, $cy1, $cx2, $cy2) = commonargs($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2);

	my $whiteundercommon = white($cx1, $cy1, $cx2, $cy2);
	my $blackundercommon = black($cx1, $cy1, $cx2, $cy2);

	my $white = $boardwhite + $blackunderwhitespillarea - $blackundercommon - $whiteunderblackspillarea;
	my $black = $boardblack - $blackunderwhitespillarea + $blackundercommon + $whiteunderblackspillarea;

	return ($white, $black);
}

sub valid {
	my ($x1, $y1, $x2, $y2) = @_;
	return $x1 <= $x2 && $y1 <= $y2;
}

sub commonargs {
	my ($wx1, $wy1, $wx2, $wy2, $bx1, $by1, $bx2, $by2) = @_;
	return (max($wx1, $bx1), max($wy1, $by1), min($wx2, $bx2), min($wy2, $by2));
}

sub color {
	my ($color, $x1, $y1, $x2, $y2) = @_;
	my $area = area($x1, $y1, $x2, $y2);

	if ($area % 2 == 0) {
		return $area / 2;
	} else {
		return colorpos($color, $x1, $y1) ? ($area + 1) / 2 : ($area - 1) / 2;
	}
}

sub colorpos {
	my ($color, $x, $y) = @_;
	my $diagonal = $x % 2 == $y % 2;
	return $color eq "white" ? $diagonal : !$diagonal;
}

sub white {
	my ($x1, $y1, $x2, $y2) = @_;
	return color("white", $x1, $y1, $x2, $y2);
}

sub black {
	my ($x1, $y1, $x2, $y2) = @_;
	return color("black", $x1, $y1, $x2, $y2);
}

sub area {
	my ($x1, $y1, $x2, $y2) = @_;
	if (valid($x1, $y1, $x2, $y2)) {
		my $lx = $x2 - $x1 + 1;
		my $ly = $y2 - $y1 + 1;
		return $lx * $ly;
	} else {
		return 0;
	}
}

# use List::Util qw[min max];
sub min {
	my ($a, $b) = @_;
	return $a <= $b ? $a : $b;
}
sub max {
	my ($a, $b) = @_;
	return $a <= $b ? $b : $a;
}

use strict;
use warnings;
use integer;

<>;
while (<>) {
	/(\d+)\s+(\d+)/;
	my $r = sumbetween($1, $2);
	print $r, "\n";
}

sub sumbetween {
	my ($beg, $end) = @_;
	my $all = mount($end);
	my $before = mount($beg - 1);
	return $all - $before;
}

sub mount {
	my $i = shift(@_);
	my $sign = $i % 2 == 0 ? +1 : -1;
	# print "(", $sign * upperdiv($i, 2), ")";
	return $sign * upperdiv($i, 2);
}

sub upperdiv {
	my ($a, $b) = @_;
	my $r = $a / $b;
	$r++ if $a % $b > 0;
	return $r;
}


Diamond Miner is a game that is similar to Gold Miner, but there are $$$n$$$ miners instead of $$$1$$$ in this game.The mining area can be described as a plane. The $$$n$$$ miners can be regarded as $$$n$$$ points on the y-axis. There are $$$n$$$ diamond mines in the mining area. We can regard them as $$$n$$$ points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point $$$(0, 0)$$$). Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point $$$(a,b)$$$ uses his hook to mine a diamond mine at the point $$$(c,d)$$$, he will spend $$$\sqrt{(a-c)^2+(b-d)^2}$$$ energy to mine it (the distance between these points). The miners can't move or help each other.The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of miners and mines. Each of the next $$$2n$$$ lines contains two space-separated integers $$$x$$$ ($$$-10^8 \le x \le 10^8$$$) and $$$y$$$ ($$$-10^8 \le y \le 10^8$$$), which represent the point $$$(x,y)$$$ to describe a miner's or a diamond mine's position. Either $$$x = 0$$$, meaning there is a miner at the point $$$(0, y)$$$, or $$$y = 0$$$, meaning there is a diamond mine at the point $$$(x, 0)$$$. There can be multiple miners or diamond mines at the same point. It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to $$$n$$$ and the number of points on the y-axis is equal to $$$n$$$. It's guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, print a single real number — the minimal sum of energy that should be spent. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}$$$.

NoteIn the first test case, the miners are at $$$(0,1)$$$ and $$$(0,-1)$$$, while the diamond mines are at $$$(1,0)$$$ and $$$(-2,0)$$$. If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy $$$\sqrt2 + \sqrt5$$$.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;
	my $n = $_;

	@_ = map ~~<>, 1 .. 2 * $n;

	my @miners;
	my @mines;

	for( @_ ){
		my( $x, $y ) = split;

		if( $x == 0 ){
			push @miners, $y;
			}
		else{
			push @mines, $x;
			}
		}

	$_ = abs $_ for @miners, @mines;

	@miners = sort { $a <=> $b } @miners;
	@mines = sort { $a <=> $b } @mines;

	my $energy = 0;

	for( 1 .. @miners ){
		$energy += sqrt( ( pop @miners ) ** 2 + ( pop @mines ) ** 2 );
		}

	print $energy;
	}


There are $$$n$$$ piranhas with sizes $$$a_1, a_2, \ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely:   The piranha $$$i$$$ can eat the piranha $$$i-1$$$ if the piranha $$$i-1$$$ exists and $$$a_{i - 1} < a_i$$$.  The piranha $$$i$$$ can eat the piranha $$$i+1$$$ if the piranha $$$i+1$$$ exists and $$$a_{i + 1} < a_i$$$. When the piranha $$$i$$$ eats some piranha, its size increases by one ($$$a_i$$$ becomes $$$a_i + 1$$$).Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.For example, if $$$a = [5, 3, 4, 4, 5]$$$, then the third piranha can be dominant. Consider the sequence of its moves:   The piranha eats the second piranha and $$$a$$$ becomes $$$[5, \underline{5}, 4, 5]$$$ (the underlined piranha is our candidate).  The piranha eats the third piranha and $$$a$$$ becomes $$$[5, \underline{6}, 5]$$$.  The piranha eats the first piranha and $$$a$$$ becomes $$$[\underline{7}, 5]$$$.  The piranha eats the second piranha and $$$a$$$ becomes $$$[\underline{8}]$$$. You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of piranhas in the aquarium. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the size of the $$$i$$$-th piranha. It is guaranteed that the sum of $$$n$$$ does not exceed $$$3 \cdot 10^5$$$ ($$$\sum n \le 3 \cdot 10^5$$$).

For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.

NoteThe first test case of the example is described in the problem statement.In the second test case of the example, there are no dominant piranhas in the aquarium.In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes $$$[4, 4, 5, 4]$$$, then it can eat any other piranha in the aquarium.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

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

	print do {
		if( $sorted[ 0 ] == $sorted[ @sorted - 1 ] ){
			-1;
			}
		else{
			my $max = $sorted[ 0 ];
			my $idx;
			for my $i ( 0 .. @_ - 1 ){
				if( $_[ $i ] == $max ){
					$idx = $i + 1;
					last;
					}
				}
			$idx;
			}
		};

	}


Sam lives in Awesomeburg, its downtown has a triangular shape. Also, the following is true about the triangle:  its vertices have integer coordinates,  the coordinates of vertices are non-negative, and  its vertices are not on a single line. He calls a point on the downtown's border (that is the border of the triangle) safe if he can reach this point from at least one point of the line $$$y = 0$$$ walking along some straight line, without crossing the interior of the triangle.  In the picture the downtown is marked with grey color. The first path is invalid because it does not go along a straight line. The second path is invalid because it intersects with the interior of the downtown. The third and fourth paths are correct. Find the total length of the unsafe parts of the downtown border. It can be proven that these parts are segments and their number is finite.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Description of the test cases follows. Each test case contains three lines, each of them contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 \le x_i, y_i \le 10^9$$$) — coordinates of the vertices of the downtown's border.

For each test case print a single number — the answer to the problem. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Formally let your answer be $$$a$$$, jury answer be $$$b$$$. Your answer will be considered correct if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}$$$.

NoteIn the picture, the downtowns of the first three test cases are illustrated. Triangles are enumerated according to the indices of test cases they belong to.  In the first two test cases, all points on the borders of the downtowns are safe, thus the answers are $$$0$$$.In the following picture unsafe points for the third test case are marked with black color:  In the fourth test case, all points on the border of the downtown are safe.


Circular land is an $$$2n \times 2n$$$ grid. Rows of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from top to bottom and columns of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and column $$$y$$$ for $$$1 \leq x \leq 2n$$$ and $$$1 \leq y \leq 2n$$$.There are $$$n^2$$$ of your friends in the top left corner of the grid. That is, in each cell $$$(x, y)$$$ with $$$1 \leq x, y \leq n$$$ there is exactly one friend. Some of the other cells are covered with snow.Your friends want to get to the bottom right corner of the grid. For this in each cell $$$(x, y)$$$ with $$$n+1 \leq x, y \leq 2n$$$ there should be exactly one friend. It doesn't matter in what cell each of friends will be.You have decided to help your friends to get to the bottom right corner of the grid.For this, you can give instructions of the following types:   You select a row $$$x$$$. All friends in this row should move to the next cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 \leq y < 2n$$$ will move to the cell $$$(x, y + 1)$$$ and friend from the cell $$$(x, 2n)$$$ will move to the cell $$$(x, 1)$$$.  You select a row $$$x$$$. All friends in this row should move to the previous cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 < y \leq 2n$$$ will move to the cell $$$(x, y - 1)$$$ and friend from the cell $$$(x, 1)$$$ will move to the cell $$$(x, 2n)$$$.  You select a column $$$y$$$. All friends in this column should move to the next cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 \leq x < 2n$$$ will move to the cell $$$(x + 1, y)$$$ and friend from the cell $$$(2n, y)$$$ will move to the cell $$$(1, y)$$$.  You select a column $$$y$$$. All friends in this column should move to the previous cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 < x \leq 2n$$$ will move to the cell $$$(x - 1, y)$$$ and friend from the cell $$$(1, y)$$$ will move to the cell $$$(2n, y)$$$. Note how friends on the grid border behave in these instructions.  Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow. You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill.In order to save your friends you can remove snow from some cells before giving the first instruction:   You can select the cell $$$(x, y)$$$ that is covered with snow now and remove snow from this cell for $$$c_{x, y}$$$ coins. You can do this operation any number of times.You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill.Please, find how many coins you will spend.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \leq n \leq 250$$$). Each of the next $$$2n$$$ lines contains $$$2n$$$ integers $$$c_{i, 1}, c_{i, 2}, \ldots, c_{i, 2n}$$$ ($$$0 \leq c_{i, j} \leq 10^9$$$) — costs of removing snow from cells. If $$$c_{i, j} = 0$$$ for some $$$i, j$$$ than there is no snow in cell $$$(i, j)$$$. Otherwise, cell $$$(i, j)$$$ is covered with snow. It is guaranteed that $$$c_{i, j} = 0$$$ for $$$1 \leq i, j \leq n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$250$$$.

For each test case output one integer — the minimal number of coins you should spend.

NoteIn the first test case you can remove snow from the cells $$$(2, 1)$$$ and $$$(2, 2)$$$ for $$$100$$$ coins. Then you can give instructions   All friends in the first collum should move to the previous cell. After this, your friend will be in the cell $$$(2, 1)$$$.  All friends in the second row should move to the next cell. After this, your friend will be in the cell $$$(2, 2)$$$. In the second test case you can remove all snow from the columns $$$3$$$ and $$$4$$$ for $$$22$$$ coins. Then you can give instructions   All friends in the first row should move to the next cell.  All friends in the first row should move to the next cell.  All friends in the second row should move to the next cell.  All friends in the second row should move to the next cell.  All friends in the third column should move to the next cell.  All friends in the third column should move to the next cell.  All friends in the fourth column should move to the next cell.  All friends in the fourth column should move to the next cell. It can be shown that none of the friends will become ill and that it is impossible to spend less coins.


You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters.A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, "defor" is a substring of "codeforces" and "fors" is not. The length of the substring is the number of letters in it.Let's call some string of length $$$n$$$ diverse if and only if there is no letter to appear strictly more than $$$\frac n 2$$$ times. For example, strings "abc" and "iltlml" are diverse and strings "aab" and "zz" are not.Your task is to find any diverse substring of string $$$s$$$ or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the length of string $$$s$$$. The second line is the string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters.

Print "NO" if there is no diverse substring in the string $$$s$$$. Otherwise the first line should contain "YES". The second line should contain any diverse substring of string $$$s$$$.

NoteThe first example has lots of correct answers. Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to "No comments" answer.


There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.    Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).

The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.

Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.

NoteIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.

my $input = <STDIN>;
chomp ($input);
my @data = split (" ", $input);
my $n = shift (@data);
my $k = shift (@data);

my $input = <STDIN>;
chomp ($input);
my @data = split (" ", $input);

my $i = 1;
my @h;
while ($i != $n + 1) {
    $h[$i] = shift (@data);
    $i++;
}

if ($n == $k) {
    print $h[1];
    exit;
}

my @s;
my $j = 1;
while ($j != $n - 1) {
    $s[$j] = $h[$j] + $h[$j+1] + $h[$j+2];
    $j++;
}

my $j = 1;
my $p = $s[$j];
my $index = 0;
while ($j != $n - 2) {
    if (($p < $s[$j+1]) || ($p == $s[$j+1])) {
        $x = 0;
    }else{
        $p = $s[$j+1];
        $index = $j + 1;
    }
    $j++;
}

print $index;


Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. Artem wants to increment the values in some cells by one to make $$$a$$$ good.More formally, find a good matrix $$$b$$$ that satisfies the following condition —   For all valid ($$$i,j$$$), either $$$b_{i,j} = a_{i,j}$$$ or $$$b_{i,j} = a_{i,j}+1$$$. For the constraints of this problem, it can be shown that such a matrix $$$b$$$ always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i,j}$$$ ($$$1 \leq a_{i,j} \leq 10^9$$$).

For each case, output $$$n$$$ lines each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$b_{i,j}$$$.

NoteIn all the cases, you can verify that no two adjacent cells have the same value and that $$$b$$$ is the same as $$$a$$$ with some values incremented by one.


Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square.A number x is said to be a perfect square if there exists an integer y such that x = y2.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array. The second line contains n integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — the elements of the array. It is guaranteed that at least one element of the array is not a perfect square.

Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.

NoteIn the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	print +( grep { /-/ || ( sqrt ) =~ /\./ } sort { $b <=> $a } split ' ', <> )[ 0 ]
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	print +( grep { ( sqrt ) =~ /\./ } grep !/-/, sort { $b <=> $a } split ' ', <> )[ 0 ]
	}


In this problem we consider a special type of an auction, which is called the second-price auction. As in regular auction n bidders place a bid which is price a bidder ready to pay. The auction is closed, that is, each bidder secretly informs the organizer of the auction price he is willing to pay. After that, the auction winner is the participant who offered the highest price. However, he pay not the price he offers, but the highest price among the offers of other participants (hence the name: the second-price auction).Write a program that reads prices offered by bidders and finds the winner and the price he will pay. Consider that all of the offered prices are different.

The first line of the input contains n (2 ≤ n ≤ 1000) — number of bidders. The second line contains n distinct integer numbers p1, p2, ... pn, separated by single spaces (1 ≤ pi ≤ 10000), where pi stands for the price offered by the i-th bidder.

The single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based.


Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?Leha has two strings s and t. The hacker wants to change the string s at such way, that it can be found in t as a substring. All the changes should be the following: Leha chooses one position in the string s and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string s="ab?b" as a result, it will appear in t="aabrbb" as a substring.Guaranteed that the length of the string s doesn't exceed the length of the string t. Help the hacker to replace in s as few symbols as possible so that the result of the replacements can be found in t as a substring. The symbol "?" should be considered equal to any other symbol.

The first line contains two integers n and m (1 ≤ n ≤ m ≤ 1000) — the length of the string s and the length of the string t correspondingly. The second line contains n lowercase English letters — string s. The third line contains m lowercase English letters — string t.

In the first line print single integer k — the minimal number of symbols that need to be replaced. In the second line print k distinct integers denoting the positions of symbols in the string s which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 1;

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

	my @C;

	while( $t =~ /(?=(.{$n}))/g ){
		my $T = $1;
		my $S = $s;
		my @c;
		for( my $i = 0; $i < $n; $i ++ ){
			$T =~ s/.//;
			my $cT = $&;
			$S =~ s/.//;
			my $cS = $&;
			$cS eq $cT or push @c, $i + 1;
		}
		push @C, @c . "\n" . join ' ', @c;
		}

	my $min = ( sort { $a <=> $b } @C )[ 0 ];
	print $min;

	}


Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

NoteIn the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.


You have $$$n$$$ chains, the $$$i$$$-th chain consists of $$$c_i$$$ vertices. Vertices in each chain are numbered independently from $$$1$$$ to $$$c_i$$$ along the chain. In other words, the $$$i$$$-th chain is the undirected graph with $$$c_i$$$ vertices and $$$(c_i - 1)$$$ edges connecting the $$$j$$$-th and the $$$(j + 1)$$$-th vertices for each $$$1 \le j < c_i$$$.Now you decided to unite chains in one graph in the following way:   the first chain is skipped;  the $$$1$$$-st vertex of the $$$i$$$-th chain is connected by an edge with the $$$a_i$$$-th vertex of the $$$(i - 1)$$$-th chain;  the last ($$$c_i$$$-th) vertex of the $$$i$$$-th chain is connected by an edge with the $$$b_i$$$-th vertex of the $$$(i - 1)$$$-th chain.   Picture of the first test case. Dotted lines are the edges added during uniting process Calculate the length of the longest simple cycle in the resulting graph.A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of chains you have. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$2 \le c_i \le 10^9$$$) — the number of vertices in the corresponding chains. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_1 = -1$$$; $$$1 \le a_i \le c_{i - 1}$$$). The fourth line of each test case contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$b_1 = -1$$$; $$$1 \le b_i \le c_{i - 1}$$$). Both $$$a_1$$$ and $$$b_1$$$ are equal to $$$-1$$$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, print the length of the longest simple cycle.

NoteIn the first test case, the longest simple cycle is shown below:   We can't increase it with the first chain, since in such case it won't be simple — the vertex $$$2$$$ on the second chain will break simplicity.


Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of m pieces of paper in the garland. Calculate what the maximum total area of the pieces of paper in the garland Vasya can get.

The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.

Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.

NoteIn the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.

sub n{print "\n"}
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

while(<>){

	$a=$_;
	$b=<>;
	chomp $a;
	chomp $b;
	#a..z
	#count a string 1
	#count a string 2
	#cnt1 <=> cnt 2

	$ans=0;
	$f=0;
	for $i('a'..'z'){

		$cnt1 = count($i,(split//,$a));
		$cnt2 = count($i,(split//,$b));
		if (!$cnt1 and $cnt2){$f++}

		if ($cnt1 < $cnt2){ $ans += $cnt1}
		else { $ans += $cnt2 }

	}
	if ($f){print -1;next}
	print $ans? $ans : -1 ;
	n();

	}

#Scott Heinrich - A01273823
use strict;
use warnings;
use 5.010;

my $count = 0;
my $line1 = <>;
chomp($line1);
my $line2 = <>;
chomp($line2);
my @n = split("",$line1);
my @m = split("",$line2);
my %nhash;
my %mhash;
my $pass = 1;

#make both hashes, key is character in @n,@m
foreach (@n)
{
  $nhash{$_}++;
}
foreach (@m)
{
  $mhash{$_}++;
}

foreach my $key (keys %mhash)
{
    if(!$nhash{$key})
    {
        $pass = 0;
        last;
    }
    if($nhash{$key} > $mhash{$key})
    {
      $count += $mhash{$key}
    }
    else
    {
      $count += $nhash{$key}
    }
}

if ($pass)
{
  say $count;
}
else
{
  say "-1";
}

#!perl

chomp ($string1 =<>);
chomp ($string2 =<>);

@array1 = split('',$string1);
@array2 = split('',$string2);

%hash1;
%hash2;
for $char (@array1)
{
($hash1{$char})++;
}

for $char (@array2)
{
($hash2{$char})++;
}

$sum;
for (keys(%hash2))
{
    if($hash1{$_} == 0) {$fail = 1;}
    $sum += min($hash1{$_},$hash2{$_});
}

if (($sum == 0)or $fail)
{
 print (-1);
}
else
{
print $sum;
}
<>;
sub min ($$)
{
    if($_[0] < $_[1])
    {
    return $_[0];
    }
    else
    {
    return $_ [1];
    }
}

sub n{print "\n"}
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

while(<>){

	$a=$_;
	$b=<>;
	#a..z
	#count a string 1
	#count a string 2
	#cnt1 <=> cnt 2

	$ans=0;
	for $i('a'..'z'){

		$cnt1 = count($i,(split//,$a));
		$cnt2 = count($i,(split//,$b));

		if ($cnt1 < $cnt2){ $ans += $cnt1}
		else { $ans += $cnt2 }

	}

	print $ans? $ans : -1 ;
	n();

	}

sub n{print "\n"}
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

while(<>){

	$a=$_;
	$b=<>;
	chomp $a;
	chomp $b;
	#a..z
	#count a string 1
	#count a string 2
	#cnt1 <=> cnt 2

	$ans=0;
	for $i('a'..'z'){

		$cnt1 = count($i,(split//,$a));
		$cnt2 = count($i,(split//,$b));

		if ($cnt1 < $cnt2){ $ans += $cnt1}
		else { $ans += $cnt2 }

	}

	print $ans? $ans : -1 ;
	n();

	}

#Scott Heinrich - A01273823
use strict;
use warnings;
use 5.010;

my $count = 0;
my $line1 = <>;
chomp($line1);
my $line2 = <>;
chomp($line2);
my @n = split("",$line1);
my @m = split("",$line2);
my %nhash;
my %mhash;

#make both hashes, key is character in @n,@m
foreach (@n)
{
  $nhash{$_}++;
}
foreach (@m)
{
  $mhash{$_}++;
}

foreach my $key (keys %mhash)
{
    if(!$nhash{$key})
    {
        say "-1";
        last;
    }
    if($nhash{$key} > $mhash{$key})
    {
      $count += $mhash{$key}
    }
    else
    {
      $count += $nhash{$key}
    }
}

if ($count)
{
  say $count;
}

#Scott Heinrich - A01273823
use strict;
use warnings;
use 5.010;

my $count = 0;
my $line1 = <>;
chomp($line1);
my $line2 = <>;
chomp($line2);
my @n = split("",$line1);
my @m = split("",$line2);
my %nhash;
my %mhash;

#make both hashes, key is character in @n,@m
foreach (@n)
{
  $nhash{$_}++;
}
foreach (@m)
{
  $mhash{$_}++;
}

foreach my $key (keys %mhash)
{
    if(!$nhash{$key})
    {
        say "-1";
        last;
    }
    if($nhash{$key} > $mhash{$key})
    {
      $count += $mhash{$key}
    }
    else
    {
      $count += $nhash{$key}
    }
}

if ($count != 0)
{
  say $count;
}

#!perl

chomp ($string1 =<>);
chomp ($string2 =<>);

@array1 = split('',$string1);
@array2 = split('',$string2);

%hash1;
%hash2;
for $char (@array1)
{
($hash1{$char})++;
}

for $char (@array2)
{
($hash2{$char})++;
}

$sum;
for (keys(%hash2))
{

    $sum += min($hash1{$_},$hash2{$_});
}

if ($sum == 0)
{
 print (-1);
}
else
{
print $sum;
}
<>;
sub min ($$)
{
    if($_[0] < $_[1])
    {
    return $_[0];
    }
    else
    {
    return $_ [1];
    }
}


One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.

The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive. We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Print a single integer — the maximum length of a repost chain.

5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya

6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp

 #!perl

 $n = <>;

 $hash { 'polycarp' } = 1;
 for(my $i = 0; $i < $n; $i++)
 {
     my $line = <>;
     chomp $line;
     my @names =  split / reposted /, $line;
     $hash { lc @names[0] } = 1 + $hash { lc @names[1] };
}

 $max= 0;
 for my $length (values %hash)
 {
     if($length > $max)
     {
         $max = $length;
     }
 }

 print $max;


A permutation is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. Let's denote the i-th element of permutation p as pi. We'll call number n the size of permutation p1, p2, ..., pn.Nickolas adores permutations. He likes some permutations more than the others. He calls such permutations perfect. A perfect permutation is such permutation p that for any i (1 ≤ i ≤ n) (n is the permutation size) the following equations hold ppi = i and pi ≠ i. Nickolas asks you to print any perfect permutation of size n for the given n.

A single line contains a single integer n (1 ≤ n ≤ 100) — the permutation size.

If a perfect permutation of size n doesn't exist, print a single integer -1. Otherwise print n distinct integers from 1 to n, p1, p2, ..., pn — permutation p, that is perfect. Separate printed numbers by whitespaces.


Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful. Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters. Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.

The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.

If there is no way of replacing '#' characters which leads to a beautiful string print  - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with. If there are several possible answers, you may output any of them.

Note|s| denotes the length of the string s.