Polycarp has come up with a new game to play with you. He calls it "A missing bigram".A bigram of a word is a sequence of two adjacent letters in it.For example, word "abbaaba" contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba".The game goes as follows. First, Polycarp comes up with a word, consisting only of lowercase letters 'a' and 'b'. Then, he writes down all its bigrams on a whiteboard in the same order as they appear in the word. After that, he wipes one of them off the whiteboard.Finally, Polycarp invites you to guess what the word that he has come up with was.Your goal is to find any word such that it's possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with.The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 2000$$$) — the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$3 \le n \le 100$$$) — the length of the word Polycarp has come up with. The second line of each testcase contains $$$n-2$$$ bigrams of that word, separated by a single space. Each bigram consists of two letters, each of them is either 'a' or 'b'. Additional constraint on the input: there exists at least one string such that it is possible to write down all its bigrams, except one, so that the resulting sequence is the same as the sequence in the input. In other words, the answer exists.

For each testcase print a word, consisting of $$$n$$$ letters, each of them should be either 'a' or 'b'. It should be possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them.

4
7
ab bb ba aa ba
7
ab ba aa ab ba
3
aa
5
bb ab bb

NoteThe first two testcases from the example are produced from the word "abbaaba". As listed in the statement, it contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba".In the first testcase, the $$$5$$$-th bigram is removed. In the second testcase, the $$$2$$$-nd bigram is removed. However, that sequence could also have been produced from the word "abaabaa". It contains bigrams "ab", "ba", "aa", "ab", "ba" and "aa". The missing bigram is the $$$6$$$-th one.In the third testcase, all of "baa", "aab" and "aaa" are valid answers.

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));
  $a = explode(" ", trim(fgets($file)));

  $newWord = [];
  $newWord[]= substr($a[0], 0, 1);

	for ($j = 1; $j < count($a);) {
		if (substr($a[$j - 1], 1, 1) == substr($a[$j], 0, 1)) {
			$newWord[] = substr($a[$j], 0, 1);
			$j++;
		} else {
			$newWord[] = substr($a[$j - 1], 1, 1);
			$a[$j - 1] = substr($a[$j - 1], 0, 1).substr($a[$j], 0, 1);
		}
	}

	$newWord[] = substr($a[$j - 1], 1, 1);

	if (count($newWord) < $n) {
		$newWord[] = substr($a[$j - 1], 1, 1);
	}

	$newStr = implode('', $newWord);
	echo $newStr.PHP_EOL;
}

?>

<?

$n = trim(fgets(STDIN));
for($i=0;$i<$n;$i++){
	$k = trim(fgets(STDIN));
	$arr = explode(" ",trim(fgets(STDIN)));
	$word = [];
	$word[]= substr($arr[0],0,1);
	//$word[]= substr($arr[0],1,1);
	for($j=1;$j<count($arr);){
		if(substr($arr[$j-1],1,1)==substr($arr[$j],0,1)){
			$word[] = substr($arr[$j],0,1);
			$j++;
		}else{
			$word[] = substr($arr[$j-1],1,1);
			$arr[$j-1] = substr($arr[$j-1],0,1).substr($arr[$j],0,1);
		}
	}
	$word[] = substr($arr[count($arr)-1],1,1);
	if(count($word)<$k){
		$word[] = substr($arr[count($arr)-1],1,1);
	}
	echo implode("",$word)."\n";
}

<?php
// $stdin = fopen('input.txt', 'r');

$solve = function ($n, $a) {

	$l = ($n - 2) * 3;
	$res = substr($a, 0,1);
	$a1 = substr($a, 1,1);
	$p = false;
	for ($i = 3; $i < $l; $i += 3){
		$a2 = substr($a, $i, 1);

		if($a1 != $a2 && !$p){
			$res .= $a1;
			$p = true;
		}

		$res .= $a2;
		$a1 = substr($a, $i + 1, 1);
	}
	$res .= $a1;
	if(!$p){
		$res .= 'a';
	}

	return $res;

};

$t = intval(fgets(STDIN));

for (; $t > 0; $t--) {

	$n = intval(fgets(STDIN));
	$a = fgets(STDIN);

	echo $solve($n, $a);
	echo "\n";

}

<?php
$fileName = 'php://stdin';
$file = fopen($fileName, "r");

$t = trim(fgets($file));

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));
  $a = explode(" ", trim(fgets($file)));

  $newWord = [];
  $newWord[]= substr($a[0], 0, 1);

	for ($j = 1; $j < count($a);) {
	  $part1 = substr($a[$j - 1], 1, 1);
	  $part2 = substr($a[$j], 0, 1);
		if ($part1 == $part2) {
			$newWord[] = $part2;
			$j++;
		} else {
			$newWord[] = $part1;
			$a[$j - 1] = substr($a[$j - 1], 0, 1).$part2;
		}
	}

	$newWord[] = $part1;

	if (count($newWord) < $n) {
		$newWord[] = $part1;
	}

	$newStr = implode('', $newWord);
	echo $newStr.PHP_EOL;
}
?>


There are $$$n$$$ blocks arranged in a row and numbered from left to right, starting from one. Each block is either black or white. You may perform the following operation zero or more times: choose two adjacent blocks and invert their colors (white block becomes black, and vice versa). You want to find a sequence of operations, such that they make all the blocks having the same color. You don't have to minimize the number of operations, but it should not exceed $$$3 \cdot n$$$. If it is impossible to find such a sequence of operations, you need to report it.

The first line contains one integer $$$n$$$ ($$$2 \le n \le 200$$$) — the number of blocks. The second line contains one string $$$s$$$ consisting of $$$n$$$ characters, each character is either "W" or "B". If the $$$i$$$-th character is "W", then the $$$i$$$-th block is white. If the $$$i$$$-th character is "B", then the $$$i$$$-th block is black.

If it is impossible to make all the blocks having the same color, print $$$-1$$$. Otherwise, print an integer $$$k$$$ ($$$0 \le k \le 3 \cdot n$$$) — the number of operations. Then print $$$k$$$ integers $$$p_1, p_2, \dots, p_k$$$ $$$(1 \le p_j \le n - 1)$$$, where $$$p_j$$$ is the position of the left block in the pair of blocks that should be affected by the $$$j$$$-th operation. If there are multiple answers, print any of them.

NoteIn the first example, it is possible to make all blocks black in $$$3$$$ operations. Start with changing blocks $$$6$$$ and $$$7$$$, so the sequence is "BWWWWBBB". Then change blocks $$$2$$$ and $$$3$$$, so the sequence is "BBBWWBB". And finally, change blocks $$$4$$$ and $$$5$$$, so all blocks are black.It is impossible to make all colors equal in the second example.All blocks are already white in the third example.In the fourth example it is possible to make all blocks white in two operations: first operation is to change blocks $$$2$$$ and $$$3$$$ (so the sequence is "BBW"), and then change blocks $$$1$$$ and $$$2$$$ (so all blocks are white).


The process of mammoth's genome decoding in Berland comes to its end!One of the few remaining tasks is to restore unrecognized nucleotides in a found chain s. Each nucleotide is coded with a capital letter of English alphabet: 'A', 'C', 'G' or 'T'. Unrecognized nucleotides are coded by a question mark '?'. Thus, s is a string consisting of letters 'A', 'C', 'G', 'T' and characters '?'.It is known that the number of nucleotides of each of the four types in the decoded genome of mammoth in Berland should be equal.Your task is to decode the genome and replace each unrecognized nucleotide with one of the four types so that the number of nucleotides of each of the four types becomes equal.

The first line contains the integer n (4 ≤ n ≤ 255) — the length of the genome. The second line contains the string s of length n — the coded genome. It consists of characters 'A', 'C', 'G', 'T' and '?'.

If it is possible to decode the genome, print it. If there are multiple answer, print any of them. If it is not possible, print three equals signs in a row: "===" (without quotes).

NoteIn the first example you can replace the first question mark with the letter 'A', the second question mark with the letter 'G', the third question mark with the letter 'T', then each nucleotide in the genome would be presented twice.In the second example the genome is already decoded correctly and each nucleotide is exactly once in it.In the third and the fourth examples it is impossible to decode the genom.

<?php

function run($length, $string) {
    if ($length % 4 !== 0) return '===';

    $n = ['A', 'C', 'G', 'T'];

    $maxLen = $length/4;

    $count = array_fill_keys($n, $maxLen);

    for ($i = 0; $i < $length; $i++){
        $letter = $string[$i];
        if (isset($count[$letter])) {
            if ($count[$letter] <= 1) {
                unset($count[$letter]);
            } else {
                $count[$letter]--;
            }
        } elseif ($letter !== '?') {
            return '===';
        }
    }

    $result = '';

    for ($i = 0; $i < $length; $i++) {
        $letter = $string[$i];

        if ($letter === '?') {
            $key = array_shift(array_keys($count));
            if ($count[$key] <= 1) {
                unset($count[$key]);
            } else {
                $count[$key]--;
            }
            $result .= $key;
        } else {
            $result .= $letter;
        }
    }

    return $result;
}

echo run(fgets(STDIN), fgets(STDIN));

<?php
fscanf(STDIN,"%d",$lenOfStr);
fscanf(STDIN,"%s",$str);
$decision=false;

if($lenOfStr%4==0)
 {
 	 $a=0;
 	 $g=0;
 	 $c=0;
 	 $t=0;
     $split=str_split($str);
     for ($i=0; $i <count($split) ; $i++)
      {
     	   if($split[$i]=='A')
     	   	   $a++;
     	   	elseif ($split[$i]=='G')
     	   	 {
     	   		$g++;
     	   	 }
     	   	elseif ($split[$i]=='C')
     	   	 {
     	   		$c++;
     	   	 }
     	   	 else
     	   	 {
     	   	 	if($split[$i]=='T')
     	   	 		$t++;
     	   	 }

      }
      $div=$lenOfStr/4;
      if($div<$a||$div<$g||$div<$c||$div<$t)
      	  $decision=false;
       //$loop=$div;
       else
       {

       	    while($a<$div)
       	    {
       	    	for ($i=0; $i <strlen($str) ; $i++)
       	    	 {
       	    		if($str[$i]=='?')
       	    		{
       	    			$str[$i]='A';
       	    			break;
       	    		}
       	    	 }
       	    	$a++;
       	    }
       	    while($c<$div)
       	    {
       	    	for ($i=0; $i <strlen($str) ; $i++)
       	    	 {
       	    		if($str[$i]=='?')
       	    		{
       	    			$str[$i]='C';
       	    			break;
       	    		}
       	    	 }
       	    	$c++;
       	    }
       	    while($g<$div)
       	    {
       	    	for ($i=0; $i <strlen($str) ; $i++)
       	    	 {
       	    		if($str[$i]=='?')
       	    		{
       	    			$str[$i]='G';
       	    			break;
       	    		}
       	    	 }
       	    	$g++;
       	    }
       	    while($t<$div)
       	    {
       	    	for ($i=0; $i <strlen($str) ; $i++)
       	    	 {
       	    		if($str[$i]=='?')
       	    		{
       	    			$str[$i]='T';
       	    			break;
       	    		}
       	    	 }
       	    	$t++;
       	    }

       	    //echo "$str";
        	$decision=true;
       }
 }
 echo $decision?"$str":"===";

