Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents.In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose the cubes are numbered from 1 to n in their order. Dima performs several steps, on step i he reverses the segment of cubes from i-th to (n - i + 1)-th. He does this while i ≤ n - i + 1.After performing the operations Dima went away, being very proud of himself. When Timofey returned to his cubes, he understood that their order was changed. Help Timofey as fast as you can and save the holiday — restore the initial order of the cubes using information of their current location.

The first line contains single integer n (1 ≤ n ≤ 2·105) — the number of cubes. The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109), where ai is the number written on the i-th cube after Dima has changed their order.

Print n integers, separated by spaces — the numbers written on the cubes in their initial order. It can be shown that the answer is unique.

NoteConsider the first sample.  At the begining row was [2, 3, 9, 6, 7, 1, 4].  After first operation row was [4, 1, 7, 6, 9, 3, 2].  After second operation row was [4, 3, 9, 6, 7, 1, 2].  After third operation row was [4, 3, 7, 6, 9, 1, 2].  At fourth operation we reverse just middle element, so nothing has changed. The final row is [4, 3, 7, 6, 9, 1, 2]. So the answer for this case is row [2, 3, 9, 6, 7, 1, 4].


Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has n episodes, numbered with integers from 1 to n.Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch?

The first line of the input contains integer n (2 ≤ n ≤ 100000) — the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to n. The second line contains n - 1 integer a1, a2, ..., an (1 ≤ ai ≤ n) — the numbers of episodes that Polycarpus has watched. All values of ai are distinct.

Print the number of the episode that Polycarpus hasn't watched.


Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described).This priority factor will be affected by three types of actions:   1. "X posted on Y's wall" (15 points),  2. "X commented on Y's post" (10 points),  3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X).You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you.

The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters.

Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently.

ahmed
3
ahmed posted on fatma's wall
fatma commented on ahmed's post
mona likes ahmed's post

#!/usr/bin/env perl

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

my $name = <>;
chomp $name;

my %action_pts = (
   'posted on'    => 15,
   'commented on' => 10,
   'likes'        => 5
);

my %H;
my $N = <>+0;
while ($N-- > 0) {
   my $s = <>;
   chomp $s;
   if ($s =~ m/^\s*([a-z]+)\s+(posted on|commented on|likes)\s+([a-z]+)/) {
      my $name1  = $1;
      my $action = $2;
      my $name2  = $3;
      if ($name1 eq $name) {
         $H{$name2} += $action_pts{$action};
      }
      elsif ($name2 eq $name) {
         $H{$name1} += $action_pts{$action};
      }
      else {
         $H{$name1} += 0;
         $H{$name2} += 0;
      }
   }
}

#while (my ($k, $v) = each(%H)) {
#   print $k, ' ', $v, "\n"
#}

my @A = sort { $H{ $a } != $H{ $b } ? ($H{ $b } <=> $H{ $a }) : ($a cmp $b) } keys %H;

foreach my $t (@A) {
   print $t, "\n"
}

#!/usr/bin/env perl

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

my $name = <>;
chomp $name;

my %action_pts = (
   'posted' => 15,
   'commented on' => 10,
   'likes' => 5
);

my %H;
my $N = <>+0;
while ($N-- > 0) {
   my $s = <>;
   chomp $s;
   if ($s =~ m/^(\w+) (posted on|commented on|likes) (\w+)/) {
      my $name1  = $1;
      my $name2  = $3;
      my $action = $2;
      if ($name1 eq $name) {
         $H{$name2} += $action_pts{$action};
      }
      elsif ($name2 eq $name) {
         $H{$name1} += $action_pts{$action};
      }
      else {
         $H{$name1} += 0;
         $H{$name2} += 0;
      }
   }
}

my @A = sort { $H{ $a } != $H{ $b } ? ($H{ $b } <=> $H{ $a }) : ($a cmp $b) } keys %H;

foreach my $t (@A) {
   print $t, "\n"
}

#!/usr/bin/env perl

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

my $name = <>;
chomp $name;

my %action_pts = (
   'posted' => 15,
   'commented on' => 10,
   'likes' => 5
);

