We just discovered a new data structure in our research group: a suffix three!It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.Let us tell you how it works.  If a sentence ends with "po" the language is Filipino.  If a sentence ends with "desu" or "masu" the language is Japanese.  If a sentence ends with "mnida" the language is Korean. Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.Oh, did I say three suffixes? I meant four.

The first line of input contains a single integer $$$t$$$ ($$$1 \leq t \leq 30$$$) denoting the number of test cases. The next lines contain descriptions of the test cases.  Each test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol "_") for ease of reading. The sentence has at least $$$1$$$ and at most $$$1000$$$ characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above.

For each test case, print a single line containing either "FILIPINO", "JAPANESE", or "KOREAN" (all in uppercase, without quotes), depending on the detected language.

8
kamusta_po
genki_desu
ohayou_gozaimasu
annyeong_hashimnida
hajime_no_ippo
bensamu_no_sentou_houhou_ga_okama_kenpo
ang_halaman_doon_ay_sarisari_singkamasu
si_roy_mustang_ay_namamasu

FILIPINO
JAPANESE
JAPANESE
KOREAN
FILIPINO
FILIPINO
JAPANESE
JAPANESE

NoteThe first sentence ends with "po", so it is written in Filipino.The second and third sentences end with "desu" and "masu", so they are written in Japanese.The fourth sentence ends with "mnida", so it is written in Korean.

<?php

function FirstReverse($str) {

  // code goes here
  $char = substr($str, strlen($str)-3);
  if(ord($char) == 111){
    return 'FILIPINO'."\r\n";
  }elseif (ord($char) == 117) {
    return 'JAPANESE'."\r\n";
  }elseif (ord($char) == 97) {
    return 'KOREAN'."\r\n";
  }
}

// keep this function call here
// $file = fopen('input.txt', 'r');
$file = fopen('php://stdin', 'r');
$num = fgets($file);
for ($i=0; $i < $num; $i++) {
  $str = fgets($file);
  echo FirstReverse($str);
  // return 0;
}
// echo FirstReverse(fgets(fopen('input.txt', 'r')));
// echo FirstReverse(fgets(fopen('php://stdin', 'r')));

?>

<?php

$head_str = readline();

$head_info = explode(' ', $head_str);
$size = $head_info[0];

for($a = 0;$a < $size; $a++){

    $str = trim(readline()).'#';
    if(strpos($str, 'desu#') !== FALSE || strpos($str, 'masu#') !== FALSE){
        print_r('JAPANESE'.PHP_EOL);
    }
    elseif(strpos($str, 'po#') !== FALSE){
        print_r('FILIPINO'.PHP_EOL);
    }
    else{
        print_r('KOREAN'.PHP_EOL);
    }
}
return 0;

?>

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

$arr = array();
for ($j=0; $j<$t; $j++) {
    array_push($arr, fscanf(STDIN, "%s"));
}

for ($j=0; $j<$t; $j++) {
	if (getSuffix($arr[$j][0], 2) === 'po') {
		echo 'FILIPINO';
	}
	else if (getSuffix($arr[$j][0], 4) === 'desu' || getSuffix($arr[$j][0], 4) === 'masu') {
		echo 'JAPANESE';
	}
	else if (getSuffix($arr[$j][0], 5) === 'mnida') {
		echo 'KOREAN';
	}
	echo "\n";
}

function getSuffix($str, $num) {
	return substr($str, strlen($str)-$num);
}

?>

<?php
function solve()
{
    $server = "php://stdin";
    $local = "input.txt";
    $handle = fopen($server, "r");
    $t= trim(fgets($handle));
    while ($t>0)
    {
       $s=trim(fgets($handle));
       $arr=explode('_',$s);
       $newStr= $arr[sizeof($arr)-1];

       if($newStr[strlen($newStr)-1]=='o')
       {
           echo "FILIPINO";
           echo "\n";
       }else if($newStr[strlen($newStr)-1]=='u')
       {
           echo "JAPANESE";
           echo "\n";
       }else
       {
           echo "KOREAN";
           echo "\n";
       }

        --$t;
    }

}

solve();
?>

<?php

function FirstReverse($str) {

  // code goes here
  $char = substr($str, strlen($str)-3);
  if(ord($char) == 111){
    return 'FILIPINO
';
  }elseif (ord($char) == 117) {
    return 'JAPANESE
';
  }elseif (ord($char) == 97) {
    return 'KOREAN
';
  }
}

// keep this function call here
$file = fopen('php://stdin', 'r');
$num = fgets($file);
for ($i=0; $i < $num; $i++) {
  $str = fgets($file);
  echo FirstReverse($str);
  // return 0;
}
// echo FirstReverse(fgets(fopen('input.txt', 'r')));
// echo FirstReverse(fgets(fopen('php://stdin', 'r')));

?>

<?php
// $dir = fopen("php://stdin", 'r');
// $dir = fopen("input.txt", 'r');
// $str = stream_get_contents($dir);
$length = fgets($dir);
for ($i=0; $i < $length; $i++) {
    // $line = substr(fgets($dir), 1);
    $line  = fgets($dir);
    $str = substr($line, strlen($line)-3);
    if (ord($str) == 111) {
        echo 'FILIPINO';
    }
    elseif (ord($str) == 117) {
        echo 'JAPANESE';
    }elseif (ord($str) == 97) {
        echo 'KOREAN';
    }
}

// echo FirstReverse(fgets(fopen('php://stdin', 'r')));
// echo (fgets(fopen('input.txt', 'r')));
?>

<?php

// function FirstReverse($str) {

//   // code goes here
//   $char = substr($str, strlen($str)-3);
//   if(ord($char) == 111){
//     return 'FILIPINO';
//   }elseif (ord($char) == 117) {
//     return 'JAPANESE';
//   }elseif (ord($char) == 97) {
//     return 'KOREAN';
//   }
// }

// // keep this function call here
// echo FirstReverse(fgets(fopen('input.txt', 'r')));
// echo FirstReverse(fgets(fopen('php://stdin', 'r')));

echo 'FILIPINO';
?>

<?php
$dir = fopen("php://stdin", 'r');
// $dir = fopen("input.txt", 'r');
// $str = stream_get_contents($dir);
$length = fgets($dir);
for ($i=0; $i < $length; $i++) {
    // $line = substr(fgets($dir), 1);
    $line  = fgets($dir);
    $str = substr($line, strlen($line)-3);
    if (ord($str) == 111) {
        echo 'FILIPINO';
    }
    elseif (ord($str) == 117) {
        echo 'JAPANESE';
    }elseif (ord($str) == 97) {
        echo 'KOREAN';
    }
}

// echo FirstReverse(fgets(fopen('php://stdin', 'r')));
// echo (fgets(fopen('input.txt', 'r')));
?>

<?php

$head_str = readline();

$head_info = explode(' ', $head_str);
$size = $head_info[0];

for($a = 0;$a < $size; $a++){

    $str = trim(readline());
    if(strpos($str, 'desu') !== FALSE || strpos($str, 'masu') !== FALSE){
        print_r('JAPANESE'.PHP_EOL);
    }
    elseif(strpos($str, 'po') !== FALSE){
        print_r('FILIPINO'.PHP_EOL);
    }
    else{
        print_r('KOREAN'.PHP_EOL);
    }
}
return 0;

?>

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

$arr = array();
for ($j=0; $j<$t; $j++) {
    array_push($arr, fscanf(STDIN, "%s"));
}

for ($j=0; $j<$t; $j++) {
	if (getSuffix($arr[$j][0], 2) === 'po') {
		echo 'PHILIPINO';
	}
	else if (getSuffix($arr[$j][0], 4) === 'desu' || getSuffix($arr[$j][0], 4) === 'masu') {
		echo 'JAPANESE';
	}
	else if (getSuffix($arr[$j][0], 5) === 'mnida') {
		echo 'KOREAN';
	}
	echo "\n";
}

function getSuffix($str, $num) {
	return substr($str, strlen($str)-$num);
}

?>


Phoenix has $$$n$$$ coins with weights $$$2^1, 2^2, \dots, 2^n$$$. He knows that $$$n$$$ is even.He wants to split the coins into two piles such that each pile has exactly $$$\frac{n}{2}$$$ coins and the difference of weights between the two piles is minimized. Formally, let $$$a$$$ denote the sum of weights in the first pile, and $$$b$$$ denote the sum of weights in the second pile. Help Phoenix minimize $$$|a-b|$$$, the absolute value of $$$a-b$$$.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 30$$$; $$$n$$$ is even) — the number of coins that Phoenix has.

For each test case, output one integer — the minimum possible difference of weights between the two piles.

NoteIn the first test case, Phoenix has two coins with weights $$$2$$$ and $$$4$$$. No matter how he divides the coins, the difference will be $$$4-2=2$$$.In the second test case, Phoenix has four coins of weight $$$2$$$, $$$4$$$, $$$8$$$, and $$$16$$$. It is optimal for Phoenix to place coins with weights $$$2$$$ and $$$16$$$ in one pile, and coins with weights $$$4$$$ and $$$8$$$ in another pile. The difference is $$$(2+16)-(4+8)=6$$$.

<?php
$input = fopen("php://stdin", 'r');
fscanf($input, "%d", $testsCount);

