Salem gave you $$$n$$$ sticks with integer positive lengths $$$a_1, a_2, \ldots, a_n$$$.For every stick, you can change its length to any other positive integer length (that is, either shrink or stretch it). The cost of changing the stick's length from $$$a$$$ to $$$b$$$ is $$$|a - b|$$$, where $$$|x|$$$ means the absolute value of $$$x$$$.A stick length $$$a_i$$$ is called almost good for some integer $$$t$$$ if $$$|a_i - t| \le 1$$$.Salem asks you to change the lengths of some sticks (possibly all or none), such that all sticks' lengths are almost good for some positive integer $$$t$$$ and the total cost of changing is minimum possible. The value of $$$t$$$ is not fixed in advance and you can choose it as any positive integer. As an answer, print the value of $$$t$$$ and the minimum cost. If there are multiple optimal choices for $$$t$$$, print any of them.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of sticks. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 100$$$) — the lengths of the sticks.

Print the value of $$$t$$$ and the minimum possible cost. If there are multiple optimal choices for $$$t$$$, print any of them.

NoteIn the first example, we can change $$$1$$$ into $$$2$$$ and $$$10$$$ into $$$4$$$ with cost $$$|1 - 2| + |10 - 4| = 1 + 6 = 7$$$ and the resulting lengths $$$[2, 4, 4]$$$ are almost good for $$$t = 3$$$.In the second example, the sticks lengths are already almost good for $$$t = 2$$$, so we don't have to do anything.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = sort { $a <=> $b } split ' ', <>;

	my @cand;

	for my $cand ( 1 .. 100 ){
		my $sum = 0;
		for( @_ ){
			my $x = abs( $cand - $_ ) - 1;
			$x < 0 and $x = 0;
			$sum += $x;
			}
		push @cand, [ $cand, $sum ];
		}

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


You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.

The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries. q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.

For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.

Note12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.8 = 4 + 4, 6 can't be split into several composite summands.1, 2, 3 are less than any composite number, so they do not have valid splittings.


Berland annual chess tournament is coming!Organizers have gathered 2·n chess players who should be divided into two teams with n people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.Thus, organizers should divide all 2·n players into two teams with n people each in such a way that the first team always wins.Every chess player has its rating ri. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.After teams assignment there will come a drawing to form n pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.Is it possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing?

The first line contains one integer n (1 ≤ n ≤ 100). The second line contains 2·n integers a1, a2, ... a2n (1 ≤ ai ≤ 1000).

If it's possible to divide all 2·n players into two teams with n people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print "YES". Otherwise print "NO".


Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.

The first line contains integer n (1 ≤ n ≤ 105) — the number of trees. Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.

Print a single number — the maximum number of trees that you can cut down by the given rules.

NoteIn the first sample you can fell the trees like that:   fell the 1-st tree to the left — now it occupies segment [ - 1;1]  fell the 2-nd tree to the right — now it occupies segment [2;3]  leave the 3-rd tree — it occupies point 5  leave the 4-th tree — it occupies point 10  fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].

$\ = $/;
$left = -10e9;
<>;
$ans = 0;

while(<>){
	$prev = $x;
	($x, $h) = split;
	$x <= $left and do {$ans -- ; $left = $prev };
	if ($x - $left > $h ){
		$left = $x;
		}
		else {
			$left = $x + $h;
			}
	$ans ++
}

print $ans


Because of budget cuts one IT company established new non-financial reward system instead of bonuses.Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets "I fixed a critical bug" pennant on his table. A man who suggested a new interesting feature gets "I suggested a new feature" pennant on his table.Because of the limited budget of the new reward system only 5 "I fixed a critical bug" pennants and 3 "I suggested a new feature" pennants were bought.In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded "I fixed a critical bug" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded "I suggested a new feature" pennants is passed on to his table.One man can have several pennants of one type and of course he can have pennants of both types on his table. There are n tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants.

The only line of the input contains one integer n (1 ≤ n ≤ 500) — the number of tables in the IT company.

Output one integer — the amount of ways to place the pennants on n tables.

#!/usr/bin/env perl

use bigint;

sub factorial {
    my $n = shift;
    if ($n==0) {
        return 1;
    } else {
        return $n*factorial($n-1);
    }
}

sub subfactorial {
    my $n = shift;
    my $sub = shift;
    if ($n==$sub) {
        return 1;
    } else {
        return $n*subfactorial($n-1, $sub);
    }
}

$tables = <STDIN>;
$result = 0;
$subnp1 = subfactorial($tables+3-1, $tables-1)/6;
$subnp2 = subfactorial($tables+5-1, $tables-1)/120;
$result = $subnp1*$subnp2;
print "$result\n";


We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$.  The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist?

The first line contains one integer $$$t$$$ ($$$1 \le t \le 6000$$$) — the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 \le n, k \le 10^6$$$) — the initial position of point $$$A$$$ and desirable absolute difference.

For each test case, print the minimum number of steps to make point $$$B$$$ exist.

NoteIn the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$|(2 - 0) - (4 - 2)| = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$.In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$|8 - 0| = 8$$$, so the answer is $$$3$$$.


Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!).Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps:  Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$;  Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$;  Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$;  Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$;  Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$;  Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$;  $$$\dots$$$  Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$;  Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i \in\{1, 2, \ldots, 5 \cdot 10^8\}$$$ as described above.For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$\rightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$\rightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ $$$\dots$$$ $$$\rightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$\rightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$\rightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array.Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it.

The first line of the input contains one integer number $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.

Print $$$n$$$ integers — $$$b_1, b_2, \dots, b_n$$$, where $$$b_i$$$ is the final value of the $$$i$$$-th element of the array after applying "Mishka's Adjacent Replacements Algorithm" to the array $$$a$$$. Note that you cannot change the order of elements in the array.

NoteThe first example is described in the problem statement.


DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS took a sheet of paper and drew $$$n$$$ distinct lines, given by equations $$$y = x + p_i$$$ for some distinct $$$p_1, p_2, \ldots, p_n$$$.Then JLS drew on the same paper sheet $$$m$$$ distinct lines given by equations $$$y = -x + q_i$$$ for some distinct $$$q_1, q_2, \ldots, q_m$$$.DLS and JLS are interested in counting how many line pairs have integer intersection points, i.e. points with both coordinates that are integers. Unfortunately, the lesson will end up soon, so DLS and JLS are asking for your help.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$), the number of test cases in the input. Then follow the test case descriptions. The first line of a test case contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the number of lines drawn by DLS. The second line of a test case contains $$$n$$$ distinct integers $$$p_i$$$ ($$$0 \le p_i \le 10^9$$$) describing the lines drawn by DLS. The integer $$$p_i$$$ describes a line given by the equation $$$y = x + p_i$$$. The third line of a test case contains an integer $$$m$$$ ($$$1 \le m \le 10^5$$$), the number of lines drawn by JLS. The fourth line of a test case contains $$$m$$$ distinct integers $$$q_i$$$ ($$$0 \le q_i \le 10^9$$$) describing the lines drawn by JLS. The integer $$$q_i$$$ describes a line given by the equation $$$y = -x + q_i$$$. The sum of the values of $$$n$$$ over all test cases in the input does not exceed $$$10^5$$$. Similarly, the sum of the values of $$$m$$$ over all test cases in the input does not exceed $$$10^5$$$. In hacks it is allowed to use only one test case in the input, so $$$t=1$$$ should be satisfied.

For each test case in the input print a single integer — the number of line pairs with integer intersection points.

NoteThe picture shows the lines from the first test case of the example. Black circles denote intersection points with integer coordinates.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my $oddA = grep $_ & 1, @A;
	my $oddB = grep $_ & 1, @B;

	my $evenA = grep !($_ & 1), @A;
	my $evenB = grep !($_ & 1), @B;

	print $oddA * $oddB + $evenA * $evenB;
	}


Innokentiy decides to change the password in the social net "Contact!", but he is too lazy to invent a new password by himself. That is why he needs your help. Innokentiy decides that new password should satisfy the following conditions:  the length of the password must be equal to n,  the password should consist only of lowercase Latin letters,  the number of distinct symbols in the password must be equal to k,  any two consecutive symbols in the password must be distinct. Your task is to help Innokentiy and to invent a new password which will satisfy all given conditions.

The first line contains two positive integers n and k (2 ≤ n ≤ 100, 2 ≤ k ≤ min(n, 26)) — the length of the password and the number of distinct symbols in it.  Pay attention that a desired new password always exists.

Print any password which satisfies all conditions given by Innokentiy.

