Polycarp's workday lasts exactly $$$n$$$ minutes. He loves chocolate bars and can eat one bar in one minute. Today Polycarp has $$$k$$$ bars at the beginning of the workday.In some minutes of the workday Polycarp has important things to do and in such minutes he is not able to eat a chocolate bar. In other minutes he can either eat or not eat one chocolate bar. It is guaranteed, that in the first and in the last minutes of the workday Polycarp has no important things to do and he will always eat bars in this minutes to gladden himself at the begining and at the end of the workday. Also it is guaranteed, that $$$k$$$ is strictly greater than $$$1$$$.Your task is to determine such an order of eating chocolate bars that the maximum break time between eating bars is as minimum as possible.Consider that Polycarp eats a bar in the minute $$$x$$$ and the next bar in the minute $$$y$$$ ($$$x < y$$$). Then the break time is equal to $$$y - x - 1$$$ minutes. It is not necessary for Polycarp to eat all bars he has.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 200\,000$$$, $$$2 \le k \le n$$$) — the length of the workday in minutes and the number of chocolate bars, which Polycarp has in the beginning of the workday. The second line contains the string with length $$$n$$$ consisting of zeros and ones. If the $$$i$$$-th symbol in the string equals to zero, Polycarp has no important things to do in the minute $$$i$$$ and he can eat a chocolate bar. In the other case, Polycarp is busy in the minute $$$i$$$ and can not eat a chocolate bar. It is guaranteed, that the first and the last characters of the string are equal to zero, and Polycarp always eats chocolate bars in these minutes.

Print the minimum possible break in minutes between eating chocolate bars.

NoteIn the first example Polycarp can not eat the chocolate bar in the second minute, so the time of the break equals to one minute.In the second example Polycarp will eat bars in the minutes $$$1$$$ and $$$8$$$ anyway, also he needs to eat the chocolate bar in the minute $$$5$$$, so that the time of the maximum break will be equal to $$$3$$$ minutes.

<?php
  // ( $N, $K ) is not allowed........... claimed by compiler being parsing error
  list($N, $K) = explode(" ", fgets(STDIN));
  $N = (int) $N;
  $K = (int) $K - 2;
  $seq = fgets(STDIN);
  $rp = array(); // rp[ i ] : maximum j such that j ≤ i and seq[ j ] == 0
  for($i = 0, $prev = 0; $i < $N; ++$i) {
    if($seq[$i] == '0') {
      $prev = $i;
    }
    array_push($rp, $prev);
  }
  $lb = 0;
  $ub = $N - 2;
  $ans = $ub;
  while($lb <= $ub) {
    $mid = floor(($lb + $ub) / 2);
    $cost = 0;
    for($i = 1, $prev = 0; $i + 1 < $N; ++$i) {
      $lim = $prev + $mid + 1;
      if($lim >= $N - 1) break;
      if($seq[$i] == '1') {
        if($lim == $i) {
          $cost = $K + 1;
        }
      }
      if($rp[$lim] == $i) {
        ++$cost;
        $prev = $i;
      }
    }
    if($cost <= $K) {
      $ans = $mid;
      $ub = $mid - 1;
    } else {
      $lb = $mid + 1;
    }
  }
  echo $ans;
?>

<?php

list($n, $k) = explode(" ", fgets(STDIN));
list($s) = explode(" ", fgets(STDIN));
$n = (int) $n;
$k = (int) $k;
$ansl = (int) 0;
$ansr = (int) $n - 2;

while($ansr > $ansl)
{
    $intv = (int) (($ansl + $ansr) / 2);
    $r = $k - 1;

    for($i = 0; $i < $n - 1;)
    {
        $pos = -1;

        for($j = min($i + $intv + 1, $n - 1); $j >= $i + 1; --$j)
        if($s[$j] == '0')
        {
            $pos = $j;
            break;
        }

        if($pos == -1 || $r == 0)
        {
            $pos = -1;
            break;
        }
        else
        {
            --$r;
            $i = $pos;
        }
    }

    if($pos != -1)
        $ansr = $intv;
    else
        $ansl = $intv + 1;
}

echo $ansr;

?>

<?php
 list($n, $k) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $k = (int) $k;
 $s = fgets(STDIN);
 $b = 0;
 $e = $n - 2;
 while ($b + 1 < $e) {
     $mm = floor(($b + $e) / 2);
     $q = true;
     $kk = $k - 2;
     $lastq = 0;
     $possible = -1;
     for ($i = 1; $i < strlen($s) - 1; $i++) {
         if ($s[$i] == '0') $possible = $i;
         if ($i - $lastq - 1 == $mm) {
             if ($kk > 0 && $possible != -1) {
                 $kk--;
                 $lastq = $possible;
                 $possible = -1;
             } else {
                 $q = false;
                 break;
             }
         }
     }
     if ($q && $n - $lastq - 2 > $mm) $q = false;
     if ($q) $e = $mm; else $b = $mm;
 }
 $qq = true;
 for ($i = 0; $i < $n; $i++) {
     if ($s[$i] == '1') {
         $qq = false;
     }
 }
 if ($qq && $n == $k) $e = 0;
 echo $e;
?>

<?php
 list($n, $k) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $k = (int) $k;
 $s = fgets(STDIN);
 $b = -1;
 $e = $n;
 while ($b + 1 < $e) {
     $mm = floor(($b + $e) / 2);
     $q = true;
     $kk = $k - 2;
     $lastq = 0;
     $possible = -1;
     for ($i = 1; $i < strlen($s) - 1; $i++) {
         if ($s[$i] == '0') $possible = $i;
         if ($i - $lastq > $mm) {
             if ($kk > 0 && $possible != -1) {
                 $kk--;
                 $lastq = $possible;
                 $possible = -1;
             } else {
                 $q = false;
                 break;
             }
         }
     }
     if ($q && $n - $lastq - 2 > $mm) $q = false;
     if ($q) $e = $mm; else $b = $mm;
 }
 echo $e;
?>

<?php
 list($n, $k) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $k = (int) $k;
 $s = fgets(STDIN);
 $b = 0;
 $e = $n - 2;
 while ($b + 1 < $e) {
     $mm = floor(($b + $e) / 2);
     $q = true;
     $kk = $k - 2;
     $lastq = 0;
     $possible = -1;
     for ($i = 1; $i < strlen($s) - 1; $i++) {
         if ($s[$i] == '0') $possible = $i;
         if ($i - $lastq - 1 > $mm) {
             if ($kk > 0 && $possible != -1) {
                 $kk--;
                 $lastq = $possible;
                 $possible = -1;
             } else {
                 $q = false;
                 break;
             }
         }
     }
     if ($q && $n - $lastq - 2 > $mm) $q = false;
     if ($q) $e = $mm; else $b = $mm;
 }
 $qq = true;
 for ($i = 0; $i < $n; $i++) {
     if ($s[$i] == '1') {
         $qq = false;
     }
 }
 if ($qq && $n == $k) $e = 0;
 echo $e;
?>

<?php
 list($n, $k) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $k = (int) $k;
 $s = fgets(STDIN);
 $b = 0;
 $e = $n - 2;
 while ($b + 1 < $e) {
     $mm = floor(($b + $e) / 2);
     $q = true;
     $kk = $k - 2;
     $lastq = 0;
     $possible = -1;
     for ($i = 1; $i < strlen($s) - 1; $i++) {
         if ($s[$i] == '0') $possible = $i;
         if ($i - $lastq - 1 > $mm) {
             if ($kk > 0 && $possible != -1) {
                 $kk--;
                 $lastq = $possible;
                 $possible = -1;
             } else {
                 $q = false;
                 break;
             }
         }
     }
     if ($q && $n - $lastq - 1 > $mm) $q = false;
     if ($q) $e = $mm; else $b = $mm;
 }
 $qq = true;
 for ($i = 0; $i < $n; $i++) {
     if ($s[$i] == '1') {
         $qq = false;
     }
 }
 if ($qq && $n == $k) $e = 0;
 echo $e;
?>

<?php
 list($n, $k) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $k = (int) $k;
 $s = fgets(STDIN);
 $b = 0;
 $e = $n - 2;
 while ($b + 1 < $e) {
     $mm = floor(($b + $e) / 2);
     $q = true;
     $kk = $k - 2;
     $lastq = 0;
     $possible = -1;
     for ($i = 1; $i < strlen($s) - 1; $i++) {
         if ($s[$i] == '0') $possible = $i;
         if ($i - $lastq > $mm) {
             if ($kk > 0 && $possible != -1) {
                 $kk--;
                 $lastq = $possible;
                 $possible = -1;
             } else {
                 $q = false;
                 break;
             }
         }
     }
     if ($q && $n - $lastq - 2 > $mm) $q = false;
     if ($q) $e = $mm; else $b = $mm;
 }
 $qq = true;
 for ($i = 0; $i < $n; $i++) {
     if ($s[$i] == '1') {
         $qq = false;
     }
 }
 if ($qq && $n == $k) $e = 0;
 echo $e;
?>


This problem is same as the previous one, but has larger constraints.Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.For each of the $$$n$$$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $$$i$$$-th day has a ribbon with color $$$u_i$$$. Shiro wants to know the largest number $$$x$$$, such that if we consider the streak of the first $$$x$$$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $$$x - 1$$$ will have the same number of occurrences.For example, consider the following sequence of $$$u_i$$$: $$$[2, 2, 1, 1, 5, 4, 4, 5]$$$. Then $$$x = 7$$$ makes a streak, since if we remove the leftmost $$$u_i = 5$$$, each ribbon color will appear exactly twice in the prefix of $$$x - 1$$$ days. Note that $$$x = 8$$$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the total number of days. The second line contains $$$n$$$ integers $$$u_1, u_2, \ldots, u_n$$$ ($$$1 \leq u_i \leq 10^5$$$) — the colors of the ribbons the cats wear.

Print a single integer $$$x$$$ — the largest possible streak of days.

