Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $$$k$$$ have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array $$$a$$$ of length $$$n$$$. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between $$$1$$$ and $$$n$$$ inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$). The second line of each test case contains $$$n$$$ space-separated integers ($$$1 \le a_i \le n$$$) — the array that Phoenix currently has. This array may or may not be already beautiful.

For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines. The first line should contain the length of the beautiful array $$$m$$$ ($$$n \le m \le 10^4$$$). You don't need to minimize $$$m$$$. The second line should contain $$$m$$$ space-separated integers ($$$1 \le b_i \le n$$$) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array $$$a$$$. You may print integers that weren't originally in array $$$a$$$. If there are multiple solutions, print any. It's guaranteed that if we can make array $$$a$$$ beautiful, we can always make it with resulting length no more than $$$10^4$$$.

NoteIn the first test case, we can make array $$$a$$$ beautiful by inserting the integer $$$1$$$ at index $$$3$$$ (in between the two existing $$$2$$$s). Now, all subarrays of length $$$k=2$$$ have the same sum $$$3$$$. There exists many other possible solutions, for example:   $$$2, 1, 2, 1, 2, 1$$$  $$$1, 2, 1, 2, 1, 2$$$ In the second test case, the array is already beautiful: all subarrays of length $$$k=3$$$ have the same sum $$$5$$$.In the third test case, it can be shown that we cannot insert numbers to make array $$$a$$$ beautiful.In the fourth test case, the array $$$b$$$ shown is beautiful and all subarrays of length $$$k=4$$$ have the same sum $$$10$$$. There exist other solutions also.

<?php
    define("MAXN", 1024) ;
    define("GAP_VAL", 1e-7) ;

    function gap($n)
    {
        return $n < GAP_VAL ;
    }

    $t = intval(trim(fgets(STDIN))) ;
    while($t --)
    {
        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $n = intval($in_str[0]) ;
        $k = intval($in_str[1]) ;

        $in_str = explode(" ", trim(fgets(STDIN))) ;

        $map_n = array_fill(0, $n + 1, 0) ;
        $cnt = 0 ;
        $footage = array() ;

        for($i = 0; $i < $n; $i ++)
        {
            $in_str[$i] = intval($in_str[$i]) ;

            if($map_n[$in_str[$i]] == 0)
            {
                $map_n[$in_str[$i]] = 1 ;
                $cnt ++ ;
                array_push($footage, $in_str[$i]) ;
            }
        }

        if($cnt > $k) echo "-1\n" ;
        else
        {
            if($cnt < $k)
            {
                for($i = 1; $i <= $n; $i ++)
                {
                    if($map_n[$i] == 0)
                    {
                        $cnt ++ ;
                        array_push($footage, $i) ;

                        if($cnt == $k) break ;
                    }
                }
            }

            $ans = array() ;
            $cycle = array_fill(0, $n + 1, 0) ;

            array_push($ans, $in_str[0]) ;
            $cycle[$in_str[0]] = 1 ;

            for($i = 1; $i < $n; $i ++)
            {
                if($cycle[$in_str[$i]] != 0)
                {
                    for($j = 0; $j < $k; $j ++)
                    {
                        if($cycle[$footage[$j]] == 0) array_push($ans, $footage[$j]) ;
                    }
                    break ;
                }
                else
                {
                    array_push($ans, $in_str[$i]) ;
                    $cycle[$in_str[$i]] = 1 ;
                }
            }

            $arr_cnt = count($ans) ;
            for(; $i < $n; $i ++)
            {
                $new_val = $ans[$arr_cnt - $k] ;

                while($new_val != $in_str[$i])
                {
                    array_push($ans, $new_val) ;

                    $arr_cnt ++ ;
                    $new_val = $ans[$arr_cnt - $k] ;
                }

                array_push($ans, $new_val) ;
                $arr_cnt ++ ;
            }
            echo $arr_cnt . "\n" ;
            echo implode(" ", $ans)."\n" ;
        }
    }
?>


A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem — a severe torcoder coordinator Ivan does not accept any Lesha's problem, calling each of them an offensive word "duped" (that is, duplicated). And one day they nearely quarrelled over yet another problem Ivan wouldn't accept.You are invited to act as a fair judge and determine whether the problem is indeed brand new, or Ivan is right and the problem bears some resemblance to those used in the previous SRMs.You are given the descriptions of Lesha's problem and each of Torcoder.com archive problems. The description of each problem is a sequence of words. Besides, it is guaranteed that Lesha's problem has no repeated words, while the description of an archive problem may contain any number of repeated words.The "similarity" between Lesha's problem and some archive problem can be found as follows. Among all permutations of words in Lesha's problem we choose the one that occurs in the archive problem as a subsequence. If there are multiple such permutations, we choose the one with the smallest number of inversions. Then the "similarity" of a problem can be written as , where n is the number of words in Lesha's problem and x is the number of inversions in the chosen permutation. Note that the "similarity" p is always a positive integer.The problem is called brand new if there is not a single problem in Ivan's archive which contains a permutation of words from Lesha's problem as a subsequence.Help the boys and determine whether the proposed problem is new, or specify the problem from the archive which resembles Lesha's problem the most, otherwise.