for ($i = 1; $i <= $testsCount; $i++) {
  fscanf($input, "%d", $coinsCount);
  echo solve($coinsCount) . "\n";
}

function solve(int $coinsCount): int
{
    $firstPileWeight = 0;
    $secondPileWeight = 0;

    $firstPileWeight += pow(2, $coinsCount);
    $secondPileWeight += pow(2, $coinsCount - 1);

    if ($coinsCount > 2) {
        for ($i = 1; $i < $coinsCount / 2; $i++) {
            $firstPileWeight += pow(2, $i);
            $secondPileWeight += pow(2, $coinsCount - 1 - $i);
        }
    }

    return abs($firstPileWeight - $secondPileWeight);
}

<?php

$count = (int)readline();

for ($i = 0; $i < $count; $i++) {
	$n = (int)readline();

	$left = 0;
	$right = pow(2, $n);

	$sideSize = $n / 2;

	$leftSize = $sideSize;
	$rightSize = $sideSize - 1;

	for ($j = 0; $j < $rightSize; $j++) {
		$num = pow(2, $j + 1);

		$right += $num;
	}

	for ($j = $n - 1; $j >= $leftSize; $j--) {
		$num = pow(2, $j);

		$left += $num;
	}

	echo max($left, $right) - min($left, $right);
	echo PHP_EOL;
}

<?php
fscanf(STDIN, '%d\n', $inputCount);

for ($i = 1; $i <= $inputCount; $i++) {
  $response = balanceCoins( (int) trim( fgets(STDIN) ) );
  echo $response . "\n";
}

function balanceCoins($power)
{
  $pile1 = [];
  $pile2 = [];

  for (;$power >= 1; $power--) {
    if ($pile1 > $pile2) $pile2[] = pow(2, $power);
    else $pile1[] = pow(2, $power);
  }

  return abs(array_sum($pile1) - array_sum($pile2));
}

<?php

$count = (int)readline();

for ($i = 0; $i < $count; $i++) {
	$n = (int)readline();

	$left = 0;
	$right = 0;

	for ($j = 0, $k = $n; $j < $n / 2; $j++, $k--) {
		$num1 = pow(2, $j + 1);
		$num2 = pow(2, $k);

		if ($n === 2) {
			$left += $num1;
			$right += $num2;
		} else if ($j % 2 === 0) {
			$left += $num1 + $num2;
		} else {
			$right += $num1 + $num2;
		}
	}

	echo max($left, $right) - min($left, $right);
	echo PHP_EOL;
}


Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.Let us say that his initial per chapter learning power of a subject is x hours. In other words he can learn a chapter of a particular subject in x hours.Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour.You can teach him the n subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.Please be careful that answer might not fit in 32 bit data type.

The first line will contain two space separated integers n, x (1 ≤ n, x ≤ 105). The next line will contain n space separated integers: c1, c2, ..., cn (1 ≤ ci ≤ 105).

Output a single integer representing the answer to the problem.

NoteLook at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2 × 1 = 2 hours. Hence you will need to spend 12 + 2 = 14 hours.Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3 × 1 = 3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2 × 4 = 8 hours. Hence you will need to spend 11 hours.So overall, minimum of both the cases is 11 hours.Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours.

<?php

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

list($n,$x) = explode(" ", rtrim(fgets($in)));
$a = explode(" ", rtrim(fgets($in)));

shuffle($a);
sort($a);
$sumHi = 0;
$sumLo = 0;
for($i=0; $i<$n; $i++){
    $sumLo += $x*$a[$i];
    $sumHi += $resHi;
    if ($sumLo >= 1000000000) {
        $temp = floor($sumLo / 1000000000);
        $sumHi += floor($sumLo / 1000000000);
        $sumLo -= $temp * 1000000000;
    }
    if($x>1)
        $x--;
}

if ($sumHi) {
    echo $sumHi . substr('' .($sumLo + 1000000000), 1);
} else {
    echo $sumLo;
}

<?php
$_fp = fopen("php://stdin", "r");
list($n , $x) = explode(' ', trim(fgets($_fp)));

$c = explode(' ' , trim(fgets($_fp)));

$c_sort = $c;
$c_rsort = $c;

$sum_sort = 0;
$sum_rsort = 0;

$x_sort = $x;
$x_rsort = $x;

sort($c_sort);
rsort($c_rsort);

for ($i = 0; $i < $n; $i++) {
    $sum_sort = bcadd($sum_sort, $x_sort * $c_sort[$i]);
    if ($x_sort > 1) {
        $x_sort--;
    }
}

for ($i = 0; $i < $n; $i++) {
    $sum_rsort = bcadd($sum_rsort, $x_rsort * $c_rsort[$i]);
    if ($x_rsort > 1) {
        $x_rsort--;
    }
}

$result = $sum_sort < $sum_rsort ? $sum_sort : $sum_rsort;

echo (string)$result;

<?php

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

list($n,$x) = explode(" ", rtrim(fgets($in)));
$a = explode(" ", rtrim(fgets($in)));

sort($a);
$sum = 0;
for($i=0; $i<$n; $i++){
    $sum += $x*$a[$i];
    if($x>1)
        $x--;
}

echo $sum;

<?php
$_fp = fopen("php://stdin", "r");
list($n , $x) = explode(' ', trim(fgets($_fp)));

$c = explode(' ' , trim(fgets($_fp)));

$c_sort = $c;
$c_rsort = $c;

$sum_sort = 0;
$sum_rsort = 0;

$x_sort = $x;
$x_rsort = $x;

sort($c_sort);
rsort($c_rsort);

for ($i = 0; $i < $n; $i++) {
    $sum_sort += $x_sort * $c_sort[$i];
    if ($x_sort > 1) {
        $x_sort--;
    }
}

for ($i = 0; $i < $n; $i++) {
    $sum_rsort += $x_rsort * $c_rsort[$i];
    if ($x_rsort > 1) {
        $x_rsort--;
    }
}

$result = $sum_sort < $sum_rsort ? $sum_sort : $sum_rsort;

echo (string)$result;

<?php
$_fp = fopen("php://stdin", "r");
list($n , $x) = explode(' ', trim(fgets($_fp)));

$c = explode(' ' , trim(fgets($_fp)));

$c_sort = $c;
$c_rsort = $c;

$sum_sort = 0;
$sum_rsort = 0;

$x_sort = $x;
$x_rsort = $x;

sort($c_sort);
rsort($c_rsort);

for ($i = 0; $i < $n; $i++) {
    $sum_sort += $x_sort * $c_sort[$i];
    if ($x_sort > 1) {
        $x_sort--;
    }
}

for ($i = 0; $i < $n; $i++) {
    $sum_rsort += $x_rsort * $c_rsort[$i];
    if ($x_rsort > 1) {
        $x_rsort--;
    }
}

echo $sum_sort < $sum_rsort ? $sum_sort : $sum_rsort;


Maxim wants to buy some games at the local game shop. There are $$$n$$$ games in the shop, the $$$i$$$-th game costs $$$c_i$$$.Maxim has a wallet which can be represented as an array of integers. His wallet contains $$$m$$$ bills, the $$$j$$$-th bill has value $$$a_j$$$.Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position $$$i$$$ in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the $$$i$$$-th game using this bill. After Maxim tried to buy the $$$n$$$-th game, he leaves the shop.Maxim buys the $$$i$$$-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the $$$i$$$-th game. If he successfully buys the $$$i$$$-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array $$$c = [2, 4, 5, 2, 4]$$$ and array $$$a = [5, 3, 4, 6]$$$ the following process takes place: Maxim buys the first game using the first bill (its value is $$$5$$$), the bill disappears, after that the second bill (with value $$$3$$$) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because $$$c_2 > a_2$$$, the same with the third game, then he buys the fourth game using the bill of value $$$a_2$$$ (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value $$$a_3$$$.Your task is to get the number of games Maxim will buy.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) — the number of games and the number of bills in Maxim's wallet. The second line of the input contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le 1000$$$), where $$$c_i$$$ is the cost of the $$$i$$$-th game. The third line of the input contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_j \le 1000$$$), where $$$a_j$$$ is the value of the $$$j$$$-th bill from the Maxim's wallet.

Print a single integer — the number of games Maxim will buy.

NoteThe first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
$e = 0;
$f = 0;
for($x = 0; $x < $a; $x++)
{
     if($d[$f] >= $c[$x])
     {
          $e++;
          $f++;
     }
}
print $e;
?>

<?php
fscanf(STDIN, "%d %d", $N, $M);
$c = explode(" ", trim(fgets(STDIN)));
$a = explode(" ", trim(fgets(STDIN)));

$ans = 0;
$cursorC = 0;
$cursorA = 0;

while ($cursorC < $N && $cursorA < $M) {
	if ($c[$cursorC] <= $a[$cursorA]) {
		$ans++;
		$cursorC++;
		$cursorA++;
	} else {
		$cursorC++;
	}
}
echo $ans;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$a = explode(" ",$a);
$b = explode(" ",$b);
$c = explode(" ",$c);

$gameCost = $b;
$bills = $c;
$currentBillIndex = 0;
$count = 0;

