Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.

The first line contains two space-separated integers x1, y1 ( - 106 ≤ x1, y1 ≤ 106) — the coordinates of your home. The second line contains two integers separated by a space x2, y2 ( - 106 ≤ x2, y2 ≤ 106) — the coordinates of the university you are studying at. The third line contains an integer n (1 ≤ n ≤ 300) — the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 ≤ ai, bi, ci ≤ 106; |ai| + |bi| > 0) — the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).

NotePictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):


Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular $$$n$$$-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the $$$OX$$$-axis and at least one of its edges is parallel to the $$$OY$$$-axis at the same time.Recall that a regular $$$n$$$-sided polygon is a convex polygon with $$$n$$$ vertices such that all the edges and angles are equal.Now he is shopping: the market has $$$t$$$ regular polygons. For each of them print YES if it is beautiful and NO otherwise.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of polygons in the market.  Each of the next $$$t$$$ lines contains a single integer $$$n_i$$$ ($$$3 \le n_i \le 10^9$$$): it means that the $$$i$$$-th polygon is a regular $$$n_i$$$-sided polygon.

For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).

NoteIn the example, there are $$$4$$$ polygons in the market. It's easy to see that an equilateral triangle (a regular $$$3$$$-sided polygon) is not beautiful, a square (a regular $$$4$$$-sided polygon) is beautiful and a regular $$$12$$$-sided polygon (is shown below) is beautiful as well.

#
# Hello World Program in Perl
#
$n = <STDIN>;
chomp $n;
@ip = <STDIN>;
chomp @ip;
foreach (@ip) {
    if ($_ != " ") {
    if ($_%4==0) {
        print "YES\n";
    }else {
        print "NO\n";
    }
    }
}


You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

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

Print the number of local extrema in the given array.

# get input
my $a;
my $b;
$a = <STDIN>;
chomp ( $b = <STDIN> );
my @arr = split m/ /, $b;

# variable counts extrema
my $extrema = 0;

for ( my $i = 1; $i < (@arr - 1); $i += 1 ) {

    # if is local min or local max, increment extrema
    if ( (@arr[ $i ] < @arr[ $i-1 ] && @arr[ $i ] < @arr[ $i+1 ]) ||
	 (@arr[ $i ] > @arr[ $i-1 ] && @arr[ $i ] > @arr[ $i+1 ]) ) {
	$extrema += 1; }}

# print answer
print "$extrema\n";

# get input
my $a;
my $b;
$a = <STDIN>;
chomp ( $b = <STDIN> );
my @arr = split m/ /, $b;

# variable counts extrema
my $extrema = 0;

for ( my $i = 1; $i < (@arr - 1); $i += 1 ) {

    # if is local min or local max, increment extrema
    if ( ($arr[ $i ] < $arr[ $i-1 ] && $arr[ $i ] < $arr[ $i+1 ]) ||
	 ($arr[ $i ] > $arr[ $i-1 ] && $arr[ $i ] > $arr[ $i+1 ]) ) {
	$extrema += 1; }}

# print answer
print "$extrema\n";


Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Print a single integer – the answer to the problem.

NoteIn the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.


Consider a conveyor belt represented using a grid consisting of $$$n$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row from the top and the $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Every cell, except $$$(n,m)$$$, has a direction R (Right) or D (Down) assigned to it. If the cell $$$(i,j)$$$ is assigned direction R, any luggage kept on that will move to the cell $$$(i,j+1)$$$. Similarly, if the cell $$$(i,j)$$$ is assigned direction D, any luggage kept on that will move to the cell $$$(i+1,j)$$$. If at any moment, the luggage moves out of the grid, it is considered to be lost. There is a counter at the cell $$$(n,m)$$$ from where all luggage is picked. A conveyor belt is called functional if and only if any luggage reaches the counter regardless of which cell it is placed in initially. More formally, for every cell $$$(i,j)$$$, any luggage placed in this cell should eventually end up in the cell $$$(n,m)$$$. This may not hold initially; you are, however, allowed to change the directions of some cells to make the conveyor belt functional. Please determine the minimum amount of cells you have to change.Please note that it is always possible to make any conveyor belt functional by changing the directions of some set of cells.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 100$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line, $$$a_{i,j}$$$ is the initial direction of the cell $$$(i, j)$$$. Please note that $$$a_{n,m}=$$$ C.

For each case, output in a new line the minimum number of cells that you have to change to make the conveyor belt functional.

4
3 3
RRD
DDR
RRC
1 4
DDDC
6 9
RDDDDDRRR
RRDDRRDDD
RRDRDRRDR
DDDDRDDRR
DRRDRDDDR
DDRDRRDDC
1 1
C

NoteIn the first case, just changing the direction of $$$(2,3)$$$ to D is enough.You can verify that the resulting belt is functional. For example, if we place any luggage at $$$(2,2)$$$, it first moves to $$$(3,2)$$$ and then to $$$(3,3)$$$. In the second case, we have no option but to change the first $$$3$$$ cells from D to R making the grid equal to RRRC.


You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.

The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Print the maximum length of an increasing subarray of the given array.

    #!/usr/bin/env perl

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

    use Carp;

    # essential
    my @tokens = ();

    sub ceil {
        my $n = shift;
        my $in = int($n);
        return $n if $in == $n;
        return $in + 1;
    }

    sub say_all {
        my @args = @_;
        say join ' ', @args;
    }

    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 ref_ref_scalar {
        my $ref = shift;
        return 1 if ref($ref) eq 'SCALAR';
    }

    sub toggle {
        my $ref = shift;
        croak "$ref does not reference to scalar" if !ref_ref_scalar($ref);

        $$ref = !$$ref;
    }

    sub odd {
        my $num = shift;
        return $num % 2 == 1;
    }

    sub even {
        my $num = shift;
        return $num % 2 == 0;
    }

    sub sum_of_digits {
        my $n = shift;
        my @numbers = split q{}, $n;

        my $sum = 0;

        for (@numbers) {
            $sum += $_;
        }

        return $sum;
    }

    # solution

    my $n = read_token;

    my @arr = split q{ }, read_line;

    my $pre_number = shift @arr;
    my $len = 1;
    my $answer = 0;

    for (@arr) {
        if ($_ <= $pre_number) {
            $answer = max($answer, $len);
            $len = 1;
            $pre_number = $_;
            next;
        }
        $len++;
        $pre_number = $_;

    }

    $answer = max($answer, $len);

    say $answer;

#!/usr/bin/env perl

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

use Carp;

# essential
my @tokens = ();

sub ceil {
    my $n = shift;
    my $in = int($n);
    return $n if $in == $n;
    return $in + 1;
}

sub say_all {
    my @args = @_;
    say join ' ', @args;
}

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 ref_ref_scalar {
    my $ref = shift;
    return 1 if ref($ref) eq 'SCALAR';
}

sub toggle {
    my $ref = shift;
    croak "$ref does not reference to scalar" if !ref_ref_scalar($ref);

    $$ref = !$$ref;
}

sub odd {
    my $num = shift;
    return $num % 2 == 1;
}

sub even {
    my $num = shift;
    return $num % 2 == 0;
}

sub sum_of_digits {
    my $n = shift;
    my @numbers = split q{}, $n;

    my $sum = 0;

    for (@numbers) {
        $sum += $_;
    }

    return $sum;
}

# solution

my $n = read_token;

my $pre_number = read_token;
my $len = 1;
my $answer = 0;

for (1..$n-1) {
    my $num = read_token;
    if ($num <= $pre_number) {
        $answer = max($answer, $len);
        $len = 1;
        $pre_number = $num;
        next;
    }
    $len++;
    $pre_number = $num;

}

$answer = max($answer, $len);

say $answer;


Let's dive into one of the most interesting areas of magic — writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic words, but only get to know the basic rules of writing spells.Each spell consists of several lines. The line, whose first non-space character is character "#" is an amplifying line and it is responsible for spell power. The remaining lines are common, and determine the effect of the spell.You came across the text of some spell. Spell was too long, so you cannot understand its meaning. So you want to make it as short as possible without changing the meaning.The only way to shorten a spell that you know is the removal of some spaces and line breaks. We know that when it comes to texts of spells, the spaces carry meaning only in the amplifying lines, so we should remove all spaces in other lines. Newlines also do not matter, unless any of the two separated lines is amplifying. Thus, if two consecutive lines are not amplifying, they need to be joined into one (i.e. we should concatenate the second line to the first one). Removing spaces in amplifying lines and concatenating the amplifying lines to anything is forbidden.Note that empty lines must be processed just like all the others: they must be joined to the adjacent non-amplifying lines, or preserved in the output, if they are surrounded with amplifying lines on both sides (i.e. the line above it, if there is one, is amplifying, and the line below it, if there is one, is amplifying too).For now those are the only instructions for removing unnecessary characters that you have to follow (oh yes, a newline is a character, too).The input contains the text of the spell, which should be reduced. Remove the extra characters and print the result to the output.