The first line contains a single integer n (1 ≤ n ≤ 15) — the number of words in Lesha's problem. The second line contains n space-separated words — the short description of the problem. The third line contains a single integer m (1 ≤ m ≤ 10) — the number of problems in the Torcoder.com archive. Next m lines contain the descriptions of the problems as "k s1 s2 ... sk", where k (1 ≤ k ≤ 500000) is the number of words in the problem and si is a word of the problem description. All words from all problem descriptions contain no more than 10 lowercase English letters. It is guaranteed that the total length of words in all problem descriptions does not exceed 500015.

If Lesha's problem is brand new, print string "Brand new problem!" (without quotes).  Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of characters [:, character | repeated p times, and characters :], where p is the "similarity" between this problem and Lesha's one. The archive problems are numbered starting from one in the order in which they are given in the input.

4
find the next palindrome
1
10 find the previous palindrome or print better luck next time

3
add two numbers
3
1 add
2 two two
3 numbers numbers numbers

4
these papers are formulas
3
6 what are these formulas and papers
5 papers are driving me crazy
4 crazy into the night

3
add two decimals
5
4 please two decimals add
5 decimals want to be added
4 two add decimals add
4 add one two three
7 one plus two plus three equals six

NoteLet us remind you that the number of inversions is the number of pairs of words that follow in the permutation not in their original order. Thus, for example, if the original problem is "add two numbers", then permutation "numbers add two" contains two inversions — pairs of words "numbers" and "add", "numbers" and "two". Sequence b1,  b2,  ...,  bk is a subsequence of sequence a1, a2,  ...,  an if there exists such a set of indices 1 ≤ i1 <  i2 < ...   < ik ≤ n that aij  =  bj (in other words, if sequence b can be obtained from a by deleting some of its elements).In the first test case the first problem contains the "find the palindrome next" permutation as a subsequence, in which the number of inversions equals 1 (words "palindrome" and "next").In the second test case there is no problem that contains a permutation of words from Lesha's problem as a subsequence.


Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Print a single integer — the minimum total time needed to turn off all the lights.

NoteIn the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.


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

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

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

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

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     list($b, $c) = explode(" ", trim(fgets(STDIN)));
     print $b * (strlen($c + 1) - 1) . "\n";
}
?>


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

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

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

<?php

list($emps, $chats, $events) = explode(" ", trim(fgets(STDIN)));

$cc = array();

// don't do anything yet...
for($i = 1; $i <= $emps; $i++)
{
	$cc[$i] = explode(" ", trim(fgets(STDIN)));
}

$ce = array();
$cn = array_fill(1, $chats, 0);
$en = array_fill(1, $emps, 0);
for($i = 0; $i < $events; $i++)
{
	list($se, $re) = explode(" ", trim(fgets(STDIN)));
	$en[$se]--;
	$cn[$re]++;
}

for($i = 1; $i <= $emps; $i++)
{
	for($j = 0; $j < $chats; $j++)
	{
		$en[$i] += ($cc[$i][$j] == '1') * $cn[($j + 1)];
	}
}

echo implode(" ", $en) . PHP_EOL;


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

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

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

NoteThe first example is explained in the statements.


Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and repeatedly presses the keys that need to be pressed only once. For example, instead of the phrase "how are you" he can type "hhoow aaaare yyoouu". Polycarp decided to automate the process of correcting such errors. He decided to write a plug-in to the text editor that will remove pairs of identical consecutive letters (if there are any in the text). Of course, this is not exactly what Polycarp needs, but he's got to start from something! Help Polycarp and write the main plug-in module. Your program should remove from a string all pairs of identical letters, which are consecutive. If after the removal there appear new pairs, the program should remove them as well. Technically, its work should be equivalent to the following: while the string contains a pair of consecutive identical letters, the pair should be deleted. Note that deleting of the consecutive identical letters can be done in any order, as any order leads to the same result.

The input data consists of a single line to be processed. The length of the line is from 1 to 2·105 characters inclusive. The string contains only lowercase Latin letters.

Print the given string after it is processed. It is guaranteed that the result will contain at least one character.

<?

$input = file_get_contents("php://stdin");
//$input = 'abacabaabacabaa';
//echo $input;
/*
$start_time = microtime();
$start_array = explode(" ",$start_time);
$time['start'] = $start_array[1] + $start_array[0];
*/
$len = strlen ($input);
$result = array();

if($len > 1){
    $prew = $input[0];
    for($i = 1; $i < $len; $i++){
        if($prew != $input[$i]){
            if(count($result) > 0 && $result[count($result) - 1] == $prew){
                array_pop($result);
                $prew = $input[$i];
            } else {
                $result[] = $prew;
                $prew = $input[$i];
            }
        } else {
            $i++;
            $prew = $input[$i];
        }
    }
    if($len == $i){
        $result[] = $prew;
    }
    echo implode("", $result);
} else {
     echo $input;
}
/*
$end_time = microtime();
$end_array = explode(" ",$end_time);
$time['gener_class'] = $end_array[1] + $end_array[0];
echo $time_gener['class'] = round($time['gener_class']-$time['start'],5);*/
?>

<?
$input = file_get_contents("php://stdin");

//echo testInput($input);

$len = strlen ($input);
$result = array();

$input = preg_split('//', $input, -1, PREG_SPLIT_NO_EMPTY);

if($len > 1){
    $prew = current($input);
    while ($char = next($input)) {
        if($prew != $char){
            if(count($result) > 0 && end($result) == $prew){
                array_pop($result);
                $prew = $char;
            } else {
                $result[] = $prew;
                $prew = $char;
            }
        } else {
            $prew = next($input);
        }
    }
    /*if($len == $i){
        $result[] = $prew;
    }*/
    echo implode($result);
} else {
     echo $input;
}
?>