foreach($gameCost as $key => $value){
    if($currentBillIndex >= $a[1]){
        break;
    }
    if($value > $bills[$currentBillIndex]){
        continue;
    }

    $count++;
    $currentBillIndex++;
}
echo "$count";

?>

<?php
  $data = file('php://stdin');
  $games = explode(' ', $data[1]);
  $money = explode(' ', $data[2]);
  $count = 0;

  foreach ($games as $game) {
    if ((int)$game <= (int)$money[0]) {
      $count++;
      array_shift($money);
    }
  }
  echo $count;
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$a = explode(" ",$a);
$b = explode(" ",$b);
$c = explode(" ",$c);

//$a = [5, 4];
//$b = [2, 4, 5, 2, 4];
//$c = [5, 3, 4, 6];

$gameCost = $b;
$bills = $c;
$currentBillIndex = 0;
$count = 0;

foreach($gameCost as $key => $value){
    if($currentBillIndex >= $a[1]){
        break;
    }
    if($value > $bills[$currentBillIndex]){
        continue;
    }

    $count++;
    $currentBillIndex++;
}
echo "$count";

?>


Lolek and Bolek are about to travel abroad by plane. The local airport has a special "Choose Your Plane" offer. The offer's conditions are as follows:  it is up to a passenger to choose a plane to fly on;  if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys (units of Polish currency). The only ticket office of the airport already has a queue of n passengers in front of it. Lolek and Bolek have not stood in the queue yet, but they are already wondering what is the maximum and the minimum number of zlotys the airport administration can earn if all n passengers buy tickets according to the conditions of this offer?The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on up to n-th person.

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 1000) — ai stands for the number of empty seats in the i-th plane before the ticket office starts selling tickets. The numbers in the lines are separated by a space. It is guaranteed that there are at least n empty seats in total.

Print two integers — the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly.

NoteIn the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum.In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 2-nd plane, the 3-rd person — to the 3-rd plane, the 4-th person — to the 1-st plane. The sum is minimized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person — to the 1-st plane, the 3-rd person — to the 2-nd plane, the 4-th person — to the 2-nd plane.

<?php
// Retrieve input
$stdin = fopen('php://stdin', 'r');
function g() {
	global $stdin;
	return explode(" ", trim(fgets($stdin)));
}

// Codeforces Problem 218B
// Codeforces Contest 134 Div 2 Problem B
$raw = g();
$n = $raw[0];
$m = $raw[1];
$raw = g();
for ($x = 0; $x < $m; $x++) {
	$a[$x] = $raw[$x];
}

$b = merge_sort($a);
$b[-1] = 0;

$max = 0;
for ($x = 0; $x < $n; $x++) {
	for ($y = $m-1; $y >= 0; $y--) {
		if ($b[$y] > $b[$y-1]) {
			$max += $b[$y];
			$b[$y]--;
			break;
		}
	}
}

$b = merge_sort($a);
$b[-1] = 0;
$min = 0;
for ($x = 0; $x < $n; $x++) {
	for ($y = 0; $y < $m; $y++) {
		if ($b[$y] != 0) {
			$min += $b[$y];
			$b[$y]--;
			break;
		}
	}
}

echo $max . " " . $min;

function merge_sort($m) {
    if (count($m) <= 1) {
        return $m;
	}
	$mid = round((count($m) - 1) / 2, 0, PHP_ROUND_HALF_UP);
    for ($x = 0; $x < $mid; $x++) {
		$left[$x] = $m[$x];
	}
    for ($x = 0; $x <= $mid; $x++) {
		if ($mid + $x < count($m)) {$right[$x] = $m[$mid + $x];}
	}
	$left = merge_sort($left);
	$right = merge_sort($right);

	$l = 0;
	$r = 0;
	for ($x = 0; $x < count($m); $x++) {
		if (($l < count($left)) && ($r < count($right))) {
			if ($left[$l] <= $right[$r]) {
				$res[$x] = $left[$l];
				$l++;
			} else {
				$res[$x] = $right[$r];
				$r++;
			}
		} else if ($l >= count($left)) {
			$res[$x] = $right[$r];
			$r++;
		} else {
			$res[$x] = $left[$l];
			$l++;
		}
	}

	return $res;
}

// End of submission file
// Submission by chaotic_iak
?>

<?php

//$stream = fopen( 'input.txt', 'r' );
$stream = STDIN;

// Input data
$data = explode( "\n", stream_get_contents( $stream ) );

$n = explode( ' ', trim( $data[0] ) ); $n = $n[0]; // Пассажири
$m = explode( ' ', trim( $data[0] ) ); $m = $m[1]; // Самолеты
$free = explode( ' ', trim( $data[1] ) );

$max = 0;
$min = 0;

// Max
$mx = $free;

for( $i = 1; $i <= $n; $i++ ) {

	$max_free_index = 0;
	$max_free_value = 0;

	foreach ($mx as $k => $f) {

		if( $f == 0 ) continue;

		if( $max_free_value < $f ) {
			$max_free_index = $k;
			$max_free_value = $f;
		}
	}

	$mx[ $max_free_index ]--;

	$max += $max_free_value;
}

// Min
$mn = $free;

for( $i = 1; $i <= $n; $i++ ) {

	$min_free_index = 0;
	$min_free_value = 1000;

	foreach ($mn as $k => $f) {

		if( $f == 0 ) continue;

		if( $min_free_value > $f ) {

			$min_free_index = $k;
			$min_free_value = $f;
		}
	}

	$mn[ $min_free_index ]--;

	$min += $min_free_value;
}

$result = "$max $min";

echo $result;

<?php
list($n,$m)=explode(" ",trim(fgets(STDIN)));
for($i=0;$i<$m;$i++) $s.="%d ";
$a_min=fscanf(STDIN,"$s");
sort($a_min); $a_max=$a_min;
$n_min=$n;$n_max=$n;
$min=0; $max=$m-1;
while($n_min>0)
    if($a_min[$min]>0) {
        --$n_min;
        $out_min+=$a_min[$min];
        --$a_min[$min];
    }
    else $min++;
while($n_max>0) {
    --$n_max;
    rsort($a_max);
    $out_max+=$a_max[0];
    $a_max[0]--;
}
print "$out_max $out_min";

<?
list($n,$m)=explode(" ",trim(fgets(STDIN)));
//$a_min=explode(" ",fgets(STDIN));
for($i=0;$i<$m;$i++) $s.="%d ";
$a_min=fscanf(STDIN,"$s");
sort($a_min); $a_max=$a_min; //rsort($a_max);
$n_min=$n;$n_max=$n;
$min=0; $max=$m-1;
while($n_min>0 or $n_max>0){
    if($a_min[$min]>0) {--$n_min; $out_min+=$a_min[$min]; --$a_min[$min]; }
        else $min++;
    if($a_max[$max]>0 && !($a_max[$max]<$a_max[$max-1])) {--$n_max; $out_max+=$a_max[$max]; --$a_max[$max]; }
        else $max--;

}
print "$out_max $out_min";

<?
list($n,$m)=explode(" ",trim(fgets(STDIN)));
//$a_min=explode(" ",fgets(STDIN));
for($i=0;$i<$m;$i++) $s.="%d ";
$a_min=fscanf(STDIN,"$s");
sort($a_min); $a_max=$a_min; //rsort($a_max);
$n_min=$n;$n_max=$n;
$min=0; $max=$m-1;
while($n_min>0) if($a_min[$min]>0) {--$n_min; $out_min+=$a_min[$min]; --$a_min[$min]; }
                                    else $min++;
while($n_max>0) if($a_max[$max]>0 && !($a_max[$max]<$a_max[$max-1])) {--$n_max; $out_max+=$a_max[$max]; --$a_max[$max]; }
                                    elseif ($a_max[$max]==0 && !($a_max[$max-1])) $max++;
                                    else $max--;

print "$out_max $out_min";


Roma works in a company that sells TVs. Now he has to prepare a report for the last year.Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.The operation of changing a number's sign is the operation of multiplying this number by -1.Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes.

The first line contains two integers n and k (1 ≤ n, k ≤ 105), showing, how many numbers are in the sequence and how many swaps are to be made. The second line contains a non-decreasing sequence, consisting of n integers ai (|ai| ≤ 104). The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.

In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.

NoteIn the first sample we can get sequence [1, 1, 1], thus the total income equals 3.In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.


Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.That element can store information about the matrix of integers size n × m. There are n + m inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to n from top to bottom, while columns are numbered with integers from 1 to m from left to right.Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of q turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists.

The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively. Next q lines contain turns descriptions, one per line. Each description starts with an integer ti (1 ≤ ti ≤ 3) that defines the type of the operation. For the operation of first and second type integer ri (1 ≤ ri ≤ n) or ci (1 ≤ ci ≤ m) follows, while for the operations of the third type three integers ri, ci and xi (1 ≤ ri ≤ n, 1 ≤ ci ≤ m,  - 109 ≤ xi ≤ 109) are given. Operation of the first type (ti = 1) means that signal comes to the input corresponding to row ri, that is it will shift cyclically. Operation of the second type (ti = 2) means that column ci will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row ri and column ci stores value xi.