my %H;
my $N = <>+0;
while ($N-- > 0) {
   my $s = <>;
   chomp $s;
   if ($s =~ m/^\s*([a-z]+)\s+(posted on|commented on|likes)\s+([a-z]+)/) {
      my $name1  = $1;
      my $name2  = $3;
      my $action = $2;
      if ($name1 eq $name) {
         $H{$name2} += $action_pts{$action};
      }
      elsif ($name2 eq $name) {
         $H{$name1} += $action_pts{$action};
      }
      else {
         $H{$name1} += 0;
         $H{$name2} += 0;
      }
   }
}

my @A = sort { $H{ $a } != $H{ $b } ? ($H{ $b } <=> $H{ $a }) : ($a cmp $b) } keys %H;

foreach my $t (@A) {
   print $t, "\n"
}


Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n × n matrix with a diamond inscribed into it.You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw.

The only line contains an integer n (3 ≤ n ≤ 101; n is odd).

***D***
**DDD**
*DDDDD*
DDDDDDD
*DDDDD*
**DDD**
***D***


Ivan is playing a strange game.He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:  Initially Ivan's score is 0;  In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped;  Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.

The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100). Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.

Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.

NoteIn the first example Ivan will replace the element a1, 2.

#!/usr/bin/perl

use warnings;
use strict;

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

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

	@_ = ();

	for ( map ~~<>, 1 .. $n ){
		my $i = 0;
		map { $_[ $i ++ ] .= $_ } split;
		}

	$debug and print for @_;
	@_ = grep m/1/, @_;
	my $ex = 0;
	my $lf = 0;

	for ( @_ ){
		my $_1 = 0;
		my $max = 0;
		my $max_id = -1;
		my @A = ();
		my $shift = 0;

		while( /(.)/g ){
			$_1 += $1;
			push @A, $1;
			if( @A > $k ){
				$_1 -= shift @A;
				$shift ++;
				}
			$debug and print "[@A]|$_1";
			$max < $_1 and $max_id = $shift;
			$max < $_1 and $max = $_1;
			}

		$debug and print $max;
		$debug and print $max_id;

		s/.{$max_id}//;
		$ex += () = $& =~ /1/g;
		s/.{1,$k}//;
		$lf += () = $& =~ /1/g;
		}

	print "$lf $ex";
	$debug and print '-' x 20;
	}


Vasya came up with a password to register for EatForces — a string $$$s$$$. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits.But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string $$$s$$$ is a string $$$x = s_l s_{l + 1} \dots s_{l + len - 1} (1 \le l \le |s|, 0 \le len \le |s| - l + 1)$$$. $$$len$$$ is the length of the substring. Note that the empty string is also considered a substring of $$$s$$$, it has the length $$$0$$$.Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length.Note that the length of $$$s$$$ should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits.

The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of testcases. Each of the next $$$T$$$ lines contains the initial password $$$s~(3 \le |s| \le 100)$$$, consisting of lowercase and uppercase Latin letters and digits. Only $$$T = 1$$$ is allowed for hacks.

For each testcase print a renewed password, which corresponds to given conditions.  The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is $$$0$$$. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" $$$\rightarrow$$$ "a7cdEf" is $$$4$$$, because the changed positions are $$$2$$$ and $$$5$$$, thus $$$(5 - 2) + 1 = 4$$$. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them.

NoteIn the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1.In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).


Given a permutation $$$p$$$ of length $$$n$$$, find its subsequence $$$s_1$$$, $$$s_2$$$, $$$\ldots$$$, $$$s_k$$$ of length at least $$$2$$$ such that:  $$$|s_1-s_2|+|s_2-s_3|+\ldots+|s_{k-1}-s_k|$$$ is as big as possible over all subsequences of $$$p$$$ with length at least $$$2$$$.  Among all such subsequences, choose the one whose length, $$$k$$$, is as small as possible. If multiple subsequences satisfy these conditions, you are allowed to find any of them.A sequence $$$a$$$ is a subsequence of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some (possibly, zero or all) elements.A permutation of length $$$n$$$ is an array of length $$$n$$$ in which every element from $$$1$$$ to $$$n$$$ occurs exactly once.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the length of the permutation $$$p$$$. The second line of each test case contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$\ldots$$$, $$$p_{n}$$$ ($$$1 \le p_i \le n$$$, $$$p_i$$$ are distinct) — the elements of the permutation $$$p$$$. The sum of $$$n$$$ across the test cases doesn't exceed $$$10^5$$$.

