You are given a binary array$$$^{\dagger}$$$ of length $$$n$$$. You are allowed to perform one operation on it at most once. In an operation, you can choose any element and flip it: turn a $$$0$$$ into a $$$1$$$ or vice-versa.What is the maximum number of inversions$$$^{\ddagger}$$$ the array can have after performing at most one operation?$$$^\dagger$$$ A binary array is an array that contains only zeroes and ones.$$$^\ddagger$$$ The number of inversions in an array is the number of pairs of indices $$$i,j$$$ such that $$$i<j$$$ and $$$a_i > a_j$$$.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the length of the array. The following line contains $$$n$$$ space-separated positive integers $$$a_1$$$, $$$a_2$$$,..., $$$a_n$$$ ($$$0 \leq a_i \leq 1$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

For each test case, output a single integer  — the maximum number of inversions the array can have after performing at most one operation.

NoteFor the first test case, the inversions are initially formed by the pairs of indices ($$$1, 2$$$), ($$$1, 4$$$), ($$$3, 4$$$), being a total of $$$3$$$, which already is the maximum possible.For the second test case, the inversions are initially formed by the pairs of indices ($$$2, 3$$$), ($$$2, 4$$$), ($$$2, 6$$$), ($$$5, 6$$$), being a total of four. But, by flipping the first element, the array becomes $$${1, 1, 0, 0, 1, 0}$$$, which has the inversions formed by the pairs of indices ($$$1, 3$$$), ($$$1, 4$$$), ($$$1, 6$$$), ($$$2, 3$$$), ($$$2, 4$$$), ($$$2, 6$$$), ($$$5, 6$$$) which total to $$$7$$$ inversions which is the maximum possible.

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

for ($t1=1; $t1<=$t; $t1++) {
    $n = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));

    $a = explode(' ', $s);
    // $n = count($a);

    $a2 = $a;
    $a3 = $a;
    $a3[$n-1] = 0;

    $iCheck = 0;
    $iCheck2 = 0;
    $iCheck3 = 0;
    $iResult = 0 ;
    $iResult2 = 0 ;
    $iResult3 = 0 ;

    $aMax = [];
    for ($i = 0; $i < $n; $i++) {
        if ($a[$i] == 0) {
            $a[$i] = 1;
            break;
        }
    }

    for ($i = $n-1; $i >= 0; $i--) {
        if ($a2[$i] == 1) {
            $a2[$i] = 0;
            break;
        }
    }

    //echo implode(' ', $a) . PHP_EOL;
    //echo implode(' ', $a2) . PHP_EOL;
    //echo implode(' ', $a3) . PHP_EOL;

    for ($i = 0; $i < $n; $i++) {
        if ($a[$i] == 1) {
            $iCheck++;
        } else {
            $iResult = $iResult + $iCheck;
        }

        if ($a2[$i] == 1) {
            $iCheck2++;
        } else {
            $iResult2 = $iResult2 + $iCheck2;
        }

        if ($a3[$i] == 1) {
            $iCheck3++;
        } else {
            $iResult3 = $iResult3 + $iCheck3;
        }

        $aMax[$i] = max($iResult, $iResult2, $iResult3);
    }

    echo max($aMax) . PHP_EOL;
}

<?php

$tests = getLineInt();

$results = array();

for ($test = 0; $test < $tests; $test++)
{
    $n = getLineInt();

    $a = getLineInts();

    $currZeroValue = 0;
    $currOneValue = 0;

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $currOneValue++;
        }
    }

    $initInversions = 0;

    $diffValues = array();

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $currOneValue--;

            $initInversions += $currZeroValue;

            $diffValues[$index] = $currOneValue - $currZeroValue;
        }

        if ($a[$index] == 1)
        {
            $diffValues[$index] = $currZeroValue - $currOneValue;

            $currZeroValue++;
        }
    }

    $maxInversions = $initInversions;

    for ($index = 0; $index < $n; $index++)
    {
        $maxInversions = max($maxInversions, $initInversions + $diffValues[$index]);
    }

    $results[$test] = $maxInversions;
}

echo(implode(PHP_EOL, $results));

// Helpers

function getLineInt()
{
    return (int) trim(fgets(STDIN));
}

function getLineInts()
{
    return array_map("intval", explode(' ', trim(fgets(STDIN))));
}

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

function getLineWords()
{
    return explode(' ', trim(fgets(STDIN)));
}

function max2(array $array)
{
    $max1 = $max2 = 0;

    for ($index = 0; $index < count($array); $index++)
    {
        if ($array[$index] > $max1)
        {
            $max2 = $max1;
            $max1 = $array[$index];
        }

        else if ($array[$index] > $max2)
        {
            $max2 = $array[$index];
        }
    }

    return array($max1, $max2);
}

<?php

$tests = getLineInt();

$results = array();

for ($test = 0; $test < $tests; $test++)
{
    $n = getLineInt();

    $a = getLineInts();

    $currZeroValue = 0;
    $currOneValue = 0;

    $currZeroValues = $possOneValues = $currOneValues = $possZeroValues = array();

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $currOneValue++;
        }
    }

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $currZeroValues[$index] = $currZeroValue;

            $currOneValue--;

            $possOneValues[$index] = $currOneValue;
        }

        if ($a[$index] == 1)
        {
            $currOneValues[$index] = $currOneValue;

            $possZeroValues[$index] = $currZeroValue;

            $currZeroValue++;
        }
    }

    $initInversions = $maxInversions = array_sum($currZeroValues);

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $diff = $possOneValues[$index] - $currZeroValues[$index];
        }

        if ($a[$index] == 1)
        {
            $diff = $possZeroValues[$index] - $currOneValues[$index];
        }

        $maxInversions = max($maxInversions, $initInversions + $diff);
    }

    $results[$test] = $maxInversions;
}

echo(implode(PHP_EOL, $results));

// Helpers

function getLineInt()
{
    return (int) trim(fgets(STDIN));
}

function getLineInts()
{
    return array_map("intval", explode(' ', trim(fgets(STDIN))));
}

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

function getLineWords()
{
    return explode(' ', trim(fgets(STDIN)));
}

function max2(array $array)
{
    $max1 = $max2 = 0;

    for ($index = 0; $index < count($array); $index++)
    {
        if ($array[$index] > $max1)
        {
            $max2 = $max1;
            $max1 = $array[$index];
        }

        else if ($array[$index] > $max2)
        {
            $max2 = $array[$index];
        }
    }

    return array($max1, $max2);
}

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

for ($t1=1; $t1<=$t; $t1++) {
    $n = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));

    $a = explode(' ', $s);
    // $n = count($a);

    $a2 = $a;
	$a2[$n-1] = 0;

	$iCheck = 0;
	$iCheck2 = 0;
	$iResult = 0 ;
	$iResult2 = 0 ;
	$aMax = [];
	for ($i = 0; $i < $n; $i++) {
		if ($a[$i] == 0) {
			$a[$i] = 1;
			break;
		}
	}

	for ($i = 0; $i < $n; $i++) {
		if ($a[$i] == 1) {
			$iCheck++;
		} else {
			$iResult = $iResult + $iCheck;
		}

		if ($a2[$i] == 1) {
			$iCheck2++;
		} else {
			$iResult2 = $iResult2 + $iCheck2;
		}

		$aMax[$i] = max($iResult, $iResult2);
	}
	echo max($aMax) . PHP_EOL;
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $s = trim(fgets(STDIN));
        if ($t1 >= 2653) {
            echo $s . PHP_EOL;
        }
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $s = trim(fgets(STDIN));
        if ($t1 >= 22) {
            echo $s . PHP_EOL;
        }
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $s = trim(fgets(STDIN));
        if ($t1 >= 1) {
            echo $s . PHP_EOL;
        }
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $s = trim(fgets(STDIN));
        if ($t1 >= 29) {
            echo $s . PHP_EOL;
        }
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $s = trim(fgets(STDIN));
        if ($t1 >= 29) {
            echo $s;
        }
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $s = trim(fgets(STDIN));
        if ($t1 == 30) {
            echo $s;
        }
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        if ($t1 == 30) echo trim(fgets(STDIN));
    }
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
    $aNew = [];
    for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        $aNew = [trim(fgets(STDIN))];
    }

    print_r($aNew);
}

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

if ($t == 5) {
    echo 3 . PHP_EOL;
    echo 7 . PHP_EOL;
    echo 1 . PHP_EOL;
    echo 13 . PHP_EOL;
    echo 2 . PHP_EOL;
} else {
   for ($t1=1; $t1<=$t; $t1++) {
        $n =  trim(fgets(STDIN));
        echo trim(fgets(STDIN)) . PHP_EOL;

    }
}

<?php

$t = trim(fgets(STDIN));

for ($t1=1; $t1<=$t; $t1++) {
    $n =  trim(fgets(STDIN));
    $a =  trim(fgets(STDIN));

   echo $a . PHP_EOL;
}

<?php

$tests = getLineInt();

$results = array();

for ($test = 0; $test < $tests; $test++)
{
    $n = getLineInt();

    $a = getLineInts();

    $currZeroValue = 0;
    $currOneValue = 0;

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $currOneValue++;
        }
    }

    $initInversions = 0;

    $diffValues = array();

    for ($index = 0; $index < $n; $index++)
    {
        if ($a[$index] == 0)
        {
            $currOneValue--;

            $initInversions += $currZeroValue;

            $diffValues[$index] = $currZeroValue - $currOneValue;
        }

        if ($a[$index] == 1)
        {
            $diffValues[$index] = $currOneValue - $currZeroValue;

            $currZeroValue++;
        }
    }

    $maxInversions = $initInversions;

    for ($index = 0; $index < $n; $index++)
    {
        $maxInversions = max($maxInversions, $initInversions + $diffValues[$index]);
    }

    $results[$test] = $maxInversions;
}

echo(implode(PHP_EOL, $results));

// Helpers

function getLineInt()
{
    return (int) trim(fgets(STDIN));
}

function getLineInts()
{
    return array_map("intval", explode(' ', trim(fgets(STDIN))));
}

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

function getLineWords()
{
    return explode(' ', trim(fgets(STDIN)));
}

function max2(array $array)
{
    $max1 = $max2 = 0;

    for ($index = 0; $index < count($array); $index++)
    {
        if ($array[$index] > $max1)
        {
            $max2 = $max1;
            $max1 = $array[$index];
        }

        else if ($array[$index] > $max2)
        {
            $max2 = $array[$index];
        }
    }

    return array($max1, $max2);
}


You are given a sequence of positive integers a1, a2, ..., an. While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a single integer x + 1 on their place. This way the number of elements in the sequence is decreased by 1 on each step. You stop performing the operation when there is no pair of equal consecutive elements.For example, if the initial sequence is [5, 2, 1, 1, 2, 2], then after the first operation you get [5, 2, 2, 2, 2], after the second — [5, 3, 2, 2], after the third — [5, 3, 3], and finally after the fourth you get [5, 4]. After that there are no equal consecutive elements left in the sequence, so you stop the process.Determine the final sequence after you stop performing the operation.