<?
$handle = fopen ("php://stdin","r");
$input = fgets($handle);
$len = strlen ($input);

if($len > 1){
    $result = array();
    $prew = $input[0];
    for($i = 1; $i < $len; $i++){
        if($prew != $input[$i]){
            if(count($result) > 0 && end($result) == $prew){
                array_pop($result);
                $prew = $input[$i];
            } else {
                $result[] = $prew;
                $prew = $input[$i];
            }
        } else {
            $i++;
            $prew = $input[$i];
        }
    }
    if($len == $i){
        $result[] = $prew;
    }
    echo implode($result);
} else {
     echo $input;
}

?>

<?

$input = file_get_contents("php://stdin");

$len = strlen ($input);

if($len > 1){
    $result = array();
    $prew = $input[0];
    for($i = 1; $i < $len; $i++){
        if($prew != $input[$i]){
            if(count($result) > 0 && end($result) == $prew){
                array_pop($result);
                $prew = $input[$i];
            } else {
                $result[] = $prew;
                $prew = $input[$i];
            }
        } else {
            $i++;
            $prew = $input[$i];
        }
    }
    if($len == $i){
        $result[] = $prew;
    }
    echo implode($result);
} else {
     echo $input;
}

?>

<?
$input = file_get_contents("php://stdin");

//echo testInput($input);

$len = strlen ($input);
$result = array();
if($len > 1){
    $prew = $input[0];
    for($i = 1; $i < $len; $i++){
        if($prew != $input[$i]){
            if(count($result) > 0 && $result[count($result) - 1] == $prew){
                array_pop($result);
                $prew = $input[$i];
            } else {
                array_push($result, $prew);
                $prew = $input[$i];
            }
        } else {
            $i++;
            $prew = $input[$i];
        }
    }
    if($len == $i){
        array_push($result, $prew);
    }
    echo implode("", $result);
} else {
     echo $input;
}
?>

<?

$input = file_get_contents("php://stdin");

//echo testInput($input);

$len = strlen ($input);
$result = '';
if($len > 1){
    $prew = $input[0];
    for($i = 1; $i < $len; $i++){
        if($prew != $input[$i]){
            if(strlen($result) > 0 && $result[strlen($result) - 1] == $prew){
                $result = substr($result, 0, -1);
                $prew = $input[$i];
            } else {
                $result .= $prew;
                $prew = $input[$i];
            }
        } else {
            $i++;
            $prew = $input[$i];
        }
    }
    if($len == $i){
        $result .= $prew;
    }
    echo $result;
} else {
     echo $input;
}

?>

<?php

	$string = trim(file_get_contents('php://stdin'));
	$result = '';
	$strlen = 0;
	for ($i = 0; $i < strlen($string); $i++) {
		if (!$strlen || $string[$i] != $result[$strlen - 1]) {
			$result .= $string[$i];
			$strlen++;
		}
		else
			$result = substr($result, 0, --$strlen);
	}
	echo "$result\n";

<?php
$clearline=Array();
while (false !== ($c = fgetc(STDIN)))
       (end($clearline)==$c)?array_pop($clearline):array_push($clearline,$c);
echo implode("",$clearline);

?>

<?php
$a=Array();
while (false !== ($c = fgetc(STDIN))){
       if (end($a)==$c){
       array_pop($a);
       } else {
       array_push($a,$c);
       }
}
print (implode("",$a));

?>

<?php
fscanf(STDIN, "%s",$s);

$clearline=Array();
for($i=0;$i<strlen($s);$i++)
  (end($clearline)==$s[$i])?array_pop($clearline):array_push($clearline,$s[$i]);
echo implode("",$clearline);

?>

<?
$input = file_get_contents("php://stdin");

$len = strlen ($input);
$result = '';
$prew = '';
for($i = 0; $i < $len; $i++){
    if($input[$i] == '.'){
        if($prew){
            $result .= $prew . '...';
        } else {
            $result .= ' ...';
        }
        $i += 2;
        $prew = '';
    } else if($input[$i] == ','){
        if($prew == ', '){
            $result .= $prew;
        }
        $prew = ', ';
    } else if($input[$i] == ' '){
        if(!$prew){
            $prew = ' ';
        }
    } else {
       if($prew == ', ' || $result[strlen ($result) - 1] != '.'){
           $result .= $prew;
       }
       $result .= $input[$i];
       $prew = '';
    }
}
echo trim($result);
?>

<?php
$clearline=Array();
while (($c = fgets(STDIN, 1)) !== false)
       (end($clearline)==$c)?array_pop($clearline):array_push($clearline,$c);
implode("",$clearline);

?>


Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has n rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height).Boris chose m types of wallpaper to paper the walls of the rooms with (but it is not necessary to use all the types). Each type of wallpaper is sold in rolls of a fixed length and width (the length, naturally, shows how long the unfolded roll will be). In addition, for each type we know the price of one roll of this type.The wallpaper of each type contains strips running along the length of the roll. When gluing the strips must be located strictly vertically (so the roll cannot be rotated, even if the length is less than the width). Besides, a roll can be cut in an arbitrary manner, but the joints of glued pieces should also be vertical. In addition, each room should be papered by only one type of wallpaper. And pieces of the same roll cannot be used to paper different rooms. That is, for each room the rolls are purchased separately. Also, some rolls can be used not completely.After buying an apartment Boris is short of cash, so he wants to spend the minimum money on wallpaper. Help him.

