While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly $$$H$$$ centimeters above the water surface. Inessa grabbed the flower and sailed the distance of $$$L$$$ centimeters. Exactly at this point the flower touched the water surface.  Suppose that the lily grows at some point $$$A$$$ on the lake bottom, and its stem is always a straight segment with one endpoint at point $$$A$$$. Also suppose that initially the flower was exactly above the point $$$A$$$, i.e. its stem was vertical. Can you determine the depth of the lake at point $$$A$$$?

The only line contains two integers $$$H$$$ and $$$L$$$ ($$$1 \le H < L \le 10^{6}$$$).

Print a single number — the depth of the lake at point $$$A$$$. The absolute or relative error should not exceed $$$10^{-6}$$$. 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^{-6}$$$.

#! /usr/bin/perl
use strict;
my $row = <STDIN>;
chomp($row);
my @inputs_p = split(' ',$row);
my $h = $inputs_p[0];
my $l = $inputs_p[1];
my $r = $l*$l/$h-$h;
printf("%.13f",$r/2);


You are given an equation:  Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order.

The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0.

In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point.

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

my ($a, $b, $c) = map int, split / /, <>;
if (!$a and !$b and !$c) {
    print "-1\n";
} elsif (!$a and !$b and $c) {
    print "0\n";
} elsif (!$a) {
    my $x = -$c / $b;
    print "1\n$x\n";
} else {
    my %roots;
    my $d = $b ** 2 - 4 * $a * $c;
    if ($d >= 0) {
	my $x = (-$b + sqrt($d)) / (2 * $a);
	$roots{$x} = 1;
	$x = (-$b - sqrt($d)) / (2 * $a);
	$roots{$x} = 1;
	my @k = keys %roots;
	print scalar @k, "\n$k[0]\n";
	if ($k[1]) { print "$k[1]\n"; }
    } else {
	print "0\n";
    }
}


Nicholas, a painter is going to paint several new canvases. Nicholas is sure that the canvases will turn out so great that each one will need framing and being hung on the wall. Frames are what Nicholas decided to begin with. Nicholas has n sticks whose lengths equal a1, a2, ... an. Nicholas does not want to break the sticks or glue them together. To make a h × w-sized frame, he needs two sticks whose lengths equal h and two sticks whose lengths equal w. Specifically, to make a square frame (when h = w), he needs four sticks of the same length.Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of sticks. The second line contains n space-separated integers. The i-th integer equals the length of the i-th stick ai (1 ≤ ai ≤ 100).

Print the single number — the maximum number of frames Nicholas can make for his future canvases.


You are given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract $$$1$$$ from it.In particular, we can apply this operation to the same number several times.We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 \cdot a_2$$$ $$$\dots$$$ $$$\cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1\cdot (-1) \cdot (-1) = 1$$$.What is the minimum cost we will have to pay to do that?

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

Output a single number — the minimal number of coins you need to pay to make the product equal to $$$1$$$.

NoteIn the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins.In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$.In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.

use strict;
use warnings;

use Data::Dumper;

sub main {

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

    my $a= <$in>; chomp($a);
    my @a= split(' ', $a);

    my @z= grep {/^0$/} @a;
    my @nz= grep {!/^0$/} @a;
    my $x= scalar(@z);

    if(scalar(@nz)==0) {
        print $x;
        return;
    }

    my $y= 1;
    for (@nz) {
        $y*= $_;
    }

    for(@nz) {
        $x+= (abs($_) - 1);
    }

    # print "x: $x y : $y\n";

    if(scalar(@z)>0) {
        if($y < 0) {
            $y=($y * (-1));
        }
    }

    if($y < 0) {
        $x+=2;
    }

    print $x;
    return;
}

main() unless caller();

use strict;
use warnings;

use Data::Dumper;

sub main {

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

    my $a= <$in>; chomp($a);
    my @a= split(' ', $a);

    my $x= scalar(grep {/0/} @a);

    my @nz= grep {!/0/} @a;

    if(scalar(@nz)==0) {
        print $x;
        return;
    }

    my $y= 1;
    for (@nz) {
        $y*= $_;
    }

    for(@nz) {
        $x+= (abs($_) - 1);
    }

    if($x>0) {
        if($y < 0) {
            $y=($y * (-1));
        }
    }

    if($y < 0) {
        $x+=2;
    }

    print $x;
    return;
}

main() unless caller();

use strict;
use warnings;

use Data::Dumper;

sub main {

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

    my $a= <$in>; chomp($a);
    my @a= split(' ', $a);

    my $x= scalar(grep {/0/} @a);

    my @nz= grep {!/0/} @a;

    if(scalar(@nz)==0) {
        print $x;
        return;
    }

    my $y= 1;
    for (@nz) {
        $y*= $_;
    }

    for(@nz) {
        $x+= (abs($_) - 1);
    }

    if($y < 0) {
        $x+=2;
    }

    print $x;
    return;
}

main() unless caller();

use strict;
use warnings;

use Data::Dumper;

sub main {

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

    my $a= <$in>; chomp($a);
    my @a= split(' ', $a);

    my $x= scalar(grep {/^0$/} @a);

    my @nz= grep {!/^0$/} @a;

    if(scalar(@nz)==0) {
        print $x;
        return;
    }

    my $y= 1;
    for (@nz) {
        $y*= $_;
    }

    for(@nz) {
        $x+= (abs($_) - 1);
    }

    if($x>0) {
        if($y < 0) {
            $y=($y * (-1));
        }
    }

    if($y < 0) {
        $x+=2;
    }

    print $x;
    return;
}

main() unless caller();


After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are $$$n$$$ crossroads in the line in the town, and there is either the bus or the tram station at each crossroad.The crossroads are represented as a string $$$s$$$ of length $$$n$$$, where $$$s_i = \texttt{A}$$$, if there is a bus station at $$$i$$$-th crossroad, and $$$s_i = \texttt{B}$$$, if there is a tram station at $$$i$$$-th crossroad. Currently Petya is at the first crossroad (which corresponds to $$$s_1$$$) and his goal is to get to the last crossroad (which corresponds to $$$s_n$$$).If for two crossroads $$$i$$$ and $$$j$$$ for all crossroads $$$i, i+1, \ldots, j-1$$$ there is a bus station, one can pay $$$a$$$ roubles for the bus ticket, and go from $$$i$$$-th crossroad to the $$$j$$$-th crossroad by the bus (it is not necessary to have a bus station at the $$$j$$$-th crossroad). Formally, paying $$$a$$$ roubles Petya can go from $$$i$$$ to $$$j$$$ if $$$s_t = \texttt{A}$$$ for all $$$i \le t < j$$$. If for two crossroads $$$i$$$ and $$$j$$$ for all crossroads $$$i, i+1, \ldots, j-1$$$ there is a tram station, one can pay $$$b$$$ roubles for the tram ticket, and go from $$$i$$$-th crossroad to the $$$j$$$-th crossroad by the tram (it is not necessary to have a tram station at the $$$j$$$-th crossroad). Formally, paying $$$b$$$ roubles Petya can go from $$$i$$$ to $$$j$$$ if $$$s_t = \texttt{B}$$$ for all $$$i \le t < j$$$.For example, if $$$s$$$="AABBBAB", $$$a=4$$$ and $$$b=3$$$ then Petya needs:  buy one bus ticket to get from $$$1$$$ to $$$3$$$,  buy one tram ticket to get from $$$3$$$ to $$$6$$$,  buy one bus ticket to get from $$$6$$$ to $$$7$$$. Thus, in total he needs to spend $$$4+3+4=11$$$ roubles. Please note that the type of the stop at the last crossroad (i.e. the character $$$s_n$$$) does not affect the final expense.Now Petya is at the first crossroad, and he wants to get to the $$$n$$$-th crossroad. After the party he has left with $$$p$$$ roubles. He's decided to go to some station on foot, and then go to home using only public transport.Help him to choose the closest crossroad $$$i$$$ to go on foot the first, so he has enough money to get from the $$$i$$$-th crossroad to the $$$n$$$-th, using only tram and bus tickets.

Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The first line of each test case consists of three integers $$$a, b, p$$$ ($$$1 \le a, b, p \le 10^5$$$) — the cost of bus ticket, the cost of tram ticket and the amount of money Petya has. The second line of each test case consists of one string $$$s$$$, where $$$s_i = \texttt{A}$$$, if there is a bus station at $$$i$$$-th crossroad, and $$$s_i = \texttt{B}$$$, if there is a tram station at $$$i$$$-th crossroad ($$$2 \le |s| \le 10^5$$$). It is guaranteed, that the sum of the length of strings $$$s$$$ by all test cases in one test doesn't exceed $$$10^5$$$.