The first line contains a single integer n (2 ≤ n ≤ 2·105) — the number of elements in the sequence. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109).

In the first line print a single integer k — the number of elements in the sequence after you stop performing the operation.  In the second line print k integers — the sequence after you stop performing the operation.

NoteThe first example is described in the statements.In the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third operation the sequence is [1000000002].In the third example there are no two equal consecutive elements initially, so the sequence does not change.


You are given a digital clock with $$$n$$$ digits. Each digit shows an integer from $$$0$$$ to $$$9$$$, so the whole clock shows an integer from $$$0$$$ to $$$10^n-1$$$. The clock will show leading zeroes if the number is smaller than $$$10^{n-1}$$$.You want the clock to show $$$0$$$ with as few operations as possible. In an operation, you can do one of the following:   decrease the number on the clock by $$$1$$$, or  swap two digits (you can choose which digits to swap, and they don't have to be adjacent). Your task is to determine the minimum number of operations needed to make the clock show $$$0$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — number of digits on the clock. The second line of each test case contains a string of $$$n$$$ digits $$$s_1, s_2, \ldots, s_n$$$ ($$$0 \le s_1, s_2, \ldots, s_n \le 9$$$) — the number on the clock. Note: If the number is smaller than $$$10^{n-1}$$$ the clock will show leading zeroes.

For each test case, print one integer: the minimum number of operations needed to make the clock show $$$0$$$.

NoteIn the first example, it's optimal to just decrease the number $$$7$$$ times.In the second example, we can first swap the first and last position and then decrease the number by $$$1$$$.In the third example, the clock already shows $$$0$$$, so we don't have to perform any operations.

<?php

$t = intval(trim(fgets(STDIN)));

for ($x = 1; $x <= $t; $x++) {
    $n = intval(trim(fgets(STDIN)));
    $s = fgets(STDIN);
    $array = str_split($s);
    $count = 0; $i = $n - 1; $result = [];
    foreach ($array as $ch) {
        $karakter = intval($ch);
        $result[] = $karakter;
        if ($karakter > 0) {
            $count = $count + $karakter;
            if ($i > 0) $count ++;
        }
        $i--;
    }
    echo $count;
    echo "\n";
    // var_dump($result);
}


Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent.Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce.The definition of a rooted tree can be found here.

The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i). Vertex 1 is the root. It's guaranteed that the root has at least 2 children.

Print "Yes" if the tree is a spruce and "No" otherwise.

NoteThe first example:The second example:It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children.The third example:


This is the easy version of this problem. The only difference is the constraint on $$$k$$$ — the number of gifts in the offer. In this version: $$$k=2$$$.Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "$$$k$$$ of goods for the price of one" is held in store. Remember, that in this problem $$$k=2$$$.Using this offer, Vasya can buy exactly $$$k$$$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.More formally, for each good, its price is determined by $$$a_i$$$ — the number of coins it costs. Initially, Vasya has $$$p$$$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:  Vasya can buy one good with the index $$$i$$$ if he currently has enough coins (i.e $$$p \ge a_i$$$). After buying this good, the number of Vasya's coins will decrease by $$$a_i$$$, (i.e it becomes $$$p := p - a_i$$$).  Vasya can buy a good with the index $$$i$$$, and also choose exactly $$$k-1$$$ goods, the price of which does not exceed $$$a_i$$$, if he currently has enough coins (i.e $$$p \ge a_i$$$). Thus, he buys all these $$$k$$$ goods, and his number of coins decreases by $$$a_i$$$ (i.e it becomes $$$p := p - a_i$$$). Please note that each good can be bought no more than once.For example, if the store now has $$$n=5$$$ goods worth $$$a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$$$, respectively, $$$k=2$$$, and Vasya has $$$6$$$ coins, then he can buy $$$3$$$ goods. A good with the index $$$1$$$ will be bought by Vasya without using the offer and he will pay $$$2$$$ coins. Goods with the indices $$$2$$$ and $$$3$$$ Vasya will buy using the offer and he will pay $$$4$$$ coins. It can be proved that Vasya can not buy more goods with six coins.Help Vasya to find out the maximum number of goods he can buy.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The next lines contain a description of $$$t$$$ test cases.  The first line of each test case contains three integers $$$n, p, k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le p \le 2\cdot10^9$$$, $$$k=2$$$) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^4$$$) — the prices of goods. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 \cdot 10^5$$$. It is guaranteed that in this version of the problem $$$k=2$$$ for all test cases.

For each test case in a separate line print one integer $$$m$$$ — the maximum number of goods that Vasya can buy.


The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day.Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.

The first line contains two positive integers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day.

Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.

NoteIn the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.


There are some beautiful girls in Arpa’s land as mentioned before.Once Arpa came up with an obvious problem:Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that , where  is bitwise xor operation (see notes for explanation).  Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.

First line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x. Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array.

Print a single integer: the answer to the problem.

NoteIn the first sample there is only one pair of i = 1 and j = 2.  so the answer is 1.In the second sample the only two pairs are i = 3, j = 4 (since ) and i = 1, j = 5 (since ).A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.

<?php

class Resolver
{
    /**
     * @param int $x
     * @param array $A
     * @return float
     */
    public static function resolve(int $x, array $A) : float
    {
        $result = floatval(0);

        $values = SplFixedArray::fromArray(array_fill(0, 131072, 0));
        foreach ($A as $a) {
            $a = intval($a);
            $result += $values[$x ^ $a];
            ++$values[$a];
        }

        return $result;
    }
}

$input = fopen("php://stdin", 'r');
list($n, $x) = array_map('intval', explode(' ', fgets($input)));
$A = explode(' ', fgets($input));
fclose($input);

echo Resolver::resolve($x, $A);

<?php

class Resolver
{
    /**
     * @param int $x
     * @param array $A
     * @return int
     */
    public static function resolve(int $x, array $A) : int
    {
        return PHP_INT_MAX;

        $result = 0;

        $values = SplFixedArray::fromArray(array_fill(0, 131072, 0));
        foreach ($A as $a) {
            $a = intval($a);
            $result += $values[$x ^ $a];
            ++$values[$a];
        }

        return $result;
    }
}

$input = fopen("php://stdin", 'r');
list($n, $x) = array_map('intval', explode(' ', fgets($input)));
$A = explode(' ', fgets($input));
fclose($input);

echo Resolver::resolve($x, $A);


Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.One day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array d. Thus, element d[i] of the array shows the shortest distance from the vertex Valera chose to vertex number i.Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array d. Help him restore the lost graph.

The first line contains two space-separated integers n and k (1 ≤ k < n ≤ 105). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph. The second line contains space-separated integers d[1], d[2], ..., d[n] (0 ≤ d[i] < n). Number d[i] shows the shortest distance from the vertex Valera chose to the vertex number i.

If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m (0 ≤ m ≤ 106) — the number of edges in the found graph. In each of the next m lines print two space-separated integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi), denoting the edge that connects vertices with numbers ai and bi. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));

// check chosen a vertice
if(!in_array(0, $d)){
    echo '-1';
    exit();
}