Print the description of any valid initial matrix as n lines containing m integers each. All output integers should not exceed 109 by their absolute value. If there are multiple valid solutions, output any of them.


Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into $$$n$$$ sub-tracks. You are given an array $$$a$$$ where $$$a_i$$$ represents the number of traffic cars in the $$$i$$$-th sub-track. You define the inconvenience of the track as $$$\sum\limits_{i=1}^{n} \sum\limits_{j=i+1}^{n} \lvert a_i-a_j\rvert$$$, where $$$|x|$$$ is the absolute value of $$$x$$$. You can perform the following operation any (possibly zero) number of times: choose a traffic car and move it from its current sub-track to any other sub-track.Find the minimum inconvenience you can achieve.

The first line of input contains a single integer $$$t$$$ ($$$1\leq t\leq 10\,000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1\leq n\leq 2\cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0\leq a_i\leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, print a single line containing a single integer: the minimum inconvenience you can achieve by applying the given operation any (possibly zero) number of times.

NoteFor the first test case, you can move a car from the $$$3$$$-rd sub-track to the $$$1$$$-st sub-track to obtain $$$0$$$ inconvenience.For the second test case, moving any car won't decrease the inconvenience of the track.

<?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)));
  $sum = array_sum($a);

  $m = floor(fmod($sum, $n));
  $r = abs($n - $m);
  $ans = $m * $r;
  echo $ans.PHP_EOL;
}

?>

<?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)));
  $sum = array_sum($a);

  $m = abs($sum % $n);
  $ans = $m * (abs($n - $m));
  echo $ans.PHP_EOL;
}

?>

<?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)));
  $sum = array_sum($a);

  $m = abs($sum % $n);
  $ans = $m * (abs($n - $m));
  echo $sum.' '.$m.' '.$ans.PHP_EOL;
}

?>

<?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)));

  $m = array_sum($a) % $n;
  $ans = $m * ($n - $m);
  echo $ans.PHP_EOL;
}

?>


The Little Girl loves problems on games very much. Here's one of them.Two players have got a string s, consisting of lowercase English letters. They play a game that is described by the following rules:  The players move in turns; In one move the player can remove an arbitrary letter from string s.  If the player before his turn can reorder the letters in string s so as to get a palindrome, this player wins. A palindrome is a string that reads the same both ways (from left to right, and vice versa). For example, string "abba" is a palindrome and string "abc" isn't. Determine which player will win, provided that both sides play optimally well — the one who moves first or the one who moves second.

The input contains a single line, containing string s (1 ≤ |s|  ≤  103). String s consists of lowercase English letters.

In a single line print word "First" if the first player wins (provided that both players play optimally well). Otherwise, print word "Second". Print the words without the quotes.


During their New Year holidays, Alice and Bob play the following game using an array $$$a$$$ of $$$n$$$ integers:   Players take turns, Alice moves first.  Each turn a player chooses any element and removes it from the array.  If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice's score does not change.  Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob's score does not change. If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared.For example, if $$$n = 4$$$ and $$$a = [5, 2, 7, 3]$$$, then the game could go as follows (there are other options):   On the first move, Alice chooses $$$2$$$ and get two points. Her score is now $$$2$$$. The array $$$a$$$ is now $$$[5, 7, 3]$$$.  On the second move, Bob chooses $$$5$$$ and get five points. His score is now $$$5$$$. The array $$$a$$$ is now $$$[7, 3]$$$.  On the third move, Alice chooses $$$7$$$ and get no points. Her score is now $$$2$$$. The array $$$a$$$ is now $$$[3]$$$.  On the last move, Bob chooses $$$3$$$ and get three points. His score is now $$$8$$$. The array $$$a$$$ is empty now.  Since Bob has more points at the end of the game, he is the winner. You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$ used to play the game. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output on a separate line:    "Alice" if Alice wins with the optimal play;  "Bob" if Bob wins with the optimal play;  "Tie", if a tie is declared during the optimal play.

<?php

$times = (int)trim(fgets(STDIN));
for ($idx = 0; $idx < $times; ++$idx) {
    $ni[] = (int)trim(fgets(STDIN));
    $ai[] = array_map('intval', explode(' ', trim(fgets(STDIN))));
}
for ($idx = 0; $idx < $times; ++$idx) {
    $n = &$ni[$idx];
    $a = &$ai[$idx];
    rsort($a);
    $sma = 0;
    $smb = 0;
    for ($i = 0; $i < $n; ++$i) {
        $i % 2 == 0 && $a[$i] % 2 == 0 && $sma += $a[$i];
        $i % 2 == 1 && $a[$i] % 2 == 1 && $smb += $a[$i];
    }
    $sma -= $smb;
    $result = ['Bob', 'Tie', 'Alice'];
    echo $result[($sma > 0) + ($sma >= 0)] . PHP_EOL;
}

<?php
function _end()
{
	$end = readline();
	die();
}
function startPlay($arr)
{
	rsort($arr);
	$A = 0;
	$B = 0;
	foreach($arr as $key => $value)
	{
		if($key % 2 == 0)
		{
			if($value % 2 == 0)
				$A += $value;
		}
		else
		{
			if($value % 2 != 0)
				$B += $value;
		}
	}

	if($A == $B)
		$res = "Tie";
	elseif($A > $B)
		$res = "Alice";
	else
		$res = "Bob";
	return $res;
}

$t = fgets(STDIN);
for($i=0;$i<$t;$i++)
{
	$arr = array();
	$n = fgets(STDIN);
	$arr_str = fgets(STDIN);
	$arr = explode(" ", trim($arr_str));
	print(startPlay($arr)."\r\n");
}
$end = fgets(STDIN);
?>

<?php
function solve() {
    $n = intval(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a = preg_split("/[\s,]+/", $s);
    rsort($a);
    $iCountAlice = 0; $iCountBob = 0;
    for ($i = 0; $i < $n; $i++) {
        if ($i % 2 == 0 && $a[$i] % 2 == 0) {
            $iCountAlice += $a[$i];
        }
        if ($i % 2 == 1 && $a[$i] % 2 == 1) {
            $iCountBob += $a[$i];
        }
    }
    if ($iCountAlice == $iCountBob) {
        echo 'Tie';
    } elseif ($iCountAlice > $iCountBob) {
        echo 'Alice';
    } else {
        echo 'Bob';
    }
}

$t = trim(fgets(STDIN));
while ($t--) {
    solve();
    echo "\n";
}
?>


There is an infinite 2-dimensional grid. The robot stands in cell $$$(0, 0)$$$ and wants to reach cell $$$(x, y)$$$. Here is a list of possible commands the robot can execute:  move north from cell $$$(i, j)$$$ to $$$(i, j + 1)$$$;  move east from cell $$$(i, j)$$$ to $$$(i + 1, j)$$$;  move south from cell $$$(i, j)$$$ to $$$(i, j - 1)$$$;  move west from cell $$$(i, j)$$$ to $$$(i - 1, j)$$$;  stay in cell $$$(i, j)$$$. The robot wants to reach cell $$$(x, y)$$$ in as few commands as possible. However, he can't execute the same command two or more times in a row.What is the minimum number of commands required to reach $$$(x, y)$$$ from $$$(0, 0)$$$?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of testcases. Each of the next $$$t$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$0 \le x, y \le 10^4$$$) — the destination coordinates of the robot.

For each testcase print a single integer — the minimum number of commands required for the robot to reach $$$(x, y)$$$ from $$$(0, 0)$$$ if no command is allowed to be executed two or more times in a row.

NoteThe explanations for the example test:We use characters N, E, S, W and 0 to denote going north, going east, going south, going west and staying in the current cell, respectively.In the first test case, the robot can use the following sequence: NENENENENE.In the second test case, the robot can use the following sequence: NENENEN.In the third test case, the robot can use the following sequence: ESENENE0ENESE.In the fourth test case, the robot doesn't need to go anywhere at all.In the fifth test case, the robot can use the following sequence: E0E.

<?php
fscanf(STDIN, "%d", $number);
$result = array();
for ($i = 0; $i < $number; $i++){
    fscanf(STDIN, "%d %d", $x, $y);
    if (abs($x - $y) <= 1)
        array_push($result, $x + $y);
    else
        array_push($result, max($x, $y) * 2 - 1);
}

for ($i = 0; $i < $number; $i++){
    echo $result[$i];
    if ($i < $number - 1)
        echo "\n";
}
?>


You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.You have to find minimum k such that there exists at least one k-dominant character.

The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

Print one number — the minimum value of k such that there exists at least one k-dominant character.


Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position".Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.

The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively. Next follow n non-empty strings that are uploaded to the memory of the mechanism. Next follow m non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.

For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).

<?php

$f = fopen('php://stdin', 'r');
//$f = fopen('test.txt', 'r');

$s = trim(fgets($f));

list($n,$m) = explode(" ",$s);

$ar = array();

$mod = pow(2,16)+7;

function mehash($s){
    global $mod;
    $p = 3;
    $hash = 0;
    //hash += (s[i] - 'a' + 1) * p_pow;
	//p_pow *= p;
    $p_pow = 1;
    for($i=0;$i<strlen($s);$i++){
        $v = 0;
        switch($s[$i]){
            case 'a':
                $v = 0;
                break;
            case 'b':
                $v = 1;
                break;
            case 'c':
                $v = 2;
                break;
        }

        $hash+= (($v+1)*$p_pow % $mod);
        $p_pow*=($p % mod);
    }
    return $hash;
}