The first line contains a positive integer n (1 ≤ n ≤ 500) — the number of rooms in Boris's apartment. Each of the next n lines contains three space-separated positive integers — the length, width and height of the walls in a given room in meters, respectively. The next line contains a positive integer m (1 ≤ m ≤ 500) — the number of available wallpaper types. Each of the following m lines contains three space-separated positive integers — the length and width in meters of a given wallpaper and the price of one roll, respectively. All numbers in the input data do not exceed 500. It is guaranteed that each room can be papered using these types of wallpaper.

Print a single number — the minimum total cost of the rolls.

NoteNote to the sample:The total length of the walls (the perimeter) of the room is 20 m.One roll of the first type can be cut into pieces to get three vertical 1 meter wide strips, ergo you need 7 rolls of this type, the price equals 700.A roll of the second type can be cut into pieces to get five 2 meter wide strips, we need 2 rolls, the price is 640.One roll of the third type can immediately paper 19 meters out of 20, but we cannot use other types and we have to buy a second roll, the price is 1000.

<?php
class Room {
    public $h, $len;
    public function __construct($a, $b, $h) {
        $this->h = $h;
        $this->len = 2 * ($a + $b);
    }
}
class Page {
    public $a, $b, $p;
    public function __construct($a, $b, $p) {
        $this->a = $a;
        $this->b = $b;
        $this->p = $p;
    }
}
function getPrice($room, $page) {
    $piece = (int) ($page->a / $room->h);
    if ($piece <= 0)
        return 99999999;
    $wide = $piece * $page->b;
    $rolls = ceil($room->len / $wide);
    return $rolls * $page->p;
}
$room = array();
$page = array();
fscanf(STDIN, "%d", $rooms);
for ($i = 0; $i < $rooms; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $h);
    array_push($room, new Room($a, $b, $h));
}
fscanf(STDIN, "%d", $pages);
for ($i = 0; $i < $pages; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $p);
    array_push($page, new Page($a, $b, $p));
}
$sum = 0;
foreach ($room as $rm) {
    $min = 99999999;
    foreach ($page as $pg) {
        $price = getPrice($rm, $pg);
        $min = min($price, $min);
    }
    $sum+=$min;
}
echo $sum;

<?php

class Room {

    public $h, $len;

    public function __construct($a, $b, $h) {
        $this->h = $h;
        $this->len = 2 * ($a + $b);
    }

}

class Page {

    public $a, $b, $p;

    public function __construct($a, $b, $p) {
        $this->a = $a;
        $this->b = $b;
        $this->p = $p;
    }

}

function getPrice($room, $page) {
    $piece = (int) ($page->a / $room->h);
    if ($piece <= 0)
        return 99999999;

    $wide = $piece * $page->b;
    $rolls = ceil($room->len / $wide);

    return $rolls * $page->p;
}

$room = array();
$page = array();

fscanf(STDIN, "%d", $rooms);
for ($i = 0; $i < $rooms; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $h);
    array_push($room, new Room($a, $b, $h));
}

fscanf(STDIN, "%d", $pages);
for ($i = 0; $i < $pages; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $p);
    array_push($page, new Page($a, $b, $p));
}

$sum = 0;

foreach ($room as $rm) {
    $min = 99999999;
    foreach ($page as $pg) {
        $price = getPrice($rm, $pg);
        if ($price < $min)
            $min = $price;
    }
    $sum+=$min;
}
echo $sum;
//
//usort($a, 'cmp');
//$b = Array();
//foreach ($a as $ai) {
//    $l = 0;
//    $r = count($b);
//    while ($l < $r) {
//        $m = ($l + $r) / 2;
//        if ($ai->h > $b[$m]->h) {
//            $l = $m + 1;
//        } else {
//            $r = $m;
//        }
//    }
//    $ai->p = ($r == 0 ? NULL : $b[$r - 1]);
//    $b[$r] = $ai;
//}
//
//echo count($b) . "\n";
//if (count($b) > 0) {
//    $c = Array();
//    for ($p = array_pop($b); $p != NULL; $p = $p->p) {
//        array_unshift($c, $p->i);
//    }
//    print implode($c, ' ') . "\n";
//}

<?php

class Room {

    public $h, $len;

    public function __construct($a, $b, $h) {
        $this->h = $h;
        $this->len = 2 * ($a + $b);
    }

}

class Page {

    public $a, $b, $p;

    public function __construct($a, $b, $p) {
        $this->a = $a;
        $this->b = $b;
        $this->p = $p;
    }

}

function getPrice($room, $page) {
    $piece = (int)($page -> a / $room->h);
    if($piece<=0)
        return 99999999;
    $wide = $piece * $page -> b;

    $yu_len = $room->len % $wide;
    $rolls = (int)($room->len / $wide) + ($yu_len != 0 ? 1 : 0);
    return $rolls * $page->p;
}

$room = array();
$page = array();

fscanf(STDIN, "%d", $rooms);
for ($i = 0; $i < $rooms; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $h);
    array_push($room, new Room($a, $b, $h));
}

fscanf(STDIN, "%d", $pages);
for ($i = 0; $i < $pages; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $p);
    array_push($page, new Page($a, $b, $p));
}

$sum=0;