NoteIn the first test there is one of the appropriate new passwords — java, because its length is equal to 4 and 3 distinct lowercase letters a, j and v are used in it.In the second test there is one of the appropriate new passwords — python, because its length is equal to 6 and it consists of 6 distinct lowercase letters.In the third test there is one of the appropriate new passwords — phphp, because its length is equal to 5 and 2 distinct lowercase letters p and h are used in it.Pay attention the condition that no two identical symbols are consecutive is correct for all appropriate passwords in tests.

use strict;
use warnings;

my ($n, $k) = map { 0 + $_ } split /\s+/, <>;
my @passwd_letters = ('a' .. chr(ord('a') + $k - 1));

my $pass = '';
while (length($pass) != $n) {
	$pass .= $passwd_letters[length($pass) % $k];
}
print "$pass\n";


You are given two circles. Find the area of their intersection.

The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle. The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.

Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.


Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is the correct formal description of the homework assignment:You are given a string $$$s$$$ of length $$$n$$$ only consisting of characters "a", "b" and "c". There are $$$q$$$ queries of format ($$$pos, c$$$), meaning replacing the element of string $$$s$$$ at position $$$pos$$$ with character $$$c$$$. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string "abc" as a substring. A valid replacement of a character is replacing it with "a", "b" or "c".A string $$$x$$$ is a substring of a string $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n, q \le 10^5)$$$, the length of the string and the number of queries, respectively. The second line contains the string $$$s$$$, consisting of characters "a", "b" and "c". Each of the next $$$q$$$ lines contains an integer $$$i$$$ and character $$$c$$$ $$$(1 \le i \le n)$$$, index and the value of the new item in the string, respectively. It is guaranteed that character's $$$c$$$ value is "a", "b" or "c".

For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain "abc" as a substring.

9 10
abcabcabc
1 a
1 b
2 c
3 a
4 b
5 c
8 a
9 b
1 c
4 a

NoteLet's consider the state of the string after each query:   $$$s =$$$ "abcabcabc". In this case $$$3$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcaccabb". This string does not contain "abc" as a substring. $$$s =$$$ "bbcabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bbcbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bccabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bccbbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcaabcabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcbbc". This string does not contain "abc" as a substring. $$$s =$$$ "bcabbcabc". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccabc". In this case $$$2$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcabb". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaac". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaac". This string does not contain "abc" as a substring. $$$s =$$$ "bcabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "bcabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccabccaab". In this case $$$1$$$ replacements can be performed to get, for instance, string $$$s =$$$ "ccabbcaab". This string does not contain "abc" as a substring. $$$s =$$$ "ccaaccaab". In this case the string does not contain "abc" as a substring and no replacements are needed.

#!/usr/bin/perl

use warnings;
use strict;

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

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

	my( $n, $q ) = split;
	$_ = <>, chomp;

	$debug and print "[$_]";

	my $cnt += () = m/abc/g;

	my @ans;

	for my $qq ( 1 .. $q ){
		my( $i, $c ) = split ' ', <>;

		my $lett = substr $_, $i - 1, 1;

		$debug and print "$qq:[$_], cnt:[$cnt], i:[$i], c:[$c], lett:[$lett]";

		if( $lett eq $c ){
			push @ans, $cnt;
			next;
			}

		$debug and print "change:";

		if( 0 ){ ; }
		elsif( $lett eq 'a' and $i < $n - 1 ){
			if( 'abc' eq substr $_, $i - 1, 3 ){
				$cnt --;
				}
			}
		elsif( $lett eq 'b' and $i > 0 and $i < $n - 0 ){
			if( 'abc' eq substr $_, $i - 2, 3 ){
				$cnt --;
				}
			}
		elsif( $lett eq 'c' and $i > 1 ){
			if( 'abc' eq substr $_, $i - 3, 3 ){
				$cnt --;
				}
			}

		substr $_, $i - 1, 1, $c;

		$debug and print "  [$_], cnt:[$cnt]";

		if( 0 ){ ; }
		elsif( $c eq 'a' and $i < $n - 1 ){
			if( 'abc' eq substr $_, $i - 1, 3 ){
				$cnt ++;
				}
			}
		elsif( $c eq 'b' and $i > 0 and $i < $n - 0 ){
			if( 'abc' eq substr $_, $i - 2, 3 ){
				$cnt ++;
				}
			}
		elsif( $c eq 'c' and $i > 1 ){
			if( 'abc' eq substr $_, $i - 3, 3 ){
				$cnt ++;
				}
			}

		$debug and print "  [$_], cnt:[$cnt]";

		push @ans, $cnt;
		}

	print join "\n", @ans;
	}


A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

NoteIn the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.


The city of Fishtopia can be imagined as a grid of $$$4$$$ rows and an odd number of columns. It has two main villages; the first is located at the top-left cell $$$(1,1)$$$, people who stay there love fishing at the Tuna pond at the bottom-right cell $$$(4, n)$$$. The second village is located at $$$(4, 1)$$$ and its people love the Salmon pond at $$$(1, n)$$$.The mayor of Fishtopia wants to place $$$k$$$ hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.A person can move from one cell to another if those cells are not occupied by hotels and share a side.Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?

The first line of input contain two integers, $$$n$$$ and $$$k$$$ ($$$3 \leq n \leq 99$$$, $$$0 \leq k \leq 2\times(n-2)$$$), $$$n$$$ is odd, the width of the city, and the number of hotels to be placed, respectively.

Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO". If it is possible, print an extra $$$4$$$ lines that describe the city, each line should have $$$n$$$ characters, each of which is "#" if that cell has a hotel on it, or "." if not.


Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:   the graph contains exactly 2n + p edges;  the graph doesn't contain self-loops and multiple edges;  for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges. A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. Your task is to find a p-interesting graph consisting of n vertices.

The first line contains a single integer t (1 ≤ t ≤ 5) — the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 ≤ n ≤ 24; p ≥ 0; ) — the number of vertices in the graph and the interest value for the appropriate test.  It is guaranteed that the required graph exists.

For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n.  Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.


Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.

First line of input consists of one integer n (1 ≤ n ≤ 3000). Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.

Output single integer — minimum amount of coins the colonel has to pay.

NoteIn first sample test we can increase factor of first badge by 1.In second sample test we can increase factors of the second and the third badge by 1.


SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the target in turns, and SmallR shoots first. The probability of shooting the target each time is  for SmallR while  for Zanoes. The one who shoots in the target first should be the winner.Output the probability that SmallR will win the match.

Print a single real number, the probability that SmallR will win the match. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

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

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

$c++;
for(1..1e5){
	$S+=$c*$`/$1; $c-=$c*$`/$1;
	$K+=$c*$2/$'; $c-=$c*$2/$'
}
print $S

}

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

sub fact{
	my $i= shift;   my $j= 1;
	$j*=$i-- while $i;
	return $j
}

# Transpose "charbox"
sub trans{
	my @t=(); my $i=0;
	$i=0, s/./$t[$i++].=$&/eg for @_;
	return @t
}

sub rotL{
	my @t=(); my $i=0; my $j;
	for (@_){
		$i=0, $j=length; $t[$i++].=chop while $j--
	}
	return @t
}

sub rotR{
    my @m=@_;  chomp @m;   my @t=();
	my $i= length $m[0];
	while ($i--){
		$t[$i]=~s/^/chop/e for @m
	}
return @t
}

sub revlines{
	$_=reverse $_ for @_;
	return @_
}

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

# rotL_s
# rotR_s

# slow?
sub revlines_s{
	$_=join" ", reverse split/\s+/ 	for @_;
	return @_
}

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

sub uniq{
	my @m=sort @_; my @u;
	push @u, my $i=shift @m;
	$i eq $_ or push @u, $i=$_ for @m;
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;

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

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

$c++;
for(1..1e3){
	$S+=$c*$`/$1; $c-=$c*$`/$1;
	$K+=$c*$2/$'; $c-=$c*$2/$'
}
print $S

}

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

sub fact{
	my $i= shift;   my $j= 1;
	$j*=$i-- while $i;
	return $j
}

# Transpose "charbox"
sub trans{
	my @t=(); my $i=0;
	$i=0, s/./$t[$i++].=$&/eg for @_;
	return @t
}

sub rotL{
	my @t=(); my $i=0; my $j;
	for (@_){
		$i=0, $j=length; $t[$i++].=chop while $j--
	}
	return @t
}

sub rotR{
    my @m=@_;  chomp @m;   my @t=();
	my $i= length $m[0];
	while ($i--){
		$t[$i]=~s/^/chop/e for @m
	}
return @t
}

sub revlines{
	$_=reverse $_ for @_;
	return @_
}

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

# rotL_s
# rotR_s

# slow?
sub revlines_s{
	$_=join" ", reverse split/\s+/ 	for @_;
	return @_
}

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