The input contains multiple lines. All characters in the lines have codes from 32 to 127 (inclusive). Please note that the lines may begin with or end with one or more spaces. The size of the input does not exceed 1048576 ( = 220) bytes. Newlines are included in this size. In the Windows operating system used on the testing computer, a newline is a sequence of characters with codes #13#10. It is guaranteed that after each line of input there is a newline. In particular, the input ends with a newline. Note that the newline is the end of the line, and not the beginning of the next one. It is guaranteed that the input contains at least one character other than a newline. It is recommended to organize the input-output line by line, in this case the newlines will be processed correctly by the language means.

Print the text of the spell where all extra characters are deleted. Please note that each output line should be followed by a newline. Please be careful: your answers will be validated by comparing them to the jury's answer byte-by-byte. So, all spaces and newlines matter.

#   include <cstdio>

using namespace std;

int main     (   ){
puts("Hello # World"); #
#
}

#   include <cstdio>
usingnamespacestd;intmain(){puts("Hello#World");#
#
}

NoteIn the first sample the amplifying lines are lines 1 and 7. So, lines 2 to 6 are concatenated to each other, all spaces are deleted from them.In the second sample the amplifying lines are lines 1 and 3. So, no lines are concatenated to each other.


Dreamoon likes to play with sets, integers and .  is defined as the largest positive integer that divides both a and b.Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs of distinct elements si, sj from S, .Given k and n, Dreamoon wants to make up n sets of rank k using integers from 1 to m such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum m that makes it possible and print one possible solution.

The single line of the input contains two space separated integers n, k (1 ≤ n ≤ 10 000, 1 ≤ k ≤ 100).

On the first line print a single integer — the minimal possible m.  On each of the next n lines print four space separated integers representing the i-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal m, print any one of them.

NoteFor the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since .


The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time.All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one).The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous.Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value.

The first input line contains two integers n and m (2 ≤ n ≤ 100, ) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space.  It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads.

Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6.

NoteIn the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make .In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal .


After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use. The second line contains n digits — Sherlock's credit card number. The third line contains n digits — Moriarty's credit card number.

First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.

NoteFirst sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	chomp;

	my( $A, $B ) = map { chomp; $_ } map ~~<>, 1 .. 2;

	my @A = split //, $A;
	my @B = split //, $B;

	my @sA = sort @A;
	my @sB = sort @B;

	my $cnt = $_;

	for (@sA){
		@sB or last;
		my $s = shift @sB;
		if( $s < $_ ){ redo }
		$cnt --;
		}

	print $cnt;

	@sB = sort @B;

	$cnt = 0;

	for (@sA){
		@sB or last;
		my $s = shift @sB;
		if( $s <= $_ ){ redo }
		$cnt ++;
		}

	print $cnt;

	}


On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle  (where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value  should be minimum possible.If there are many optimal solutions, Mister B should be satisfied with any of them.

First and only line contains two space-separated integers n and a (3 ≤ n ≤ 105, 1 ≤ a ≤ 180) — the number of vertices in the polygon and the needed angle, in degrees.

Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.

NoteIn first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: "3 1 2", "3 2 4", "4 2 3", "4 3 1", "1 3 4", "1 4 2", "2 4 1", "4 1 3", "3 1 4", "3 4 2", "2 4 3", "2 3 1", "1 3 2", "1 2 4", "4 2 1".In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| < |45 - 68|. Other correct answers are: "2 1 4", "3 2 1", "1 2 3", "4 3 2", "2 3 4", "1 4 3", "3 4 1".

#!/usr/bin/perl

use warnings;
use strict;

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

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

	my $beta = ( 180 - 360 / $n ) / ( $n - 2 );

	$debug and print $beta;

	my $m = int $alpha / $beta;

	$m +=
	abs( $m * $beta - $alpha ) < abs( ($m + 1) * $beta - $alpha ) ?
		0 : 1;

	$m ||= 1;

	$debug and print $m;

	print join ' ', 2, 1, 2 + $m > $n ? $n : 2 + $m;

	}

#!/usr/bin/perl

use warnings;
use strict;

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

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

	my $beta = ( 180 - 360 / $n ) / ( $n - 2 );

	$debug and print $beta;

	my $m = int $alpha / $beta;

	$m +=
	abs( $m * $beta - $alpha ) < abs( ($m + 1) * $beta - $alpha ) ?
		0 : 1;

	$m ||= 1;

	$debug and print $m;

	print join ' ', 2, 1, 2 + $m;

	}


Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.Andryusha wants to use as little different colors as possible. Help him to choose the colors!

The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park. Each of the next (n - 1) lines contains two integers x and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path. It is guaranteed that any square is reachable from any other using the paths.

In the first line print single integer k — the minimum number of colors Andryusha has to use. In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.

NoteIn the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.    Illustration for the first sample. In the second example there are following triples of consequently connected squares:   1 → 3 → 2  1 → 3 → 4  1 → 3 → 5  2 → 3 → 4  2 → 3 → 5  4 → 3 → 5  We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.    Illustration for the second sample. In the third example there are following triples:   1 → 2 → 3  2 → 3 → 4  3 → 4 → 5  We can see that one or two colors is not enough, but there is an answer that uses three colors only.    Illustration for the third sample.


The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations:  Operation ++ increases the value of variable x by 1.  Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).

The first line contains a single integer n (1 ≤ n ≤ 150) — the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter «X»). Thus, there are no empty statements. The operation and the variable can be written in any order.

Print a single integer — the final value of x.

#!/usr/bin/env perl

use v5.012;
use warnings;

my $cnt = <STDIN>;

my $x = 0;
while ($cnt--) {
	chomp(my $stmt = <STDIN>);
	$x++ if $stmt =~ /\+\+/;
	$x-- if $stmt =~ /\-\-/;
}
print $x;

use strict;
use warnings;

my $n = <>;
my $ans = 0;

while($n-- > 0){
    my $var = <>;
    if($var =~ /\+\+/){
        $ans++;
    }elsif($var =~ /--/){
        $ans--;
    }
}
print "$ans\n";

my $n = <>;
my $ans = 0;
my $str;
for (1..$n) {
    $str = <>;
    chomp $str;
    $ans++ if ($str eq "++X" || $str eq "X++");
    $ans-- if ($str eq "--X" || $str eq "X--");
}
print $ans . "\n";

#!/usr/bin/env perl

use 5.010;

@lines = <>;
$accum = 0;

foreach $line (@lines) {
	if ($line =~ /\+\+/) {
		$accum++;
	}
	if ($line =~ /--/) {
		$accum--;
	}
}

say $accum;

#!perl

use strict;
use warnings;

chomp( my $n =<STDIN> );

my $acc_num = 0 ;
foreach ( 0 .. ($n-1) ){
    my $readnum = <STDIN> ;

    if ( $readnum =~ m/\+\+/ ){
        $acc_num++;
    }else{
        $acc_num--;
    }
}
print "$acc_num\n" ;

#!/usr/bin/perl
# your code goes here

$x = 0;
chomp($n = <>);
while ($n-- > 0) {
	chomp($line = <>);
	if ((substr $line, 1, 1) eq "+") {
		$x++
	} else {
		$x--;
	}
}
print $x;

chomp ($n = <STDIN>);
$x = 0;
while ($n--) {
	chomp ($str = <STDIN>);
	if ($str =~ /--/) { $x-- } else { $x++}
}
printf "$x\n";

use strict;
use warnings;

my $n = <>;
chomp($n);

die ('n should be between 1 and 150') unless $n >= 1 && $n <= 150;

my $x = 0;
for (my $i = 0; $i < $n; $i++) {
    my $in = <>;
    chomp($in);
    if ($in =~ m/\+\+/) {
        $x++;
    } elsif ($in =~ m/\-\-/) {
        $x--;
    } else {
        die ('invalid input');
    }
}

print $x;

use strict;
use warnings;

my $n = <>;
my $x = 0;
for (1..$n) {
    my @term = split '', <>;
    if ($term[1] eq '+') {
        $x++;
    } else {
        $x--;
    }
}

print "$x\n";

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

chomp(my $n = <STDIN>);
my $x = 0;
for (my $i = 0; $i < $n; $i++) {
    chomp(my $cmd = <STDIN>);
    if (index($cmd, "++") != -1) {
        $x += 1;
    } elsif (index($cmd, "--") != -1) {
        $x -= 1;
    }
}
print $x;