For each test case print one number — the minimal index $$$i$$$ of a crossroad Petya should go on foot. The rest of the path (i.e. from $$$i$$$ to $$$n$$$ he should use public transport).

5
2 2 1
BB
1 1 1
AB
3 2 8
AABBBBAABB
5 3 4
BBBBB
2 1 1
ABABAB

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my $len = 0;

	while( /./ ){
		if( /^A/ ){
			if( $p < $A ){
				last;
				}
			$p -= $A;
			s/^A+//;
			$len += length $&;
			}
		else{
			if( $p < $B ){
				last;
				}
			$p -= $B;
			s/^B+//;
			$len += length $&;
			}
		}

	print $length - $len;
	}


Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 50$$$) — the number of strings and the length of each string. Next $$$n$$$ lines contain a string of length $$$m$$$ each, consisting of lowercase Latin letters only. All strings are distinct.

In the first line, print the length of the longest palindrome string you made. In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.

9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji

NoteIn the first example, "battab" is also a valid answer.In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.In the third example, the empty string is the only valid palindrome string.

#!/usr/bin/perl -w
use feature ":5.20.1";

sub is_palindrome {
    my $ent = $_[0];
    my @string = split("", $ent);
    my $fim = @string - 1;
    my $ini = 0;
    while($ini < $fim) {
        if($string[$ini] ne $string[$fim]){
            return 0;
        }
        $ini++;
        $fim--;
    }
    return 1;
}

chomp($line = <>);
($n, $m) = split " ", $line;
@list = ();

for($i = 0; $i < $n; $i++){
    chomp($line = <>);
    push(@list, $line);
}

$m = @list;
$n = $m;
@v_palin = ();
@par = ();

for($i = 0; $i < $n; $i++){
    push(@par, -1);
}

for($i = 0; $i < $n; $i++){
    if(is_palindrome($list[$i])){
        push(@v_palin, $i);
    }
}

for($i = 0; $i < $n; $i++){
    for($j = 0; $j < $n; $j++){
        if($i == $j || $par[$i] != -1 || $par[$j] != -1){
            next;
        }
        $aux = scalar reverse ($list[$j]);
        if($list[$i] eq $aux){
            $par[$i] = $j;
            $par[$j] = $i;
            last;
        }
    }
}

$res = "";
for($i = 0; $i < $n; $i++){
    if($par[$i] != -1 && $par[$i] > $i){
        $res .= $list[$i];
    }
}
$res2 = scalar reverse($res);
foreach $i (@v_palin){
    if($par[$i] == -1){
        $res .= $list[$i];
        last;
    }
}

$res .= $res2;

say length($res);
if(length($res)){
    say $res;
}

#!/usr/bin/perl -w
use feature ":5.20.1";

sub is_palindrome {
    my $ent = $_[0];
    my @string = split("", $ent);
    my $fim = @string - 1;
    my $ini = 0;
    while($ini < $fim) {
        if($string[$ini] ne $string[$fim]){
            return 0;
        }
        $ini++;
        $fim--;
    }
    return 1;
}

chomp($line = <>);
($n, $m) = split " ", $line;
@list = ();

for($i = 0; $i < $n; $i++){
    chomp($line = <>);
    push(@list, $line);
}

$m = @list;
$n = $m;
@v_palin = ();
@par = ();

for($i = 0; $i < $n; $i++){
    push(@par, -1);
}

for($i = 0; $i < $n; $i++){
    if(is_palindrome($list[$i])){
        push(@v_palin, $i);
    }
}

for($i = 0; $i < $n; $i++){
    for($j = 0; $j < $n; $j++){
        if($i == $j || $par[$i] != -1 || $par[$j] != -1){
            next;
        }
        $aux = scalar reverse ($list[$j]);
        if($list[$i] eq $aux){
            $par[$i] = $j;
            $par[$j] = $i;
            last;
        }
    }
}

$res = "";
for($i = 0; $i < $n; $i++){
    if($par[$i] != -1 && $par[$i] > $i){
        $res .= $list[$i];
    }
}

foreach $i (@v_palin){
    if($par[$i] == -1){
        $res .= $list[$i];
        last;
    }
}

for($i = 0; $i < $n; $i++){
    if($par[$i] != -1 && $par[$i] > $i){
        $res .= $list[$par[$i]];
    }
}

say length($res);
if(length($res)){
    say $res;
}


You are given a rooted tree with vertices numerated from $$$1$$$ to $$$n$$$. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.Ancestors of the vertex $$$i$$$ are all vertices on the path from the root to the vertex $$$i$$$, except the vertex $$$i$$$ itself. The parent of the vertex $$$i$$$ is the nearest to the vertex $$$i$$$ ancestor of $$$i$$$. Each vertex is a child of its parent. In the given tree the parent of the vertex $$$i$$$ is the vertex $$$p_i$$$. For the root, the value $$$p_i$$$ is $$$-1$$$.    An example of a tree with $$$n=8$$$, the root is vertex $$$5$$$. The parent of the vertex $$$2$$$ is vertex $$$3$$$, the parent of the vertex $$$1$$$ is vertex $$$5$$$. The ancestors of the vertex $$$6$$$ are vertices $$$4$$$ and $$$5$$$, the ancestors of the vertex $$$7$$$ are vertices $$$8$$$, $$$3$$$ and $$$5$$$ You noticed that some vertices do not respect others. In particular, if $$$c_i = 1$$$, then the vertex $$$i$$$ does not respect any of its ancestors, and if $$$c_i = 0$$$, it respects all of them.You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex $$$v$$$, all children of $$$v$$$ become connected with the parent of $$$v$$$.    An example of deletion of the vertex $$$7$$$. Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of vertices in the tree. The next $$$n$$$ lines describe the tree: the $$$i$$$-th line contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$1 \le p_i \le n$$$, $$$0 \le c_i \le 1$$$), where $$$p_i$$$ is the parent of the vertex $$$i$$$, and $$$c_i = 0$$$, if the vertex $$$i$$$ respects its parents, and $$$c_i = 1$$$, if the vertex $$$i$$$ does not respect any of its parents. The root of the tree has $$$-1$$$ instead of the parent index, also, $$$c_i=0$$$ for the root. It is guaranteed that the values $$$p_i$$$ define a rooted tree with $$$n$$$ vertices.

In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer $$$-1$$$.

NoteThe deletion process in the first example is as follows (see the picture below, the vertices with $$$c_i=1$$$ are in yellow):  first you will delete the vertex $$$1$$$, because it does not respect ancestors and all its children (the vertex $$$2$$$) do not respect it, and $$$1$$$ is the smallest index among such vertices;  the vertex $$$2$$$ will be connected with the vertex $$$3$$$ after deletion;  then you will delete the vertex $$$2$$$, because it does not respect ancestors and all its children (the only vertex $$$4$$$) do not respect it;  the vertex $$$4$$$ will be connected with the vertex $$$3$$$;  then you will delete the vertex $$$4$$$, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth);  you will just delete the vertex $$$4$$$;  there are no more vertices to delete.   In the second example you don't need to delete any vertex:  vertices $$$2$$$ and $$$3$$$ have children that respect them;  vertices $$$4$$$ and $$$5$$$ respect ancestors.   In the third example the tree will change this way:

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	my %h;
	my %g;
	my $root;
	@_ = ();

	map {
		my( $v, $c ) = split ' ', <>;
		$h{ $_ }{ $v } = 1;
		$h{ $v }{ $_ } = 2;
		$g{ $_ } = $c;
		$v == -1 and $root = $_;
		} 1 .. $_;

	$g{ -1 } = 0;

	$debug and print $root;

	for my $key ( keys %h ){
		next if not $g{ $key };
		next if grep { not $g{ $_ } } grep { $h{ $key }{ $_ } == 2 } keys %{ $h{ $key } };
		push @_, $key;
		}

	@_ or push @_, -1;

	print join ' ', sort { $a <=> $b } @_;
	}


The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.Note that all these questions are independent from each other — the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).

The first line of the input contains a single number n (1 ≤ n ≤ 105) — the number of houses in the capital of Berland. The second line contains n space-separated positive integers hi (1 ≤ hi ≤ 109), where hi equals the number of floors in the i-th house.

Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero. All houses are numbered from left to right, starting from one.

($_ > $max and $max = $_), unshift @a, $max for reverse @_ = split ' ', (<>, <>);
shift @a;
print join ' ', map { (sort {$b <=> $a} ( (shift @a) - $_ + 1, 0) )[0] } @_


A map of some object is a rectangular field consisting of n rows and n columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly k islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island).Find a way to cover some cells with sand so that exactly k islands appear on the n × n map, or determine that no such way exists.

The single line contains two positive integers n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ n2) — the size of the map and the number of islands you should form.

