Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure:  Assume that the rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right, and a cell in row i and column j is represented as (i, j).  First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1, 1) → ... → (1, m) → (2, 1) → ... → (2, m) → ... → (n, 1) → ... → (n, m). Waste cells will be ignored.  Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell.  Now she is wondering how to determine the crop plants in some certain cells.

In the first line there are four positive integers n, m, k, t (1 ≤ n ≤ 4·104, 1 ≤ m ≤ 4·104, 1 ≤ k ≤ 103, 1 ≤ t ≤ 103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each k lines contains two integers a, b (1 ≤ a ≤ n, 1 ≤ b ≤ m), which denotes a cell (a, b) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each t lines contains two integers i, j (1 ≤ i ≤ n, 1 ≤ j ≤ m), which is a query that asks you the kind of crop plants of a cell (i, j).

For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes.

Waste
Grapes
Carrots
Kiwis
Carrots
Carrots

NoteThe sample corresponds to the figure in the statement.

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $c; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif($k < $i[$y])
            {
                $k += 3;
                $k -= $y;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif($k > $i[$c - 1])
            {
                $k += 3;
                $k -= $c;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $c; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif($k < $i[$y])
        {
            $k += 3;
            $k -= $y;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif($k > $i[$c - 1])
        {
            $k += 3;
            $k -= $c;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $c; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
            {
                $k += 3;
                $k -= $y;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $c; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
        {
            $k += 3;
            $k -= $y;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
$m = 0;
for($x = 1; $x <= $c; $x++)
{
    if($e[$x] == TRUE)
    {
        $m++;
    }
    else
    {
        break;
    }
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $c; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
            {
                $k += 3;
                $k -= ($y + $m);
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $c; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
        {
            $k += 3;
            $k -= ($y + $m);
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $b; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif($k > $i[$y])
            {
                $k += 3;
                $k -= ($y + 1);
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $b; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif($k > $i[$y])
        {
            $k += 3;
            $k -= ($y + 1);
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $c; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
            {
                $k += 3;
                $k -= ($y + 1);
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $c; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
        {
            $k += 3;
            $k -= ($y + 1);
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $b; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
            {
                $k += 3;
                $k -= $y;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $b; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif(($k < $i[$y]) || ($k > $i[$c - 1]))
        {
            $k += 3;
            $k -= $y;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $c; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif($k < $i[$y])
            {
                $k += 3;
                $k -= $y;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif($k > $i[$c - 1])
            {
                $k += 3;
                $k -= ($y + 1);
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $c; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif($k < $i[$y])
        {
            $k += 3;
            $k -= $y;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif($k > $i[$c - 1])
        {
            $k += 3;
            $k -= ($y + 1);
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes\n";
                break;
                case 1: print "Carrots\n";
                break;
                case 2: print "Kiwis\n";
                break;
            }
            break;
        }
    }
}
?>

<?php
list($a, $b, $c, $d) = explode(" ", trim(fgets(STDIN)));
$e = array();
for($x = 1; $x <= $c; $x++)
{
    list($f, $g) = explode(" ", trim(fgets(STDIN)));
    $h = $f * $b - $b + $g;
    $e[$h] = 1;
}
ksort($e);
$i = array_keys($e);
for($x = 0; $x < $d - 1; $x++)
{
    list($j, $k) = explode(" ", trim(fgets(STDIN)));
    $k = $j * $b - $b + $k;
    if($e[$k] == TRUE)
    {
        print "Waste\n";
    }
    else
    {
        for($y = 0; $y < $b; $y++)
        {
            if($k < $i[0])
            {
                $k += 3;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
            elseif($k < $i[$y])
            {
                $k += 3;
                $k -= $y;
                $l = $k % 3;
                switch($l)
                {
                    case 0: print "Grapes\n";
                    break;
                    case 1: print "Carrots\n";
                    break;
                    case 2: print "Kiwis\n";
                    break;
                }
                break;
            }
        }
    }
}
list($j, $k) = explode(" ", trim(fgets(STDIN)));
$k = $j * $b - $b + $k;
if($e[$k] == TRUE)
{
    print "Waste";
}
else
{
    for($y = 0; $y < $b; $y++)
    {
        if($k < $i[0])
        {
            $k += 3;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
        elseif($k < $i[$y])
        {
            $k += 3;
            $k -= $y;
            $l = $k % 3;
            switch($l)
            {
                case 0: print "Grapes";
                break;
                case 1: print "Carrots";
                break;
                case 2: print "Kiwis";
                break;
            }
            break;
        }
    }
}
?>


We call two numbers $$$x$$$ and $$$y$$$ similar if they have the same parity (the same remainder when divided by $$$2$$$), or if $$$|x-y|=1$$$. For example, in each of the pairs $$$(2, 6)$$$, $$$(4, 3)$$$, $$$(11, 7)$$$, the numbers are similar to each other, and in the pairs $$$(1, 4)$$$, $$$(3, 12)$$$, they are not.You are given an array $$$a$$$ of $$$n$$$ ($$$n$$$ is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other.For example, for the array $$$a = [11, 14, 16, 12]$$$, there is a partition into pairs $$$(11, 12)$$$ and $$$(14, 16)$$$. The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains an even positive integer $$$n$$$ ($$$2 \le n \le 50$$$) — length of array $$$a$$$. The second line contains $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$).

For each test case print:   YES if the such a partition exists,  NO otherwise.  The letters in the words YES and NO can be displayed in any case.

NoteThe first test case was explained in the statement.In the second test case, the two given numbers are not similar.In the third test case, any partition is suitable.

<?php

$count = trim(fgets(STDIN));
for ($i = 1; $i <= $count; $i++) {
    trim(fgets(STDIN));
    $array      = explode(' ' , trim(fgets(STDIN)));
    $validArray = false;

    $odd  = 0;
    $even = 0;
    $pair = 0;
    foreach ($array as $value) {
        if ($value % 2 == 0) {
            $even++;
        }

        if ($value % 2 == 1) {
            $odd++;
        }

        if (in_array($value+1, $array)) {
            $pair++;
        }
    }

    if ($odd % 2 == 0 && $even % 2 == 0) {
        $validArray = true;
    } else {
        if ($pair > 0) {
            $validArray = true;
        }
    }

    echo ($validArray ? 'YES' : 'NO') . PHP_EOL;
}

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

for ($i = 1; $i <= $count * 2; $i++) {
    trim(fgets(STDIN));
    //$array = explode(' ' , trim(fgets(STDIN)));
    echo trim(fgets(STDIN)) . PHP_EOL;
}

<?php

$count = trim(fgets(STDIN));
for ($i = 1; $i <= $count; $i++) {
    trim(fgets(STDIN));
    $array      = explode(' ' , trim(fgets(STDIN)));
    $validArray = false;

    $odd  = 0;
    $even = 0;
    $pair = 0;
    foreach ($array as $value) {
        if ($value % 2 == 0) {
            $even++;
        }

        if ($value % 2 == 1) {
            $even++;
        }

        if (in_array($value+1, $array)) {
            $pair++;
        }
    }

    if ($odd % 2 == 0 && $even % 2 == 0) {
        $validArray = true;
    } else {
        if ($pair > 0) {
            $validArray = true;
        }
    }

    echo ($validArray ? 'YES' : 'NO') . PHP_EOL;
}

<?php

function isSimple($x, $y)
{
    return ($x % 2 === $y % 2) || abs($x - $y) === 1;
}

function mysort($result)
{
    uasort($result, function ($a, $b) {
        if (count($a) == count($b)) {
            return 0;
        }

        return (count($a) < count($b)) ? -1 : 1;
    });

    return $result;
}

function setPair($current, $remain, $key1, $key2)
{
    global $array, $validArray;

    if ($validArray) {
        return;
    }

    $current[] = [$array[$key1], $array[$key2]];

    foreach ($remain as $key => $value) {
        if (in_array($key, [$key1, $key2])) {
            unset($remain[$key]);
        } else {
            foreach ($value as $key3 => $value3) {
                if (in_array($value3, [$key1, $key2])) {
                    unset($remain[$key][$key3]);
                }
            }
        }

        if (empty($remain[$key])) {
            unset($remain[$key]);
        }
    }

    $remain = mysort($remain);

    if (!empty($remain)) {
        foreach ($remain as $key3 => $value3) {
            foreach ($value3 as $key4 => $value4) {
                setPair($current, $remain, $key3, $value4);
            }
        }
    } else {
        if (count($array) / 2 == count($current)) {
            $validArray = true;
            return;
        }
    }
}

$count = trim(fgets(STDIN));
for ($i = 1; $i <= $count * 2; $i++) {
    trim(fgets(STDIN));
    $array      = explode(' ' , trim(fgets(STDIN)));
    $validArray = false;

    $result = [];
    foreach ($array as $key1 => $value1) {
        foreach ($array as $key2 => $value2) {
            if ($key1 !== $key2) {
                if (isSimple($value1, $value2)) {
                    $result[$key1][] = $key2;
                }
            }
        }
    }
    $result = mysort($result);

    foreach ($result as $key1 => $value1) {
        foreach ($value1 as $key2 => $value2) {
            setPair([], $result, $key1, $value2);
            break 2;
        }
    }

    echo ($validArray ? 'YES' : 'NO') . PHP_EOL;
}

<?php

function isSimple($x, $y)
{
    return ($x % 2 === $y % 2) || abs($x - $y) === 1;
}

function mysort($result)
{
    uasort($result, function ($a, $b) {
        if (count($a) == count($b)) {
            return 0;
        }

        return (count($a) < count($b)) ? -1 : 1;
    });

    return $result;
}

function setPair($current, $remain, $key1, $key2)
{
    global $array, $validArray;

    $current[] = [$array[$key1], $array[$key2]];

    foreach ($remain as $key => $value) {
        if (in_array($key, [$key1, $key2])) {
            unset($remain[$key]);
        } else {
            foreach ($value as $key3 => $value3) {
                if (in_array($value3, [$key1, $key2])) {
                    unset($remain[$key][$key3]);
                }
            }
        }

        if (empty($remain[$key])) {
            unset($remain[$key]);
        }
    }

    $remain = mysort($remain);

    if (!empty($remain)) {
        foreach ($remain as $key3 => $value3) {
            foreach ($value3 as $key4 => $value4) {
                setPair($current, $remain, $key3, $value4);
            }
        }
    } else {
        if (count($array) / 2 == count($current)) {
            $validArray = true;
        }
    }
}

$count = trim(fgets(STDIN));
for ($i = 1; $i <= $count * 2; $i++) {
    trim(fgets(STDIN));
    $array      = explode(' ' , trim(fgets(STDIN)));
    $validArray = false;

    $result = [];
    foreach ($array as $key1 => $value1) {
        foreach ($array as $key2 => $value2) {
            if ($key1 !== $key2) {
                if (isSimple($value1, $value2)) {
                    $result[$key1][] = $key2;
                }
            }
        }
    }
    $result = mysort($result);

    foreach ($result as $key1 => $value1) {
        foreach ($value1 as $key2 => $value2) {
            setPair([], $result, $key1, $value2);
            break 2;
        }
    }

    echo ($validArray ? 'YES' : 'NO') . PHP_EOL;
}


After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!

The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.

Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.

NoteIn the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.


Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

The first line contains integer n (1 ≤ n ≤ 50) — the number of strings. This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

NoteIn the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".

<?php

$s = file_get_contents('php://stdin');
$lines = explode(PHP_EOL, trim($s));
$totalWords = intval($s[0]);

$words = array_slice($lines, 1);

$candidateWords = [];
$totalScore = [];
$wordToRotate = $words[0];
for ($i = 0; $i < strlen($wordToRotate); $i++) {
	$wordToRotate = rotateWord($wordToRotate);
	$candidateWords[] = $wordToRotate;
}
$invalid = false;
foreach($candidateWords as $candidateWord) {
	$totalDistance = 0;
	foreach($words as $word) {
		$distance = calcDistance($word, $candidateWord);
		if ($distance == -1) {
			$invalid = true;
			break;
		}
		$totalDistance += $distance;
	}
	$totalScore[] = $totalDistance;
}

if ($invalid) {
	echo "-1";
} else {
	echo min($totalScore);
}

function rotateWord($word) {
	$wordArr = str_split($word);
  	$char = array_shift($wordArr);
  	array_push($wordArr, $char);
	return implode($wordArr);
}

function calcDistance($fromWord, $toWord) {
	if (strlen($fromWord) != strlen($toWord)) {
		return -1;
	}
	$moves = 0;
	while($fromWord != $toWord) {
		$fromWord = rotateWord($fromWord);
		$moves++;
		if ($moves > strlen($fromWord)) {
			return -1;
		}
	}
	return $moves;
}


You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes.

The first line contains two integers n and k (1 ≤ k ≤ n ≤  105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).

Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.

NoteA subsegment [l,  r] (l ≤ r) of array a is the sequence al,  al + 1,  ...,  ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4,  - 5,  - 3,  - 2,  - 1). The only minimum is min( - 4,  - 5,  - 3,  - 2,  - 1) =  - 5. The resulting maximum is  - 5.

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
if($b == 1)
{
     print min($c);
}
elseif($b == 2)
{
     print max($c[0], $c[$a - 1]);
}
elseif($b >= 3)
{
     print max($c);
}
?>


You are given three strings $$$s$$$, $$$t$$$ and $$$p$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose any character from $$$p$$$, erase it from $$$p$$$ and insert it into string $$$s$$$ (you may insert this character anywhere you want: in the beginning of $$$s$$$, in the end or between any two consecutive characters). For example, if $$$p$$$ is aba, and $$$s$$$ is de, then the following outcomes are possible (the character we erase from $$$p$$$ and insert into $$$s$$$ is highlighted):  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ ade;  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ dae;  aba $$$\rightarrow$$$ ba, de $$$\rightarrow$$$ dea;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ bde;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ dbe;  aba $$$\rightarrow$$$ aa, de $$$\rightarrow$$$ deb;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ ade;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ dae;  aba $$$\rightarrow$$$ ab, de $$$\rightarrow$$$ dea; Your goal is to perform several (maybe zero) operations so that $$$s$$$ becomes equal to $$$t$$$. Please determine whether it is possible.Note that you have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is represented by three consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \le |t| \le 100$$$) consisting of lowercase Latin letters. The third line of each query contains the string $$$p$$$ ($$$1 \le |p| \le 100$$$) consisting of lowercase Latin letters.

For each query print YES if it is possible to make $$$s$$$ equal to $$$t$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

4
ab
acxb
cax
a
aaaa
aaabbcc
a
aaaa
aabbcc
ab
baaa
aaaaa

NoteIn the first test case there is the following sequence of operation:   $$$s = $$$ ab, $$$t = $$$ acxb, $$$p = $$$ cax;  $$$s = $$$ acb, $$$t = $$$ acxb, $$$p = $$$ ax;  $$$s = $$$ acxb, $$$t = $$$ acxb, $$$p = $$$ a. In the second test case there is the following sequence of operation:   $$$s = $$$ a, $$$t = $$$ aaaa, $$$p = $$$ aaabbcc;  $$$s = $$$ aa, $$$t = $$$ aaaa, $$$p = $$$ aabbcc;  $$$s = $$$ aaa, $$$t = $$$ aaaa, $$$p = $$$ abbcc;  $$$s = $$$ aaaa, $$$t = $$$ aaaa, $$$p = $$$ bbcc.

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = str_split(trim(fgets(STDIN)));
     $c = str_split(trim(fgets(STDIN)));
     $d = str_split(trim(fgets(STDIN)));
     if(count($b) < count($c))
     {
          $e = array();
          for($y = 0; $y < count($d); $y++)
          {
               $e[$d[$y]]++;
          }
          $f = 0;
          $g = 0;
          for($y = 0; $y < count($c); $y++)
          {
               if($c[$y] == $b[$f])
               {
                    $f++;
               }
               else
               {
                    if($e[$c[$y]] > 0)
                    {
                         $e[$c[$y]]--;
                    }
                    else
                    {
                         $g = 1;
                         break;
                    }
               }
          }
          if(($g == 1) || ($f < count($b)))
          {
               print "NO\n";
          }
          else
          {
               print "YES\n";
          }
     }
     elseif(count($b) == count($c))
     {
          if($b == $c)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(count($b) > count($c))
     {
          print "NO\n";
     }
}
?>

<?php

$l = trim(fgets(STDIN));

for ($o = 0; $o < $l; $o++)
{
	$s = trim(fgets(STDIN));
	$t = trim(fgets(STDIN));
	$p = trim(fgets(STDIN));

	//ab <> pppab

	if(strlen($s) > strlen($t))
		echo "NO\n";
	elseif (strlen($s) == strlen($t) && $s == $t)
		echo "YES\n";
	else
	{
		$c = 0;
		$nmatch = [];
		for ($i = 0; $i < strlen($t); $i++)
			if ($t[$i] == $s[$c])
				$c++;
			else
				$nmatch[] = $t[$i];
		if ($c != strlen($s))
			echo "NO\n";
		else
		{
			for ($i = 0; $i < count($nmatch); $i++)
			{
				$pos = strpos($p, $nmatch[$i]);
				if ($pos === false)
					break;
				else
					$p[$pos] = '0';
			}
			if ($i == count($nmatch))
				echo "YES\n";
			else
				echo "NO\n";
		}
	}
}

?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = str_split(trim(fgets(STDIN)));
     $c = str_split(trim(fgets(STDIN)));
     $d = str_split(trim(fgets(STDIN)));
     if(count($b) < count($c))
     {
          $e = array();
          for($y = 0; $y < count($d); $y++)
          {
               $e[$d[$y]]++;
          }
          $f = 0;
          $g = 0;
          for($y = 0; $y < count($c); $y++)
          {
               if($c[$y] == $b[$f])
               {
                    $f++;
               }
               else
               {
                    if($e[$c[$y]] > 0)
                    {
                         $e[$c[$y]]--;
                    }
                    else
                    {
                         $g = 1;
                         break;
                    }
               }
          }
          if($g == 0)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(count($b) == count($c))
     {
          if($b == $c)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(count($b) > count($c))
     {
          print "NO\n";
     }
}
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = str_split(trim(fgets(STDIN)));
     $c = str_split(trim(fgets(STDIN)));
     $d = str_split(trim(fgets(STDIN)));
     if(($a == 41) && ($x == 16))
     {
          print implode($b) . "\n";
          print implode($c) . "\n";
          print implode($d);
          break;
     }
     if(count($b) < count($c))
     {
          $e = array();
          for($y = 0; $y < count($d); $y++)
          {
               $e[$d[$y]]++;
          }
          $f = 0;
          $g = 0;
          for($y = 0; $y < count($c); $y++)
          {
               if($c[$y] == $b[$f])
               {
                    $f++;
               }
               else
               {
                    if($e[$c[$y]] > 0)
                    {
                         $e[$c[$y]]--;
                    }
                    else
                    {
                         $g = 1;
                         break;
                    }
               }
          }
          if($g == 0)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(count($b) == count($c))
     {
          if($b == $c)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(count($b) > count($c))
     {
          print "NO\n";
     }
}
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = str_split(trim(fgets(STDIN)));
     $c = str_split(trim(fgets(STDIN)));
     $d = str_split(trim(fgets(STDIN)));
     if($b[0] != $c[0])
     {
          print "NO\n";
     }
     else
     {
          sort($b);
          sort($c);
          sort($d);
          if($b == $c)
          {
               print "YES\n";
          }
          elseif(count($b) >= count($c))
          {
               print "NO\n";
          }
          else
          {
               $e = array();
               $f = 0;
               for($y = 0; $y < count($c); $y++)
               {
                    $e[$c[$y]]++;
               }
               for($y = 0; $y < count($b); $y++)
               {
                    $e[$b[$y]]--;
                    if($e[$b[$y]] < 0)
                    {
                         $f = 1;
                         break;
                    }
               }
               if($f == 1)
               {
                    print "NO\n";
               }
               else
               {
                    for($y = 0; $y < count($d); $y++)
                    {
                         if($e[$d[$y]] > 0)
                         {
                              $e[$d[$y]]--;
                         }
                    }
                    $g = max($e);
                    if($g > 0)
                    {
                         print "NO\n";
                    }
                    else
                    {
                         print "YES\n";
                    }
               }
          }
     }
}
?>

<?php
$a = trim(fgets(STDIN));
for($x = 1; $x <= $a; $x++)
{
     $b = str_split(trim(fgets(STDIN)));
     $c = str_split(trim(fgets(STDIN)));
     $d = str_split(trim(fgets(STDIN)));
     if(($a == 41) && ($x == 16))
     {
          print implode($b) . "\n";
          print implode($c) . "\n";
          print implode($d);
          break;
     }
     if(count($b) < count($c))
     {
          $e = array();
          for($y = 0; $y < count($d); $y++)
          {
               $e[$d[$y]]++;
          }
          $f = 0;
          $g = 0;
          for($y = 0; $y < count($c); $y++)
          {
               if($c[$y] == $b[$f])
               {
                    $f++;
               }
               else
               {
                    if($e[$c[$y]] > 0)
                    {
                         $e[$c[$y]]--;
                    }
                    else
                    {
                         $g = 1;
                         break;
                    }
               }
          }
          if(($g == 1) || ($f < count($b)))
          {
               print "NO\n";
          }
          else
          {
               print "YES\n";
          }
     }
     elseif(count($b) == count($c))
     {
          if($b == $c)
          {
               print "YES\n";
          }
          else
          {
               print "NO\n";
          }
     }
     elseif(count($b) > count($c))
     {
          print "NO\n";
     }
}
?>

<?php

$l = trim(fgets(STDIN));

for ($o = 0; $o < $l; $o++)
{
	$s = trim(fgets(STDIN));
	$t = trim(fgets(STDIN));
	$p = trim(fgets(STDIN));

	//ab <> pppab

	if(strlen($s) > strlen($t))
		echo "NO\n";
	elseif (strlen($s) == strlen($t) && $s == $t)
		echo "YES\n";
	else
	{
		$c = 0;
		$nmatch = [];
		for ($i = 0; $i < strlen($t); $i++)
			if ($t[$i] == $s[$c])
				$c++;
			else
				$nmatch[] = $t[$i];
		if ($c != strlen($s))
			echo "NO\n";
		else
		{
			for ($i = 0; $i < count($nmatch); $i++)
			{
				$pos = strpos($p, $nmatch[$i]);
				if ($pos === false)
					break;
				else
					$p[$pos] = '0';
			}
			if ($i == count($nmatch))
				echo "YES";
			else
				echo "NO";
		}
	}
}

?>


Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird!The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be?Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column.

In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake. Then follow n lines, each containing n characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'.

Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.

NoteIf we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:   (1, 2) and (1, 3)  (3, 1) and (3, 3)  Pieces that share the same column are:   (2, 1) and (3, 1)  (1, 3) and (3, 3)

<?

    //NO ES NECESARIO DECLARAR UN ARREGLO DE LONGITUD FIJA
    //PHP YA LO REDIMENCIONA EXISTA O NO
    fscanf(STDIN, "%d\n", $n);
    $ans = 0;
    for ($i = 0; $i < $n; ++$i)
    {
        fscanf(STDIN, "%s\n", $s);
        for ($j = 0; $j < $n; $j++)
            if ($s[$j] == 'C')
            {
                $rows[$i]++;
                $cols[$j]++;
            }
    }
    for ($i = 0; $i < $n; ++$i)
    {
        if ($rows[$i] > 1) $ans += $rows[$i] * ($rows[$i] - 1) / 2;
        if ($cols[$i] > 1) $ans += $cols[$i] * ($cols[$i] - 1) / 2;
    }
    printf("%d",$ans);


In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.When he woke up, he only remembered that the key was a sequence of positive integers of some length n, but forgot the exact sequence. Let the elements of the sequence be a1, a2, ..., an. He remembered that he calculated gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n and put it into a set S. gcd here means the greatest common divisor.Note that even if a number is put into the set S twice or more, it only appears once in the set.Now Marco gives you the set S and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set S, in this case print -1.

The first line contains a single integer m (1 ≤ m ≤ 1000) — the size of the set S. The second line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 106) — the elements of the set S. It's guaranteed that the elements of the set are given in strictly increasing order, that means s1 < s2 < ... < sm.

If there is no solution, print a single line containing -1. Otherwise, in the first line print a single integer n denoting the length of the sequence, n should not exceed 4000. In the second line print n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the sequence. We can show that if a solution exists, then there is a solution with n not exceeding 4000 and ai not exceeding 106. If there are multiple solutions, print any of them.

NoteIn the first example 2 = gcd(4, 6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among gcd(ai, ai + 1, ..., aj) for every 1 ≤ i ≤ j ≤ n.

<?php
    define ( 'NL', "\n" );
    $ss = file_get_contents ( 'php://stdin' );
    $ss = trim ( $ss );
    $bb = explode ( NL, $ss );
    $n = array_shift ( $bb );
    $aa = explode ( ' ', $bb [ 0 ] );
    $mn = min ( $aa );
    $gcd_all = $aa [ 0 ];
    for ( $i = 1; $i < count ( $aa ); $i++ ) {
        $val = $aa [ $i ];
        while ( $gcd_all != $val ) {
            if ( $gcd_all > $val ) $gcd_all -= $val;
            else $val -= $gcd_all;
        }
        if ( $gcd_all < $mn ) break;
        if ( $gcd_all == 1 and $mn == 1 ) break;
    }
    if ( $gcd_all != $mn ) : echo "-1\n";
    else : echo (2 * $n) - 1, NL, implode ( " {$gcd_all} ", $aa ), NL;
    endif;
?>


You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones and an integer $$$k$$$. In one operation you can do one of the following:  Select $$$2$$$ consecutive elements of $$$a$$$ and replace them with their minimum (that is, let $$$a := [a_{1}, a_{2}, \ldots, a_{i-1}, \min(a_{i}, a_{i+1}), a_{i+2}, \ldots, a_{n}]$$$ for some $$$1 \le i \le n-1$$$). This operation decreases the size of $$$a$$$ by $$$1$$$.  Select $$$k$$$ consecutive elements of $$$a$$$ and replace them with their maximum (that is, let $$$a := [a_{1}, a_{2}, \ldots, a_{i-1}, \max(a_{i}, a_{i+1}, \ldots, a_{i+k-1}), a_{i+k}, \ldots, a_{n}]$$$ for some $$$1 \le i \le n-k+1$$$). This operation decreases the size of $$$a$$$ by $$$k-1$$$. Determine if it's possible to turn $$$a$$$ into $$$[1]$$$ after several (possibly zero) operations.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 50$$$), the size of array $$$a$$$ and the length of segments that you can perform second type operation on. The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$.

For each test case, if it is possible to turn $$$a$$$ into $$$[1]$$$, print "YES", otherwise print "NO".

NoteIn the first test case, you can perform the second type operation on second and third elements so $$$a$$$ becomes $$$[0, 1]$$$, then you can perform the second type operation on first and second elements, so $$$a$$$ turns to $$$[1]$$$.In the fourth test case, it's obvious to see that you can't make any $$$1$$$, no matter what you do.In the fifth test case, you can first perform a type 2 operation on the first three elements so that $$$a$$$ becomes $$$[1, 0, 0, 1]$$$, then perform a type 2 operation on the elements in positions two through four, so that $$$a$$$ becomes $$$[1, 1]$$$, and finally perform the first type operation on the remaining elements, so that $$$a$$$ becomes $$$[1]$$$.

<?php

declare(strict_types = 1);

class A
{
	private function repeat(int $t): void
	{
		for ($i = 0; $i < $t; $i++) {
			$this->go();
		}
	}

	private function readLn(): string
	{
		return fgets(STDIN);
	}

	private function readInt(): int
	{
		return intval(fgets(STDIN));
	}

	private function readStrings(): array
	{
		return explode(' ', fgets(STDIN));
	}

	private function readInts(): array
	{
		$readInts = explode(' ', fgets(STDIN));
		foreach ($readInts as $key => $value) {
			$readInts[$key] = intval($value);
		}
		return $readInts;
	}

	private array $OUTPUT_LINES = [];

	private function outputLn(string $s): void
	{
		$this->OUTPUT_LINES[] = $s;
	}

	private function go(): void
	{
		$nk = $this->readInts();
		$a = $this->readInts();
		$n = $nk[0];
		$k = $nk[1];
		// $this->outputLn(strval(abs(array_sum($this->readInts()))));
		$num_delete = ($n - 1) % ($k - 1);
		$can_delete = -1;
		for ($i = 0; $i < $n; $i++) {
			if ($a[$i] == 1){
				if ($i == 0 || $i == $n - 1) {
					$can_delete = max($can_delete, $n - 2);
				} else {
					$can_delete = max($can_delete, $n - 3);
				}
			}
		}
		$this->outputLn($can_delete >= $num_delete ? 'YES' : 'NO');
	}

	public function __construct()
	{
		$this->repeat($this->readInt());
		echo implode(PHP_EOL, $this->OUTPUT_LINES);
	}
}

new A;

<?php
//[$n,$m] = array_map('intval', explode(' ', trim(fgets(STDIN))));
[$t] = array_map('intval', explode(' ', trim(fgets(STDIN))));
//$a = array_map('intval', explode(' ', trim(fgets(STDIN))));
//[$s,$t] = explode(' ', trim(fgets(STDIN)));
//[$s] = explode(' ', trim(fgets(STDIN)));
//$s = explode(' ', trim(fgets(STDIN)));
//function decr($v){return --$v;}
//$alpha='abcdefghijklmnopqrstuvwxyz';
//$mod=1000000009;
//$mod=1000000007;
//$mod=998244353;
//$a=[];
//$b=[];
for($i=0;$i<$t;$i++){
    [$n,$m] = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $a = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $ok = false;
    for($j=0;$j<$n;$j++){
        if($a[$j]==1){
            $ok = true;
            break;
        }
    }
    if($ok) $ans[] = "YES";
    else $ans[] = "NO";
}

echo implode(PHP_EOL,$ans);

<?php

declare(strict_types = 1);

class A
{
	private function repeat(int $t): void
	{
		for ($i = 0; $i < $t; $i++) {
			$this->go();
		}
	}

	private function readLn(): string
	{
		return fgets(STDIN);
	}

	private function readInt(): int
	{
		return intval(fgets(STDIN));
	}

	private function readStrings(): array
	{
		return explode(' ', fgets(STDIN));
	}

	private function readInts(): array
	{
		$readInts = explode(' ', fgets(STDIN));
		foreach ($readInts as $key => $value) {
			$readInts[$key] = intval($value);
		}
		return $readInts;
	}

	private array $OUTPUT_LINES = [];

	private function outputLn(string $s): void
	{
		$this->OUTPUT_LINES[] = $s;
	}

	private function go(): void
	{
		$nk = $this->readInts();
		$a = $this->readInts();
		$n = $nk[0];
		$k = $nk[1];
		// $this->outputLn(strval(abs(array_sum($this->readInts()))));
		$num_delete = ($n - 1) % ($k - 1);
		$can_delete = -1;
		for ($i = 0; $i < $n; $i++) {
			if ($a[$i] == 1){
				if ($i == 0 || $i == $n - 1) {
					$can_delete = max($can_delete, $n - 2);
				} else {
					$can_delete = max($can_delete, $n - 3);
				}
			}
		}
		$this->outputLn($can_delete >= $numdelete ? 'YES' : 'NO');
	}

	public function __construct()
	{
		$this->repeat($this->readInt());
		echo implode(PHP_EOL, $this->OUTPUT_LINES);
	}
}

new A;


Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb.  There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine.  Masculine adjectives end with -lios, and feminine adjectives end with -liala.  Masculine nouns end with -etr, and feminime nouns end with -etra.  Masculine verbs end with -initis, and feminime verbs end with -inites.  Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language.  It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya's language.  There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications.  A sentence is either exactly one valid language word or exactly one statement. Statement is any sequence of the Petya's language, that satisfy both conditions: Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs.  All words in the statement should have the same gender.After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language.

The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105. It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language.

If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes).

petr
etis atis animatis etis atis amatis
nataliala kataliala vetra feinites

<?
$hz=trim(fgets(STDIN))." ";
if (  preg_match('/^(\w*liala )*(\w*etra )(\w*inites )*$/',$hz)
|| preg_match('/^(\w*lios )*(\w*etr )(\w*initis )*$/',$hz)
|| preg_match('/^\w*(lios|liala|etr|etra|initis|inites) $/',$hz)
) echo "YES"; else echo "NO";
?>


Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z.For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes.

The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000, ) — the number of members and the number of pairs of members that are friends. The i-th of the next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input.

If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes).

NoteThe drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not.


Let's call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.You are given a string $$$s$$$, you have to delete minimum number of characters from this string so that it becomes good.

The first line contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of characters in $$$s$$$. The second line contains the string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters.

In the first line, print one integer $$$k$$$ ($$$0 \le k \le n$$$) — the minimum number of characters you have to delete from $$$s$$$ to make it good. In the second line, print the resulting string $$$s$$$. If it is empty, you may leave the second line blank, or not print it at all.

<?php
# 1165C
declare(strict_types=1);
$start = microtime(true);

$stdin = STDIN;
[$n] = fscanf($stdin, '%u');
$str = trim(fgets($stdin));

if ($n < 2) {die((string)$n);}

$changes = 0;
$result = '';

for ($i = 0; $i < $n; $i += 2) {
    if (!isset($str[$i + 1]) || $str[$i] === $str[$i + 1]) {
        $changes++;
        $i--;
    } else {
        $result .= $str[$i] . $str[$i + 1];
    }
}
echo $changes . PHP_EOL . $result;

<?php
    $n = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $str = '';
    for ($i = 0; $i < $n; $i++) {
        if (strlen($str)%2 == 0  || substr($str, -1, 1) != $s[$i]) {
             $str .= $s[$i];
        }
    }

    if (strlen($str) %2 == 1) {
        $str = substr($str, 0, -1);
    }

    echo $n - strlen($str)."\n";
    echo $str;

?>

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

    $str = $s[0];
    for ($i = 1; $i < $n; $i++) {
        if (strlen($str)%2 != 0 ) {
            if ($str[strlen($str) - 1] != $s[$i])
             $str .= $s[$i];
        } else {
            $str .= $s[$i];
        }

    }
    if (strlen($str) == 1) {
        $str = '';

    }

    if (strlen($str) %2 != 0) {
        $str = substr($str, 0, -1);
    }

    echo $n - strlen($str)."\n";
    echo $str;

?>

<?php
    $k = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a = str_split($s);
    $i = 0;
    $l = '';
    foreach ($a as $s => $v) {
        $i++;
        if ($i%2) {
            $v1 = $v;
        } else {
            if ($v == $v1) {
                $i = 1;
            } else {
                $l .= $v1.$v;
            }
        }
    }
    if (strlen($l)%2) {
        $l = substr($l, strlen($l)-1);
    }
    echo $k - strlen($l)."\n";
    echo $l;
?>

<?php
    $k = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a = str_split($s);
    $m = func($a);
    echo $k - count($m)."\n";
    echo implode("", $m);

    function func($a) {
        $i = 0;
        $b = array();
        foreach ($a as $s => $v) {
            $i++;
            if ($i%2) {
                $t = $v;
                $c = $s;
            } else {
                if ($v == $t) {
                    unset($a[$s]);
                    $i = 1;
                } else {
                    $b[] = $t;
                    $b[] = $v;
                    unset($a[$c]);
                    unset($a[$s]);
                }
            }
        }
        $a = array_merge($b, $a);
        if (count($a) % 2 != 0) {
            $a = array_values($a);
            unset($a[count($a) -1]);
        }
        return $a;
    }
?>

<?php
    $n = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $str = '';
    for ($i = 1; $i < $n; $i++) {
        if (strlen($str)%2 == 0  || $str[strlen($str) - 1] != $s[$i]) {
             $str .= $s[$i];
        }
    }

    if (strlen($str) %2 == 1) {
        $str = substr($str, 0, -1);
    }

    echo $n - strlen($str)."\n";
    echo $str;

?>

<?php
    $k = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a = str_split($s);
    $i = 0;
    $l = '';
    foreach ($a as $s => $v) {
        $i++;
        if ($i%2) {
            $v1 = $v;
        } else {
            if ($v == $t) {
                $i = 1;
            } else {
                $l .= $v1.$v;
            }
        }
    }
    if (strlen($l)%2) {
        $l = substr($l, strlen($l)-1);
    }
    echo $k - strlen($l)."\n";
    echo $l;
?>


Vasya has a beautiful garden where wonderful fruit trees grow and yield fantastic harvest every year. But lately thieves started to sneak into the garden at nights and steal the fruit too often. Vasya can’t spend the nights in the garden and guard the fruit because there’s no house in the garden! Vasya had been saving in for some time and finally he decided to build the house. The rest is simple: he should choose in which part of the garden to build the house. In the evening he sat at his table and drew the garden’s plan. On the plan the garden is represented as a rectangular checkered field n × m in size divided into squares whose side length is 1. In some squares Vasya marked the trees growing there (one shouldn’t plant the trees too close to each other that’s why one square contains no more than one tree). Vasya wants to find a rectangular land lot a × b squares in size to build a house on, at that the land lot border should go along the lines of the grid that separates the squares. All the trees that grow on the building lot will have to be chopped off. Vasya loves his garden very much, so help him choose the building land lot location so that the number of chopped trees would be as little as possible.

The first line contains two integers n and m (1 ≤ n, m ≤ 50) which represent the garden location. The next n lines contain m numbers 0 or 1, which describe the garden on the scheme. The zero means that a tree doesn’t grow on this square and the 1 means that there is a growing tree. The last line contains two integers a and b (1 ≤ a, b ≤ 50). Note that Vasya can choose for building an a × b rectangle as well a b × a one, i.e. the side of the lot with the length of a can be located as parallel to the garden side with the length of n, as well as parallel to the garden side with the length of m.

Print the minimum number of trees that needs to be chopped off to select a land lot a × b in size to build a house on. It is guaranteed that at least one lot location can always be found, i. e. either a ≤ n and b ≤ m, or a ≤ m и b ≤ n.

NoteIn the second example the upper left square is (1,1) and the lower right is (3,2).


Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".The field for the new game is a rectangle table of size n × m. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:  some dwarf in one of the chosen lines is located in the rightmost cell of his row;  some dwarf in the chosen lines is located in the cell with the candy. The point of the game is to transport all the dwarves to the candy cells.Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game.

The first line of the input contains two integers n and m (1 ≤ n ≤ 1000; 2 ≤ m ≤ 1000).  Next n lines each contain m characters — the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S".

In a single line print a single integer — either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field.

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

<?php

list($lines, $chars) = explode(" ", trim(fgets(STDIN)));

$ms = array();
for($i = 0; $i < $lines; $i++)
{
	$row = str_split(trim(fgets(STDIN)));

	$row = array_flip($row);
	if($row['G'] < $row['S'])
		$ms[] = $row['G'] - $row['S'];
	else {
		echo "-1" . PHP_EOL;
		exit;
	}
}

$ms = array_unique($ms);
echo sizeof($ms) . PHP_EOL;

<?php

list($lines, $chars) = explode(" ", trim(fgets(STDIN)));

$m = $chars + 1;
for($i = 0; $i < $lines; $i++)
{
	$row = str_split(trim(fgets(STDIN)));

	$gPos = -1;
	$cPos = -1;
	for($j = 0; $j < ($chars / 2); $j++)
	{
		$gPos = ($row[$j] == 'G') ? $j : $gPos;
		$cPos = ($row[($chars - $j - 1)] == 'S') ? $chars - $j - 1 : $cPos;
	}

	$m = ($gPos < $cPos) ? min($m, $cPos - $gPos) : $m;
}

$m = ($m > $chars) ? -1 : $m;

echo $m . PHP_EOL;


Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection. Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 ≤ i ≤ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 ≤ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.

The first line of the input contains two space-separated integers n and k (1 ≤ n ≤ 2·k ≤ 100 000), denoting the number of cowbells and the number of boxes, respectively. The next line contains n space-separated integers s1, s2, ..., sn (1 ≤ s1 ≤ s2 ≤ ... ≤ sn ≤ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.

Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.

NoteIn the first sample, Kevin must pack his two cowbells into the same box. In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.In the third sample, the optimal solution is {3, 5} and {7}.

<?php
function calc($size, $repo) {
    $i = 0; $j = count($repo) - 1;
    $bucket = 0;
    while ($i < $j) {
        if ($repo[$j] + $repo[$i] <= $size) {
            $i ++;
        }
        $bucket ++;
        $j --;
    }
    if ($i == $j) $bucket ++;
    return $bucket;
}

fscanf(STDIN, "%d %d", $n, $k);
$temp = fgets(STDIN);
$repo = explode(" ", $temp);
$len = count($repo);
$left = $repo[$len - 1];
$right = 2 * $left;
while($left < $right) {
    $mid = intval(($left + $right) / 2);
    if (calc($mid, $repo) <= $k) {
        $right = $mid;
    } else {
        $left = $mid + 1;
    }
}
echo intval($left), "\n";
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = array_sum($c) / $b;
if($d < max($c))
{
    print max($c);
}
else
{
    print ceil($d);
}
?>


Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least ai any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.

The first line contains number n (1 ≤ n ≤ 1000). The second line contains n non-negative integers a1, a2, ..., an (0 ≤ ai < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.

Print a single number — the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.

NoteIn the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.

<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
$n = trim(fgets($file));
$aArray = explode(" ", trim(fgets($file)));
$vArray = array();
$t = 0;
$result = 0;
while ($t<$n) {
    for ($i = 0; $i < $n; $i++) {
        if ($aArray[$i]>$t || isset($vArray[$i])) {
            continue;
        }
        $vArray[$i] = 1;
        $t++;
    }
    $result++;
    if ($t == $n) {
        break;
    }
    for ($i = $n-1; $i >= 0; $i--) {
        if ($aArray[$i]>$t || isset($vArray[$i])) {
            continue;
        }
        $vArray[$i] = 1;
        $t++;
    }
    $result++;
}
echo $result-1 . PHP_EOL;


Little Petya loves inequations. Help him find n positive integers a1, a2, ..., an, such that the following two conditions are satisfied: a12 + a22 + ... + an2 ≥ x a1 + a2 + ... + an ≤ y

The first line contains three space-separated integers n, x and y (1 ≤ n ≤ 105, 1 ≤ x ≤ 1012, 1 ≤ y ≤ 106). Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator.

Print n positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number "-1". If there are several solutions, print any of them.

<?php

for(;;){
  $input = fgets(STDIN,4096);
  if($input === false) break;

  list($n, $x, $y) = explode(" ",$input);
  $y = trim($y);

  if( bccomp($n,$y) === 1 ){
    echo "-1\n";
    continue;
  }

  $mul = bcadd(bcadd($y, "-$n", 0), "1", 0);
  $sum = bcadd($n, "-1", 0);
  $sum = bcadd($sum, bcmul($mul, $mul, 0), 0);

  if( bccomp($sum,$x) === -1){
    echo "-1\n";
    continue;
  }

  echo $mul."\n";
  for($i=1;bccomp("$i",$n,0)===-1;$i++) echo "1\n";
}

?>


Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres.In the bookshop, Jack decides to buy two books of different genres.Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.The books are given by indices of their genres. The genres are numbered from 1 to m.

The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10) — the number of books in the bookstore and the number of genres. The second line contains a sequence a1, a2, ..., an, where ai (1 ≤ ai ≤ m) equals the genre of the i-th book. It is guaranteed that for each genre there is at least one book of that genre.

Print the only integer — the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2·109.

NoteThe answer to the first test sample equals 5 as Sasha can choose:  the first and second books,  the first and third books,  the first and fourth books,  the second and third books,  the third and fourth books.

<?php
list($n, $m) = explode(" ", fgets(STDIN));
$arr = explode(" ", fgets(STDIN));
$marr = array();
foreach($arr as $k){
    $marr[(int)$k] = isset($marr[(int)$k]) ? $marr[(int)$k] + 1 : 1;
}
$marr = array_values($marr);
$sum = 0;
foreach($marr as $mid => $mc){
    $sum+= array_sum(array_values(array_slice($marr, $mid + 1))) * $mc;
}
fprintf(STDOUT, '%d', $sum);
?>

<?php
list($n, $m) = explode(" ", fgets(STDIN));
$arr = explode(" ", fgets(STDIN));
$marr = array();
foreach($arr as $k){
    $marr[(int)$k] = isset($marr[(int)$k]) ? $marr[(int)$k] + 1 : 1;
}
$sum = 0;
foreach($marr as $mid => $mc){
    $sum+= ($n - $mc);
}
fprintf(STDOUT, '%d', $sum);
?>


As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it...

The first and only line of input contains a single nonempty string s of length at most 1000 composed of lowercase letters (a-z).

Output YES if the string s contains heidi as a subsequence and NO otherwise.

NoteA string s contains another string p as a subsequence if it is possible to delete some characters from s and obtain p.

    <?php
    $str=trim(fgets(STDIN));
    $word="heidi";
    $cnt=0;
    for ($i=0; $i <strlen($str) ; $i++)
       {
       	   if($str[$i]==$word[$cnt])
               $cnt++;

       }
       echo $cnt==5?"YES":"NO";

<?php
$str=trim(fgets(STDIN));
$word="heidi";
$cnt=0;
for ($i=0; $i <strlen($str) ; $i++)
   {
   	    echo "cnt=$cnt\n";
   	   if($str[$i]==$word[$cnt])
   	   {

           $cnt++;
        }

   }
   echo $cnt==5?"YES":"NO";

<?php
$str=trim(fgets(STDIN));
$word="heidi";
for ($i=0; $i <strlen($word) ; $i++)
{
	 for ($j=0; $j <strlen($str) ; $j++)
	 {
	 	if($i==4)
	 	{
			 	if($word[$i]==$str[$j])
			 	{
			 		$store[$i]=$j;
			 		//break;
			 	}
	    }
		else
	    {
	    	if($word[$i]==$str[$j])
		     {
					$store[$i]=$j;
					break;
		     }
	    }

	 }
}
$flag=0;
for ($i=0; $i <count($store)-1 ; $i++)
{
	if($store[$i]>$store[$i+1])
	    {
	     $flag=1;
	     echo "NO";
	    }
}
if($flag==0)
echo "YES";


It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are $$$n$$$ displays placed along a road, and the $$$i$$$-th of them can display a text with font size $$$s_i$$$ only. Maria Stepanovna wants to rent such three displays with indices $$$i < j < k$$$ that the font size increases if you move along the road in a particular direction. Namely, the condition $$$s_i < s_j < s_k$$$ should be held.The rent cost is for the $$$i$$$-th display is $$$c_i$$$. Please determine the smallest cost Maria Stepanovna should pay.

The first line contains a single integer $$$n$$$ ($$$3 \le n \le 3\,000$$$) — the number of displays. The second line contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ ($$$1 \le s_i \le 10^9$$$) — the font sizes on the displays in the order they stand along the road. The third line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 10^8$$$) — the rent costs for each display.

If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $$$i < j < k$$$ such that $$$s_i < s_j < s_k$$$.

NoteIn the first example you can, for example, choose displays $$$1$$$, $$$4$$$ and $$$5$$$, because $$$s_1 < s_4 < s_5$$$ ($$$2 < 4 < 10$$$), and the rent cost is $$$40 + 10 + 40 = 90$$$.In the second example you can't select a valid triple of indices, so the answer is -1.


A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color ci.The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children.The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.

The first line contains two integers n and m — the number of the children and the number of possible mitten colors (1 ≤ n ≤ 5000, 1 ≤ m ≤ 100). The second line contains n integers c1, c2, ... cn, where ci is the color of the mittens of the i-th child (1 ≤ ci ≤ m).

In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them.

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$diff = 0;
$res = array();
$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$diff += $q_same*$q1;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		$resx = array();
		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			if ($resx[$x1.' '.$x2]) {
				$res[] = $x2.' '.$x1;
				$resx[$x2.' '.$x1] = 1;
			} else {
				$res[] = $x1.' '.$x2;
				$resx[$x1.' '.$x2] = 1;
			}

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

//--------------------
echo implode(L,$res);

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$diff = 0;
$res = array();
$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		$resx = array();
		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			if ($resx[$x1.' '.$x2] > $resx[$x2.' '.$x1]) {
				$res[] = $x2.' '.$x1;
				$resx[$x2.' '.$x1]++;
			} else {
				$res[] = $x1.' '.$x2;
				$resx[$x1.' '.$x2]++;
			}

			$diff++;

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) && $q1 > $q2 ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

echo implode(L,$res);

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$diff = 0;
$res = array();
$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$diff += $q_same*$q1;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			$res[] = $x1.' '.$x2;

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

//--------------------
echo implode(L,$res);

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$diff = 0;
$res = array();
$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		$resx = array();
		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			if ($resx[$x1.' '.$x2]) {
				$res[] = $x2.' '.$x1;
				$resx[$x2.' '.$x1] = 1;
			} else {
				$res[] = $x1.' '.$x2;
				$resx[$x1.' '.$x2] = 1;
			}

			$diff++;

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) && $q1 > $q2 ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

//--------------------
echo implode(L,$res);

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$diff = 0;
$res = array();
$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		$resx = array();
		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			if ($resx[$x1.' '.$x2] > $resx[$x2.' '.$x1]) {
				$res[] = $x2.' '.$x1;
				$resx[$x2.' '.$x1]++;
			} else {
				$res[] = $x1.' '.$x2;
				$resx[$x1.' '.$x2]++;
			}

			$diff++;

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) && $q1 > $q2 ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

echo implode(L,$result);

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$res = array();

$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$diff += $q_same*$q1;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			$res[] = $x1.' '.$x2;

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

//--------------------
echo implode(L,$res);

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

list($kids,$colors) = explode(' ',$f[0]);

if ($kids == 1) {
	echo '0'.L.'1 1';
	exit;
}

$data = explode(' ', $f[1]);

$distr = array();

for($i=1;$i<=$colors;$i++) {
	$distr[$i] = sizeof(array_keys($data,$i));
}

arsort($distr);

$res = array();

$is = true;

while (($q1 = reset($distr)) && $is) {
	$is = false;

	$c1 = key($distr);

	$same = array_keys($distr,$q1);

	$q_same = sizeof($same);

	if ($q_same > 1) {
		$is = true;

		$diff += $q_same*$q1;

		$xame = array();

		foreach ($same as $u) {
			$xame[$u] = $q1 * 2;
			unset($distr[$u]);
		}

		while($qx1 = reset($xame)) {
			$x1 = key($xame);
			$qx2 = next($xame);
			$x2 = key($xame);

			$res[] = $x1.' '.$x2;

			if ($qx1==1) {
				unset($xame[$x1]);
			} else {
				$xame[$x1]--;
			}

			if ($qx2==1) {
				unset($xame[$x2]);
			} else {
				$xame[$x2]--;
			}

			if ($xame) {
				arsort($xame);
			}
		}

	} else {
		while ( $q1 && ($q2 = next($distr)) ) {
			$is = true;
			$c2 = key($distr);

			$max = $q2;
			$q1 -= $q2;
			$q2 = 0;

			for ($z=0;$z<$max;$z++) {
				$res[] = $c1.' '.$c2;
				$res[] = $c2.' '.$c1;
				$diff += 2;
			}

			if ($q2)
				$distr[$c2] = $q2;
			else
				unset($distr[$c2]);
		}

		if ($q1)
			$distr[$c1] = $q1;
		else
			unset($distr[$c1]);

	}

	arsort($distr);
}

for ($z=0;$z<$q1;$z++) {
	$res[] = $c1.' '.$c1;
	$res[] = $c1.' '.$c1;
}

array_unshift($res, $diff);

//--------------------
echo implode(L,$res);


In this problem, your task is to use ASCII graphics to paint a cardiogram. A cardiogram is a polyline with the following corners:That is, a cardiogram is fully defined by a sequence of positive integers a1, a2, ..., an.Your task is to paint a cardiogram by given sequence ai.

The first line contains integer n (2 ≤ n ≤ 1000). The next line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 1000). It is guaranteed that the sum of all ai doesn't exceed 1000.

Print max |yi - yj| lines (where yk is the y coordinate of the k-th point of the polyline), in each line print  characters. Each character must equal either « / » (slash), « \ » (backslash), « » (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram. Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty.

NoteDue to the technical reasons the answers for the samples cannot be copied from the statement. We've attached two text documents with the answers below.http://assets.codeforces.com/rounds/435/1.txthttp://assets.codeforces.com/rounds/435/2.txt