<?php
fscanf(STDIN,"%d",$lenOfStr);
fscanf(STDIN,"%s",$str);
$decision=false;

if($lenOfStr%4==0)
 {
 	 $a=0;
 	 $g=0;
 	 $c=0;
 	 $t=0;
     $split=str_split($str);
     for ($i=0; $i <count($split) ; $i++)
      {
     	   if($split[$i]=='A')
     	   	   $a++;
     	   	elseif ($split[$i]=='G')
     	   	 {
     	   		$g++;
     	   	 }
     	   	elseif ($split[$i]=='C')
     	   	 {
     	   		$c++;
     	   	 }
     	   	 else
     	   	 {
     	   	 	if($split[$i]=='T')
     	   	 		$t++;
     	   	 }

      }
      $div=$lenOfStr/4;
      if($div<$a||$div<$g||$div<$c||$div<$t)
      	  $decision=false;
       //$loop=$div;
       else
       {
       	$rslt=str_repeat("AGCT",$div);
       	$decision=true;
       }
     // echo "$str";
 }
 echo $decision?"$rslt":"===";


Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number $$$1$$$. Each person is looking through the circle's center at the opposite person.    A sample of a circle of $$$6$$$ persons. The orange arrows indicate who is looking at whom. You don't know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number $$$a$$$ is looking at the person with the number $$$b$$$ (and vice versa, of course). What is the number associated with a person being looked at by the person with the number $$$c$$$? If, for the specified $$$a$$$, $$$b$$$, and $$$c$$$, no such circle exists, output -1.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le a,b,c \le 10^8$$$).

For each test case output in a separate line a single integer $$$d$$$ — the number of the person being looked at by the person with the number $$$c$$$ in a circle such that the person with the number $$$a$$$ is looking at the person with the number $$$b$$$. If there are multiple solutions, print any of them. Output $$$-1$$$ if there's no circle meeting the given conditions.

NoteIn the first test case, there's a desired circle of $$$8$$$ people. The person with the number $$$6$$$ will look at the person with the number $$$2$$$ and the person with the number $$$8$$$ will look at the person with the number $$$4$$$.In the second test case, there's no circle meeting the conditions. If the person with the number $$$2$$$ is looking at the person with the number $$$3$$$, the circle consists of $$$2$$$ people because these persons are neighbors. But, in this case, they must have the numbers $$$1$$$ and $$$2$$$, but it doesn't meet the problem's conditions.In the third test case, the only circle with the persons with the numbers $$$2$$$ and $$$4$$$ looking at each other consists of $$$4$$$ people. Therefore, the person with the number $$$10$$$ doesn't occur in the circle.


Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed x in the absolute value.Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found n of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero?You can assume that initially Vanya had infinitely many cards with each integer number from  - x to x.

The first line contains two integers: n (1 ≤ n ≤ 1000) — the number of found cards and x (1 ≤ x ≤ 1000) — the maximum absolute value of the number on a card. The second line contains n space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed x in their absolute value.

Print a single number — the answer to the problem.

NoteIn the first sample, Vanya needs to find a single card with number -2.In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value.

<?php
  $s = trim(fgets(STDIN));
  list($n, $x) = explode(' ', $s);
  $s = trim(fgets(STDIN));
  $a = explode(' ', $s);
  $sum = 0;
  for ($i = 0; $i < $n; $i++) {
    $sum += $a[$i];
  }
  $sum = abs($sum);
  echo floor($sum / $x) + ($sum % $x ? 1 : 0), "\n";

<?php
// 401A     Ваня и карточки

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

$input_line = fgets($in);
list($n, $x) = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);
//$n = (int) $input_line;

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

$summa = 0;
foreach ($arr as $key => $value) {
    $summa += (int) $value;
}
$summa = abs($summa);

$col = intval($summa / (int) $x);
if ($summa % (int) $x !== 0) $col ++;

echo "$col\n";

?>

<?php

list($n, $x) = explode(' ', line());
$a = explode(' ', line());

$n = intval($n);
$x = intval($x);

$sum = 0;

for ($i = 0; $i < $n; $i ++) {
  $sum += intval($a[$i]);
}
$sum = abs($sum);

print intval($sum / $x) + ($sum%$x > 0 ? 1 : 0);

function line() {
  return trim(fgets(STDIN));
}

<?php
 $f = fopen("php://stdin","r");
 $str1 = fgets($f);
 $str2 = fgets($f);
 $numbers = explode(" ", $str2);
 $summ = 0;
 foreach($numbers as $key=>$value)
 {
 		$summ += $value;
 }
 $data = explode(" ", $str1);
 $summ = abs($summ);
 $res = floor($summ / $data[1]);
 if($res * $data[1] != $summ)
 	$res++;
 echo $res;
 ?>

<?php
  $s = trim(fgets(STDIN));
  list($n, $x) = explode(' ', $s);
  $s = trim(fgets(STDIN));
  $a = explode(' ', $s);
  $sum = 0;
  for ($i = 0; $i < $n; $i++) {
    $sum += $a[$i];
  }
  $sum = abs($sum);
  echo floor($sum / $n) + ($sum % $n ? 1 : 0), "\n";


A sportsman starts from point xstart = 0 and runs to point with coordinate xfinish = m (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than s meters (in this case for these s meters his path should have no obstacles), and after that he can jump over a length of not more than d meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle.On the way of an athlete are n obstacles at coordinates x1, x2, ..., xn. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point.

The first line of the input containsd four integers n, m, s and d (1 ≤ n ≤ 200 000, 2 ≤ m ≤ 109, 1 ≤ s, d ≤ 109) — the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ m - 1) — the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order.

If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes). If the athlete can get from start to finish, print any way to do this in the following format:   print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should run for "X" more meters;  print a line of form "JUMP Y" (where "Y" should be a positive integer), if the sportsman starts a jump and should remain in air for "Y" more meters.  All commands "RUN" and "JUMP" should strictly alternate, starting with "RUN", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish.

<?

list($n, $m, $s, $d) = explode(' ', fgets(STDIN));

if ($d < 2) {
    die('IMPOSSIBLE');
}

$a = explode(' ', fgets(STDIN)); // Координаты препятствий

sort($a, SORT_NUMERIC);

$x = 0;
$result = [];
for ($i = 0; $i < $n; $i++) {
    // Может ли разогнаться?
    if ($a[$i] - $x > $s) {
        $result[] = 'RUN ' . ($a[$i] - $x - 1);
    } else {
        die('IMPOSSIBLE');
    }

    $x = $a[$i] - 1;

    // Сколько ещё может пролететь за прыжок?
    while (($i + 1 < $n)
            && ($a[$i + 1] - $x < $d)
            && ($a[$i + 1] - $a[$i] - 1 <= $s)) {
        $i++;
    }

    $result[] = 'JUMP ' . ($a[$i] - $x + 1);

    $x = $a[$i] + 1;
}

if ($x < $m) {
    $result[] = 'RUN ' . ($m - $x);
}

echo implode(chr(10), $result);

<?

list($n, $m, $s, $d) = explode(' ', fgets(STDIN));

if ($d < 2) {
    die('IMPOSSIBLE');
}

$a = explode(' ', fgets(STDIN)); // Координаты препятствий

sort($a, SORT_NUMERIC);

$x = 0;
$result = [];
for ($i = 0; $i < $n; $i++) {
    // Может ли разогнаться?
    if ($a[$i] - $x > $s) {
        $result[] = 'RUN ' . ($a[$i] - $x - 1);
    } else {
        die('IMPOSSIBLE');
    }

    $x = $a[$i] - 1;

    // Сколько ещё может пролететь за прыжок?
    while (($i + 1 < $n)
            && ($a[$i + 1] - $x < $d)
            && ($a[$i + 1] - $a[$i] <= $s)) {
        $i++;
    }

    $result[] = 'JUMP ' . ($a[$i] - $x + 1);

    $x = $a[$i] + 1;
}

if ($x < $m) {
    $result[] = 'RUN ' . ($m - $x);
}

echo implode(chr(10), $result);

<?

list($n, $m, $s, $d) = explode(' ', fgets(STDIN));

if ($d < 2) {
    die('IMPOSSIBLE');
}

$a = explode(' ', fgets(STDIN)); // Координаты препятствий

sort($a, SORT_NUMERIC);

$x = 0;
$result = [];
for ($i = 0; $i < $n; $i++) {
    // Может ли разогнаться?
    if ($a[$i] - $x > $s) {
        $result[] = 'RUN ' . ($a[$i] - $x - 1);
    } else {
        die('IMPOSSIBLE');
    }

    $x = $a[$i] - 1;

    // Сколько ещё может пролететь за прыжок?
    while ($i + 1 < $n && $a[$i + 1] - $x < $d) {
        $i++;
    }

    $result[] = 'JUMP ' . ($a[$i] - $x + 1);

    $x = $a[$i] + 1;
}

if ($x < $m) {
    $result[] = 'RUN ' . ($m - $x);
}

echo implode(chr(10), $result);

<?

list($n, $m, $s, $d) = explode(' ', fgets(STDIN));

if ($d < 2) {
    die('IMPOSSIBLE');
}

$a = explode(' ', fgets(STDIN)); // Координаты препятствий

sort($a, SORT_NUMERIC);

$x = 0;
$result = [];
for ($i = 0; $i < $n; $i++) {
    // Может ли разогнаться?
    if ($a[$i] - $x > $s) {
        $result[] = 'RUN ' . ($a[$i] - $x - 1);
    } else {
        die('IMPOSSIBLE');
    }

    $x = $a[$i] - 1;

    // Сколько ещё может пролететь за прыжок?
    while (($i + 1 < $n)
            && ($a[$i + 1] - $x < $d)
            && ($a[$i + 1] - $a[$i] < $s)) {
        $i++;
    }

    $result[] = 'JUMP ' . ($a[$i] - $x + 1);

    $x = $a[$i] + 1;
}

if ($x < $m) {
    $result[] = 'RUN ' . ($m - $x);
}

echo implode(chr(10), $result);


Bob has a rectangular chocolate bar of the size W × H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.