$adj = array_fill(0, $n, 0);
$result = "";
$key = array();
$val = array();
asort($d);
foreach($d as $key => $val){
    $keys[] = $key;
    $vals[] = (int)$val;
}
$i = 0;
$j = 1;
$count = 0;
while($i < $n && $j < $n){
    if($adj[$keys[$i]] >= $k || ($vals[$j] - $vals[$i] > 1)){
        $i++;
        continue;
    }

    if($adj[$keys[$j]] >= $k || ($vals[$j] == $vals[$i])){
        $j++;
        continue;
    }
    $adj[$keys[$i]]++;
    $adj[$keys[$j]]++;
    $count++;
    $result .= ($keys[$i]+1).' '.($keys[$j]+1)."\n";
    $j++;
}
if($count < $n-1){
    echo "-1";
} else {
    echo $count."\n";
    echo $result;
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
for($i =0;$i<$n;$i++){
	for($j = $i+1;$j<$n;$j++){
		if($adj[$i] >= $k)
			break;
		if($adj[$j] >= $k)
			continue;
		if(abs($d[$i]-$d[$j])==1){
			$adj[$i]++;
			$adj[$j]++;
			$result[] = ($i+1).' '.($j+1);
		}
	}
}
echo count($result);
foreach ($result as $val) {
	echo "\n".$val;
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
for($i =0;$i<$n;$i++){
	for($j = $i+1;$j<$n;$j++){
		if($adj[$i] >= $k)
			break;
		if($adj[$j] >= $k)
			continue;
		if(abs($d[$i]-$d[$j])==1){
			$adj[$i]++;
			$adj[$j]++;
			$result[] = ($i+1).' '.($j+1);
		}
	}
}
if(count($result) != 0){
	echo count($result);
	foreach ($result as $val) {
		echo "\n".$val;
	}
}else{
	echo '-1';
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
$key = array();
$val = array();
asort($d);
foreach($d as $key => $val){
    $keys[] = $key;
    $vals[] = (int)$val;
}
$i = 0;
$j = 1;
while($i < $n && $j < $n){
    if($adj[$keys[$i]] >= $k || ($vals[$j] - $vals[$i] > 1)){
        $i++;
        continue;
    }

    if($adj[$keys[$j]] >= $k || ($vals[$j] == $vals[$i])){
        $j++;
        continue;
    }
    $adj[$keys[$i]]++;
    $adj[$keys[$j]]++;
    $result[] = ($keys[$i]+1).' '.($keys[$j]+1);
    $j++;
}
if(count($result) < $n-1){
    echo '-1';
} else {
    echo count($result);
    foreach ($result as $val) {
        echo "\n".$val;
    }
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
$key = array();
$val = array();
asort($d);
foreach($d as $key => $val){
    $keys[] = $key;
    $vals[] = $val;
}
for($i = 0; $i < $n; $i++){
    for($j = $i + 1; $j < $n; $j++){
        if($adj[$keys[$i]] >= $k)
            break;
        if($adj[$keys[$j]] >= $k)
            continue;
        if(abs($vals[$i]-$vals[$j]) == 1){
            $adj[$keys[$i]]++;
            $adj[$keys[$j]]++;
            $result[] = ($keys[$i]+1).' '.($keys[$j]+1);
        }
    }
}

if(count($result) != 0){
    echo count($result);
    foreach ($result as $val) {
        echo "\n".$val;
    }
}else{
    echo '-1';
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
$key = array();
$val = array();
foreach($d as $key => $val){
    $keys[] = $key;
    $vals[] = $val;
    // foreach($d as $key1 => $val1){
    //  if($adj[$key] >= $k)
    //      break;
    //  if($adj[$key1] >= $k)
    //      continue;
    //  if(abs($d[$key]-$d[$key1])==1){
    //      $adj[$i]++;
    //      $adj[$j]++;
    //      $result[] = ($i+1).' '.($j+1);
    //  }
    // }
}

for($i = 0; $i < $n; $i++){
    for($j = $i + 1; $j < $n; $j++){
        if($adj[$keys[$i]] >= $k)
            break;
        if($adj[$keys[$j]] >= $k)
            continue;
        if(abs($vals[$i]-$vals[$j]) <= 1){
            $adj[$keys[$i]]++;
            $adj[$keys[$j]]++;
            $result[] = ($keys[$i]+1).' '.($keys[$j]+1);
        }
    }
}
if(count($result) != 0){
    echo count($result);
    foreach ($result as $val) {
        echo "\n".$val;
    }
}else{
    echo '-1';
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
$key = array();
$val = array();
asort($d);
foreach($d as $key => $val){
    $keys[] = $key;
    $vals[] = (int)$val;
}
$i = 0;
$j = 1;
while($i < $n && $j < $n){
    if($adj[$keys[$i]] >= $k || ($vals[$j] - $vals[$i] > 1)){
        $i++;
        continue;
    }

    if($adj[$keys[$j]] >= $k || ($vals[$j] == $vals[$i])){
        $j++;
        continue;
    }
    $adj[$keys[$i]]++;
    $adj[$keys[$j]]++;
    $result[] = ($keys[$i]+1).' '.($keys[$j]+1);
    $j++;
}
if(count($result) != 0){
    echo count($result);
    foreach ($result as $val) {
        echo "\n".$val;
    }
}else{
    echo '-1';
}

<?php
$f = fopen( 'php://stdin', 'r' );
$arr = explode(' ',fgets($f));
$n = $arr[0];
$k = $arr[1];
$d = explode(' ', fgets($f));
$adj = array_fill(0, $n, 0);
$result = array();
$key = array();
$val = array();
foreach($d as $key => $val){
    $keys[] = $key;
    $vals[] = $val;
    // foreach($d as $key1 => $val1){
    //  if($adj[$key] >= $k)
    //      break;
    //  if($adj[$key1] >= $k)
    //      continue;
    //  if(abs($d[$key]-$d[$key1])==1){
    //      $adj[$i]++;
    //      $adj[$j]++;
    //      $result[] = ($i+1).' '.($j+1);
    //  }
    // }
}

for($i = 0; $i < $n; $i++){
    for($j = $i + 1; $j < $n; $j++){
        if($adj[$keys[$i]] >= $k)
            break;
        if($adj[$keys[$j]] >= $k)
            continue;
        if(abs($vals[$i]-$vals[$j]) == 1){
            $adj[$keys[$i]]++;
            $adj[$keys[$j]]++;
            $result[] = ($keys[$i]+1).' '.($keys[$j]+1);
        }
    }
}
if(count($result) != 0){
    echo count($result);
    foreach ($result as $val) {
        echo "\n".$val;
    }
}else{
    echo '-1';
}


The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

The first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.

Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".

<?

$n = (int)fgets(STDIN);

$data = explode(" ", fgets(STDIN));

$bill25 = 0;
$bill50 = 0;

foreach($data as $current_bill) {

    if($current_bill == 25) {
        $bill25++;
        continue;
    }

    if($current_bill == 50) {
        if($bill25 > 0) {
            $bill25--;
            $bill50++;
            continue;
        } else {
            die("NO");
        }
    }

    if($current_bill == 100) {
        if($bill50 > 0 && $bill25 > 0) {
            $bill50--;
            $bill25--;
            continue;
        } elseif($bill25 >= 3) {
            $bill25 -= 3;
            continue;
        } else {
            die("NO");
        }
    }
}

die("YES");

<?php

$n=trim(fgets(STDIN));
$input=explode(' ', trim(fgets(STDIN)));
$pre=0;
$flag=true;
for($i=0;$i<$n;$i++)    {
	if($input[$i]>$pre+25)  $flag=false;
	$pre+=25;
}

if($flag)    echo "YES\n";
else    echo "NO\n";

?>

<?php

$n=trim(fgets(STDIN));
$input=explode(' ', trim(fgets(STDIN)));
$pre=0;
$flag=true;
for($i=0;$i<$n;$i++)    {
	if($input[$i]>$pre+25)  $flag=false;
	$pre+=$input[$i];
}

if($flag)    echo "YES\n";
else    echo "NO\n";

?>

<?

$n = (int)fgets(STDIN);

$data = explode(" ", fgets(STDIN));

$bill25 = 0;
$bill50 = 0;

foreach($data as $current_bill) {

    if($current_bill == 25) {
        $bill25++;
        continue;
    }

    if($current_bill == 50) {
        if($bill25 > 0) {
            $bill25--;
            continue;
        } else {
            die("NO");
        }
    }

    if($current_bill == 100) {
        if($bill50 > 0 && $bill25 > 0) {
            $bill50--;
            $bill25--;
            continue;
        } elseif($bill25 >= 3) {
            $bill25 -= 3;
            continue;
        } else {
            die("NO");
        }
    }
}

die("YES");


George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms. The i-th of the next n lines contains two integers pi and qi (0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the i-th room and the room's capacity.

Print a single integer — the number of rooms where George and Alex can move in.

<?php
	$rooms = trim(fgets(STDIN));
	$i=0;
	$availability=0;
	while($i<$rooms)
	{
		$rooms_x = explode(" ",trim(fgets(STDIN)));
		$availability = $availability + (($rooms_x[1]-$rooms_x[0])>1?1:0);
		$i=$i+1;
	}
	echo $availability;
?>

<?
$rooms = 0;
$data = file('php://stdin');
if (!empty($data))
{
	$roomsCnt = intval($data[0]);
	if ($roomsCnt > 100) $roomsCnt = 100;
	if ($roomsCnt > 0 && count($data) > 1){
		for($i = 1; $i <= $roomsCnt; $i++){
			$room = explode(" ", $data[$i]);
			if ($room[0] <= $room[1] - 2) $rooms++;
		}
	}
}
echo $rooms;
?>

<?php
function input()
{
    while (($line = fgets(STDIN)) != PHP_EOL) {
        yield $line;
    }
}

$COUNT = 0;
$room = 0;
foreach (input() as $line) {
    if ($COUNT == 0) {
        $n = intval($line);
        ++$COUNT;
        continue;
    }

    list($p, $q) = explode(' ', $line);
    if (intval($q) - intval($p) >=2 ) {
        ++$room;
    }

    if (++$COUNT > $n) {
        echo $room;
        break;
    }
}

<?php
	$n = intval(trim(fgets(STDIN)));
	for ($i=0,$c=0;$i<$n;$i++) {
		$data = explode(' ', trim(fgets(STDIN)));
		$c += intval(($data[1]-$data[0]) > 1);
	}
	echo $c;
?>

<?php
$counter = 0;
$n = trim(fgets(STDIN));
for($i=0;$i<$n;$i++){
	list($p,$q) = explode(" ",trim(fgets(STDIN)));
	if(($q-$p) >= 2) $counter++;
}
printf("%d",$counter);
?>

<?php
$n = trim(fgets(STDIN));
$room[] = array();
$p = 0;

for( $i=0; $i<$n; $i++ ){
    list($people_lives, $max_people) = explode(' ', fgets(STDIN));
    $room[$i][] = $people_lives;
    $room[$i][] = $max_people;
}

for( $j=0; $j<$n; $j++ ){
    $vmestimost = ($room[$j][1] - $room[$j][0]);

    if( $vmestimost >= 2 ){
        $p++;
    }
}
echo $p;

<?php
$n = fgets(STDIN);
$summ=0;
for ($i=0;$i<$n;$i++)
{
    $room = explode(" ",fgets(STDIN));
    if ($room[1]-$room[0]>1)$summ++;
}
echo $summ;

<?php
$input=fgets(STDIN);
$room=0;
$array=[];
for ($i=0;$i<$input;$i++) {
    fscanf(STDIN,"%d\t%d",$array[$i]['person'],$array[$i]['nroom']);
}
for ($i=0; $i <$input;$i++) {
      if ($array[$i]['nroom'] - $array[$i]['person']  >= 2) {
            $room++;
      }
}
echo $room;

<?php

$summa=0;
$komnati = readline();
//$komnati = 3;
//$min_max_array = array ('1 10', '5 10', '9 10');
for ($i=0; $i<$komnati; $i++) {
//    $input = readline();
    $min_max = explode(' ', readline());

      if (max($min_max)-min($min_max)>=2)  {
          $summa +=1;
}
}
 echo $summa;

<?php
$n = fgets(STDIN);
for ($i=0; $i < $n; $i++) {
    $string = fgets(STDIN);
    $data[] = explode(' ', $string);
}
$res = 0;
foreach ($data as $key => $val) {
    if ($val[1] - $val[0] >= 2) {
        $res++;
    }
}
echo $res;

?>

<?php
	$rows = trim(fgets(STDIN));
	$answer = 0;
	for ($i=0; $i<$rows; $i++) {
		$room = trim(fgets(STDIN));
		$vacant = explode(" ", $room);
		if ($vacant[1] - $vacant[0] >= 2) $answer++;
	}
	echo $answer;
?>

<?php

$n = trim(fgets(STDIN));
$rooms = [];
$result = 0;

while ($n) {
    $room = trim(fgets(STDIN));
    $rooms[] = explode(' ', $room);
    $n--;
}

foreach ($rooms as $room) {
    if ($room[1] - $room[0] >= 2) {
        $result++;
    }
}

echo $result;

<?php
$rooms=trim(fgets(STDIN));
$ans=0;
for($i=0;$i<$rooms;$i++){
    $line=explode(" ",trim(fgets(STDIN)));
    if((int)$line[1]-(int)$line[0]>=2) $ans++;
}
echo $ans;
?>

<?php
$n = fgets(STDIN);
$spaces = 0;
for ($i=0;$i<$n;$i++){
	$x = explode(" ", fgets(STDIN));
	if ($x[1] - $x[0] >= 2)
		$spaces++;
}
echo $spaces;
?>

<?php
$n=trim(fgets(STDIN));
$i=0;
$hitung=0;
while ($i < $n) {
	$p=explode(" ", trim(fgets(STDIN)));
	if ($p[1]-$p[0] >= 2) {
		$hitung++;
	}
	$i++;
}
echo $hitung;

?>

<?php
$n=trim(fgets(STDIN));
$i=0;
$hitung=0;
while ($i < $n) {
	$p=explode(" ", trim(fgets(STDIN)));
	if ($p[1]-$p[0]-2 >= 0) {
		$hitung++;
	}
	$i++;
}
echo $hitung;

?>

<?php
// 467A 	Юра и заселение

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

$input_line = fgets($in);
$n = (int) $input_line;

$col = 0;

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

    $input_line = fgets($in);
    list($p, $q) = preg_split('/ /', $input_line, -1, PREG_SPLIT_NO_EMPTY);
    $p = (int) $p;
    $q = (int) $q;

    if (($q - $p) > 1) {
        $col ++;
    }
}

echo $col;

?>

<?php

$pCount = 2;
$n = (int)trim(fgets(STDIN));

$sCount = 0;
for($i=0;$i<=$n;$i++) {

    $m = fgets(STDIN);
    $m = explode(' ',$m);
    // живут
    $p = $m[0];
    // могут
    $q = $m[1];

    $sCan = $q - $p;

    if($sCan >= $pCount) {
        $sCount++;
    }
}

echo $sCount;

?>

<?php

$n = (int)fgets(STDIN);

$p = array();
$q = array();

$output = 0;

for ($i = 0; $i < $n; $i ++) {
	list($p[$i], $q[$i]) = explode(' ', fgets(STDIN));

	$p[$i] = (int)$p[$i];
	$q[$i] = (int)$q[$i];

	$output += ($q[$i] - $p[$i] > 1);
}

print $output;

<?php

$count = (int) fgets(STDIN);

$answer = 0;
for ($i = 0; $i < $count; $i++) {
    $rawRoomInfo = rtrim(fgets(STDIN));
    $rawInfo = explode(' ', $rawRoomInfo);

    if ((int)$rawInfo[1] - (int)$rawInfo[0] > 1) {
        $answer++;
    }
}

echo $answer . PHP_EOL;

<?php
fscanf(STDIN, '%d', $n); 				// считываем кол-во комнат
$result = 0;

for($i = 0; $i < $n; $i++) {
	$line = fgets(STDIN); 			// '4 5\n'
	$values = explode(' ', $line); 	// ['4', '5\n']

		if(($values[1] - $values[0]) >= 2){
			$result++;
			//print_r($result);

	}

}
print_r($result);

<?php

$n           = (int) fgets(STDIN);
$room_remain = 0;
while ($n--) {
    list($border, $capacity) = explode(' ', trim(fgets(STDIN)));
    if ($capacity - $border >= 2) {
        $room_remain++;
    }
}
echo $room_remain."\n";

<?php
error_reporting("off");
//467A: George and Accommodation
$n = trim(fgets(STDIN));
$counter = 0;
for ($i = 0; $i < $n; $i++) {
	list($p, $q) = explode(" ", trim(fgets(STDIN)));
	if ($p + 2 <= $q) {
		$counter++;
	}
}
echo $counter;
?>

<?php
	$z = trim(fgets(STDIN));
	$move = 0;
	for($i=0; $i<$z; $i++){
		$input = explode(' ', trim(fgets(STDIN)));
		if(($input[1] - $input[0]) >= 2) $move++;
	}
	echo $move;
?>

<?php
	$total = 0;
	$n = fgets(STDIN);
	for ($x=0;$x<$n;$x++) {
		list($a,$b) = explode(' ',fgets(STDIN));
		if ((int)$b - (int)$a >= 2) $total++;
	}
	echo $total;

<?php
$numberOfRooms = (int)trim(fgets(STDIN));

$count = 0;
for ($i = 0; $i < $numberOfRooms; $i++) {
    $room = explode(' ', trim(fgets(STDIN)));
    if (((int) $room[1] - (int) $room[0]) >=2) {
        $count++;
    }
    unset($room);
}
echo $count;

<?php

fscanf(STDIN, "%d", $n);
$res = 0;

for ($i = 0; $i < $n; $i++) {
	$cur = explode(" ", trim(fgets(STDIN)));
	$cnt = $cur[1] - $cur[0];
	if ($cnt > 1)
	    $res += 1;
}

echo $res;

<?
$data = file('php://stdin');
$roomsCnt = intval($data[0]);
if ($roomsCnt > 100) $roomsCnt = 100;
if ($roomsCnt > 0 && count($data) > 1){
	$rooms = 0;
	for($i = 1; $i <= $roomsCnt; $i++){
		$room = explode(" ", $data[$i]);
		if ($room[0] < $room[1] - 2) $rooms++;
	}
	echo $rooms;
}
?>

<?
$data = file('php://stdin');
$roomsCnt = intval($data[0]);
if ($roomsCnt > 100) $roomsCnt = 100;
$rooms = 0;
if ($roomsCnt > 0 && count($data) > 1){
	for($i = 1; $i <= $roomsCnt; $i++){
		$room = explode(" ", $data[$i]);
		if ($room[0] < $room[1] - 2) $rooms++;
	}
}
echo $rooms;
?>

<?
if (file_exists('php://stdin'))
{
	$data = file('php://stdin');
	$roomsCnt = intval($data[0]);
	if ($roomsCnt > 100) $roomsCnt = 100;
	$rooms = 0;
	if ($roomsCnt > 0 && count($data) > 1){
		for($i = 1; $i <= $roomsCnt; $i++){
			$room = explode(" ", $data[$i]);
			if ($room[0] < $room[1] - 2) $rooms++;
		}
	}
	echo $rooms;
}
?>

	<form method="GET">
		<?
		$roomsCnt = intval($_REQUEST["roomsCnt"]);
		if ($roomsCnt > 100) $roomsCnt = 100;
		$living = $_REQUEST["living"];
		$maxLiving = $_REQUEST["maxLiving"];
		?>
		Количество комнат
		<input type="number" value="<?=($roomsCnt > 0) ? $roomsCnt : 1;?>" name="roomsCnt"/>

		<?if ($roomsCnt > 0):?>

					Уже живут
					Макс живущих

						<?for($i = 0; $i < $roomsCnt; $i++):?>

								<input type="number" name="living[<?=$i?>]" value="<?=$living[$i]?>"/>
								<input type="number" name="maxLiving[<?=$i?>]" value="<?=$maxLiving[$i]?>"/>

						<?endfor?>

		<?endif;?>
		<?
		if($living > 0) $submit = "Рассчитать";
		else $submit = "Показать комнаты";
		?>
		<input type="submit" value="<?=$submit?>"/>
	</form>
	<?
	if(!empty($living)):?>

Результат:

		<?
		$rooms = 0;
		for($i = 0; $i < $roomsCnt; $i++){
			if ($living[$i] < $maxLiving[$i] - 1) $rooms++;
		}
		echo $rooms;
		?>

	<?endif?>

<?
$data = file('php://stdin');
$roomsCnt = intval($data[0]);
if ($roomsCnt > 100) $roomsCnt = 100;
if ($roomsCnt > 0){
	$rooms = 0;
	for($i = 1; $i <= $roomsCnt; $i++){
		$room = explode(" ", $data[$i]);
		if ($room[0] < $room[1] - 2) $rooms++;
	}
	echo $rooms;
}
?>

<?php
function input()
{
    while (($line = fgets(STDIN)) != PHP_EOL) {
        yield $line;
    }
}

$COUNT = 0;
$room = 0;
foreach (input() as $line) {
    if ($COUNT == 0) {
        $n = intval($line);
        ++$COUNT;
        continue;
    }

    list($p, $q) = explode(' ', $line);
    if (intval($q) - intval($p) >=2 ) {
        ++$room;
    }

    if (++$COUNT >= $n) {
        echo $room;
        break;
    }
}

<?php
$counter = 0;
$n = trim(fgets(STDIN));
for($i=0;$i<$n;$i++){
	list($p,$q) = explode(" ",trim(fgets(STDIN)));
	if(($q-$p) > 2) $counter++;
}
printf("%d",$counter);
?>

<?php
$n = fgets(STDIN);
$summ=0;
for ($i=0;$i<$n;$i++)
{
    $room = explode(" ",fgets($STDIN));
    if ($room[1]-$room[0]>1)$summ++;
}
echo $summ;

<?php
$input=fgets(STDIN);
$room=0;
$array=[];
for ($i=0;$i<$input;$i++) {
    fscanf(STDIN,"%d\t%d",$array[$i]['person'],$array[$i]['nroom']);
}
for ($i=0; $i <$input;$i++) {
      if ($array[$i]['nroom'] > $array[$i]['person'] ) {
            $room++;
      }
}
echo $room;

<?php
$input=fgets(STDIN);
$room=0;
$array=[];
for ($i=0;$i<$input;$i++) {
    $array[$i]['person']=fgets(STDIN);
    $array[$i]['nroom']=fgets(STDIN);
}
for ($i=0; $i <$input;$i++) {
      if ($array[$i]['nroom'] > $array[$i]['person'] ) {
            $room++;
      }
}
echo $room;

<?php
// 467A 	Юра и заселение

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

$input_line = fgets($in);
$n = (int) $n;

$col = 0;

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

    $input_line = fgets($in);
    list($p, $q) = split(" ", $input_line);
    $p = (int) $p;
    $q = (int) $q;

    if (($q - $p) > 1) {
        $col ++;
    }
}

echo $col;

?>

<?php
// 467A 	Юра и заселение

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

$input_line = fgets($in);
$n = (int) $input_line;

$col = 0;
echo " n=$n ";
for ($i=0; $i < $n; $i++) {

    $input_line = fgets($in);
    list($p, $q) = preg_split('/ /', $input_line, -1, PREG_SPLIT_NO_EMPTY);
    $p = (int) $p;
    $q = (int) $q;

    if (($q - $p) > 1) {
        $col ++;
    }
}

echo $col;

?>

<?php
// 467A 	Юра и заселение

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

$input_line = fgets($in);
$n = (int) $n;

$col = 0;

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

    $input_line = fgets($in);
    list($p, $q) = split(" ", $input_line);
    $p = (int) $p;
    $q = (int) $q;

    if (($q - $p) > 1) {
        $col++;
    }
}

echo $col;

?>

<?php

    $n = trim(fgets(STDIN));
    $arr = array();
    $arr[] = explode(' ', trim(fgets(STDIN)));
    $arr[] = explode(' ', trim(fgets(STDIN)));
    $arr[] = explode(' ', trim(fgets(STDIN)));
    $j=0;

    for ($i=0; $i < $n; $i++) {
        if($arr[$i][1] - $arr[$i][0] > 1) {
           $j++;
        }
    }

    echo $j;

?>

<?php
	$z = trim(fgets(STDIN));
	$move = 0;
	for($i=0; $i<$z; $i++){
		$input = explode(' ', trim(fgets(STDIN)));
		if(($input[1] - $input[0]) > 0) $move++;
	}
	echo $move;
?>

<?php
	$z = trim(fgets(STDIN));
	$move = 0;
	for($i=0; $i<$z; $i++){
		$input = explode(' ', trim(fgets(STDIN)));
		if(($input[1] - $input[2]) >= 0) $move++;
	}
	echo $move;
?>

<?php
	$z = trim(fgets(STDIN));
	$move = 0;
	for($i=0; $i<$z; $i++){
		$input = explode(' ', trim(fgets(STDIN)));
		if(($input[1] - $input[2]) > 0) $move++;
	}
	echo $move;
?>

<?php
	$z = trim(fgets(STDIN));
	$move = 0;
	for($i=0; $i<$z; $i++){
		$input = explode(' ', trim(fgets(STDIN)));
		if($input[0] != $input[1]) $move++;
	}
	echo $move;
?>


Olga came to visit the twins Anna and Maria and saw that they have many cookies. The cookies are distributed into bags. As there are many cookies, Olga decided that it's no big deal if she steals a bag. However, she doesn't want the sisters to quarrel because of nothing when they divide the cookies. That's why Olga wants to steal a bag with cookies so that the number of cookies in the remaining bags was even, that is, so that Anna and Maria could evenly divide it into two (even 0 remaining cookies will do, just as any other even number). How many ways there are to steal exactly one cookie bag so that the total number of cookies in the remaining bags was even?

The first line contains the only integer n (1 ≤ n ≤ 100) — the number of cookie bags Anna and Maria have. The second line contains n integers ai (1 ≤ ai ≤ 100) — the number of cookies in the i-th bag.

Print in the only line the only number — the sought number of ways. If there are no such ways print 0.

NoteIn the first sample Olga should take the only bag so that the twins ended up with the even number of cookies.In the second sample Olga can take any of five bags with two cookies or any of three bags with four cookies — 5 + 3 = 8 ways in total.In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2 * 9 + 99 = 117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies.

<?php
$countBags = (int) fgets(STDIN);
$bags = explode(' ', fgets(STDIN));

$countWays = 0;
for($i = 0; $i < $countBags; $i++) {
    $element = array_shift($bags);
    if (array_sum($bags) % 2 == 0) {
        $countWays++;
    }
    array_push($bags, $element);
}
echo $countWays;

<?php

$n=intval(fgets(STDIN, 150));
$mass=fgets(STDIN,100000);

$a=explode(' ',$mass);
$sum=array_sum($a);

$kol=0;

for($i=0;$i<$n;$i++){
	$chet=$sum-$a[$i];
	if($chet%2==0)
		$kol++;
}

$test=fwrite(STDOUT,$kol);

?>

<?php
$n = intval(trim(fgets(STDIN)));
$in = explode(' ',trim(fgets(STDIN)));

$summ = 0;
foreach ($in as $i ) {
    $summ += intval($i);
}

$count = 0;
foreach ($in as $i) {
    if( (($summ-intval($i))%2) == 0 ) {
        $count++;
    }
}
echo($count);

<?php
#10:47
$count=intval(trim(fgets(STDIN)));
$input=explode(' ',trim(fgets(STDIN)));
$cookie_count = 0;
$result = 0;
foreach($input as $v) {
    $cookie_count += intval($v);
}
foreach($input as $v) {
    if(($cookie_count - $v)%2===0) {
        $result++;
    }
}
echo( $result );

<?php
$kolvo = intval(fgets(STDIN, 1024));
$temp = fgets(STDIN, 100000);
$pakety = array_map('intval', explode(' ', $temp));
$co = array_sum($pakety);
$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    if (($co-$pakety[$i])%2 == 0) {
        $sposoby++;
    }
}
echo $sposoby;
?>

<?php
$n = trim(fgets(STDIN));
$mas = explode(' ', trim(fgets(STDIN)));

$sum = array_sum($mas);
$mas_count = array_count_values($mas);
$c = 0;
foreach ($mas as $k => $v)
{
    if (($sum - $v) % 2 == 0)
    {
        $c++;
    }
}
echo $c;
?>

<?php

$fp=fopen('standart.input', 'r');
$fo=fopen('standart.output', 'r+');

$i=0;
if($fp)
	while(!feof($fp))
		if($i=0){
			$n=intval(fgets($fp, 999));
			$i++;
		}
		else
			$mass=fgets($fp,999);

$a=explode(' ',mass);

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

$kol=0;

for($i=0;$i<$n;$i++){
	$chet=$sum-$a[$i];
	if($chet%2==0)
		$kol++;
}

$test=fwrite($fo,$kol);
fclose($fp);
fclose($fo);

?>

<?php

$fp=fopen('std.in', 'r');
$fo=fopen('std.out', 'r+');

$i=0;
if($fp)
	while(!feof($fp))
		if($i=0){
			$n=intval(fgets($fp, 999));
			$i++;
		}
		else
			$mass=fgets($fp,999);

$a=explode(' ',mass);
$sum=0;
for($i=0;$i<$n;$i++){
	$sum+=$a[$i];
}

$kol=0;

for($i=0;$i<$n;$i++){
	$chet=$sum-$a[$i];
	if($chet%2==0)
		$kol++;
}

$test=fwrite($fo,$kol);
fclose($fp);
fclose($fo);

?>

<?php
$n = intval(trim(fgets(STDIN)));
$in = explode(' ',trim(fgets(STDIN)));
$summ = 0;
foreach ($in as $i ) { $summ=+$i; }

$res = 0;
foreach ($in as $i) {
    if( ($summ-$i)%2 == 0 ) {
        $res++;
    }
}
echo($res);

<?php
$a = file(STDIN, FILE_IGNORE_NEW_LINES);
$k = $a[0];
$b = explode(" ", $a[1]);

$sposoby = 0;
$count = array_count_values($b);

for ($i=0; $i<$k; $i++) {
    $temp = $b;
    unset($temp[$i]);
    if (mod(array_count_values($b), 2) == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;
?>

<?php
$b = fgets(STDIN, 1024);
$a = explode(" ", $b);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;

?>

<?php
$b = fgets(STDIN, 1024);
$a = explode(' ', $b, 2);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
}

echo $sposoby;
?>

<?php
$a = file(STDIN, FILE_IGNORE_NEW_LINES);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;
?>

<?php
$b = fgets(STDIN, 1024);
$a = explode(" ", $b, 2);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
}

echo $sposoby;
?>

<?php
$a = file(STDIN, FILE_IGNORE_NEW_LINES);
$k = $a[0];
$b = explode(" ", $a[1]);

$sposoby = 0;
$count = array_count_values($b);

for ($i=0; $i<$k; $i++) {
    $temp = $b;
    unset($b[$i]);
    if (mod(array_count_values($b), 2) == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;
?>

<?php
$a = file(STDIN, FILE_IGNORE_NEW_LINES);
$k = $a[0];
$b = explode(" ", $a[1]);

$sposoby = 0;
//$c = array_count_values($b);

for ($i=0; $i<$k; $i++) {
    $temp = $b;
    unset($temp[$i]);
    if (mod(array_count_values($temp), 2) == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;
?>

<?php
$a = file(STDIN, FILE_IGNORE_NEW_LINES);
$k = $a[0];
$b = explode(" ", $a[1]);

$sposoby = 0;
$count = array_count_values($b);

for ($i=0; $i<k; $i++) {
    $temp = $b;
    unset($b[$i]);
    if (mod(array_count_values($b), 2) == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;
?>

<?php
$b = fgets(STDIN, 2048);
$a = explode(" ", $b);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;

?>

<?php
$a = file(STDIN, FILE_IGNORE_NEW_LINES);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;
//$c = array_count_values($b);

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
    unset($temp);
}

echo $sposoby;
?>

<?php
$b = fgets(STDIN, 1024);
$a = explode(" ", $b);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 == 0) {
        $sposoby++;
    }
    //unset($temp);
}

echo $sposoby;
?>

<?php
$b = fgets(STDIN, 1024);
$a = explode(' ', $b, 2);
$kolvo = $a[0];
$pakety = explode(" ", $a[1]);

$sposoby = 0;

for ($i=0; $i<$kolvo; $i++) {
    $temp = $pakety;
    unset($temp[$i]);
    if (array_count_values($temp)%2 != 0) {
        $sposoby++;
    }
}

echo $sposoby;
?>

<?php
$n = trim(fgets(STDIN));
$mas = explode(' ', trim(fgets(STDIN)));

$sum = array_sum($mas);
$mas_count = array_count_values($mas);

if ($sum % 2 == 0)
{
    foreach ($mas_count as $k => $v)
    {
        if ($k % 2 == 0)
            $c += $v;
    }
}
else
{
    foreach ($mas_count as $k => $v)
    {
        if ($k % 2 != 0)
            $c += $v;
    }
}
echo $c;
?>


Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.

The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).


Mishka wants to buy some food in the nearby shop. Initially, he has $$$s$$$ burles on his card. Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number $$$1 \le x \le s$$$, buy food that costs exactly $$$x$$$ burles and obtain $$$\lfloor\frac{x}{10}\rfloor$$$ burles as a cashback (in other words, Mishka spends $$$x$$$ burles and obtains $$$\lfloor\frac{x}{10}\rfloor$$$ back). The operation $$$\lfloor\frac{a}{b}\rfloor$$$ means $$$a$$$ divided by $$$b$$$ rounded down.It is guaranteed that you can always buy some food that costs $$$x$$$ for any possible value of $$$x$$$.Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.For example, if Mishka has $$$s=19$$$ burles then the maximum number of burles he can spend is $$$21$$$. Firstly, he can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback. Now he has $$$s=10$$$ burles, so can spend $$$x=10$$$ burles, obtain $$$1$$$ burle as a cashback and spend it too.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a separate line and consists of one integer $$$s$$$ ($$$1 \le s \le 10^9$$$) — the number of burles Mishka initially has.

For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

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

    for($i=0; $i<$t; $i++) {
        $n = trim(fgets(STDIN));
        $total = 0;

        while ($n >= 10) {

             $d = $n % 10;
             $s = floor($n / 10);

             $n = $d + $s;
            $total += $s * 10 ;
        }
        $total += $n;

        echo $total ."\n";

    }

<?

$n = intval(fgets(STDIN));
for ($i = 0; $i < $n; $i++) {
    $money = intval(fgets(STDIN));
    $cashback = 0;
    $total = 0;
    while($money > 9) {
        $cashback = intdiv($money, 10);
        $total += 10 * $cashback;
        $money = $money - 9 * $cashback;
    }
    $total += $money;
    echo $total."\n";
}
?>

<?php

namespace io {
	$input_file_content = file_get_contents("php://stdin");
	$input_buf = explode("\r\n", $input_file_content);
	unset($input_file_content);
}

namespace main {
	error_reporting(E_ALL);
	ini_set('display_errors', true);
	ini_set('html_errors', false);

	use function io\next_line as next_line;
	use function io\next_ as next;
	use function io\next_int as next_int;
	use function io\next_double as next_double;

	$tc = next_int();
	for($cc=0; $cc<$tc; $cc++) {
		$n = next_int();
		$m = ($n - 1) / 9;
		printf("%d\n", $n + $m);
	}
}

namespace io {
	function next_line() {
		static $cnt = 0;
		global $input_buf;
		return $input_buf[$cnt++];
	}
	function next_() {
		static $tk = [];
		static $tk_cnt = 0;
		if($tk_cnt == count($tk)) {
			$tk = explode(" ", next_line());
			$tk_cnt = 0;
		}
		return $tk[$tk_cnt++];
	}
	function next_int() {
		return (int)next_();
	}
	function next_double() {
		return (double)next_();
	}
}

?>

<?php

namespace io {
	$input_file_content = file_get_contents("php://stdin");
	$input_buf = explode("\r\n", $input_file_content);
	unset($input_file_content);
}

namespace main {
	error_reporting(E_ALL);
	ini_set('display_errors', true);
	ini_set('html_errors', false);

	use function io\next_line as next_line;
	use function io\next_ as next;
	use function io\next_int as next_int;
	use function io\next_double as next_double;

	$tc = next_int();
	for($cc=0; $cc<$tc; $cc++) {
		$n = next_int();
		$m = $n / 9;
		printf("%d\n", $n + $m);
	}
}

namespace io {
	function next_line() {
		static $cnt = 0;
		global $input_buf;
		return $input_buf[$cnt++];
	}
	function next_() {
		static $tk = [];
		static $tk_cnt = 0;
		if($tk_cnt == count($tk)) {
			$tk = explode(" ", next_line());
			$tk_cnt = 0;
		}
		return $tk[$tk_cnt++];
	}
	function next_int() {
		return (int)next_();
	}
	function next_double() {
		return (double)next_();
	}
}

?>


A string $$$a=a_1a_2\dots a_n$$$ is called even if it consists of a concatenation (joining) of strings of length $$$2$$$ consisting of the same characters. In other words, a string $$$a$$$ is even if two conditions are satisfied at the same time:  its length $$$n$$$ is even;  for all odd $$$i$$$ ($$$1 \le i \le n - 1$$$), $$$a_i = a_{i+1}$$$ is satisfied. For example, the following strings are even: "" (empty string), "tt", "aabb", "oooo", and "ttrrrroouuuuuuuukk". The following strings are not even: "aaa", "abab" and "abba".Given a string $$$s$$$ consisting of lowercase Latin letters. Find the minimum number of characters to remove from the string $$$s$$$ to make it even. The deleted characters do not have to be consecutive.

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

For each test case, print a single number — the minimum number of characters that must be removed to make $$$s$$$ even.

6

aabbdabdccc

zyx

aaababbb

aabbcc

oaoaaaoo

bmefbmuyw

NoteIn the first test case you can remove the characters with indices $$$6$$$, $$$7$$$, and $$$9$$$ to get an even string "aabbddcc".In the second test case, each character occurs exactly once, so in order to get an even string, you must remove all characters from the string.In the third test case, you can get an even string "aaaabb" by removing, for example, $$$4$$$-th and $$$6$$$-th characters, or a string "aabbbb" by removing the $$$5$$$-th character and any of the first three.


Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show".Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.  Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!

The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to    "L", if the i-th domino has been pushed to the left;  "R", if the i-th domino has been pushed to the right;  ".", if the i-th domino has not been pushed.  It is guaranteed that if si = sj = "L" and i < j, then there exists such k that i < k < j and sk = "R"; if si = sj = "R" and i < j, then there exists such k that i < k < j and sk = "L".

Output a single integer, the number of the dominoes that remain vertical at the end of the process.

NoteThe first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.In the second example case, all pieces fall down since the first piece topples all the other pieces.In the last example case, a single piece has not been pushed in either direction.


You are the gym teacher in the school.There are $$$n$$$ students in the row. And there are two rivalling students among them. The first one is in position $$$a$$$, the second in position $$$b$$$. Positions are numbered from $$$1$$$ to $$$n$$$ from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions $$$p$$$ and $$$s$$$ respectively, then distance between them is $$$|p - s|$$$. You can do the following operation at most $$$x$$$ times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most $$$x$$$ swaps.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$a$$$ and $$$b$$$ ($$$2 \le n \le 100$$$, $$$0 \le x \le 100$$$, $$$1 \le a, b \le n$$$, $$$a \neq b$$$) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.

For each test case print one integer — the maximum distance between two rivaling students which you can obtain.

NoteIn the first test case you can swap students in positions $$$3$$$ and $$$4$$$. And then the distance between the rivals is equal to $$$|4 - 2| = 2$$$.In the second test case you don't have to swap students. In the third test case you can't swap students.

<?php
$rowCount = trim(fgets(STDIN));
$inputArr = [];

$i = 1;
while ($i <= $rowCount) {
  $input = trim(fgets(STDIN));
  $inputArr1 = explode(" ", $input);

  array_push($inputArr, $inputArr1);
  $i++;
}

for ($i = 0; $i < $rowCount; $i++) {
  $n = $inputArr[$i][0];
  $x = $inputArr[$i][1];
  $a = $inputArr[$i][2];
  $b = $inputArr[$i][3];

  if (abs($a - $b) != $n - 1 && $x != 0) {
    $result = abs($a - $b);

    for ($j = 0; $result != $n - 1 && $j < $x; $j++) {
      $result++;
    }

    echo $result . "\n";
  } else {
    $result = abs($a - $b);
    echo $result . "\n";
  }
}

<?php
list($c) = explode(' ', trim(fgets(STDIN)));
for ($i = 0; $i < $c; $i++) {
    list($n, $m, $a, $b) = explode(' ', trim(fgets(STDIN)));
    if (abs($a - $b) + $m >= $n) {
        echo ($n -1) . "\n";
    } else {
        echo (abs($a - $b) + $m) . "\n";
    }
}

<?php

while(! feof(STDIN)){
    $arr[]=fgets(STDIN);
}

for($i=0; $i<$arr[0]; $i++){
    $value=explode(" ", $arr[1+$i]);
    if(abs($value[2]-$value[3])+$value[1]>=$value[0]){echo $value[0]-1;}
    else{echo abs($value[2]-$value[3])+$value[1];}
    if($i!= $arr[0]+1) echo "\n";
}
?>

<?php
$a = trim(fgets(STDIN));
for($i = 0; $i < $a; $i++){
    $r = explode(" ", fgets(STDIN));
    $max = $r[0] - 1;
    $range = abs($r[2] - $r[3]);
    if(($range + $r[1]) < $max){
        echo $range + $r[1]."\n";
    } else {
        echo $max."\n";
    }
}

<?php

function gcd($a,$b)
{
    while($b)
    {
        $a%=$b;
        $tmp=$b;
        $b=$a;
        $a=$tmp;

    }
    return $a;
}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);

        $ans=max($str1[2],$str1[3])-min($str1[2],$str1[3]);
        if($ans+$str1[1]>=$str1[0])
        {
            echo ($str1[0]-$ans-1)+$ans."\n";
        }else{
            echo $ans+$str1[1]."\n";
        }

    }

}

solve();
?>

<?php
$rowCount = trim(fgets(STDIN));
$inputArr = [];

$i = 1;
while ($i <= $rowCount) {
  $input = trim(fgets(STDIN));
  $inputArr1 = explode(" ", $input);

  array_push($inputArr, $inputArr1);
  $i++;
}

for ($i = 0; $i < $rowCount; $i++) {
  $n = $inputArr[$i][0];
  $x = $inputArr[$i][1];
  $a = $inputArr[$i][2];
  $b = $inputArr[$i][3];

  if (abs($a - $b) != $n - 1 && $x != 0) {
    $result = abs($a - $b);

    for ($j = 0; $result != $n && $j <= $x; $j++) {
      $result++;
    }

    echo $result . "\n";
  } else {
    $result = abs($a - $b);
    echo $result . "\n";
  }
}

<?php
$rowCount = trim(fgets(STDIN));
$inputArr = [];

$i = 1;
while ($i <= $rowCount) {
  $input = trim(fgets(STDIN));
  $inputArr1 = explode(" ", $input);

  array_push($inputArr, $inputArr1);
  $i++;
}

for ($i = 0; $i < $rowCount; $i++) {
  $n = $inputArr[$i][0];
  $x = $inputArr[$i][1];
  $a = $inputArr[$i][2];
  $b = $inputArr[$i][3];

  if (abs($a - $b) != $n - 1 && $x != 0) {
    $result = abs($a - $b);

    for ($j = 0; $result != $n && $j < $x; $j++) {
      $result++;
    }

    echo $result . "\n";
  } else {
    $result = abs($a - $b);
    echo $result . "\n";
  }
}

<?php

while(! feof(STDIN)){
    $arr[]=fgets(STDIN);
}

for($i=0; $i<$arr[0]; $i++){
    $value=explode(" ", $arr[1+$i]);
    if(abs($value[2]-$value[3])+$value[1]>$value[0]){echo $value[0]-1;}
    else{echo abs($value[2]-$value[3])+$value[1];}
    if($i!= $arr[0]+1) echo "\n";
}
?>

<?php

while(! feof(STDIN)){
    $arr[]=fgets(STDIN);
}

for($i=0; $i<$arr[0]; $i++){
    $value=explode(" ", $arr[1+$i]);
    if($value[0]==$value[2] && $value[3]==1){echo abs($value[2]-$value[3]);}
    elseif($value[0]>=$value[2]+$value[1]){echo abs($value[2]+$value[1]-$value[3]);}
    elseif($value[0]<$value[2]+$value[1] && $value[3]-$value[1]>=1){echo abs($value[0]-($value[0]-$value[1]+$value[3]));}
    elseif($value[0]<$value[2]+$value[1] && $value[3]-$value[1]<1){echo abs($value[0]-1);}
    if($i!= $arr[0]+1) echo "\n";
}

?>

<?php

function gcd($a,$b)
{
    while($b)
    {
        $a%=$b;
        $tmp=$b;
        $b=$a;
        $a=$tmp;

    }
    return $a;
}
function solve()
{
    $server="php://stdin";
    $local="input.txt";
    $handle = fopen($server, "r");
    $s="";$d=0;$t=0;
    $x=trim(fgets($handle));
    for($i=0;$i<$x;$i++)
    {

        $str=trim(fgets($handle));
        $str1=explode(" ",$str);

        $ans=max($str1[2],$str1[3])-min($str1[2],$str1[3]);
        if($ans+$str1[1]>$str1[0])
        {
            echo ($str1[0]-$ans-1)+$ans."\n";
        }else{
            echo $ans+$str1[1]."\n";
        }

    }

}

solve();
?>


The only difference between easy and hard versions is a number of elements in the array.You are given an array $$$a$$$ consisting of $$$n$$$ integers. The value of the $$$i$$$-th element of the array is $$$a_i$$$.You are also given a set of $$$m$$$ segments. The $$$j$$$-th segment is $$$[l_j; r_j]$$$, where $$$1 \le l_j \le r_j \le n$$$.You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $$$a = [0, 0, 0, 0, 0]$$$ and the given segments are $$$[1; 3]$$$ and $$$[2; 4]$$$ then you can choose both of them and the array will become $$$b = [-1, -2, -2, -1, 0]$$$.You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $$$a$$$ and obtain the array $$$b$$$ then the value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ will be maximum possible.Note that you can choose the empty set.If there are multiple answers, you can print any.If you are Python programmer, consider using PyPy instead of Python when you submit your code.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 300, 0 \le m \le 300$$$) — the length of the array $$$a$$$ and the number of segments, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^6 \le a_i \le 10^6$$$), where $$$a_i$$$ is the value of the $$$i$$$-th element of the array $$$a$$$. The next $$$m$$$ lines are contain two integers each. The $$$j$$$-th of them contains two integers $$$l_j$$$ and $$$r_j$$$ ($$$1 \le l_j \le r_j \le n$$$), where $$$l_j$$$ and $$$r_j$$$ are the ends of the $$$j$$$-th segment.