for($i=0;$i<$n;$i++){
    $s = trim(fgets($f));
    $l = strlen($s);

    for($j=0;$j<$l;$j++){

        if ($s[$j]=='a'){
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='b'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='c'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
        }
    }
}

for($i=0;$i<$m;$i++) {

    $s = trim(fgets($f));

    if (isset($ar[mehash($s)])) echo "YES\n";
    else echo "NO\n";
}

<?php

$f = fopen('php://stdin', 'r');
//$f = fopen('test.txt', 'r');

$s = trim(fgets($f));

list($n,$m) = explode(" ",$s);

$ar = array();

$mod = pow(2,16)+7;

function mehash($s){
    global $mod;
    $p = 3;
    $hash = 0;
    //hash += (s[i] - 'a' + 1) * p_pow;
	//p_pow *= p;
    $p_pow = 1;
    for($i=0;$i<strlen($s);$i++){
        $v = 0;
        switch($s[$i]){
            case 'a':
                $v = 0;
                break;
            case 'b':
                $v = 1;
                break;
            case 'c':
                $v = 2;
                break;
        }

        $hash+= (($v+1)*$p_pow % $mod);
        $p_pow*=($p % $mod);
    }
    return $hash;
}

for($i=0;$i<$n;$i++){
    $s = trim(fgets($f));
    $l = strlen($s);

    for($j=0;$j<$l;$j++){

        if ($s[$j]=='a'){
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='b'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='c'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
        }
    }
}

for($i=0;$i<$m;$i++) {

    $s = trim(fgets($f));

    if (isset($ar[mehash($s)])) echo "YES\n";
    else echo "NO\n";
}

<?php

$f = fopen('php://stdin', 'r');
//$f = fopen('test.txt', 'r');

$s = trim(fgets($f));

list($n,$m) = explode(" ",$s);

$ar = array();

function mehash($s){

    $p = 3;
    $hash = 0;
    //hash += (s[i] - 'a' + 1) * p_pow;
	//p_pow *= p;
    $p_pow = 1;
    for($i=0;$i<strlen($s);$i++){
        $v = 0;
        switch($s[$i]){
            case 'a':
                $v = 0;
                break;
            case 'b':
                $v = 1;
                break;
            case 'c':
                $v = 2;
                break;
        }

        $hash+= (int)($v+1)*$p_pow;
        $p_pow *= $p;
    }
    return $hash;
}

for($i=0;$i<$n;$i++){
    $s = trim(fgets($f));
    $l = strlen($s);

    for($j=0;$j<$l;$j++){

        if ($s[$j]=='a'){
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='b'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='c'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
        }
    }
}

for($i=0;$i<$m;$i++) {

    $s = trim(fgets($f));

    if (isset($ar[mehash($s)])) echo "YES\n";
    else echo "NO\n";
}

<?php

//$f = fopen('php://stdin', 'r');
$begin_time = time() - 1272000000 + floatval(microtime());
$f = fopen('test.txt', 'r');

$s = trim(fgets($f));

list($n,$m) = explode(" ",$s);

$ar = array();

//10^16+3 and base 203
//$mod = 1000000007;
$pow = pow(10,16)+3;
$mode = 203;

$p = array();
$p[0] = 1;
for($i=1;$i<600000;$i++){
    $p[$i] = ($p[$i-1]*$pow)%$mod;
}

function mehash($s){
    global $mod;
    global $p;

    $hash = 0;

    for($i=0;$i<strlen($s);$i++){
        $v = 0;
        switch($s[$i]){
            case 'a':
                $v = 0;
                break;
            case 'b':
                $v = 1;
                break;
            case 'c':
                $v = 2;
                break;
        }

        $hash+=((($v+1)*$p[$i]) % $mod);
    }
    return $hash;
}

for($i=0;$i<$n;$i++){
    $s = trim(fgets($f));
    $l = strlen($s);

    for($j=0;$j<$l;$j++){

        if ($s[$j]=='a'){
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='b'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='c'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
        }
    }
}

for($i=0;$i<$m;$i++) {

    $s = trim(fgets($f));

    if (isset($ar[mehash($s)])) echo "YES\n";
    else echo "NO\n";
}

echo $end_time = time() - 1272000000 + floatval(microtime()) - $begin_time;

<?php

$f = fopen('php://stdin', 'r');
//$begin_time = time() - 1272000000 + floatval(microtime());
//$f = fopen('test.txt', 'r');

$s = trim(fgets($f));

list($n,$m) = explode(" ",$s);

$ar = array();

//10^16+3 and base 203
//$mod = 1000000007;
$pow = pow(10,16)+3;
$mode = 203;

$p = array();
$p[0] = 1;
for($i=1;$i<600000;$i++){
    $p[$i] = ($p[$i-1]*$pow)%$mod;
}

function mehash($s){
    global $mod;
    global $p;

    $hash = 0;

    for($i=0;$i<strlen($s);$i++){
        $v = 0;
        switch($s[$i]){
            case 'a':
                $v = 0;
                break;
            case 'b':
                $v = 1;
                break;
            case 'c':
                $v = 2;
                break;
        }

        $hash+=((($v+1)*$p[$i]) % $mod);
    }
    return $hash;
}

for($i=0;$i<$n;$i++){
    $s = trim(fgets($f));
    $l = strlen($s);

    for($j=0;$j<$l;$j++){

        if ($s[$j]=='a'){
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='b'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'c',$j,1))] = 1;
        }

        if ($s[$j]=='c'){
            $ar[mehash(substr_replace($s,'a',$j,1))] = 1;
            $ar[mehash(substr_replace($s,'b',$j,1))] = 1;
        }
    }
}

for($i=0;$i<$m;$i++) {

    $s = trim(fgets($f));

    if (isset($ar[mehash($s)])) echo "YES\n";
    else echo "NO\n";
}

//echo $end_time = time() - 1272000000 + floatval(microtime()) - $begin_time;


Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem.There are $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ on Bob's homework paper, where $$$n$$$ is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least $$$2$$$ numbers. Suppose the numbers are divided into $$$m$$$ groups, and the sum of the numbers in the $$$j$$$-th group is $$$s_j$$$. Bob's aim is to minimize the sum of the square of $$$s_j$$$, that is $$$$$$\sum_{j = 1}^{m} s_j^2.$$$$$$Bob is puzzled by this hard problem. Could you please help him solve it?

The first line contains an even integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$), denoting that there are $$$n$$$ integers on Bob's homework paper. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^4$$$), describing the numbers you need to deal with.

A single line containing one integer, denoting the minimum of the sum of the square of $$$s_j$$$, which is $$$$$$\sum_{i = j}^{m} s_j^2,$$$$$$ where $$$m$$$ is the number of groups.

NoteIn the first sample, one of the optimal solutions is to divide those $$$4$$$ numbers into $$$2$$$ groups $$$\{2, 8\}, \{5, 3\}$$$. Thus the answer is $$$(2 + 8)^2 + (5 + 3)^2 = 164$$$.In the second sample, one of the optimal solutions is to divide those $$$6$$$ numbers into $$$3$$$ groups $$$\{1, 2\}, \{1, 2\}, \{1, 2\}$$$. Thus the answer is $$$(1 + 2)^2 + (1 + 2)^2 + (1 + 2)^2 = 27$$$.

<?
$n = trim(fgets(STDIN));

$arr = $arr = explode(' ',trim(fgets(STDIN)));
sort($arr,SORT_NUMERIC);

$sum = 0 ;
//print_r($arr);
for($i=0;$i<$n/2;$i++){
	//echo $arr[$i]." ".$arr[$n-$i-1]."\n";
	$sum += ($arr[$i]+$arr[$n-$i-1])*($arr[$i]+$arr[$n-$i-1]);
}

echo $sum;


It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .You should write a program which will calculate average sleep times of Polycarp over all weeks.