sub uniq{
	my @m=sort @_; my @u;
	push @u, my $i=shift @m;
	$i eq $_ or push @u, $i=$_ for @m;
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
}


Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view):  Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.To understand the problem better please read the notes to the test samples.

The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise.

Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.

NoteThe first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses.In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled:   In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher:   In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself:


You are given an array $$$a$$$ consisting of $$$n$$$ ($$$n \ge 3$$$) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array $$$[4, 11, 4, 4]$$$ all numbers except one are equal to $$$4$$$).Print the index of the element that does not equal others. The numbers in the array are numbered from one.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 100$$$). It is guaranteed that all the numbers except one in the $$$a$$$ array are the same.

For each test case, output a single integer — the index of the element that is not equal to others.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my %h;

	map $h{ $_ } ++, @_;

	my $this;

	$h{ $_ } == 1 and $this = $_ for keys %h;

	for( my $i = 0; $i < @_; $i ++ ){
		$_[ $i ] == $this and do {
			print $i + 1;
			last;
			};
		}
	}


We saw the little game Marmot made for Mole's lunch. Now it's Marmot's dinner time and, as we all know, Marmot eats flowers. At every dinner he eats some red and white flowers. Therefore a dinner can be represented as a sequence of several flowers, some of them white and some of them red.But, for a dinner to be tasty, there is a rule: Marmot wants to eat white flowers only in groups of size k.Now Marmot wonders in how many ways he can eat between a and b flowers. As the number of ways could be very large, print it modulo 1000000007 (109 + 7).

Input contains several test cases. The first line contains two integers t and k (1 ≤ t, k ≤ 105), where t represents the number of test cases. The next t lines contain two integers ai and bi (1 ≤ ai ≤ bi ≤ 105), describing the i-th test.

Print t lines to the standard output. The i-th line should contain the number of ways in which Marmot can eat between ai and bi flowers at dinner modulo 1000000007 (109 + 7).

Note  For K = 2 and length 1 Marmot can eat (R).  For K = 2 and length 2 Marmot can eat (RR) and (WW).  For K = 2 and length 3 Marmot can eat (RRR), (RWW) and (WWR).  For K = 2 and length 4 Marmot can eat, for example, (WWWW) or (RWWR), but for example he can't eat (WWWR).

# mod value by ((10 ** 9) + 7)
sub my_mod {
    return ($_[0] % 1000000007);
}

# read input

my $upper_bound = ((10 ** 5) + 13);
my ($t, $k, $f);
$f = <STDIN>;
chomp ( $f );
($t, $k) = split m/ /, $f;

# calculate dp
my @dp = ();
for my $i (1..($upper_bound)) {
    if ( $i > $k ) {
	$dp[ $i ] = &my_mod ( $dp[ $i-1 ] + $dp[ $i-$k ] );
    }
    elsif ( $i < $k ) {
	$dp[ $i ] = 1;
    }
    else {
	$dp[ $i ] = 2;
    }
    $dp[ $i ] = &my_mod ( $dp[ $i ] );
}

# calculate prefix sums
for ( my $j = 1; $j < $upper_bound ; $j += 1 ) {
    $dp[ $j ] = &my_mod ( $dp[ $j ] + $dp[ $j-1 ] );
}

# lookup queries
my $a, $b, $f;
for (1..$t) {
    $f = <STDIN>;
    chomp ( $f );
    ($a, $b) = split m/ /, $f;
    print (&my_mod ( $dp[ $b ] - $dp[ $a-1 ] ));
    print "\n";
}

# read input
my $upper_bound = ((10 ** 5) + 13);
my ($t, $k, $f);
$f = <STDIN>;
chomp ( $f );
($t, $k) = split m/ /, $f;

# calculate dp
my @dp = ();
for my $i (1..($upper_bound)) {
    if ( $i > $k ) {
	$dp[ $i ] = $dp[ $i-1 ] + $dp[ $i-$k ];
    }
    elsif ( $i < $k ) {
	$dp[ $i ] = 1;
    }
    else {
	$dp[ $i ] = 2;
    }

}

# calculate prefix sums
for ( my $j = 1; $j < $upper_bound ; $j += 1 ) {
    $dp[ $j ] += $dp[ $j-1 ];
}

# lookup queries
my $a, $b, $f;
for (1..$t) {
    $f = <STDIN>;
    chomp ( $f );
    ($a, $b) = split m/ /, $f;
    print (($dp[ $b ] - $dp[ $a-1 ]) % ((10 ** 9) + 7));
    print "\n";
}


You are given two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ elements, each element is either $$$0$$$ or $$$1$$$.You can make operations of $$$2$$$ kinds.   Pick an index $$$i$$$ and change $$$a_i$$$ to $$$1-a_i$$$.  Rearrange the array $$$a$$$ however you want. Find the minimum number of operations required to make $$$a$$$ equal to $$$b$$$.

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 400$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of the arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$a$$$. The third line of each test case contains $$$n$$$ space-separated integers $$$b_1,b_2,\ldots,b_n$$$ ($$$b_i$$$ is $$$0$$$ or $$$1$$$), representing the array $$$b$$$.

For each test case, print the minimum number of operations required to make $$$a$$$ equal to $$$b$$$.

NoteIn the first case, we need only one operation: change $$$a_1$$$ to $$$1-a_i$$$. Now $$$a = [0, 0]$$$ which is equal to $$$b$$$.In the second case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 11$$$. Now $$$a = [0, 0, 1]$$$ which is equal to $$$b$$$.In the second case, one of optimal ways would be to first change $$$a_3$$$ to $$$1 - a_3$$$, then rearrange $$$a$$$.In the third case, no operation is needed.In the fourth case, the optimal way is to rearrange $$$a$$$ to get the array $$$[0, 1, 1, 0]$$$.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my $L = 0;
	my $R = 0;

	while( @A ){
		my $i = ( shift @A ) - ( shift @B );

		if( $i < 0 ){
			$L ++;
			}
		elsif( $i > 0 ){
			$R ++;
			}
		}

	if( $L and $R ){
		print 1 + abs( $L - $R );
		}
	elsif( $L or $R ){
		print $L + $R;
		}
	else{
		print 0;
		}
	}


A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).For a positive integer $$$n$$$, we call a permutation $$$p$$$ of length $$$n$$$ good if the following condition holds for every pair $$$i$$$ and $$$j$$$ ($$$1 \le i \le j \le n$$$) —   $$$(p_i \text{ OR } p_{i+1} \text{ OR } \ldots \text{ OR } p_{j-1} \text{ OR } p_{j}) \ge j-i+1$$$, where $$$\text{OR}$$$ denotes the bitwise OR operation. In other words, a permutation $$$p$$$ is good if for every subarray of $$$p$$$, the $$$\text{OR}$$$ of all elements in it is not less than the number of elements in that subarray. Given a positive integer $$$n$$$, output any good permutation of length $$$n$$$. We can show that for the given constraints such a permutation always exists.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first and only line of every test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$).

For every test, output any good permutation of length $$$n$$$ on a separate line.

use strict;
use warnings;
use v5.020;

my $t = <>;
while($t--) {
    my $n;# = <>;
    chomp($n=<>);
    my @ans;
    if($n % 2 == 1){
        push @ans, int($n);
        for(my $i = 1; $i <= int($n/2); $i++){
            push @ans, $i;
            push @ans, $n-$i
        }
    } else {
        push @ans, int($n/2);
        for(my $i = 1; $i < $n/2; $i++){
            push @ans, $i;
            push @ans, $n+1-$i;
        }
        push @ans, $n/2 + 1;
    }
    say "@ans";
}


You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.For some indices i (1 ≤ i ≤ n - 1) it is possible to swap i-th element with (i + 1)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the number of times you swap i-th element with (i + 1)-th (if the position is not forbidden).Can you make this array sorted in ascending order performing some sequence of swapping operations?

The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 200000) — the elements of the array. Each integer from 1 to n appears exactly once. The third line contains a string of n - 1 characters, each character is either 0 or 1. If i-th character is 1, then you can swap i-th element with (i + 1)-th any number of times, otherwise it is forbidden to swap i-th element with (i + 1)-th.

If it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO.

NoteIn the first example you may swap a3 and a4, and then swap a4 and a5.


You are given a grid with $$$n$$$ rows and $$$m$$$ columns. Rows and columns are numbered from $$$1$$$ to $$$n$$$, and from $$$1$$$ to $$$m$$$. The intersection of the $$$a$$$-th row and $$$b$$$-th column is denoted by $$$(a, b)$$$. Initially, you are standing in the top left corner $$$(1, 1)$$$. Your goal is to reach the bottom right corner $$$(n, m)$$$.You can move in four directions from $$$(a, b)$$$: up to $$$(a-1, b)$$$, down to $$$(a+1, b)$$$, left to $$$(a, b-1)$$$ or right to $$$(a, b+1)$$$.You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach $$$(n, m)$$$?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of the test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^9$$$) — the size of the grid.