foreach ($room as $rm) {
    $min=99999999;
    foreach ($page as $pg) {
        $price=getPrice($rm, $pg);
        if($price<$min)
            $min=$price;
    }
    $sum+=$min;
}
echo $sum;
//
//usort($a, 'cmp');
//$b = Array();
//foreach ($a as $ai) {
//    $l = 0;
//    $r = count($b);
//    while ($l < $r) {
//        $m = ($l + $r) / 2;
//        if ($ai->h > $b[$m]->h) {
//            $l = $m + 1;
//        } else {
//            $r = $m;
//        }
//    }
//    $ai->p = ($r == 0 ? NULL : $b[$r - 1]);
//    $b[$r] = $ai;
//}
//
//echo count($b) . "\n";
//if (count($b) > 0) {
//    $c = Array();
//    for ($p = array_pop($b); $p != NULL; $p = $p->p) {
//        array_unshift($c, $p->i);
//    }
//    print implode($c, ' ') . "\n";
//}

<?php

function getPrice($room_long, $room_height, $paper_width, $paper_height, $paper_price) {
    //print_r(func_get_args());
    if (!floor($paper_height/$room_height))
        return false;
    return (ceil(ceil($room_long/$paper_width)/floor($paper_height/$room_height))*$paper_price);
}

//incomming data
$file = file('php://stdin');
$rooms_cnt = $file[0];
if ((int)$rooms_cnt<1)
    exit;
$rooms = array();
for ($i=0; $i<$rooms_cnt; $i++) {
    $data = explode(' ', $file[$i+1]);
    $rooms[$i]['long'] = ($data[0]+$data[1])*2;
    $rooms[$i]['height'] = $data[2];
}

$i++;
$papers_cnt = $file[$i];
if ((int)$papers_cnt<1)
    exit;

$papers = array();
for ($k=($i+1); $k<($i+1+$papers_cnt); $k++) {
    $data = explode(' ', $file[$k]);
    $papers[$k]['height'] = $data[0];
    $papers[$k]['width'] = $data[1];
    $papers[$k]['price'] = $data[2];
}

$result = 0;
foreach ($rooms as $room_number=>$room_data) {
    foreach ($papers as $paper) {
        $res = getPrice($room_data['long'], $room_data['height'], $paper['width'], $paper['height'], $paper['price']);
        //echo $res;
        if ($res!==false) {
            if (!isset($min)) {
                $min = $res;
            } else {
                $min = min($min, $res);
            }
        }
    }
    $result += (int)$min;
    unset($min);
}

echo $result;

?>

<?php

function getPrice($room_long, $room_height, $paper_width, $paper_height, $paper_price) {
    //print_r(func_get_args());
    if (!floor($paper_height/$room_height))
        return false;
    return (ceil(ceil($room_long/$paper_width)/floor($paper_height/$room_height))*$paper_price);
}

//incomming data
$file = file('php://stdin');
$rooms_cnt = $file[0];
if ((int)$rooms_cnt<1)
    exit;
$rooms = array();
for ($i=0; $i<$rooms_cnt; $i++) {
    $data = explode(' ', $file[$i+1]);
    $rooms[$i]['long'] = ($data[0]+$data[1])*2;
    $rooms[$i]['height'] = $data[2];
}

$i++;
$papers_cnt = $file[$i];
if ((int)$papers_cnt<1)
    exit;

$papers = array();
for ($k=($i+1); $k<($i+1+$papers_cnt); $k++) {
    $data = explode(' ', $file[$k]);
    $papers[$k]['height'] = $data[0];
    $papers[$k]['width'] = $data[1];
    $papers[$k]['price'] = $data[2];
}

$result = 0;
foreach ($rooms as $room_number=>$room_data) {
    $room_possibility = array();
    foreach ($papers as $paper) {
        $res = getPrice($room_data['long'], $room_data['height'], $paper['width'], $paper['height'], $paper['price']);
        //echo $res;
        if ($res!==false) {
            $room_possibility[] = $res;
        }
    }
    $result += (int)min($room_possibility);
}

echo $result;

?>

<?php

class Room {

    public $h, $len;

    public function __construct($a, $b, $h) {
        $this->h = $h;
        $this->len = 2 * ($a + $b);
    }

}

class Page {

    public $a, $b, $p;

    public function __construct($a, $b, $p) {
        $this->a = $a;
        $this->b = $b;
        $this->p = $p;
    }

}

function getPrice($room, $page) {
    $piece = (int)($page -> a / $room->h);
    $wide = $piece * $page -> b;

    $yu_len = $room->len % $wide;
    $rolls = (int)($room->len / $wide) + ($yu_len != 0 ? 1 : 0);
    return $rolls * $page->p;
}

$room = array();
$page = array();

fscanf(STDIN, "%d", $rooms);
for ($i = 0; $i < $rooms; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $h);
    array_push($room, new Room($a, $b, $h));
}

fscanf(STDIN, "%d", $pages);
for ($i = 0; $i < $pages; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $p);
    array_push($page, new Page($a, $b, $p));
}

$sum=0;

foreach ($room as $rm) {
    $min=99999999;
    foreach ($page as $pg) {
        $price=getPrice($rm, $pg);
        if($price<$min)
            $min=$price;
    }
    $sum+=$min;
}
echo $sum;
//
//usort($a, 'cmp');
//$b = Array();
//foreach ($a as $ai) {
//    $l = 0;
//    $r = count($b);
//    while ($l < $r) {
//        $m = ($l + $r) / 2;
//        if ($ai->h > $b[$m]->h) {
//            $l = $m + 1;
//        } else {
//            $r = $m;
//        }
//    }
//    $ai->p = ($r == 0 ? NULL : $b[$r - 1]);
//    $b[$r] = $ai;
//}
//
//echo count($b) . "\n";
//if (count($b) > 0) {
//    $c = Array();
//    for ($p = array_pop($b); $p != NULL; $p = $p->p) {
//        array_unshift($c, $p->i);
//    }
//    print implode($c, ' ') . "\n";
//}