NoteIn the first example, we can choose the longest streak of $$$13$$$ days, since upon removing the last day out of the streak, all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$ will have the same number of occurrences of $$$3$$$. Note that the streak can also be $$$10$$$ days (by removing the $$$10$$$-th day from this streak) but we are interested in the longest streak.In the fourth example, if we take the streak of the first $$$6$$$ days, we can remove the third day from this streak then all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ will occur exactly once.


You are given a string A. Find a string B, where B is a palindrome and A is a subsequence of B.A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, "cotst" is a subsequence of "contest".A palindrome is a string that reads the same forward or backward.The length of string B should be at most 104. It is guaranteed that there always exists such string.You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104.

First line contains a string A (1 ≤ |A| ≤ 103) consisting of lowercase Latin letters, where |A| is a length of A.

Output single line containing B consisting of only lowercase Latin letters. You do not need to find the shortest answer, the only restriction is that the length of string B should not exceed 104. If there are many possible B, print any of them.

NoteIn the first example, "aba" is a subsequence of "aba" which is a palindrome.In the second example, "ab" is a subsequence of "aabaa" which is a palindrome.


Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?

The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn't contain any leading zeroes.

Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.

NoteIn the first sample the number already is one-digit — Herald can't cast a spell.The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit.

<?php
$a = trim(fgets(STDIN));
$b = str_split($a);
$c = 0;
for($x = 1; $x <= count($b); $x++)
{
    if(count($b) > 1)
    {
        $c++;
        $b = str_split(array_sum($b));
        $x = 1;
    }
    else
    {
        break;
    }
}
print $c;
?>

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

$input = (string) $input;
if((int)$input < 10) die('0');
if((int)$input == 10) die('1');

function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}
$c = 0;
while($input >= 10)
{
    $input = numsum($input);
    $c++;
}

echo $c;

<?php
//$src = 'input.txt';
$src = 'php://stdin';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
$data = (string) array_sum(str_split($data));
	$i++;
}
echo $i;

<?php

function digitalRoot($str, $len){
    $sum = 0;
    for($i = 0; $i < $len; $i++){
        $sum += $str[$i];
    }
    return strval($sum);
}

while (FALSE !== ($line = fgets(STDIN))) {

    $tLine = trim($line);
    $count = 0;

    while(1){
        $len = strlen($tLine);
        if($len > 1){
            $count++;
            $tLine = digitalRoot($tLine, $len);
        }else{
            break;
        }
    }
    echo $count . PHP_EOL;
}

<?php
fscanf(STDIN, "%s",$s);
function sumstr($s){
for ($i=0;$i<strlen($s);$i++)
    $sum += (int)$s[$i];
  return $sum;
}

$cnt=0;

while (strlen($s)>1){
 $s = sumstr("".$s);
 $cnt++;
}

echo $cnt;

?>

<?
$str=trim(fgets(STDIN));
$sum=0;
if(strlen($str)==1) $cnt=0;
else $cnt=1;
for($i=0; $i<strlen($str); $i++)
    $sum+=$str[$i];
while($sum>9){
    $str=(string)$sum;
    $sum=0;
    for($i=0; $i<strlen($str); $i++)
        $sum+=$str[$i];
    $cnt++;
}
echo $cnt;
?>

<?
$fp=fopen('php://stdin', 'r');
$lines=stream_get_contents($fp);
$lines=trim($lines);

$res=0;

while(strlen($lines)>1)
{
    $new=0;
    for ($i=0; $i<strlen($lines); $i++)
        $new+=$lines{$i};
    $lines=$new;
    $lines=(string)$lines;
    $res++;
}

echo $res;
?>

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}

$input = (string) $input;

if((int)$input < 10) die('0');

$sum = numsum($input);
$c = 1;

while($sum > 10)
{
    $sum = numsum($sum);
    $c++;
}

echo $c;

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}

$input = (string) $input;
$sum = numsum($input);
$c = 1;

if($sum <= 10) die('0');

while($sum >= 10)
{
    $sum = numsum($sum);
    $c++;
}

echo $c;

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}

$input = (string) $input;
$sum = numsum($input);
$c = 1;

if($sum < 10) die('0');

while($sum >= 10)
{
    $sum = numsum($sum);
    $c++;
}

echo $c;

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}

$input = (string) $input;
$sum = numsum($input);
$c = 1;

if($sum < 10) die('0');

while($sum > 10)
{
    $sum = numsum($sum);
    $c++;
}

echo $c;

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}

$input = (string) $input;
$sum = numsum($input);
$c = 1;

if($sum <= 10) die('1');

while($sum > 10)
{
    $sum = numsum($sum);
    $c++;
}

echo $c;

<?php
while($s = fgets(STDIN)){
    $input .= $s;
}

$input = (double) $input;
function numsum($i)
{
    $i = (string)$i;
    $s = (double)0;
    for($a = 0; $a<strlen($i); $a++)
    {
        $s += (double)$i[$a];
    }
    return $s;
}
$c = 0;
while($input >= 10)
{
    $input = numsum($input);
    $c++;
}

echo $c;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += (int) $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i . "\n";

<?php

//$src = 'input.txt';
$src = 'php://input';

$h = fopen($src, 'r');

$data = 0;
$non_zero = false;
while ($c = fgetc($h)) {
	if ($data) {
		$non_zero = true;
	}
	$data += (int) $c;
}
fclose($h);
if (!$non_zero) {
	die('0');
}
$i = 1;
$data = (string) $data;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += (int) $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i . "\n";

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
	$data = (string) ((int) array_sum(str_split($data)));
	$i++;
}

echo $i;

<?php

//$src = 'input.txt';
$src = 'php://input';

$h = fopen($src, 'r');

$data = 0;
$non_zero = false;
while (($c = fgetc($h)) !== false) {
	if ($data) {
		$non_zero = true;
	}
	$data += (int) $c;
}
fclose($h);
if (!$non_zero) {
	die('0');
}
$i = 1;
$data = (string) $data;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += (int) $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i . "\n";

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += (int) $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i;
echo PHP_EOL;

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += (int) $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i . "\r\n";

<?php

//$src = 'input.txt';
$src = 'php://input';

$h = fopen($src, 'r');

$data = 0;
$non_zero = false;
while ($c = fgetc($h)) {
	if ($data) {
		$non_zero = true;
	}
	$data += (int) $c;
}
fclose($h);
if (!$non_zero) {
	die('0');
}
$i = 1;
while (strlen($data) > 1) {
	$sum = 0;
	for ($i = 0; $i < strlen($data); $i++) {
		$sum += (int) $data{$i};
	}
	$data = (string) $sum;
	$i++;
}

echo $i . "\n";

<?php

//$src = 'input.txt';
$src = 'php://input';

$data = trim(file_get_contents($src));

$i = 0;
while (strlen($data) > 1) {
	$data = (string) array_sum(str_split($data));
	$i++;
}

echo $i;


As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).It is known that the profit, the «Two Paths» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.

The first line contains an integer n (2 ≤ n ≤ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 ≤ ai, bi ≤ n).

<?php

function dfs($v, $p = -1) {
	global $g, $bestlen;
	$lens = array();
	foreach ($g[$v] as $x)
		if ($x != $p) {
			// printf("%d -> %d\n", $v, $x);
			array_push($lens, dfs($x, $v));
		}
	rsort($lens);
	$k = count($lens);
	if ($k >= 2) {
		$bestlen = max($bestlen, $lens[0] + $lens[1] + 1);
		// printf("%d: %d + %d + 1 => %d\n", $v, $lens[0], $lens[1], $bestlen);
	}
	return 1 + ($k > 0 ? $lens[0] : 0);
}

function solve($v) {
	global $bestlen;
	$bestlen = -1;
	$len = dfs($v);
	$bestlen = max($bestlen, $len);
	// printf($bestlen."\n");
	return $bestlen - 1;
}

$ans = 0;
$n = intval(fgets(STDIN));
$m = $n - 1;
for ($i = 0; $i < $m; $i++) {
	fscanf(STDIN, "%d %d", $a[$i], $b[$i]);
	$a[$i]--; $b[$i]--;
}
for ($j = 0; $j < $m; $j++) {
	$g = array_fill(0, $n, array());
	for ($i = 0; $i < $m; $i++)
		if ($i != $j) {
			array_push($g[$a[$i]], $b[$i]);
			array_push($g[$b[$i]], $a[$i]);
		}
	// printf("j = %d : (%d, %d)\n", $j, $a[$j], $b[$j]);
	$ans = max($ans, solve($a[$j]) * solve($b[$j]));
}
printf("%d\n", $ans);

?>


Initially Ildar has an empty array. He performs $$$n$$$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array. The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $$$[0, 2, 3]$$$ is $$$1$$$, while the mex of the multiset $$$[1, 2, 1]$$$ is $$$0$$$.More formally, on the step $$$m$$$, when Ildar already has an array $$$a_1, a_2, \ldots, a_{m-1}$$$, he chooses some subset of indices $$$1 \leq i_1 < i_2 < \ldots < i_k < m$$$ (possibly, empty), where $$$0 \leq k < m$$$, and appends the $$$mex(a_{i_1}, a_{i_2}, \ldots a_{i_k})$$$ to the end of the array.After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $$$a_1, a_2, \ldots, a_n$$$ the minimum step $$$t$$$ such that he has definitely made a mistake on at least one of the steps $$$1, 2, \ldots, t$$$, or determine that he could have obtained this array without mistakes.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$) — the number of steps Ildar made. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$) — the array Ildar obtained.

If Ildar could have chosen the subsets on each step in such a way that the resulting array is $$$a_1, a_2, \ldots, a_n$$$, print $$$-1$$$. Otherwise print a single integer $$$t$$$ — the smallest index of a step such that a mistake was made on at least one step among steps $$$1, 2, \ldots, t$$$.