If the answer doesn't exist, print "NO" (without the quotes) in a single line. Otherwise, print "YES" in the first line. In the next n lines print the description of the map. Each of the lines of the description must consist only of characters 'S' and 'L', where 'S' is a cell that is occupied by the sea and 'L' is the cell covered with sand. The length of each line of the description must equal n. If there are multiple answers, you may print any of them. You should not maximize the sizes of islands.


Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes, and the second — y (y ≥ 1) potatoes. Valera — very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater than n, and, secondly, was divisible by k.Help Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order.

The first line of input contains three integers y, k, n (1 ≤ y, k, n ≤ 109;   ≤ 105).

Print the list of whitespace-separated integers — all possible values of x in ascending order. You should print each possible value of x exactly once. If there are no such values of x print a single integer -1.


You have an image file of size $$$2 \times 2$$$, consisting of $$$4$$$ pixels. Each pixel can have one of $$$26$$$ different colors, denoted by lowercase Latin letters.You want to recolor some of the pixels of the image so that all $$$4$$$ pixels have the same color. In one move, you can choose no more than two pixels of the same color and paint them into some other color (if you choose two pixels, both should be painted into the same color).What is the minimum number of moves you have to make in order to fulfill your goal?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of two lines. Each of these lines contains two lowercase letters of Latin alphabet without any separators, denoting a row of pixels in the image.

For each test case, print one integer — the minimum number of moves you have to make so that all $$$4$$$ pixels of the image have the same color.

5

rb

br

cc

wb

aa

aa

ab

cd

yy

xx

NoteLet's analyze the test cases of the example.In the first test case, you can paint the bottom left pixel and the top right pixel (which share the same color) into the color r, so all pixels have this color.In the second test case, two moves are enough:  paint both top pixels, which have the same color c, into the color b;  paint the bottom left pixel into the color b. In the third test case, all pixels already have the same color.In the fourth test case, you may leave any of the pixels unchanged, and paint all three other pixels into the color of that pixel in three moves.In the fifth test case, you can paint both top pixels into the color x.


There is a graph of $$$n$$$ rows and $$$10^6 + 2$$$ columns, where rows are numbered from $$$1$$$ to $$$n$$$ and columns from $$$0$$$ to $$$10^6 + 1$$$:  Let's denote the node in the row $$$i$$$ and column $$$j$$$ by $$$(i, j)$$$.Initially for each $$$i$$$ the $$$i$$$-th row has exactly one obstacle — at node $$$(i, a_i)$$$. You want to move some obstacles so that you can reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph (you can't pass through obstacles). Moving one obstacle to an adjacent by edge free node costs $$$u$$$ or $$$v$$$ coins, as below:  If there is an obstacle in the node $$$(i, j)$$$, you can use $$$u$$$ coins to move it to $$$(i-1, j)$$$ or $$$(i+1, j)$$$, if such node exists and if there is no obstacle in that node currently.  If there is an obstacle in the node $$$(i, j)$$$, you can use $$$v$$$ coins to move it to $$$(i, j-1)$$$ or $$$(i, j+1)$$$, if such node exists and if there is no obstacle in that node currently.  Note that you can't move obstacles outside the grid. For example, you can't move an obstacle from $$$(1,1)$$$ to $$$(0,1)$$$. Refer to the picture above for a better understanding. Now you need to calculate the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$u$$$ and $$$v$$$ ($$$2 \le n \le 100$$$, $$$1 \le u, v \le 10^9$$$) — the number of rows in the graph and the numbers of coins needed to move vertically and horizontally respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — where $$$a_i$$$ represents that the obstacle in the $$$i$$$-th row is in node $$$(i, a_i)$$$. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^4$$$.

For each test case, output a single integer — the minimal number of coins you need to spend to be able to reach node $$$(n, 10^6+1)$$$ from node $$$(1, 0)$$$ by moving through edges of this graph without passing through obstacles. It can be shown that under the constraints of the problem there is always a way to make such a trip possible.

NoteIn the first sample, two obstacles are at $$$(1, 2)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(2, 2)$$$ to $$$(2, 3)$$$, then to $$$(1, 3)$$$. The total cost is $$$u+v = 7$$$ coins.  In the second sample, two obstacles are at $$$(1, 3)$$$ and $$$(2,2)$$$. You can move the obstacle on $$$(1, 3)$$$ to $$$(2, 3)$$$. The cost is $$$u = 3$$$ coins.


You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).

Print $$$t$$$ integers — the required numbers $$$a+b$$$.

#!/bin/usr/perl

my $t=<>;
chomp($t);
while ($t--){
	my $line = <>;
	chomp($line);
	@numbers = split(" ",$line);
	my $a = $numbers[0];
	my $b = $numbers[1];
	print($a+$b);
	print("\n");
}


Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules so that the event gets more interesting to watch.Each mushroom grower has a mushroom that he will grow on the competition. Under the new rules, the competition consists of two parts. The first part lasts t1 seconds and the second part lasts t2 seconds. The first and the second part are separated by a little break.After the starting whistle the first part of the contest starts, and all mushroom growers start growing mushrooms at once, each at his individual speed of vi meters per second. After t1 seconds, the mushroom growers stop growing mushrooms and go to have a break. During the break, for unexplained reasons, the growth of all mushrooms is reduced by k percent. After the break the second part of the contest starts and all mushrooms growers at the same time continue to grow mushrooms, each at his individual speed of ui meters per second. After a t2 seconds after the end of the break, the competition ends. Note that the speeds before and after the break may vary.Before the match dwarf Pasha learned from all participants, what two speeds they have chosen. However, the participants did not want to disclose to him all their strategy and therefore, did not say in what order they will be using these speeds. That is, if a participant chose speeds ai and bi, then there are two strategies: he either uses speed ai before the break and speed bi after it, or vice versa.Dwarf Pasha really wants to win the totalizer. He knows that each participant chooses the strategy that maximizes the height of the mushroom. Help Dwarf Pasha make the final table of competition results.The participants are sorted in the result table by the mushroom height (the participants with higher mushrooms follow earlier in the table). In case of equal mushroom heights, the participants are sorted by their numbers (the participants with a smaller number follow earlier).