<?php

class Room {

    public $h, $len;

    public function __construct($a, $b, $h) {
        $this->h = $h;
        $this->len = 2 * ($a + $b);
    }

}

class Page {

    public $a, $b, $p;

    public function __construct($a, $b, $p) {
        $this->a = $a;
        $this->b = $b;
        $this->p = $p;
    }

}

function getPrice($room, $page) {
    $piece = $page -> a / $room->h;
    $wide = $piece * $page -> b;

    $yu_len = $room->len % $wide;
    $rolls = $room->len / $wide + ($yu_len != 0 ? 1 : 0);
    return $rolls * $page->p;
}

$room = array();
$page = array();

fscanf(STDIN, "%d", $rooms);
for ($i = 0; $i < $rooms; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $h);
    array_push($room, new Room($a, $b, $h));
}

fscanf(STDIN, "%d", $pages);
for ($i = 0; $i < $pages; ++$i) {
    fscanf(STDIN, "%d%d%d", $a, $b, $p);
    array_push($page, new Page($a, $b, $p));
}

$sum=0;

foreach ($room as $rm) {
    $min=99999999;
    foreach ($page as $pg) {
        $price=getPrice($rm, $pg);
        if($price<$min)
            $min=$price;
    }
    $sum+=$min;
}
echo $sum;
//
//usort($a, 'cmp');
//$b = Array();
//foreach ($a as $ai) {
//    $l = 0;
//    $r = count($b);
//    while ($l < $r) {
//        $m = ($l + $r) / 2;
//        if ($ai->h > $b[$m]->h) {
//            $l = $m + 1;
//        } else {
//            $r = $m;
//        }
//    }
//    $ai->p = ($r == 0 ? NULL : $b[$r - 1]);
//    $b[$r] = $ai;
//}
//
//echo count($b) . "\n";
//if (count($b) > 0) {
//    $c = Array();
//    for ($p = array_pop($b); $p != NULL; $p = $p->p) {
//        array_unshift($c, $p->i);
//    }
//    print implode($c, ' ') . "\n";
//}

<?php

function getPrice($room_square, $paper_square, $paper_price) {
    //echo "room_square=$room_square; paper_square=$paper_square; paper_price=$paper_price";
    return (ceil($room_square/$paper_square)*$paper_price);
}

//incomming data
$file = file('php://stdin');
$rooms_cnt = $file[0];
if ((int)$rooms_cnt<1)
    exit;
$rooms_square = array();
for ($i=0; $i<$rooms_cnt; $i++) {
    $data = explode(' ', $file[$i+1]);
    $rooms_square[$i] = ($data[0]+$data[1])*2*$data[2];
}

$i++;
$papers_cnt = $file[$i];
if ((int)$papers_cnt<1)
    exit;

$papers = array();
for ($k=($i+1); $k<($i+1+$papers_cnt); $k++) {
    $data = explode(' ', $file[$k]);
    $papers[$k]['square'] = $data[0]*$data[1];
    $papers[$k]['price'] = $data[2];
}

$result = 0;
foreach ($rooms_square as $room_number=>$room_square) {
    $room_possibility = array();
    foreach ($papers as $paper) {
        $room_possibility[] = getPrice($room_square, $paper['square'], $paper['price']);
    }
    $result += (int)min($room_possibility);
}

echo $result;

?>


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

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

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

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

<?php

function FindStudentPosition($students_no,$ratings){

    $ratings     = explode(" ",$ratings);
    $endPosition = array();

    for($i=0;$i<$students_no;$i++){
        $higher_rating_student_no = 1;
        for($j=0;$j<$students_no;$j++){
            if($ratings[$i]<$ratings[$j]){
                $higher_rating_student_no++;
            }
        }
        $endPosition[$i] = $higher_rating_student_no;
    }

    for($i=0;$i<$students_no;$i++){
        echo $endPosition[$i].' ';
    }
}

$students_no   = trim(fgets(STDIN));
$ratings       = trim(fgets(STDIN));
echo FindStudentPosition($students_no,$ratings);

<?php
// 490A     Командная олимпиада
$in = fopen("php://stdin", 'r');

$input_line = fgets($in);
$n = (int) $input_line;

$input_line = fgets($in);
$arr = array();
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arrout = array();

for ($i=0; $i < $n; $i++) {
    $col = 1;
    for ($j=0; $j < $n; $j++) {
        if (((int) $arr[$i]) < ((int) $arr[$j])) {
            $col ++;
        }
    }
    array_push($arrout, $col);
}

$out ='';

foreach ($arrout as $key => $value) {
    $out .= $value . ' ';
}

echo trim($out);

?>

<?php
$freq = array_fill(1, 2100, 0);
$rank = array_fill(1, 2100, 0);
$sum = 0;
$rank_now = 1;
$_fp = fopen("php://stdin", "r");
fscanf($_fp, "%d\n", $count);
$numbers = explode(" ", trim(fgets($_fp)));
foreach ($numbers as &$number)
{
    $number = intval($number);
    $freq[$number]++;
}
for ($i=2000; $i > 0; $i--) {
	$rank[$i] = $rank_now;
	$rank_now += $freq[$i];
}
for ($i=0; $i < $count; $i++) {
	if ($i != 0) {
		echo " ";
	}
	echo $rank[$numbers[$i]];
}
echo "\n";
?>