For each test case, the first line should contain the length of the found subsequence, $$$k$$$. The second line should contain $$$s_1$$$, $$$s_2$$$, $$$\ldots$$$, $$$s_k$$$ — its elements. If multiple subsequences satisfy these conditions, you are allowed to find any of them.

NoteIn the first test case, there are $$$4$$$ subsequences of length at least $$$2$$$:  $$$[3,2]$$$ which gives us $$$|3-2|=1$$$.  $$$[3,1]$$$ which gives us $$$|3-1|=2$$$.  $$$[2,1]$$$ which gives us $$$|2-1|=1$$$.  $$$[3,2,1]$$$ which gives us $$$|3-2|+|2-1|=2$$$. So the answer is either $$$[3,1]$$$ or $$$[3,2,1]$$$. Since we want the subsequence to be as short as possible, the answer is $$$[3,1]$$$.


Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing n characters (which is the size of one text message). Thus, whole sentences and words get split!Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space).The little walrus's text looks in the following manner: TEXT ::= SENTENCE | SENTENCE SPACE TEXTSENTENCE ::= WORD SPACE SENTENCE | WORD ENDEND ::= {'.', '?', '!'}WORD ::= LETTER | LETTER WORDLETTER ::= {'a'..'z', 'A'..'Z'}SPACE ::= ' 'SPACE stands for the symbol of a space.So, how many messages did Fangy send?

The first line contains an integer n, which is the size of one message (2 ≤ n ≤ 255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty.

On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes.

NoteLet's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".


The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.A message is a sequence of n integers a1, a2, ..., an. Encryption uses a key which is a sequence of m integers b1, b2, ..., bm (m ≤ n). All numbers from the message and from the key belong to the interval from 0 to c - 1, inclusive, and all the calculations are performed modulo c.Encryption is performed in n - m + 1 steps. On the first step we add to each number a1, a2, ..., am a corresponding number b1, b2, ..., bm. On the second step we add to each number a2, a3, ..., am + 1 (changed on the previous step) a corresponding number b1, b2, ..., bm. And so on: on step number i we add to each number ai, ai + 1, ..., ai + m - 1 a corresponding number b1, b2, ..., bm. The result of the encryption is the sequence a1, a2, ..., an after n - m + 1 steps.Help the Beaver to write a program that will encrypt messages in the described manner.

The first input line contains three integers n, m and c, separated by single spaces.  The second input line contains n integers ai (0 ≤ ai < c), separated by single spaces — the original message.  The third input line contains m integers bi (0 ≤ bi < c), separated by single spaces — the encryption key. The input limitations for getting 30 points are:    1 ≤ m ≤ n ≤ 103  1 ≤ c ≤ 103  The input limitations for getting 100 points are:    1 ≤ m ≤ n ≤ 105  1 ≤ c ≤ 103

Print n space-separated integers — the result of encrypting the original message.

NoteIn the first sample the encryption is performed in two steps: after the first step a = (0, 0, 0, 1) (remember that the calculations are performed modulo 2), after the second step a = (0, 1, 1, 0), and that is the answer.

#!/usr/bin/perl

# use strict;
# OMG! It works!

##main();
##
##sub main {
#    my ($n, $k) = split(/\D/, <>);
#    my @nums = split(/\D/, <>);
#    my %hash;
#    for my $i (0..$n-1) {
#        push @{$hash{$nums[$i]}}, $i;
#    }
#    for my $i (keys %hash) {
#        if (@{$hash{$i}} < $k) {
#            delete $hash{$i};
#        }
#    }
#    my %accepted_indexes;
#    for my $i (keys %hash) {
#        my @indexes = sort @{$hash{$i}};
#        for my $j (0..@indexes-$k) {
#            for my $p ($j+$k-1..@indexes-1) {
#                for my $f (0..$indexes[$j]) {
#                    for my $l ($indexes[$p]..@nums-1) {
#                        $accepted_indexes{$f}->{$l} = 1;
#                    }
#                }
#            }
#        }
#    }
#    my $amount = 0;
#    for my $i (keys %accepted_indexes) {
#        $amount += values $accepted_indexes{$i};
#        my @keys = keys $accepted_indexes{$i};
#    }
#    print $amount;
##}

my ($n, $k) = split(/\D/, <>);
my @a = split(/\D/, <>);
my %mp = ();
my $res = 0;
my $j = 0;
my $t = 0;
for my $i (0..$n-1) {
    if (defined $mp{$a[$i]}) {
        $mp{$a[$i]}++;
    } else {
        $mp{$a[$i]} = 1;
    }
    while ( ($j <= $i) && ($k <= $mp{$a[$i]}) ) {
        $mp{$a[$i]}--;
        $res += $n - $i;
        $j++;
    }
}
print $res;

__END__


You are given two strings a and b. You have to remove the minimum possible number of consecutive (standing one after another) characters from string b in such a way that it becomes a subsequence of string a. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from b and make it empty.Subsequence of string s is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string s.

The first line contains string a, and the second line — string b. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.

On the first line output a subsequence of string a, obtained from b by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output «-» (a minus sign).

NoteIn the first example strings a and b don't share any symbols, so the longest string that you can get is empty.In the second example ac is a subsequence of a, and at the same time you can obtain it by erasing consecutive symbols cepted from string b.


Andryusha is an orderly boy and likes to keep things in their place.Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe.Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time?

The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs. The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi. It is guaranteed that Andryusha took exactly two socks of each pair.

Print single integer — the maximum number of socks that were on the table at the same time.

NoteIn the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time.In the second example Andryusha behaved as follows:   Initially the table was empty, he took out a sock from pair 2 and put it on the table.  Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table.  Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe.  Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table.  Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe.  Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe.  Thus, at most two socks were on the table at the same time.

#!/usr/bin/perl
use strict;
use warnings;
chomp(my $n = <>);
my $ans = 0;
my $hashKeyCount;
my %h;
chomp(my $line = <>);
my @socks = split(/ /,$line);
foreach(@socks){
        if(defined $h{$_}){
                delete $h{$_};
        }else{
                $h{$_} = 1;
                $hashKeyCount = keys %h;
                $ans = $hashKeyCount > $ans ? $hashKeyCount : $ans;
        }
}
print "$ans\n";

#!/usr/bin/perl
use strict;
use warnings;
chomp(my $n = <>);
my $ans = 0;
my $tableCount = 0;
my %h;
chomp(my $line = <>);
my @socks = split(/ /,$line);
foreach(@socks){
	if(defined $h{$_}){
		if($tableCount > $ans){
			$ans = $tableCount;
			$tableCount -= 1;
		}
	}else{
		$h{$_} = 1;
		$tableCount++;
	}
}
print "$ans\n";


User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

NoteIn the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).   A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