use strict;
use warnings;
my $N=<STDIN>;
my $X=0;
for(my $i=0;$i<$N;$i++){
   my $s=<STDIN>;
   if ($s =~ /[X++]{3}/ ){
      $X+=1;
   }
   else {
      $X-=1;
   }
}
print STDOUT $X;

#!usr/bin/perl
#use warnings;
use 5.012;
use 5.010;
use 5.014;
#use strict;
#use autodie;
my($n,$count,$x,$line);
chomp($n=<STDIN>);
$count=0;
while($count ne $n)
{
    $count++;
    chomp($line=<STDIN>);
    if($line=~m/\+\+/)
    {$x++;}
    else
    {$x--;}
}
say "$x";

#!/usr/bin/perl
use strict;

my $n = <STDIN>;
chomp($n);

my $x = 0;

foreach (1..$n) {
	$_ = <STDIN>;
	chomp;
	if ($_ eq "X++" or $_ eq "++X") { $x++; }
	else { $x--; }
}

print $x;

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

my $x = 0; <STDIN>;
/-/ ? --$x : ++$x while <STDIN>;
print $x . "\n"

$n = <STDIN>;
@a = undef;
$result = 0;
foreach (0..$n - 1) {
	chomp ($a[$_] = <STDIN>);
	#unshift (@a, <STDIN>)
}
foreach $a (@a) {
	if (index ($a, '+') >= 0) {
		$result += 1;
	} else {
		$result -= 1;
	}
}
print ($result);

my $len = <STDIN>;
my $ans = 0;

for (1 .. $len) {
	my $middle = substr <STDIN>, 1, 1;

	$ans += $middle eq "+"? 1 : -1;
}

print $ans."\n";

my $len = <STDIN>;
my $ans = 0;

for my $val (1 .. $len) {
	my $comm = <STDIN>;
	my $middle = substr $comm, 1, 1;

	$ans = $middle eq "+"? ($ans + 1) : ($ans - 1);
}

print $ans."\n";

use strict;
use warnings;
my $N=<STDIN>;
my $X=0;
for(my $i=0;$i<$N;$i++){
   my $s=<STDIN>;
   if ($s == "X++" || $s == "++X"){
      $X+=1;
   }
   else {
      $X-=1;
   }
}
print STDOUT; $X;

use strict;
use warnings;
my $N=<STDIN>;
my $X=0;
for(my $i=0;$i<$N;$i++){
   my $s=<STDIN>;
   if ($s == "X++"){
      $X+=1;
   }
   else {
      $X-=1;
   }
}
print STDOUT; $X;

use strict;
use warnings;
my $N=<STDIN>;
my $X=0;
for(my $i=0;$i<$N;$i++){
   my $s=<STDIN>;
   if ($s == "X++" || $s == "++X"){
      $X+=1;
   }
   else {
      $X-=1;
   }
}
print STDOUT $X;

#!usr/bin/perl
#use warnings;
use 5.012;
use 5.010;
use 5.014;
#use strict;
#use autodie;
my($n,$count,$x,$line);
chomp($n=<STDIN>);
$count=0;
while($count lt $n)
{
    $count++;
    chomp($line=<STDIN>);
    if($line=~m/\+\+/)
    {$x+=1;}
    else
    {$x-=1;}
}
say "$x";


Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such natural phenomena strongly influence all life on our planet.Our hero Vasya is quite concerned about the problems. He decided to try a little experiment and observe how outside daily temperature changes. He hung out a thermometer on the balcony every morning and recorded the temperature. He had been measuring the temperature for the last n days. Thus, he got a sequence of numbers t1, t2, ..., tn, where the i-th number is the temperature on the i-th day.Vasya analyzed the temperature statistics in other cities, and came to the conclusion that the city has no environmental problems, if first the temperature outside is negative for some non-zero number of days, and then the temperature is positive for some non-zero number of days. More formally, there must be a positive integer k (1 ≤ k ≤ n - 1) such that t1 < 0, t2 < 0, ..., tk < 0 and tk + 1 > 0, tk + 2 > 0, ..., tn > 0. In particular, the temperature should never be zero. If this condition is not met, Vasya decides that his city has environmental problems, and gets upset.You do not want to upset Vasya. Therefore, you want to select multiple values of temperature and modify them to satisfy Vasya's condition. You need to know what the least number of temperature values needs to be changed for that.

The first line contains a single integer n (2 ≤ n ≤ 105) — the number of days for which Vasya has been measuring the temperature.  The second line contains a sequence of n integers t1, t2, ..., tn (|ti| ≤ 109) — the sequence of temperature values. Numbers ti are separated by single spaces.

Print a single integer — the answer to the given task.

NoteNote to the first sample: there are two ways to change exactly one number so that the sequence met Vasya's condition. You can either replace the first number 1 by any negative number or replace the number -2 by any positive number.


An array is beautiful if both of the following two conditions meet:  there are at least $$$l_1$$$ and at most $$$r_1$$$ elements in the array equal to its minimum;  there are at least $$$l_2$$$ and at most $$$r_2$$$ elements in the array equal to its maximum. For example, the array $$$[2, 3, 2, 4, 4, 3, 2]$$$ has $$$3$$$ elements equal to its minimum ($$$1$$$-st, $$$3$$$-rd and $$$7$$$-th) and $$$2$$$ elements equal to its maximum ($$$4$$$-th and $$$5$$$-th).Another example: the array $$$[42, 42, 42]$$$ has $$$3$$$ elements equal to its minimum and $$$3$$$ elements equal to its maximum.Your task is to calculate the minimum possible number of elements in a beautiful array.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Each test case consists of one line containing four integers $$$l_1$$$, $$$r_1$$$, $$$l_2$$$ and $$$r_2$$$ ($$$1 \le l_1 \le r_1 \le 50$$$; $$$1 \le l_2 \le r_2 \le 50$$$).

For each test case, print one integer — the minimum possible number of elements in a beautiful array.

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

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

while( $testcase -- > 0 ){

  my ($l1,$r1,$l2,$r2) = map { $_ - 0 } split(/\s+/,<STDIN>);
  if( not ( $r1 < $l2 or $r2 < $l1 ) ){
    my $v = &max($l1,$l2);
    print "$v\n";
  } else {
    print ( ( $l1 + $l2 ) . "\n" );
  }

}

exit(0);

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


There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.

The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.  The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.

In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.  Print the only integer -1, if the collision of particles doesn't happen.

NoteIn the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.


After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.Help guys determine the winner photo by the records of likes.

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the total likes to the published photoes.  The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000), where ai is the identifier of the photo which got the i-th like.

Print the identifier of the photo which won the elections.

NoteIn the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second). Thus, the winner is the photo with identifier 2, as it got:  more likes than the photo with id 3;  as many likes as the photo with id 1, but the photo with the identifier 2 got its second like earlier.

#!/usr/bin/perl

my $sum = <>;
my @like_hist = split " ", <>;
@like_hist = map { $like_hist[$_] } 0..$sum-1;

my $counts = {};
my $max = {id => $like_hist[0], count => 1 };

my $k = 0;
for (0..$sum-1) {
	my $id = $like_hist[$_];
	my $count = ++$counts->{$id};
	$max = {id => $id, count => $count } if ($count > $max->{count});
}

print $max->{id};


Polycarp has an array $$$a$$$ consisting of $$$n$$$ integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains $$$n-1$$$ elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.Formally:   If it is the first move, he chooses any element and deletes it;  If it is the second or any next move:   if the last deleted element was odd, Polycarp chooses any even element and deletes it;  if the last deleted element was even, Polycarp chooses any odd element and deletes it.   If after some move Polycarp cannot make a move, the game ends. Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.Help Polycarp find this value.

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2000$$$) — the number of elements of $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

Print one integer — the minimum possible sum of non-deleted elements of the array after end of the game.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	@_ = split ' ', <>;

	my @odd = grep $_ % 2, @_;
	my @even = grep { not $_ % 2 } @_;

	$debug and print "[@odd|@even]";

	my $min = ( sort { $a <=> $b } ~~ @odd, ~~ @even )[ 0 ];
	$debug and print "[$min]";

	@odd = sort { $a <=> $b } @odd;
	@even = sort { $a <=> $b } @even;

	for( 1 .. $min + 1 ){
		@odd and pop @odd;
		@even and pop @even;
		}

	print eval join '+', 0, 0, @odd, @even;
	}


A card pyramid of height $$$1$$$ is constructed by resting two cards against each other. For $$$h>1$$$, a card pyramid of height $$$h$$$ is constructed by placing a card pyramid of height $$$h-1$$$ onto a base. A base consists of $$$h$$$ pyramids of height $$$1$$$, and $$$h-1$$$ cards on top. For example, card pyramids of heights $$$1$$$, $$$2$$$, and $$$3$$$ look as follows:  You start with $$$n$$$ cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed?

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains a single integer $$$n$$$ ($$$1\le n\le 10^9$$$) — the number of cards. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^9$$$.