NoteIn the first example it is possible that Ildar made no mistakes. Here is the process he could have followed.  $$$1$$$-st step. The initial array is empty. He can choose an empty subset and obtain $$$0$$$, because the mex of an empty set is $$$0$$$. Appending this value to the end he gets the array $$$[0]$$$.  $$$2$$$-nd step. The current array is $$$[0]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1]$$$.  $$$3$$$-rd step. The current array is $$$[0,1]$$$. He can choose a subset $$$[0,1]$$$ and obtain an integer $$$2$$$, because $$$mex(0,1) = 2$$$. Appending this value to the end he gets the array $$$[0,1,2]$$$.  $$$4$$$-th step. The current array is $$$[0,1,2]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1,2,1]$$$. Thus, he can get the array without mistakes, so the answer is $$$-1$$$.In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $$$0$$$.In the third example he could have obtained $$$[0, 1, 2]$$$ without mistakes, but $$$239$$$ is definitely wrong.


Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$.In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters.Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$|index(s_i) - index(s_j)|$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) .Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps.In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps.Polycarp can visit each tile at most once.Polycarp asks you to help — print the sequence of indices of string $$$s$$$ on which he should jump.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 \le |s| \le 2 \cdot 10^5$$$), where $$$|s|$$$ — is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, \dots, j_m$$$ ($$$1 \le j_i \le |s|$$$) — the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$|s|$$$ (that is, $$$j_m=|s|$$$). If there are multiple answers, print any of them.

6

logic

codeforces

bca

aaaaaaaaaaa

adbaadabad

to

NoteIn the first test case, the required path corresponds to the picture:  In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$|12-9|+|9-7|+|7-3|=3+2+4=9$$$.

<?php

$al = array_flip(range('a', 'z'));

fscanf(STDIN, '%u', $t);

while ($t--) {
    fscanf(STDIN, '%s', $s);
    $sar = str_split($s);
    $n = count($sar);

    $start = $sar[0];
    $end = $sar[$n - 1];

    if ($start <= $end) {
        asort($sar);
    } else {
        arsort($sar);
    }

    $res = [];
    foreach ($sar as $key => $item) {
        if (($start <= $end && $item < $start) || ($end <= $start && $item > $start)) {
            continue;
        }

        if (($start <= $end && $item > $end) || ($end <= $start && $item < $end)) {
            break;
        }

        $res[] = $key + 1;
    }

    fprintf(STDOUT, '%u %u'.PHP_EOL, abs($al[$end] - $al[$start]), count($res));
    fprintf(STDOUT, '%s'.PHP_EOL, implode(' ', $res));
}
/*

1
aaaaaaaa
0

1
ab

1
logac
// 9 3
// 1 3 5

a b c d e f g h i j k l m n o p q r s t u v w x y z

codeforces
c o d e f o r c e s
1 2 3 4 5 6 7 8 9 10

c c d e e f o o r s // until the last last letter reached
1 8 3 4 9 5 2 6 7 10

logic
1 4 3 5

logac
1 3 5

 */


After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.

The first line contains two integers hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59) — the time of Andrew's awakening. The second line contains four integers H, D, C and N (1 ≤ H ≤ 105, 1 ≤ D, C, N ≤ 102).

Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .

NoteIn the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.

<?php

fscanf(STDIN, "%d %d", $hh, $mm);
fscanf(STDIN, "%d%d%d%d", $H, $D, $C, $N);

//first case
$c1 = ceil(1.0 * $H / $N) * $C;

//second case
$diff = 1200 - (($hh * 60) + $mm);
$current_price = $C - ($C*0.2);
if ($diff > 0) {
	$current_hunger = $H + ($diff * $D);
	$c2 = ceil($current_hunger / $N) * $current_price;
}
else {
	$c2 = ceil(1.0 * $H / $N) * $current_price;
}

echo min($c1, $c2)."\n";

<?php

fscanf(STDIN, "%d %d", $hh, $mm);
fscanf(STDIN, "%d%d%d%d", $H, $D, $C, $N);

//first case
$c1 = ceil(1.0 * $H / $N) * $C;

//second case
$diff = 1200 - (($hh * 60) + $mm);
$current_price = $C - ($C*0.2);
if ($diff > 0) {
	$current_hunger = $H + ($diff * $D);
	$c2 = ceil(1.0 * $current_hunger / $N) * $current_price;
}
else {
	$c2 = (1.0 * $H / $N) * $current_price;
}

echo min($c1, $c2)."\n";

<?php

fscanf(STDIN, "%d %d", $hh, $mm);
fscanf(STDIN, "%d%d%d%d", $H, $D, $C, $N);

//first case
$c1 = ceil(1.0 * $H / $N) * $C;

//second case
$diff = 1200 - (($hh * 60) + $mm);

if ($diff > 0) {
	$current_hunger = $H + ($diff * $D);
	$current_price = $C - ($C*0.2);
	$c2 = ceil(1.0 * $current_hunger / $N) * $current_price;
}
else {
	$c2 = (1.0 * $H / $N) * $current_price;
}

echo min($c1, $c2)."\n";


There is a $$$n \times m$$$ grid. You are standing at cell $$$(1, 1)$$$ and your goal is to finish at cell $$$(n, m)$$$.You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell $$$(x, y)$$$. You can:   move right to the cell $$$(x, y + 1)$$$ — it costs $$$x$$$ burles;  move down to the cell $$$(x + 1, y)$$$ — it costs $$$y$$$ burles. Can you reach cell $$$(n, m)$$$ spending exactly $$$k$$$ burles?

The first line contains the single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first and only line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m \le 100$$$; $$$0 \le k \le 10^4$$$) — the sizes of grid and the exact amount of money you need to spend.

For each test case, if you can reach cell $$$(n, m)$$$ spending exactly $$$k$$$ burles, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

NoteIn the first test case, you are already in the final cell, so you spend $$$0$$$ burles.In the second, third and fourth test cases, there are two paths from $$$(1, 1)$$$ to $$$(2, 2)$$$: $$$(1, 1)$$$ $$$\rightarrow$$$ $$$(1, 2)$$$ $$$\rightarrow$$$ $$$(2, 2)$$$ or $$$(1, 1)$$$ $$$\rightarrow$$$ $$$(2, 1)$$$ $$$\rightarrow$$$ $$$(2, 2)$$$. Both costs $$$1 + 2 = 3$$$ burles, so it's the only amount of money you can spend.In the fifth test case, there is the only way from $$$(1, 1)$$$ to $$$(1, 4)$$$ and it costs $$$1 + 1 + 1 = 3$$$ burles.

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

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

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

  $y = $m;
  $x = ($n - 1) * $y;
  $ans = $y + $x - 1;
  if ($ans == $k) { echo 'YES'.PHP_EOL; }
  else { echo 'NO'.PHP_EOL; }
}

?>


You are given a string $$$s$$$, consisting of brackets of two types: '(', ')', '[' and ']'.A string is called a regular bracket sequence (RBS) if it's of one of the following types:   empty string;  '(' + RBS + ')';  '[' + RBS + ']';  RBS + RBS. where plus is a concatenation of two strings.In one move you can choose a non-empty subsequence of the string $$$s$$$ (not necessarily consecutive) that is an RBS, remove it from the string and concatenate the remaining parts without changing the order.What is the maximum number of moves you can perform?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains a non-empty string, consisting only of characters '(', ')', '[' and ']'. The total length of the strings over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase print a single integer — the maximum number of moves you can perform on a given string $$$s$$$.

NoteIn the first example you can just erase the whole string.In the second example you can first erase the brackets on positions $$$1$$$ and $$$2$$$: "[]()", then "()" is left. After that you can erase it whole. You could erase the whole string from the beginning but you would get one move instead of two.In the third example you can first erase the brackets on positions $$$1$$$ and $$$3$$$: "([)]". They form an RBS "()". Then "[]" is left, so you can erase it whole.In the fourth example there is no subsequence that is an RBS, so you can't perform a move at all.In the fifth example you can erase the brackets on positions $$$2$$$ and $$$4$$$: ")[(]" and get ")(" as a result. You can erase nothing from it.

<?php
fscanf(STDIN,"%d",$tc);
while($tc--)
{
fscanf(STDIN,"%s",$str);
$len=strlen($str);
$frstLeft=0;
$scndLeft=0;
$cnt1=0;
$cnt2=0;
for ($i=0; $i <$len ; $i++)
 {
	if($str[$i]=='(')
		$frstLeft++;
	elseif($str[$i]=='[')
		$scndLeft++;
	elseif($frstLeft>0&&$str[$i]==')')
	{
		$cnt1++;
		$frstLeft--;

	}
	elseif($scndLeft>0&&$str[$i]==']')
	{
		$cnt2++;
		$scndLeft--;
	}

 }
 echo $cnt1+$cnt2."\n";
}

<?php
fscanf(STDIN, "%d", $number);
$result = array();
for ($i = 0; $i < $number; $i++){
    fscanf(STDIN, "%s", $string);
    $j = 0;
    $turns = 0;
    $round = 0;
    $square = 0;
    while ($j < strlen($string)){
        if ($string[$j] == '[')
            $square++;
        else if ($string[$j] == ']' && $square > 0) {
            $square --;
            $turns++;
        }
        else if ($string[$j] == '(')
            $round++;
        else if ($string[$j] == ')' && $round > 0) {
            $round--;
            $turns++;
        }
        $j++;
    }
    array_push($result, $turns);
}

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

<?php
fscanf(STDIN, "%d", $number);
$result = array();
for ($i = 0; $i < $number; $i++){
    fscanf(STDIN, "%s", $string);
    $j = 0;
    $turns = 0;
    $round = 0;
    $square = 0;
    while ($j < strlen($string)){
        if ($string[$j] == '[')
            $square = 1;
        else if ($string[$j] == ']' && $square == 1) {
            $square = 0;
            $turns++;
        }
        else if ($string[$j] == '(')
            $round = 1;
        else if ($string[$j] == ')' && $round == 1) {
            $round = 0;
            $turns++;
        }
        $j++;
    }
    array_push($result, $turns);
}

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


Once Bob decided to lay a parquet floor in his living room. The living room is of size n × m metres. Bob had planks of three types: a planks 1 × 2 meters, b planks 2 × 1 meters, and c planks 2 × 2 meters. Help Bob find out, if it is possible to parquet the living room with such a set of planks, and if it is possible, find one of the possible ways to do so. Bob doesn't have to use all the planks.