The first line contains 3 integers W, H and n (1 ≤ W, H, n ≤ 100) — width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 — coordinates of the endpoints of the i-th break (0 ≤ xi, 1 ≤ xi, 2 ≤ W, 0 ≤ yi, 1 ≤ yi, 2 ≤ H, or xi, 1 = xi, 2, or yi, 1 = yi, 2). Breaks are given in arbitrary order. It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.

Output n + 1 numbers — areas of the resulting parts in the increasing order.

<?php

class Rect {
	function __construct($x1, $y1, $x2, $y2) {
		$this->x1 = $x1;
		$this->y1 = $y1;
		$this->x2 = $x2;
		$this->y2 = $y2;
	}
	function area() {
		return ($this->x2 - $this->x1) * ($this->y2 - $this->y1);
	}
}

fscanf(STDIN, "%d %d %d", $W, $H, $n);
$cuts = array();
for ($i = 0; $i < $n; $i++) {
	$cut = new Rect(0, 0, 0, 0);
	fscanf(STDIN, "%d %d %d %d", $cut->x1, $cut->y1, $cut->x2, $cut->y2);
	array_push($cuts, $cut);
}

$a = array();
array_push($a, new Rect(0, 0, $W, $H));
while (count($a) < $n + 1) {
	foreach ($cuts as $cut) {
		$b = array();
		foreach ($a as $rect) {
			if ($cut->x1 == $cut->x2) {
				if ($rect->x1 < $cut->x1 && $cut->x1 < $rect->x2 &&
					$rect->y1 == $cut->y1 && $rect->y2 == $cut->y2) {
					array_push($b, new Rect($rect->x1, $rect->y1, $cut->x1, $rect->y2));
					$rect->x1 = $cut->x1;
				}
			} else {
				if ($rect->y1 < $cut->y1 && $cut->y1 < $rect->y2 &&
					$rect->x1 == $cut->x1 && $rect->x2 == $cut->x2) {
					array_push($b, new Rect($rect->x1, $rect->y1, $rect->x2, $cut->y1));
					$rect->y1 = $cut->y1;
				}
			}
			array_push($b, $rect);
		}
		$a = $b;
	}
}
$s = array_map(function($rect) { return $rect->area();}, $a);
sort($s);
print(implode(" ", $s)."\n");

?>

<?php

class Rect {
	function __construct($x1, $y1, $x2, $y2) {
		$this->x1 = $x1;
		$this->y1 = $y1;
		$this->x2 = $x2;
		$this->y2 = $y2;
	}
	function area() {
		return ($this->x2 - $this->x1) * ($this->y2 - $this->y1);
	}
}

fscanf(STDIN, "%d %d %d", $W, $H, $n);
$a = array();
array_push($a, new Rect(0, 0, $W, $H));
for ($i = 0; $i < $n; $i++) {
	fscanf(STDIN, "%d %d %d %d", $x1, $y1, $x2, $y2);
	$b = array();
	foreach ($a as $rect) {
		if ($x1 == $x2) {
			if ($rect->x1 < $x1 && $x1 < $rect->x2 && $rect->y1 == $y1 && $rect->y2 == $y2) {
				array_push($b, new Rect($rect->x1, $rect->y1, $x1, $rect->y2));
				$rect->x1 = $x1;
			}
		} else {
			if ($rect->y1 < $y1 && $y1 < $rect->y2 && $rect->x1 == $x1 && $rect->x2 == $x2) {
				array_push($b, new Rect($rect->x1, $rect->y1, $rect->x2, $y1));
				$rect->y1 = $y1;
			}
		}
		array_push($b, $rect);
	}
	$a = $b;
}
$s = array_map(function($rect) { return $rect->area();}, $a);
sort($s);
print(implode(" ", $s)."\n");

?>


You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.You know that you have $$$n$$$ bacteria in the Petri dish and size of the $$$i$$$-th bacteria is $$$a_i$$$. Also you know intergalactic positive integer constant $$$K$$$.The $$$i$$$-th bacteria can swallow the $$$j$$$-th bacteria if and only if $$$a_i > a_j$$$ and $$$a_i \le a_j + K$$$. The $$$j$$$-th bacteria disappear, but the $$$i$$$-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria $$$i$$$ can swallow any bacteria $$$j$$$ if $$$a_i > a_j$$$ and $$$a_i \le a_j + K$$$. The swallow operations go one after another.For example, the sequence of bacteria sizes $$$a=[101, 53, 42, 102, 101, 55, 54]$$$ and $$$K=1$$$. The one of possible sequences of swallows is: $$$[101, 53, 42, 102, \underline{101}, 55, 54]$$$ $$$\to$$$ $$$[101, \underline{53}, 42, 102, 55, 54]$$$ $$$\to$$$ $$$[\underline{101}, 42, 102, 55, 54]$$$ $$$\to$$$ $$$[42, 102, 55, \underline{54}]$$$ $$$\to$$$ $$$[42, 102, 55]$$$. In total there are $$$3$$$ bacteria remained in the Petri dish.Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.

The first line contains two space separated positive integers $$$n$$$ and $$$K$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le K \le 10^6$$$) — number of bacteria and intergalactic constant $$$K$$$. The second line contains $$$n$$$ space separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — sizes of bacteria you have.

Print the only integer — minimal possible number of bacteria can remain.

NoteThe first example is clarified in the problem statement.In the second example an optimal possible sequence of swallows is: $$$[20, 15, 10, 15, \underline{20}, 25]$$$ $$$\to$$$ $$$[20, 15, 10, \underline{15}, 25]$$$ $$$\to$$$ $$$[20, 15, \underline{10}, 25]$$$ $$$\to$$$ $$$[20, \underline{15}, 25]$$$ $$$\to$$$ $$$[\underline{20}, 25]$$$ $$$\to$$$ $$$[25]$$$.In the third example no bacteria can swallow any other bacteria.


A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ of length $$$n$$$ containing each number exactly once. For example, $$$[1]$$$, $$$[4, 3, 5, 1, 2]$$$, $$$[3, 2, 1]$$$ are permutations, and $$$[1, 1]$$$, $$$[0, 1]$$$, $$$[2, 2, 1, 4]$$$ are not.There was a permutation $$$p[1 \dots n]$$$. It was merged with itself. In other words, let's take two instances of $$$p$$$ and insert elements of the second $$$p$$$ into the first maintaining relative order of elements. The result is a sequence of the length $$$2n$$$.For example, if $$$p=[3, 1, 2]$$$ some possible results are: $$$[3, 1, 2, 3, 1, 2]$$$, $$$[3, 3, 1, 1, 2, 2]$$$, $$$[3, 1, 3, 1, 2, 2]$$$. The following sequences are not possible results of a merging: $$$[1, 3, 2, 1, 2, 3$$$], [$$$3, 1, 2, 3, 2, 1]$$$, $$$[3, 3, 1, 2, 2, 1]$$$.For example, if $$$p=[2, 1]$$$ the possible results are: $$$[2, 2, 1, 1]$$$, $$$[2, 1, 2, 1]$$$. The following sequences are not possible results of a merging: $$$[1, 1, 2, 2$$$], [$$$2, 1, 1, 2]$$$, $$$[1, 2, 2, 1]$$$.Your task is to restore the permutation $$$p$$$ by the given resulting sequence $$$a$$$. It is guaranteed that the answer exists and is unique.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 400$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of permutation. The second line of the test case contains $$$2n$$$ integers $$$a_1, a_2, \dots, a_{2n}$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the array $$$a$$$ represents the result of merging of some permutation $$$p$$$ with the same permutation $$$p$$$.

For each test case, print the answer: $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$), representing the initial permutation. It is guaranteed that the answer exists and is unique.

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = trim(fgets(STDIN));
     $c = explode(" ", trim(fgets(STDIN)));
     $d = array();
     $e = array();
     for($y = 0; $y < $b * 2; $y++)
     {
          if($d[$c[$y]] == FALSE)
          {
               $d[$c[$y]] = TRUE;
               $e[count($e)] = $c[$y];
          }
     }
     print implode(" ", $e) . "\n";
}
?>


Do you know a story about the three musketeers? Anyway, you will learn about its origins now.Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.

The first line contains two space-separated integers, n and m (3 ≤ n ≤ 4000, 0 ≤ m ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other. i-th of the following m lines contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.

If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes).

NoteIn the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn't know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.In the second sample there is no triple of warriors knowing each other.

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

foreach ($aArray as $w => $kw) {
    $ak = array_keys($kw);
    for ($i = 0; $i < $kArray[$w]; $i++) {
        if ($ak[$i]<$w) {
            for ($j = $i+1; $j < $kArray[$w]; $j++) {
                if (isset($aArray[$ak[$i]][$ak[$j]])) {
                    $sum = $kArray[$w] + $kArray[$ak[$i]] + $kArray[$ak[$j]] - 6;
                    $result = min($result, $sum);
                }
            }
        }
    }
}

echo $result == 2000000 ? "-1" : $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

foreach ($aArray as $w => $kw) {
    $ak = array_keys($kw);
    for ($i = 0; $i < $kArray[$w]; $i++) {
        for ($j = $i+1; $j < $kArray[$w]; $j++) {
            $I = $ak[$i];
            $J = $ak[$j];
            if (isset($aArray[$I][$J])) {
                $sum = $kArray[$w] + $kArray[$I] + $kArray[$J] - 6;
                $result = min($result, $sum);
            }
        }
    }
}

echo $result == 2000000 ? "-1" : $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

foreach ($aArray as $w => $kw) {
    foreach ($kw as $i=>$v) {
        foreach ($kw as $j=>$v2) {
            if (isset($aArray[$i][$j])) {
                $sum = $kArray[$w] + $kArray[$i] + $kArray[$j] - 6;
                $result = min($result, $sum);
            }
        }
    }

}

echo $result == 2000000 ? "-1" : $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

foreach ($aArray as $w => $kw) {
    $ak = array_keys($kw);
    for ($i = 0; $i < $kArray[$w]; $i++) {
        for ($j = $i+1; $j < $kArray[$w]; $j++) {
            if (isset($aArray[$ak[$i]][$ak[$j]])) {
                $sum = $kArray[$w] + $kArray[$ak[$i]] + $kArray[$ak[$j]] - 6;
                $result = min($result, $sum);
            }
        }
    }
}

echo $result == 2000000 ? "-1" : $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
$vArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    $vArray[$i] = array($x, $y);
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

for ($i = 0; $i < $q; $i++) {
    $has = array();
    $x = $vArray[$i][0];
    $y = $vArray[$i][1];

    foreach ($aArray[$x] as $t=>$f) {
        if (isset($aArray[$y][$t])) {
            $result = min($result, $kArray[$x] + $kArray[$y] + $kArray[$t]-6);
        }
    }

}
echo $result == 2000000 ? "-1" : $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