The first input line contains four integer numbers n, t1, t2, k (1 ≤ n, t1, t2 ≤ 1000; 1 ≤ k ≤ 100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following n lines contains two integers. The i-th (1 ≤ i ≤ n) line contains space-separated integers ai, bi (1 ≤ ai, bi ≤ 1000) — the speeds which the participant number i chose.

Print the final results' table: n lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.

Note  First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2·3·0.5 + 4·3 > 4·3·0.5 + 2·3.

#!/usr/bin/perl -w
use strict;
use List::Util qw[min max];

my ($n, $t1, $t2, $k) = split /\D/, <>;
my %len = ();
for my $i (1..$n) {
	my ($a, $b) = split /\D/, <>;
	$len{$i} = max ( ($t1*$a*(100-$k)/100+$t2*$b), ($t1*$b*(100-$k)/100+$t2*$a));
}

foreach my $key (sort lensort keys %len) {
	print "$key ";
	printf "%.2f\n", $len{$key};
}

sub lensort {
	if ($len{$a} == $len{$b}) {
		$a <=> $b;
	} else {
		$len{$b} <=> $len{$a};
	}
}

#!/usr/bin/perl -w
use strict;
use List::Util qw[min max];

my ($n, $t1, $t2, $k) = split /\D/, <>;
my %len = ();
for my $i (1..$n) {
	my ($a, $b) = split /\D/, <>;
	$len{$i} = max ( ($t1*$a*(100-$k)/100+$t2*$b), ($t1*$b*(100-$k)/100+$t2*$a));
}

foreach my $key (reverse sort {$len{$a}<=>$len{$b}} keys %len) {
	print "$key ";
	printf "%.2f\n", $len{$key};
}


Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.Mishka can put a box i into another box j if the following conditions are met:  i-th box is not put into another box;  j-th box doesn't contain any other boxes;  box i is smaller than box j (ai < aj). Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.Help Mishka to determine the minimum possible number of visible boxes!

The first line contains one integer n (1 ≤ n ≤ 5000) — the number of boxes Mishka has got. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the side length of i-th box.

Print the minimum possible number of visible boxes.

NoteIn the first example it is possible to put box 1 into box 2, and 2 into 3.In the second example Mishka can put box 2 into box 3, and box 4 into box 1.


Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings. The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.  Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000.

If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print  - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.

NoteIn the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1.In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.


Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.  There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.

The first line of input contains integer n (1 ≤ n ≤ 105), the number of days. In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.

Print the minimum money needed to keep Duff happy for n days, in one line.

NoteIn the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.

#!/usr/bin/perl

use 5.012;
use warnings;

my $n = <>;
my $cheapest = 101;
my $cost;
while ($n--) {
	chomp(my $line = <>);
	my ($a, $p) = split / /, $line;
	$cheapest = $p if $p < $cheapest;
	$cost += $a * $cheapest;
}
print $cost;

<>;
$min = ~0;
( $min > (split)[1] and $min = (split)[1] ), $sum += $_ * $min for <>;
print $sum

use 5.016;
use strict;
use warnings;

sub argmin {
  my ($aref) = @_;

  my $argmin = 0;
  for my $i (1..$#$aref) {
    if ($aref->[$i] < $aref->[$argmin]) {
      $argmin = $i;
    }
  }
  $argmin;
}

sub sum_range {
  my ($aref, $l, $r) = @_;

  my $sum = 0;
  for my $i ($l..$r) {
    $sum += $aref->[$i];
  }
  $sum;
}

my @eat;
my @price;

my $n = <>;
for (1..$n) {
  my ($eat, $price) = split ' ', <>;
  push @eat, $eat;
  push @price, $price;
}

my $cost = 0;

while (@eat) {
  my $i = argmin(\@price);
  $cost += $price[$i] * sum_range(\@eat, $i, $#eat);
  splice @price, $i;
  splice @eat, $i;
}

say $cost;

$,=" ";
my $n=<>;
chomp($n);
$n--;
my ($x,$y)=split / /,<>;
chomp($x,$y);
my $amount=$y;
my $cost=$x*$amount;
while($n--)
{
  ($x,$y)=split / /,<>;
  chomp($x,$y);
  if($y<$amount)
  {
    $amount=$y;
  }
  $cost+=$x*$amount;
}
print $cost , "\n";

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

<>;
my $m = 111;
my $ans;
for(<>)
{
    my ($a, $p) = split(' ', $_);
    $m = ($p < $m) ? $p : $m;

    $ans += $a * $m;
}

print $ans;


There is a game called "I Wanna Be the Guy", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?

The first line contains a single integer n (1 ≤  n ≤ 100).  The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a1, a2, ..., ap (1 ≤ ai ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n.

If they can pass all the levels, print "I become the guy.". If it's impossible, print "Oh, my keyboard!" (without the quotes).

NoteIn the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.In the second sample, no one can pass level 4.

my $n = <>;
($na, @a) = split " ", <>;
($nb, @b) = (split " ", <>);
push @b, @a;

%k = ();
grep { $k{$_} = $_ } @b;

print scalar keys %k == $n? "I become the guy.\n" : "Oh, my keyboard!\n";

my $n = <>;
my %hash;
my @a = split " ", <>;
my @b = split " ", <>;
shift @a;
shift @b;
$hash{$_} = 0 foreach(1..$n);
$hash{$_} = 1 foreach(@a);
$hash{$_} = 1 foreach(@b);
my $ans = "I become the guy.\n";
foreach(values %hash) {
    $ans = "Oh, my keyboard!\n" if ($_ == 0);
}
print $ans;

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

chomp($n = <>);
my %tb;
@a = split / /, <>;
$tb{int($_)}=1 foreach(@a[1..$#a]);
@a = split / /, <>;
$tb{int($_)}=1 foreach(@a[1..$#a]);
scalar(keys %tb)==$n and say "I become the guy." or say "Oh, my keyboard!";

chomp (my $n = <>);

chomp (my @ps = split / /, <>);
chomp (my @qs = split / /, <>);

shift @ps;
shift @qs;

my %ls;

$ls{$_}++ foreach (@ps, @qs);

if ((scalar keys %ls) == $n) {
  printf "I become the guy.\n";
} else {
  printf "Oh, my keyboard!\n";
}

#!/usr/bin/perl
use strict;
use warnings;
#pass array by reference

my $n = <>;
my @arr = (0) x $n;
my @arr1 = split(' ',<>); shift(@arr1);
my @arr2 = split(' ',<>); shift(@arr2);

foreach(@arr1){
    $arr[$_-1] = 1;
}
foreach(@arr2){
    $arr[$_-1] = 1;
}
my $yes = 1;
foreach(@arr){
    if($_ == 0){
        $yes=0;
        last;
    }
}
if($yes == 1){
    print "I become the guy.\n";
}
else{
    print "Oh, my keyboard!\n";
}

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

my $n = <STDIN>;

my @first = split(' ', <STDIN>);
my @second = split(' ', <STDIN>);

my %set;

shift @first;
shift @second;
foreach my $level ((@first, @second))
{
    unless ($level == 0)
    {
        $set{$level} = 1;
    }
}
my $size = keys %set;
if ($size == $n) {
    print "I become the guy.";
}
else {
    print "Oh, my keyboard!";
}

my $n = <>;
my %hash;
my @a = split " ", <>;
push @a, split " ", <>;
$hash{$_} = 0 foreach(1..$n);
$hash{$_} = 1 foreach(@a);
my $ans = "I become the guy.\n";
foreach(values %hash) {
    $ans = "Oh, my keyboard!\n" if ($_ == 0);
}
print $ans;

my $n = <>;

my @ps = split / /, <>;
my @qs = split / /, <>;

shift @ps;
shift @qs;

my %ls;

$ls{$_}++ foreach (@ps, @qs);

if (scalar keys %ls == $n) {
  printf "I become the guy.\n";
} else {
  printf "Oh, my keyboard!\n";
}

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

my $n = <>;
my @arr = (0) x $n;
my @arr1 = split(' ',readline);
my @arr2 = split(' ',readline);

foreach(@arr1){
    $arr[$_-1] = 1;
}
foreach(@arr2){
    $arr[$_-1] = 1;
}
my $yes = 1;
foreach(@arr){
    if($_ == 0){
        $yes=0;
        last;
    }
}
if($yes == 1){
    print "I become the guy.\n";
}
else{
    print "Oh, my keyboard!\n";
}

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

my $n = <STDIN>;

my @first = split(' ', <STDIN>);
my @second = split(' ', <STDIN>);

my %set;

foreach my $level ((@first, @second))
{
    unless ($level != 0)
    {
        $set{$level} = 1;
    }
}
my $size = keys %set;
if ($size == $n) {
    print "I become the guy.";
}
else {
    print "Oh, my keyboard!";
}

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

my $n = <STDIN>;

my @first = split(' ', <STDIN>);
my @second = split(' ', <STDIN>);

my %set;

foreach my $level ((@first, @second))
{
    $set{$level} = 1;
}
my $size = keys %set;
if ($size == $n) {
    print "I become the guy.";
}
else {
    print "Oh, my keyboard!";
}

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

my $n = <STDIN>;

my @first = split(' ', <STDIN>);
my @second = split(' ', <STDIN>);

my %set;

foreach my $level ((@first, @second))
{
    unless ($level == 0)
    {
        $set{$level} = 1;
    }
}
my $size = keys %set;
if ($size == $n) {
    print "I become the guy.";
}
else {
    print "Oh, my keyboard!";
}


PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 edges, where k is some integer. Note that one vertex is a valid tree.There is exactly one relative living in each vertex of each tree, they have unique ids from 1 to n. For each Ball i we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those.How many trees are there in the forest?

The first line contains single integer n (1 ≤ n ≤ 104) — the number of Balls living in the forest. The second line contains a sequence p1, p2, ..., pn of length n, where (1 ≤ pi ≤ n) holds and pi denotes the most distant from Ball i relative living on the same tree. If there are several most distant relatives living on the same tree, pi is the id of one with the smallest id. It's guaranteed that the sequence p corresponds to some valid forest. Hacking: To hack someone, you should provide a correct forest as a test. The sequence p will be calculated according to the forest and given to the solution you try to hack as input. Use the following format: In the first line, output the integer n (1 ≤ n ≤ 104) — the number of Balls and the integer m (0 ≤ m < n) — the total number of edges in the forest. Then m lines should follow. The i-th of them should contain two integers ai and bi and represent an edge between vertices in which relatives ai and bi live. For example, the first sample is written as follows: 5 31 23 44 5

You should output the number of trees in the forest where PolandBall lives.

NoteIn the first sample testcase, possible forest is: 1-2 3-4-5. There are 2 trees overall.In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree.


It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are n tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all the n tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks.

The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The larger number hi is, the more difficult the i-th task is.

In the first line print "YES" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line "NO" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form. If there are multiple possible answers, you can print any of them.

NoteIn the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.In the second sample there are only two sequences of tasks that meet the conditions — [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks.


Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.

The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.

If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).

NoteIn the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.

use strict;
use warnings;

use Data::Dumper;
$,=" ";
my $N=<>;
chomp($N);

my @t=split / /,<>;
chomp(@t);
my %d=map {$_ => 1} @t;
my $count=scalar(keys %d);
if($count>3)
{
  print "NO\n";
  exit(0);
}
if($count<3)
{
  print "YES\n";
  exit(0);
}
@t=sort {$a <=> $b} keys %d;
if($t[2]-$t[1] == $t[1]-$t[0])
{
  print "YES\n";
  exit(0);
}
print "NO\n";


As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has n servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form "command ip;" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.  Each ip is of form "a.b.c.d" where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is "command ip;" Dustin has to replace it with "command ip; #name" where name is the name of the server with ip equal to ip.Dustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him.

The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000). The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct. The next m lines contain the commands in the configuration file. Each line is of form "command ip;" (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.

Print m lines, the commands in the configuration file after Dustin did his task.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my %h;

	map { my( $name, $ip ) = ( split ' ', <> ); $h{ $ip }{ name } = $name } 1 .. $n;

	for( 1 .. $m ){
		my( $comm, $ip ) = split ' ', <>;
		chop $ip;

		print "$comm $ip; #$h{ $ip }{ name }"
		}
	}


The $$$\text{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$\text{$$$gcdSum$$$}(x) = gcd(x, \text{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ — the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$.For example: $$$\text{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$.Given an integer $$$n$$$, find the smallest integer $$$x \ge n$$$ such that $$$\text{$$$gcdSum$$$}(x) > 1$$$.

The first line of input contains one integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases.  Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 \le n \le 10^{18})$$$. All test cases in one test are different.

Output $$$t$$$ lines, where the $$$i$$$-th line is a single integer containing the answer to the $$$i$$$-th test case.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	my $found = 0;

	while( not $found ){
		my $sum = eval join '+', split //;

		for my $i ( 2 .. $sum ){
			next if not $sum % $i == 0;

			if( $_ % $i == 0 ){
				$found = $_;
				last;
				}
			}

		$_ ++;
		}

	print $found;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	my $found = 0;

	while( not $found ){
		my $sum = eval join '+', split //;

		for my $i ( 2 .. 1 + sqrt $sum, $sum ){
			next if not $sum % $i == 0;

			if( $_ % $i == 0 ){
				$found = $_;
				last;
				}
			}

		$_ ++;
		}

	print $found;
	}


You are given an angle $$$\text{ang}$$$. The Jury asks You to find such regular $$$n$$$-gon (regular polygon with $$$n$$$ vertices) that it has three vertices $$$a$$$, $$$b$$$ and $$$c$$$ (they can be non-consecutive) with $$$\angle{abc} = \text{ang}$$$ or report that there is no such $$$n$$$-gon.  If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed $$$998244353$$$.

The first line contains single integer $$$T$$$ ($$$1 \le T \le 180$$$) — the number of queries.  Each of the next $$$T$$$ lines contains one integer $$$\text{ang}$$$ ($$$1 \le \text{ang} < 180$$$) — the angle measured in degrees.

For each query print single integer $$$n$$$ ($$$3 \le n \le 998244353$$$) — minimal possible number of vertices in the regular $$$n$$$-gon or $$$-1$$$ if there is no such $$$n$$$.

NoteThe answer for the first query is on the picture above.The answer for the second query is reached on a regular $$$18$$$-gon. For example, $$$\angle{v_2 v_1 v_6} = 50^{\circ}$$$.The example angle for the third query is $$$\angle{v_{11} v_{10} v_{12}} = 2^{\circ}$$$.In the fourth query, minimal possible $$$n$$$ is $$$180$$$ (not $$$90$$$).

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

my %h = ( 179 => 360 );

for my $n ( 3 .. 180 ){
    next if 180 % $n;
    for my $i ( 1 .. $n - 2 ){
        $h{ 180 / $n * $i } //= $n;
        }
    }

$debug and print " $_ => $h{ $_ }" for sort { $a <=> $b } keys %h;

<>;

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

    chomp;
	$debug and print;

    print $h{ $_ };
    }

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

my %h;

for my $n ( 3 .. 180 ){
    next if 180 % $n;
    for my $i ( 1 .. $n - 2 ){
        $h{ 180 / $n * $i } //= $n;
        }
    }

$debug and print " $_ => $h{ $_ }" for sort { $a <=> $b } keys %h;

<>;

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

    chomp;
	$debug and print;

    print $h{ $_ };
    }


Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

The first line contains integer n (1 ≤ n ≤ 50) — the number of strings. This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

NoteIn the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".


You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes.

The first line contains two integers n and k (1 ≤ k ≤ n ≤  105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).

Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.

NoteA subsegment [l,  r] (l ≤ r) of array a is the sequence al,  al + 1,  ...,  ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4,  - 5,  - 3,  - 2,  - 1). The only minimum is min( - 4,  - 5,  - 3,  - 2,  - 1) =  - 5. The resulting maximum is  - 5.


You are given three strings $$$s$$$, $$$t$$$ and $$$p$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose any character from $$$p$$$, erase it from $$$p$$$ and insert it into string $$$s$$$ (you may insert this character anywhere you want: in the beginning of $$$s$$$, in the end or between any two consecutive characters). For example, if $$$p$$$ is aba, and $$$s$$$ is de, then the following outcomes are possible (the character we erase from $$$p$$$ and insert into $$$s$$$ is highlighted):  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ ade;  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ dae;  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ dea;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ bde;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ dbe;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ deb;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ ade;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ dae;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ dea; Your goal is to perform several (maybe zero) operations so that $$$s$$$ becomes equal to $$$t$$$. Please determine whether it is possible.Note that you have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is represented by three consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \le |t| \le 100$$$) consisting of lowercase Latin letters. The third line of each query contains the string $$$p$$$ ($$$1 \le |p| \le 100$$$) consisting of lowercase Latin letters.

For each query print YES if it is possible to make $$$s$$$ equal to $$$t$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa

NoteIn the first test case there is the following sequence of operation:   $$$s = $$$ ab, $$$t = $$$ acxb, $$$p = $$$ cax;  $$$s = $$$ acb, $$$t = $$$ acxb, $$$p = $$$ ax;  $$$s = $$$ acxb, $$$t = $$$ acxb, $$$p = $$$ a. In the second test case there is the following sequence of operation:   $$$s = $$$ a, $$$t = $$$ aaaa, $$$p = $$$ aaabbcc;  $$$s = $$$ aa, $$$t = $$$ aaaa, $$$p = $$$ aabbcc;  $$$s = $$$ aaa, $$$t = $$$ aaaa, $$$p = $$$ abbcc;  $$$s = $$$ aaaa, $$$t = $$$ aaaa, $$$p = $$$ bbcc.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	my $s = $_;
	my $t = <>;
	my $p = <>;
	chomp( $s, $t, $p );
	$debug and print "[$s|$t|$p]";

	my $f = 0;
	my $left = '';

	while( $s =~ /./g ){
		my $del = $&;

		$t =~ s/^(.*?)$del// ? do { $left .= $1 } : do { $f = 1 };
		}

	$left .= $t;

	$debug and print $left;

	while( $p =~ /./g ){
		my $del = $&;

		$left =~ s/$del//;
		}

	$debug and print $left;

	length $left and $f = 1;

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


Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.

In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake. Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.

Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.

NoteIf we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:   (1, 2) and (1, 3)  (3, 1) and (3, 3)  Pieces that share the same column are:   (2, 1) and (3, 1)  (1, 3) and (3, 3)


There are $$$n$$$ districts in the town, the $$$i$$$-th district belongs to the $$$a_i$$$-th bandit gang. Initially, no districts are connected to each other.You are the mayor of the city and want to build $$$n-1$$$ two-way roads to connect all districts (two districts can be connected directly or through other connected districts).If two districts belonging to the same gang are connected directly with a road, this gang will revolt.You don't want this so your task is to build $$$n-1$$$ two-way roads in such a way that all districts are reachable from each other (possibly, using intermediate districts) and each pair of directly connected districts belong to different gangs, or determine that it is impossible to build $$$n-1$$$ roads to satisfy all the conditions.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — 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 5000$$$) — the number of districts. 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 gang the $$$i$$$-th district belongs to. It is guaranteed that the sum of $$$n$$$ does not exceed $$$5000$$$ ($$$\sum n \le 5000$$$).

For each test case, print:   NO on the only line if it is impossible to connect all districts satisfying the conditions from the problem statement.  YES on the first line and $$$n-1$$$ roads on the next $$$n-1$$$ lines. Each road should be presented as a pair of integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n; x_i \ne y_i$$$), where $$$x_i$$$ and $$$y_i$$$ are two districts the $$$i$$$-th road connects.  For each road $$$i$$$, the condition $$$a[x_i] \ne a[y_i]$$$ should be satisfied. Also, all districts should be reachable from each other (possibly, using intermediate districts).

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	@_ = split ' ', <>;

	my $first = $_[ 0 ];

	my @eq;
	my @neq;

	for my $i ( 1 .. @_ - 1 ){
		if( $_[ $i ] == $first ){
			push @eq, $i + 1;
			}
		else{
			push @neq, $i + 1;
			}
		}

	$debug and print "[eq:@eq][neq:@neq]";

	if( @neq ){
		print "YES";
		}
	else{
		print "NO";
		next;
		}

	for( @neq ){
		print "1 $_";
		}

	for( @eq ){
		print "$neq[ 0 ] $_"
		}

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	@_ = split ' ', <>;

	my $first = $_[ 0 ];

	my @eq;
	my @neq;

	for my $i ( 1 .. @_ - 1 ){
		if( $_[ $i ] == $first ){
			push @eq, $i + 1;
			}
		else{
			push @neq, $i + 1;
			}
		}

	$debug and print "[eq:@eq][neq:@neq]";

	if( @neq ){
		print "YES";
		}
	else{
		print "NO";
		next;
		}

	for( @neq ){
		print "$first $_";
		}

	for( @eq ){
		print "$neq[ 0 ] $_"
		}

	}


You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones and an integer $$$k$$$. In one operation you can do one of the following:  Select $$$2$$$ consecutive elements of $$$a$$$ and replace them with their minimum (that is, let $$$a := [a_{1}, a_{2}, \ldots, a_{i-1}, \min(a_{i}, a_{i+1}), a_{i+2}, \ldots, a_{n}]$$$ for some $$$1 \le i \le n-1$$$). This operation decreases the size of $$$a$$$ by $$$1$$$.  Select $$$k$$$ consecutive elements of $$$a$$$ and replace them with their maximum (that is, let $$$a := [a_{1}, a_{2}, \ldots, a_{i-1}, \max(a_{i}, a_{i+1}, \ldots, a_{i+k-1}), a_{i+k}, \ldots, a_{n}]$$$ for some $$$1 \le i \le n-k+1$$$). This operation decreases the size of $$$a$$$ by $$$k-1$$$. Determine if it's possible to turn $$$a$$$ into $$$[1]$$$ after several (possibly zero) operations.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 50$$$), the size of array $$$a$$$ and the length of segments that you can perform second type operation on. The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$.

For each test case, if it is possible to turn $$$a$$$ into $$$[1]$$$, print "YES", otherwise print "NO".

NoteIn the first test case, you can perform the second type operation on second and third elements so $$$a$$$ becomes $$$[0, 1]$$$, then you can perform the second type operation on first and second elements, so $$$a$$$ turns to $$$[1]$$$.In the fourth test case, it's obvious to see that you can't make any $$$1$$$, no matter what you do.In the fifth test case, you can first perform a type 2 operation on the first three elements so that $$$a$$$ becomes $$$[1, 0, 0, 1]$$$, then perform a type 2 operation on the elements in positions two through four, so that $$$a$$$ becomes $$$[1, 1]$$$, and finally perform the first type operation on the remaining elements, so that $$$a$$$ becomes $$$[1]$$$.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my $A = grep m/1/, @_;

	print $A ? 'YES' : 'NO';
	}

#!/usr/bin/perl
use Data::Dumper;
use integer;   ### important!
my $mod = 10 ** 9 + 7;
# my $mod = 998244353;

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

while( $testcase -- > 0 ){

  my ($n,$k) = map { $_ - 0 } split(/\s+/,<STDIN>);
  my @A = map { $_ - 0 } split(/\s+/,<STDIN>);
  my $c1 = 0; my $c0 = 0;
  for(my $i=0;$i<$n;$i++){
    $c1++ if( $A[$i] == 1 );
    $c0++ if( $A[$i] == 0 );
  }
  if( $c0 == 0 ){
    print "YES\n"; next;
  }
  if( $c1 == 0 ){
    print "NO\n"; next;
  }
  if( $k > $n ){
    print "NO\n"; next;
  }
  print "YES\n";

}

exit(0);

sub max {
  my $r = undef;
  foreach my $e (@_){  $r = $e if !defined($r) or $e > $r;  }
  return $r;
}
sub min {
  my $r = undef;
  foreach my $e (@_){  $r = $e if !defined($r) or $e < $r;  }
  return $r;
}
sub sum {
  my $r = 0;
  foreach my $e (@_){ $r += $e; }
  return $r;
}
sub ruisum {   # my @rui = (0); &ruisum(\@rui,\@array);
  my $r = shift; my $ar = shift;
  $#{$r} = 1+$#{$ar}; $r->[0] = 0; my $rr = 0;
  for(my $i=0;$i<=$#ar;$i++){
    $rr += $ar->[$i];
    $r->[1+$i] = $rr;
  }
}


As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore. A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-dimensional objects through three-dimensional objects by using his magnificent sculptures. And his new project is connected with this. Cicasso wants to make a coat for the haters of anticubism. To do this, he wants to create a sculpture depicting a well-known geometric primitive — convex polygon.Cicasso prepared for this a few blanks, which are rods with integer lengths, and now he wants to bring them together. The i-th rod is a segment of length li.The sculptor plans to make a convex polygon with a nonzero area, using all rods he has as its sides. Each rod should be used as a side to its full length. It is forbidden to cut, break or bend rods. However, two sides may form a straight angle .Cicasso knows that it is impossible to make a convex polygon with a nonzero area out of the rods with the lengths which he had chosen. Cicasso does not want to leave the unused rods, so the sculptor decides to make another rod-blank with an integer length so that his problem is solvable. Of course, he wants to make it as short as possible, because the materials are expensive, and it is improper deed to spend money for nothing. Help sculptor!

The first line contains an integer n (3 ≤ n ≤ 105) — a number of rod-blanks. The second line contains n integers li (1 ≤ li ≤ 109) — lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.

Print the only integer z — the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods.

NoteIn the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}. In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}.