The first input line contains 5 space-separated integer numbers n, m, a, b, c (1 ≤ n, m ≤ 100, 0 ≤ a, b, c ≤ 104), n and m — the living room dimensions, a, b and c — amount of planks 1 × 2, 2 × 1 и 2 × 2 respectively. It's not allowed to turn the planks.

If it is not possible to parquet the room with such a set of planks, output IMPOSSIBLE. Otherwise output one of the possible ways to parquet the room — output n lines with m lower-case Latin letters each. Two squares with common sides should contain the same letters, if they belong to one and the same plank, and different letters otherwise. Different planks can be marked with one and the same letter (see examples). If the answer is not unique, output any.


Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties:  In one second we can add an arbitrary (possibly negative) integer to all elements of the array that are not equal to zero.  When all elements of the array become equal to zero, the array explodes. Nastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded.

The first line contains a single integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains n integers a1, a2, ..., an ( - 105 ≤ ai ≤ 105) — the elements of the array.

Print a single integer — the minimum number of seconds needed to make all elements of the array equal to zero.

NoteIn the first example you can add  - 1 to all non-zero elements in one second and make them equal to zero.In the second example you can add  - 2 on the first second, then the array becomes equal to [0, 0,  - 3]. On the second second you can add 3 to the third (the only non-zero) element.

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = 0;
for($x = 0; $x < $a; $x++)
{
     if($b[$x] == 0)
     {
          $c = 1;
          break;
     }
}
$d = array_unique($b);
print count($d) - $c;
?>

<?php
    $f = fopen('php://stdin', 'r');
    fgets($f);
    $arr = explode(' ', str_replace("\r\n", '', fgets($f)));
    $arr = array_count_values($arr);

    echo count($arr) - (isset($arr[0]) ? 1 : 0);

?>


You are given a rectangular parallelepiped with sides of positive integer lengths $$$A$$$, $$$B$$$ and $$$C$$$. Find the number of different groups of three integers ($$$a$$$, $$$b$$$, $$$c$$$) such that $$$1\leq a\leq b\leq c$$$ and parallelepiped $$$A\times B\times C$$$ can be paved with parallelepipeds $$$a\times b\times c$$$. Note, that all small parallelepipeds have to be rotated in the same direction.For example, parallelepiped $$$1\times 5\times 6$$$ can be divided into parallelepipeds $$$1\times 3\times 5$$$, but can not be divided into parallelepipeds $$$1\times 2\times 3$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. Each of the next $$$t$$$ lines contains three integers $$$A$$$, $$$B$$$ and $$$C$$$ ($$$1 \leq A, B, C \leq 10^5$$$) — the sizes of the parallelepiped.

For each test case, print the number of different groups of three points that satisfy all given conditions.

NoteIn the first test case, rectangular parallelepiped $$$(1, 1, 1)$$$ can be only divided into rectangular parallelepiped with sizes $$$(1, 1, 1)$$$.In the second test case, rectangular parallelepiped $$$(1, 6, 1)$$$ can be divided into rectangular parallelepipeds with sizes $$$(1, 1, 1)$$$, $$$(1, 1, 2)$$$, $$$(1, 1, 3)$$$ and $$$(1, 1, 6)$$$.In the third test case, rectangular parallelepiped $$$(2, 2, 2)$$$ can be divided into rectangular parallelepipeds with sizes $$$(1, 1, 1)$$$, $$$(1, 1, 2)$$$, $$$(1, 2, 2)$$$ and $$$(2, 2, 2)$$$.

<?php
ini_set('error_reporting', E_ALL & ~E_NOTICE);
define('N', pow(10, 5) + 5);

function init() {
	global $f;
	for ($i = 1; $i < N; $i++) {
		for ($j = $i; $j < N; $j += $i) {
			++$f[$j];
		}
	}
}
function count1($x, $y, $z) {
	return $x * $y * $z;
}
function count2($x, $y) {
	return ($x + $x * ($x - 1) / 2) * $y;
}
function count3($x) {
	return $x + $x * ($x - 1) + $x * ($x - 1) * ($x - 2) / 6;
}
function __gcd($a, $b) {
	if ($a == 0 || $b == 0) {
		return abs(max(abs($a), abs($b)));
	}

	$r = $a % $b;
	return ($r != 0) ? __gcd($b, $r) : abs($b);
}
init();
fscanf(STDIN, "%d", $t);
while ($t--) {
	fscanf(STDIN, "%d %d %d", $a, $b, $c);
	$ab = __gcd($a, $b);
	$bc = __gcd($b, $c);
	$ac = __gcd($a, $c);
	$abc = __gcd($ab, $c);
	$a = $f[$a];
	$b = $f[$b];
	$c = $f[$c];
	$ab = $f[$ab];
	$bc = $f[$bc];
	$ac = $f[$ac];
	$abc = $f[$abc];
	$ab -= $abc;
	$ac -= $abc;
	$bc -= $abc;
	$a -= $ab + $ac + $abc;
	$b -= $ab + $bc + $abc;
	$c -= $ac + $bc + $abc;
	$ans = count1($a, $b, $c) + count1($a, $b, $ac) + count1($a, $b, $bc) + count1($a, $b, $abc) + count1($a, $ab, $c) + count1($a, $ab, $ac) + count1($a, $ab, $bc) + count1($a, $ab, $abc) + count1($a, $bc, $c)
	 + count1($a, $bc, $ac) + count2($bc, $a) + count1($a, $bc, $abc) + count1($a, $abc, $c) + count1($a, $abc, $ac) + count2($abc, $a) + count1($ab, $b, $c) + count1($ab, $b, $ac) + count1($ab, $b, $bc)
	 + count1($ab, $b, $abc) + count2($ab, $c) + count2($ab, $ac) + count2($ab, $bc) + count2($ab, $abc) + count1($ab, $bc, $c) + count1($ab, $bc, $ac) + count2($bc, $ab) + count1($ab, $bc, $abc)
	 + count1($ab, $abc, $c) + count1($ab, $abc, $ac) + count2($abc, $ab) + count1($ac, $b, $c) + count2($ac, $b) + count1($ac, $b, $bc) + count1($ac, $b, $abc) + count1($ac, $ab, $c) + count2($ac, $ab)
	 + count1($ac, $bc, $c) + count2($ac, $bc) + count2($bc, $ac) + count1($ac, $bc, $abc) + count1($ac, $abc, $c) + count2($ac, $abc) + count2($abc, $ac) + count1($abc, $b, $c) + count1($abc, $b, $bc)
	 + count2($abc, $b) + count1($abc, $bc, $c) + count2($bc, $abc) + count2($abc, $bc) + count2($abc, $c) + count3($abc);
	printf("%d\n", $ans);
}


You have a playlist consisting of $$$n$$$ songs. The $$$i$$$-th song is characterized by two numbers $$$t_i$$$ and $$$b_i$$$ — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of $$$3$$$ songs having lengths $$$[5, 7, 4]$$$ and beauty values $$$[11, 14, 6]$$$ is equal to $$$(5 + 7 + 4) \cdot 6 = 96$$$.You need to choose at most $$$k$$$ songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 3 \cdot 10^5$$$) – the number of songs in the playlist and the maximum number of songs you can choose, respectively. Each of the next $$$n$$$ lines contains two integers $$$t_i$$$ and $$$b_i$$$ ($$$1 \le t_i, b_i \le 10^6$$$) — the length and beauty of $$$i$$$-th song.

Print one integer — the maximum pleasure you can get.

NoteIn the first test case we can choose songs $$${1, 3, 4}$$$, so the total pleasure is $$$(4 + 3 + 6) \cdot 6 = 78$$$.In the second test case we can choose song $$$3$$$. The total pleasure will be equal to $$$100 \cdot 100 = 10000$$$.

<?php

function input() {
    return array_map("intval", explode(" ", fgets(STDIN)));
}

[$n, $k] = input();

for ($i = 0; $i < $n; $i++) {
    [$t, $b] = input();
    $a[] = [$b, $t];
}

rsort($a);


Alice and Bob are playing a game. They have an array of positive integers $$$a$$$ of size $$$n$$$.Before starting the game, Alice chooses an integer $$$k \ge 0$$$. The game lasts for $$$k$$$ stages, the stages are numbered from $$$1$$$ to $$$k$$$. During the $$$i$$$-th stage, Alice must remove an element from the array that is less than or equal to $$$k - i + 1$$$. After that, if the array is not empty, Bob must add $$$k - i + 1$$$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $$$k$$$-th stage ends and Alice hasn't lost yet, she wins.Your task is to determine the maximum value of $$$k$$$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible.

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 size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$).

For each test case, print one integer — the maximum value of $$$k$$$ such that Alice can win if both players play optimally.

<?php
      $inp = preg_split('#\s+#', trim(file_get_contents("php://stdin")));
      $p = 0;
      $tests = intval($inp[$p++]);
      for($test=0;$test<$tests;$test++)
      {
        $n = intval($inp[$p++]);
        $a = array();
        for($i=0;$i<$n;$i++) $a[] = intval($inp[$p++]);
        $r = 0;
        foreach($a as $b) if ($b == 1) $r ++;
        echo $r."\n";
      }


Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length l. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of p meters per second, and the impulse of You-Know-Who's magic spell flies at a speed of q meters per second.The impulses are moving through the corridor toward each other, and at the time of the collision they turn round and fly back to those who cast them without changing their original speeds. Then, as soon as the impulse gets back to it's caster, the wizard reflects it and sends again towards the enemy, without changing the original speed of the impulse.Since Harry has perfectly mastered the basics of magic, he knows that after the second collision both impulses will disappear, and a powerful explosion will occur exactly in the place of their collision. However, the young wizard isn't good at math, so he asks you to calculate the distance from his position to the place of the second meeting of the spell impulses, provided that the opponents do not change positions during the whole fight.

The first line of the input contains a single integer l (1 ≤ l ≤ 1 000) — the length of the corridor where the fight takes place. The second line contains integer p, the third line contains integer q (1 ≤ p, q ≤ 500) — the speeds of magical impulses for Harry Potter and He-Who-Must-Not-Be-Named, respectively.