use warnings; use strict;
my $n = scalar <> + 0;
my @aa = split ' ', scalar <>; $_ += 0 foreach @aa;
@aa == $n or die "err";
my @m = <>;
@m == $n or die "err";
foreach (@m){ chomp; $_ = [ split '', $_ ];  foreach (@$_) { $_ += 0; } @$_ == $n or die "err"; }
my @used = (0) x $n;
my @comp = ();
my @cc = ();
sub dfs{ my $i = shift;
    return if $used[$i];
    $used[$i] = 1; push @cc, $i;
    foreach (0 .. ($n-1)) {
        dfs($_) if $m[$i][$_] == 1 || $m[$_][$i] == 1;
        }
    }
sub numeric {$a <=> $b}
foreach (0 .. ($n-1)){
    dfs($_);
    #print join(' ', @cc), "\n";
    push @comp, [sort numeric @cc] if @cc;
    @cc = ();
    }
foreach (@comp){
    #print join(' ', @$), "\n";
    @aa[@$_] = sort numeric @aa[@$_];
    }
print join ' ', @aa;
print "\n";

use warnings; use strict;
my $n = scalar <> + 0;
my @a = split ' ', scalar <>; $_ += 0 foreach @a;
@a == $n or die "err";
my @m = <>;
@m == $n or die "err";
foreach (@m){ chomp; $_ = [ split '', $_ ];  foreach (@$_) { $_ += 0; } @$_ == $n or die "err"; }
my @used = (0) x $n;
my @comp = ();
my @cc = ();
sub dfs{ my $i = shift;
    return if $used[$i];
    $used[$i] = 1; push @cc, $i;
    foreach (0 .. ($n-1)) {
        dfs($_) if $m[$i][$_] || $m[$_][$i];
        }
    }
foreach (0 .. ($n-1)){
    dfs($_);
    push @comp, [sort @cc] if @cc;
    @cc = ();
    }
@a[@$_] = sort @a[@$_] foreach (@comp);
print join ' ', @a;
print "\n";

