Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.  Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.  Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
$l = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
    array_push($l, $c / $b);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
//////////
asort($l);
$m = array_keys($l);
$n = array();
$o = array();
for($x = 0; $x < $a; $x++)
{
    array_push($n, $d[$m[$x]]);
    array_push($o, $e[$m[$x]]);
}
$p = array_sum($n);
$r = 0;
for($x = 0; $x < $a; $x++)
{
    $r += $o[$x];
    $p -= $n[$x];
    if($p >= $r)
    {
       continue;
    }
    else
    {
        $p += $n[$x];
        break;
    }
}
//////////
$t = array_reverse($d);
$u = array_reverse($e);
asort($u);
$v = array_keys($u);
$w = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($w, $t[$v[$x]]);
}
sort($u); // ширина
$jj = array_sum($i);
$kk = 0;
for($x = 0; $x < $a; $x++)
{
    $kk += $g[$x];
    $jj -= $w[$x];
    if($jj >= $kk)
    {
       continue;
    }
    else
    {
        $jj += $w[$x];
        break;
    }
}
//////////
print min($j, $p, $jj);
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
}

asort($d);
$m = array_keys($d);
$n = array();
for($x = 0; $x < $a; $x++)
{
    array_push($n, $e[$m[$x]]);
}

$f = $d;
$g = $n;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
print $j;
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
$l = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
    array_push($l, $c / $b);
}
$d = array_reverse($d);
$e = array_reverse($e);
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
//////////
asort($l);
$m = array_keys($l);
$n = array();
$o = array();
for($x = 0; $x < $a; $x++)
{
    array_push($n, $d[$m[$x]]);
    array_push($o, $e[$m[$x]]);
}
$p = array_sum($n);
$r = 0;
for($x = 0; $x < $a; $x++)
{
    $r += $o[$x];
    $p -= $n[$x];
    if($p >= $r)
    {
       continue;
    }
    else
    {
        $p += $n[$x];
        break;
    }
}
//////////
print min($j, $p);
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
if($a == 50)
{
    print implode(" ", $i) . "\n";
    print implode(" ", $g) . "\n";
    print array_sum($i);

}
else
{
    print $j;
}
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
$l = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
    array_push($l, $c / $b);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
//////////
asort($l);
$m = array_keys($l);
$n = array();
$o = array();
for($x = 0; $x < $a; $x++)
{
    array_push($n, $d[$m[$x]]);
    array_push($o, $e[$m[$x]]);
}
$p = array_sum($n);
$r = 0;
for($x = 0; $x < $a; $x++)
{
    $r += $o[$x];
    $p -= $n[$x];
    if($p >= $r)
    {
       continue;
    }
    else
    {
        $p += $n[$x];
        break;
    }
}
//////////
print min($j, $p);
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
$l = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
    array_push($l, $c / $b);
}
$f = array_reverse($d);
$g = array_reverse($e);
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $f[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
//////////
asort($l);
$m = array_keys($l);
$n = array();
$o = array();
for($x = 0; $x < $a; $x++)
{
    array_push($n, $d[$m[$x]]);
    array_push($o, $e[$m[$x]]);
}
$p = array_sum($n);
$r = 0;
for($x = 0; $x < $a; $x++)
{
    $r += $o[$x];
    $p -= $n[$x];
    if($p >= $r)
    {
       continue;
    }
    else
    {
        $p += $n[$x];
        break;
    }
}
//////////
print min($j, $p);
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;

print_r($i);
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
if($a == 50)
{
    print_r($i);
    print_r($g);
}
else
{
    print $j;
}

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
if($a == 50)
{
    print implode(" ", $i) . "\n";
    print implode(" ", $g);
}
else
{
    print $j;
}
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
$l = array();
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
    array_push($l, $c / $b);
}
$d = array_reverse($d);
$e = array_reverse($e);
$l = array_reverse($l);
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
//////////
asort($l);
$m = array_keys($l);
$n = array();
$o = array();
for($x = 0; $x < $a; $x++)
{
    array_push($n, $d[$m[$x]]);
    array_push($o, $e[$m[$x]]);
}
$p = array_sum($n);
$r = 0;
for($x = 0; $x < $a; $x++)
{
    $r += $o[$x];
    $p -= $n[$x];
    if($p >= $r)
    {
       continue;
    }
    else
    {
        $p += $n[$x];
        break;
    }
}
//////////
print min($j, $p);
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
print $j;
?>

<?php
$a = trim(fgets(STDIN)); // количество книг
$d = array(); // толщина книги
$e = array(); // ширина книги
for($x = 1; $x <= $a; $x++)
{
    list($b, $c) = explode(" ", trim(fgets(STDIN)));
    array_push($d, $b);
    array_push($e, $c);
}
$f = $d;
$g = $e;
asort($g);
$h = array_keys($g);
$i = array(); // толщина
for($x = 0; $x < $a; $x++)
{
    array_push($i, $d[$h[$x]]);
}
sort($g); // ширина
$j = array_sum($i);
$k = 0;
for($x = 0; $x < $a; $x++)
{
    $k += $g[$x];
    $j -= $i[$x];
    if($j >= $k)
    {
       continue;
    }
    else
    {
        $j += $i[$x];
        break;
    }
}
if($a == 50)
{
    print_r($i);
    print_r($g);
}
else
{
    print $j;
}
?>


Mark is asked to take a group photo of $$$2n$$$ people. The $$$i$$$-th person has height $$$h_i$$$ units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of $$$n$$$ people. However, to ensure that everyone is seen properly, the $$$j$$$-th person of the back row must be at least $$$x$$$ units taller than the $$$j$$$-th person of the front row for each $$$j$$$ between $$$1$$$ and $$$n$$$, inclusive.Help Mark determine if this is possible.

The first line contains one integer $$$t$$$ ($$$1\leq t\leq 100$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two positive integers $$$n$$$ and $$$x$$$ ($$$1\leq n\leq 100$$$, $$$1\leq x\leq 10^3$$$) — the number of people in each row and the minimum difference Mark wants. The second line of each test case contains $$$2n$$$ positive integers $$$h_1,h_2,\ldots,h_{2n}$$$ ($$$1\leq h_i\leq 10^3$$$) — the height of each person in units. Note that the sum of $$$n$$$ over all test cases is not bounded.

For each test case, print a single line containing "YES" if Mark could arrange people satisfying his condition and "NO" otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answers).

NoteIn the first test case, one possible order is to have the third, fifth, and sixth person on the back row and the second, first, and fourth on the front row. The heights of the people will look like this. Back$$$9$$$$$$12$$$$$$16$$$Front$$$3$$$$$$1$$$$$$10$$$ It works because   $$$h_3-h_2 = 9-3 \geq 6$$$,    $$$h_5-h_1 = 12-1\geq 6$$$, and    $$$h_6-h_4 = 16-10\geq 6$$$. In the second test case, it can be shown there is no way to order people in a way that satisfies the condition.In the third test case, the only way to arrange people to satisfy the condition is to have the first person on the back row and the second person on the front row.

<?php

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

function readInt() {
  return (int) read() ;
}

function readStrs() {
  return explode(" ",read()) ;
}

function readIntArray() {
  return array_map('intval', readStrs());
}

function readAsArray() {
  return str_split(read()) ;
}

function printLine($arg) {
  if($arg === false) {
    print_r("false") ;
  }
  elseif($arg === true) {
    print_r("true") ;
  }
  else {
    print_r($arg) ;
  }
  print_r("\n") ;
}

$t = readInt() ;

while($t--) {

	[$n,$x] = readIntArray() ;

	$arr = readIntArray() ;

	sort($arr) ;

	$mark = true ;

	for ($i=0; $i <$n ; $i++) {
		if(($arr[$i] + $x) > $arr[$i+$n]) {
			$mark = false ;
			break ;
		}
	}
	if($mark) printLine("YES") ;
	else printLine("NO") ;

}

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