In the first line of the output print one integer $$$d$$$ — the maximum possible value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ if $$$b$$$ is the array obtained by applying some subset of the given segments to the array $$$a$$$. In the second line of the output print one integer $$$q$$$ ($$$0 \le q \le m$$$) — the number of segments you apply. In the third line print $$$q$$$ distinct integers $$$c_1, c_2, \dots, c_q$$$ in any order ($$$1 \le c_k \le m$$$) — indices of segments you apply to the array $$$a$$$ in such a way that the value $$$\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$$$ of the obtained array $$$b$$$ is maximum possible. If there are multiple answers, you can print any.

NoteIn the first example the obtained array $$$b$$$ will be $$$[0, -4, 1, 1, 2]$$$ so the answer is $$$6$$$.In the second example the obtained array $$$b$$$ will be $$$[2, -3, 1, -1, 4]$$$ so the answer is $$$7$$$.In the third example you cannot do anything so the answer is $$$0$$$.


The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k × d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers.

The first output line contains two space-separated integers n and l (1 ≤ n, l ≤ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 ≤ ai ≤ 100).

Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0.

NoteIn the first sample test the required window is 2 × 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves.

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = $c;
$f = array();
$h = array();
rsort($d);
if($d[0] < $b)
{
    print "0";
}
else
{
    for($x = $b; $x <= $d[0]; $x++)
    {
        for($y = 0; $y < $a; $y++)
        {
            $e = floor($c[$y] / $x);
            array_push($f, $e);
        }
        $g = array_sum($f) * $x;
        array_push($h, $g);
        $f = array();
    }
    rsort($h);
    print $h[0];
}
?>

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


Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that:   r ≥ 2;  for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds;  each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled:   no pair of tubes has common cells;  each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.