use warnings; use strict;
my $n = scalar <> + 0;
my @a = split ' ', scalar <>; $_ += 0 foreach @a;
@a == $n or die "err";
my @m = <>;
@m == $n or die "err";
foreach (@m){ chomp; $_ = [ split '', $_ ];  foreach (@$_) { $_ += 0; } }
my @used = (0) x $n;
my @comp = ();
my @cc = ();
sub dfs{ my $i = shift;
    return if $used[$i];
    $used[$i] = 1; push @cc, $i;
    foreach (0 .. ($n-1)) {
        dfs($_) if $m[$i][$_];
        }
    }
foreach (0 .. ($n-1)){
    dfs($_);
    push @comp, [sort @cc] if @cc;
    @cc = ();
    }
@a[@$_] = sort @a[@$_] foreach (@comp);
print join ' ', @a;
print "\n";

use warnings; use strict;
my $n = scalar <> + 0;
my @a = split ' ', scalar <>; $_ += 0 foreach @a;
@a == $n or die "err";
my @m = <>;
@m == $n or die "err";
foreach (@m){ chomp; $_ = [ split '', $_ ];  foreach (@$_) { $_ += 0; } @$_ == $n or die "err"; }
my @used = (0) x $n;
my @comp = ();
my @cc = ();
sub dfs{ my $i = shift;
    return if $used[$i];
    $used[$i] = 1; push @cc, $i;
    foreach (0 .. ($n-1)) {
        dfs($_) if $m[$i][$_] == 1 || $m[$_][$i] == 1;
        }
    }
foreach (0 .. ($n-1)){
    dfs($_);
    print join(' ', @cc), "\n";
    push @comp, [sort {$a <=> $b} @cc] if @cc;
    @cc = ();
    }
foreach (@comp){
    print join(' ', @$), "\n";
    @a[@$_] = sort @a[@$_];
    }
print join ' ', @a;
print "\n";

use warnings; use strict;
my $n = scalar <> + 0;
my @aa = split ' ', scalar <>; $_ += 0 foreach @aa;
@aa == $n or die "err";
my @m = <>;
@m == $n or die "err";
foreach (@m){ chomp; $_ = [ split '', $_ ];  foreach (@$_) { $_ += 0; } @$_ == $n or die "err"; }
my @used = (0) x $n;
my @comp = ();
my @cc = ();
sub dfs{ my $i = shift;
    return if $used[$i];
    $used[$i] = 1; push @cc, $i;
    foreach (0 .. ($n-1)) {
        dfs($_) if $m[$i][$_] == 1 || $m[$_][$i] == 1;
        }
    }
foreach (0 .. ($n-1)){
    dfs($_);
    #print join(' ', @cc), "\n";
    push @comp, [sort {$a <=> $b} @cc] if @cc;
    @cc = ();
    }
foreach (@comp){
    #print join(' ', @$), "\n";
    @aa[@$_] = sort @aa[@$_];
    }
print join ' ', @aa;
print "\n";

use warnings; use strict;
my $n = scalar <> + 0;
my @a = split ' ', scalar <>; $_ += 0 foreach @a;
my @m = <>; $#m = $n-1;
foreach (@m){ chomp; $_ = [ split '', $_ ];  foreach (@$_) { $_ += 0; } }
my @used = (0) x $n;
my @comp = ();
my @cc = ();
sub dfs{ my $i = shift;
    return if $used[$i];
    $used[$i] = 1; push @cc, $i;
    foreach (0 .. ($n-1)) {
        dfs($_) if $m[$i][$_] || $m[$i][$_];
        }
    }
foreach (0 .. ($n-1)){
    dfs($_);
    push @comp, [sort @cc] if @cc;
    @cc = ();
    }
@a[@$_] = sort @a[@$_] foreach (@comp);
print join ' ', @a;
print "\n";


Let $$$f(i)$$$ denote the minimum positive integer $$$x$$$ such that $$$x$$$ is not a divisor of $$$i$$$.Compute $$$\sum_{i=1}^n f(i)$$$ modulo $$$10^9+7$$$. In other words, compute $$$f(1)+f(2)+\dots+f(n)$$$ modulo $$$10^9+7$$$.

The first line contains a single integer $$$t$$$ ($$$1\leq t\leq 10^4$$$), the number of test cases. Then $$$t$$$ cases follow. The only line of each test case contains a single integer $$$n$$$ ($$$1\leq n\leq 10^{16}$$$).