foreach ($aArray as $w => $kw) {
    $ak = array_keys($kw);
    for ($i = 0; $i < $kArray[$w]; $i++) {
        if ($ak[$i]>$w) {
            for ($j = $i+1; $j < $kArray[$w]; $j++) {
                if (isset($aArray[$ak[$i]][$ak[$j]])) {
                    $sum = $kArray[$w] + $kArray[$ak[$i]] + $kArray[$ak[$j]] - 6;
                    $result = min($result, $sum);
                }
            }
        }
    }
}

echo $result == 2000000 ? "-1" : $result;

<?php
$knights_arr = array();
$groups = array();
$recognation = array();
$r = 0;

$file = STDIN;

list($vNumber, $q) = explode(" ", trim(fgets($file)));

for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));

	if(empty($knights_arr[$x])){
		$knights_arr[$x] = array();
	}
	$knights_arr[$x][] = $y;
	if(empty($knights_arr[$y])){
		$knights_arr[$y] = array();
	}
	$knights_arr[$y][] = $x;

}

ksort($knights_arr,1);
foreach($knights_arr as $key => $arr){
	if ( count($arr) < 2 ) {
		continue;
	}
	sort($arr);
	foreach($arr as $i => $sec){
		if($sec < $key || count($knights_arr[$sec]) < 2 || ($i == count($arr)-1) ){
			continue;
		}
		$rem_arr = array_slice($arr, $i+1);
		foreach($rem_arr as $thrd){
			if($thrd > $sec && in_array($key, $knights_arr[$thrd]) && in_array($sec, $knights_arr[$thrd])){
				$group[$r] =  "$key-$sec-$thrd";
				$recognation[$r] = count($knights_arr[$key]) + count($knights_arr[$sec]) + count($knights_arr[$thrd]) - 6;
				$r++;
			}
		}
	}
}
if($r == 0){
	echo -1;
}else{
	echo min($recognation);
}

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    if ($x > $y) {
        $aArray[$x][$y] = 1;
    } else {
        $aArray[$y][$x] = 1;
    }
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

foreach ($aArray as $w => $kw) {
    $ak = array_keys($kw);
    $cw = count($kw);
    for ($i = 0; $i < $cw; $i++) {
        for ($j = $i + 1; $j < $cw; $j++) {
            if (isset($aArray[$ak[$i]])) {
                if (isset($aArray[$ak[$i]][$ak[$j]])) {
                    $sum = $kArray[$w] + $kArray[$ak[$i]] + $kArray[$ak[$j]] - 6;
                    $result = min($result, $sum);
                }
            } elseif (isset($aArray[$ak[$j]])) {
                if (isset($aArray[$ak[$j]][$ak[$i]])) {
                    $sum = $kArray[$w] + $kArray[$ak[$i]] + $kArray[$ak[$j]] - 6;
                    $result = min($result, $sum);
                }
            }
        }
    }
}

echo $result == 2000000 ? "-1" : $result;

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
$vArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    $vArray[$i] = array($x, $y);
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;
for ($i = 0; $i < $q; $i++) {
    foreach (array_intersect($aArray[$vArray[$i][0]], $aArray[$vArray[$i][1]]) as $z) {
        $sum = $kArray[$vArray[$i][0]] + $kArray[$vArray[$i][1]] + $kArray[$z] - 6;
        $result = min($result, $sum);
    }

}
echo $result == 2000000 ? "-1" : $result;

<?php
$file = fopen('input.in', 'r');
//$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
$vArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][] = $y;
    $aArray[$y][] = $x;
    $vArray[$i] = array($x, $y);
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

for ($i = 0; $i < $q; $i++) {
    $ai = array_intersect($aArray[$vArray[$i][0]], $aArray[$vArray[$i][1]]);
    foreach ($ai as $z) {
        $sum = $kArray[$vArray[$i][0]] + $kArray[$vArray[$i][1]] + $kArray[$z] - 6;
        $result = min($result, $sum);
    }

}
echo $result == 2000000 ? "-1" : $result;

<?php
$file = fopen('input.in', 'r');
//$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$aArray = array();
$kArray = array();
$vArray = array();
for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $aArray[$x][$y] = 1;
    $aArray[$y][$x] = 1;
    $vArray[$i] = array($x, $y);
    if (isset($kArray[$x])) {
        $kArray[$x]++;
    } else {
        $kArray[$x] = 1;
    }
    if (isset($kArray[$y])) {
        $kArray[$y]++;
    } else {
        $kArray[$y] = 1;
    }
}

$result = 2000000;

for ($i = 0; $i < $q; $i++) {
    $has = array();
    $x = $vArray[$i][0];
    $y = $vArray[$i][1];

    foreach ($aArray[$x] as $t=>$f) {
        if (isset($aArray[$y][$t])) {
            $result = min($result, $kArray[$x] + $kArray[$y] + $kArray[$t]-6);
        }
    }

}
echo $result == 2000000 ? "-1" : $result;

<?php
$knights_arr = array();
$groups = array();
$recognation = array();
$r = 0;

$file = STDIN;

list($vNumber, $q) = explode(" ", trim(fgets($file)));

for ($i = 0; $i < $q; $i++) {
    list($x, $y) = explode(" ", trim(fgets($file)));

if($i == 0){
		continue;
	}

	if(empty($knights_arr[$x])){
		$knights_arr[$x] = array();
	}
	$knights_arr[$x][] = $y;
	if(empty($knights_arr[$y])){
		$knights_arr[$y] = array();
	}
	$knights_arr[$y][] = $x;

}

ksort($knights_arr,1);
foreach($knights_arr as $key => $arr){
	if ( count($arr) < 2 ) {
		continue;
	}
	sort($arr);
	foreach($arr as $i => $sec){
		if($sec < $key || count($knights_arr[$sec]) < 2 || ($i == count($arr)-1) ){
			continue;
		}
		$rem_arr = array_slice($arr, $i+1);
		foreach($rem_arr as $thrd){
			if($thrd > $sec && in_array($key, $knights_arr[$thrd]) && in_array($sec, $knights_arr[$thrd])){
				$group[$r] =  "$key-$sec-$thrd";
				$recognation[$r] = count($knights_arr[$key]) + count($knights_arr[$sec]) + count($knights_arr[$thrd]) - 6;
				$r++;
			}
		}
	}
}
if($r == 0){
	echo -1;
}else{
	echo min($recognation);
}

<?php
$file = STDIN;
list($vNumber, $q) = explode(" ", trim(fgets($file)));
$lines = array('0 0');
for ($i = 0; $i < $q; $i++) {
 $lines[] = trim(fgets($file));
}

foreach ($lines as &$line) {
$line = explode(" ", $line);
}
unset($lines[0]);

$probabilities = array();
$summations = array();
for($i = 1 ; $i <= sizeof($lines); $i++){
	for($j = $i+1 ; $j <= sizeof($lines) ; $j++){
		for($k = $j+1 ; $k <= sizeof($lines) ; $k++){
			$probabilities = array();
			$probabilities[$lines[$i][0]]++;
			$probabilities[$lines[$i][1]]++;
			$probabilities[$lines[$j][0]]++;
			$probabilities[$lines[$j][1]]++;
			$probabilities[$lines[$k][0]]++;
			$probabilities[$lines[$k][1]]++;
			$counter = 0;
			foreach ($probabilities as $probability) {
				if($probability == 2)
					$counter++;
			}
			if($counter == 3){
				getSummation($probabilities, $i, $j, $k);
			}
		}
	}
}

echo min($summations);
function getSummation($probabilities, $i, $j, $k){
	global $lines;
	global $summations;
	$selection = array_keys($probabilities);
	$sum = 0;
	foreach($lines as $key => $line){
		if($key == $i || $key == $j || $key == $k)
			continue;
		if($line[0] == $selection[0] || $line[0] == $selection[1] || $line[0] == $selection[2]
			|| $line[1] == $selection[0] || $line[1] == $selection[1] || $line[1] == $selection[2])
			$sum ++;
	}
	$summations[] = $sum;
}


Anfisa the monkey learns to type. She is yet unfamiliar with the "space" key and can only type in lower-case Latin letters. Having typed for a fairly long line, Anfisa understood that it would be great to divide what she has written into k lines not shorter than a and not longer than b, for the text to resemble human speech more. Help Anfisa.

The first line contains three integers k, a and b (1 ≤ k ≤ 200, 1 ≤ a ≤ b ≤ 200). The second line contains a sequence of lowercase Latin letters — the text typed by Anfisa. It is guaranteed that the given line is not empty and its length does not exceed 200 symbols.

Print k lines, each of which contains no less than a and no more than b symbols — Anfisa's text divided into lines. It is not allowed to perform any changes in the text, such as: deleting or adding symbols, changing their order, etc. If the solution is not unique, print any of them. If there is no solution, print "No solution" (without quotes).


You are given an integer $$$n$$$ and an array $$$a_1, a_2, \ldots, a_n$$$. You should reorder the elements of the array $$$a$$$ in such way that the sum of $$$\textbf{MEX}$$$ on prefixes ($$$i$$$-th prefix is $$$a_1, a_2, \ldots, a_i$$$) is maximized.Formally, you should find an array $$$b_1, b_2, \ldots, b_n$$$, such that the sets of elements of arrays $$$a$$$ and $$$b$$$ are equal (it is equivalent to array $$$b$$$ can be found as an array $$$a$$$ with some reordering of its elements) and $$$\sum\limits_{i=1}^{n} \textbf{MEX}(b_1, b_2, \ldots, b_i)$$$ is maximized.$$$\textbf{MEX}$$$ of a set of nonnegative integers is the minimal nonnegative integer such that it is not in the set.For example, $$$\textbf{MEX}(\{1, 2, 3\}) = 0$$$, $$$\textbf{MEX}(\{0, 1, 2, 4, 5\}) = 3$$$.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 100)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(0 \le a_i \le 100)$$$.

For each test case print an array $$$b_1, b_2, \ldots, b_n$$$  — the optimal reordering of $$$a_1, a_2, \ldots, a_n$$$, so the sum of $$$\textbf{MEX}$$$ on its prefixes is maximized. If there exist multiple optimal answers you can find any.

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aAnswer = [];
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    sort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sV])) {
            $aTest[$sV] = $sV;
        } else {
            $aR[$sK] = $sV;
        }
    }
    $aAnswer = array_merge($aTest , $aR);
    foreach ($aAnswer as $sK => $sV) {
        echo $sV . ' ';
    }
    echo "\n";
}

<?php
# ╔══╗─╔═══╗╔╗╔═╗╔════╗╔═══╗╔════╗
# ║╔╗║─║╔══╝║║║╔╝╚══╗═║║╔═╗║║╔╗╔╗║
# ║╚╝╚╗║╚══╗║╚╝╝───╔╝╔╝║║─║║╚╝║║╚╝
# ║╔═╗║║╔══╝║╔╗║──╔╝╔╝─║╚═╝║──║║
# ║╚═╝║║╚══╗║║║╚╗╔╝═╚═╗║╔═╗║──║║
# ╚═══╝╚═══╝╚╝╚═╝╚════╝╚╝─╚╝──╚╝