The first line contains two integer numbers n and k (1 ≤ k ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output average sleeping time over all weeks.  The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.

NoteIn the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.


Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.In the beginning, uncle Bogdan wrote on a board a positive integer $$$x$$$ consisting of $$$n$$$ digits. After that, he wiped out $$$x$$$ and wrote integer $$$k$$$ instead, which was the concatenation of binary representations of digits $$$x$$$ consists of (without leading zeroes). For example, let $$$x = 729$$$, then $$$k = 111101001$$$ (since $$$7 = 111$$$, $$$2 = 10$$$, $$$9 = 1001$$$).After some time, uncle Bogdan understood that he doesn't know what to do with $$$k$$$ and asked Denis to help. Denis decided to wipe last $$$n$$$ digits of $$$k$$$ and named the new number as $$$r$$$.As a result, Denis proposed to find such integer $$$x$$$ of length $$$n$$$ that $$$r$$$ (as number) is maximum possible. If there are multiple valid $$$x$$$ then Denis is interested in the minimum one.All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?Note: in this task, we compare integers ($$$x$$$ or $$$k$$$) as numbers (despite what representations they are written in), so $$$729 < 1999$$$ or $$$111 < 1000$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the integer $$$x$$$ you need to find. It's guaranteed that the sum of $$$n$$$ from all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print the minimum integer $$$x$$$ of length $$$n$$$ such that obtained by Denis number $$$r$$$ is maximum possible.

NoteIn the second test case (with $$$n = 3$$$), if uncle Bogdan had $$$x = 998$$$ then $$$k = 100110011000$$$. Denis (by wiping last $$$n = 3$$$ digits) will obtain $$$r = 100110011$$$.It can be proved that the $$$100110011$$$ is the maximum possible $$$r$$$ Denis can obtain and $$$998$$$ is the minimum $$$x$$$ to obtain it.

<?php
$tests = readline();
$answers = [];
for($t = 1; $t <= $tests; $t++) {

	$n = readline();

	$eights = ($n % 4) ? floor($n / 4) + 1 : $n / 4;
	$nines = $n - $eights;

	$answers[]= str_pad('',$nines, '9') . str_pad('', $eights, '8');
	#echo "x: " . $answer_x . PHP_EOL;
	#echo "k (bin): " . $answer_k . PHP_EOL;
	#echo "r (bin): " . $answer_bin . PHP_EOL;
	#echo "r (dec): ". $maximum . PHP_EOL;
}
foreach ($answers as $a) echo $a.PHP_EOL;

<?php
$tests = readline();
$answers = [];
for($t = 1; $t <= $tests; $t++) {

	$n = readline();

	$eights = floor($n / 4);
	$nines = $n - $eights;

	$answers[]= str_pad('',$nines, '9') . str_pad('', $eights, '8');
	#echo "x: " . $answer_x . PHP_EOL;
	#echo "k (bin): " . $answer_k . PHP_EOL;
	#echo "r (bin): " . $answer_bin . PHP_EOL;
	#echo "r (dec): ". $maximum . PHP_EOL;
}
foreach ($answers as $a) echo $a.PHP_EOL;

<?php

$tests = readline();
$answers = [];
for($t = 1; $t <= $tests; $t++) {

	$n = readline();
	$answers[] = str_pad('',$n-1, '9') . '8';continue;
	$start = microtime(true);

	$min = pow( 10, $n - 1 );
	$max = pow( 10, $n ) - 1;

	$maximum = - 1;
	$maximum_k = -1;
	$answer = 0;
	for ( $i = $max; $i > $min; $i -- ) {

		$x = (string) $i;
		$k = '';
		for ( $j = 0; $j < strlen( $x ); $j ++ ) {
			$k .= decbin( $x[ $j ] );
		}
		echo "K= $k".PHP_EOL;

		$r = substr( $k, 0, $n * - 1 );
		if ( strlen($r)  >= $maximum ) {
			$maximum    = strlen($r);
			$answer     = $x;
		}else{
			break;
		}
	}
	$answers[]= $answer;

	$time_elapsed_secs = microtime(true) - $start;
	#echo 'Execution Time: ' .  number_format($time_elapsed_secs,2) .'s'.PHP_EOL;

	#echo "x: " . $answer . PHP_EOL;
	#echo "k (bin): " . $answer_k . PHP_EOL;
	#echo "r (bin): " . $answer_bin . PHP_EOL;
	#echo "r (dec): ". $maximum . PHP_EOL;
}
foreach ($answers as $a) echo $a.PHP_EOL;

<?php
$tests = readline();
$answers = [];
for($t = 1; $t <= $tests; $t++) {

	$n = readline();

	$eights = (floor($n / 4) + 1);
	$nines = $n - $eights;

	$answers[]= str_pad('',$nines, '9') . str_pad('', $eights, '8');
	#echo "x: " . $answer_x . PHP_EOL;
	#echo "k (bin): " . $answer_k . PHP_EOL;
	#echo "r (bin): " . $answer_bin . PHP_EOL;
	#echo "r (dec): ". $maximum . PHP_EOL;
}
foreach ($answers as $a) echo $a.PHP_EOL;

<?php

$t = readline();
$n = readline();

$min = pow(10, $n-1);
$max = pow(10, $n) - 1;
#echo "Min: " . $min . PHP_EOL;
#echo "Max: " . $max . PHP_EOL;

$maximum = -1;
for($i = $min; $i <= $max; $i++){

	$x = (string) $i;
	$k = '';
	for($j = 0; $j < strlen($x); $j++) $k .= decbin($x[$j]);
	$r = substr($k,0, $n*-1);
	if(bindec($r)>$maximum){
		$maximum = bindec($r);
		$answer_bin = $r;
		$answer_k = $k;
		$answer_x = $x;
	}
}
echo $answer_x;


You're given two arrays $$$a[1 \dots n]$$$ and $$$b[1 \dots n]$$$, both of the same length $$$n$$$.In order to perform a push operation, you have to choose three integers $$$l, r, k$$$ satisfying $$$1 \le l \le r \le n$$$ and $$$k > 0$$$. Then, you will add $$$k$$$ to elements $$$a_l, a_{l+1}, \ldots, a_r$$$.For example, if $$$a = [3, 7, 1, 4, 1, 2]$$$ and you choose $$$(l = 3, r = 5, k = 2)$$$, the array $$$a$$$ will become $$$[3, 7, \underline{3, 6, 3}, 2]$$$.You can do this operation at most once. Can you make array $$$a$$$ equal to array $$$b$$$?(We consider that $$$a = b$$$ if and only if, for every $$$1 \le i \le n$$$, $$$a_i = b_i$$$)

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 20$$$) — the number of test cases in the input. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100\ 000$$$) — the number of elements in each array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 1000$$$). The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

For each test case, output one line containing "YES" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing at most once the described operation or "NO" if it's impossible. You can print each letter in any case (upper or lower).

NoteThe first test case is described in the statement: we can perform a push operation with parameters $$$(l=3, r=5, k=2)$$$ to make $$$a$$$ equal to $$$b$$$.In the second test case, we would need at least two operations to make $$$a$$$ equal to $$$b$$$.In the third test case, arrays $$$a$$$ and $$$b$$$ are already equal.In the fourth test case, it's impossible to make $$$a$$$ equal to $$$b$$$, because the integer $$$k$$$ has to be positive.

<?php
function solve()
{
    $server = "php://stdin";
    $local = "input.txt";
    $handle = fopen($server, "r");
    $x = trim(fgets($handle));
    for ($j = 0; $j < $x; $j++) {
        $n=trim(fgets($handle));
        $qx = trim(fgets($handle));
        $arr1 = explode(' ', $qx);
        $qb = trim(fgets($handle));
        $arr2= explode(' ', $qb);
        $l=0;$r=$n-1;
        for ($i=0;$i<$n;$i++)
        {
            if($arr1[$i]!=$arr2[$i])
            {
                $r=$i;
            }
        }
        for ($i=$n-1;$i>=0;$i--)
        {
            if($arr1[$i]!=$arr2[$i])
            {
                $l=$i;
            }
        }
        $ans=1;
        $count=$arr2[$l]-$arr1[$l];
        if($count>=0)
            for ($i=$l;$i<$r+1;$i++)
            {
                if($arr2[$i]-$arr1[$i]!=$count)
                {
                    $ans=0;
                    break;
                }
            }
        else
        {
            $ans=0;
        }

        if($ans)
        {
            echo "YES\n";
        }else
        {
            echo "NO\n";
        }
    }
}

solve();
?>

<?php

$t = trim(fgets(STDIN));

$ans = '';

for ($i = 0; $i < $t; $i++) {
	$n = trim(fgets(STDIN));
	$a = explode(" ", trim(fgets(STDIN)));
	$b = explode(" ", trim(fgets(STDIN)));
	$is_diff = 0;
	$diff = 0;
	$diff0 = 0;
	$ok = 1;
	for ($j = 0; $j < $n; $j++) {
		if ($a[$j] != $b[$j])
		{
			if ($is_diff == 2)
			{
				$ok = 0;
				$ans .= "NO\n";
				break;
			}
			if ($is_diff == 0) {
				$is_diff = 1;
				$diff = $a[$j] - $b[$j];
			}
			if ($diff > 0)
			{
				$ok = 0;
				$ans .= "NO\n";
				break;
			}
			if ($a[$j] - $b[$j] != $diff) {
				$ok = 0;
				$ans .= "NO\n";
				break;
			}
		} else {
			if ($is_diff == 1)
				$is_diff = 2;
		}
	}
	if ($ok == 1)
		$ans .= "YES\n";
}

echo $ans;
?>

<?php
function solve()
{
    $server = "php://stdin";
    $local = "input.txt";
    $handle = fopen($server, "r");
    $x = trim(fgets($handle));
    for ($j = 0; $j < $x; $j++) {
        $n=trim(fgets($handle));
        $qx = trim(fgets($handle));
        $arr1 = explode(' ', $qx);
        $qb = trim(fgets($handle));
        $arr2= explode(' ', $qb);
        $flage=false;$count=0;
        $ans=1;
        for ($i=0;$i<$n;$i++)
        {
            if($arr1[$i]!=$arr2[$i] && $flage!=true)
            {
                $flage=true;
                $count=$arr2[$i]-$arr1[$i];
                if($count<0)
                {
                    $ans=0;
                    break;
                }
            }
            if($flage==true && $arr1[$i]!=$arr2[$i])
            {
                if($count!=$arr2[$i]-$arr1[$i] || ($arr2[$i]-$arr1[$i])<0) {
                    $ans=0;
                    break;
                }
            }
        }
        if($ans)
        {
            echo "YES\n";
        }else
        {
            echo "NO\n";
        }

    }
}