For each test case, output a single integer $$$ans$$$, where $$$ans=\sum_{i=1}^n f(i)$$$ modulo $$$10^9+7$$$.

NoteIn the fourth test case $$$n=4$$$, so $$$ans=f(1)+f(2)+f(3)+f(4)$$$.  $$$1$$$ is a divisor of $$$1$$$ but $$$2$$$ isn't, so $$$2$$$ is the minimum positive integer that isn't a divisor of $$$1$$$. Thus, $$$f(1)=2$$$.  $$$1$$$ and $$$2$$$ are divisors of $$$2$$$ but $$$3$$$ isn't, so $$$3$$$ is the minimum positive integer that isn't a divisor of $$$2$$$. Thus, $$$f(2)=3$$$.  $$$1$$$ is a divisor of $$$3$$$ but $$$2$$$ isn't, so $$$2$$$ is the minimum positive integer that isn't a divisor of $$$3$$$. Thus, $$$f(3)=2$$$.  $$$1$$$ and $$$2$$$ are divisors of $$$4$$$ but $$$3$$$ isn't, so $$$3$$$ is the minimum positive integer that isn't a divisor of $$$4$$$. Thus, $$$f(4)=3$$$. Therefore, $$$ans=f(1)+f(2)+f(3)+f(4)=2+3+2+3=10$$$.


You and your friend are participating in a TV show "Run For Your Prize".At the start of the show n prizes are located on a straight line. i-th prize is located at position ai. Positions of all prizes are distinct. You start at position 1, your friend — at position 106 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order.You know that it takes exactly 1 second to move from position x to position x + 1 or x - 1, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend's speed) at all.Now you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend.What is the minimum number of seconds it will take to pick up all the prizes?

The first line contains one integer n (1 ≤ n ≤ 105) — the number of prizes. The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 106 - 1) — the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order.

Print one integer — the minimum number of seconds it will take to collect all prizes.

NoteIn the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8.In the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5.


A $$$\mathbf{0}$$$-indexed array $$$a$$$ of size $$$n$$$ is called good if for all valid indices $$$i$$$ ($$$0 \le i \le n-1$$$), $$$a_i + i$$$ is a perfect square$$$^\dagger$$$.Given an integer $$$n$$$. Find a permutation$$$^\ddagger$$$ $$$p$$$ of $$$[0,1,2,\ldots,n-1]$$$ that is good or determine that no such permutation exists.$$$^\dagger$$$ An integer $$$x$$$ is said to be a perfect square if there exists an integer $$$y$$$ such that $$$x = y^2$$$.$$$^\ddagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output $$$n$$$ distinct integers $$$p_0, p_1, \dots, p_{n-1}$$$ ($$$0 \le p_i \le n-1$$$) — the permutation $$$p$$$ — if the answer exists, and $$$-1$$$ otherwise.

#!/usr/bin/perl

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

my $debug = 0;

$\ = $/;

<>;

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

	chomp;

	$_ --;

	my @squares;

	my $i = 0;

	while( $i ** 2 < $_ ){
		push @squares, $i ** 2;
		$i ++;
		}

	push @squares, $i ** 2;

	$debug and print "$_:@squares";

	my @A = ( -1 ) x ( 1 + $_ );

	for my $i ( reverse 0 .. $_ ){
		for my $sq ( reverse @squares ){
			if( $A[ $sq - $i ] == -1 ){
				$A[ $sq - $i ] = $i;
				last;
				}
			}
		}

	$debug and print map "[$_]", join ',', @A;

	print "@A";
	}

#!/usr/bin/perl

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

my $debug = 0;

$\ = $/;

<>;

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

	chomp;

	my @squares;

	my $i = 0;

	while( $i ** 2 < $_ ){
		push @squares, $i ** 2;
		$i ++;
		}

	push @squares, $i ** 2;

	$debug and print "$_:@squares";

	my @A = ( -1 ) x ( 1 + $_ );

	for my $i ( reverse 0 .. $_ ){
		for my $sq ( reverse @squares ){
			if( $A[ $sq - $i ] == -1 ){
				$A[ $sq - $i ] = $i;
				last;
				}
			}
		}

	$debug and print map "[$_]", join ',', @A;

	print "@A";
	}


You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You should write a program which finds sum of the best subsequence.