class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
}

//$iQueryCount = 1;
$iQueryCount = IO::str();

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $n = IO::str();
    $a = IO::arr();
    sort($a);
    $b = $c = [];
    foreach ($a as $v) {
        if(isset($b[$v])) {
            $c[] = $v;
        } else {
            $b[$v] = $v;
        }
    }
    IO::line(implode(' ', $b) . ' ' . implode(' ', $c));
}

<?php

$n = readline();
$final = array();
$cnt = 0;
while($n--) {
    $res = array();
    $buf = array();
    $k = readline();
    $arr = explode(' ', readline());
    sort($arr);
    for ($i = 0; $i < $k; $i++) {
        if ($i == $k - 1) {
            $res[] = $arr[$i];
        } elseif ($arr[$i] == $arr[$i+1]) {
            $buf[] = $arr[$i];
            unset($arr[$i]);
        } else {
            $res[] = $arr[$i];
        }
    }
    $final[] = array_merge($res, $buf);
    $cnt++;
}

//print_r($final);
//exit;
//print_r($res);
//print_r($res);
foreach ($final as $r) {
    foreach ($r as $item) {
        echo $item . ' ';
    }
    echo "\n";
}

<?php
fscanf(STDIN, "%d", $n);
while ($n--) {
    fgets(STDIN);
    $a = fgetcsv(STDIN, 0, ' ');
    $b = array_unique($a);
    sort($b, SORT_NUMERIC);
    echo $b ? implode(' ', $b) : 0;
    $b = array_fill_keys($b, true);
    foreach ($a as $value) {
        if (empty($b[$value])) {
            echo ' ', $value;
        } else {
            unset($b[$value]);
        }
    }
    echo "\n";
}

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aAnswer = [];
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    sort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sK])) {
            $aTest[$sK] = $sV;
        } else {
            $aR[$sK] = $sV;
        }
    }
    $aAnswer = array_merge($aTest , $aR);
    foreach ($aAnswer as $sK => $sV) {
        echo $sV . ' ';
    }
    echo "\n";
}

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aAnswer = [];
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    sort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sV])) {
            $aTest[$sK] = $sV;
        } else {
            $aR[$sK] = $sV;
        }
    }
    $aAnswer = array_merge($aTest , $aR);
    foreach ($aAnswer as $sK => $sV) {
        echo $sV . ' ';
    }
    echo "\n";
}

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aAnswer = [];
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    sort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sV])) {
            $aTest[$sV] = $sV;
        } else {
            $aR[$sV] = $sV;
        }
    }
    $aAnswer = array_merge($aTest , $aR);
    foreach ($aAnswer as $sK => $sV) {
        echo $sV . ' ';
    }
    echo "\n";
}

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aAnswer = [];
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    asort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sV])) {
            $aTest[$sV] = $sV;
        } else {
            $aR[$sV] = $sV;
        }
    }
    $aAnswer = array_merge($aTest , $aR);
    foreach ($aAnswer as $sK => $sV) {
        echo $sV . ' ';
    }
    echo "\n";
}

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    asort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sV])) {
            $aTest[$sV] = $sV;
        } else {
            $aR[$sV] = $sV;
        }
    }
    $aAnswer = array_merge($aTest , $aR);
    echo implode(" ",$aAnswer). "\n";
}

<?php
list($n) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $n; $i++) {
    list($m) = explode(' ', trim(fgets(STDIN)));
    $aTest = [];
    $aR = [];
    $aList = explode(' ', trim(fgets(STDIN)));
    asort($aList);
    foreach ($aList as $sK => $sV) {
        if (!isset($aTest[$sV])) {
            $aTest[$sV] = $sV;
        } else {
            $aR[$sV] = $sV;
        }
    }

    echo implode(" ",$aTest) . " " . implode(" ",$aR) . "\n";
}

<?php
# ╔══╗─╔═══╗╔╗╔═╗╔════╗╔═══╗╔════╗
# ║╔╗║─║╔══╝║║║╔╝╚══╗═║║╔═╗║║╔╗╔╗║
# ║╚╝╚╗║╚══╗║╚╝╝───╔╝╔╝║║─║║╚╝║║╚╝
# ║╔═╗║║╔══╝║╔╗║──╔╝╔╝─║╚═╝║──║║
# ║╚═╝║║╚══╗║║║╚╗╔╝═╚═╗║╔═╗║──║║
# ╚═══╝╚═══╝╚╝╚═╝╚════╝╚╝─╚╝──╚╝

class IO {
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }
}

//$iQueryCount = 1;
$iQueryCount = IO::str();

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $n = IO::str();
    $a = IO::arr();
    sort($a);
    $b = $c = [];
    foreach ($a as $v) {
        if(isset($b[$v])) {
            $c[$v] = $v;
        } else {
            $b[$v] = $v;
        }
    }
    IO::line(implode(' ', $b) . ' ' . implode(' ', $c));
}


You are given a square grid with $$$n$$$ rows and $$$n$$$ columns. Each cell contains either $$$0$$$ or $$$1$$$. In an operation, you can select a cell of the grid and flip it (from $$$0 \to 1$$$ or $$$1 \to 0$$$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$ and $$$270^{\circ}$$$.The picture below shows an example of all rotations of a grid.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the size of the grid. Then $$$n$$$ lines follow, each with $$$n$$$ characters $$$a_{i,j}$$$ ($$$0 \leq a_{i,j} \leq 1$$$) — the number written in each cell.

For each test case output a single integer  — the minimum number of operations needed to make the square look the same rotated $$$0^{\circ}$$$, $$$90^{\circ}$$$, $$$180^{\circ}$$$ and $$$270^{\circ}$$$.

NoteIn the first test case, we can perform one operations to make the grid $$$\begin{matrix}0 & 1 & 0\\ 1 & 1 & \color{red}{1}\\ 0 & 1 & 0\end{matrix}$$$. Now, all rotations of the square are the same.In the second test case, all rotations of the square are already the same, so we don't need any flips.

<?php
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
//101
//101
//001
function solve()
{
    $n = IO::get();
    $ans =0;
    $a = [];
    for($i=0; $i<$n; $i++) {
        $a[] = str_split(IO::get());
    }

    for($j=0; $j<$n/2; $j++) {
        for ($i = $j; $i < $n-1-$j; $i++) {
            $k = 0;
            $temp = 0;
            $sum=$a[$j][$i]+$a[$i][$n-$j-1]+$a[$n-$j-1][$n-$i-1]+$a[$n-$i-1][$j];
           $ans += min(abs($sum), abs(4-$sum));

        }
    }

    IO::pr($ans);

}

class IO {
    public static function get()
    {
        return trim(fgets(STDIN));
    }

    public static function getArray()
    {
        return explode(' ', trim(fgets(STDIN)));
    }
    public static function pr($s)
    {
        echo $s, PHP_EOL;
    }
    public static function prArray($s)
    {
        echo implode( ' ', $s), PHP_EOL;
    }
    public static function getMinValue($x, $y) {
        return $x > $y ? $y : $x;
    }
    public static function getMaxValue($x, $y) {
        return $x > $y ? $x : $y;
    }
}


Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.

The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 ≤ n ≤ 500) — amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 ≤ Wi ≤ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.

If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.

abc
ab
6
a b 4
a b 7
b a 8
c b 11
c a 3
a c 0


Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.Each digit is allowed to occur in the number the same number of times it occurs in the set.

A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.

On a single line print the answer to the problem. If such number does not exist, then you should print -1.

NoteIn the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.

<?php
error_reporting("off");
//214B: Hometask
$n = trim(fgets(STDIN)); //$n = 11;
$digits = explode(" ", trim(fgets(STDIN))); //$digits = explode(" ", "3 9 9 6 4 3 6 4 9 6 0"); //999666330
rsort($digits);
$answer = 0;

//let's construct some sets of 1mod3 and 2mod3 digits
$onemodthree = array();
$twomodthree = array();
foreach ($digits as $value) {
    if ($value % 3 == 1) {
        $onemodthree[] = $value;
    } elseif ($value % 3 == 2) {
        $twomodthree[] = $value;
    }
}
sort($onemodthree);
sort($twomodthree);

//to be divisible by 2 and 5 the last digit has to be 0
if (!in_array(0, $digits)) { //if there's no 0 there's no way
    $answer = -1;
} else { //there's a zero
    $sum = array_sum($digits);
    if ($sum % 3 == 0) { //if it's divisible by 3, great!
        $answer = implode($digits); //also can be 00000...
    } elseif ($sum % 3 == 1) { //1mod3
        //check to delete just one digit to make it 0mod3
        if (isset($onemodthree[0])) {
            unset($digits[array_search($onemodthree[0], $digits)]);
        } else { //if you can't delete only one digit you have to delete two; it's always possible.
            unset($digits[array_search($twomodthree[0], $digits)]);
            unset($digits[array_search($twomodthree[1], $digits)]);
        }
        rsort($digits);
        $answer = implode($digits);
    } elseif ($sum % 3 == 2) { //2mod3
        //check to delete just one digit to make it 0mod3
        if (isset($twomodthree[0])) {
            unset($digits[array_search($twomodthree[0], $digits)]);
        } else { //if you can't delete only one digit you have to delete two; it's always possible.
            unset($digits[array_search($onemodthree[0], $digits)]);
            unset($digits[array_search($onemodthree[1], $digits)]);
        }
        rsort($digits);
        $answer = implode($digits);
    }
}

if (substr($answer, 0, 1) == "0") {
    $answer = 0;
}

echo $answer;
?>

<?php
error_reporting("off");
//214B: Hometask
$n = trim(fgets(STDIN)); //$n = 6;
$digits = explode(" ", trim(fgets(STDIN))); //$digits = explode(" ", "1 0 0 0 0 0");
rsort($digits);
$answer = 0;

//let's construct some sets of 1mod3 and 2mod3 digits
$onemodthree = array();
$twomodthree = array();
foreach ($digits as $value) {
    if ($value % 3 == 1) {
        $onemodthree[] = $value;
    } elseif ($value % 3 == 2) {
        $twomodthree[] = $value;
    }
}
sort($onemodthree);
sort($twomodthree);