Print a single real number — the distance from the end of the corridor, where Harry is located, to the place of the second meeting of the spell impulses. Your answer will be considered correct if its absolute or relative error will not exceed 10 - 4.  Namely: let's assume that your answer equals a, and the answer of the jury is b. The checker program will consider your answer correct if .

NoteIn the first sample the speeds of the impulses are equal, so both of their meetings occur exactly in the middle of the corridor.

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$d = $a / ($b + $c);
$e = $b * $d;
print $e;
?>

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
$l = trim(fgets($file));
$p = trim(fgets($file));
$q = trim(fgets($file));
echo $p * $l / ($p + $q);

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = trim(fgets(STDIN));
$d = $a / ($b + $c);
$e = $b * $d;
$f = $c * $d;
print max(round($e, 5), round($f, 5));
?>


The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.Polycarp is a very famous freelancer. His current rating is $$$r$$$ units.Some very rich customers asked him to complete some projects for their companies. To complete the $$$i$$$-th project, Polycarp needs to have at least $$$a_i$$$ units of rating; after he completes this project, his rating will change by $$$b_i$$$ (his rating will increase or decrease by $$$b_i$$$) ($$$b_i$$$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.

The first line of the input contains two integers $$$n$$$ and $$$r$$$ ($$$1 \le n \le 100, 1 \le r \le 30000$$$) — the number of projects and the initial rating of Polycarp, respectively. The next $$$n$$$ lines contain projects, one per line. The $$$i$$$-th project is represented as a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i \le 30000$$$, $$$-300 \le b_i \le 300$$$) — the rating required to complete the $$$i$$$-th project and the rating change after the project completion.

NoteIn the first example, the possible order is: $$$1, 2, 3$$$.In the second example, the possible order is: $$$2, 3, 1$$$.In the third example, the possible order is: $$$3, 1, 4, 2$$$.


Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.Mr. Scrooge's signature can be represented as a polyline A1A2... An. Scrooge signs like that: first it places a pen at the point A1, then draws a segment from point A1 to point A2, then he draws a segment from point A2 to point A3 and so on to point An, where he stops signing and takes the pen off the paper. At that the resulting line can intersect with itself and partially repeat itself but Scrooge pays no attention to it and never changes his signing style. As Scrooge makes the signature, he never takes the pen off the paper and his writing speed is constant — 50 millimeters per second.Scrooge signed exactly k papers throughout his life and all those signatures look the same.Find the total time Scrooge wasted signing the papers.

The first line contains two integers n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai — integers xi and yi, separated by a space. All points Ai are different. The absolute value of all coordinates does not exceed 20. The coordinates are measured in millimeters.

Print one real number — the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6.


You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1.Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0.You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set cu = x.It is guaranteed that you have to color each vertex in a color different from 0.You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory).

The first line contains a single integer n (2 ≤ n ≤ 104) — the number of vertices in the tree. The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi < i), where pi means that there is an edge between vertices i and pi. The third line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ n), where ci is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree.

Print a single integer — the minimum number of steps you have to perform to color the tree into given colors.

NoteThe tree from the first sample is shown on the picture (numbers are vetices' indices):On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors):On seond step we color all vertices in the subtree of vertex 5 into color 1:On third step we color all vertices in the subtree of vertex 2 into color 1:The tree from the second sample is shown on the picture (numbers are vetices' indices):On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors):On second step we color all vertices in the subtree of vertex 3 into color 1:On third step we color all vertices in the subtree of vertex 6 into color 2:On fourth step we color all vertices in the subtree of vertex 4 into color 1:On fith step we color all vertices in the subtree of vertex 7 into color 3:

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


Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.

The first line contains an integer n (1 ≤ n ≤ 100): number of names. Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.

If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on). Otherwise output a single word "Impossible" (without quotes).

10
tourist
petr
wjmzbmr
yeputons
vepifanov
scottwu
oooooooooooooooo
subscriber
rowdark
tankengineer

10
petr
egor
endagorion
feferivan
ilovetanyaromanova
kostka
dmitriyh
maratsnowbear
bredorjaguarturnik
cgyforever

7
car
care
careful
carefully
becarefuldontforgetsomething
otherwiseyouwillbehacked
goodluck

bcdefghijklmnopqrsatuvwxyz Impossible aghjlnopefikdmbcqrstuvwxyz acbdefhijklmnogpqrstuvwxyz

<?php
error_reporting(0);

$n = (int)fgets(STDIN);
$names = array();
for ($i = 0; $i < $n; $i++) {
  $names[] = trim(fgets(STDIN));
}

$charset = "abcdefghijklmnopqrstuvwxyz";
$rels = array();
foreach (str_split($charset) as $ch1) {
  $rels[$ch1] = array();
  foreach (str_split($charset) as $ch2) {
    $rels[$ch1][$ch2] = '?';
  }
}

$ok = true;

for ($i = 1; $i < count($names); $i++) {
  $name1 = $names[$i - 1];
  $name2 = $names[$i];
  if ($name1 === $name2) {
    continue;
  }
  if (substr($name1, 0, strlen($name2)) === $name2) {
    $ok = false;
    break;
  }
  if (substr($name2, 0, strlen($name1)) === $name1) {
    continue;
  }
  for ($j = 0; $name1[$j] == $name2[$j]; $j++);
  $ch1 = $name1[$j];
  $ch2 = $name2[$j];

  $lessQueue = array();
  $lessQueue["$ch1/$ch2"] = true;
  while (!empty($lessQueue)) {
    list ($ch1, $ch2) = explode('/', key($lessQueue));
    if ($rels[$ch1][$ch2] == '>') {
      $ok = false;
      break(2);
    }
    $rels[$ch1][$ch2] = '<';
    $rels[$ch2][$ch1] = '>';
    foreach ($rels[$ch1] as $ich2 => $irel) {
      if ($irel == '>') {
        $lessQueue["$ich2/$ch2"] = true;
      }
    }
    foreach ($rels[$ch2] as $ich1 => $irel) {
      if ($irel == '<') {
        $lessQueue["$ch1/$ich1"] = true;
      }
    }
    unset($lessQueue[key($lessQueue)]);
  }
}

if (! $ok) {
  echo "Impossible\n";
} else {
  $answer = "";
  foreach ($rels as $ch1 => $irels) {
    $pos = strlen($answer);
    foreach ($irels as $ch2 => $rel) {
      if ($ch2 >= $ch1) {
        break;
      }
      if ($rel == '<') {
        $pos = min($pos, strpos($answer, $ch2));
      }
    }
    $answer = substr($answer, 0, $pos) . $ch2 . substr($answer, $pos);
  }
  echo "$answer\n";
}


You are given a binary string of length $$$n$$$ (i. e. a string consisting of $$$n$$$ characters '0' and '1').In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $$$k$$$ moves? It is possible that you do not perform any moves at all.Note that you can swap the same pair of adjacent characters with indices $$$i$$$ and $$$i+1$$$ arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.You have to answer $$$q$$$ independent test cases.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6, 1 \le k \le n^2$$$) — the length of the string and the number of moves you can perform. The second line of the test case contains one string consisting of $$$n$$$ characters '0' and '1'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$\sum n \le 10^6$$$).

For each test case, print the answer on it: the lexicographically minimum possible string of length $$$n$$$ you can obtain from the given one if you can perform no more than $$$k$$$ moves.

NoteIn the first example, you can change the string as follows: $$$1\underline{10}11010 \rightarrow \underline{10}111010 \rightarrow 0111\underline{10}10 \rightarrow 011\underline{10}110 \rightarrow 01\underline{10}1110 \rightarrow 01011110$$$. In the third example, there are enough operations to make the string sorted.


Ashish and Vivek play a game on a matrix consisting of $$$n$$$ rows and $$$m$$$ columns, where they take turns claiming cells. Unclaimed cells are represented by $$$0$$$, while claimed cells are represented by $$$1$$$. The initial state of the matrix is given. There can be some claimed cells in the initial state.In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends.If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves.

The first line consists of a single integer $$$t$$$ $$$(1 \le t \le 50)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case consists of two space-separated integers $$$n$$$, $$$m$$$ $$$(1 \le n, m \le 50)$$$ — the number of rows and columns in the matrix. The following $$$n$$$ lines consist of $$$m$$$ integers each, the $$$j$$$-th integer on the $$$i$$$-th line denoting $$$a_{i,j}$$$ $$$(a_{i,j} \in \{0, 1\})$$$.

For each test case if Ashish wins the game print "Ashish" otherwise print "Vivek" (without quotes).

NoteFor the first case: One possible scenario could be: Ashish claims cell $$$(1, 1)$$$, Vivek then claims cell $$$(2, 2)$$$. Ashish can neither claim cell $$$(1, 2)$$$, nor cell $$$(2, 1)$$$ as cells $$$(1, 1)$$$ and $$$(2, 2)$$$ are already claimed. Thus Ashish loses. It can be shown that no matter what Ashish plays in this case, Vivek will win. For the second case: Ashish claims cell $$$(1, 1)$$$, the only cell that can be claimed in the first move. After that Vivek has no moves left.For the third case: Ashish cannot make a move, so Vivek wins.For the fourth case: If Ashish claims cell $$$(2, 3)$$$, Vivek will have no moves left.

<?php
    define("MAXN", 1024) ;

    function solve($n, $k)
    {
    }

    $T = intval(fgets(STDIN));
    while($T --)
    {
        $in = explode(" ", trim(fgets(STDIN))) ;

        $n = intval($in[0]) ;
        $m = intval($in[1]) ;

        $matrix = array() ;

        for($i = 0; $i < $n; $i ++) array_push($matrix, explode(" ", trim(fgets(STDIN)))) ;

        $rows_cnt = 0 ;
        $cols_cnt = 0 ;

        for($i = 0; $i < $n ; $i ++)
        {
            for($j = 0; $j < $m; $j ++) if($matrix[$i][$j] == '1') break ;

            if($j >= $m) $rows_cnt ++ ;
        }

        for($i = 0; $i < $m; $i ++)
        {
            for($j = 0; $j < $n; $j ++) if($matrix[$j][$i] == '1') break ;

            if($j >= $n) $cols_cnt ++ ;
        }

        $ans = min($rows_cnt, $cols_cnt) ;

        if($ans & 1) echo "Ashish\n" ;
        else echo "Vivek\n" ;
    }