sub solve {
	my $n = shift;
	my @a = split(" ", shift);
	my $s = 0;
	for my $x (@a) {
		$s += $x;
	}
	my $res = 0;
	for my $x (@a) {
		my $tmp = 2 * $x - $s;
		if ($res < $tmp) {
			$res = $tmp;
		}
	}
	return $res + 1;
};

my $n = <>;
my $input = <>;
print solve($n, $input);


Monocarp is playing a computer game. In this game, his character fights different monsters.A fight between a character and a monster goes as follows. Suppose the character initially has health $$$h_C$$$ and attack $$$d_C$$$; the monster initially has health $$$h_M$$$ and attack $$$d_M$$$. The fight consists of several steps:  the character attacks the monster, decreasing the monster's health by $$$d_C$$$;  the monster attacks the character, decreasing the character's health by $$$d_M$$$;  the character attacks the monster, decreasing the monster's health by $$$d_C$$$;  the monster attacks the character, decreasing the character's health by $$$d_M$$$;  and so on, until the end of the fight. The fight ends when someone's health becomes non-positive (i. e. $$$0$$$ or less). If the monster's health becomes non-positive, the character wins, otherwise the monster wins.Monocarp's character currently has health equal to $$$h_C$$$ and attack equal to $$$d_C$$$. He wants to slay a monster with health equal to $$$h_M$$$ and attack equal to $$$d_M$$$. Before the fight, Monocarp can spend up to $$$k$$$ coins to upgrade his character's weapon and/or armor; each upgrade costs exactly one coin, each weapon upgrade increases the character's attack by $$$w$$$, and each armor upgrade increases the character's health by $$$a$$$.Can Monocarp's character slay the monster if Monocarp spends coins on upgrades optimally?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases. Each test case consists of three lines: The first line contains two integers $$$h_C$$$ and $$$d_C$$$ ($$$1 \le h_C \le 10^{15}$$$; $$$1 \le d_C \le 10^9$$$) — the character's health and attack; The second line contains two integers $$$h_M$$$ and $$$d_M$$$ ($$$1 \le h_M \le 10^{15}$$$; $$$1 \le d_M \le 10^9$$$) — the monster's health and attack; The third line contains three integers $$$k$$$, $$$w$$$ and $$$a$$$ ($$$0 \le k \le 2 \cdot 10^5$$$; $$$0 \le w \le 10^4$$$; $$$0 \le a \le 10^{10}$$$) — the maximum number of coins that Monocarp can spend, the amount added to the character's attack with each weapon upgrade, and the amount added to the character's health with each armor upgrade, respectively. The sum of $$$k$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print YES if it is possible to slay the monster by optimally choosing the upgrades. Otherwise, print NO.