//to be divisible by 2 and 5 the last digit has to be 0
if (!in_array(0, $digits)) { //if there's no 0 there's no way
    $answer = -1;
} else { //there's a zero
    $sum = array_sum($digits);
    if ($sum % 3 == 0) { //if it's divisible by 3, great!
        $answer = implode($digits); //also can be 00000...
    } elseif ($sum % 3 == 1) { //1mod3
        //check to delete just one digit to make it 0mod3
        if (isset($onemodthree[0])) {
            unset($digits[array_search($onemodthree[0], $digits)]);
        } else { //if you can't delete only one digit you have to delete two; it's always possible.
            $twounique = array_unique($twomodthree);
            unset($digits[array_search($twounique[0], $digits)]);
            unset($digits[array_search($twounique[1], $digits)]);
        }
        rsort($digits);
        $answer = implode($digits);
    } elseif ($sum % 3 == 2) { //2mod3
        //check to delete just one digit to make it 0mod3
        if (isset($twomodthree[0])) {
            unset($digits[array_search($twomodthree[0], $digits)]);
        } else { //if you can't delete only one digit you have to delete two; it's always possible.
            $oneunique = array_unique($onemodthree);
            unset($digits[array_search($oneunique[0], $digits)]);
            unset($digits[array_search($oneunique[1], $digits)]);
        }
        rsort($digits);
        $answer = implode($digits);
    }
}

if (substr($answer, 0, 1) == "0") {
    $answer = 0;
}

echo $answer;
?>


Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to ai. Authorities plan to build two cities, first for n1 people and second for n2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n1 settle in the first city and then some subset of size n2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth ai among all its residents divided by the number of them (n1 or n2 depending on the city). The division should be done in real numbers without any rounding.Please, help authorities find the optimal way to pick residents for two cities.

The first line of the input contains three integers n, n1 and n2 (1 ≤ n, n1, n2 ≤ 100 000, n1 + n2 ≤ n) — the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), the i-th of them is equal to the wealth of the i-th candidate.

Print one real value — the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.  Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

NoteIn the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (a3 + a4) / 2 + a2 = (3 + 2) / 2 + 4 = 6.5