solve();
?>

<?php

$t = trim(fgets(STDIN));

$ans = '';

for ($i = 0; $i < $t; $i++) {
	$n = trim(fgets(STDIN));
	$a = explode(" ", trim(fgets(STDIN)));
	$b = explode(" ", trim(fgets(STDIN)));
	$is_diff = 0;
	$diff = 0;
	$ok = 1;
	for ($j = 0; $j < $n; $j++) {
		if ($a[$j] != $b[$j])
		{
			if ($is_diff == 2)
			{
				$ok = 0;
				$ans .= "NO\n";
				break;
			}
			$is_diff = 1;
			$diff = $a[$j] - $b[$j];
			if ($diff > 0)
			{
				$ok = 0;
				$ans .= "NO\n";
				break;
			}
			if ($a[$j] - $b[$j] != $diff) {
				$ok = 0;
				$ans .= "NO\n";
				break;
			}
		} else {
			if ($is_diff == 1)
				$is_diff = 2;
		}
	}
	if ($ok == 1)
		$ans .= "YES\n";
}

echo $ans;
?>


Let's define the value of the permutation $$$p$$$ of $$$n$$$ integers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once) as follows:  initially, an integer variable $$$x$$$ is equal to $$$0$$$;  if $$$x < p_1$$$, then add $$$p_1$$$ to $$$x$$$ (set $$$x = x + p_1$$$), otherwise assign $$$0$$$ to $$$x$$$;  if $$$x < p_2$$$, then add $$$p_2$$$ to $$$x$$$ (set $$$x = x + p_2$$$), otherwise assign $$$0$$$ to $$$x$$$;  ...  if $$$x < p_n$$$, then add $$$p_n$$$ to $$$x$$$ (set $$$x = x + p_n$$$), otherwise assign $$$0$$$ to $$$x$$$;  the value of the permutation is $$$x$$$ at the end of this process. For example, for $$$p = [4, 5, 1, 2, 3, 6]$$$, the value of $$$x$$$ changes as follows: $$$0, 4, 9, 0, 2, 5, 11$$$, so the value of the permutation is $$$11$$$.You are given an integer $$$n$$$. Find a permutation $$$p$$$ of size $$$n$$$ with the maximum possible value among all permutations of size $$$n$$$. If there are several such permutations, you can print any of them.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 97$$$) — the number of test cases. The only line of each test case contains one integer $$$n$$$ ($$$4 \le n \le 100$$$).

For each test case, print $$$n$$$ integers — the permutation $$$p$$$ of size $$$n$$$ with the maximum possible value among all permutations of size $$$n$$$.


Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes. Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≤ l1, i ≤ r1, i ≤ 109) — the i-th variant of a period of time when Anton can attend chess classes. The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes. Each of the following m lines of the input contains two integers l2, i and r2, i (1 ≤ l2, i ≤ r2, i ≤ 109) — the i-th variant of a period of time when Anton can attend programming classes.

Output one integer — the maximal possible distance between time periods.

NoteIn the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.

<?php
$n = trim(fgets(STDIN));
$fMin = -1;
$fMax = -1;
for($i = 0; $i < $n; $i++) {
    list($a, $b) = explode(' ', trim(fgets(STDIN)));
    if($fMax == -1 || $fMax < $a) {
        $fMax = $a;
    }
    if($fMin == -1 || $fMin > $b) {
        $fMin = $b;
    }
}
$m = trim(fgets(STDIN));
$sMin = -1;
$sMax = -1;
for($i = 0; $i < $m; $i++) {
    list($a, $b) = explode(' ', trim(fgets(STDIN)));
    if($sMax == -1 || $sMax < $a) {
        $sMax = $a;
    }
    if($sMin == -1 || $sMin > $b) {
        $sMin = $b;
    }
}
$r = 0;
if($fMin < $sMax) {
    $r = max($r, $sMax - $fMin);
}
if($sMin < $fMax) {
    $r = max($r, $fMax - $sMin);
}
echo $r;

<?php
$n = trim(fgets(STDIN));
$fMin = -1;
$fMax = -1;
for($i = 0; $i < $n; $i++) {
    list($a, $b) = explode(' ', trim(fgets(STDIN)));
    if($fMax == -1 || $fMax < $a) {
        $fMax = $a;
    }
    if($fMin == -1 || $fMin > $b) {
        $fMin = $b;
    }
}
$m = trim(fgets(STDIN));
$sMin = -1;
$sMax = -1;
for($i = 0; $i < $m; $i++) {
    list($a, $b) = explode(' ', trim(fgets(STDIN)));
    if($sMax == -1 || $sMax < $a) {
        $sMax = $a;
    }
    if($sMin == -1 || $sMin > $b) {
        $sMin = $b;
    }
}
//echo $fMin, $sMax, $sMin, $fMax;
if($fMin < $sMax) {
    echo $sMax - $fMin;
}
else if($sMin < $fMax) {
    echo $fMax - $sMin;
}
else {
    echo 0;
}


On a random day, Neko found $$$n$$$ treasure chests and $$$m$$$ keys. The $$$i$$$-th chest has an integer $$$a_i$$$ written on it and the $$$j$$$-th key has an integer $$$b_j$$$ on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.The $$$j$$$-th key can be used to unlock the $$$i$$$-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, $$$a_i + b_j \equiv 1 \pmod{2}$$$. One key can be used to open at most one chest, and one chest can be opened at most once.Find the maximum number of chests Neko can open.

The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^5$$$) — the number of chests and the number of keys. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the numbers written on the treasure chests. The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \leq b_i \leq 10^9$$$) — the numbers written on the keys.

Print the maximum number of chests you can open.

NoteIn the first example, one possible way to unlock $$$3$$$ chests is as follows:  Use first key to unlock the fifth chest,  Use third key to unlock the second chest,  Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).In the third example, no key can unlock the given chest.

<?php
# 1152A

fgets(STDIN);
$coc = $cec = 0;
$_cs = explode(' ', fgets(STDIN));
foreach ($_cs as $_c) {if ((int)$_c % 2 === 0) {$cec++;} else {$coc++;}}
unset($_cs);
$koc = $kec = 0;
$_ks = explode(' ', fgets(STDIN));
foreach ($_ks as $_k) {if ((int)$_k % 2 === 0) {$kec++;} else {$koc++;}}
unset($_ks);
echo min($cec, $koc) + min($coc, $kec);

<?php
  $n = explode(" ", trim(fgets(STDIN)));
  $m = explode(" ", trim(fgets(STDIN)));
  $z = explode(" ", trim(fgets(STDIN)));
  $jup = 0;
  $tak = 0;
  $summ = 0;
 foreach($m as $sK => $sV) {
     if($sV % 2 == 0) {
         $jup++;
     }
 }
 foreach($z as $sK1 => $sV1) {
     if($sV1 % 2 !== 0){
         $tak++;
     }
 }
 $a = $n[0] - $jup;
 $b = $n[1] - $tak;
 if($a == 0 && $tak == 0) { echo '0'; exit;}
 if($b == 0 && $jup == 0) { echo '0'; exit;}
 if($jup > $tak){
     $summ += $tak;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 } else {
     $summ += $jup;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 }
  echo $summ;

?>

<?php
list($n,$b) = explode(' ', trim(fgets(STDIN)));
$aChests = explode(' ', trim(fgets(STDIN)));
$aKeys = explode(' ', trim(fgets(STDIN)));

$iEven = $iOdd = 0; $jEven = $jOdd = 0; $iTotal = 0;
foreach ($aKeys as $sK => $sV){
    $sV % 2 === 1 ? $iEven++ : $iOdd++;
}

foreach ($aChests as $sK => $sVal){
    $sVal % 2 === 1 ? $jEven++ : $jOdd++;
}

if($jOdd >= $iEven){
    $iTotal += $iEven;
} else {
    $iTotal += $jOdd;
}

if($jEven >= $iOdd){
    $iTotal += $iOdd;
} else {
    $iTotal += $jEven;
}
echo $iTotal;

<?php
# 1152A

$input = fopen('php://stdin', 'rb');
[$chestsCount, $keysCount] = fscanf($input, '%u %u' . "\n");
$chestsOddCount = $chestsEvenCount = 0;
$chests = explode(' ', trim(fgets($input)));
foreach ($chests as $chest) {
    if ($chest % 2 === 0) {
        $chestsEvenCount++;
    } else {
        $chestsOddCount++;
    }
}
unset($chests);
$keysOddCount = $keysEvenCount = 0;
$keys = explode(' ', trim(fgets($input)));
foreach ($keys as $key) {
    if ($key % 2 === 0) {
        $keysEvenCount++;
    } else {
        $keysOddCount++;
    }
}
unset($keys);
echo min($chestsEvenCount, $keysOddCount) + min($chestsOddCount, $keysEvenCount);