For each test case, print a single integer: $$$-1$$$ if it is impossible to reach $$$(n, m)$$$ under the given conditions, otherwise the minimum number of moves.

NoteTest case $$$1$$$: $$$n=1$$$, $$$m=1$$$, and initially you are standing in $$$(1, 1)$$$ so $$$0$$$ move is required to reach $$$(n, m) = (1, 1)$$$.Test case $$$2$$$: you should go down to reach $$$(2, 1)$$$.Test case $$$3$$$: it is impossible to reach $$$(1, 3)$$$ without moving right two consecutive times, or without leaving the grid.Test case $$$4$$$: an optimal moving sequence could be: $$$(1, 1) \to (1, 2) \to (2, 2) \to (2, 1) \to (3, 1) \to (3, 2) \to (4, 2)$$$. It can be proved that this is the optimal solution. So the answer is $$$6$$$.


Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all $$$m$$$ taxi drivers in the city, who every day transport the rest of the city residents — $$$n$$$ riders.Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $$$i$$$ the number $$$a_{i}$$$ — the number of riders that would call the $$$i$$$-th taxi driver when all drivers and riders are at their home?The taxi driver can neither transport himself nor other taxi drivers.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^5$$$) — number of riders and taxi drivers. The second line contains $$$n + m$$$ integers $$$x_1, x_2, \ldots, x_{n+m}$$$ ($$$1 \le x_1 < x_2 < \ldots < x_{n+m} \le 10^9$$$), where $$$x_i$$$ is the coordinate where the $$$i$$$-th resident lives.  The third line contains $$$n + m$$$ integers $$$t_1, t_2, \ldots, t_{n+m}$$$ ($$$0 \le t_i \le 1$$$). If $$$t_i = 1$$$, then the $$$i$$$-th resident is a taxi driver, otherwise $$$t_i = 0$$$. It is guaranteed that the number of $$$i$$$ such that $$$t_i = 1$$$ is equal to $$$m$$$.

Print $$$m$$$ integers $$$a_1, a_2, \ldots, a_{m}$$$, where $$$a_i$$$ is the answer for the $$$i$$$-th taxi driver. The taxi driver has the number $$$i$$$ if among all the taxi drivers he lives in the $$$i$$$-th smallest coordinate (see examples for better understanding).

NoteIn the first example, we have only one taxi driver, which means an order from any of $$$n$$$ riders will go to him.In the second example, the first taxi driver lives at the point with the coordinate $$$2$$$, and the second one lives at the point with the coordinate $$$6$$$. Obviously, the nearest taxi driver to the rider who lives on the $$$3$$$ coordinate is the first one, and to the rider who lives on the coordinate $$$5$$$ is the second one. The rider who lives on the $$$4$$$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.In the third example, we have one rider and the taxi driver nearest to him is the fourth one.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	@_ = split ' ', <>;

	my @A;
	my @B;

	for( split ' ', <> ){
		my $q = shift @_;
		$_ ? ( push @A, $q ) : ( push @B, $q );
		}

	$debug and print "@A";
	$debug and print "@B";

	push @A, 2e9 + 1;
	unshift @A, -1e9;

	my @cnt;

	for my $i ( 1 .. @A - 2 ){
		my $cnt = 0;

		my $mid;
		$mid = ( $A[ $i ] + $A[ $i - 1 ] ) / 2;
		$debug and print " mid: $mid";

		while( @B ){
			my $B = shift @B;
			if( $B > $A[ $i ] ){
				unshift @B, $B;
				last;
				}
			$cnt += $B > $mid;
			}

		$mid = ( $A[ $i ] + $A[ $i + 1 ] ) / 2;
		$debug and print " mid: $mid";

		while( @B ){
			my $B = shift @B;
			if( $B > $mid ){
				unshift @B, $B;
				last;
				}
			$cnt ++;
			}

		$debug and print " cnt: $cnt";

		push @cnt, $cnt;
		}

	print "@cnt";
	}


You are given two integers $$$A$$$ and $$$B$$$, calculate the number of pairs $$$(a, b)$$$ such that $$$1 \le a \le A$$$, $$$1 \le b \le B$$$, and the equation $$$a \cdot b + a + b = conc(a, b)$$$ is true; $$$conc(a, b)$$$ is the concatenation of $$$a$$$ and $$$b$$$ (for example, $$$conc(12, 23) = 1223$$$, $$$conc(100, 11) = 10011$$$). $$$a$$$ and $$$b$$$ should not contain leading zeroes.

The first line contains $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case contains two integers $$$A$$$ and $$$B$$$ $$$(1 \le A, B \le 10^9)$$$.

Print one integer — the number of pairs $$$(a, b)$$$ such that $$$1 \le a \le A$$$, $$$1 \le b \le B$$$, and the equation $$$a \cdot b + a + b = conc(a, b)$$$ is true.

NoteThere is only one suitable pair in the first test case: $$$a = 1$$$, $$$b = 9$$$ ($$$1 + 9 + 1 \cdot 9 = 19$$$).


The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.

The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly.  Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.

Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.


A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format:   at first, the text of the comment is written;  after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments);  after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm).  All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.For example, if the comments look like:  then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format:   at first, print a integer d — the maximum depth of nesting comments;  after that print d lines, the i-th of them corresponds to nesting level i;  for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space.

The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas.  It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid.

Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.

NoteThe first example is explained in the statements.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){

	my $i = 0;
	my $j = 0;
	my @A;
	my @j;

	() = / (\w+),(\d+)
		(?{

			push @{ $A[@j - 0] }, $1;

			$debug and print "<$&>";

			push @j, $2;

			while( 1 ){
				@j or last;
				$j = pop @j;
				$j or next;
				$j --;
				push @j, $j;
				last;
				}

			$debug and print "[@j]";
		})
	/gx;

	print ~~ @A;
	print "@$_" for @A;

	$debug and print '-' x 10;
	}


Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the rating of i-th student as ai. After the contest ends, every student will end up with some positive integer position. GukiZ expects that his students will take places according to their ratings. He thinks that each student will take place equal to . In particular, if student A has rating strictly lower then student B, A will get the strictly better position than B, and if two students have equal ratings, they will share the same position. GukiZ would like you to reconstruct the results by following his expectations. Help him and determine the position after the end of the contest for each of his students if everything goes as expected.

The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students.  The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).

In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.

NoteIn the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating.In the second sample, first student is the only one on the contest.In the third sample, students 2 and 5 share the first position with highest rating, student 4 is next with third position, and students 1 and 3 are the last sharing fourth position.

use strict;
use List::MoreUtils 'firstidx';

my $range = <STDIN>;
my @numbers = split / /, <STDIN>;
my @sorted = reverse sort { $a <=> $b } @numbers;

for my $i (0..$range-1) {
    $numbers[$i] = ( firstidx { $_ == $numbers[$i] } @sorted ) + 1;
}

$, = ' ';
print @numbers;

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

my $n = <>;
my @num = split ' ',<>;
my (%rank,%freq);
my ($idx,$last) = (1,0);
foreach(@num){
    $freq{$_}++;
}
foreach(sort {$b <=> $a} keys %freq){
    $rank{$_} = $last+1;
    $last += $freq{$_};
}
foreach(@num){
    print $rank{$_}, " ";
}

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

my $n = <>;
my @num = split ' ',<>;
my @sorted = sort {$a <=> $b} @num;
my (%rank,%cnt);
my ($idx,$last) = (1,0);
foreach(@sorted){
    $cnt{$_}++;
}
foreach(sort {$b <=> $a} keys %cnt){
    $rank{$_} = $last+1;
    $last += $cnt{$_};
}
foreach(@num){
    print $rank{$_}, " ";
}

use strict;
my $t = <STDIN>;
my @input = split //, <STDIN>;

my $max = 0;
for my $i ( @input ) {
    if ( $i > $max ) {
        $max = $i;
    }
}

my %ranks;
my $rank = 1;

while( $max ) {
    if ( grep { $_ == $max } @input ) {
        my $plus = scalar grep { $_ == $max } @input;
        $ranks{ $max } = $rank;
        $rank += $plus;
    }
    $max--;
}

print $ranks{$_} for @input;

use strict;
$, = ' ';
my $t = <STDIN>;
my @input = split //, <STDIN>;
my $max = 0;
for my $i ( @input ) {
    if ( $i > $max ) {
        $max = $i;
    }
}