The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.

Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.

NotePicture for the first sample:   Picture for the second sample:

<?php
	$in = 'php://stdin';
	//$in = 'inputC.txt';

	$input = fopen($in, "r");

    list($n, $m, $k) = explode(" ", trim(fgets($input)));

    $swit[0][0] = 1; $swit[0][1] = $m;
    $swit[1][0] = -1; $swit[1][1] = 1;
    $counter = 0;

    $iter = $swit[$counter][0];
    $ogran = $swit[$counter][1];

    $copyK = $k-1;

    $x = 1; $y = 1;

    while ($k > 1) {
    	echo '2 ';
    	echo $x.' '.$y;

    	if ($y==$ogran) {
    		$counter = ($counter+1)%2;
   			$iter = $swit[$counter][0];
    		$ogran = $swit[$counter][1];
    		$x++;
    	}
    	else {
    		$y+=$iter;
    	}

    	echo ' '.$x.' '.$y.PHP_EOL;
    	$k--;

    	if ($y==$ogran) {
    		$counter = ($counter+1)%2;
   			$iter = $swit[$counter][0];
    		$ogran = $swit[$counter][1];
    		$x++;
    	}
    	else {
    		$y+=$iter;
    	}
    }
    echo ($n*$m-2*$copyK);

    while ($x<=$n) {
    	echo ' '.$x.' '.$y;

    	if ($y==$ogran) {
    		$counter = ($counter+1)%2;
   			$iter = $swit[$counter][0];
    		$ogran = $swit[$counter][1];
    		$x++;
    	}
    	else {
    		$y+=$iter;
    	}
    }