For each test case output a single integer — the number of pyramids you will have constructed in the end.

NoteIn the first test, you construct a pyramid of height $$$1$$$ with $$$2$$$ cards. There is $$$1$$$ card remaining, which is not enough to build a pyramid.In the second test, you build two pyramids, each of height $$$2$$$, with no cards remaining.In the third test, you build one pyramid of height $$$3$$$, with no cards remaining.In the fourth test, you build one pyramid of height $$$3$$$ with $$$9$$$ cards remaining. Then you build a pyramid of height $$$2$$$ with $$$2$$$ cards remaining. Then you build a final pyramid of height $$$1$$$ with no cards remaining.In the fifth test, one card is not enough to build any pyramids.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

my $cnt = 1;
my $sum = 0;
my @sums;

while( $sum < 1e9 ){
	$sum += $cnt * 2 + $cnt - 1;
	push @sums, $sum;
	$cnt ++;
	}

@sums = reverse @sums;

$debug and print "@sums";

$debug and print ~~ @sums;

<>;

while(<>){
	chomp;

	my $cnt = 0;
	my $i = 0;

	while( $i < @sums ){
		if( defined $sums[ $i + 30 ] and $sums[ $i + 30 ] > $_ ){
			$i += 30;
			next;
			}
		if( $sums[ $i ] <= $_ ){
			$_ -= $sums[ $i ];
			$cnt ++;
			redo;
			}
		$i ++;
		}

	print $cnt;
	}


A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition — when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation. Thus, your cup is a cylinder with diameter equals d centimeters. Initial level of water in cup equals h centimeters from the bottom.   You drink a water with a speed equals v milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on e centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously. Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds.Note one milliliter equals to one cubic centimeter.

The only line of the input contains four integer numbers d, h, v, e (1 ≤ d, h, v, e ≤ 104), where:   d — the diameter of your cylindrical cup,  h — the initial level of water in the cup,  v — the speed of drinking process from the cup in milliliters per second,  e — the growth of water because of rain if you do not drink from the cup.

If it is impossible to make the cup empty, print "NO" (without quotes). Otherwise print "YES" (without quotes) in the first line. In the second line print a real number — time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. It is guaranteed that if the answer exists, it doesn't exceed 104.

NoteIn the first example the water fills the cup faster than you can drink from it.In the second example area of the cup's bottom equals to , thus we can conclude that you decrease the level of water by  centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be empty in  seconds.

use strict;
use warnings;

my $pi = 3.1415926535897;

sub solve {
	my ($d, $h, $v, $e) = split(" ", shift);
	$e = $e * $pi * ($d ** 2) / 4;
	if ($v <= $e) {
		return "NO";
	}
	return "YES\n" . ($h * $pi * ($d ** 2) / 4) / ($v - $e);
};

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

use strict;
use warnings;

my $pi = 3.1415926535897;

sub solve {
	my ($d, $h, $v, $e) = split(" ", shift);
	$e = $e * $pi * ($d ** 2) / 4;
	if ($v <= $e) {
		return "NO";
	}
	return ($h * $pi * ($d ** 2) / 4) / ($v - $e);
};

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


Bob watches TV every day. He always sets the volume of his TV to $$$b$$$. However, today he is angry to find out someone has changed the volume to $$$a$$$. Of course, Bob has a remote control that can change the volume.There are six buttons ($$$-5, -2, -1, +1, +2, +5$$$) on the control, which in one press can either increase or decrease the current volume by $$$1$$$, $$$2$$$, or $$$5$$$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $$$0$$$.As Bob is so angry, he wants to change the volume to $$$b$$$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $$$a$$$ and $$$b$$$, finds the minimum number of presses to change the TV volume from $$$a$$$ to $$$b$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \le T \le 1\,000$$$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $$$a$$$ and $$$b$$$ ($$$0 \le a, b \le 10^{9}$$$) — the current volume and Bob's desired volume, respectively.

For each test case, output a single integer — the minimum number of presses to change the TV volume from $$$a$$$ to $$$b$$$. If Bob does not need to change the volume (i.e. $$$a=b$$$), then print $$$0$$$.

NoteIn the first example, Bob can press the $$$-2$$$ button twice to reach $$$0$$$. Note that Bob can not press $$$-5$$$ when the volume is $$$4$$$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $$$+5$$$ twice, then press $$$-1$$$ once.In the last example, Bob can press the $$$+5$$$ once, then press $$$+1$$$.


A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows.Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter tij units of time.Order is important everywhere, so the painters' work is ordered by the following rules:  Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n);  each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on;  each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest;  as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter. Given that the painters start working at time 0, find for each picture the time when it is ready for sale.

The first line of the input contains integers m, n (1 ≤ m ≤ 50000, 1 ≤ n ≤ 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers ti1, ti2, ..., tin (1 ≤ tij ≤ 1000), where tij is the time the j-th painter needs to work on the i-th picture.

Print the sequence of m integers r1, r2, ..., rm, where ri is the moment when the n-th painter stopped working on the i-th picture.


Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.

The first line contains integer n (1 ≤ n ≤ 1000) — the number of gears. The second line contains n digits a1, a2, ..., an (0 ≤ ai ≤ n - 1) — the sequence of active teeth: the active tooth of the i-th gear contains number ai.

In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.

NoteIn the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.


Andre has very specific tastes. Recently he started falling in love with arrays.Andre calls an nonempty array $$$b$$$ good, if sum of its elements is divisible by the length of this array. For example, array $$$[2, 3, 1]$$$ is good, as sum of its elements — $$$6$$$ — is divisible by $$$3$$$, but array $$$[1, 1, 2, 3]$$$ isn't good, as $$$7$$$ isn't divisible by $$$4$$$. Andre calls an array $$$a$$$ of length $$$n$$$ perfect if the following conditions hold:   Every nonempty subarray of this array is good.  For every $$$i$$$ ($$$1 \le i \le n$$$), $$$1 \leq a_i \leq 100$$$. Given a positive integer $$$n$$$, output any perfect array of length $$$n$$$. We can show that for the given constraints such an array always exists.An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

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 perfect array of length $$$n$$$ on a separate line.

NoteArray $$$[19, 33]$$$ is perfect as all $$$3$$$ its subarrays: $$$[19]$$$, $$$[33]$$$, $$$[19, 33]$$$, have sums divisible by their lengths, and therefore are good.


A sequence $$$a_1, a_2, \dots, a_n$$$ is called good if, for each element $$$a_i$$$, there exists an element $$$a_j$$$ ($$$i \ne j$$$) such that $$$a_i+a_j$$$ is a power of two (that is, $$$2^d$$$ for some non-negative integer $$$d$$$).For example, the following sequences are good:  $$$[5, 3, 11]$$$ (for example, for $$$a_1=5$$$ we can choose $$$a_2=3$$$. Note that their sum is a power of two. Similarly, such an element can be found for $$$a_2$$$ and $$$a_3$$$),  $$$[1, 1, 1, 1023]$$$,  $$$[7, 39, 89, 25, 89]$$$,  $$$[]$$$. Note that, by definition, an empty sequence (with a length of $$$0$$$) is good.For example, the following sequences are not good:  $$$[16]$$$ (for $$$a_1=16$$$, it is impossible to find another element $$$a_j$$$ such that their sum is a power of two),  $$$[4, 16]$$$ (for $$$a_1=4$$$, it is impossible to find another element $$$a_j$$$ such that their sum is a power of two),  $$$[1, 3, 2, 8, 8, 8]$$$ (for $$$a_3=2$$$, it is impossible to find another element $$$a_j$$$ such that their sum is a power of two). You are given a sequence $$$a_1, a_2, \dots, a_n$$$. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.

The first line contains the integer $$$n$$$ ($$$1 \le n \le 120000$$$) — the length of the given sequence. The second line contains the sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all $$$n$$$ elements, make it empty, and thus get a good sequence.

NoteIn the first example, it is enough to delete one element $$$a_4=5$$$. The remaining elements form the sequence $$$[4, 7, 1, 4, 9]$$$, which is good.

#!/usr/bin/env perl

use 5.012;

sub isPowerOfTwo
{
    my $num = shift;
    my $l = log($num)/log(2);
    $l == int $l;
}

chomp(my $n = <STDIN>);
chomp(my $line = <STDIN>);
my @nums = split ' ', $line;

my $i = 0;
my $deleted = 0;
my %pos;
my %numCount;
for my $i (0..$#nums) {
    ++$numCount{$nums[$i]};
    $pos{$nums[$i]} = $i;
}
my @powers = map 2 ** $_, 0..31;
NUM:for my $i (0..$#nums) {
    my $curNum = $nums[$i];
    for my $power (0..31) {
        my $t = $powers[$power];
        my $rest = $t - $curNum;
        my $count = $numCount{$rest};
        if ($count > 1 || ($count == 1 && $pos{$rest} != $i)) {
            next NUM;
        }
    }
    ++$deleted;
}

say $deleted;


On a chessboard with a width of $$$10^9$$$ and a height of $$$10^9$$$, the rows are numbered from bottom to top from $$$1$$$ to $$$10^9$$$, and the columns are numbered from left to right from $$$1$$$ to $$$10^9$$$. Therefore, for each cell of the chessboard you can assign the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number and $$$y$$$ is the row number.Every day there are fights between black and white pieces on this board. Today, the black ones won, but at what price? Only the rook survived, and it was driven into the lower left corner — a cell with coordinates $$$(1,1)$$$. But it is still happy, because the victory has been won and it's time to celebrate it! In order to do this, the rook needs to go home, namely — on the upper side of the field (that is, in any cell that is in the row with number $$$10^9$$$).Everything would have been fine, but the treacherous white figures put spells on some places of the field before the end of the game. There are two types of spells:   Vertical. Each of these is defined by one number $$$x$$$. Such spells create an infinite blocking line between the columns $$$x$$$ and $$$x+1$$$.  Horizontal. Each of these is defined by three numbers $$$x_1$$$, $$$x_2$$$, $$$y$$$. Such spells create a blocking segment that passes through the top side of the cells, which are in the row $$$y$$$ and in columns from $$$x_1$$$ to $$$x_2$$$ inclusive. The peculiarity of these spells is that it is impossible for a certain pair of such spells to have a common point. Note that horizontal spells can have common points with vertical spells.    An example of a chessboard. Let's recall that the rook is a chess piece that in one move can move to any point that is in the same row or column with its initial position. In our task, the rook can move from the cell $$$(r_0,c_0)$$$ into the cell $$$(r_1,c_1)$$$ only under the condition that $$$r_1 = r_0$$$ or $$$c_1 = c_0$$$ and there is no blocking lines or blocking segments between these cells (For better understanding, look at the samples).Fortunately, the rook can remove spells, but for this it has to put tremendous efforts, therefore, it wants to remove the minimum possible number of spells in such way, that after this it can return home. Find this number!

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n,m \le 10^5$$$) — the number of vertical and horizontal spells. Each of the following $$$n$$$ lines contains one integer $$$x$$$ ($$$1 \le x < 10^9$$$) — the description of the vertical spell. It will create a blocking line between the columns of $$$x$$$ and $$$x+1$$$. Each of the following $$$m$$$ lines contains three integers $$$x_1$$$, $$$x_2$$$ and $$$y$$$ ($$$1 \le x_{1} \le x_{2} \le 10^9$$$, $$$1 \le y < 10^9$$$) — the numbers that describe the horizontal spell. It will create a blocking segment that passes through the top sides of the cells that are in the row with the number $$$y$$$, in columns from $$$x_1$$$ to $$$x_2$$$ inclusive. It is guaranteed that all spells are different, as well as the fact that for each pair of horizontal spells it is true that the segments that describe them do not have common points.