?>


Vasya owns a cornfield which can be defined with two integers $$$n$$$ and $$$d$$$. The cornfield can be represented as rectangle with vertices having Cartesian coordinates $$$(0, d), (d, 0), (n, n - d)$$$ and $$$(n - d, n)$$$.    An example of a cornfield with $$$n = 7$$$ and $$$d = 2$$$. Vasya also knows that there are $$$m$$$ grasshoppers near the field (maybe even inside it). The $$$i$$$-th grasshopper is at the point $$$(x_i, y_i)$$$. Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.Help Vasya! For each grasshopper determine if it is inside the field (including the border).

The first line contains two integers $$$n$$$ and $$$d$$$ ($$$1 \le d < n \le 100$$$). The second line contains a single integer $$$m$$$ ($$$1 \le m \le 100$$$) — the number of grasshoppers. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i, y_i \le n$$$) — position of the $$$i$$$-th grasshopper.

Print $$$m$$$ lines. The $$$i$$$-th line should contain "YES" if the position of the $$$i$$$-th grasshopper lies inside or on the border of the cornfield. Otherwise the $$$i$$$-th line should contain "NO". You can print each letter in any case (upper or lower).

NoteThe cornfield from the first example is pictured above. Grasshoppers with indices $$$1$$$ (coordinates $$$(2, 4)$$$) and $$$4$$$ (coordinates $$$(4, 5)$$$) are inside the cornfield.The cornfield from the second example is pictured below. Grasshoppers with indices $$$1$$$ (coordinates $$$(4, 4)$$$), $$$3$$$ (coordinates $$$(8, 1)$$$) and $$$4$$$ (coordinates $$$(6, 1)$$$) are inside the cornfield.

<?php
ini_set('error_reporting', E_ALL & ~E_NOTICE);

fscanf(STDIN, "%d %d", $n, $d);
fscanf(STDIN, "%d", $m);

for ($i = 0; $i < $m; $i++) {
    fscanf(STDIN, "%d %d", $x, $y);
    // x+y が dより大きい 2n-dより小さい
    $con1 = ($x + $y) >= $d;
    $con2 = ($x + $y) <= 2 * $n - $d;
    $con3 = ($x - $y) >= -$d;
    $con4 = ($x - $y) <= $d;
    // printf("%d %d %d %d\n", $con1, $con2, $con3, $con4);
    echo ($con1 && $con2 && $con3 && $con4) ? "YES\n" : "NO\n";
}


You are given $$$n$$$ rectangles, each of height $$$1$$$. Each rectangle's width is a power of $$$2$$$ (i. e. it can be represented as $$$2^x$$$ for some non-negative integer $$$x$$$). You are also given a two-dimensional box of width $$$W$$$. Note that $$$W$$$ may or may not be a power of $$$2$$$. Moreover, $$$W$$$ is at least as large as the width of the largest rectangle.You have to find the smallest height of this box, such that it is able to fit all the given rectangles. It is allowed to have some empty space left in this box after fitting all the rectangles.You cannot rotate the given rectangles to make them fit into the box. Moreover, any two distinct rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.See notes for visual explanation of sample input.

The first line of input contains one integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^3$$$) — the number of test cases. Each test case consists of two lines. For each test case:  the first line contains two integers $$$n$$$ ($$$1 \le n \le 10^5$$$) and $$$W$$$ ($$$1 \le W \le 10^9$$$); the second line contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 10^6$$$), where $$$w_i$$$ is the width of the $$$i$$$-th rectangle. Each $$$w_i$$$ is a power of $$$2$$$; additionally, $$$\max\limits_{i=1}^{n} w_i \le W$$$.  The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output $$$t$$$ integers. The $$$i$$$-th integer should be equal to the answer to the $$$i$$$-th test case — the smallest height of the box.

NoteFor the first test case in the sample input, the following figure shows one way to fit the given five rectangles into the 2D box with minimum height:  In the figure above, the number inside each rectangle is its width. The width of the 2D box is $$$16$$$ (indicated with arrow below). The minimum height required for the 2D box in this case is $$$2$$$ (indicated on the left).In the second test case, you can have a minimum height of three by keeping two blocks (one each of widths eight and two) on each of the three levels.

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

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

function binary_search($list, $item) {
  $low = 0;
  $high = count($list) - 1; $s = $high;

  while ($low <= $high) {
    $mid = floor(($low + $high) / 2);
    $guess = $list[$mid];

    if ($guess >= $item) { $s = $mid; $high = $mid - 1; }
    else { $low = $mid + 1; }
  }
  return $s;
}

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

  for ($j = 0; $j < $n; $j++) $f[$j] = $w;
  for ($j = $n - 1; $j >= 0; $j--) {
		$x = binary_search($f, $a[$j]);
		$f[$x] -= $a[$j];
	}
	$ans = 0;

	for ($j = 0; $j < $n; $j++) {
	  if ($f[$j] != $w) $ans++;
	}
	echo $ans.PHP_EOL;
}
?>

<?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;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
}

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

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    list($n, $w) = IO::arr();
    $a = IO::arr();
    $arr = [];
    foreach ($a as $v) {
        if (isset($arr[$v])) {
            $arr[$v] += 1;
        } else {
            $arr[$v] = 1;
        }
    }
    $i = 0;
    $res = 0;
    krsort($arr);
    while($i < $n) {
        $base = $w;
        $b = [];
        foreach ($arr as $k => $ar) {
            $f = floor($base / $k);
            $f = min([$ar, $f]);
            $d = $f * $k;
            if ($base - $d >= 0) {
                $i += $f;
                $base -= $d;
            }
            $b[$k] = $ar - $f;
            if ($b[$k] < 1) {
                unset($b[$k]);
            }
        }
        $res++;
        $arr = $b;
    }
    IO::line($res);
}

?>

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

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

function binary_search($list, $item) {
  $low = 0;
  $high = count($list) - 1; $s = $high;

  while ($low <= $high) {
    $mid = (int) ($low + $high) / 2;
    $guess = $list[$mid];

    if ($guess >= $item) { $s = $mid; $high = $mid - 1; }
    else { $low = $mid + 1; }
  }
  return $s;
}

for ($i = 0; $i < $t; $i++) {
  list($n, $w) = explode(" ", trim(fgets($file)));
  $a = explode(" ", trim(fgets($file)));
  sort($a);
  $f = array();
  for ($j = 0; $j < $n; $j++) $f[$j] = $w;
  for ($j = $n - 1; $j >= 0; $j--) {
		$x = binary_search($f, $a[$j]);
		$f[$x] -= $a[$j];
	}
	$ans = 0;

	for ($j = 0; $j < $n; $j++) {
	  if ($f[$j] != $w) $ans++;
	}
	echo $ans.PHP_EOL;
}
?>

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

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

function binary_search($list, $item) {
  $low = 0;
  $high = count($list); $s = $high;

  while ($low <= $high) {
    $mid = floor(($low + $high) / 2);
    $guess = $list[$mid];

    if ($guess >= $item) { $s = $mid; $high = $mid - 1; }
    else { $low = $mid + 1; }
  }
  return $s;
}

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

  for ($j = 0; $j < $n; $j++) $f[$j] = $w;
  for ($j = $n - 1; $j >= 0; $j--) {
		$x = binary_search($f, $a[$j]);
		$f[$x] -= $a[$j];
	}
	$ans = 0;

	for ($j = 0; $j < $n; $j++) {
	  if ($f[$j] != $w) $ans++;
	}
	echo $ans.PHP_EOL;
}
?>

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

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

function binary_search($list, $item) {
  $low = 0;
  $high = count($list) - 1; $s = $high;

  while ($low <= $high) {
    $mid = floor(($low + $high) / 2);
    $guess = $list[$mid];

    if ($guess >= $item) { $s = $mid; $high = $mid - 1; }
    else { $low = $mid + 1; }
  }
  return $s;
}

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

  for ($j = 0; $j < $n; $j++) $f[$j] = $w;
  for ($j = $n - 1; $j >= 0; $j--) {
		$x = binary_search($f, $a[$j]);
		$f[$x] -= $a[$j];
	}
	$ans = 0;

	for ($j = 0; $j < $n; $j++) {
	  if ($f[$j] != $w) $ans++;
	}
	echo $ans.PHP_EOL;
}
?>

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

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

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

  $ans = ceil($sum / $w);
  echo $ans.PHP_EOL;
}

?>

<?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;
    }
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
}

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

while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    list($n, $w) = IO::arr();
    $a = IO::arr();
    $arr = [];
    foreach ($a as $v) {
        if (isset($arr[$v])) {
            $arr[$v] += 1;
        } else {
            $arr[$v] = 1;
        }
    }
    $i = 0;
    $res = 0;
    krsort($arr);
    while($i < $n) {
        $base = $w;
        $t = [];
        foreach ($arr as $k => $ar) {
            if ($base - $k >= 0) {
                $t[$k] = $ar;
                $base -= $k;
            }
        }
        $x = min($t);
        $res += $x;
        $i += $x * count($t);
        foreach ($t as $k => $f) {
            $arr[$k] -= $x;
            if($arr[$k] == 0) {
                unset($arr[$k]);
            }
        }
    }
    IO::line($res);
}

?>


The legend of the foundation of Vectorland talks of two integers $$$x$$$ and $$$y$$$. Centuries ago, the array king placed two markers at points $$$|x|$$$ and $$$|y|$$$ on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points $$$|x - y|$$$ and $$$|x + y|$$$ and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.Here $$$|z|$$$ denotes the absolute value of $$$z$$$.Now, Jose is stuck on a question of his history exam: "What are the values of $$$x$$$ and $$$y$$$?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$. Now, he wants to know the number of unordered pairs formed by two different elements from these $$$n$$$ integers such that the legend could be true if $$$x$$$ and $$$y$$$ were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$)  — the number of choices. The second line contains $$$n$$$ pairwise distinct integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the choices Jose is considering.