<?php
/**
 * Created by PhpStorm.
 * User: Rana
 * Date: 15-06-12
 * Time: 12:56 PM
 */

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

$handle = fopen('php://stdin', 'r');

$line = fgets( $handle );

$n =   (int)$line;

$line = fgets($handle);

$ratings = preg_split('/[\s]/',$line);
//last extra one is empty

$rankings = array();

for($i=0;$i<$n;$i++){
    $rankings[$i] = 1;
    for($j=0;$j<$n;$j++){
        if($i != $j){
            if($ratings[$j]>$ratings[$i]){
                $rankings[$i]++;
            }
        }
    }
}

for($i=0;$i<$n;$i++){
    echo $rankings[$i]." ";
}
echo "\n";

fclose($handle);

<?php
/**
 * Created by PhpStorm.
 * User: Rana
 * Date: 15-06-12
 * Time: 12:56 PM
 */

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

$handle = fopen('php://stdin', 'r');

$line = fgets( $handle );

$n =   (int)$line;

$line = fgets($handle);

$ratings = preg_split('/[\s]/',$line);
//last extra one is empty

$rankings = array();

for($i=0;$i<count($ratings)-1;$i++){
    $rankings[$i] = 1;
    for($j=0;$j<count($ratings)-1;$j++){
        if($i != $j){
            if($ratings[$j]>$ratings[$i]){
                $rankings[$i]++;
            }
        }
    }
}

for($i=0;$i<count($ratings)-1;$i++){
    echo $rankings[$i]." ";
}
echo "\n";

fclose($handle);

<?php
/**
 * Created by PhpStorm.
 * User: Rana
 * Date: 15-06-12
 * Time: 12:56 PM
 */

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

$handle = fopen('php://stdin', 'r');

$line = fgets( $handle );

$n =   (int)$line;

$line = fgets($handle);

$ratings = preg_split('/[\s]/',$line);
//last extra one is empty

$rankings = array();

for($i=0;$i<count($ratings)-1;$i++){
    $rankings[$i] = 1;
    for($j=0;$j<count($ratings)-1;$j++){
        if($i != $j){
            if($ratings[$j]>$ratings[$i]){
                $rankings[$i]++;
            }
        }
    }
}

for($i=0;$i<count($ratings)-1;$i++){
    echo $rankings[$i]." ";
}

fclose($handle);


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

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

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

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

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($n, $k) = explode(" ", trim(fgets($file)));
$aArray = explode(" ", trim(fgets($file)));
$pArray = array_fill(0, 10, 0);
$extra = 0;
$result = 0;
for ($i = 0; $i < $n; $i++) {
    $extra += floor((100 - $aArray[$i]) / 10);
    $result += floor($aArray[$i] / 10);
    if ($aArray[$i]%10==0) {
        continue;
    }
    $pArray[10 - ($aArray[$i] % 10)]++;
}
for ($i = 1; $i < 10; $i++) {
    if ($pArray[$i] == 0) {
        continue;
    }
    $x = floor($k / $i);
    $x = min($x, $pArray[$i]);
    $result += $x;
    $k -= $x * $i;
}
$x = floor($k / 10);
$result += min($x, $extra);
echo $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($n, $k) = explode(" ", trim(fgets($file)));
$aArray = explode(" ", trim(fgets($file)));
$pArray = array_fill(0, 10, 0);
$extra = 0;
$result = 0;
for ($i = 0; $i < $n; $i++) {
    $extra += floor((100 - $aArray[$i]) / 10);
    $result += floor($aArray[$i] / 10);
    if ($aArray[$i]%10==0) {
        continue;
    }
    $pArray[10 - $aArray[$i] % 10]++;
}
for ($i = 1; $i < 10; $i++) {
    if ($pArray[$i] == 0) {
        continue;
    }
    $x = floor($k / $pArray[$i]);
    $x = min($x, $pArray[$i]);
    $result += $x;
    $k -= $x * $i;
}
$x = floor($k / 10);
$result += min($x, $extra);
echo $result;


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

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

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

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


Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills: a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1.For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards).Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.

The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.

If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).

<?php
# 输入处理
    # 读入第1行，并丢弃
    $in=fgets(STDIN);
    # 读入第2行
    $in=fgets(STDIN);
    # 按空格分隔并转换为整数
    $num=explode(" ", $in);
    foreach ($num as &$v) {
        $v=intval($v);
    }
    # $len 存储数据个数
    $len=count($num);
# 输入处理结束

# 当只有0或1个数字的时候，输出-1并退出
if($len<=1){
    echo -1;
    exit();
}

# 预处理
    # 将 $num 数组按升序排列
    sort($num);
    # 建立空数组 $error，用以存放两数差值
    $error=array();
    # 分别计算数组中前后两数差值，以这个差值为键，存入$error中。
    for($i=1;$i<$len;$i++){
        # 计算当前数和之前数的差值
        $ds=$num[$i]-$num[$i-1];
        # 如果存在这个差值，就把当前下标插入这个差值所在的数组中。
        if(array_key_exists($ds, $error)){
            array_push($error[$ds],$i);
        # 否则，新建这个差值为键，下标为值的数组
        }else{
            $error[$ds]=array($i);
        }
    }
    # 统计存在的差值个数
    $errorcount=count($error);