In a single line print one integer — the minimum number of spells the rook needs to remove so it can get from the cell $$$(1,1)$$$ to at least one cell in the row with the number $$$10^9$$$

NoteIn the first sample, in order for the rook return home, it is enough to remove the second horizontal spell.  Illustration for the first sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the second horizontal spell. It also shows the path, on which the rook would be going home. In the second sample, in order for the rook to return home, it is enough to remove the only vertical spell. If we tried to remove just one of the horizontal spells, it would not allow the rook to get home, because it would be blocked from above by one of the remaining horizontal spells (either first one or second one), and to the right it would be blocked by a vertical spell.  Illustration for the second sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletion of the vertical spell. It also shows the path, on which the rook would be going home. In the third sample, we have two horizontal spells that go through the whole field. These spells can not be bypassed, so we need to remove both of them.  Illustration for the third sample. On the left it shows how the field looked at the beginning. On the right it shows how the field looked after the deletion of the horizontal spells. It also shows the path, on which the rook would be going home. In the fourth sample, we have no spells, which means that we do not need to remove anything.In the fifth example, we can remove the first vertical and third horizontal spells.  Illustration for the fifth sample. On the left it shows how the field looked at the beginning. On the right it shows how it looked after the deletions. It also shows the path, on which the rook would be going home.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	my( $v, $h ) = split;

	my @v = sort { $a <=> $b } map ~~<>, 1 .. $v;
	chomp @v;

	push @v, 1e9 + 0;

	no warnings;
	my @h = sort { $a <=> $b } map { ( split )[ 1 ] } grep $_ == 1, map ~~<>, 1 .. $h;
	use warnings;

	$debug and print " h: [@h]";
	$debug and print " v: [@v]";

	my $c_h = @h;

	my @ans;
	my $i = 0;

	for my $v ( @v ){

		my $cnt = 0;

		while( @h ){
			my $h = shift @h;
			if( $v <= $h ){
				unshift @h, $h;
				last;
				}
			$cnt ++;
			}

		push @ans, $c_h - $cnt + $i;

		$c_h -= $cnt;

		$i ++;
		}

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

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

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	my( $v, $h ) = split;

	my @v = sort { $a <=> $b } map ~~<>, 1 .. $v;
	chomp @v;

	push @v, 1e9 + 0;

	no warnings;
	my @h = sort { $a <=> $b } map { ( split )[ 1 ] } grep $_ == 1, map ~~<>, 1 .. $h;
	use warnings;

	$debug and print " h: [@h]";
	$debug and print " v: [@v]";

	my $c_h = @h;

	my @ans;
	my $i = 0;

	for my $v ( @v ){

		my $cnt = 0;

		while( @h ){
			my $h = shift @h;
			if( $v <= $h ){
				unshift @h, $h;
				last;
				}
			$cnt ++;
			}

		push @ans, $c_h - $cnt + $i;

		$i ++;
		}

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

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


Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?

The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.

Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.

NoteConsider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $min = 10e9;
	$min > $_ and $min = $_ for @_;

	my $f = 0;
	my $sum = 0;

	for (@_){
		( $_ - $min ) % $k and ++ $f and last;
		$sum += ( $_ - $min ) / $k;
		}

	print $f ? -1 : $sum;

	}


A sequence a0, a1, ... is called a recurrent binary sequence, if each term ai (i = 0, 1, ...) is equal to 0 or 1 and there exist coefficients  such that an = c1·an - 1 + c2·an - 2 + ... + ck·an - k (mod 2),  for all n ≥ k. Assume that not all of ci are zeros.Note that such a sequence can be uniquely recovered from any k-tuple {as, as + 1, ..., as + k - 1} and so it is periodic. Moreover, if a k-tuple contains only zeros, then the sequence contains only zeros, so this case is not very interesting. Otherwise the minimal period of the sequence is not greater than 2k - 1, as k-tuple determines next element, and there are 2k - 1 non-zero k-tuples. Let us call a sequence long if its minimal period is exactly 2k - 1. Your task is to find a long sequence for a given k, if there is any.

Input contains a single integer k (2 ≤ k ≤ 50).

If there is no long sequence for a given k, output "-1" (without quotes). Otherwise the first line of the output should contain k integer numbers: c1, c2, ..., ck (coefficients). The second line should contain first k elements of the sequence: a0, a1, ..., ak - 1. All of them (elements and coefficients) should be equal to 0 or 1, and at least one ci has to be equal to 1. If there are several solutions, output any.

Note1. In the first sample: c1 = 1, c2 = 1, so an = an - 1 + an - 2  (mod 2). Thus the sequence will be:so its period equals 3 = 22 - 1.2. In the second sample: c1 = 0, c2 = 1, c3 = 1, so an = an - 2 + an - 3  (mod 2). Thus our sequence is:and its period equals 7 = 23 - 1.Periods are colored.


One day Nikita found the string containing letters "a" and "b" only. Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?

The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".

Print a single integer — the maximum possible size of beautiful string Nikita can get.

NoteIt the first sample the string is already beautiful.In the second sample he needs to delete one of "b" to make it beautiful.

sub max {
	my $a = shift;
	my $b = (@_ == 1? shift: max(@_));
	$a > $b? $a: $b;
}