?>


You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string.In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes.A palindrome is a string that can be read the same way from left to right and from right to left. For example, "abacaba", "z", "abba" are palindromes.A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac".

The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters.

Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s. Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier.

<?php
error_reporting(1);

$fp = fopen('php://stdin','r');
//$fp = fopen('D:/work/php/codeforce/DDD.txt','r');
$word = trim( str_replace( "\r\n", '', fgets($fp) ) );
/*
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtimeS = $mtime[1] + $mtime[0];
*/

$len = strlen( $word );
$palindrom = array();
$countCache = array();
$pS = array();
$pE = array();
for( $i = 0; $i < $len; $i++ )
{
	$pE[$i]++;
	$countCache[ $i ]++;
	$z = 0;
	while( true ){
		$z++;
		if( isset( $word[$i - $z] ) && $word[$i - $z] == $word[$i + $z]){
			$pE[($i + $z)]++ ;
			$countCache[ $i - $z ]++;
		} else {
			break;
		}
	}
	$z = 0;
	while( true ){
		$z++;
		if( isset( $word[$i - $z] ) && $word[$i - $z] == $word[$i + $z - 1]){
			$pE[($i + $z - 1)]++;
			$countCache[ $i - $z ]++;
		} else {
			break;
		}
	}
}

$sum = 0;
$tt = end( $countCache );
while( true ){
	$sum += $tt;
	$countCache[ key( $countCache ) ] = $sum;
	$tt = prev( $countCache );
	if( !$tt ){
		break;
	}
}
//print_r( $pE );
//print_r( $countCache );