NoteIn the first example, Monocarp can spend one coin to upgrade weapon (damage will be equal to $$$5$$$), then health during battle will change as follows: $$$(h_C, h_M) = (25, 9) \rightarrow (25, 4) \rightarrow (5, 4) \rightarrow (5, -1)$$$. The battle ended with Monocarp's victory.In the second example, Monocarp has no way to defeat the monster.In the third example, Monocarp has no coins, so he can't buy upgrades. However, the initial characteristics are enough for Monocarp to win.In the fourth example, Monocarp has $$$4$$$ coins. To defeat the monster, he has to spend $$$2$$$ coins to upgrade weapon and $$$2$$$ coins to upgrade armor.


Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:  some dwarf in one of the chosen lines is located in the rightmost cell of his row;  some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells.Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.

The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).  Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".

In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.

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

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

while(<>){
	($n,$m)=split/ /;
	@distance=(); # empty
	@end=();
	$f=1;
	for $i(1..$n){

		$_=<>;
		/G\**S/ and (($distance[length($&)]++), next);
		$f=0;
		}
	$j=0;
	$_ and $j++ for @distance;
	printf "%d\n", $f? ( $j ) : -1;

	}

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

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

while(<>){
	($n,$m)=split/ /;
	@distance=(); # empty
	@end=();
	$f=1;
	for $i(1..$n){

		$_=<>;
		/G\**S\*/ and ((push @distance, length($&)-2), next);
		/G\**S$/ and ((push @end, length($&)-1), next);
		$f=0;
		}
	printf "%d\n", $f? (0+@end? min(@end) : max(@distance) ) : -1;

	}

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

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