# 预处理结束

# 结果处理
# 新建结果数组
$result=array();
# 对存在的差值个数进行分类讨论
# 如果差值个数小于或等于0
if($errorcount<=0){
    # 你TM在逗我？
    exit();
}else
# 如果只有一个差值——可能情况：
# 1.只有2个元素；
# 2.已经是等差数列；
if($errorcount==1){
    # 检出这个差值，保存在 $d 中
    $d=array_keys($error);
    $d=(int)$d[0];
    # 如果只有两个元素
    if($len==2){
        # 检查是否可以在中间插入
        if($d%2==0)
            array_push($result, $num[1]-$d/2);
    }
    # 对于已经是等差数列的情况和只有两个元素的情况，都需要两边插入
    array_push($result, $num[0]-$d, $num[$len-1]+$d);
    # 去除结果数组中重复数据
    $result=array_unique($result);
    # 对结果数组再排序
    sort($result);
    # 输出
    echo count($result);
    echo "\n";
    foreach ($result as $v) {
        echo $v." ";
    }
    exit();
}else
# 如果存在两个差值，则有可能存在一个
if($errorcount==2){
    # 检出这两个差值
    $d=array_keys($error);
    sort($d);
    # 检查较大的key是否是较小的key的2倍
    if($d[1]-$d[0]==$d[0]) {
        # 如果是，则检查较大的key是否只有一个元素。
        if(count($error[$d[1]])==1){
            # 如果是，则在此键值处插入
            $result=$num[$error[$d[1]][0]]-$d[0];
            # 输出 “1\n此数字”
            echo "1\n".$result;
            exit();
        }
    }
    # 否则，输出0
    echo 0;
    exit();
}
# 如果存在多于两个差值，则一定不存在可插入元素
else{
    # 输出0
    echo 0;
    exit();
}
?>
<!doctype html>

        <title>Zyzsdy</title>

这数据也太坑了。。。。

@author: Zyzsdy

@Website: http://www.namido.net

@date: 2014/1/18 15:02 (UTC+8)

感想： 这次CF就过一题居然没扣分。。。。。。

不过我这题差一点就做出来了。。。

。。。。。。。

唉。。。。

<?php
$in=fgets(STDIN);
$n=intval($in);
$in2=fgets(STDIN);
$n2=explode(" ", $in2);
foreach ($n2 as &$v) {
	$v=intval($v);
}
$ct=count($n2);
if($ct==1){
	echo -1;
	exit();
}
$error=array();
sort($n2);
$deim=$n2[1]-$n2[0];
if($ct==2){
	$result=array();
	if($deim%2==0){
		array_push($result,$n2[1]-$deim/2,$n2[0]-$deim,$n2[1]+$deim);
	}else{
		array_push($result,$n2[0]-$deim,$n2[1]+$deim);
	}
	$result=array_unique($result);
	sort($result);
	echo count($result);
	echo "\n";
	foreach ($result as $v) {
		echo $v." ";
	}
	exit();
}
for($i=2;$i<$ct;$i++){
	$deim2=$n2[$i]-$n2[$i-1];
	if($deim2!=$deim){
		array_push($error,array($i,$deim2));
	}
}
$errorcount=count($error);
if($errorcount>=2){
	echo 0;
	exit();
}
else if($errorcount==1){
	if($error[0][1]-$deim!=$deim){
		echo 0;
		exit();
	}
	else{
		echo "1\n";
		echo $n2[$error[0][0]]-$deim;
		exit();
	}
}
else if($errorcount==0){
	$result=array();
	array_push($result,$n2[0]-$deim,$n2[$ct-1]+$deim);
	$result=array_unique($result);
	sort($result);
	echo count($result);
	echo "\n";
	foreach ($result as $v) {
		echo $v." ";
	}
	exit();

}

?>

<?php
$in=fgets(STDIN);
$n=intval($in);
$in2=fgets(STDIN);
$n2=explode(" ", $in2);
foreach ($n2 as &$v) {
	$v=intval($v);
}
$ct=count($n2);
if($ct==1){
	echo -1;
	exit();
}
$error=array();
sort($n2);
$deim=$n2[1]-$n2[0];
if($ct==2){
	$result=array();
	if($deim%2==0){
		array_push($result,$n2[1]-$deim/2,$n2[0]-$deim,$n2[1]+$deim);
	}
	$result=array_unique($result);
	sort($result);
	echo count($result);
	echo "\n";
	foreach ($result as $v) {
		echo $v." ";
	}
	exit();
}
for($i=2;$i<$ct;$i++){
	$deim2=$n2[$i]-$n2[$i-1];
	if($deim2!=$deim){
		array_push($error,array($i,$deim2));
	}
}
$errorcount=count($error);
if($errorcount>=2){
	echo 0;
	exit();
}
else if($errorcount==1){
	if($error[0][1]-$deim!=$deim){
		echo 0;
		exit();
	}
	else{
		echo "1\n";
		echo $n2[$error[0][0]]-$deim;
		exit();
	}
}
else if($errorcount==0){
	$result=array();
	array_push($result,$n2[0]-$deim,$n2[$ct-1]+$deim);
	$result=array_unique($result);
	sort($result);
	echo count($result);
	echo "\n";
	foreach ($result as $v) {
		echo $v." ";
	}
	exit();

}

?>