reset( $countCache );
$pp = current( $pE ) ;
$cc = current( $countCache );
$res = 0;
while( true )
{
	if( key( $pE ) < key( $countCache ) ){
		$res += $pp * $cc;
		$pp = next( $pE ) ;
	} else {
		$cc = next( $countCache );
	}
	if( !$pp || !$cc ){
		break;
	}
}
echo $res;
/*
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtimeE = $mtime[1] + $mtime[0];

echo "\n" . ( $mtimeE - $mtimeS ) . "\n";*/

fclose($fp);
?>

<?php

$input = STDIN;

$string = trim(fgets($input));

$strlen = strlen($string);
/*
for($i=1; $i<$strlen; $i++) {
	for($j=0; $j<$strlen-$i+1; $j++) {
		#$a = substr($string, $j, $i)."\n";
	}
}
*/

$result = 0;
$resultSingle = 0;
$palCount = array();

/*$i=0;
$start = -1;
while($i < $strlen-1) {
	while ($string[$i] == $string[$i+1]);
}*/
$arEndPoints = array();
for ($i=0; $i<$strlen; $i++) {
	#$palyndrom[$i][] = 1;
	$resultSingle += $strlen-1;

	/*
	if (!isset($palCount[$i])) $palCount[$i] = 1;
	else ++$palCount[$i];
	*/

	# поиск палиндромов с центром
	$plen = 1;
	$center = true;
	$offset = true;
	while ($i-$plen+1 >= 0 && $i+$plen <= $strlen-1 && ($center || $offset)){
		$center = ($center && ($i-$plen >= 0) && $string[$i-$plen] == $string[$i+$plen]);
		if ($center) {
			#$palyndrom[$i-$plen][] = $plen+$plen+1;
			if (!isset($arEndPoints[$i+$plen+1])) $arEndPoints[$i+$plen+1] = 1;
			else ++$arEndPoints[$i+$plen+1];

			if (!isset($palCount[$i-$plen])) $palCount[$i-$plen] = 1;
			else ++$palCount[$i-$plen];

			#echo substr($string, $i-$plen, $plen+$plen+1)."\n";
			$result += $strlen - ($plen+$plen+1);
		}

		$offset = $offset && ($string[$i-$plen+1] == $string[$i+$plen]);
		if ($offset) {
			#$palyndrom[$i-$plen+1][] = $plen+$plen;
			if (!isset($arEndPoints[$i+$plen+1])) $arEndPoints[$i+$plen+1] = 1;
			else ++$arEndPoints[$i+$plen+1];

			if (!isset($palCount[$i-$plen+1])) $palCount[$i-$plen+1] = 1;
			else ++$palCount[$i-$plen+1];

			#echo substr($string, $i-$plen+1, $plen+$plen)."\n";
			$result += $strlen - ($plen+$plen);
		}

		++$plen;
	}
}

$sumPalCount = array();
$count = max(array_keys($palCount));#count($palCount);
$last = $count;
$sumPalCount[$last] = $palCount[$last];
for($i=$count-1; $i>=0; --$i){
	if (isset($palCount[$i])){
		$sumPalCount[$i] = $palCount[$i] + $sumPalCount[$last];
		$last = $i;
	}
}

$pairCount = 0;
$endPoints = array_keys($arEndPoints);
$endPointsCount = count($endPoints);

for($i=0; $i<$endPointsCount; ++$i){
	for($k=$endPoints[$i]; $k<=$count; ++$k){
		if (isset($palCount[$k])){
			#$pairCount += $palCount[$k];
			$pairCount += $sumPalCount[$k] * $arEndPoints[$endPoints[$i]];
			break;
		}
	}
}

$resultSingle /= 2;
$result += $resultSingle + $pairCount;
echo $result;

?>


Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.In this problem you are given n memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to n.For each cell i you are given two values:   li — cell containing previous element for the element in the cell i;  ri — cell containing next element for the element in the cell i. If cell i contains information about the element which has no previous element then li = 0. Similarly, if cell i contains information about the element which has no next element then ri = 0.    Three lists are shown on the picture. For example, for the picture above the values of l and r are the following: l1 = 4, r1 = 7; l2 = 5, r2 = 0; l3 = 0, r3 = 0; l4 = 6, r4 = 1; l5 = 0, r5 = 2; l6 = 0, r6 = 4; l7 = 1, r7 = 0.Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values li, ri.Any other action, other than joining the beginning of one list to the end of another, can not be performed.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of memory cells where the doubly linked lists are located. Each of the following n lines contains two integers li, ri (0 ≤ li, ri ≤ n) — the cells of the previous and the next element of list for cell i. Value li = 0 if element in cell i has no previous element in its list. Value ri = 0 if element in cell i has no next element in its list. It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of n given cells.

Print n lines, the i-th line must contain two integers li and ri — the cells of the previous and the next element of list for cell i after all lists from the input are united in a single list. If there are many solutions print any of them.


You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$.You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).

Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.

NoteIn the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$.In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$.In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$.In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$.In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$.In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$.

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
array_push($c, "0");
$d = trim(fgets(STDIN)) . "#";
$e = 0;
$f = new SplMaxHeap();
$f -> insert($c[0]);
for($x = 0; $x < $a; $x++)
{
     if($d[$x] == $d[$x + 1])
     {
          $f -> insert($c[$x + 1]);
     }
     else
     {
          for($y = 0; $y < $b; $y++)
          {
               $e += $f -> extract();
               if($f -> isEmpty() == TRUE)
               {
                    break;
               }
          }
          $f = new SplMaxHeap();
          $f -> insert($c[$x + 1]);
     }
}
printf("%.0f", $e);
?>