Print a single integer number — the number of unordered pairs $$$\{x, y\}$$$ formed by different numbers from Jose's choices that could make the legend true.

NoteConsider the first sample. For the pair $$$\{2, 5\}$$$, the situation looks as follows, with the Arrayland markers at $$$|2| = 2$$$ and $$$|5| = 5$$$, while the Vectorland markers are located at $$$|2 - 5| = 3$$$ and $$$|2 + 5| = 7$$$:  The legend is not true in this case, because the interval $$$[2, 3]$$$ is not conquered by Vectorland. For the pair $$$\{5, -3\}$$$ the situation looks as follows, with Arrayland consisting of the interval $$$[3, 5]$$$ and Vectorland consisting of the interval $$$[2, 8]$$$:  As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair $$$\{2, -3\}$$$, for a total of two pairs.In the second sample, the only pair is $$$\{3, 6\}$$$, and the situation looks as follows:  Note that even though Arrayland and Vectorland share $$$3$$$ as endpoint, we still consider Arrayland to be completely inside of Vectorland.

<?php
# 1166B

$stdin = STDIN;
[$n] = fscanf($stdin, '%u');
$items = array_map(static function($i){
    return (int)abs($i);
}, explode(' ', fgets($stdin)));
sort($items);

for ($i = 0, $j = 0, $amount = 0.0; $i < $n; $i++) {
    while (($items[$j] * 2) < $items[$i]) {$j++;}
    $amount += $i - $j;
}
echo sprintf('%.0f', $amount);


Fox Ciel is playing a card game with her friend Jiro.Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.Now is Ciel's battle phase, Ciel can do the following operation many times:  Choose one of her cards X. This card mustn't be chosen before.  If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:   If Y's position is Attack, then (X's strength)  ≥  (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).  If Y's position is Defense, then (X's strength)  >  (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.  Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have. Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card.

Output an integer: the maximal damage Jiro can get.

NoteIn the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.

<?php
/** chaotic_iak's solution in PHP 5.4 to the problem 321B (Round 190 Div 2 Problem D) in Codeforces **/

/** Module for retrieving input **/

// If this is a local test (a variable declared in a specialized page to open this script), open from localtest.txt instead of php://stdin (because I'm too lazy to supply the arguments by stdin)
if (isset($localtest)) {$stdin = fopen("localtest.txt", "r");} else {$stdin = fopen("php://stdin", "r");}

// A function to retrieve the next line. Takes one optional argument $single, which should be true if the next line (based on problem's specification) has only one number/string/whatever, so I don't have to do silly things like $raw = g(); $num = $raw[0];
function g($single = false) {

	// Take the global $stdin variable aka the file handler so we can access it from this function
	global $stdin;

	// Take the input from the handler, trim it, and explode it based on space (" 1 2 3 " becomes array("1", "2", "3")); trim is usually to remove trailing \r
	$input = explode(" ", trim(fgets($stdin)));

	// If the line is not a single number, return the whole array, otherwise simply return the single string that we exploded for nothing
	if (!$single) {return $input;} else {return $input[0];}
}

/** Imported functions from chaotic_iak's library **/
// none

/** chaotic_iak's solution **/
// This is a competitive programming solution. The code is not remarked (because I want speed).

$raw = g();
$n = $raw[0];
$m = $raw[1];
$atk = array();
$def = array();

for ($i = 0; $i < $n; $i++) {
	$raw = g();
	if ($raw[0] == "ATK") {$atk[] = $raw[1];} else {$def[] = $raw[1];}
}

for ($i = 0; $i < $m; $i++) {
	$own[$i] = g(true);
}

// sort
sort($own, SORT_NUMERIC);
sort($atk, SORT_NUMERIC);
sort($def, SORT_NUMERIC);

// strategy 1
// match all def, beat all atk, do direct damage
$atk1 = $atk;
$def1 = $def;
$own1 = $own;
$dmg = 0;

$i = 0;

$n = count($own1);

for ($j = 0; $j < $n; $j++) {
	if ($i >= count($def1)) {break;}
	if ($own1[$j] > $def1[$i]) {$i++; unset($own1[$j]);}
}

if ($i >= count($def1)) {

$own1 = array_values($own1);
sort($own1, SORT_NUMERIC);
$i = count($atk1)-1;

for ($j = count($own1)-1; $j >= 0; $j--) {
	if ($i < 0) {$dmg += $own1[$j];} else {
		if ($own1[$j] < $atk1[$i]) {break;} else {$dmg += $own1[$j] - $atk1[$i]; $i--;}
	}
}

}

$res = $dmg;

// strategy 2
// beat least atk with most power
$atk2 = $atk;
$own2 = $own;
$dmg = 0;

$i = 0;

for ($j = count($own2)-1; $j >= 0; $j--) {
	if ($i >= count($atk2)) {break;}
	if ($own2[$j] < $atk2[$i]) {break;} else {$dmg += $own2[$j] - $atk2[$i]; $i++;}
}

$res = ($res < $dmg) ? $dmg : $res;

echo $res;

<?php
/** chaotic_iak's solution in PHP 5.4 to the problem 321B (Round 190 Div 2 Problem D) in Codeforces **/

/** Module for retrieving input **/

// If this is a local test (a variable declared in a specialized page to open this script), open from localtest.txt instead of php://stdin (because I'm too lazy to supply the arguments by stdin)
if (isset($localtest)) {$stdin = fopen("localtest.txt", "r");} else {$stdin = fopen("php://stdin", "r");}

// A function to retrieve the next line. Takes one optional argument $single, which should be true if the next line (based on problem's specification) has only one number/string/whatever, so I don't have to do silly things like $raw = g(); $num = $raw[0];
function g($single = false) {

	// Take the global $stdin variable aka the file handler so we can access it from this function
	global $stdin;

	// Take the input from the handler, trim it, and explode it based on space (" 1 2 3 " becomes array("1", "2", "3")); trim is usually to remove trailing \r
	$input = explode(" ", trim(fgets($stdin)));

	// If the line is not a single number, return the whole array, otherwise simply return the single string that we exploded for nothing
	if (!$single) {return $input;} else {return $input[0];}
}

/** Imported functions from chaotic_iak's library **/
// none

/** chaotic_iak's solution **/
// This is a competitive programming solution. The code is not remarked (because I want speed).

$raw = g();
$n = $raw[0];
$m = $raw[1];
$atk = array();
$def = array();

for ($i = 0; $i < $n; $i++) {
	$raw = g();
	if ($raw[0] == "ATK") {$atk[] = $raw[1];} else {$def[] = $raw[1];}
}

for ($i = 0; $i < $m; $i++) {
	$own[$i] = g(true);
}

// sort
sort($own, SORT_NUMERIC);
sort($atk, SORT_NUMERIC);
sort($def, SORT_NUMERIC);

// strategy 1
// match all def, beat all atk, do direct damage
$atk1 = $atk;
$def1 = $def;
$own1 = $own;
$dmg = 0;

$i = 0;

for ($j = 0; $j < count($own1); $j++) {
	if ($i >= count($def1)) {break;}
	if ($own1[$j] > $def1[$i]) {$i++; unset($own1[$j]);}
}

if ($i >= count($def1)) {

$own1 = array_values($own1);
sort($own1);
$i = count($atk1)-1;

for ($j = count($own1)-1; $j >= 0; $j--) {
	if ($i < 0) {$dmg += $own1[$j];} else {
		if ($own1[$j] < $atk1[$i]) {$dmg = 0; break;} else {$dmg += $own1[$j] - $atk1[$i]; $i--;}
	}
}

}

$res = $dmg;

// strategy 2
// beat least atk with most power
$atk2 = $atk;
$own2 = $own;
$dmg = 0;

$i = 0;

for ($j = count($own2)-1; $j >= 0; $j--) {
	if ($i >= count($atk2)) {break;}
	if ($own2[$j] < $atk2[$i]) {break;} else {$dmg += $own2[$j] - $atk2[$i]; $i++;}
}

$res = ($res < $dmg) ? $dmg : $res;

echo $res;


Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture:  As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.

The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.

Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.

<?php
$IN = STDIN;
fscanf($IN, "%d", $n);
$items = explode(" ",trim(fgets($IN)));

$curr = 0;
for ($i=0;$i<(count($items));$i++){
    if ($items[$i]!=$items[$i-1]){
        $normalized[$curr] = $items[$i];
        $curr++;
    } else {
        $cellweight[$curr-1] += 1;
    }
}

if (count($normalized)>1) {
    $locmin[] = 0;
    for ($i=1;$i<(count($normalized)-1);$i++)
        if (($normalized[$i]<$normalized[$i+1])&&($normalized[$i]<$normalized[$i-1]))
            $locmin[] = $i;
    $locmin[] = count($normalized)-1;
} else {
    $locmin = Array(0,0);
}

for ($i=1;$i<count($locmin);$i++){
    $len = 0;
    for ($j=$locmin[$i-1];$j<=$locmin[$i];$j++)
        $len = $len + 1 + (int)$cellweight[$j];
        $lenarr[] = $len;
}

echo (count($lenarr)>1)?max($lenarr):$lenarr[0];

?>


There are $$$n$$$ Christmas trees on an infinite number line. The $$$i$$$-th tree grows at the position $$$x_i$$$. All $$$x_i$$$ are guaranteed to be distinct.Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.There are $$$m$$$ people who want to celebrate Christmas. Let $$$y_1, y_2, \dots, y_m$$$ be the positions of people (note that all values $$$x_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m$$$ should be distinct and all $$$y_j$$$ should be integer). You want to find such an arrangement of people that the value $$$\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j|$$$ is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized).In other words, let $$$d_j$$$ be the distance from the $$$j$$$-th human to the nearest Christmas tree ($$$d_j = \min\limits_{i=1}^{n} |y_j - x_i|$$$). Then you need to choose such positions $$$y_1, y_2, \dots, y_m$$$ that $$$\sum\limits_{j=1}^{m} d_j$$$ is the minimum possible.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of Christmas trees and the number of people. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$-10^9 \le x_i \le 10^9$$$), where $$$x_i$$$ is the position of the $$$i$$$-th Christmas tree. It is guaranteed that all $$$x_i$$$ are distinct.