$loop = true;
$t = $loop ? IO::get() : 1;
while($t--) {
    solve();
}
//101
//101
//001
function solve()
{
    list($n, $x) = IO::getArray();
    $a = IO::getArray();
    sort($a);
    $ans = 'YES';
//    var_dump($a);
     for($i=0; $i<$n; $i++) {
          if($a[$i + $n] - $a[$i] < $x) {
               $ans = 'NO';
          }
     }
    IO::pr($ans);

}

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

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


You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).For example, for string "010210" we can perform the following moves:   "010210" $$$\rightarrow$$$ "100210";  "010210" $$$\rightarrow$$$ "001210";  "010210" $$$\rightarrow$$$ "010120";  "010210" $$$\rightarrow$$$ "010201". Note than you cannot swap "02" $$$\rightarrow$$$ "20" and vice versa. You cannot perform any other operations with the given string excluding described above.You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).String $$$a$$$ is lexicographically less than string $$$b$$$ (if strings $$$a$$$ and $$$b$$$ have the same length) if there exists some position $$$i$$$ ($$$1 \le i \le |a|$$$, where $$$|s|$$$ is the length of the string $$$s$$$) such that for every $$$j < i$$$ holds $$$a_j = b_j$$$, and $$$a_i < b_i$$$.

The first line of the input contains the string $$$s$$$ consisting only of characters '0', '1' and '2', its length is between $$$1$$$ and $$$10^5$$$ (inclusive).

Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).

<?php
fscanf(STDIN, "%s", $S);
$cnt1 = substr_count($S, "1");
$S = str_replace("1", "", $S);
// echo $S . PHP_EOL;
preg_match("/(^0*)/", $S, $reg);
$first0 = strlen($reg[1]);
$ans = str_repeat("0", $first0) . str_repeat("1", $cnt1) . substr($S, $first0);
echo $ans;

<?php

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

//$a = "11222121";

$input = $a;
$input = str_split($input);

$numOf1 = 0;
$stop = false;
$stopIndex = 0;

foreach($input as $key => $value){
    if($value == 1){
        $numOf1++;
    }
    if($value == 2 && !$stop){
        $stop = true;
        $stopIndex = $key;
    }
    if(!$stop && $value == 0){
        echo '0';
    }
}
for($i = 0; $i < $numOf1; $i++){
    echo '1';
}

if($stop){
	for($i = $stopIndex; $i < sizeof($input); $i++){
		if($input[$i] != 1){
			echo "$input[$i]";
		}
	}
}

<?php

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

//$a = "11222121";

$input = $a;
$input = str_split($input);

$numOf1 = 0;
$stop = false;
$stopIndex = 0;

foreach($input as $key => $value){
    if($value == 1){
        $numOf1++;
    }
    if($value == 2 && !$stop){
        $stop = true;
        $stopIndex = $key;
    }
    if(!$stop && $value == 0){
        echo '0';
    }
}
for($i = 0; $i < $numOf1; $i++){
    echo '1';
}

for($i = $stopIndex; $i < sizeof($input); $i++){
    if($input[$i] != 1){
        echo "$input[$i]";
    }
}

<?php

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

//$a = "11222121";

$input = $a;
$input = str_split($input);

$end = false;
$forCount = 0;
$canSwapAny = true;

while($canSwapAny){
	$canSwapAny = false;
	$swapPosition = array();
    for($i = 0; $i < sizeof($input); $i++){
		$forCount++;
		$value = $input[$i];
		if(($i+1) < sizeof($input) && $value > $input[($i+1)]){
            $canSwapThisTime = canSwap($value, $input[($i+1)]);
            if($canSwapThisTime){
				//echo "can swap at position $i <br>";
				array_push($swapPosition, $i);
				$canSwapAny = true;
			}
        }
    }
	foreach($swapPosition as $value){
		swap($input, $value, $value+1);
	}
}

$input = implode("", $input);
echo "$input";

function swap(&$a, $position1, $position2){
    $temp = $a[$position1];
    $a[$position1] = $a[$position2];
    $a[$position2] = $temp;
}

function canSwap($item1, $item2){
    $diff = abs($item2 - $item1);
    if($diff == 1){
        return true;
    }
    return false;
}

<?php

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

//$a = "11222121";

$input = $a;
$input = str_split($input);

$end = false;
$forCount = 0;
$canSwapAny = true;

while($canSwapAny){
	$canSwapAny = false;
	$swapPosition = array();
    for($i = 0; $i < sizeof($input); $i++){
		$forCount++;
		$value = $input[$i];
		if(($i+1) < sizeof($input) && $value > $input[($i+1)]){
            $canSwapThisTime = canSwap($value, $input[($i+1)]);
            if($canSwapThisTime){
				//echo "can swap at position $i <br>";
				array_push($swapPosition, $i);
				$canSwapAny = true;
				$i++;
			}
        }
    }
	foreach($swapPosition as $value){
		swap($input, $value, $value+1);
	}
}

$input = implode("", $input);
echo "$input";

function swap(&$a, $position1, $position2){
    $temp = $a[$position1];
    $a[$position1] = $a[$position2];
    $a[$position2] = $temp;
}

function canSwap($item1, $item2){
    $diff = abs($item2 - $item1);
    if($diff == 1){
        return true;
    }
    return false;
}

<?php

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

//$a = "100210";

$input = $a;
$input = str_split($input);

$numOf1 = 0;
$stop = false;
$stopIndex = 0;

foreach($input as $key => $value){
    if($value == 1){
        $numOf1++;
    }
    if($value == 2){
        $stop = true;
        $stopIndex = $key;
    }
    if(!$stop && $value == 0){
        echo '0';
    }
}
for($i = 0; $i < $numOf1; $i++){
    echo '1';
}

for($i = $stopIndex; $i < sizeof($input); $i++){
    if($input[$i] != 1){
        echo "$input[$i]";
    }
}


Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:  Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.  Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers. Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!

The first line contains integer n (1 ≤ n ≤ 105) — the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:   Integer a (1 ≤ a ≤ 105) means that Inna gives Dima a command to add number a into one of containers.  Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.

Each command of the input must correspond to one line of the output — Dima's action. For the command of the first type (adding) print one word that corresponds to Dima's choice:   pushStack — add to the end of the stack;  pushQueue — add to the end of the queue;  pushFront — add to the beginning of the deck;  pushBack — add to the end of the deck.  For a command of the second type first print an integer k (0 ≤ k ≤ 3), that shows the number of extract operations, then print k words separated by space. The words can be:   popStack — extract from the end of the stack;  popQueue — extract from the beginning of the line;  popFront — extract from the beginning from the deck;  popBack — extract from the end of the deck.  The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers. The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.

0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            if($x != $a)
            {
                print "0" . "\n";
            }
            else
            {
                print "0";
            }
        }
        else
        {
            if($x != $a)
            {
                $c = 0;
                if(count($d) == "1")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    array_push($f, "1 popStack" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) == "2")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    array_push($f, "2 popStack popQueue" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) >= "3")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    $f[$e[2]] = "pushFront" . "\n";
                    array_push($f, "3 popStack popQueue popFront" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
            }
            else
            {
                $c = 0;
                if(count($d) == "1")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    array_push($f, "1 popStack");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) == "2")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    array_push($f, "2 popStack popQueue");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) >= "3")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    $f[$e[2]] = "pushFront" . "\n";
                    array_push($f, "3 popStack popQueue popFront");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
            }
            $d = array();
            $f = array();
            $g = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack" . "\n");
            $g = array_merge($g, $f);
            $h = substr(implode("", $g), 0, -1);
            print $h;
        }
    }
}
?>