my %ranks;
my $rank = 1;
my @values;
while( $max ) {
    if ( grep { $_ == $max } @input ) {
        my $plus = scalar grep { $_ == $max } @input;
        $ranks{ $max } = $rank;
        $rank += $plus;
    }
    $max--;
}

push @values, $ranks{$_} for @input;
print @values;

use strict;
use List::MoreUtils 'firstidx';

my $range = <STDIN>;
my @numbers = split //, <STDIN>;
my @sorted = reverse sort @numbers;

for my $i (0..$range-1) {
    $numbers[$i] = ( firstidx { /$numbers[$i]/ } @sorted ) + 1;
}

$, = ' ';
print @numbers;

use strict;
use List::MoreUtils 'firstidx';

my $range = <STDIN>;
my @numbers = split / /, <STDIN>;
my @sorted = reverse sort @numbers;

for my $i (0..$range-1) {
    $numbers[$i] = ( firstidx { /$numbers[$i]/ } @sorted ) + 1;
}

$, = ' ';
print @numbers;


Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values of  for all i from 1 to n. The expression ⌊ x⌋ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.

The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th skill of the character.

The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units.

NoteIn the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to  lfloor frac{100}{10} rfloor +  lfloor frac{100}{10} rfloor = 10 + 10 =  20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .


You're given a tree with $$$n$$$ vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$) denoting the size of the tree.  The next $$$n - 1$$$ lines contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$) each, describing the vertices connected by the $$$i$$$-th edge. It's guaranteed that the given edges form a tree.

Output a single integer $$$k$$$ — the maximum number of edges that can be removed to leave all connected components with even size, or $$$-1$$$ if it is impossible to remove edges in order to satisfy this property.

NoteIn the first example you can remove the edge between vertices $$$1$$$ and $$$4$$$. The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is $$$-1$$$.

use warnings;
use strict;
$\ = qq/\n/, $, = qq/ /;
chomp ( my $n = <STDIN> );
my %graph = ();
for ( 2 .. $n ) {
    my ( $u, $v ) = split qq/ /, <STDIN>;
    $graph { $u } -> { $v } = 1;
    $graph { $v } -> { $u } = 1;
}
my %nodes = ();
my %visited = ();
my $removed = 0;
sub dfs {
    return 0 if $visited { $_ [ 0 ] };
    my $curr = $_ [ 0 ];
    $visited { $curr } = 1;
    my $cnt = 1;
    for my $ii ( keys %{ $graph { $curr } } ) { $cnt += dfs ( $ii ) }
    if ( $cnt % 2 == 0 ) {
        $removed += 1;
        return 0;
    } else { return $cnt }
}
dfs ( 1 );
if ( $n % 2 == 1 ) { print qq/-1/ }
elsif ( $removed == 0 ) { print qq/-1/ }
else { print ( $removed - 1 ) }

$n = <>;
%graph = map { $_ => [] } (1..$n);
for (2..$n) {
	($u, $v) = split ' ', <>;
	push @{$graph{$u}}, $v;
	push @{$graph{$v}}, $u;
}

sub dfs {
	my $v = shift || 1;
	my $par = shift || -1;
	(my $removed, my $size) = (0, 1);
	for my $u (@{$graph{$v}}) {
		if ($u != $par) {
			my $child = dfs($u, $v); # [removed, size]
			$removed += $child->[0];
			if (($child->[1]) % 2 == 0) {
				$removed += 1;
			}
			$size += $child->[1];
		}
	}
	return [$removed, $size];
}

$removed = dfs()->[0];
print ($n % 2 == 0 ? $removed : -1);

#!/usr/bin/perl

use warnings;
use strict;

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

while(<>){
	my %h;

	map {
		my( $u, $v ) = split ' ', <>;
		$h{ $u }{ $v } ++;
		$h{ $v }{ $u } ++;
		} 1 .. $_ - 1;

	if( $_ % 2 ){
		print -1;
		next;
		}

	my @leafs = grep { 1 == keys %{ $h{ $_ } } } keys %h;

	$debug and print "leafs: @leafs";

	my @g = ( 1 ) x ( $_ + 1 );

	my $cnt = 0;

	my %next = map { $_ => 1 } @leafs;

##	my $c = 0;

	while( 1 ){

	##	$c ++ > 15 and last;

		@leafs = grep { 1 == keys %{ $h{ $_ } } } keys %next;

		$debug and print "  leafs: @leafs";

		last if not @leafs;

		undef %next;

		while( @leafs ){
			my $leaf = shift @leafs;
			my( $parent ) = keys %{ $h{ $leaf } };
			next if not $parent;
			$next{ $parent } ++;
			delete $h{ $parent }{ $leaf };
			delete $h{ $leaf };

			if( $g[ $leaf ] % 2 == 0 ){
				$cnt ++;
				}
			else{
				$g[ $parent ] += $g[ $leaf ];
				}
			}

		}

	print $cnt;

	$debug and print '-' x 15;
	}

use warnings;
use strict;
$\ = qq/\n/;
my $n = <STDIN>;
if ( $n == 0 ) { print qq/0/ }
elsif ( $n % 2 == 0 ) { print $n - 1 - int ( $n / 2 ) }
else { print qq/-1/ }

use warnings;
use strict;
$\ = qq/\n/, $, = qq/ /;
chomp ( my $n = <STDIN> );
my %graph = ();
for ( 2 .. $n ) {
    my ( $u, $v ) = split qq/ /, <STDIN>;
    $graph { $u } -> { $v } = 1;
    $graph { $v } -> { $u } = 1;
}
my %nodes = ();
my %visited = ();
my $removed = 0;
sub dfs {
    return 0 if $visited { $_ [ 0 ] };
    my $curr = $_ [ 0 ];
    $visited { $curr } = 1;
    my $cnt = 1;
    for my $ii ( keys %{ $graph { $curr } } ) { $cnt += dfs ( $ii ) }
    if ( $cnt % 2 == 0 ) {
        $removed += 1;
        return 0;
    } else { return $cnt }
}
dfs ( 1 );
if ( $removed == 0 ) { print qq/-1/ }
else { print ($removed - 1) }

use warnings;
use strict;
$\ = qq/\n/;
my $n = <STDIN>;
print qq/-1/ if $n % 2 == 1;
print $n - 1 - int ( $n / 2 ) if $n % 2 == 0;

use warnings;
use strict;
$\ = qq/\n/;
my $n = <STDIN>;
my %graph = ();
my ( $u, $v );
for my $edge ( 1 .. $n-1 ) {
    ( $u, $v ) = split ( qq/ /, <STDIN> );
    $graph { $u } = 0 if not exists $graph { $u };
    $graph { $u } += 1;
    $graph { $v } = 0 if not exists $graph { $v };
    $graph { $v } += 1;
}
my $val = grep { $_ == 1 } values %graph;
sub min { $_ [ 0 ] < $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }
sub max { $_ [ 0 ] > $_ [ 1 ] ? $_ [ 0 ] : $_ [ 1 ] }
if ( $n % 2 == 1 ) { print qq/-1/ }
else { print STDOUT max ( 0, min ( $n - 1 - $val, $n - 1 - int ( $n / 2 ) ) ) }


There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kilometers long.The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.Each city ci (except for the last city cn) has a supply of si liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times. Initially (at time zero) the Old Peykan is at city c1 and s1 liters of fuel is transferred to it's empty tank from c1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.Find the minimum time the Old Peykan needs to reach city cn.

The first line of the input contains two space-separated integers m and k (1 ≤ m, k ≤ 1000). The value m specifies the number of roads between cities which is equal to n - 1. The next line contains m space-separated integers d1, d2, ..., dm (1 ≤ di ≤ 1000) and the following line contains m space-separated integers s1, s2, ..., sm (1 ≤ si ≤ 1000).

In the only line of the output print a single integer — the minimum time required for The Old Peykan to reach city cn from city c1.

NoteIn the second sample above, the Old Peykan stays in c1 for 3 hours.

#! /usr/bin/perl -wl
#------------------------------------------------
# Author:    vj
# Created:   Thu Nov  1 21:33:17 IST 2012
# File Name: a.pl
# USAGE:
#       a.pl
#
#
#------------------------------------------------
use strict;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);

my $buf;

chomp ($buf = <STDIN>);
my ($m, $k) = split /\s+/, $buf;

chomp ($buf = <STDIN>);
my @d = split /\s+/, $buf;

chomp ($buf = <STDIN>);
my @f = split /\s+/, $buf;

my ($time, $fuel) = (0, 0);
my ($curfuel, $maxfuel) = (0, 0);