$_ = <>; chomp; @char = split //;
for $char (@char) {
	if ($char eq "a") {
			$aba = max($aba, $a, $ab) + 1;
			$a += 1;
	} else {
		$ab = max($a, $ab) + 1;
	}
}
print max($a, $ab, $aba);

#!/usr/bin/perl

use warnings;
use strict;

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

while(<>){
	chomp;
	$debug and print "[$_]";

	my $A = () = /a/g;

	my $from_L = 0;
	my $from_R = 0;
	my $max_L = 0;
	my $max_R = 0;

	while( /./g ){
		$from_L += 1 - 2 * ( $& eq 'a' );
		$from_L < 0 and $from_L = 0;
		$max_L < $from_L   and $max_L = $from_L;
		}

	my $rev = reverse $_;
	while( $rev =~ /./g ){
		$from_R += 1 - 2 * ( $& eq 'a' );
		$from_R < 0 and $from_R = 0;
		$max_R < $from_R and $max_R = $from_R;
		}

	$debug and print "max_L: $max_L";
	$debug and print "max_R: $max_R";

	print $A + ( $max_L > $max_R ? $max_L : $max_R );

	$debug and print '-' x 20;
	}

#!/usr/bin/perl

use warnings;
use strict;

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

while(<>){
	my $max_b = 0;

	/(b*)(??{ $max_b < length $1 and $max_b = length $1; 'X' })/;

	my $A = () = /a/g;

	print $max_b + $A;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my $max = -~0;

	/(a*b*)(?=(a*)(??{ 0 and print "$1.$2"; $max < length( $1.$2 ) and $max = length( $1.$2 ); "X"}))/;

	print $max;
	}

#!/usr/bin/perl

use warnings;
use strict;

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

while(<>){
	chomp;

	my $max_b = length( ( grep length, sort split /a+/ )[ 0 ] );

	my $A = () = /a/g;

	print $max_b + $A;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my $max = -~0;

	/(a*)(?=(b*a*)(??{ 0 and print "$1.$2"; $max < length( $1.$2 ) and $max = length( $1.$2 ); "X"}))/;

	print $max;
	}


This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:   Only segments already presented on the picture can be painted;  The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;  The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.

First line of the input contains two integers n and m(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively.  Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.

Print the maximum possible value of the hedgehog's beauty.

NoteThe picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.


«Polygon» is a system which allows to create programming tasks in a simple and professional way. When you add a test to the problem, the corresponding form asks you for the test index. As in most cases it is clear which index the next test will have, the system suggests the default value of the index. It is calculated as the smallest positive integer which is not used as an index for some previously added test.You are to implement this feature. Create a program which determines the default index of the next test, given the indexes of the previously added tests.

The first line contains one integer n (1 ≤ n ≤ 3000) — the amount of previously added tests. The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 3000) — indexes of these tests.

Output the required default value for the next test index.


You have a board represented as a grid with $$$2 \times n$$$ cells.The first $$$k_1$$$ cells on the first row and first $$$k_2$$$ cells on the second row are colored in white. All other cells are colored in black.You have $$$w$$$ white dominoes ($$$2 \times 1$$$ tiles, both cells are colored in white) and $$$b$$$ black dominoes ($$$2 \times 1$$$ tiles, both cells are colored in black).You can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, you can place a black domino if both cells are black and not occupied by any other domino.Can you place all $$$w + b$$$ dominoes on the board if you can place dominoes both horizontally and vertically?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 3000$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$ and $$$k_2$$$ ($$$1 \le n \le 1000$$$; $$$0 \le k_1, k_2 \le n$$$). The second line of each test case contains two integers $$$w$$$ and $$$b$$$ ($$$0 \le w, b \le n$$$).

For each test case, print YES if it's possible to place all $$$w + b$$$ dominoes on the board and NO, otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

NoteIn the first test case, $$$n = 1$$$, $$$k_1 = 0$$$ and $$$k_2 = 1$$$. It means that $$$2 \times 1$$$ board has black cell $$$(1, 1)$$$ and white cell $$$(2, 1)$$$. So, you can't place any white domino, since there is only one white cell.In the second test case, the board of the same size $$$2 \times 1$$$, but both cell are white. Since $$$w = 0$$$ and $$$b = 0$$$, so you can place $$$0 + 0 = 0$$$ dominoes on the board.In the third test case, board $$$2 \times 3$$$, but fully colored in black (since $$$k_1 = k_2 = 0$$$), so you can't place any white domino.In the fourth test case, cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, and $$$(2, 1)$$$ are white and other cells are black. You can place $$$2$$$ white dominoes at positions $$$((1, 1), (2, 1))$$$ and $$$((1, 2), (1, 3))$$$ and $$$2$$$ black dominoes at positions $$$((1, 4), (2, 4))$$$ $$$((2, 2), (2, 3))$$$.


Jafar has n cans of cola. Each can is described by two integers: remaining volume of cola ai and can's capacity bi (ai  ≤  bi).Jafar has decided to pour all remaining cola into just 2 cans, determine if he can do this or not!

The first line of the input contains one integer n (2 ≤ n ≤ 100 000) — number of cola cans. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — volume of remaining cola in cans. The third line contains n space-separated integers that b1, b2, ..., bn (ai ≤ bi ≤ 109) — capacities of the cans.

Print "YES" (without quotes) if it is possible to pour all remaining cola in 2 cans. Otherwise print "NO" (without quotes). You can print each letter in any case (upper or lower).

NoteIn the first sample, there are already 2 cans, so the answer is "YES".

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	print(
			( eval join ' + ', split ' ', <> ) >
			( eval join ' + ', map $_ + 0, ( sort { $b <=> $a } split ' ', <> )[ 0, 1 ] )
			?
				"NO" : "YES"
		);
	}


You are given two integers $$$n$$$ and $$$k$$$.You should create an array of $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ such that the sum $$$(a_1 + a_2 + \dots + a_n)$$$ is divisible by $$$k$$$ and maximum element in $$$a$$$ is minimum possible.What is the minimum possible maximum element in $$$a$$$?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^9$$$; $$$1 \le k \le 10^9$$$).

For each test case, print one integer — the minimum possible maximum element in array $$$a$$$ such that the sum $$$(a_1 + \dots + a_n)$$$ is divisible by $$$k$$$.

NoteIn the first test case $$$n = 1$$$, so the array consists of one element $$$a_1$$$ and if we make $$$a_1 = 5$$$ it will be divisible by $$$k = 5$$$ and the minimum possible.In the second test case, we can create array $$$a = [1, 2, 1, 2]$$$. The sum is divisible by $$$k = 3$$$ and the maximum is equal to $$$2$$$.In the third test case, we can create array $$$a = [1, 1, 1, 1, 1, 1, 1, 1]$$$. The sum is divisible by $$$k = 8$$$ and the maximum is equal to $$$1$$$.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	if( $n <= $k ){
		print 0 + ( int $k / $n ) + !!( $k % $n );
		}
	elsif( $n % $k == 0 ){
		print 1;
		}
	else{
		print 2;
		}
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $k ) = sort { $b <=> $a } split;

	print 0 + ( int $n / $k ) + !!( $n % $k );
	}


You are given a number $$$n$$$ and an array $$$b_1, b_2, \ldots, b_{n+2}$$$, obtained according to the following algorithm:   some array $$$a_1, a_2, \ldots, a_n$$$ was guessed;  array $$$a$$$ was written to array $$$b$$$, i.e. $$$b_i = a_i$$$ ($$$1 \le i \le n$$$);  The $$$(n+1)$$$-th element of the array $$$b$$$ is the sum of the numbers in the array $$$a$$$, i.e. $$$b_{n+1} = a_1+a_2+\ldots+a_n$$$;  The $$$(n+2)$$$-th element of the array $$$b$$$ was written some number $$$x$$$ ($$$1 \le x \le 10^9$$$), i.e. $$$b_{n+2} = x$$$; The  array $$$b$$$ was shuffled. For example, the array $$$b=[2, 3, 7, 12 ,2]$$$ it could be obtained in the following ways:   $$$a=[2, 2, 3]$$$ and $$$x=12$$$;  $$$a=[3, 2, 7]$$$ and $$$x=2$$$. For the given array $$$b$$$, find any array $$$a$$$ that could have been guessed initially.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$). The second row of each test case contains $$$n+2$$$ integers $$$b_1, b_2, \ldots, b_{n+2}$$$ ($$$1 \le b_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output:    "-1", if the array $$$b$$$ could not be obtained from any array $$$a$$$;  $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, otherwise.  If there are several arrays of $$$a$$$, you can output any.


There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c in pile i and number of pebbles of color c in pile j is at most one.In other words, let's say that bi, c is the number of pebbles of color c in the i-th pile. Then for any 1 ≤ c ≤ k, 1 ≤ i, j ≤ n the following condition must be satisfied |bi, c - bj, c| ≤ 1. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then bi, c is considered to be zero.

The first line of the input contains positive integers n and k (1 ≤ n, k ≤ 100), separated by a space — the number of piles and the number of colors respectively. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) denoting number of pebbles in each of the piles.