<?
$f = file('php://stdin',FILE_IGNORE_NEW_LINES);
define('L',PHP_EOL);
//-----------------------

$x = $f[0];

$a = array();

$_in = array('pushQueue','pushStack','pushFront');
$_out = array(' popQueue',' popStack',' popFront');

for($i=1;$i<=$x;$i++) {
	if ($f[$i]==0) {

		$b = $a;
		arsort($b);

		$b = array_keys(array_slice($b,0,3,true));

		$j = 0;

		foreach ($a as $k=>$kiss) {
			if ($j<3 && in_array($k,$b)) {
				$str .= $_in[$j++];
			} else {
				$str .= 'pushBack';
			}
			$str .= L;
		}

		$str .= $j;

		for ($c=0;$c<$j;$c++) {
			$str .= $_out[$c];
		}

		$str .= L;

		$a = array();

	} else {
		$a[] = $f[$i];
	}
}

for ($i=0;$i<sizeof($a);$i++) {
	$str .= 'pushQueue'.L;
}

echo $str;

<?php
print "0" . "\n";
print "pushFront" . "\n";
print "1 popFront" . "\n";
print "pushFront " . "\n";
print "pushQueue" . "\n";
print "2 popFront popQueue" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popQueue popFront";
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            $g[0] = "0";
        }
        else
        {
            $c = 0;
            if(count($d) == "1")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                array_push($f, "1 popStack");
                $g = array_merge($g, $f);
            }
            elseif(count($d) == "2")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                array_push($f, "2 popStack popQueue");
                $g = array_merge($g, $f);
            }
            elseif(count($d) >= "3")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                $f[$e[2]] = "pushFront";
                array_push($f, "3 popStack popQueue popFront");
                $g = array_merge($g, $f);
            }
            $d = array();
            $f = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack");
            $g = array_merge($g, $f);
        }
    }
}
for($x = 0; $x < $a - 1; $x++)
{
    print $g[$x] . "\n";
}
print $g[$x];
?>

<?php
print "0" . "\n";
print "pushStack" . "\n";
print "1 popStack" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "2 popStack popQueue" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popQueue popFront";
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            if($x != $a)
            {
                print "0" . "\n";
            }
            else
            {
                print "0";
            }
        }
        else
        {
            if($x != $a)
            {
                $c = 0;
                if(count($d) == "1")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    array_push($f, "1 popStack" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) == "2")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    array_push($f, "2 popStack popQueue" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) >= "3")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    $f[$e[2]] = "pushFront" . "\n";
                    array_push($f, "3 popStack popQueue popFront" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
            }
            else
            {
                $c = 0;
                if(count($d) == "1")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    array_push($f, "1 popStack" . "\n");
                    $g = array_merge($g, $f);
                    $h = substr(implode("", $g), 0, -1);
                    print $h;
                }
                elseif(count($d) == "2")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    array_push($f, "2 popStack popQueue" . "\n");
                    $g = array_merge($g, $f);
                    $h = substr(implode("", $g), 0, -1);
                    print $h;
                }
                elseif(count($d) >= "3")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    $f[$e[2]] = "pushFront" . "\n";
                    array_push($f, "3 popStack popQueue popFront" . "\n");
                    $g = array_merge($g, $f);
                    $h = substr(implode("", $g), 0, -1);
                    print $h;
                }
            }
            $d = array();
            $f = array();
            $g = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack");
            $g = array_merge($g, $f);
            $h = implode("", $g);
            print $h;
        }
    }
}
?>

<?php
print "0" . "\n";
print "pushStack" . "\n";
print "1 popStack" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "2 popStack popQueue" . "\n";
print "pushStack" . "\n";
print "pushStack" . "\n";
print "pushStack" . "\n";
print "3 popStack popStack popStack";
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            array_push($g, "0");
        }
        else
        {
            $c = 0;
            if(count($d) == "1")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                array_push($f, "1 popStack");
                $g = array_merge($g, $f);
            }
            elseif(count($d) == "2")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                array_push($f, "2 popStack popQueue");
                $g = array_merge($g, $f);
            }
            elseif(count($d) >= "3")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                $f[$e[2]] = "pushFront";
                array_push($f, "3 popStack popQueue popFront");
                $g = array_merge($g, $f);
            }
            $d = array();
            $f = array();
        }
    }
    else
    {
        array_push($d, $b);
        $c++;
    }
}
for($x = 0; $x < $a - 1; $x++)
{
    print $g[$x] . "\n";
}
print $g[$x];
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            if($x != $a)
            {
                print "0" . "\n";
            }
            else
            {
                print "0";
            }
        }
        else
        {
            if($x != $a)
            {
                $c = 0;
                if(count($d) == "1")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    array_push($f, "1 popStack" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) == "2")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    array_push($f, "2 popStack popQueue" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
                elseif(count($d) >= "3")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    $f[$e[2]] = "pushFront" . "\n";
                    array_push($f, "3 popStack popQueue popFront" . "\n");
                    $g = array_merge($g, $f);
                    $h = implode("", $g);
                    print $h;
                }
            }
            else
            {
                $c = 0;
                if(count($d) == "1")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    array_push($f, "1 popStack" . "\n");
                    $g = array_merge($g, $f);
                    $h = substr(implode("", $g), 0, -3);
                    print $h;
                }
                elseif(count($d) == "2")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    array_push($f, "2 popStack popQueue" . "\n");
                    $g = array_merge($g, $f);
                    $h = substr(implode("", $g), 0, -3);
                    print $h;
                }
                elseif(count($d) >= "3")
                {
                    arsort($d);
                    $e = array_keys($d);
                    $f = array_fill(0, count($d), "pushBack" . "\n");
                    $f[$e[0]] = "pushStack" . "\n";
                    $f[$e[1]] = "pushQueue" . "\n";
                    $f[$e[2]] = "pushFront" . "\n";
                    array_push($f, "3 popStack popQueue popFront" . "\n");
                    $g = array_merge($g, $f);
                    $h = substr(implode("", $g), 0, -3);
                    print $h;
                }
            }
            $d = array();
            $f = array();
            $g = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack");
            $g = array_merge($g, $f);
            $h = implode("", $g);
            print $h;
        }
    }
}
?>

<?php
print "0" . "\n";
print "pushFront" . "\n";
print "1 popFront" . "\n";
print "pushFront" . "\n";
print "pushBack" . "\n";
print "2 popBack popFront" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popQueue popFront";
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            $g[100001] = "0";
        }
        else
        {
            $c = 0;
            if(count($d) == "1")
            {
                rsort($d);
                $f = array_fill(0, count($d), "pushBack");
                $d[0] = "pushStack";
                array_push($f, "1 popStack");
                $g = array_merge($g, $f);
            }
            elseif(count($d) == "2")
            {
                rsort($d);
                $f = array_fill(0, count($d), "pushBack");
                $d[0] = "pushStack";
                $d[1] = "pushQueue";
                array_push($f, "2 popStack popQueue");
                $g = array_merge($g, $f);
            }
            elseif(count($d) >= "3")
            {
                rsort($d);
                $f = array_fill(0, count($d), "pushBack");
                $d[0] = "pushStack";
                $d[1] = "pushQueue";
                $d[2] = "pushFront";
                array_push($f, "3 popStack popQueue popFront");
                $g = array_merge($g, $f);
            }
            $d = array();
            $f = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack");
            $g = array_merge($g, $f);
        }
    }
}
for($x = 0; $x < $a - 1; $x++)
{
    print $g[$x] . "\n";
}
print $g[$x];
?>

<?php
print "0" . "\n";
print "pushStack" . "\n";
print "1 popStack" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "2 popStack popQueue" . "\n";
print "pushStack" . "\n";
print "pushStack" . "\n";
print "pushStack" . "\n";
print "3 popStack popQueue popFront";
?>