foreach (@d) {
  $curfuel = shift @f;
  $fuel += $curfuel;
  $maxfuel = $maxfuel > $curfuel ? $maxfuel : $curfuel;

  while ($fuel < $_) {
    $fuel += $maxfuel;
    $time += $k;
  }
  $time += $_;
  $fuel -= $_;
}
print $time;


Polycarp has created his own training plan to prepare for the programming contests. He will train for $$$n$$$ days, all days are numbered from $$$1$$$ to $$$n$$$, beginning from the first.On the $$$i$$$-th day Polycarp will necessarily solve $$$a_i$$$ problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems.Determine the index of day when Polycarp will celebrate the equator.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the number of days to prepare for the programming contests. The second line contains a sequence $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10\,000$$$), where $$$a_i$$$ equals to the number of problems, which Polycarp will solve on the $$$i$$$-th day.

Print the index of the day when Polycarp will celebrate the equator.

NoteIn the first example Polycarp will celebrate the equator on the evening of the second day, because up to this day (inclusive) he will solve $$$4$$$ out of $$$7$$$ scheduled problems on four days of the training.In the second example Polycarp will celebrate the equator on the evening of the third day, because up to this day (inclusive) he will solve $$$6$$$ out of $$$12$$$ scheduled problems on six days of the training.

use warnings;
use strict;
$\  = qq/\n/, $, = qq/ /;
<STDIN>;
sub ceil { int ( $_ [ 0 ] ) < $_ [ 0 ] ? int ( $_ [ 0 ] ) + 1 : $_ [ 0 ] }
my @arr = split qq/ /, <STDIN>;
my $sum = 0;
$sum += $_ for @arr;
my $one = ceil ( $sum / 2 );
my ( $two, $ii ) = ( 0, 0 );
while ( $two < $one ) {
    $two += $arr [ $ii ];
    $ii += 1;
}
print $ii;

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $sum = eval join ' + ', @_;

	my $half = 0;

	my $ans;

	for my $i ( 0 .. @_ - 1 ){
		$half += $_[ $i ];
		$half >= $sum / 2 and $ans = $i;
		$half >= $sum / 2 and last;
		}

	print $ans + 1;
	}


Given three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$, find the medium number between all of them.The medium number is the number that is neither the minimum nor the maximum of the given three numbers. For example, the median of $$$5,2,6$$$ is $$$5$$$, since the minimum is $$$2$$$ and the maximum is $$$6$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 6840$$$) — the number of test cases. The description of each test case consists of three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \leq a, b, c \leq 20$$$).

For each test case, output a single integer — the medium number of the three numbers.

<>;

print +( sort { $a <=> $b } split )[ 1 ] . $/ while <>

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	print +( sort { $a <=> $b } split )[ 1 ];
	}


Jack decides to invite Emma out for a dinner. Jack is a modest student, he doesn't want to go to an expensive restaurant. Emma is a girl with high taste, she prefers elite places.Munhattan consists of n streets and m avenues. There is exactly one restaurant on the intersection of each street and avenue. The streets are numbered with integers from 1 to n and the avenues are numbered with integers from 1 to m. The cost of dinner in the restaurant at the intersection of the i-th street and the j-th avenue is cij.Jack and Emma decide to choose the restaurant in the following way. Firstly Emma chooses the street to dinner and then Jack chooses the avenue. Emma and Jack makes their choice optimally: Emma wants to maximize the cost of the dinner, Jack wants to minimize it. Emma takes into account that Jack wants to minimize the cost of the dinner. Find the cost of the dinner for the couple in love.

The first line contains two integers n, m (1 ≤ n, m ≤ 100) — the number of streets and avenues in Munhattan. Each of the next n lines contains m integers cij (1 ≤ cij ≤ 109) — the cost of the dinner in the restaurant on the intersection of the i-th street and the j-th avenue.

Print the only integer a — the cost of the dinner for Jack and Emma.

NoteIn the first example if Emma chooses the first or the third streets Jack can choose an avenue with the cost of the dinner 1. So she chooses the second street and Jack chooses any avenue. The cost of the dinner is 2.In the second example regardless of Emma's choice Jack can choose a restaurant with the cost of the dinner 1.


A frog is currently at the point $$$0$$$ on a coordinate axis $$$Ox$$$. It jumps by the following algorithm: the first jump is $$$a$$$ units to the right, the second jump is $$$b$$$ units to the left, the third jump is $$$a$$$ units to the right, the fourth jump is $$$b$$$ units to the left, and so on.Formally:   if the frog has jumped an even number of times (before the current jump), it jumps from its current position $$$x$$$ to position $$$x+a$$$;  otherwise it jumps from its current position $$$x$$$ to position $$$x-b$$$. Your task is to calculate the position of the frog after $$$k$$$ jumps.But... One more thing. You are watching $$$t$$$ different frogs so you have to answer $$$t$$$ independent queries.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of queries. Each of the next $$$t$$$ lines contain queries (one query per line). The query is described as three space-separated integers $$$a, b, k$$$ ($$$1 \le a, b, k \le 10^9$$$) — the lengths of two types of jumps and the number of jumps, respectively.

Print $$$t$$$ integers. The $$$i$$$-th integer should be the answer for the $$$i$$$-th query.

NoteIn the first query frog jumps $$$5$$$ to the right, $$$2$$$ to the left and $$$5$$$ to the right so the answer is $$$5 - 2 + 5 = 8$$$.In the second query frog jumps $$$100$$$ to the right, $$$1$$$ to the left, $$$100$$$ to the right and $$$1$$$ to the left so the answer is $$$100 - 1 + 100 - 1 = 198$$$.In the third query the answer is $$$1 - 10 + 1 - 10 + 1 = -17$$$.In the fourth query the answer is $$$10^9 - 1 + 10^9 - 1 + 10^9 - 1 = 2999999997$$$.In the fifth query all frog's jumps are neutralized by each other so the answer is $$$0$$$.The sixth query is the same as the fifth but without the last jump so the answer is $$$1$$$.

#!/usr/bin/perl

use warnings;
use strict;
use bigint;

$\ = $/;

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

	for( @_ ){
		my( $A, $B, $k ) = split;

		if( $k % 2 == 1 ){
			print $A * int( $k / 2 ) + $A - $B * int( $k / 2 );
			}
		else{
			print 0 + ( $A - $B ) / 2 * $k;
			}

		}

	}


You are given an array $$$a$$$ of $$$n$$$ positive integers. Determine if, by rearranging the elements, you can make the array strictly increasing. In other words, determine if it is possible to rearrange the elements such that $$$a_1 < a_2 < \dots < a_n$$$ holds.

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 length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.

For each test case, output "YES" (without quotes) if the array satisfies the condition, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

NoteIn the first test case any rearrangement will keep the array $$$[1,1,1,1]$$$, which is not strictly increasing.In the second test case, you can make the array $$$[1,3,4,7,8]$$$.


In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions:  overall, there must be m dances; exactly three people must take part in each dance; each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland). The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance. You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the n dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.

The first line contains two space-separated integers n (3 ≤ n ≤ 105) and m (1 ≤ m ≤ 105) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.

Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.


Let's denote correct match equation (we will denote it as CME) an equation $$$a + b = c$$$ there all integers $$$a$$$, $$$b$$$ and $$$c$$$ are greater than zero.For example, equations $$$2 + 2 = 4$$$ (||+||=||||) and $$$1 + 2 = 3$$$ (|+||=|||) are CME but equations $$$1 + 2 = 4$$$ (|+||=||||), $$$2 + 2 = 3$$$ (||+||=|||), and $$$0 + 1 = 1$$$ (+|=|) are not.Now, you have $$$n$$$ matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!For example, if $$$n = 2$$$, you can buy two matches and assemble |+|=||, and if $$$n = 5$$$ you can buy one match and assemble ||+|=|||.   Calculate the minimum number of matches which you have to buy for assembling CME.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. The only line of each query contains one integer $$$n$$$ ($$$2 \le n \le 10^9$$$) — the number of matches.

For each test case print one integer in single line — the minimum number of matches which you have to buy for assembling CME.

NoteThe first and second queries are explained in the statement.In the third query, you can assemble $$$1 + 3 = 4$$$ (|+|||=||||) without buying matches.In the fourth query, buy one match and assemble $$$2 + 4 = 6$$$ (||+||||=||||||).

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	print $_ == 2 ? 2 : $_ % 2 ? 1 : 0;
	}

#!/usr/bin/env perl

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

# essential
my @tokens = ();

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

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

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

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

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

sub remainder {
    my ($dividend, $divisor) = @_;
}

# solve

my $q = read_token;