while(<>){
	($n,$m)=split/ /;
	@distance=(); # empty
	@end=();
	$f=1;
	for $i(1..$n){

		chomp($_=<>);
		/G\**S/ ? (push @distance, $d=length($&)-1) : ($f=0);
		/S$/ and push @end, $d;
		}
	printf "%d\n", $f? min(max(@distance),min(@end)): -1;

	}

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

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

while(<>){
	($n,$m)=split/ /;
	@distance=(); # empty
	$f=1;
	for $i(1..$n){

		$_=<>;
		/G\**S/ ? (push @distance, length($&)-1) : ($f=0);

		}
	printf "%d\n", $f? max(@distance)-min(@distance)+1: -1;

	}


Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection. Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively. The next line contains n space-separated integers s1, s2, ..., sn (1 ≤ s1 ≤ s2 ≤ ... ≤ sn ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.

Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.

NoteIn the first sample, Kevin must pack his two cowbells into the same box. In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.In the third sample, the optimal solution is {3, 5} and {7}.


In this problem you will write a simple generator of Brainfuck (https://en.wikipedia.org/wiki/Brainfuck) calculators.You are given an arithmetic expression consisting of integers from 0 to 255 and addition/subtraction signs between them. Output a Brainfuck program which, when executed, will print the result of evaluating this expression.We use a fairly standard Brainfuck interpreter for checking the programs: 30000 memory cells. memory cells store integers from 0 to 255 with unsigned 8-bit wraparound. console input (, command) is not supported, but it's not needed for this problem.

The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive. The calculations result is guaranteed to be an integer between 0 and 255, inclusive (results of intermediary calculations might be outside of these boundaries).

Output a Brainfuck program which, when executed, will print the result of evaluating this expression. The program must be at most 5000000 characters long (including the non-command characters), and its execution must be complete in at most 50000000 steps.

NoteYou can download the source code of the Brainfuck interpreter by the link http://assets.codeforces.com/rounds/784/bf.cpp. We use this code to interpret outputs.

#!/usr/bin/perl

use strict;
use warnings;

while (<>) {
  chomp;
  my $n = eval;
  my $res = '';
  my @ds = split //, $n;
  for my $d (@ds) {
    $res .= '+' x ($d + 48) . '.>';
  }
  print "$res\n";
}

#!/usr/bin/perl

use strict;
use warnings;

while (<>) {
  chomp;
  my $n = eval;
  my @res = ();
  if ($n >= 0) {
    @res = ('+' x $n, '.');
  } else {
    @res = ('-' x -$n, '.');
  }
  print @res, "\n";
}

#!/usr/bin/perl

use strict;
use warnings;

while (<>) {
  chomp;
  my $n = eval;
  my @res = ('+' x $n, '.');
  print @res, "\n";
}


Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least ai any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.

The first line contains number n (1 ≤ n ≤ 1000). The second line contains n non-negative integers a1, a2, ..., an (0 ≤ ai < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.

Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.

NoteIn the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.


You have an $$$n \times n$$$ chessboard and $$$k$$$ rooks. Rows of this chessboard are numbered by integers from $$$1$$$ to $$$n$$$ from top to bottom and columns of this chessboard are numbered by integers from $$$1$$$ to $$$n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and collumn $$$y$$$ for $$$1 \leq x \leq n$$$ and $$$1 \leq y \leq n$$$.The arrangement of rooks on this board is called good, if no rook is beaten by another rook.A rook beats all the rooks that shares the same row or collumn with it.The good arrangement of rooks on this board is called not stable, if it is possible to move one rook to the adjacent cell so arrangement becomes not good. Otherwise, the good arrangement is stable. Here, adjacent cells are the cells that share a side.  Such arrangement of $$$3$$$ rooks on the $$$4 \times 4$$$ chessboard is good, but it is not stable: the rook from $$$(1, 1)$$$ can be moved to the adjacent cell $$$(2, 1)$$$ and rooks on cells $$$(2, 1)$$$ and $$$(2, 4)$$$ will beat each other. Please, find any stable arrangement of $$$k$$$ rooks on the $$$n \times n$$$ chessboard or report that there is no such arrangement.

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 two integers $$$n$$$, $$$k$$$ ($$$1 \leq k \leq n \leq 40$$$) — the size of the chessboard and the number of rooks.

If there is a stable arrangement of $$$k$$$ rooks on the $$$n \times n$$$ chessboard, output $$$n$$$ lines of symbols . and R. The $$$j$$$-th symbol of the $$$i$$$-th line should be equals R if and only if there is a rook on the cell $$$(i, j)$$$ in your arrangement. If there are multiple solutions, you may output any of them. If there is no stable arrangement, output $$$-1$$$.

NoteIn the first test case, you should find stable arrangement of $$$2$$$ rooks on the $$$3 \times 3$$$ chessboard. Placing them in cells $$$(3, 1)$$$ and $$$(1, 3)$$$ gives stable arrangement.In the second test case it can be shown that it is impossbile to place $$$3$$$ rooks on the $$$3 \times 3$$$ chessboard to get stable arrangement.


Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.In the bookshop, Jack decides to buy two books of different genres.Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.The books are given by indices of their genres. The genres are numbered from 1 to m.

The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre.

Print the only integer — the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2·109.

NoteThe answer to the first test sample equals 5 as Sasha can choose:  the first and second books,  the first and third books,  the first and fourth books,  the second and third books,  the third and fourth books.


A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color ci.The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children.The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.

The first line contains two integers n and m — the number of the children and the number of possible mitten colors (1 ≤ n ≤ 5000, 1 ≤ m ≤ 100). The second line contains n integers c1, c2, ... cn, where ci is the color of the mittens of the i-th child (1 ≤ ci ≤ m).

In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them.

use List::Util qw(max min);

my ($n, $m) = split ' ', scalar <>;
my @c = sort {$a cmp $b} split ' ', scalar <>;

$n == @c or die "invalid count";

my %cnt = ();

foreach (@c){
    $cnt{$_}++;
    }

my $mcnt = max(values %cnt);

my @d = @c[( $mcnt..$#c , 0..($mcnt-1) )];

my $res = min($n, 2*($n - $mcnt));

print "$res\n";
for(my $i = 0; $i < $n; $i++){
    print "$c[$i] $d[$i]\n"
    }

1;

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

while (<>){
	@a=@m=@smm=();
	$a=$c="";
	$n=0;
	@_=split/ /,<>;
	for (@_){$m[$_]++}
	@sm = sort num_rev @m;
	for $i(@sm){
		$i>0 and push @smm, $i;
		}

	for $i(@smm){
		for $j(1..@m-1){
			if ($m[$j]==$i){
				$a.="$j "x$m[$j];
				$m[$j]=0;
				}
			}
		}
	chop $a;

	$aa=@a=split/ /,$a;
	push @a, @a;

	for $i(0..$aa-1){
		$c.=$a[$i]." ".$a[$i+$smm[0]]."\n";
		if ($a[$i]==$a[$i+$smm[0]]){$n++}
		}
	print @_-$n,"\n";
	print "$c\n";
	}

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

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

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

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

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

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

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

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

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

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

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

while (<>){
	@a=@m=@sm=@smm=();
	$a=$c="";
	$n=0;
	@_=split/ /,<>;
	for (@_){$m[$_]++}
	@sm = sort num_rev @m;
	for $i(@sm){
		$i>0 and push @smm, $i;
		}
#		print "@m\n";
#		print "@sm\n";
#		print "@smm\n";
	for $i(@smm){
		#print " ...\n";
		for $j(1..@m-1){
			#print " $m[$j]==$i\n";
			if ($m[$j]==$i){

				$a.="$j "x$m[$j];
				$m[$j]=0;

				}

			}

		}
	# print $a; n();

	chop $a;

	$aa=@a=split/ /,$a;
	push @a, @a;

	for $i(0..$aa-1){
		$c.=$a[$i]." ".$a[$i+$smm[0]]."\n";
		if ($a[$i]==$a[$i+$smm[0]]){$n++}
		}
	print @_-$n,"\n";
	print "$c\n";
	}

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

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

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

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

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

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

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

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

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

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


Recently Vasya got interested in finding extra-terrestrial intelligence. He made a simple extra-terrestrial signals’ receiver and was keeping a record of the signals for n days in a row. Each of those n days Vasya wrote a 1 in his notebook if he had received a signal that day and a 0 if he hadn’t. Vasya thinks that he has found extra-terrestrial intelligence if there is a system in the way the signals has been received, i.e. if all the intervals between successive signals are equal. Otherwise, Vasya thinks that the signals were sent by some stupid aliens no one cares about. Help Vasya to deduce from the information given by the receiver if he has found extra-terrestrial intelligence or not.

The first line contains integer n (3 ≤ n ≤ 100) — amount of days during which Vasya checked if there were any signals. The second line contains n characters 1 or 0 — the record Vasya kept each of those n days. It’s guaranteed that the given record sequence contains at least three 1s.

If Vasya has found extra-terrestrial intelligence, output YES, otherwise output NO.


Luca has a cypher made up of a sequence of $$$n$$$ wheels, each with a digit $$$a_i$$$ written on it. On the $$$i$$$-th wheel, he made $$$b_i$$$ moves. Each move is one of two types:   up move (denoted by $$$\texttt{U}$$$): it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$.  down move (denoted by $$$\texttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$.     Example for $$$n=4$$$. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.

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 a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the number of wheels. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 \leq a_i \leq 9$$$) — the digit shown on the $$$i$$$-th wheel after all moves have been performed. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains the integer $$$b_i$$$ ($$$1 \leq b_i \leq 10$$$) and $$$b_i$$$ characters that are either $$$\texttt{U}$$$ or $$$\texttt{D}$$$ — the number of moves performed on the $$$i$$$-th wheel, and the moves performed. $$$\texttt{U}$$$ and $$$\texttt{D}$$$ represent an up move and a down move respectively.

For each test case, output $$$n$$$ space-separated digits  — the initial sequence of the cypher.

3

3

9 3 1

3 DDD

4 UDUU

2 DU

2

0 9

9 DDDDDDDDD

9 UUUUUUUUU

5

0 5 9 8 3

10 UUUUUUUUUU

3 UUD

8 UUDUUDDD

10 UUDUUDUDDU

4 UUUU

NoteIn the first test case, we can prove that initial sequence was $$$[2,1,1]$$$. In that case, the following moves were performed:   On the first wheel: $$$2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{D}]{} 9$$$.  On the second wheel: $$$1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{U}]{} 3$$$.  On the third wheel: $$$1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{U}]{} 1$$$.  The final sequence was $$$[9,3,1]$$$, which matches the input.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my @moves =
		map { ( $_ cmp 'E' ) * -length }
		map { 1 while s/DU//g; $_ }
		map { join '', sort split // }
		map { ( split )[ 1 ] } map ~~<>, 1 .. @_;

	for( @_ ){
		my $move = shift @moves;
		$_ += $move;
		$_ %= 10;
		}

	print "@_";
	}


One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said "lala." at the end of her sentences, while Rainbow always said "miao." at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is.

The first line of the input contains an integer n (1 ≤ n ≤ 10), number of sentences in the chat record. Each of the next n lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesn’t exceed 100.

For each sentence, output "Freda's" if the sentence was said by Freda, "Rainbow's" if the sentence was said by Rainbow, or "OMG>.< I don't know!" if liouzhou_101 can’t recognize whose sentence it is. He can’t recognize a sentence if it begins with "miao." and ends with "lala.", or satisfies neither of the conditions.

5
I will go to play with you lala.
wow, welcome.
miao.lala.
miao.
miao .

Freda's
OMG>.< I don't know!
OMG>.< I don't know!
Rainbow's
OMG>.< I don't know!

<>;
while (<>) {
    $f = $_ =~ /(lala\.)$/;
    $r = $_ =~ /(^miao\.)/;
    if ($f == $r) {
        print "OMG>.< I don't know!\n";
    } elsif ($f) { print "Freda's\n"; } elsif ($r) { print "Rainbow's\n"; }
}

chomp($n=<STDIN>);
while($n > 0) {
	chomp($line=<STDIN>);
	if($line =~ /^miao\..*lala\.$/) {
		print "OMG>.< I don't know!\n";
	} elsif ($line =~ /lala\.$/) {
		print "Freda's\n";
	} elsif ($line =~ /^miao\./) {
		print "Rainbow's\n";
	} else {
		print "OMG>.< I don't know!\n";
	}
	$n--;
}

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

my $n = <>;
for my $i (1..$n) {
    my $s = <>;
    chomp($s);
    my $isFreda = 0;
    my $isRainbow = 0;
    if ($s =~ m/^miao\./) {
        $isRainbow = 1;
    }
    if ($s =~ m/lala\.$/) {
        $isFreda = 1;
    }
    if ($isFreda && ! $isRainbow) {
        print "Freda's" . "\n";
    } elsif ($isRainbow && !$isFreda) {
        print "Rainbow's" . "\n";
    } else {
        print "OMG>.< I don't know!" . "\n";
    }
}

#!/usr/bin/perl

while ($eil=<>) {

sub any{print "OMG>.< I don't know!\n"};

while (<>){

    if (/lala\.$/ and /^miao\./){any; next}
    if (/lala\.$/){print "Freda's\n"; next}
    if (/^miao\./){print "Rainbow's\n"; next}
    any;

    }
}

<>;
while (<>) {
    $f = $_ =~ /lala.$/;
    $r = $_ =~ /^miao./;
    if ($f == $r) { print "OMG>.< I don't know!"; }
    if ($f) { print "Freda's"; } elsif ($r) { print "Rainbow's"; }
}

chomp($n=<STDIN>);
while($n > 0) {
	chomp($line=<STDIN>);
	if($line eq 'miao\..*lala\.') {
		print "OMG>.< I don't know!\n";
	} elsif ($line =~ /lala\.$/) {
		print "Freda's\n";
	} elsif ($line =~ /^miao\./) {
		print "Rainbow's\n";
	} else {
		print "OMG>.< I don't know!\n";
	}
	$n--;
}

chomp($n=<STDIN>);
while($n > 0) {
	chomp($line=<STDIN>);
	if($line eq 'miao.lala.') {
		print "OMG>.< I don't know!\n";
	} elsif ($line =~ /lala\.$/) {
		print "Freda's\n";
	} elsif ($line =~ /^miao\./) {
		print "Rainbow's\n";
	} else {
		print "OMG>.< I don't know!\n";
	}
	$n--;
}

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

my $n = <>;
for my $i (1..$n) {
    my $s = <>;
    chomp($s);
    my $isFreda = 0;
    my $isRainbow = 0;
    if ($s =~ m/^miao\./) {
        $isRainbow = 1;
    }
    if ($s =~ m/lala\.$/) {
        $isFreda = 1;
    }
    print $isFreda . ' ' . $isRainbow . '\n';
    if ($isFreda && ! $isRainbow) {
        print "Freda's" . "\n";
    } elsif ($isRainbow && !$isFreda) {
        print "Rainbow's" . "\n";
    } else {
        print "OMG>.< I don't know!" . "\n";
    }
}

#!/usr/bin/perl

while ($eil=<>) {

while (<>){

    if (/lala\.$/ and !/miao\./){print "Freda's\n"; next}
    if (/miao\.$/){print "Rainbow's\n"; next}
    print "OMG>.< I don't know!\n"

    }
}

#!/usr/bin/perl

while ($eil=<>) {

while (<>){

    if (/lala\.$/ and !/miao\./){print "Freda's\n"; next}
    if (/miao\.$/ and !/lala\./){print "Rainbow's\n"; next}
    print "OMG>.< I don't know!\n"

    }
}

#!/usr/bin/perl

while ($eil=<>) {

sub any{print "OMG>.< I don't know!\n"};

while (<>){

    if (/lala\.$/ and /^miao\./){any; next}
    if (/miao\.$/){print "Rainbow's\n"; next}
    if (/^miao\./){print "Freda's\n"; next}
    any;

    }
}