<?php
list($a, $b, $c) = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
rsort($d);
if(($b + $c == $a) && ($b < $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b == $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b > $c))
{
     $e = array_slice($d, 0, $c);
     $f = array_slice($d, $c);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b < $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $c);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b == $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b > $c)
{
     $e = array_slice($d, 0, $c);
     $f = array_slice($d, $c, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
?>

<?php
$h = fopen("php://stdin", "r");

$line = trim(fgets($h));
$line_arr = explode(' ', $line);
$n = intval($line_arr[0]);
$n1 = intval($line_arr[1]);
$n2 = intval($line_arr[2]);

$line2 = trim(fgets($h));
$line_2_arr = explode(' ', $line2);
$arr = array_map('intval', $line_2_arr);

rsort($arr);
$total = $n1 + $n2;

$arr = array_slice($arr, 0, $total);

if ($n1 > $n2) {
	$temp = $n1;
	$n1 = $n2;
	$n2 = $temp;
}

$arr1 = array_slice($arr, 0, $n1);
$arr2 = array_slice($arr, $n1);

$res = array_sum($arr1)/$n1 + array_sum($arr2)/$n2;

echo $res;

<?php
list($a, $b, $c) = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
rsort($d);
if(($b + $c == $a) && ($b < $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b == $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b > $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = bcadd(bcdiv($g, $b), bcdiv($h, $c));
     print number_format($i, 6, '.', '');
}
elseif($b < $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $c);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b == $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b > $c)
{
     $e = array_slice($d, 0, $c);
     $f = array_slice($d, $c, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
?>

<?php
list($a, $b, $c) = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
rsort($d);
if(($b + $c == $a) && ($b < $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b == $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b > $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b < $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $c);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b == $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b > $c)
{
     $e = array_slice($d, 0, $c);
     $f = array_slice($d, $c, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
?>

<?php
list($a, $b, $c) = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
rsort($d);
if(($b + $c == $a) && ($b < $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b == $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b > $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b < $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $c);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b == $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b > $c)
{
     $e = array_slice($d, 0, $c);
     $f = array_slice($d, $c, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
?>

<?php
list($a, $b, $c) = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
rsort($d);
if(($b + $c == $a) && ($b < $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b == $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif(($b + $c == $a) && ($b > $c))
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b < $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $c);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $c);
     print number_format($i, 6, '.', '');
}
elseif($b == $c)
{
     $e = array_slice($d, 0, $b);
     $f = array_slice($d, $b, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $b) + ($h / $b);
     print number_format($i, 6, '.', '');
}
elseif($b > $c)
{
     $e = array_slice($d, 0, $c);
     $f = array_slice($d, $c, $b);
     $g = array_sum($e);
     $h = array_sum($f);
     $i = ($g / $c) + ($h / $b);
     print number_format($i, 6, '.', '');
}
?>


Recently, Norge found a string $$$s = s_1 s_2 \ldots s_n$$$ consisting of $$$n$$$ lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string $$$s$$$. Yes, all $$$\frac{n (n + 1)}{2}$$$ of them!A substring of $$$s$$$ is a non-empty string $$$x = s[a \ldots b] = s_{a} s_{a + 1} \ldots s_{b}$$$ ($$$1 \leq a \leq b \leq n$$$). For example, "auto" and "ton" are substrings of "automaton".Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only $$$k$$$ Latin letters $$$c_1, c_2, \ldots, c_k$$$ out of $$$26$$$.After that, Norge became interested in how many substrings of the string $$$s$$$ he could still type using his broken keyboard. Help him to find this number.

The first line contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq k \leq 26$$$) — the length of the string $$$s$$$ and the number of Latin letters still available on the keyboard. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters. The third line contains $$$k$$$ space-separated distinct lowercase Latin letters $$$c_1, c_2, \ldots, c_k$$$ — the letters still available on the keyboard.

Print a single number — the number of substrings of $$$s$$$ that can be typed using only available letters $$$c_1, c_2, \ldots, c_k$$$.

<?php
list($n, $k) = explode(' ', trim(fgets(STDIN)));
list($s) = explode(' ', trim(fgets(STDIN)));
$aArray = explode(' ', trim(fgets(STDIN)));
$iCount = 0;
$j = 0;
for($i = 0; $i < strlen($s); $i++){
    if(in_array($s[$i],$aArray)){
        $j = $j + 1;
    } else {
        $iCount += ($j*($j + 1))/2;
        $j = 0;
    }
    if ($i == (strlen($s) - 1) && $j > 0) {
        $iCount += ($j*($j + 1))/2;
        $j = 0;
    }
}
echo $iCount . "\n";


Innocentius has a problem — his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus.Innocentius is touch-typing a program that paints a white square one-pixel wide frame on the black screen. As the monitor is broken, some pixels that should be white remain black. Polycarpus should look at what the program displayed on the screen and guess the position and size of the frame Innocentius has painted. Polycarpus doesn't like the game but Innocentius persuaded brother to play as "the game is good for the imagination and attention".Help Polycarpus, automatize his part in the gaming process. Write the code that finds such possible square frame that:  the frame's width is 1 pixel,  the frame doesn't go beyond the borders of the screen,  all white pixels of the monitor are located on the frame,  of all frames that satisfy the previous three conditions, the required frame must have the smallest size. Formally, a square frame is represented by such pixels of the solid square, that are on the square's border, that is, are not fully surrounded by the other pixels of the square. For example, if the frame's size is d = 3, then it consists of 8 pixels, if its size is d = 2, then it contains 4 pixels and if d = 1, then the frame is reduced to a single pixel.

The first line contains the resolution of the monitor as a pair of integers n, m (1 ≤ n, m ≤ 2000). The next n lines contain exactly m characters each — the state of the monitor pixels at the moment of the game. Character "." (period, ASCII code 46) corresponds to the black pixel, and character "w" (lowercase English letter w) corresponds to the white pixel. It is guaranteed that at least one pixel of the monitor is white.

Print the monitor screen. Represent the sought frame by characters "+" (the "plus" character). The pixels that has become white during the game mustn't be changed. Print them as "w". If there are multiple possible ways to position the frame of the minimum size, print any of them. If the required frame doesn't exist, then print a single line containing number -1.

NoteIn the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist.


Vasya has started watching football games. He has learned that for some fouls the players receive yellow cards, and for some fouls they receive red cards. A player who receives the second yellow card automatically receives a red card.Vasya is watching a recorded football match now and makes notes of all the fouls that he would give a card for. Help Vasya determine all the moments in time when players would be given red cards if Vasya were the judge. For each player, Vasya wants to know only the first moment of time when he would receive a red card from Vasya.

The first line contains the name of the team playing at home. The second line contains the name of the team playing away. Both lines are not empty. The lengths of both lines do not exceed 20. Each line contains only of large English letters. The names of the teams are distinct. Next follows number n (1 ≤ n ≤ 90) — the number of fouls.  Each of the following n lines contains information about a foul in the following form:    first goes number t (1 ≤ t ≤ 90) — the minute when the foul occurs;  then goes letter "h" or letter "a" — if the letter is "h", then the card was given to a home team player, otherwise the card was given to an away team player;  then goes the player's number m (1 ≤ m ≤ 99);  then goes letter "y" or letter "r" — if the letter is "y", that means that the yellow card was given, otherwise the red card was given.  The players from different teams can have the same number. The players within one team have distinct numbers. The fouls go chronologically, no two fouls happened at the same minute.

For each event when a player received his first red card in a chronological order print a string containing the following information:   The name of the team to which the player belongs;  the player's number in his team;  the minute when he received the card.  If no player received a card, then you do not need to print anything. It is possible case that the program will not print anything to the output (if there were no red cards).

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$h = array();
array_unshift($h, " ");
unset($h[0]);
$i = array();
array_unshift($i, " ");
unset($i[0]);
$j = array();
$l = array();
array_unshift($l, " ");
unset($l[0]);
for($x = 1; $x <= $c; $x++)
{
    list($d, $e, $f, $g) = explode(" ", trim(fgets(STDIN)));
    if($g == "r")
    {
        $k = array_search($e . $f . $g, $h);
        $n = array_search($e . $f, $l);
        if(($k == FALSE) && ($n == FALSE))
        {
            if($e == "a")
            {
                array_push($j, $b . " " . $f . " " . $d);
            }
            else
            {
                array_push($j, $a . " " . $f . " " . $d);
            }
        }
        array_push($h, $e . $f . $g);
        array_push($i, "0");
        array_push($l, $e . $f);
    }
    else
    {
        $k = array_search($e . $f . $g, $h);
        $m = array_search($e . $f, $l);
        if(($k == TRUE) && ($i[$k] == 0) && ($l[$m] == FALSE))
        {
            $i[$k] = 1;
            array_push($l, $e . $f);
            if($e == "a")
            {
                array_push($j, $b . " " . $f . " " . $d);
            }
            else
            {
                array_push($j, $a . " " . $f . " " . $d);
            }
        }
        else
        {
            array_push($h, $e . $f . $g);
            array_push($i, "0");
        }
    }
}
for($x = 0; $x < count($j) - 1; $x++)
{
    print $j[$x] . "\n";
}
print $j[$x];
?>

<?php
	$fh = fopen("php://stdin", "r");
	$home = fgets($fh);
	$away = fgets($fh);
	$ncase = (int)(fgets($fh));
	$cases = array();
	for($i=0;$i<$ncase;$i++)
	{
		$cases[] = fgets($fh);
	}

	$reds = array();
    $yellow = array();
	foreach($cases as $case)
	{
		$caseAr =  explode(" ",$case);
		$player =  $caseAr[1].$caseAr[2];
		$time   =  $caseAr[0];
		$card   =  trim($caseAr[3]);
		if(! array_key_exists($player,$reds))
		{
			if(($card == "r") || (array_key_exists($player, $yellow)))
			{
				$team = ($caseAr[1]=="h") ?"$home":"$away";
				$nos  = $caseAr[2];
				$time = $caseAr[0];
				echo trim($team).' '.$nos.' '.$time."\n";
				$reds[$player] = $time;
			}
			else
			{
				$yellow[$player] = $time;
			}
		}

	}

	/*
	ASGDFJH
AHGRSDXGER
3
23 h 15 r
68 h 15 y
79 h 15 y
Output
ASGDFJH 15 79
Answer
ASGDFJH 15 23
Checker Log
wrong answer 1st lines differ - expected: 'ASGDFJH 15 23', found: 'ASGDFJH 15 79'

*/

<?php
class fl{
var $numq;
var $time;
var $team;
var $count;
var $out;
function __construct($a,$b,$c,$d){
$this->numq = $a;
$this->time = $b;
$this->team = $c;
$this->count += $d;
$this->out = true;
$this->rec();
}
function upd($a,$b){
$this->time = $a;
$this->count += $b;
$this->rec();
}
function rec(){
if($this->count >= 2 && $this->out){
printf ("%s %d %d\n",$this->team,$this->numq,$this->time);
$this->out = false;
}
}
}
$name = array();
$name_h = trim(fgets(STDIN));
$name_a = trim(fgets(STDIN));
$fls = array();
$n = trim(fgets(STDIN));
for ($i=0;$i<$n;$i++){
list($t,$a,$m,$b) = explode(" ",trim(fgets(STDIN)));
$b = ($b=='r')? 2 : 1;
$a = ($a=='h')? $name_h : $name_a;
if (!isset($fls[$a.$m])){
$fls[$a.$m]= new fl($m,$t,$a,$b);
}else{
$fls[$a.$m]->upd($t,$b);
}
}

<?php
$arr2 = array();

class calc {
    var $number;
    var $time;
    var $team;
    var $count;
    var $out;
    function __construct($a,$b,$c,$d){
        $this->number = $a;
        $this->time = $b;
        $this->team = $c;
        $this->count += $d;
        $this->output = true;
        $this->record();
    }
    function update($a,$b){
        $this->time = $a;
        $this->count += $b;
        $this->record();
    }
    function record(){
        global $arr2;
        if ($this->count >= 2 && $this->output){
            $arr2[$this->time] = sprintf("%s %d %d\n",$this->team,$this->number,$this->time);
            $this->output = false;
        }
    }
}

$name = array();
$name_h = trim(fgets(STDIN));
$name_a = trim(fgets(STDIN));
$n = trim(fgets(STDIN));

$arr = array();
for ($i=0; $i<$n; $i++) {
    list($t,$a,$m,$b) = explode(" ",trim(fgets(STDIN)));
    $b = ($b == 'r')? 2 : 1;
    $a = ($a == 'h')? $name_h : $name_a;
    if (!isset($arr[$a.$m])) {
        $arr[$a.$m] = new calc($m,$t,$a,$b);
    } else {
        $arr[$a.$m]->update($t,$b);
    }
}
ksort($arr2);
foreach ($arr2 as $value){
    echo $value;
}
?>

<?php
$file = fopen("php://stdin", "r");
// $file = fopen("493A.txt", "r");
$teamName["h"] = trim(fgets($file));
$teamName["a"] = trim(fgets($file));
$round = trim(fgets($file));

$history = array();
for($i=0;$i<$round;$i++){
    $perHis = fgets($file);

    //place==h||a , card==y||r
    $base = explode(" ", $perHis);
    $time = $base[0];
    $place = $base[1];
    $number = $base[2];
    $card = trim($base[3]);
    $key = $place.$number;

    if($card == "r"){
        if(array_key_exists($key, $history)){
            if($history[$key]==1){
                echo $teamName[$place]." ".$number." ".$time."\n";
                $history[$key] = 2;
            }
        }else{
            echo $teamName[$place]." ".$number." ".$time."\n";
                $history[$key] = 2;
        }
    }else if($card =="y"){
        if(array_key_exists($key,$history)){
            if($history[$key]==1){
                $history[$key] =2;
                echo $teamName[$place]." ".$number." ".$time."\n";
            }else if($history[$key]==1){
                echo $teamName[$place]." ".$number." ".$time."\n";
            }
        }else{
            $history[$key] = 1;
        }
    }
}

?>

<?php
class flclass{
var $num;
var $time;
var $team;
var $count;
var $out;
function __construct($a,$b,$c,$d){
$this->num = $a;
$this->time = $b;
$this->team = $c;
$this->count += $d;
$this->out = true;
$this->rec();
}
function upd($a,$b){
$this->time = $a;
$this->count += $b;
$this->rec();
}
function rec(){
if($this->count >= 2 && $this->out){
printf ("%s %d %d\n",$this->team,$this->num,$this->time);
$this->out = false;
}
}
}
$name__ = array();
$name___h = trim(fgets(STDIN));
$name___a = trim(fgets(STDIN));
$flclasss = array();
$n = trim(fgets(STDIN));
for ($i=0;$i<$n;$i++){
list($t,$a,$m,$b) = explode(" ",trim(fgets(STDIN)));
$b = ($b=='r')? 2 : 1;
$a = ($a=='h')? $name___h : $name___a;
if (!isset($flclasss[$a.$m])){
$flclasss[$a.$m]= new flclass($m,$t,$a,$b);
}else{
$flclasss[$a.$m]->upd($t,$b);
}
}
?>

<?php
	$fls2 = array();
	class fl{
		var $num;
		var $time;
		var $team;
		var $count;
		var $out;
		function __construct($a,$b,$c,$d){
			$this->num = $a;
			$this->time = $b;
			$this->team = $c;
			$this->count += $d;
			$this->out = true;
			$this->rec();
		}
		function upd($a,$b){
			$this->time = $a;
			$this->count += $b;
			$this->rec();
		}
		function rec(){
			global $fls2;
			if($this->count >= 2 && $this->out){
				$fls2[$this->time] = sprintf ("%s %d %d\n",$this->team,$this->num,$this->time);
				$this->out = false;
			}
		}
	}
	$name_h = trim(fgets(STDIN));
	$name_a = trim(fgets(STDIN));
	$fls = array();
	$n = trim(fgets(STDIN));
	for ($i=0;$i<$n;$i++){
		list($t,$a,$m,$b) = explode(" ",trim(fgets(STDIN)));
		$b = ($b=='r')? 2 : 1;
		$a = ($a=='h')? $name_h : $name_a;
		if (!isset($fls[$a.$m])){
			$fls[$a.$m]= new fl($m,$t,$a,$b);
		}else{
			$fls[$a.$m]->upd($t,$b);
		}
	}
	ksort($fls2);
	foreach($fls2 as $fl){
		echo $fl;
	}
?>

<?php
class fl{
var $num;
var $time;
var $team;
var $count;
var $out;
function __construct($a1,$b,$c,$d){
$this->num = $a1;
$this->time = $b;
$this->team = $c;
$this->count += $d;
$this->out = true;
$this->rec();
}
function upd($a1,$b){
$this->time = $a1;
$this->count += $b;
$this->rec();
}
function rec(){
if($this->count >= 2 && $this->out){
printf ("%s %d %d\n",$this->team,$this->num,$this->time);
$this->out = false;
}
}
}
$name = array();
$name_h = trim(fgets(STDIN));
$name_a = trim(fgets(STDIN));
$fls = array();
$n = trim(fgets(STDIN));
for ($i=0;$i<$n;$i++){
list($t,$a1,$m,$b) = explode(" ",trim(fgets(STDIN)));
$b = ($b=='r')? 2 : 1;
$a1 = ($a1=='h')? $name_h : $name_a;
if (!isset($fls[$a1.$m])){
$fls[$a1.$m]= new fl($m,$t,$a1,$b);
}else{
$fls[$a1.$m]->upd($t,$b);
}
}
?>

<?php
 class fl{
  var $avfhk;
  var $loinvds;
  var $pknndfj;
  var $hbjbskblj;
  var $hbhhnbnbnk;
  function __construct($a,$b,$c,$d){
   $this->avfhk = $a;
   $this->loinvds = $b;
   $this->pknndfj = $c;
   $this->hbjbskblj += $d;
   $this->hbhhnbnbnk = true;
   $this->rec();
  }
  function upd($a,$b){
   $this->loinvds = $a;
   $this->hbjbskblj += $b;
   $this->rec();
  }
  function rec(){
   if($this->hbjbskblj >= 2 && $this->hbhhnbnbnk){
    printf ("%s %d %d\n",$this->pknndfj,$this->avfhk,$this->loinvds);
    $this->hbhhnbnbnk = false;
   }
  }
 }
 $omnomnom = array();
 $omnomnom_h = trim(fgets(STDIN));
 $omnomnom_a = trim(fgets(STDIN));
 $fls = array();
 $n = trim(fgets(STDIN));
 for ($i=0;$i<$n;$i++){
  list($t,$a,$m,$b) = explode(" ",trim(fgets(STDIN)));
  $b = ($b=='r')? 2 : 1;
  $a = ($a=='h')? $omnomnom_h : $omnomnom_a;
  if (!isset($fls[$a.$m])){
   $fls[$a.$m]= new fl($m,$t,$a,$b);
  }else{
   $fls[$a.$m]->upd($t,$b);
  }
 }
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$h = array();
array_unshift($h, " ");
unset($h[0]);
$i = array();
array_unshift($i, " ");
unset($i[0]);
$j = array();
$l = array();
array_unshift($l, " ");
unset($l[0]);
for($x = 1; $x <= $c; $x++)
{
    list($d, $e, $f, $g) = explode(" ", trim(fgets(STDIN)));
    if($g == "r")
    {
        $k = array_search($e . $f . $g, $h);
        if($k == FALSE)
        {
            if($e == "a")
            {
                array_push($j, $b . " " . $f . " " . $d);
            }
            else
            {
                array_push($j, $a . " " . $f . " " . $d);
            }
        }
        array_push($h, $e . $f . $g);
        array_push($i, "0");
        array_push($l, $e . $f);
    }
    else
    {
        $k = array_search($e . $f . $g, $h);
        $m = array_search($e . $f, $l);
        if(($k == TRUE) && ($i[$k] == 0) && ($l[$m] == FALSE))
        {
            $i[$k] = 1;
            if($e == "a")
            {
                array_push($j, $b . " " . $f . " " . $d);
            }
            else
            {
                array_push($j, $a . " " . $f . " " . $d);
            }
        }
        else
        {
            array_push($h, $e . $f . $g);
            array_push($i, "0");
        }
    }
}
for($x = 0; $x < count($j) - 1; $x++)
{
    print $j[$x] . "\n";
}
print $j[$x];
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$h = array();
array_unshift($h, " ");
unset($h[0]);
$i = array();
array_unshift($i, " ");
unset($i[0]);
$j = array();
for($x = 1; $x <= $c; $x++)
{
    list($d, $e, $f, $g) = explode(" ", trim(fgets(STDIN)));
    if($g == "r")
    {
        $k = array_search($e . $f . $g, $h);
        if($k == FALSE)
        {
            if($e == "a")
            {
                array_push($j, $b . " " . $f . " " . $d);
            }
            else
            {
                array_push($j, $a . " " . $f . " " . $d);
            }
        }
        array_push($h, $e . $f . $g);
        array_push($i, "0");
    }
    else
    {
        $k = array_search($e . $f . $g, $h);
        if(($k == TRUE) && ($i[$k] == 0))
        {
            $i[$k] = 1;
            if($e == "a")
            {
                array_push($j, $b . " " . $f . " " . $d);
            }
            else
            {
                array_push($j, $a . " " . $f . " " . $d);
            }
        }
        else
        {
            array_push($h, $e . $f . $g);
            array_push($i, "0");
        }
    }
}
for($x = 0; $x < count($j) - 1; $x++)
{
    print $j[$x] . "\n";
}
print $j[$x];
?>

<?php
	$fh = fopen("php://stdin", "r");
	$home = fgets($fh);
	$away = fgets($fh);
	$ncase = (int)(fgets($fh));
	$cases = array();
	for($i=0;$i<$ncase;$i++)
	{
		$cases[] = fgets($fh);
	}

	$reds = array();
    $yellow = array();
	foreach($cases as $case)
	{
		$caseAr =  explode(" ",$case);
		$player =  $caseAr[1].$caseAr[2];
		$time   =  $caseAr[0];
		$card   =  trim($caseAr[3]);
		if($card == "r")
		{
			if(array_key_exists($player,$reds)) continue;
			$reds[$player] = $time;
			continue;
		}
		if(array_key_exists($player, $yellow))
		{
			$reds[$player] = $time;
			unset($yellow[$player]);
		}
		else
		{
			$yellow[$player] = $time;
		}

	}

	if(count($reds)> 0)
	{
		foreach($reds as $key=>$value)
		{
			$team = (substr($key,0,1)=="h") ?"$home":"$away";
			$nos  = substr($key,1,strlen($key)-1);
			$time = $value;
			echo trim($team).' '.$nos.' '.$value."\n";

		}

	}

	/*
	TANC
XNCOR
2
15 h 27 r
28 h 27 r
*/

<?php
	$fh = fopen("php://stdin", "r");
	$home = fgets($fh);
	$away = fgets($fh);
	$ncase = (int)(fgets($fh));
	$cases = array();
	for($i=0;$i<$ncase;$i++)
	{
		$cases[] = fgets($fh);
	}

	$reds = array();
    $yellow = array();
	foreach($cases as $case)
	{
		$caseAr =  explode(" ",$case);
		$player =  $caseAr[1].$caseAr[2];
		$time   =  $caseAr[0];
		$card   =  trim($caseAr[3]);
		if(array_key_exists($player,$reds))
		{
			continue;
		}
		else
		{
			$reds[$player] = $time;
			continue;

		}

		if(array_key_exists($player, $yellow))
		{
			$reds[$player] = $time;
			unset($yellow[$player]);
		}
		else
		{
			$yellow[$player] = $time;
		}

	}

	if(count($reds)> 0)
	{
		foreach($reds as $key=>$value)
		{
			$team = (substr($key,0,1)=="h") ?"$home":"$away";
			$nos  = substr($key,1,strlen($key)-1);
			$time = $value;
			echo trim($team).' '.$nos.' '.$value."\n";

		}

	}

	/*
	ASGDFJH
AHGRSDXGER
3
23 h 15 r
68 h 15 y
79 h 15 y
Output
ASGDFJH 15 79
Answer
ASGDFJH 15 23
Checker Log
wrong answer 1st lines differ - expected: 'ASGDFJH 15 23', found: 'ASGDFJH 15 79'

*/

<?php
	$fh = fopen("php://stdin", "r");
	$home = fgets($fh);
	$away = fgets($fh);
	$ncase = (int)(fgets($fh));
	$cases = array();
	for($i=0;$i<$ncase;$i++)
	{
		$cases[] = fgets($fh);
	}

	$reds = array();
    $yellow = array();
	foreach($cases as $case)
	{
		$caseAr =  explode(" ",$case);
		$player =  $caseAr[1].$caseAr[2];
		$time   =  $caseAr[0];
		$card   =  trim($caseAr[3]);
		if($card == "r")
		{
			$reds[$player] = $time;
			continue;
		}
		if(array_key_exists($player, $yellow))
		{
			$reds[$player] = $time;
			unset($yellow[$player]);
		}
		else
		{
			$yellow[$player] = $time;
		}

	}

	if(count($reds)> 0)
	{
		foreach($reds as $key=>$value)
		{
			$team = (substr($key,0,1)=="h") ?"$home":"$away";
			$nos  = substr($key,1,strlen($key)-1);
			$time = $value;
			echo trim($team).' '.$nos.' '.$value."\n";

		}

	}

<?php
$file = fopen("php://stdin", "r");
// $file = fopen("493A.txt", "r");
$nameH = trim(fgets($file));
$nameA = trim(fgets($file));
$round = trim(fgets($file));

$arrayH = array();
$arrayA = array();
for($i=0;$i<$round;$i++){
    $perHis = fgets($file);
    //0-time,1-place,2-number,3-color
    //place=h/a , card=y/r
    $baseArray = "";
    $baseArray = explode(" ", $perHis);
    if($baseArray[1]=="h"){
        if(in_array($baseArray[2], $arrayH)){
            echo $nameH." ".$baseArray[2]." ".$baseArray[0]."\n";
            $arrayH[$i] = "";
        }else{
            $arrayH[] = $baseArray[2];
        }
    }else if($baseArray[1]=="a"){
        if(in_array($baseArray[2],$arrayA)){
            echo $nameA." ".$baseArray[2]." ".$baseArray[0]."\n";
            $arrayA[$i] = "";
        }else{
            $arrayA[] = $baseArray[2];
        }
    }
}

?>

<?php
$file = fopen("php://stdin", "r");
// $file = fopen("493A.txt", "r");
$teamName["h"] = trim(fgets($file));
$teamName["a"] = trim(fgets($file));
$round = trim(fgets($file));

$history = array();
for($i=0;$i<$round;$i++){
    $perHis = fgets($file);

    //place==h||a , card==y||r
    $base = explode(" ", $perHis);
    $time = $base[0];
    $place = $base[1];
    $number = $base[2];
    $card = trim($base[3]);
    $key = $place.$number;

    if($card == "r"){
        echo $teamName[$place]." ".$number." ".$time."\n";
        if(array_key_exists($key, $history)){
            $history[$key] = 0;
        }
    }else if($card =="y"){
        if(array_key_exists($key,$history)){
            if($history[$key]==0){
                $history[$key] =1;
            }else{
                echo $teamName[$place]." ".$number." ".$time."\n";
            }
        }else{
            $history[$key] = 1;
        }
    }
}

?>

<?php
class fl{
var $num;
var $time;
var $team;
var $count;
var $out;
function __construct($e,$b,$c,$d){
$this->num = $e;
$this->time = $b;
$this->team = $c;
$this->count += $d;
$this->out = true;
$this->rec();
}
function upd($a,$b){
$this->time = $e;
$this->count += $b;
$this->rec();
}
function rec(){
if($this->count >= 2 && $this->out){
printf ("%s %d %d\n",$this->team,$this->num,$this->time);
$this->out = false;
}
}
}
$name = array();
$name_h = trim(fgets(STDIN));
$name_e = trim(fgets(STDIN));
$fls = array();
$n = trim(fgets(STDIN));
for ($i=0;$i<$n;$i++){
list($t,$e,$m,$b) = explode(" ",trim(fgets(STDIN)));
$b = ($b=='r')? 2 : 1;
$e = ($e=='h')? $name_h : $name_e;
if (!isset($fls[$e.$m])){
$fls[$e.$m]= new fl($m,$t,$e,$b);
}else{
$fls[$e.$m]->upd($t,$b);
}
}
?>

<?php
$h_team = trim(fgets(STDIN));
$a_team = trim(fgets(STDIN));
$count_of_falls = trim(fgets(STDIN));
$arr = array();
for ($i=0; $i<$count_of_falls; $i++) {
 $arr[] = explode(" ", trim(fgets(STDIN)));
}

for ($i=0; $i<$count_of_falls; $i++) {
 if ($arr[$i][1] == "h") {
 $arr_h[] = $arr[$i];
 } else if ($arr[$i][1] == "a") {
 $arr_a[] = $arr[$i];
 }
}
//print_r($arr_h);
//print_r($arr_a);

$arr_falls_h = array();
for ($i=0; $i<count($arr_h); $i++) {
 if (array_key_exists($arr_h[$i][2], $arr_falls_h) or $arr_h[$i][3] == "r") {
 $arr_falls_h[$arr_h[$i][2]] = array(2, $arr_h[$i][0], $arr_h[$i][2]);
 } else {
 $arr_falls_h[$arr_h[$i][2]] = 1;
 }
}
$arr_falls_a = array();
for ($i=0; $i<count($arr_a); $i++) {
 if (array_key_exists($arr_a[$i][2], $arr_falls_a) or $arr_a[$i][3] == "r") {
 $arr_falls_a[$arr_a[$i][2]] = array(2, $arr_a[$i][0], $arr_a[$i][2]);
 } else {
 $arr_falls_a[$arr_a[$i][2]] = 1;
 }
}
//print_r($arr_falls_h);
//print_r($arr_falls_a);

foreach ($arr_falls_h as $value) {
 if (!empty($value[0])) {
 echo $h_team." ".$value[2]." ".$value[1]."\n";
 }
}
foreach ($arr_falls_a as $value) {
 if (!empty($value[0])) {
 echo $a_team." ".$value[2]." ".$value[1]."\n";
 }
}
?>