<?php
/*****************************************/
class InputReader {protected $_fh;public function __construct($fileHandler = STDIN){$this->_fh = $fileHandler;}
	public function readInt() {$d = trim(fgets($this->_fh));return (int)$d;}
	public function readString() {$d = trim(fgets($this->_fh));return $d;}
	public function readArrayOfInt($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = (int)$item;}return $a;}
	public function readArrayOfString($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = $item;}return $a;}
}

class Utils {
	/* Perform action N times. Can return values in array */
	static public function ntimes($n, $func, $returnResults = false) {$a = [];for ($i=1; $i<=$n; $i++) {$result = $func();if ($returnResults) {$a[$i] = $result;}}return $a;}
	/* Integral. Keys preserved. By reference. */
	static public function cumsum(&$a){$sum = 0;foreach($a as $k=>$v) {$a[$k] += $sum;$sum = $a[$k];}}
	/* Difference. Keys preserved. By reference. */
	static public function diff(&$a){$prev = 0;foreach($a as $k=>$v) {$buf=$a[$k];$a[$k]-=$prev;$prev = $buf;}}
	static public function bitCount($n){$cnt = 0;for ($j=$n; $j; $j>>=1){$cnt += $j&1;}return $cnt;}
}

class PrimeUtils {static protected $divisors = [1 => [1=>true],2 => [1=>true, 2=>true],];static protected $primes = [];
	static public function primes($limit){$a = str_repeat(chr(85), ($limit>>3) + 1);$a[0] = chr(83);for ($i=3; $i*$i < $limit; $i+=2) {if (ord($a[$i>>3]) & (1<<($i%8))) {;} else {for ($k=$i; $k*$i<$limit; $k++) {$n = $k*$i;$a[$n>>3] = chr(ord($a[$n>>3]) | (1<<($n%8)));}}}yield 2;for ($i=3; $i<$limit; $i+=2) {if (!(ord($a[$i>>3]) & (1<<($i%8)))) {yield $i;}}}
	static public function divisors($n, $lp = 2) {if (isset(self::$divisors[$n])) {return self::$divisors[$n];}if (empty(self::$primes)) {$primes = [];foreach (self::primes(1e3) as $prime) {$primes[$prime] = $prime;}self::$primes = $primes;}$r = [];foreach (self::$primes as $p) {if ($p>=$lp) {if ($n < $p*$p) {$r = [1=>true, $n=>true];break;} elseif ($n%$p==0) {foreach([1, $p] as $pre) {foreach(self::divisors(intdiv($n,$p), $p) as $nxt=>$v) {$r[$nxt*$pre] = true;}}break;}}}self::$divisors[$n] = $r;return self::$divisors[$n];}
}

/*****************************************/

$ir = new InputReader();

/* TASK */

list($n, $k) = $ir->readArrayOfInt(2);
$vals = $ir->readArrayOfInt($n);
$vals[] = 0;
$s = $ir->readString() . ' ';

$dmg = '0';

$groupSymbol = '';

$group = [];
for($i=0; $i<=$n; $i++) {
	if ($s[$i] == $groupSymbol) {
		$group[] = $vals[$i];
	} else {
		$cnt = count($group);
		foreach ($group as $gg) {
			$dmg = bcadd($dmg, $gg);
		}
		$j = $cnt - $k;
		if ($j>0) {
			sort($group);
			while ($j--) {
				$dmg = bcsub($dmg, $group[$j]);
			}
		}

		$groupSymbol = $s[$i];
		$group = [$vals[$i]];
	}
}

echo $dmg;

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
array_push($c, "0");
$d = trim(fgets(STDIN)) . "#";
$e = 0;
$f = new SplMaxHeap();
$f -> insert($c[0]);
for($x = 0; $x < $a; $x++)
{
     if($d[$x] == $d[$x + 1])
     {
          $f -> insert($c[$x + 1]);
     }
     else
     {
          for($y = 0; $y < $b; $y++)
          {
               $e += $f -> extract();
               if($f -> isEmpty() == TRUE)
               {
                    break;
               }
          }
          $f = new SplMaxHeap();
          $f -> insert($c[$x + 1]);
     }
}
print $e;
?>

<?php
/*****************************************/
class InputReader {protected $_fh;public function __construct($fileHandler = STDIN){$this->_fh = $fileHandler;}
	public function readInt() {$d = trim(fgets($this->_fh));return (int)$d;}
	public function readString() {$d = trim(fgets($this->_fh));return $d;}
	public function readArrayOfInt($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = (int)$item;}return $a;}
	public function readArrayOfString($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = $item;}return $a;}
}

class Utils {
	/* Perform action N times. Can return values in array */
	static public function ntimes($n, $func, $returnResults = false) {$a = [];for ($i=1; $i<=$n; $i++) {$result = $func();if ($returnResults) {$a[$i] = $result;}}return $a;}
	/* Integral. Keys preserved. By reference. */
	static public function cumsum(&$a){$sum = 0;foreach($a as $k=>$v) {$a[$k] += $sum;$sum = $a[$k];}}
	/* Difference. Keys preserved. By reference. */
	static public function diff(&$a){$prev = 0;foreach($a as $k=>$v) {$buf=$a[$k];$a[$k]-=$prev;$prev = $buf;}}
	static public function bitCount($n){$cnt = 0;for ($j=$n; $j; $j>>=1){$cnt += $j&1;}return $cnt;}
}

class PrimeUtils {static protected $divisors = [1 => [1=>true],2 => [1=>true, 2=>true],];static protected $primes = [];
	static public function primes($limit){$a = str_repeat(chr(85), ($limit>>3) + 1);$a[0] = chr(83);for ($i=3; $i*$i < $limit; $i+=2) {if (ord($a[$i>>3]) & (1<<($i%8))) {;} else {for ($k=$i; $k*$i<$limit; $k++) {$n = $k*$i;$a[$n>>3] = chr(ord($a[$n>>3]) | (1<<($n%8)));}}}yield 2;for ($i=3; $i<$limit; $i+=2) {if (!(ord($a[$i>>3]) & (1<<($i%8)))) {yield $i;}}}
	static public function divisors($n, $lp = 2) {if (isset(self::$divisors[$n])) {return self::$divisors[$n];}if (empty(self::$primes)) {$primes = [];foreach (self::primes(1e3) as $prime) {$primes[$prime] = $prime;}self::$primes = $primes;}$r = [];foreach (self::$primes as $p) {if ($p>=$lp) {if ($n < $p*$p) {$r = [1=>true, $n=>true];break;} elseif ($n%$p==0) {foreach([1, $p] as $pre) {foreach(self::divisors(intdiv($n,$p), $p) as $nxt=>$v) {$r[$nxt*$pre] = true;}}break;}}}self::$divisors[$n] = $r;return self::$divisors[$n];}
}

/*****************************************/

$ir = new InputReader();

/* TASK */

list($n, $k) = $ir->readArrayOfInt(2);
$vals = $ir->readArrayOfInt($n);
$vals[] = 0;
$s = $ir->readString() . ' ';

$dmg = 0;

$groupSymbol = '';

$group = [];
for($i=0; $i<=$n; $i++) {
	if ($s[$i] == $groupSymbol) {
		$group[] = $vals[$i];
	} else {
		//var_dump($group);
		$cnt = count($group);
		$dmg += array_sum($group);
		$j = $cnt - $k;
		if ($j>0) {
			sort($group);
			while ($j--) {
				$dmg -= $group[$j];
			}
		}

		$groupSymbol = $s[$i];
		$group = [$vals[$i]];
	}
}

echo $dmg;

<?php
/*****************************************/
class InputReader {protected $_fh;public function __construct($fileHandler = STDIN){$this->_fh = $fileHandler;}
	public function readInt() {$d = trim(fgets($this->_fh));return (int)$d;}
	public function readString() {$d = trim(fgets($this->_fh));return $d;}
	public function readArrayOfInt($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = (int)$item;}return $a;}
	public function readArrayOfString($size, $indexFrom = 0) {$a = [];$ind = $indexFrom;foreach (explode(' ', fgets($this->_fh)) as $item) {$a[$ind++] = $item;}return $a;}
}

class Utils {
	/* Perform action N times. Can return values in array */
	static public function ntimes($n, $func, $returnResults = false) {$a = [];for ($i=1; $i<=$n; $i++) {$result = $func();if ($returnResults) {$a[$i] = $result;}}return $a;}
	/* Integral. Keys preserved. By reference. */
	static public function cumsum(&$a){$sum = 0;foreach($a as $k=>$v) {$a[$k] += $sum;$sum = $a[$k];}}
	/* Difference. Keys preserved. By reference. */
	static public function diff(&$a){$prev = 0;foreach($a as $k=>$v) {$buf=$a[$k];$a[$k]-=$prev;$prev = $buf;}}
	static public function bitCount($n){$cnt = 0;for ($j=$n; $j; $j>>=1){$cnt += $j&1;}return $cnt;}
}

class PrimeUtils {static protected $divisors = [1 => [1=>true],2 => [1=>true, 2=>true],];static protected $primes = [];
	static public function primes($limit){$a = str_repeat(chr(85), ($limit>>3) + 1);$a[0] = chr(83);for ($i=3; $i*$i < $limit; $i+=2) {if (ord($a[$i>>3]) & (1<<($i%8))) {;} else {for ($k=$i; $k*$i<$limit; $k++) {$n = $k*$i;$a[$n>>3] = chr(ord($a[$n>>3]) | (1<<($n%8)));}}}yield 2;for ($i=3; $i<$limit; $i+=2) {if (!(ord($a[$i>>3]) & (1<<($i%8)))) {yield $i;}}}
	static public function divisors($n, $lp = 2) {if (isset(self::$divisors[$n])) {return self::$divisors[$n];}if (empty(self::$primes)) {$primes = [];foreach (self::primes(1e3) as $prime) {$primes[$prime] = $prime;}self::$primes = $primes;}$r = [];foreach (self::$primes as $p) {if ($p>=$lp) {if ($n < $p*$p) {$r = [1=>true, $n=>true];break;} elseif ($n%$p==0) {foreach([1, $p] as $pre) {foreach(self::divisors(intdiv($n,$p), $p) as $nxt=>$v) {$r[$nxt*$pre] = true;}}break;}}}self::$divisors[$n] = $r;return self::$divisors[$n];}
}

/*****************************************/

$ir = new InputReader();

/* TASK */

list($n, $k) = $ir->readArrayOfInt(2);
$vals = $ir->readArrayOfInt($n);
$vals[] = 0;
$s = $ir->readString() . ' ';

$dmg = '0';

$groupSymbol = '';

$group = [];
for($i=0; $i<=$n; $i++) {
	if ($s[$i] == $groupSymbol) {
		$group[] = $vals[$i];
	} else {
		//var_dump($group);
		$cnt = count($group);
		$dmg = bcadd($dmg, array_sum($group));
		$j = $cnt - $k;
		if ($j>0) {
			sort($group);
			while ($j--) {
				$dmg = bcsub($dmg, $group[$j]);
			}
		}

		$groupSymbol = $s[$i];
		$group = [$vals[$i]];
	}
}

echo $dmg;