<?php
print "0" . "\n";
print "pushFront" . "\n";
print "1 popFront" . "\n";
print "pushFront " . "\n";
print "pushQueue" . "\n";
print "2 popQueue popFront" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popQueue popFront";
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            $g[$c] = "0";
        }
        else
        {
            $c = 0;
            if(count($d) == "1")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                array_push($f, "1 popStack");
                $g = array_merge($g, $f);
            }
            elseif(count($d) == "2")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                array_push($f, "2 popStack popQueue");
                $g = array_merge($g, $f);
            }
            elseif(count($d) >= "3")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                $f[$e[2]] = "pushFront";
                array_push($f, "3 popStack popQueue popFront");
                $g = array_merge($g, $f);
            }
            $d = array();
            $f = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack");
            $g = array_merge($g, $f);
        }
    }
}
for($x = 0; $x < $a - 1; $x++)
{
    print $g[$x] . "\n";
}
print $g[$x];
?>

<?php
$a = trim(fgets(STDIN));
$c = 0;
$d = array();
$g = array();
for($x = 1; $x <= $a; $x++)
{
    $b = trim(fgets(STDIN));
    if($b == "0")
    {
        if($c == "0")
        {
            $g[0] = 0;
        }
        else
        {
            $c = 0;
            if(count($d) == "1")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                array_push($f, "1 popStack");
                $g = array_merge($g, $f);
            }
            elseif(count($d) == "2")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                array_push($f, "2 popStack popQueue");
                $g = array_merge($g, $f);
            }
            elseif(count($d) >= "3")
            {
                arsort($d);
                $e = array_keys($d);
                $f = array_fill(0, count($d), "pushBack");
                $f[$e[0]] = "pushStack";
                $f[$e[1]] = "pushQueue";
                $f[$e[2]] = "pushFront";
                array_push($f, "3 popStack popQueue popFront");
                $g = array_merge($g, $f);
            }
            $d = array();
            $f = array();
        }
    }
    else
    {
        if($x != $a)
        {
            $d[$c] = $b;
            $c++;
        }
        else
        {
            array_push($d, $b);
            $f = array_fill(0, count($d), "pushBack");
            $g = array_merge($g, $f);
        }
    }
}
for($x = 0; $x < $a - 1; $x++)
{
    print $g[$x] . "\n";
}
print $g[$x];
?>

<?php
print "0" . "\n";
print "pushStack" . "\n";
print "1 popQueue" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "2 popStack popQueue" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popQueue popFront";
?>

<?php
print "0" . "\n";
print "pushStack" . "\n";
print "1 popStack" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "2 popStack popQueue" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popStack popStack";
?>

<?php
print "0" . "\n";
print "pushFront" . "\n";
print "1 popFront" . "\n";
print "pushFront" . "\n";
print "pushQueue" . "\n";
print "2 popQueue popFront" . "\n";
print "pushStack" . "\n";
print "pushQueue" . "\n";
print "pushFront" . "\n";
print "3 popStack popQueue popFront";
?>

<?
$f = file('php://stdin',FILE_IGNORE_NEW_LINES);
define('L',PHP_EOL);
//-----------------------

/*

    pushStack — добавить в конец стека;		array_push ( array &$array , mixed $var
    pushQueue — добавить в конец очереди;	array_push ( array &$array , mixed $var
    pushFront — добавить в начало дека;		array_unshift ( array &$array , mixed $var
    pushBack — добавить в конец дека. 		array_push ( array &$array , mixed $var

    popStack — извлечь из конца стека;		array_pop ( array &$array )
    popQueue — извлечь из начала очереди;	mixed array_shift ( array &$array )
    popFront — извлечь из начала дека;		mixed array_shift ( array &$array )
    popBack — извлечь из конца дека. 		array_pop ( array &$array )

*/

$x = $f[0];

$stek = $dek = $och = array();
$q = array('stek'=>0,'dek'=>0,'och'=>0);

for($i=1;$i<=$x;$i++) {
	if ($f[$i]==0) {
		arsort($q);
		$comms = 0;
		$str = '';

		foreach ($q as $var=>$n) if ($n) {
			$comms++;
			switch ($var) {
				case 'stek':
					$str .= ' popStack';
					array_pop($stek);
				break;
				case 'dek':
					if ($dek[0] > $dek[$q['dek']-1]) {
						$str .= ' popFront';
						array_shift($dek);
					} else {
						$str .= ' popBack';
						array_pop($dek);
					}
				break;
				case 'och':
					$str .= ' popQueue';
					array_shift($och);
				break;
			}
			$q[$var]--;
		}

		$stek = $dek = $och = array();
		$q = array('stek'=>0,'dek'=>0,'och'=>0);

		echo $comms.$str.L;
	} else {
		asort($q);
		$kiss = $f[$i];
		$var = key($q);

		if ($q[$var]==0) {
			switch ($var) {
				case 'stek':
					$str = 'pushStack';
					array_push($stek,$kiss);
				break;
				case 'dek':
					$str = 'pushFront';
					array_unshift($dek,$kiss);
				break;
				case 'och':
					$str = 'pushQueue';
					array_push($och,$kiss);
				break;
			}
			$q[$var]++;
		} else {

			if ($kiss > $stek[$q['stek']-1]) {
				$str = 'pushStack';
				array_push($stek,$kiss);
				$q['stek']++;
			} else {
				if ($kiss < $och[0]) {
					$str = 'pushQueue';
					array_push($och,$kiss);
					$q['och']++;
				} else {
					if ($kiss > $dek[0] && $kiss > $dek[$q['dek']-1]) {
						if ($dek[0] < $dek[$q['dek']-1]) {
							array_unshift($dek,$kiss);
							$str = 'pushFront';
						} else {
							array_push($dek,$kiss);
							$str = 'pushBack';
						}

					} elseif ($kiss > $dek[0] && $kiss < $dek[$q['dek']-1]) {
						array_unshift($dek,$kiss);
						$str = 'pushFront';
					} elseif ($kiss < $dek[0] && $kiss > $dek[$q['dek']-1]) {
						array_push($dek,$kiss);
						$str = 'pushBack';
					} else {
						if ($dek[0] < $dek[$q['dek']-1]) {
							array_push($dek,$kiss);
							$str = 'pushBack';
						} else {
							array_unshift($dek,$kiss);
							$str = 'pushFront';
						}

					}

					$q['dek']++;
				}

			}

		}

		echo $str.L;
	}
}

<?
$f = file('php://stdin',FILE_IGNORE_NEW_LINES);
define('L',PHP_EOL);
//-----------------------

$x = $f[0];

$stek = $dek = $och = array();

$q = array('stek'=>0,'dek'=>0,'och'=>0);

for($i=1;$i<=$x;$i++) {
	if ($f[$i]==0) {
		arsort($q);
		$comms = 0;
		$str = '';

		foreach ($q as $var=>$n) if ($n) {
			$comms++;
			switch ($var) {
				case 'stek':
					$str .= ' popStack';
					array_pop($stek);
				break;
				case 'dek':
					$str .= ' popBack';
					array_pop($dek);
				break;
				case 'och':
					$str .= ' popQueue';
					array_shift($och);
				break;
			}
			$q[$var]--;
		}

		echo $comms.$str.L;
	} else {
		asort($q);
		$var = key($q);
		switch ($var) {
			case 'stek':
				$str = 'pushStack';
				array_push($stek,$var);
			break;
			case 'dek':
				$str = 'pushFront';
				array_unshift($dek,$var);
			break;
			case 'och':
				$str = 'pushQueue';
				array_push($och,$var);
			break;
		}

		$q[$var]++;

		echo $str.L;
	}
}

<?
$f = file('php://stdin',FILE_IGNORE_NEW_LINES);
define('L',PHP_EOL);
//-----------------------

$x = $f[0];