If there is no way to paint the pebbles satisfying the given condition, output "NO" (without quotes) . Otherwise in the first line output "YES" (without quotes). Then n lines should follow, the i-th of them should contain ai space-separated integers. j-th (1 ≤ j ≤ ai) of these integers should be equal to the color of the j-th pebble in the i-th pile. If there are several possible answers, you may output any of them.

#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'uninitialized';

my $line = <>;
chomp $line;
my ($N, $K) = split(/ +/, $line);

$line = <>;
chomp $line;
my @A = split(/ +/, $line);

my @D;
for (my $i = 0; $i < $N; ++$i) {
   push(@D, []);
}

my ($level, $col) = (0, 1);
for (my $t = 1; $col <= $K; ++$t) {
   my $new_col = 0;
   my $none_left = 1;
   for (my $i = 0; $i < $N; ++$i) {
      if ($A[$i] > 0) {
         $A[$i]--;
         push( @{$D[$i]}, $col);
         $none_left = 0 if ($A[$i] > 0);
      }
      else {
         $new_col = 1;
      #  printf "t = %d  i = %d\n", $t, $i;
      }
   }
   last if $none_left;
   $col++ if $new_col;
}

#print $col, "\n";
if ($col > $K) {
   print "NO\n"
}
else {
   print "YES\n";
   for (my $i = 0; $i < $N; ++$i) {
      for (my $j = 0; $j < @{$D[$i]}; ++$j) {
         print ' ' if $j > 0;
         print $D[$i][$j];
      }
      print "\n"
   }
}

#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'uninitialized';

my $line = <>;
chomp $line;
my ($N, $K) = split(/ +/, $line);

$line = <>;
chomp $line;
my @A = split(/ +/, $line);

my @F = (1) x $N;
my @D;
for (my $i = 0; $i < $N; ++$i) {
   push(@D, []);
}

my ($level, $col) = (0, 1);
for (my $t = 1; $col <= $K; ++$t) {
   my $new_col = 0;
   my $none_left = 1;
   for (my $i = 0; $i < $N; ++$i) {
      if ($A[$i] > 0) {
         $A[$i]--;
         push( @{$D[$i]}, $col);
         $none_left = 0;
      }
      else {
         if ($F[$i]) {
            $F[$i] = 0;
            $new_col = 1;
         #  printf "t = %d  i = %d\n", $t, $i;
         }
      }
   }
   last if $none_left;
   $col++ if $new_col;
}

#print $col, "\n";
if ($col > $K) {
   print "NO\n"
}
else {
   print "YES\n";
   for (my $i = 0; $i < $N; ++$i) {
      for (my $j = 0; $j < @{$D[$i]}; ++$j) {
         print ' ' if $j > 0;
         print $D[$i][$j];
      }
      print "\n"
   }
}

#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'uninitialized';

my $line = <>;
chomp $line;
my ($N, $K) = split(/ +/, $line);

$line = <>;
chomp $line;
my @A = split(/ +/, $line);

my @F = (1) x $N;
my @D;
for (my $i = 0; $i < $N; ++$i) {
   push(@D, []);
}

my ($level, $col) = (0, 1);
for (my $t = 1; $col <= $K; ++$t) {
   my $new_col = 0;
   my $none_left = 1;
   for (my $i = 0; $i < $N; ++$i) {
      if ($A[$i] > 0) {
         $A[$i]--;
         push( @{$D[$i]}, $col);
         $none_left = 0 if $A[$i] > 0;
      }
      else {
         if ($F[$i]) {
            $F[$i] = 0;
            $new_col = 1;
         #  printf "t = %d  i = %d\n", $t, $i;
         }
      }
   }
   last if $none_left;
   $col++ if $new_col;
}

#print $col, "\n";
if ($col > $K) {
   print "NO\n"
}
else {
   print "YES\n";
   for (my $i = 0; $i < $N; ++$i) {
      for (my $j = 0; $j < @{$D[$i]}; ++$j) {
         print ' ' if $j > 0;
         print $D[$i][$j];
      }
      print "\n"
   }
}


You are given a string $$$s=s_1s_2\dots s_n$$$ of length $$$n$$$, which only contains digits $$$1$$$, $$$2$$$, ..., $$$9$$$.A substring $$$s[l \dots r]$$$ of $$$s$$$ is a string $$$s_l s_{l + 1} s_{l + 2} \ldots s_r$$$. A substring $$$s[l \dots r]$$$ of $$$s$$$ is called even if the number represented by it is even. Find the number of even substrings of $$$s$$$. Note, that even if some substrings are equal as strings, but have different $$$l$$$ and $$$r$$$, they are counted as different substrings.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 65000$$$) — the length of the string $$$s$$$. The second line contains a string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists only of digits $$$1$$$, $$$2$$$, ..., $$$9$$$.

Print the number of even substrings of $$$s$$$.

NoteIn the first example, the $$$[l, r]$$$ pairs corresponding to even substrings are:   $$$s[1 \dots 2]$$$ $$$s[2 \dots 2]$$$ $$$s[1 \dots 4]$$$ $$$s[2 \dots 4]$$$ $$$s[3 \dots 4]$$$ $$$s[4 \dots 4]$$$ In the second example, all $$$10$$$ substrings of $$$s$$$ are even substrings. Note, that while substrings $$$s[1 \dots 1]$$$ and $$$s[2 \dots 2]$$$ both define the substring "2", they are still counted as different substrings.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my $sum = 0;

	/
		[02468]
		(?{ $sum += pos })
		(*F)
	/x;

	print $sum;
	}


You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position $$$3$$$ and ends in position $$$6$$$), but "aa" or "d" aren't substrings of this string. So the substring of the string $$$s$$$ from position $$$l$$$ to position $$$r$$$ is $$$s[l; r] = s_l s_{l + 1} \dots s_r$$$.You have to choose exactly one of the substrings of the given string and reverse it (i. e. make $$$s[l; r] = s_r s_{r - 1} \dots s_l$$$) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.If it is impossible to reverse some substring of the given string to obtain a string that is less, print "NO". Otherwise print "YES" and any suitable substring.String $$$x$$$ is lexicographically less than string $$$y$$$, if either $$$x$$$ is a prefix of $$$y$$$ (and $$$x \ne y$$$), or there exists such $$$i$$$ ($$$1 \le i \le min(|x|, |y|)$$$), that $$$x_i < y_i$$$, and for any $$$j$$$ ($$$1 \le j < i$$$) $$$x_j = y_j$$$. Here $$$|a|$$$ denotes the length of the string $$$a$$$. The lexicographic comparison of strings is implemented by operator < in modern programming languages.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the length of $$$s$$$. The second line of the input contains the string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters.

If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print "NO". Otherwise print "YES" and two indices $$$l$$$ and $$$r$$$ ($$$1 \le l < r \le n$$$) denoting the substring you have to reverse. If there are multiple answers, you can print any.

NoteIn the first testcase the resulting string is "aacabba".


The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building.The slogan of the company consists of n characters, so the decorators hung a large banner, n meters wide and 1 meter high, divided into n equal squares. The first character of the slogan must be in the first square (the leftmost) of the poster, the second character must be in the second square, and so on.Of course, the R1 programmers want to write the slogan on the poster themselves. To do this, they have a large (and a very heavy) ladder which was put exactly opposite the k-th square of the poster. To draw the i-th character of the slogan on the poster, you need to climb the ladder, standing in front of the i-th square of the poster. This action (along with climbing up and down the ladder) takes one hour for a painter. The painter is not allowed to draw characters in the adjacent squares when the ladder is in front of the i-th square because the uncomfortable position of the ladder may make the characters untidy. Besides, the programmers can move the ladder. In one hour, they can move the ladder either a meter to the right or a meter to the left.Drawing characters and moving the ladder is very tiring, so the programmers want to finish the job in as little time as possible. Develop for them an optimal poster painting plan!

The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 100) — the number of characters in the slogan and the initial position of the ladder, correspondingly. The next line contains the slogan as n characters written without spaces. Each character of the slogan is either a large English letter, or digit, or one of the characters: '.', '!', ',', '?'.

In t lines, print the actions the programmers need to make. In the i-th line print:   "LEFT" (without the quotes), if the i-th action was "move the ladder to the left";  "RIGHT" (without the quotes), if the i-th action was "move the ladder to the right";  "PRINT x" (without the quotes), if the i-th action was to "go up the ladder, paint character x, go down the ladder".  The painting time (variable t) must be minimum possible. If there are multiple optimal painting plans, you can print any of them.