NoteIn the first example sum of the second and the fourth elements is 3.

=item
797 B

You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You should write a program which finds sum of the best subsequence.
Input

The first line contains integer number n (1 ≤ n ≤ 105).

The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Output

Print sum of resulting subseqeuence.
Examples
Input

4
-2 2 -3 1

Output

3

Input

3
2 -5 -3

Output

-1
=cut

my $n=<>;
chomp $n;
my @vs=split(' ',<>);
# print "# $n : ",join('|',@vs),"\n";

my @ps;
my @ns;

my $spos=0;
for(my $i=0;$i<$n;++$i) {
  my $v=$vs[$i];
  if($v>0) {
    push @ps,$v;
    $spos+=$v;
  } else {
    push @ns,$v;
  }
}
if(($spos%2)==1) {
  print "$spos\n";
  exit();
}
# print "# spos=$spos : ps=@ps : ns=@ns\n";
@ops=sort {$a-$b} grep {$_%2==1} @ps;
@ons=sort {$a-$b} grep {$_%2==1} @ns;
# print "# ops=@ops : ons=@ons\n";

my $mp=scalar(@ops)>0 ? $ops[0] : 1e20;
my $mn=scalar(@ons)>0 ? -$ons[-1] : 1e20;

$m=($mp<$mn ? $mp : $mn);
# print "# mp=$mp mn=$mn m=$m\n";
$spos-=$m;
print "$spos\n";

=item
797 B

You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You should write a program which finds sum of the best subsequence.
Input

The first line contains integer number n (1 ≤ n ≤ 105).

The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Output

Print sum of resulting subseqeuence.
Examples
Input

4
-2 2 -3 1

Output

3

Input

3
2 -5 -3

Output

-1
=cut

my $n=<>;
chomp $n;
my @vs=split(' ',<>);
# print "# $n : ",join('|',@vs),"\n";

my @ps;
my @ns;

my $spos=0;
for(my $i=0;$i<$n;++$i) {
  my $v=$vs[$i];
  if($v>0) {
    push @ps,$v;
    $spos+=$v;
  } else {
    push @ns,$v;
  }
}
if($spos % 2 ==1) {
  print "$spos\n";
  exit();
}
# print "# spos=$spos : ps=@ps : ns=@ns\n";
@ops=sort {$a-$b} grep {$_%2==1} @ps;
@ons=sort {$a-$b} grep {$_%2==1} @ns;
# print "# ops=@ops : ons=@ons\n";

my $mp=scalar(@ops)>0 ? $ops[0] : 0;
my $mn=scalar(@ons)>0 ? -$ons[-1] : 0;

$m=((0<$mp and $mp<$mn) ? $mp : $mn);
# print "# mp=$mp mn=$mn m=$m\n";
$spos-=$m;
print "$spos\n";

=item
797 B

You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You should write a program which finds sum of the best subsequence.
Input

The first line contains integer number n (1 ≤ n ≤ 105).

The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Output

Print sum of resulting subseqeuence.
Examples
Input

4
-2 2 -3 1

Output

3

Input

3
2 -5 -3

Output

-1
=cut

my $n=<>;
chomp $n;
my @vs=split(' ',<>);
print "# $n : ",join('|',@vs),"\n";

my @ps;
my @ns;

my $spos=0;
for(my $i=0;$i<$n;++$i) {
  my $v=$vs[$i];
  if($v>0) {
    push @ps,$v;
    $spos+=$v;
  } else {
    push @ns,$v;
  }
}
if($spos % 2 ==1) {
  print "$spos\n";
  exit();
}
print "# spos=$spos : ps=@ps : ns=@ns\n";
@ops=sort {$a-$b} grep {$_%2==1} @ps;
@ons=sort {$a-$b} grep {$_%2==1} @ns;
print "# ops=@ops : ons=@ons\n";

my $mp=scalar(@ops)>0 ? $ops[0] : 0;
my $mn=scalar(@ons)>0 ? -$ons[-1] : 0;

$m=((0<$mp and $mp<$mn) ? $mp : $mn);
print "# mp=$mp mn=$mn m=$m\n";
$spos-=$m;
print "$spos\n";

=item
797 B

You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You should write a program which finds sum of the best subsequence.
Input

The first line contains integer number n (1 ≤ n ≤ 105).

The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Output

Print sum of resulting subseqeuence.
Examples
Input