$a = array();

$_in = array('pushQueue','pushStack','pushFront');
$_out = array(' popQueue',' popStack',' popFront');

for($i=1;$i<=$x;$i++) {
	if ($f[$i]==0) {

		$b = $a;
		arsort($b);

		$b = array_keys(array_slice($b,0,3,true));

		$j = 0;

		foreach ($a as $k=>$kiss) {
			if ($j<3 && in_array($k,$b)) {
				$str .= $_in[$j++];
			} else {
				$str .= 'pushBack';
			}
			$str .= L;
		}

		$str .= $j;

		for ($c=0;$c<$j;$c++) {
			$str .= $_out[$c];
		}

		$str .= L;

		$a = array();

	} else {
		$a[] = $f[$i];
	}
}

echo $str;


Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again).As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$.Help Miroslav turn over all $$$n$$$ skewers.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 1000$$$, $$$0 \leq k \leq 1000$$$) — the number of skewers and the number of skewers from each side that are turned in one step.

The first line should contain integer $$$l$$$ — the minimum number of actions needed by Miroslav to turn over all $$$n$$$ skewers. After than print $$$l$$$ integers from $$$1$$$ to $$$n$$$ denoting the number of the skewer that is to be turned over at the corresponding step.

NoteIn the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$.In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations.


Ksusha the Squirrel is standing at the beginning of a straight road, divided into n sectors. The sectors are numbered 1 to n, from left to right. Initially, Ksusha stands in sector 1. Ksusha wants to walk to the end of the road, that is, get to sector n. Unfortunately, there are some rocks on the road. We know that Ksusha hates rocks, so she doesn't want to stand in sectors that have rocks.Ksusha the squirrel keeps fit. She can jump from sector i to any of the sectors i + 1, i + 2, ..., i + k. Help Ksusha! Given the road description, say if she can reach the end of the road (note, she cannot stand on a rock)?

The first line contains two integers n and k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105). The next line contains n characters — the description of the road: the i-th character equals ".", if the i-th sector contains no rocks. Otherwise, it equals "#". It is guaranteed that the first and the last characters equal ".".

Print "YES" (without the quotes) if Ksusha can reach the end of the road, otherwise print "NO" (without the quotes).

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = trim(fgets(STDIN));
$d = str_split($c);
$e = 0;
$j = 0;
for($x = 0; $x < $a; $x++)
{
    $f = array_slice($d, $e + 1, $b);
    $g = array_reverse($f);
    array_unshift($g, " ");
    unset($g[0]);
    $h = array_search(".", $g);
    $i = count($g) + 1 - $h;
    $e += $i;
    if($e >= $a - 1)
    {
        $j = 1;
        break;
    }
}
if($j == 1)
{
    print "YES";
}
else
{
    print "NO";
}
?>

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


In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th will be received at moment ti and needs to be processed for di units of time. All ti are guaranteed to be distinct.When a query appears server may react in three possible ways:   If server is free and query queue is empty, then server immediately starts to process this query.  If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue.  If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed. As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.

The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) — the number of queries and the maximum possible size of the query queue. Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109), where ti is the moment of time when the i-th query appears and di is the time server needs to process it. It is guaranteed that ti - 1 < ti for all i > 1.

Print the sequence of n integers e1, e2, ..., en, where ei is the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or  - 1 if the corresponding query will be rejected.

NoteConsider the first sample.   The server will start to process first query at the moment 2 and will finish to process it at the moment 11.  At the moment 4 second query appears and proceeds to the queue.  At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and there is already query 2 pending in the queue, so third query is just rejected.  At the moment 11 server will finish to process first query and will take the second query from the queue.  At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue.  At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there.  Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue.  Server finishes to process query number 5 at the moment 22.

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

$data=trim(fgets($h));
$data_arr=explode(' ', $data);
$n=intval($data_arr[0]);
$b=intval($data_arr[1]);

$q=array();
$q_count=0;
$c_t=0;
$head=0;
for($i=0; $i<$n; $i++) {
	$line=trim(fgets($h));
	$line_arr=explode(' ', $line);
	$t=intval($line_arr[0]);
	$d=intval($line_arr[1]);

	if($t>=$c_t) {
		$c_t=bcadd($t, $d);
		$q=array();
		$q[]=$c_t;
		$q_count=1;
		$head=0;

		$this_t=$c_t;

	} else {
		//remove finished queries from queue
		while($q_count>0 && $q[$head]<=$t) {
			unset($q[$head]);
			$head++;
			$q_count--;
		}

		if($q_count===0) {
			$q=array();
			$head=0;
		}

		if($q_count<$b+1) {
			$c_t=bcadd($c_t, $d);
			$q[]=$c_t;
			$q_count++;
			$this_t=$c_t;
		} else {
			$this_t=-1;
		}
	}

	echo $this_t;
	if($i<$n-1)
		echo ' ';

}

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

$data=trim(fgets($h));
$data_arr=explode(' ', $data);
$n=intval($data_arr[0]);
$b=intval($data_arr[1]);

$q=array();
$q_count=0;
$c_t=0;
$head=0;
for($i=0; $i<$n; $i++) {
	$line=trim(fgets($h));
	$line_arr=explode(' ', $line);
	$t=intval($line_arr[0]);
	$d=intval($line_arr[1]);

	if($t>=$c_t) {
		$c_t=$t+$d;
		$q=array();
		$q[]=$c_t;
		$q_count=1;
		$head=0;

		$this_t=$c_t;

	} else {
		//remove finished queries from queue
		while($q_count>0 && $q[$head]<=$t) {
			unset($q[$head]);
			$head++;
			$q_count--;
		}

		if($q_count===0) {
			$q=array();
			$head=0;
		}

		if($q_count<$b+1) {
			$c_t=$c_t+$d;
			$q[]=$c_t;
			$q_count++;
			$this_t=$c_t;
		} else {
			$this_t=-1;
		}
	}

	echo $this_t;
	if($i<$n-1)
		echo ' ';

}


In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time.

The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integers mi, di, pi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12, di ≥ 1, 1 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year.

Print a single number — the minimum jury size.

<?php

$f = fopen('input.txt', 'r');
fscanf($f, '%d', $n);

$day = 60 * 60 * 24;
$hash = array();
for ($i = 0; $i < $n; $i++) {
    fscanf($f, '%d %d %d %d', $m, $d, $p, $t);
    $s_day = strtotime("2013/$m/$d") - $day * $t;
    for ($j = 0; $j < $t; $j++) {
        if (!isset($hash[$s_day + $day * $j])) {
            $hash[$s_day + $day * $j] = 0;
        }
        $hash[$s_day + $day * $j] += $p;
    }
}

file_put_contents('output.txt', max($hash) . PHP_EOL);


You are given a string $$$s$$$ of length $$$n$$$ consisting of characters a and/or b.Let $$$\operatorname{AB}(s)$$$ be the number of occurrences of string ab in $$$s$$$ as a substring. Analogically, $$$\operatorname{BA}(s)$$$ is the number of occurrences of ba in $$$s$$$ as a substring.In one step, you can choose any index $$$i$$$ and replace $$$s_i$$$ with character a or b.What is the minimum number of steps you need to make to achieve $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$?Reminder:The number of occurrences of string $$$d$$$ in $$$s$$$ as substring is the number of indices $$$i$$$ ($$$1 \le i \le |s| - |d| + 1$$$) such that substring $$$s_i s_{i + 1} \dots s_{i + |d| - 1}$$$ is equal to $$$d$$$. For example, $$$\operatorname{AB}($$$aabbbabaa$$$) = 2$$$ since there are two indices $$$i$$$: $$$i = 2$$$ where aabbbabaa and $$$i = 6$$$ where aabbbabaa.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The first and only line of each test case contains a single string $$$s$$$ ($$$1 \le |s| \le 100$$$, where $$$|s|$$$ is the length of the string $$$s$$$), consisting only of characters a and/or b.