RIGHT
RIGHT
PRINT !
LEFT
PRINT O
LEFT
PRINT G
LEFT
PRINT ?
LEFT
PRINT O
LEFT
PRINT G

NoteNote that the ladder cannot be shifted by less than one meter. The ladder can only stand in front of some square of the poster. For example, you cannot shift a ladder by half a meter and position it between two squares. Then go up and paint the first character and the second character.

use strict;
use POSIX;
my ($n,$k)=split(' ',<>);
$_=<>;
chomp;
my @l = split('',$_);
$\ = "\n";
unshift (@l,"lal");
my $adfs = 1 if $k == 1;

if($k >= floor($#l/2)+1)
{
    while($k<=$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
                print "RIGHT" if  $k++ <$#l;
    }
    $k--;

    goto exit if $adfs;

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;

        print "LEFT" if $k-- >1;
    }
}
else
{

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "LEFT" if $k-- >1;
    }
    $k++;
    while($k<=$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "RIGHT" if  $k++ <$#l;

    }
}
exit

while(<>){
	chomp;
	($n,$m)=split/ /;
	$_=<>;
	chomp;

	if ($n<$m*2){
		for $i(1..($n-$m)){
			print "RIGHT\n"
			}
		$_=reverse $_;
		s/.// and print "PRINT $&\n";
		s/./print "LEFT\nPRINT $&\n"/ge;

		}
	else {
		for $i(1..($m-1)){
			print "LEFT\n"
			}
		s/.// and print "PRINT $&\n";
		s/./print "RIGHT\nPRINT $&\n"/ge;

		}

	}

use strict;
use POSIX;
my ($n,$k)=split(' ',<>);
$_=<>;
chomp;
my @l = split('',$_);
$\ = "\n";
unshift (@l,"lal");
my $adfs = 1 if $k == 1;

if($k>= floor($#l/2))
{
    do
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
                print "RIGHT" if  $k++ <$#l;
    }
    while ($k<=$#l);
    $k--;

    while ($k>0 and !$adfs)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;

        print "LEFT" if $k-- >1;
    }
}
else
{

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "LEFT" if $k-- >1;
    }

    do{
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "RIGHT" if  $k++ <$#l;

    }while($k<$#l);
}
exit

use strict;
my ($n,$k)=split(' ',<>);
$_=<>;
chomp;
my @l = split('',$_);
$\ = "\n";
unshift (@l,"lal");
if($k>= ($#l/2))
{
    while($k<$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
                print "RIGHT" if  $k++ <$#l;
    }

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;

        print "LEFT" if $k-- >1;
    }
}
else
{

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "LEFT" if $k-- >1;
    }
    while($k<=$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "RIGHT" if  $k++ <$#l;

    }
}

use strict;
use POSIX;
my ($n,$k)=split(' ',<>);
$_=<>;
chomp;
my @l = split('',$_);
$\ = "\n";
unshift (@l,"lal");
my $adfs = 1 if $k == 1;

if($k>= floor($#l/2))
{
    while($k<=$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
                print "RIGHT" if  $k++ <$#l;
    }
    $k--;

    goto exit if $adfs;

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;

        print "LEFT" if $k-- >1;
    }
}
else
{

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "LEFT" if $k-- >1;
    }
    while($k<$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "RIGHT" if  $k++ <$#l;

    }
}
exit

use strict;
use POSIX;
my ($n,$k)=split(' ',<>);
$_=<>;
chomp;
my @l = split('',$_);
$\ = "\n";
unshift (@l,"lal");
my $adfs = 1 if $k == 1;

if($k>= floor($#l/2))
{
    while($k<=$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
                print "RIGHT" if  $k++ <$#l;
    }
    $k--;

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;

        print "LEFT" if $k-- >1;
    }
}
else
{

    while ($k>0)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "LEFT" if $k-- >1;
    }
    while($k<$#l)
    {
        print "PRINT $l[$k]" if defined $l[$k];
        $l[$k] = undef;
        print "RIGHT" if  $k++ <$#l;

    }
}


Businessman Divan loves chocolate! Today he came to a store to buy some chocolate. Like all businessmen, Divan knows the value of money, so he will not buy too expensive chocolate. At the same time, too cheap chocolate tastes bad, so he will not buy it as well.The store he came to has $$$n$$$ different chocolate bars, and the price of the $$$i$$$-th chocolate bar is $$$a_i$$$ dollars. Divan considers a chocolate bar too expensive if it costs strictly more than $$$r$$$ dollars. Similarly, he considers a bar of chocolate to be too cheap if it costs strictly less than $$$l$$$ dollars. Divan will not buy too cheap or too expensive bars.Divan is not going to spend all his money on chocolate bars, so he will spend at most $$$k$$$ dollars on chocolates.Please determine the maximum number of chocolate bars Divan can buy.

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 description of each test case consists of two lines. The first line contains integers $$$n$$$, $$$l$$$, $$$r$$$, $$$k$$$ ($$$1 \le n \le 100$$$, $$$1 \le l \le r \le 10^9$$$, $$$1 \le k \le 10^9$$$) — the lowest acceptable price of a chocolate, the highest acceptable price of a chocolate and Divan's total budget, respectively. The second line contains a sequence $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) integers — the prices of chocolate bars in the store.

For each test case print a single integer — the maximum number of chocolate bars Divan can buy.

NoteIn the first example Divan can buy chocolate bars $$$1$$$ and $$$3$$$ and spend $$$100$$$ dollars on them.In the second example Divan can buy chocolate bars $$$3$$$ and $$$4$$$ and spend $$$7$$$ dollars on them.In the third example Divan can buy chocolate bars $$$3$$$, $$$4$$$, and $$$5$$$ for $$$12$$$ dollars.In the fourth example Divan cannot buy any chocolate bar because each of them is either too cheap or too expensive.In the fifth example Divan cannot buy any chocolate bar because he considers the first bar too cheap, and has no budget for the second or third.In the sixth example Divan can buy all the chocolate bars in the shop.

for (1..<>) {
        chomp (my $in = <>);
        my ($n, $l, $r, $k) = split / /, $in;
        $minprice = $l;
        $maxprice = $r;
        $total_cash = $k;
        chomp (my $in = <>);
        my @a = split / /, $in;

        @a = sort {$a <=> $b} @a;
        $t = 0;

        foreach (@a) {
                $_ < $minprice and next;
                $_ > $maxprice and last;
                $t++;
                $k -= $_;
                $k <= 0 and last;
        }
        $k < 0 and $t--;
        print $t, "\n";
}


You have two positive integers $$$a$$$ and $$$b$$$.You can perform two kinds of operations:  $$$a = \lfloor \frac{a}{b} \rfloor$$$ (replace $$$a$$$ with the integer part of the division between $$$a$$$ and $$$b$$$)  $$$b=b+1$$$ (increase $$$b$$$ by $$$1$$$) Find the minimum number of operations required to make $$$a=0$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The only line of the description of each test case contains two integers $$$a$$$, $$$b$$$ ($$$1 \le a,b \le 10^9$$$).

For each test case, print a single integer: the minimum number of operations required to make $$$a=0$$$.

NoteIn the first test case, one of the optimal solutions is:  Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 4$$$ and $$$b = 2$$$.  Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 2$$$.  Increase $$$b$$$. After this operation $$$a = 2$$$ and $$$b = 3$$$.  Divide $$$a$$$ by $$$b$$$. After this operation $$$a = 0$$$ and $$$b = 3$$$.

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

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

while( $test_case -- > 0 ){
  my ($a,$b) = map { $_ - 0 } split(/\s+/,<STDIN>);
  if( $a < $b ){
    print "1\n";
    next;
  }
  my $mn = 10**9;
  my $ans = 0;
  if( $b == 1 ){
    $b++; $ans ++;
  }
  for(my $bb = $b; $bb <= $b+35; $bb++){
    my $cnt = $ans + ($bb-$b);
    my $t = 1;
    while( $bb ** $t <= $a ){
      $t++;
    }
    my $v = $cnt + $t;
    $mn = $v if $v < $mn;
  }
  print "$mn\n";

}

exit(0);

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my $ans = 0;

	$B == 1 and do { $B ++; $ans ++; };

	my @cand;

	for my $i ( 0 .. 10 ){
		my $B2 = $B + $i;
		my $A2 = $A;
		my $ans2 = $ans + $i;

		while( $A2 >= 1 ){
			$A2 /= $B2;
			$A2 = int $A2;
			$ans2 ++;
			}

		push @cand, $ans2;
		}

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