In the first line print one integer $$$res$$$ — the minimum possible value of $$$\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j|$$$ (in other words, the sum of distances to the nearest Christmas tree for all people). In the second line print $$$m$$$ integers $$$y_1, y_2, \dots, y_m$$$ ($$$-2 \cdot 10^9 \le y_j \le 2 \cdot 10^9$$$), where $$$y_j$$$ is the position of the $$$j$$$-th human. All $$$y_j$$$ should be distinct and all values $$$x_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m$$$ should be distinct. If there are multiple answers, print any of them.

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

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

$trees = explode(' ', trim(fgets(STDIN)));
$places = [];

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

sort($trees);
$min = $trees[0] - 1;
$pos2 = $min;
for ($i = 1; $i < $n; $i++){
	$pos1 = $pos2 + 2;
	$pos2 = $trees[$i]-1;
	if($pos1 <= $pos2){
		$places[] = [$pos1, $pos2];
	}
}
$max = $pos2 + 2;
$peoples = [];
$sum = 0;
$d = 0;

$check = function($pos, $d) use (&$m, &$peoples, &$sum){
	$m--;
	$peoples[] = $pos;
	$sum += $d;
	if($m == 0){
		echo $sum . "\n";
		echo implode(' ', $peoples);
		die;
	}
};

$iterate = function($d) use(&$places, &$min, &$max, &$check){
	$c = count($places);
	$places2 = [];
	$check($min--, $d);
	for ($i = 0; $i < $c; $i++){
		list($p1,$p2) = $places[$i];
		$check($p1++, $d);
		if($p1 <= $p2){
			$check($p2--, $d);
			if($p1 <= $p2){
				$places2[] = [$p1, $p2];
			}
		}
	}
	$check($max++, $d);
	return $places2;
};

while($m > 0){
	$d++;
	$places = $iterate($d);
}

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

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

$trees = explode(' ', trim(fgets(STDIN)));
$occupied = [];
$places = [];

for ($i = 0; $i < $n; $i++){
	$trees[$i] = intval($trees[$i]);
	$occupied[$trees[$i]] = 1;
}

sort($trees);

for ($i = 0; $i < $n; $i++){
	if(!array_key_exists($i-1, $occupied)){
		$places[] = [$trees[$i], -1];
	}
	if(!array_key_exists($i+1, $occupied)){
		$places[] = [$trees[$i], +1];
	}
}

$peoples = [];
$sum = 0;
$d = 0;

$iterate = function($d) use(&$places, &$occupied, &$peoples, &$m, &$sum){
	$c = count($places);
	$places2 = [];
	for ($i = 0; $i < $c; $i++){
		$pos = $places[$i][0] + $places[$i][1] * $d;
		if(!array_key_exists($pos, $occupied)){
			$m--;
			$peoples[] = $pos;
			$occupied[$pos] = 1;
 			$places2[] = $places[$i];
			$sum +=$d;
			if($m == 0){
				return null;
			}
		}
	}
	return $places2;
};

while($m > 0){
	$d++;
	$places = $iterate($d);
}

echo $sum . "\n";
echo implode(' ', $peoples);

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

list($n, $m) = explode(' ', trim(fgets($stdin)));

$trees = explode(' ', trim(fgets($stdin)));

$peoples = [];
$sum = 0;
$d = 1;

while($m > 0){
	for($tIndex = 0; $tIndex < $n; $tIndex++ ){
		$pos = $trees[$tIndex] - $d;
		if(array_search($pos, $trees) === false && array_search($pos, $peoples) === false){
			$sum += $d;
			$peoples[] = $pos;
			$m--;
			if($m == 0){
				break;
			}
		}

		$pos = $trees[$tIndex] + $d;
		if(array_search($pos, $trees) === false && array_search($pos, $peoples) === false){
			$sum += $d;
			$peoples[] = $pos;
			$m--;
			if($m == 0){
				break;
			}
		}
	}
	$d++;
}

echo $sum . "\n";
echo implode(' ', $peoples);


The Third Doctor Who once correctly said that travel between parallel universes is "like travelling sideways". However, he incorrectly thought that there were infinite parallel universes, whereas in fact, as we now all know, there will never be more than 250.Heidi recently got her hands on a multiverse observation tool. She was able to see all $$$n$$$ universes lined up in a row, with non-existent links between them. She also noticed that the Doctor was in the $$$k$$$-th universe.The tool also points out that due to restrictions originating from the space-time discontinuum, the number of universes will never exceed $$$m$$$.Obviously, the multiverse is unstable because of free will. Each time a decision is made, one of two events will randomly happen: a new parallel universe is created, or a non-existent link is broken.More specifically,    When a universe is created, it will manifest itself between any two adjacent universes or at one of the ends.   When a link is broken, it could be cut between any two adjacent universes. After separating the multiverse into two segments, the segment NOT containing the Doctor will cease to exist. Heidi wants to perform a simulation of $$$t$$$ decisions. Each time a decision is made, Heidi wants to know the length of the multiverse (i.e. the number of universes), and the position of the Doctor.

The first line contains four integers $$$n$$$, $$$k$$$, $$$m$$$ and $$$t$$$ ($$$2 \le k \le n \le m \le 250$$$, $$$1 \le t \le 1000$$$). Each of the following $$$t$$$ lines is in one of the following formats:    "$$$1$$$ $$$i$$$" — meaning that a universe is inserted at the position $$$i$$$ ($$$1 \le i \le l + 1$$$), where $$$l$$$ denotes the current length of the multiverse.  "$$$0$$$ $$$i$$$" — meaning that the $$$i$$$-th link is broken ($$$1 \le i \le l - 1$$$), where $$$l$$$ denotes the current length of the multiverse.

Output $$$t$$$ lines. Each line should contain $$$l$$$, the current length of the multiverse and $$$k$$$, the current position of the Doctor. It is guaranteed that the sequence of the steps will be valid, i.e. the multiverse will have length at most $$$m$$$ and when the link breaking is performed, there will be at least one universe in the multiverse.

NoteThe multiverse initially consisted of 5 universes, with the Doctor being in the second.First, link 1 was broken, leaving the multiverse with 4 universes, and the Doctor in the first.Then, a universe was added to the leftmost end of the multiverse, increasing the multiverse length to 5, and the Doctor was then in the second universe.Then, the rightmost link was broken.Finally, a universe was added between the first and the second universe.


Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.He has a special interest to create difficult problems for others to solve. This time, with many $$$1 \times 1 \times 1$$$ toy bricks, he builds up a 3-dimensional object. We can describe this object with a $$$n \times m$$$ matrix, such that in each cell $$$(i,j)$$$, there are $$$h_{i,j}$$$ bricks standing on the top of each other.However, Serval doesn't give you any $$$h_{i,j}$$$, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are $$$m$$$ columns, and in the $$$i$$$-th of them, the height is the maximum of $$$h_{1,i},h_{2,i},\dots,h_{n,i}$$$. It is similar for the left view, where there are $$$n$$$ columns. And in the top view, there is an $$$n \times m$$$ matrix $$$t_{i,j}$$$, where $$$t_{i,j}$$$ is $$$0$$$ or $$$1$$$. If $$$t_{i,j}$$$ equals $$$1$$$, that means $$$h_{i,j}>0$$$, otherwise, $$$h_{i,j}=0$$$.However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try?

The first line contains three positive space-separated integers $$$n, m, h$$$ ($$$1\leq n, m, h \leq 100$$$) — the length, width and height. The second line contains $$$m$$$ non-negative space-separated integers $$$a_1,a_2,\dots,a_m$$$, where $$$a_i$$$ is the height in the $$$i$$$-th column from left to right of the front view ($$$0\leq a_i \leq h$$$). The third line contains $$$n$$$ non-negative space-separated integers $$$b_1,b_2,\dots,b_n$$$ ($$$0\leq b_j \leq h$$$), where $$$b_j$$$ is the height in the $$$j$$$-th column from left to right of the left view. Each of the following $$$n$$$ lines contains $$$m$$$ numbers, each is $$$0$$$ or $$$1$$$, representing the top view, where $$$j$$$-th number of $$$i$$$-th row is $$$1$$$ if $$$h_{i, j}>0$$$, and $$$0$$$ otherwise. It is guaranteed that there is at least one structure satisfying the input.

Output $$$n$$$ lines, each of them contains $$$m$$$ integers, the $$$j$$$-th number in the $$$i$$$-th line should be equal to the height in the corresponding position of the top view. If there are several objects satisfying the views, output any one of them.

Note  The graph above illustrates the object in the first example.    The first graph illustrates the object in the example output for the second example, and the second graph shows the three-view drawing of it.

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

<?php

$ser = explode(' ', trim(fgets(STDIN)));

$n = $ser[0];
$m = $ser[1];
$h = $ser[2];

$front = explode(' ', trim(fgets(STDIN)));
$left = explode(' ', trim(fgets(STDIN)));

$top = [];

for($i = 0; $i<$n; $i++){
	$top[] = explode(' ', trim(fgets(STDIN)));
}

for($i = 0; $i<$n; $i++){
	for($j = 0; $j<$m; $j++){
		if($top[$i][$j] == 1){
			$top[$i][$j] = min($front[$j], $left[$i]);
		}
	}
}

for($i = 0; $i<$n; $i++){
	fwrite(STDOUT, implode(' ', $top[$i])."\n");
}


The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n), ai + 1 - ai = k, where k is the number the Queen chose.Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?

The first line contains two space-separated integers: n, k (1 ≤ n, k ≤ 1000). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000) — the heights of the trees in the row.

In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions.  If the gardener needs to increase the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, print on the corresponding line "- j x". If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.