For each test case, print the resulting string $$$s$$$ with $$$\operatorname{AB}(s) = \operatorname{BA}(s)$$$ you'll get making the minimum number of steps. If there are multiple answers, print any of them.

NoteIn the first test case, both $$$\operatorname{AB}(s) = 0$$$ and $$$\operatorname{BA}(s) = 0$$$ (there are no occurrences of ab (ba) in b), so can leave $$$s$$$ untouched.In the second test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 2$$$, so you can leave $$$s$$$ untouched. In the third test case, $$$\operatorname{AB}(s) = 1$$$ and $$$\operatorname{BA}(s) = 0$$$. For example, we can change $$$s_1$$$ to b and make both values zero.In the fourth test case, $$$\operatorname{AB}(s) = 2$$$ and $$$\operatorname{BA}(s) = 1$$$. For example, we can change $$$s_6$$$ to a and make both values equal to $$$1$$$.


Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held:  a < b < c;  a divides b, b divides c. Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has  groups of three.Help Xenia, find the required partition or else say that it doesn't exist.

The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7. It is guaranteed that n is divisible by 3.

If the required partition exists, print  groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them. If there is no solution, print -1.

<?php
/** SECTION 0: NOTES **/
/*
Problem:
Codeforces Contest 199 Div 2 Problem A

Author          : chaotic_iak (http://chaoticiak.comlu.com/)
Language        : PHP 5.4
Released under  : CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
Template version: 20130720 (http://chaoticiak.comlu.com/prog/assorted/templates.phps)
                           (http://ideone.com/AV2T2U)
*/

/** SECTION 1: SET UP INPUT HANDLER **/
// 0: Don't output format at all
// 1: Trim newline characters only
// 2: Assume \x00..\x20 (accaw) as delimiters and output as array

if (isset($inputstream)) {$stdin = $inputstream;} else {$stdin = fopen("php://stdin", "r");}
function g($format = 2) {
    global $stdin;
    $input = fgets($stdin);
    if ($format == 0) {return $input;}
    $input = trim($input, "\n\r");
    if ($format == 1) {return $input;}
    $input = trim($input, "\x00..\x20");
    $input = preg_replace("/[\\x00-\\x20]+/", " ", $input);
    return explode(" ", $input);
}

/** SECTION 2: IMPORT FUNCTIONS FROM chaotic_iak'S LIBRARY **/
// none

/** SECTION 3: SOLVING THE PROBLEM **/
$n = g(1);
$num = g();
$ct = array(0,0,0,0,0,0,0);

for ($i = 0; $i < $n; $i++) {
	$ct[$num[$i]-1]++;
}

if ($ct[4] || $ct[6]) {echo "-1";} else {

$ct[0] -= $ct[3];
$ct[1] -= $ct[3];
$ct[0] -= $ct[2];
$ct[5] -= $ct[2];
$ct[0] -= $ct[5];
$ct[1] -= $ct[5];

if ($ct[0] || $ct[1] || ($ct[5] < 0)) {echo "-1";} else {

echo str_repeat("1 2 4\n", $ct[3]);
echo str_repeat("1 2 6\n", $ct[5]);
echo str_repeat("1 3 6\n", $ct[2]);

}
}

<?php
/** SECTION 0: NOTES **/
/*
Problem:
Codeforces Contest 199 Div 2 Problem A

Author          : chaotic_iak (http://chaoticiak.comlu.com/)
Language        : PHP 5.4
Released under  : CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
Template version: 20130720 (http://chaoticiak.comlu.com/prog/assorted/templates.phps)
                           (http://ideone.com/AV2T2U)
*/

/** SECTION 1: SET UP INPUT HANDLER **/
// 0: Don't output format at all
// 1: Trim newline characters only
// 2: Assume \x00..\x20 (accaw) as delimiters and output as array

if (isset($inputstream)) {$stdin = $inputstream;} else {$stdin = fopen("php://stdin", "r");}
function g($format = 2) {
    global $stdin;
    $input = fgets($stdin);
    if ($format == 0) {return $input;}
    $input = trim($input, "\n\r");
    if ($format == 1) {return $input;}
    $input = trim($input, "\x00..\x20");
    $input = preg_replace("/[\\x00-\\x20]+/", " ", $input);
    return explode(" ", $input);
}

/** SECTION 2: IMPORT FUNCTIONS FROM chaotic_iak'S LIBRARY **/
// none

/** SECTION 3: SOLVING THE PROBLEM **/
$n = g(1);
$num = g();
$ct = array(0,0,0,0,0,0,0);

for ($i = 0; $i < $n; $i++) {
	$ct[$num[$i]-1]++;
}

if ($ct[4] || $ct[6]) {echo "-1";} else {

$ct[0] -= $ct[3];
$ct[1] -= $ct[3];
$ct[0] -= $ct[2];
$ct[5] -= $ct[2];
$ct[0] -= $ct[5];
$ct[1] -= $ct[5];

if ($ct[0] || $ct[1]) {echo "-1";} else {

echo str_repeat("1 2 4\n", $ct[3]);
echo str_repeat("1 2 6\n", $ct[5]);
echo str_repeat("1 3 6\n", $ct[2]);

}
}


The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).  If the wind blows to the east, the boat will move to (x + 1, y).  If the wind blows to the south, the boat will move to (x, y - 1).  If the wind blows to the west, the boat will move to (x - 1, y).  If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?

The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105,  - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different. The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).

If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).

NoteIn the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.In the second sample, they cannot sail to the destination.

<?php
// Is local test?
$LOCALTEST = false;

// Retrieve input
if ($LOCALTEST) {
	$stdin = fopen('test.txt', 'r');
} else {
	$stdin = fopen('php://stdin', 'r');
}
function g() {
	global $stdin;
	return explode(" ", trim(fgets($stdin)));
}

// Codeforces Contest 180 Div 2 Problem B
$raw = g();
$t = $raw[0];
$x1 = $raw[1];
$y1 = $raw[2];
$x2 = $raw[3];
$y2 = $raw[4];
$xd = $x2 - $x1;
$yd = $y2 - $y1;

$n = 0;
$e = 0;
$s = 0;
$w = 0;
if ($xd > 0) {$e = $xd;} else {$w = -$xd;}
if ($yd > 0) {$n = $yd;} else {$s = -$yd;}

$raw = g();
$str = $raw[0];
for ($i = 0; $i < $t; $i++) {
	$wind = substr($str, $i, 1);
	switch ($wind) {
		case "N":
			$n--;
			break;
		case "E":
			$e--;
			break;
		case "S":
			$s--;
			break;
		case "W":
			$w--;
			break;
	}
	if (check()) {
		echo $i+1;
		exit;
	}
}
echo "-1";

function check() {
	global $n, $e, $s, $w;
	if (($n <= 0) && ($e <= 0) && ($s <= 0) && ($w <= 0)) {return true;} else {return false;}
}

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

<?php

$arr = array();
while ($line = trim(fgets(STDIN)))
    $arr[] = $line;
$exp = explode(' ', $arr[0]);
$steps = $arr[1];
//$exp = array(10, 0, 0, 0, 0);
//$steps = 'NENSWESNEE';

for($i = 0; $i < strlen($steps); $i++){
    if($exp[1] == $exp[3] && $exp[2] == $exp[4]){
        echo $i;
        exit;
    }
    switch($steps[$i]){
        case 'E':
            if($exp[3] - $exp[1] > 0)
                $exp[1]++;
            break;
        case 'S':
            if($exp[2] - $exp[4] > 0)
                $exp[2]--;
            break;
        case 'W':
            if($exp[1] - $exp[3] > 0)
                $exp[1]--;
            break;
        case 'N':
            if($exp[4] - $exp[2] > 0)
                $exp[2]++;
            break;
    }
    if($exp[1] == $exp[3] && $exp[2] == $exp[4]){
        echo $i + 1;
        exit;
    }
}
echo -1;
exit;