for (1..$q) {
    my $n = read_token;
    if ($n == 2) {
        say 2;
        next;
    }

    if ($n % 2 == 1) {
        say 1;
    }
    else {
        say 0;
    }
}


Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n.Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.

The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given.

Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them.

NoteIn the first case, the answer can be {1, 2} or {2, 1}.In the second case, another possible answer is {2, 4, 5, 1, 3}.


There is a given sequence of integers a1, a2, ..., an, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.

The first line contains an integer n (1 ≤ n ≤ 106). The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 3).

Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.

NoteIn the example all the numbers equal to 1 and 3 should be replaced by 2.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $N = <>;
chomp $N;
my $num_str = <>;
chomp $num_str;
my @nums = split /\s+/, $num_str;

#print"$N input <".join(',',@nums)."\n";

my @a =(0,0,0,0);
map{$a[$_]+=1} @nums;
#print "ansss:" .join(',',@a);
my $max = ($a[1] > $a[2])? $a[1]:$a[2];
$max = $a[3] if $a[3] > $max;
#print"max=$max\n";
my $ans = $N-$max;
print "$ans\n";
exit;


Recently Vladik discovered a new entertainment — coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!

The first line contains single integer t (1 ≤ t ≤ 10) — the number of chats. The t chats follow. Each chat is given in the following format. The first line of each chat description contains single integer n (1 ≤ n ≤ 100) — the number of users in the chat. The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters. The next line contains single integer m (1 ≤ m ≤ 100) — the number of messages in the chat. The next m line contain the messages in the following formats, one per line:    <username>:<text> — the format of a message with known sender. The username should appear in the list of usernames of the chat.  <?>:<text> — the format of a message with unknown sender.  The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters. We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha". It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.

Print the information about the t chats in the following format: If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print m messages in the following format: <username>:<text> If there are multiple answers, print any of them.

1
2
Vladik netman
2
?: Hello, Vladik!
?: Hi

1
2
netman vladik
3
netman:how are you?
?:wrong message
vladik:im fine

2
3
netman vladik Fedosik
2
?: users are netman, vladik, Fedosik
vladik: something wrong with this chat
4
netman tigerrrrr banany2001 klinchuh
4
?: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?
klinchuh: yes, coach!
?: yes, netman
banany2001: yes of course.

Impossible
netman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?
klinchuh: yes, coach!
tigerrrrr: yes, netman
banany2001: yes of course.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	for (1 .. $_){
		$debug and print '-' x 10;

		my $n = <>;
		my %names = map { $_, 1 } split ' ', <>;
		my $m = <>;
		my @msgs = map ~~<>, 1 .. $m;

		chomp @msgs;

		unshift @msgs, 'a!:';
		push @msgs, 'z!:';

		for (@msgs){
			my( $name, $text ) = split ':';
			my @found_names = grep { $names{ $_ } } /\w+/g;

			$debug and print "${name}[@found_names]";

			my %cand_names;

			if( $name eq '?' ){
				%cand_names = %names;
				delete $cand_names{ $_ } for @found_names;
			}

			$_ = {
				name => $name,
				text => $text,
				cand => { %cand_names },
				};
			}

		$debug and do {
			for (@msgs){
				print "[$_->{name}]",
					"[$_->{text}]",
					"[", (join ' ', keys %{$_->{cand}} ), "]"
				}
			};

		my $FAIL = 0;
		my $del = 1;

		while( $del ){
			$del = 0;
			for my $i (1 .. @msgs - 2, reverse 1 .. @msgs - 2){
				next if $msgs[$i]->{name} ne '?';

				$debug and print map "<<$_>>", join ' ', keys %{ $msgs[$i]->{cand} };

				delete $msgs[$i]->{cand}->{ $msgs[$i-1]->{name} } and $del ++;
				delete $msgs[$i]->{cand}->{ $msgs[$i+1]->{name} } and $del ++;

				$debug and print "del:$del";

				my $num = keys %{ $msgs[$i]->{cand} };
				if( $num == 1 ){
					( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };
					}
				elsif( $num == 0 ){
					$FAIL = 1;
					last;
					}
				}
			}

		$debug and do {
			for (@msgs){
				print "[$_->{name}]",
					"[$_->{text}]",
					"[", (join ' ', keys %{$_->{cand}} ), "]"
				}
			};

		my $idx = -2;
		my $key;

		for my $i (0 .. @msgs - 1){
			last if $FAIL;

			$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};

			$msgs[$i]->{name} eq '?' and do {
				$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];
				%{ $msgs[$i]->{cand} } = ();
				$msgs[$i]->{name} = $key;
				$idx = $i;
				};
			}

		shift @msgs;
		pop @msgs;

		print $FAIL ? "Impossible" : join "\n", map { "$_->{name}:$_->{text}" } @msgs;

		}

	}

$\ = $/;

while(<>){
	for (1 .. $_){

		<>;
		my %names = map { $_, 1 } split ' ', <>;
		my @msgs = map ~~<>, 1 .. <>;

		chomp @msgs;

		@msgs = ( '!:', @msgs, '!:' );

		for (@msgs){
			my( $name, $text ) = split ':';

			my %cand;

			if( $name eq '?' ){
				%cand = %names;
				delete $cand{ $_ } for grep $names{ $_ }, /\w+/g;
			}

			$_ = {
				name => $name,
				text => $text,
				cand => { %cand },
				};
			}

		my $FAIL = 0;
		my $del = 1;

		while( $del ){
			$del = 0;
			for my $i (1 .. @msgs - 2, reverse 1 .. @msgs - 2){
				next if $msgs[$i]->{name} ne '?';

				delete $msgs[$i]->{cand}->{ $msgs[$i + $_]->{name} } and $del ++ for -1, 1;

				my $num = keys %{ $msgs[$i]->{cand} };
				if( $num == 1 ){
					( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };
					}
				elsif( $num == 0 ){
					$FAIL = 1;
					last;
					}
				}
			}

		my $idx = -2;
		my $key;

		for my $i (0 .. @msgs - 1){
			last if $FAIL;

			$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};

			$msgs[$i]->{name} eq '?' and do {
				$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];
				%{ $msgs[$i]->{cand} } = ();
				$msgs[$i]->{name} = $key;
				$idx = $i;
				};
			}

		print $FAIL ? "Impossible" : join "\n", map { "$_->{name}:$_->{text}" } @msgs[1 .. @msgs-2];

		}

	}

$\ = $/;