4
-2 2 -3 1

Output

3

Input

3
2 -5 -3

Output

-1
=cut

my $n=<>;
chomp $n;
my @vs=split(' ',<>);
print "# $n : ",join('|',@vs),"\n";

my $m=-1e20;
for(my $i=0;$i<$n;++$i) {
  my $s=0;
  for(my $j=$i;$j<$n;++$j) {
    $s+=$vs[$j];
    if($s % 2 == 1 and $s>$m) {
      $m=$s;
    }
  }
}
print "$m\n";


You are given a permutation $$$a_1, a_2, \ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once.You can do the following operation any number of times (possibly, zero):  Choose any three indices $$$i, j, k$$$ ($$$1 \le i < j < k \le n$$$).  If $$$a_i > a_k$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$. Otherwise, swap $$$a_j$$$ and $$$a_k$$$. Determine whether you can make the array $$$a$$$ sorted in non-descending order.

Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 10$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1 \le a_i \le n$$$, $$$a_i \neq a_j$$$ if $$$i \neq j$$$) — the elements of the array $$$a$$$.

For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "YES", "yEs" and "yes" will be recognized as a positive response).

NoteIn the first test case, $$$[1,2,3]$$$ is already sorted in non-descending order.In the second test case, we can choose $$$i = 1,j = 2,k = 3$$$. Since $$$a_1 \le a_3$$$, swap $$$a_2$$$ and $$$a_3$$$, the array then becomes $$$[1,2,3]$$$, which is sorted in non-descending order.In the seventh test case, we can do the following operations successively:  Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 \le a_7$$$, swap $$$a_6$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,4,5,3]$$$.  Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 > a_7$$$, replace $$$a_5$$$ with $$$a_5+a_6=9$$$, the array then becomes $$$[1,2,6,7,9,5,3]$$$.  Choose $$$i = 2,j = 5,k = 7$$$. Since $$$a_2 \le a_7$$$, swap $$$a_5$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,3,5,9]$$$.  Choose $$$i = 2,j = 4,k = 6$$$. Since $$$a_2 \le a_6$$$, swap $$$a_4$$$ and $$$a_6$$$, the array then becomes $$$[1,2,6,5,3,7,9]$$$.  Choose $$$i = 1,j = 3,k = 5$$$. Since $$$a_1 \le a_5$$$, swap $$$a_3$$$ and $$$a_5$$$, the array then becomes $$$[1,2,3,5,6,7,9]$$$, which is sorted in non-descending order. In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	print 1 - <> ? 'NO' : 'YES';
	}


Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...$$$n$$$ friends live in a city which can be represented as a number line. The $$$i$$$-th friend lives in a house with an integer coordinate $$$x_i$$$. The $$$i$$$-th friend can come celebrate the New Year to the house with coordinate $$$x_i-1$$$, $$$x_i+1$$$ or stay at $$$x_i$$$. Each friend is allowed to move no more than once.For all friends $$$1 \le x_i \le n$$$ holds, however, they can come to houses with coordinates $$$0$$$ and $$$n+1$$$ (if their houses are at $$$1$$$ or $$$n$$$, respectively).For example, let the initial positions be $$$x = [1, 2, 4, 4]$$$. The final ones then can be $$$[1, 3, 3, 4]$$$, $$$[0, 2, 3, 3]$$$, $$$[2, 2, 5, 5]$$$, $$$[2, 1, 3, 5]$$$ and so on. The number of occupied houses is the number of distinct positions among the final ones.So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of friends. The second line contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le n$$$) — the coordinates of the houses of the friends.

Print two integers — the minimum and the maximum possible number of occupied houses after all moves are performed.

NoteIn the first example friends can go to $$$[2, 2, 3, 3]$$$. So friend $$$1$$$ goes to $$$x_1+1$$$, friend $$$2$$$ stays at his house $$$x_2$$$, friend $$$3$$$ goes to $$$x_3-1$$$ and friend $$$4$$$ goes to $$$x_4-1$$$. $$$[1, 1, 3, 3]$$$, $$$[2, 2, 3, 3]$$$ or $$$[2, 2, 4, 4]$$$ are also all valid options to obtain $$$2$$$ occupied houses.For the maximum number of occupied houses friends can go to $$$[1, 2, 3, 4]$$$ or to $$$[0, 2, 4, 5]$$$, for example.