<?php
    $stdin = fopen('php://stdin', 'r');
    fscanf($stdin, "%d %d %d %d %d\n", $n, $sx, $sy, $ex, $ey);
    fscanf($stdin, "%s\n", $str);

    $arr = str_split($str);
    $cnt = array(
        'S' => 0,
        'W' => 0,
        'E' => 0,
        'N' => 0
    );
    $c = 1;
    $flag = TRUE;
    if(check($sx, $sy, $ex, $ey, $cnt)){
        echo 0;
        exit;
    }
    foreach($arr as $a){

        $cnt[$a] += 1;
        if(check($sx, $sy, $ex, $ey,$cnt)){
            $flag = FALSE;
            break;
        }
        $c++;
    }

    if($flag == TRUE){
        echo -1;
    }
    else{
        echo $c;
    }

    function check($sx, $sy, $ex, $ey, $cnt){

        $flag1 = FALSE;
        $flag2 = FALSE;
        if($ex - $sx >= 0 && $cnt['E'] >= ($ex - $sx)){

            $flag1 = TRUE;
        }
        if($sx - $ex >= 0 && $cnt['W'] >= ($sx - $ex)){

            $flag1 = TRUE;
        }

        if($ey - $sy >= 0 && $cnt['N'] >= ($ey - $sy)){

            $flag2 = TRUE;
        }
        if($sy - $ey >= 0 && $cnt['S'] >= ($sy - $ey)){

            $flag2 = TRUE;
        }

        if($flag1 && $flag2) return TRUE;
        return FALSE;
    }
?>

<?php
// Is local test?
$LOCALTEST = false;

// Retrieve input
if ($LOCALTEST) {
	$stdin = fopen('test.txt', 'r');
} else {
	$stdin = fopen('php://stdin', 'r');
}
function g() {
	global $stdin;
	return explode(" ", trim(fgets($stdin)));
}

// Codeforces Contest 180 Div 2 Problem B
$raw = g();
$t = $raw[0];
$x1 = $raw[1];
$y1 = $raw[2];
$x2 = $raw[3];
$y2 = $raw[4];
$xd = $x2 - $x1;
$yd = $y2 - $y1;

$n = 0;
$e = 0;
$s = 0;
$w = 0;
if ($xd > 0) {$e = $xd;} else {$w = -$xd;}
if ($yd > 0) {$n = $yd;} else {$s = -$yd;}

$raw = g();
$str = $raw[0];
for ($i = 0; $i < $t; $i++) {
	$wind = substr($str, $i, 1);
	switch ($wind) {
		case "N":
			$n--;
			break;
		case "E":
			$e--;
			break;
		case "S":
			$s--;
			break;
		case "W":
			$w--;
			break;
	}
	if (check()) {
		echo $i+1;
		exit;
	}
}
echo "-1";
echo "\n $n $e $s $w";

function check() {
	global $n, $e, $s, $w;
	if (($n <= 0) && ($e <= 0) && ($s <= 0) && ($w <= 0)) {return true;} else {return false;}
}

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

<?php

$arr = array();
while ($line = trim(fgets(STDIN)))
    $arr[] = $line;
$exp = explode(' ', $arr[0]);
$steps = $arr[1];
//$exp = array(10, 5, 3, 3, 6);
//$steps = 'NENSWESNEE';

for($i = 0; $i < strlen($steps); $i++){
    switch($steps[$i]){
        case 'E':
            if($exp[3] - $exp[1] > 0)
                $exp[1]++;
            break;
        case 'S':
            if($exp[2] - $exp[4] > 0)
                $exp[2]++;
            break;
        case 'W':
            if($exp[1] - $exp[3] > 0)
                $exp[1]++;
            break;
        case 'N':
            if($exp[4] - $exp[2] > 0)
                $exp[2]++;
            break;
    }
    if($exp[1] == $exp[3] && $exp[2] == $exp[4]){
        echo $i + 1;
        exit;
    }
}
echo -1;
exit;

<?php

$arr = array();
while ($line = trim(fgets(STDIN)))
    $arr[] = $line;
$exp = explode(' ', $arr[0]);
$steps = $arr[1];
//$exp = array(10, 0, 0, 0, 0);
//$steps = 'NENSWESNEE';

for($i = 0; $i < strlen($steps); $i++){
    if($exp[1] == $exp[3] && $exp[2] == $exp[4]){
        echo $i;
        exit;
    }
    switch($steps[$i]){
        case 'E':
            if($exp[3] - $exp[1] > 0)
                $exp[1]++;
            break;
        case 'S':
            if($exp[2] - $exp[4] > 0)
                $exp[2]++;
            break;
        case 'W':
            if($exp[1] - $exp[3] > 0)
                $exp[1]++;
            break;
        case 'N':
            if($exp[4] - $exp[2] > 0)
                $exp[2]++;
            break;
    }
    if($exp[1] == $exp[3] && $exp[2] == $exp[4]){
        echo $i + 1;
        exit;
    }
}
echo -1;
exit;

<?php
    $stdin = fopen('php://stdin', 'r');
    fscanf($stdin, "%d %d %d %d %d\n", $n, $sx, $sy, $ex, $ey);
    fscanf($stdin, "%s\n", $str);

    $arr = str_split($str);
    $cnt = array(
        'S' => 0,
        'W' => 0,
        'E' => 0,
        'N' => 0
    );
    $c = 0;
    $flag = TRUE;
    foreach($arr as $a){

        if(check($sx, $sy, $ex, $ey,$cnt)){
            $flag = FALSE;
            break;
        }
        $cnt[$a] += 1;
        $c++;
    }

    if($flag == TRUE){
        echo -1;
    }
    else{
        echo $c;
    }

    function check($sx, $sy, $ex, $ey, $cnt){

        $flag1 = FALSE;
        $flag2 = FALSE;
        if($ex - $sx >= 0 && $cnt['E'] >= ($ex - $sx)){

            $flag1 = TRUE;
        }
        if($sx - $ex >= 0 && $cnt['W'] >= ($sx - $ex)){

            $flag1 = TRUE;
        }

        if($ey - $sy >= 0 && $cnt['N'] >= ($ey - $sy)){

            $flag2 = TRUE;
        }
        if($sy - $ey >= 0 && $cnt['S'] >= ($sy - $ey)){

            $flag2 = TRUE;
        }

        if($flag1 && $flag2) return TRUE;
        return FALSE;
    }
?>

<?php
    $stdin = fopen('php://stdin', 'r');
    fscanf($stdin, "%d %d %d %d %d\n", $n, $sx, $sy, $ex, $ey);
    fscanf($stdin, "%s\n", $str);

    $arr = str_split($str);
    $cnt = array(
        'S' => 0,
        'W' => 0,
        'E' => 0,
        'N' => 0
    );
    $c = 1;
    $flag = TRUE;
    foreach($arr as $a){

        $cnt[$a] += 1;
        if(check($sx, $sy, $ex, $ey,$cnt)){
            $flag = FALSE;
            break;
        }
        $c++;
    }

    if($flag == TRUE){
        echo -1;
    }
    else{
        echo $c;
    }

    function check($sx, $sy, $ex, $ey, $cnt){

        $flag1 = FALSE;
        $flag2 = FALSE;
        if($ex - $sx > 0 && $cnt['E'] >= ($ex - $sx)){

            $flag1 = TRUE;
        }
        if($sx - $ex > 0 && $cnt['W'] >= ($sx - $ex)){

            $flag1 = TRUE;
        }

        if($ey - $sy > 0 && $cnt['N'] >= ($ey - $sy)){

            $flag2 = TRUE;
        }
        if($sy - $ey > 0 && $cnt['S'] >= ($sy - $ey)){

            $flag2 = TRUE;
        }

        if($flag1 && $flag2) return TRUE;
        return FALSE;
    }