<?php
  $n = explode(" ", trim(fgets(STDIN)));
  $m = explode(" ", trim(fgets(STDIN)));
  $z = explode(" ", trim(fgets(STDIN)));
  $jup = 0;
  $tak = 0;
  $summ = 0;
 foreach($m as $sK => $sV) {
     if($sV % 2 == 0) {
         $jup++;
     }
 }
 foreach($z as $sK1 => $sV1) {
     if($sV1 % 2 !== 0){
         $tak++;
     }
 }
 $a = $n[0] - $jup;
 $b = $n[1] - $tak;
 if($ja == 0 || $tak == 0) { echo '0'; exit;}
 if($jb == 0 || $jup == 0) { echo '0'; exit;}
 if($jup > $tak){
     $summ += $tak;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 } else {
     $summ += $jup;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 }
  echo $summ;

?>

<?php
  $n = explode(" ", trim(fgets(STDIN)));
  $m = explode(" ", trim(fgets(STDIN)));
  $z = explode(" ", trim(fgets(STDIN)));
  $jup = 0;
  $tak = 0;
  $summ = 0;
 foreach($m as $sK => $sV) {
     if($sV % 2 == 0){
         $jup++;
     }
 }
 foreach($z as $sK1 => $sV1) {
     if($sV1 % 2 !== 0){
         $tak++;
     }
 }
 $a = $n[0] - $jup;
 $b = $n[1] - $tak;
 if($jup == 0 || $tak == 0) { echo '0'; exit;}
 if($jup > $tak){
     $summ += $tak;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 } else {
     $summ += $jup;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 }
  echo $summ;

?>

<?php
  $n = explode(" ", trim(fgets(STDIN)));
  $m = explode(" ", trim(fgets(STDIN)));
  $z = explode(" ", trim(fgets(STDIN)));
  $jup = 0;
  $tak = 0;
  $summ = 0;
 foreach($m as $sK => $sV) {
     if($sV % 2 == 0) {
         $jup++;
     }
 }
 foreach($z as $sK1 => $sV1) {
     if($sV1 % 2 !== 0){
         $tak++;
     }
 }
 $a = $n[0] - $jup;
 $b = $n[1] - $tak;
 if($a == 0 || $tak == 0) { echo '0'; exit;}
 if($b == 0 || $jup == 0) { echo '0'; exit;}
 if($jup > $tak){
     $summ += $tak;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 } else {
     $summ += $jup;
     if($a > $b) {
         $summ += $b;
     } else {
         $summ += $a;
     }
 }
  echo $summ;

?>


We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $$$a$$$ yuan for $$$b$$$ kilos (You don't need to care about what "yuan" is), the same as $$$a/b$$$ yuan for a kilo.Now imagine you'd like to buy $$$m$$$ kilos of apples. You've asked $$$n$$$ supermarkets and got the prices. Find the minimum cost for those apples.You can assume that there are enough apples in all supermarkets.

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 5\,000$$$, $$$1 \leq m \leq 100$$$), denoting that there are $$$n$$$ supermarkets and you want to buy $$$m$$$ kilos of apples. The following $$$n$$$ lines describe the information of the supermarkets. Each line contains two positive integers $$$a, b$$$ ($$$1 \leq a, b \leq 100$$$), denoting that in this supermarket, you are supposed to pay $$$a$$$ yuan for $$$b$$$ kilos of apples.

The only line, denoting the minimum cost for $$$m$$$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $$$10^{-6}$$$. Formally, let your answer be $$$x$$$, and the jury's answer be $$$y$$$. Your answer is considered correct if $$$\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}$$$.

NoteIn the first sample, you are supposed to buy $$$5$$$ kilos of apples in supermarket $$$3$$$. The cost is $$$5/3$$$ yuan.In the second sample, you are supposed to buy $$$1$$$ kilo of apples in supermarket $$$2$$$. The cost is $$$98/99$$$ yuan.

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = 1000000;
for($x = 0; $x < $a; $x++)
{
     list($d, $e) = explode(" ", trim(fgets(STDIN)));
     $e = $d / $e * $b;
     $c = min($c, $e);
}
print number_format($c, 8, '.', '');
?>

<?php

// ჰაიტ.

// აგამემნონი  :3

// მენელაოსის ძმა. ბერძენთა წინამძღოლი ტროას ომში.  ელენას მაზლი :3
// ნუ.. ფილმში ძაან ჩათლახადაა გამოყვანილი ((:

$agamemnoni = 1234321;

list($a,$b) = explode(" ",trim(fgets(STDIN)));

for($i=0;$i<$a;$i++){
    list($x,$y) = explode(" ",trim(fgets(STDIN)));
    $agamemnoni = min($agamemnoni, $x/$y);
}

$agamemnoni*=$b;

echo $agamemnoni;

?>

<?php

fscanf(STDIN, "%d %d\n", $n, $m);
$best_price = 5000 * 100 + 1.0;
while($n--){
  fscanf(STDIN, "%d %d\n", $a, $b);
  $best_price = min($best_price, ($a/$b));
}
echo ($best_price * $m);


You are given one integer $$$n$$$ ($$$n > 1$$$).Recall that a permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation of length $$$5$$$, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n = 3$$$ but there is $$$4$$$ in the array).Your task is to find a permutation $$$p$$$ of length $$$n$$$ that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied).You have to answer $$$t$$$ independent test cases.If there are several answers, you can print any. It can be proven that the answer exists for each $$$n > 1$$$.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the permutation you have to find.

For each test case, print $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ — a permutation that there is no index $$$i$$$ ($$$1 \le i \le n$$$) such that $$$p_i = i$$$ (so, for all $$$i$$$ from $$$1$$$ to $$$n$$$ the condition $$$p_i \ne i$$$ should be satisfied). If there are several answers, you can print any. It can be proven that the answer exists for each $$$n > 1$$$.

<?php
/**
 * Created by PhpStorm.
 * User: ordec
 * Date: 24/11/20
 * Time: 20:39
 */

$n = readline();
$res = array();
$cnt = 0;
for ($i = 0; $i < $n; $i++) {

    $num = readline();
    for ($j = 2; $j <= $num; $j++) {
        $res[$cnt][$j] = $j;
    }
//    $res[$cnt][$num] = "\n";
    $res[$cnt][1] = 1;
    $cnt++;
}

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

<?php
$t = trim(fgets(STDIN));

while ($t--) {
    $n = trim(fgets(STDIN));
    $res = '';
    for ($i = 2; $i <= $n; $i++) {
        $res.= $i.' ';
    }

    echo $res."1\n";
}

<?php
fscanf(STDIN, "%d", $n);
for ($i = 0; $i < $n; $i++) {
    $p = range(1, intval(fgets(STDIN)));
    $p[] = array_shift($p);
    printf("%s \n", implode(' ', $p));
}

<?php

$t = intval(readline());

for ($i = 0; $i < $t; $i++) {
    $n = intval(readline());
    $isOdd = $n % 2 === 1;
    $middle = ($n + 1) / 2;
    for ($j = $n; $j >= 1; $j--) {
        if ($isOdd && $j === 1) {
            echo "$middle ";
        } elseif ($isOdd && $j === $middle) {
            echo "1 ";
        } else {
            echo "$j ";
        }
    }
    echo "\n";
}

<?php
/**
 * Created by PhpStorm.
 * User: ordec
 * Date: 24/11/20
 * Time: 20:39
 */

$n = readline();
$res = array();
$cnt = 0;
for ($i = 0; $i < $n; $i++) {

    $num = readline();
    for ($j = 2; $j <= $num; $j++) {
        $res[$cnt][$j] = $j;
    }
    $res[$num] = "\n";
    $res[$cnt][1] = 1;
    $cnt++;
}

foreach ($res as $items) {
    foreach ($items as $item){
        echo $item . ' ';
    }
}

<?php
$t = trim(fgets(STDIN));

while ($t--) {
    $n = trim(fgets(STDIN));
    $res = '';
    for ($i = $n; $i > 0; $i--) {
        $res.=$i.' ';
    }
    echo trim($res)."\n";
}

<?php
fscanf(STDIN,"%d",$tc);
while($tc--)
{
	fscanf(STDIN,"%d",$n);
	for ($i=$n; $i>=1 ; $i--)
	 {
	 	if($i==1)
		echo "$i"."\n";
	    else
	    	echo "$i ";
	 }
}

<?php

$t = intval(readline());

for ($i = 0; $i < $t; $i++) {
    $n = intval(readline());
    $isOdd = $n % 2 === 1;
    $middle = ($n + 1) / 2;
    for ($j = $n; $j >= 1; $j--) {
        if ($isOdd && $j === $n) {
            echo "$middle ";
        } elseif ($isOdd && $j === $middle) {
            echo "1 ";
        } else {
            echo "$j ";
        }
    }
    echo "\n";
}


One day n cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move i-th cell can exchange it's value with the value of some other j-th cell, if |i - j| = di, where di is a favourite number of i-th cell. Cells make moves in any order, the number of moves is unlimited.The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to n. You are to determine whether the game could move to this state.

The first line contains positive integer n (1 ≤ n ≤ 100) — the number of cells in the array. The second line contains n distinct integers from 1 to n — permutation. The last line contains n integers from 1 to n — favourite numbers of the cells.

If the given state is reachable in the described game, output YES, otherwise NO.