while(<>){
	for (1 .. $_){

		<>;
		my %names = map { $_, 1 } split ' ', <>;
		my @msgs = map ~~<>, 1 .. <>;

		chomp @msgs;

		@msgs = ( '!:', @msgs, '!:' );

		for (@msgs){
			my( $name, $text ) = split ':';

			my %cand;

			if( $name eq '?' ){
				%cand = %names;
				delete $cand{ $_ } for grep $names{ $_ }, /\w+/g;
			}

			$_ = {
				name => $name,
				text => $text,
				cand => { %cand },
				};
			}

		my $FAIL = 0;
		my $del = 1;

		while( $del ){
			$del = 0;
			for my $i (1 .. @msgs - 2, 1 .. @msgs - 2){
				next if $msgs[$i]->{name} ne '?';

				delete $msgs[$i]->{cand}->{ $msgs[$i + $_]->{name} } and $del ++ for -1, 1;

				my $num = keys %{ $msgs[$i]->{cand} };
				if( $num == 1 ){
					( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };
					}
				elsif( $num == 0 ){
					$FAIL = 1;
					last;
					}
				}
			}

		my $idx = -2;
		my $key;

		for my $i (0 .. @msgs - 1){
			last if $FAIL;

			$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};

			$msgs[$i]->{name} eq '?' and do {
				$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];
				%{ $msgs[$i]->{cand} } = ();
				$msgs[$i]->{name} = $key;
				$idx = $i;
				};
			}

		print $FAIL ? "Impossible" : join "\n", map { "$_->{name}:$_->{text}" } @msgs[1 .. @msgs-2];

		}

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	for (1 .. $_){
		$debug and print '-' x 10;

		my $n = <>;
		my %names = map { $_, 1 } split ' ', <>;
		my $m = <>;
		my @msgs = map ~~<>, 1 .. $m;

		chomp @msgs;

		unshift @msgs, 'a!:';
		push @msgs, 'z!:';

		for (@msgs){
			my( $name, $text ) = split ':';
			my @found_names = grep { $names{ $_ } } /\w+/g;

			$debug and print "${name}[@found_names]";

			my %cand_names;

			if( $name eq '?' ){
				%cand_names = %names;
				delete $cand_names{ $_ } for @found_names;
			}

			$_ = {
				name => $name,
				text => $text,
				cand => { %cand_names },
				};
			}

		$debug and do {
			for (@msgs){
				print "[$_->{name}]",
					"[$_->{text}]",
					"[", (join ' ', keys %{$_->{cand}} ), "]"
				}
			};

		my $FAIL = 0;
		my $del = 1;

		while( $del ){
			$del = 0;
			for my $i (1 .. @msgs - 2){
				next if $msgs[$i]->{name} ne '?';

				$debug and print map "<<$_>>", join ' ', keys %{ $msgs[$i]->{cand} };

				delete $msgs[$i]->{cand}->{ $msgs[$i-1]->{name} } and $del ++;
				delete $msgs[$i]->{cand}->{ $msgs[$i+1]->{name} } and $del ++;

				$debug and print "del:$del";

				my $num = keys %{ $msgs[$i]->{cand} };
				if( $num == 1 ){
					( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };
					}
				elsif( $num == 0 ){
					$FAIL = 1;
					last;
					}
				}
			}

		$debug and do {
			for (@msgs){
				print "[$_->{name}]",
					"[$_->{text}]",
					"[", (join ' ', keys %{$_->{cand}} ), "]"
				}
			};

		shift @msgs;
		pop @msgs;

		print $FAIL ? "Impossible" : join "\n", map { "$_->{name}:$_->{text}" } @msgs;

		}

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	for (1 .. $_){
		$debug and print '-' x 10;

		my $n = <>;
		my %names = map { $_, 1 } split ' ', <>;
		my $m = <>;
		my @msgs = map ~~<>, 1 .. $m;

		chomp @msgs;

		unshift @msgs, 'a!:';
		push @msgs, 'z!:';

		for (@msgs){
			my( $name, $text ) = split ':';
			my @found_names = grep { $names{ $_ } } /\w+/g;

			$debug and print "${name}[@found_names]";

			my %cand_names;

			if( $name eq '?' ){
				%cand_names = %names;
				delete $cand_names{ $_ } for @found_names;
			}

			$_ = {
				name => $name,
				text => $text,
				cand => { %cand_names },
				};
			}

		$debug and do {
			for (@msgs){
				print "[$_->{name}]",
					"[$_->{text}]",
					"[", (join ' ', keys %{$_->{cand}} ), "]"
				}
			};

		my $FAIL = 0;
		my $del = 1;

		while( $del ){
			$del = 0;
			for my $i (1 .. @msgs - 2){
				next if $msgs[$i]->{name} ne '?';

				$debug and print map "<<$_>>", join ' ', keys %{ $msgs[$i]->{cand} };

				delete $msgs[$i]->{cand}->{ $msgs[$i-1]->{name} } and $del ++;
				delete $msgs[$i]->{cand}->{ $msgs[$i+1]->{name} } and $del ++;

				$debug and print "del:$del";

				my $num = keys %{ $msgs[$i]->{cand} };
				if( $num == 1 ){
					( $msgs[$i]->{name} ) = keys %{ $msgs[$i]->{cand} };
					}
				elsif( $num == 0 ){
					$FAIL = 1;
					last;
					}
				}
			}

		$debug and do {
			for (@msgs){
				print "[$_->{name}]",
					"[$_->{text}]",
					"[", (join ' ', keys %{$_->{cand}} ), "]"
				}
			};

		my $idx = -2;
		my $key;

		for my $i (0 .. @msgs - 1){
			last if $FAIL;

			$idx == $i - 1 and delete $msgs[$i]->{cand}->{$key};

			$msgs[$i]->{name} eq '?' and do {
				$key = ( keys %{ $msgs[$i]->{cand} } )[ 0 ];
				%{ $msgs[$i]->{cand} } = ();
				$msgs[$i]->{name} = $key;
				$idx = $i;
				};
			}

		shift @msgs;
		pop @msgs;

		print $FAIL ? "Impossible" : join "\n", map { "$_->{name}:$_->{text}" } @msgs;

		}

	}


You are given the array $$$a$$$ consisting of $$$n$$$ positive (greater than zero) integers.In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$i \ne j$$$) such that the absolute difference between $$$a_i$$$ and $$$a_j$$$ is no more than one ($$$|a_i - a_j| \le 1$$$) and remove the smallest of these two elements. If two elements are equal, you can remove any of them (but exactly one).Your task is to find if it is possible to obtain the array consisting of only one element using several (possibly, zero) such moves or not.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

For each test case, print the answer: "YES" if it is possible to obtain the array consisting of only one element using several (possibly, zero) moves described in the problem statement, or "NO" otherwise.

NoteIn the first test case of the example, we can perform the following sequence of moves:  choose $$$i=1$$$ and $$$j=3$$$ and remove $$$a_i$$$ (so $$$a$$$ becomes $$$[2; 2]$$$);  choose $$$i=1$$$ and $$$j=2$$$ and remove $$$a_j$$$ (so $$$a$$$ becomes $$$[2]$$$). In the second test case of the example, we can choose any possible $$$i$$$ and $$$j$$$ any move and it doesn't matter which element we remove.In the third test case of the example, there is no way to get rid of $$$2$$$ and $$$4$$$.

use strict;
use warnings;

my $t = <>;
while($t--){
    my $n = <>;
    my $find = 0;
    my @arr = split(' ', <>);

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

    for(my $i = 1; $i <= $#arr; $i++){
	    if($arr[$i] - $arr[$i-1] > 1){
            $find = 1;
            last;
	    }
    }
    if($find == 1){
	    print "NO\n";
    } else {
	    print "YES\n";
    }
}


You have n distinct points on a plane, none of them lie on OY axis. Check that there is a point after removal of which the remaining points are located on one side of the OY axis.

The first line contains a single positive integer n (2 ≤ n ≤ 105). The following n lines contain coordinates of the points. The i-th of these lines contains two single integers xi and yi (|xi|, |yi| ≤ 109, xi ≠ 0). No two points coincide.

Print "Yes" if there is such a point, "No" — otherwise. You can print every letter in any case (upper or lower).

NoteIn the first example the second point can be removed.In the second example there is no suitable for the condition point.In the third example any point can be removed.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $L = 0;
	my $R = 0;

	for( @_ ){
		my( $x, $y ) = split;
		$x > 0 and $R ++;
		$x < 0 and $L ++;
		}

	if( $R <= 1 or $L <= 1 ){
		print "Yes"
		}
	else{
		print "No"
		}
	}


Polycarp doesn't like integers that are divisible by $$$3$$$ or end with the digit $$$3$$$ in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.Polycarp starts to write out the positive (greater than $$$0$$$) integers which he likes: $$$1, 2, 4, 5, 7, 8, 10, 11, 14, 16, \dots$$$. Output the $$$k$$$-th element of this sequence (the elements are numbered from $$$1$$$).

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$k$$$ ($$$1 \le k \le 1000$$$).

For each test case, output in a separate line one integer $$$x$$$ — the $$$k$$$-th element of the sequence that was written out by Polycarp.


Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems — the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.

The first line contains integer n (1 ≤ n ≤ 30) — the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.

Print a string, consisting of lowercase English letters — the lexicographically minimum shortest original title.

5
threehorses
goodsubstrings
secret
primematrix
beautifulyear

NoteIn the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.

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

sub issubstr {
    my ($s,$a) = @_;
    for (0 .. @$a-1) {
	return 1 if index($a->[$_],$s) != -1;
    }
    return 0;
}

my $n = int <>;
my @a;
for (1 .. $n) {
    my $s = <>;
    chomp $s;
    push @a, $s;
}
my $s = 'a';
while (issubstr($s,\@a)) {
    $s++;
}
print $s;

my $num = <>;
chomp $num;
my @title;
for (1..$num) {
    my $title = <>;
    chomp $title;
    push @title, $title;
}

my $title = '';
my $flag = 0;
while (!$flag) {
    for ('a'..'z') {
        my $new = $title.$_;
        if (!grep /$new/, @title) {
            $title = $new;
            $flag = 1;
            last;
        }
    }
    if (!$flag) {
        if ($title =~ /^z*$/) {
            $title = 'a' x (1 + length $title);
        } else {
            $title =~ /(.*?)([^z]z*)$/;
            my $temp = $2;
            $temp =~ y/a-z/b-za/;
            $title = $1.$temp;
        }
    }

}
print $title;

my $num = <>;
chomp $num;
my @title;
for (1..$num) {
    my $title = <>;
    chomp $title;
    push @title, $title;
}

my $title = '';
my $flag = 0;
while (!$flag) {
    for ('a'..'z') {
        my $new = $title.$_;
        if (!grep /$new/, @title) {
            $title = $new;
            $flag = 1;
            last;
        }
    }
    $title .= 'a' if !$flag;
}
print $title;