?>


Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).

The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm.

Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.

<?php
/** SECTION 0: NOTES **/
/*
Problem:
Codeforces Contest 199 Div 2 Problem B

Author          : chaotic_iak (http://chaoticiak.comlu.com/)
Language        : PHP 5.4
Released under  : CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
Template version: 20130720 (http://chaoticiak.comlu.com/prog/assorted/templates.phps)
                           (http://ideone.com/AV2T2U)
*/

/** SECTION 1: SET UP INPUT HANDLER **/
// 0: Don't output format at all
// 1: Trim newline characters only
// 2: Assume \x00..\x20 (accaw) as delimiters and output as array

if (isset($inputstream)) {$stdin = $inputstream;} else {$stdin = fopen("php://stdin", "r");}
function g($format = 2) {
    global $stdin;
    $input = fgets($stdin);
    if ($format == 0) {return $input;}
    $input = trim($input, "\n\r");
    if ($format == 1) {return $input;}
    $input = trim($input, "\x00..\x20");
    $input = preg_replace("/[\\x00-\\x20]+/", " ", $input);
    return explode(" ", $input);
}

/** SECTION 2: IMPORT FUNCTIONS FROM chaotic_iak'S LIBRARY **/
// none

/** SECTION 3: SOLVING THE PROBLEM **/
list($n,$m,$s,$f) = g();
for ($i = 0; $i < $m; $i++) {
	list($t[$i], $l[$i], $r[$i]) = g();
}

$res = "";
$dir = ($s > $f) ? -1 : 1;
$pt = 0;
$time = 1;
while ($s != $f) {
	if ($t[$pt] != $time) {$s += $dir; $res .= ($dir < 0) ? "L" : "R"; $time++;} else {
		if ((($l[$pt] <= $s) && ($s <= $r[$pt])) || (($l[$pt] <= $s+$dir) && ($s+$dir <= $r[$pt]))) {$res .= "X";} else {$s += $dir; $res .= ($dir < 0) ? "L" : "R";}
		$pt++; $time++;
	}
}
echo $res;


Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.

The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.

Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.

NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................


Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.  The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.

Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

NoteIn the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
array_unshift($b, " ");
unset($b[0]);
$c = trim(fgets(STDIN));
$d = explode(" ", trim(fgets(STDIN)));
asort($b);
$e = array_keys($b);
array_unshift($e, " ");
unset($e[0]);
$g = 0;
$h = 0;
for($x = 0; $x < $c; $x++)
{
    $g += $e[$d[$x]];
    $h += $a + 1 - $e[$d[$x]];
}
print $g . " " . $h;
?>

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%d", $n);
$index = array();

$vars = explode(' ', fgets(STDIN));

for($i = 0; $i < $n; ++$i) {
    $index[intval($vars[$i]) - 1] = $i;
}

fscanf(STDIN, "%d", $k);
$x = $y = 0;

$vars = explode(' ', fgets(STDIN));

for($i = 0; $i < $k; ++$i) {
    $v = intval($vars[$i]) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

for($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%s", $s);
    $index[intval($s) - 1] = $i;
}

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

for($i = 0; $i < $k; ++$i) {
    fscanf(STDIN, "%s", $s);
    $v = intval($s) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

for($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%s", $s);
    $index[intval($s) - 1] = $i;
}

print_r($index);

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

for($i = 0; $i < $k; ++$i) {
    fscanf(STDIN, "%s", $s);
    $v = intval($s) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[i]) - 1] = $i;
}

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[i]) - 1] = $i;
}
print_r($vals);
print_r($index);

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

for($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%d ", $a);
    $index[$a - 1] = $i;
}

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

for($i = 0; $i < $k; ++$i) {
    fscanf(STDIN, "%d ", $a);
    $v = $a - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[$i]) - 1] = $i;
}

//print_r($index);

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%d", $n);
$index = array();

for($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%d ", $a);
    $index[$a - 1] = $i;
}

print_r($index);

fscanf(STDIN, "%d", $k);
$x = $y = 0;

for($i = 0; $i < $k; ++$i) {
    fscanf(STDIN, "%d ", $a);
    $v = $a - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[i]) - 1] = $i;
}

print_r($index);

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%d ", $n);
$index = array();

for($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%d ", $a);
    $index[$a - 1] = $i;
}

print_r($index);

fscanf(STDIN, "%d ", $k);
$x = $y = 0;

for($i = 0; $i < $k; ++$i) {
    fscanf(STDIN, "%d ", $a);
    $v = $a - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[$i]) - 1] = $i;
}

print_r($index);

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]) - 1;
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[$i])] = $i;
}

print_r($index);

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]);
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;

<?php

// Using permutation index to figure out the position in order to get the comparison count.

fscanf(STDIN, "%s", $s);
$n = intval($s);
$index = array();

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);
for($i = 0; $i < $n; ++$i) {
    $index[intval($vals[i]) - 1] = $i;
}

fscanf(STDIN, "%s", $s);
$k = intval($s);
$x = 0;
$y = 0;

fscanf(STDIN, "%s", $s);
$vals = explode(' ', $s);

for($i = 0; $i < $k; ++$i) {
    $v = intval($vals[$i]);
    $x += $index[$v] + 1;
    $y += $n - $index[$v];
}

echo $x.' '.$y;


Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module.So imagine Monocarp got recommended $$$n$$$ songs, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th song had its predicted rating equal to $$$p_i$$$, where $$$1 \le p_i \le n$$$ and every integer from $$$1$$$ to $$$n$$$ appears exactly once. In other words, $$$p$$$ is a permutation.After listening to each of them, Monocarp pressed either a like or a dislike button. Let his vote sequence be represented with a string $$$s$$$, such that $$$s_i=0$$$ means that he disliked the $$$i$$$-th song, and $$$s_i=1$$$ means that he liked it.Now the service has to re-evaluate the song ratings in such a way that:  the new ratings $$$q_1, q_2, \dots, q_n$$$ still form a permutation ($$$1 \le q_i \le n$$$; each integer from $$$1$$$ to $$$n$$$ appears exactly once);  every song that Monocarp liked should have a greater rating than every song that Monocarp disliked (formally, for all $$$i, j$$$ such that $$$s_i=1$$$ and $$$s_j=0$$$, $$$q_i>q_j$$$ should hold). Among all valid permutations $$$q$$$ find the one that has the smallest value of $$$\sum\limits_{i=1}^n |p_i-q_i|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$.Print the permutation $$$q_1, q_2, \dots, q_n$$$. If there are multiple answers, you can print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of songs. The second line of each testcase contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation of the predicted ratings. The third line contains a single string $$$s$$$, consisting of $$$n$$$ characters. Each character is either a $$$0$$$ or a $$$1$$$. $$$0$$$ means that Monocarp disliked the song, and $$$1$$$ means that he liked it. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase, print a permutation $$$q$$$ — the re-evaluated ratings of the songs. If there are multiple answers such that $$$\sum\limits_{i=1}^n |p_i-q_i|$$$ is minimum possible, you can print any of them.

NoteIn the first testcase, there exists only one permutation $$$q$$$ such that each liked song is rating higher than each disliked song: song $$$1$$$ gets rating $$$2$$$ and song $$$2$$$ gets rating $$$1$$$. $$$\sum\limits_{i=1}^n |p_i-q_i|=|1-2|+|2-1|=2$$$.In the second testcase, Monocarp liked all songs, so all permutations could work. The permutation with the minimum sum of absolute differences is the permutation equal to $$$p$$$. Its cost is $$$0$$$.