Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'!To compose a story, Stephen wrote out $$$n$$$ words consisting of the first $$$5$$$ lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story.Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together.For example, the story consisting of three words "bac", "aaada", "e" is interesting (the letter 'a' occurs $$$5$$$ times, all other letters occur $$$4$$$ times in total). But the story consisting of two words "aba", "abcde" is not (no such letter that it occurs more than all other letters in total).You are given a sequence of $$$n$$$ words consisting of letters 'a', 'b', 'c', 'd' and 'e'. Your task is to choose the maximum number of them to make an interesting story. If there's no way to make a non-empty story, output $$$0$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of the words in the sequence. Then $$$n$$$ lines follow, each of them contains a word — a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i. e. duplicates are allowed). Only the letters 'a', 'b', 'c', 'd' and 'e' may occur in the words. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$; the sum of the lengths of all words over all test cases doesn't exceed $$$4 \cdot 10^5$$$.

For each test case, output the maximum number of words that compose an interesting story. Print 0 if there's no way to make a non-empty interesting story.

6
3
bac
aaada
e
3
aba
abcde
aba
2
baba
baba
4
ab
ab
c
bc
5
cbdca
d
a
d
e
3
b
c
ca

NoteIn the first test case of the example, all $$$3$$$ words can be used to make an interesting story. The interesting story is "bac aaada e".In the second test case of the example, the $$$1$$$-st and the $$$3$$$-rd words can be used to make an interesting story. The interesting story is "aba aba". Stephen can't use all three words at the same time.In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $$$0$$$.In the fourth test case of the example, Stephen can use the $$$3$$$-rd and the $$$4$$$-th words to make an interesting story. The interesting story is "c bc".

<?php
class IO {
    public static $aAlphabet = ['A','B','C','D', 'E', 'F', 'G', 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    //Input/Output
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function asLine($mArr, $sep = ' ') { echo implode($sep, $mArr) . PHP_EOL; }

    //Z-function
    public static function z($sString) {
        $s = str_split($sString);
        $n = count($s);
        $z = array_fill(0, $n, 0);
        $r = $l = 0;
        for ($i = 1; $i < $n; $i++) {
            if ($r >= $i) {
                $z[$i] = min($z[$i - $l], $r - $i + 1);
            }
            while ($z[$i] + $i < $n && $s[$z[$i]] == $s[$z[$i] + $i]) {
                $z[$i]++;
            }
            if ($i + $z[$i] - 1 > $r) {
                $l = $i;
                $r = $i + $z[$i] - 1;
            }
        }
        return $z;
    }
    public static function getCountOfDiffSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $n = count($s);
        $count = 0;
        $t = '';
        for ($i = $n - 1; $i >= 0; $i--) {
            $t = $s[$i] . $t;
            $count += $i + 1 - max(self::z($t));
        }
        return $count;
    }
    public static function getMinPeriodOfSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $res = count($s);
        $z = self::z($sString);
        foreach ($z as $iI => $sV) {
            if ((int) $iI + $z[$iI] == $res && $res % (int) $iI == 0) {
                $res = $iI;
                break;
            }
        }
        return $res;
    }

    //Other functions
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }

}

//$iQueryCount = 1;
$iQueryCount = IO::str();
while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $n = IO::str();
    $a = [];
    $m = [];
    $counter = 0;
    while($n > 0) {
        $n--;
        $a[$counter] = ['all' => 0];
        foreach (str_split(IO::str()) as $v) {
            if (isset($m[$v])) {
                $m[$v] += 1;
            } else {
                $m[$v] = 1;
            }
            if (isset($a[$counter][$v])) {
                $a[$counter][$v] += 1;
            } else {
                $a[$counter][$v] = 1;
            }
            $a[$counter]['all'] += 1;
        }
        $counter++;
    }

    $result = [];
    foreach ($m as $key => $valll) {
        $tt = [];
        foreach ($a as $word) {
            $tf = 0;
            if (isset($word[$key])) {
                $tf = $word[$key];
            }
            $tt[] = $tf - ($word['all'] - $tf);
        }
        rsort($tt);
        $res = 0;
        $value = 0;
        foreach ($tt as $vv) {
            $value += $vv;
            if ($value > 0) {
                $res++;
            } else {
                break;
            }
        }
        $result[] = $res;
    }

    IO::line(max($result));

}

?>

<?php
class IO {
    public static $aAlphabet = ['A','B','C','D', 'E', 'F', 'G', 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    //Input/Output
    public static function arr($sDelimeter = ' ') { return explode($sDelimeter, trim(fgets(STDIN))); }
    public static function str() { return trim(fgets(STDIN)); }
    public static function yes() { echo 'YES' . PHP_EOL; }
    public static function no() { echo 'NO' . PHP_EOL; }
    public static function line($sLine) { echo $sLine . PHP_EOL; }
    public static function asLine($mArr, $sep = ' ') { echo implode($sep, $mArr) . PHP_EOL; }

    //Z-function
    public static function z($sString) {
        $s = str_split($sString);
        $n = count($s);
        $z = array_fill(0, $n, 0);
        $r = $l = 0;
        for ($i = 1; $i < $n; $i++) {
            if ($r >= $i) {
                $z[$i] = min($z[$i - $l], $r - $i + 1);
            }
            while ($z[$i] + $i < $n && $s[$z[$i]] == $s[$z[$i] + $i]) {
                $z[$i]++;
            }
            if ($i + $z[$i] - 1 > $r) {
                $l = $i;
                $r = $i + $z[$i] - 1;
            }
        }
        return $z;
    }
    public static function getCountOfDiffSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $n = count($s);
        $count = 0;
        $t = '';
        for ($i = $n - 1; $i >= 0; $i--) {
            $t = $s[$i] . $t;
            $count += $i + 1 - max(self::z($t));
        }
        return $count;
    }
    public static function getMinPeriodOfSubstrWithZ($sString)
    {
        $s = str_split($sString);
        $res = count($s);
        $z = self::z($sString);
        foreach ($z as $iI => $sV) {
            if ((int) $iI + $z[$iI] == $res && $res % (int) $iI == 0) {
                $res = $iI;
                break;
            }
        }
        return $res;
    }

    //Other functions
    public static function gcd($a, $b) {
        return ($a % $b) ? self::gcd($b,$a % $b) : $b;
    }
    public static function lcm($x, $y) {
        $p = $x;
        $q = $y;
        while ($x != $y)
        {
            if ($x > $y)
                $x = $x - $y;
            else
                $y = $y - $x;
        }
        return ($p*$q)/$x;
    }

}

//$iQueryCount = 1;
$iQueryCount = IO::str();
while($iQueryCount > 0) {
    $iQueryCount--;
    solve();
}
function solve() {
    $n = IO::str();
    $a = [];
    $m = [];
    $counter = 0;
    while($n > 0) {
        $n--;
        $a[$counter] = ['all' => 0];
        foreach (str_split(IO::str()) as $v) {
            if (isset($m[$v])) {
                $m[$v] += 1;
            } else {
                $m[$v] = 1;
            }
            if (isset($a[$counter][$v])) {
                $a[$counter][$v] += 1;
            } else {
                $a[$counter][$v] = 1;
            }
            $a[$counter]['all'] += 1;
        }
        $counter++;
    }

    $result = [];
    foreach ($m as $key => $valll) {
        $tt = [];
        var_dump($a);
        foreach ($a as $word) {
            $tf = 0;
            if (isset($word[$key])) {
                $tf = $word[$key];
            }
            $tt[] = $tf - ($word['all'] - $tf);
        }
        rsort($tt);
        $res = 0;
        $value = 0;
        var_dump($tt);
        foreach ($tt as $vv) {
            $value += $vv;
            if ($value > 0) {
                $res++;
            } else {
                break;
            }
        }
        $result[] = $res;
    }

    IO::line(max($result));

}

?>


Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.

The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively. The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.

Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.

NoteIn the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:1. Move to floor 5: takes 2 seconds.2. Pick up passenger 3.3. Move to floor 3: takes 2 seconds.4. Wait for passenger 2 to arrive: takes 4 seconds.5. Pick up passenger 2.6. Go to floor 2: takes 1 second.7. Pick up passenger 1.8. Go to floor 0: takes 2 seconds.This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.

<?
//$input = fopen("input.txt","r");
//$output = fopen("output.txt","w");
//fscanf(STDIN,"%d %d",$n, $s);
//fprintf(STDOUT,"%d",$t);

$in = fopen("php://stdin","r");
list($n,$s) = explode(" ",trim(fgets($in)));
for($i = 0; $i <$n; $i++){
 list($f,$t) = explode(" ",trim(fgets($in)));
 $fl[$i] = $f;
 $ti[$i] = $t;
 $flag[$i] = false;
}

$time = 0; $tf = $s;
for($i = 0; $i <$n; $i++){
 $idx = -1; $temp = -1;
 for($r = 0; $r <$n; $r++){
    if($flag[$r]==false && $fl[$r] > $temp )
	{
	  $idx = $r;
	  $temp = $fl[$r];
	}
 }
 $flag[$idx] = true;
 while($tf > $fl[$idx])
 {
  $time++;
  $tf--;
 }
 while( $ti[$idx] > $time)
   $time++;
}
while($tf >0)
{
  $time++;
  $tf--;
}

echo $time;

<?
$in = fopen("php://stdin","r");
list($n,$s) = explode(" ",trim(fgets($in)));
for($i = 0; $i <$n; $i++){
 list($f,$t) = explode(" ",trim(fgets($in)));
 $fl[$i] = $f;
 $ti[$i] = $t;
 $flag[$i] = false;
}

$time = 0; $tf = $s;
for($i = 0; $i <$n; $i++){
 $idx = -1; $temp = -1;
 for($r = 0; $r <$n; $r++){
    if($flag[$r]==false && $fl[$r] > $temp )
	{
	  $idx = $r;
	  $temp = $fl[$r];
	}
 }
 $flag[$idx] = true;
 while($tf > $fl[$idx])
 {
  $time++;
  $tf--;
 }
 while( $ti[$idx] > $time)
   $time++;
}
while($tf >0)
{
  $time++;
  $tf--;
}

echo $time;


This problem is given in two editions, which differ exclusively in the constraints on the number $$$n$$$.You are given an array of integers $$$a[1], a[2], \dots, a[n].$$$ A block is a sequence of contiguous (consecutive) elements $$$a[l], a[l+1], \dots, a[r]$$$ ($$$1 \le l \le r \le n$$$). Thus, a block is defined by a pair of indices $$$(l, r)$$$.Find a set of blocks $$$(l_1, r_1), (l_2, r_2), \dots, (l_k, r_k)$$$ such that:  They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $$$(l_i, r_i)$$$ and $$$(l_j, r_j$$$) where $$$i \neq j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$.  For each block the sum of its elements is the same. Formally, $$$$$$a[l_1]+a[l_1+1]+\dots+a[r_1]=a[l_2]+a[l_2+1]+\dots+a[r_2]=$$$$$$ $$$$$$\dots =$$$$$$ $$$$$$a[l_k]+a[l_k+1]+\dots+a[r_k].$$$$$$  The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $$$(l_1', r_1'), (l_2', r_2'), \dots, (l_{k'}', r_{k'}')$$$ satisfying the above two requirements with $$$k' > k$$$.     The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.

The first line contains integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the given array. The second line contains the sequence of elements $$$a[1], a[2], \dots, a[n]$$$ ($$$-10^5 \le a_i \le 10^5$$$).

In the first line print the integer $$$k$$$ ($$$1 \le k \le n$$$). The following $$$k$$$ lines should contain blocks, one per line. In each line print a pair of indices $$$l_i, r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the bounds of the $$$i$$$-th block. You can print blocks in any order. If there are multiple answers, print any of them.


Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size $$$n \times n$$$ is called prime if the following three conditions are held simultaneously:   all numbers on the square are non-negative integers not exceeding $$$10^5$$$;  there are no prime numbers in the square;  sums of integers in each row and each column are prime numbers. Sasha has an integer $$$n$$$. He asks you to find any prime square of size $$$n \times n$$$. Sasha is absolutely sure such squares exist, so just help him!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the required size of a square.

For each test case print $$$n$$$ lines, each containing $$$n$$$ integers — the prime square you built. If there are multiple answers, print any.

<?php
function read(){
    return explode(" ",trim(fgets(STDIN)));
}
function is_prime($x){
    if($x==1){
        return false;
    }
    for($i=2;$i<$x;$i++){
        if($x%$i==0){
            return false;
        }
    }
    return true;
}
list($t)=read();
for($ii=0; $ii<$t;$ii++){
    list($n)=read();
    if($n==1){
        print "1\n";
        continue;
    }
    $ans=0;
    for(;;$ans++){
        if(is_prime($ans+$n)&&!is_prime(1+$ans)){
            break;
        }
    }
    for($i=0;$i<$n;$i++){
        for($j=0;$j<$n;$j++){
            if($i==$j)print (1+$ans)." ";
            else print "1 ";
        }
        print "\n";
    }
}

<?php
function read(){
    return explode(" ",trim(fgets(STDIN)));
}
list($t)=read();
for($ii=0; $ii<$t;$ii++){
    list($n)=read();
    for($i=0;$i<$n;$i++){
        for($j=0;$j<$n;$j++){
            if($i==$j)print (101-$n)." ";
            else print "1 ";
        }
        print("\n");
    }
}

<?php
function read(){
    return explode(" ",trim(fgets(STDIN)));
}
list($t)=read();
for($i=0; $i<$t;$i++){
    list($n)=read();
    for($i=0;$i<$n;$i++){
        for($j=0;$j<$n;$j++){
            if($i==$j)print("100 ");
            else print("1 ");
        }
        print("\n");
    }
}

<?php
function read(){
    return explode(" ",trim(fgets(STDIN)));
}
list($t)=read();
for($ii=0; $ii<$t;$ii++){
    list($n)=read();
    for($i=0;$i<$n;$i++){
        for($j=0;$j<$n;$j++){
            if($i==$j)print("100 ");
            else print("1 ");
        }
        print("\n");
    }
}


You are given a number $$$n$$$ (divisible by $$$3$$$) and an array $$$a[1 \dots n]$$$. In one move, you can increase any of the array elements by one. Formally, you choose the index $$$i$$$ ($$$1 \le i \le n$$$) and replace $$$a_i$$$ with $$$a_i + 1$$$. You can choose the same index $$$i$$$ multiple times for different moves.Let's denote by $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ the number of numbers from the array $$$a$$$ that have remainders $$$0$$$, $$$1$$$ and $$$2$$$ when divided by the number $$$3$$$, respectively. Let's say that the array $$$a$$$ has balanced remainders if $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ are equal.For example, if $$$n = 6$$$ and $$$a = [0, 2, 5, 5, 4, 8]$$$, then the following sequence of moves is possible:   initially $$$c_0 = 1$$$, $$$c_1 = 1$$$ and $$$c_2 = 4$$$, these values are not equal to each other. Let's increase $$$a_3$$$, now the array $$$a = [0, 2, 6, 5, 4, 8]$$$;  $$$c_0 = 2$$$, $$$c_1 = 1$$$ and $$$c_2 = 3$$$, these values are not equal. Let's increase $$$a_6$$$, now the array $$$a = [0, 2, 6, 5, 4, 9]$$$;  $$$c_0 = 3$$$, $$$c_1 = 1$$$ and $$$c_2 = 2$$$, these values are not equal. Let's increase $$$a_1$$$, now the array $$$a = [1, 2, 6, 5, 4, 9]$$$;  $$$c_0 = 2$$$, $$$c_1 = 2$$$ and $$$c_2 = 2$$$, these values are equal to each other, which means that the array $$$a$$$ has balanced remainders. Find the minimum number of moves needed to make the array $$$a$$$ have balanced remainders.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^4$$$) — the length of the array $$$a$$$. It is guaranteed that the number $$$n$$$ is divisible by $$$3$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 100$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$150\,000$$$.

For each test case, output one integer — the minimum number of moves that must be made for the $$$a$$$ array to make it have balanced remainders.

NoteThe first test case is explained in the statements.In the second test case, you need to make one move for $$$i=2$$$.The third test case you need to make three moves:   the first move: $$$i=9$$$;  the second move: $$$i=9$$$;  the third move: $$$i=2$$$. In the fourth test case, the values $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ initially equal to each other, so the array $$$a$$$ already has balanced remainders.


There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it's favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.

The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime. The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.

Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.  Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

NoteA prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows: (1, 420, 420420): note that s0·s1 = 420, s1·s2 = 176576400, and s2·s0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars. (1, 420, 420421): now, the product s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1 will receive 2000. The total is 4000. (1, 421, 420420): total is 4000  (1, 421, 420421): total is 0.  (2, 420, 420420): total is 6000.  (2, 420, 420421): total is 6000.  (2, 421, 420420): total is 6000. (2, 421, 420421): total is 4000.The expected value is .In the second sample, no combination of quantities will garner the sharks any money.

<?php
list ($n, $p) = explode(" ", trim(fgets(STDIN)));

$pSharks = [];
for ($i = 0; $i < $n; $i++) {
  list ($l, $r) = explode(" ", trim(fgets(STDIN)));
  if ($l > $r) {
    list ($l, $r) = [$r, $l];
  }
  $numTotal = $r - $l + 1;

  $lP = ceil($l / $p) * $p;
  $rP = floor($r / $p) * $p;
  if ($lP > $rP) {
    $numOk = 0;
  } else {
    $numOk = ($rP - $lP) / $p + 1;
  }

  $pSharks[] = $numOk / $numTotal;
}

$rub = 0;
foreach ($pSharks as $i => $p1) {
  $p2 = $pSharks[($i + 1) % $n];
  $rub += 2000 * ($p1 + $p2 - $p1 * $p2);
}

printf("%f\n", $rub);


In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.Write a program to find the minimum number of seconds needed to balance the load of servers.

The first line contains positive number n (1 ≤ n ≤ 105) — the number of the servers.  The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≤ mi ≤ 2·104), where mi is the number of tasks assigned to the i-th server.

Print the minimum number of seconds required to balance the load.

NoteIn the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.In the second example the load is already balanced.A possible sequence of task movements for the third example is:  move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5);  then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4);  then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3). The above sequence is one of several possible ways to balance the load of servers in three seconds.


Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.There are $$$n$$$ solutions, the $$$i$$$-th of them should be tested on $$$a_i$$$ tests, testing one solution on one test takes $$$1$$$ second. The solutions are judged in the order from $$$1$$$ to $$$n$$$. There are $$$k$$$ testing processes which test solutions simultaneously. Each of them can test at most one solution at a time.At any time moment $$$t$$$ when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id $$$i$$$, then it is being tested on the first test from time moment $$$t$$$ till time moment $$$t + 1$$$, then on the second test till time moment $$$t + 2$$$ and so on. This solution is fully tested at time moment $$$t + a_i$$$, and after that the testing process immediately starts testing another solution.Consider some time moment, let there be exactly $$$m$$$ fully tested solutions by this moment. There is a caption "System testing: $$$d$$$%" on the page with solutions, where $$$d$$$ is calculated as$$$$$$d = round\left(100\cdot\frac{m}{n}\right),$$$$$$where $$$round(x) = \lfloor{x + 0.5}\rfloor$$$ is a function which maps every real to the nearest integer.Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test $$$q$$$, and the caption says "System testing: $$$q$$$%". Find the number of interesting solutions.Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter.

The first line contains two positive integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1000$$$, $$$1 \le k \le 100$$$) standing for the number of submissions and the number of testing processes respectively. The second line contains $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 150$$$), where $$$a_i$$$ is equal to the number of tests the $$$i$$$-th submission is to be run on.

Output the only integer — the number of interesting submissions.

NoteConsider the first example. At time moment $$$0$$$ both solutions start testing. At time moment $$$49$$$ the first solution is fully tested, so at time moment $$$49.5$$$ the second solution is being tested on the test $$$50$$$, and the caption says "System testing: $$$50$$$%" (because there is one fully tested solution out of two). So, the second solution is interesting.Consider the second example. At time moment $$$0$$$ the first and the second solutions start testing. At time moment $$$32$$$ the first solution is fully tested, the third solution starts testing, the caption says "System testing: $$$25$$$%". At time moment $$$32 + 24.5 = 56.5$$$ the third solutions is being tested on test $$$25$$$, the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment $$$32 + 33 = 65$$$, the fourth solution is fully tested at time moment $$$65 + 1 = 66$$$. The captions becomes "System testing: $$$75$$$%", and at time moment $$$74.5$$$ the second solution is being tested on test $$$75$$$. So, this solution is also interesting. Overall, there are two interesting solutions.


Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player. Alex is a perfectionist, so he decided to get as many points as possible. Help him.

The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex's sequence.  The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).

Print a single integer — the maximum number of points that Alex can earn.

NoteConsider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points.

<?php
$n = fgets(STDIN);
$mass = explode(" ",trim(fgets(STDIN)));
$len = count($mass);
$max = max($mass);
$cnt = array_fill(0,$max+1,0);
for ($i = 0;$i<$len;$i++)
{
    $cnt[$mass[$i]]++;
}
$dp[0]=0;
$dp[1]=$cnt[1];
for ($i = 2;$i<=$max;$i++)
{
    $dp[$i] = max($dp[$i-1],$dp[$i-2]+$cnt[$i]*$i);
}
echo $dp[$max];

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);

$n_1 = 0;
$n_2 = 0;
$sum = 0;

foreach ($arr_N as $key => $value) {
    $n_tmp = $n_1;
    $n_1 = max($n_1, $n_2 + $key * $value);
    $n_2 = $n_tmp;

    if (!(isset($arr_N[$key+1]))) {
        $sum += $n_1;
        $n_1 = 0;
        $n_2 = 0;
    }
}

$sum += $n_1;

echo "$sum\n";

?>

<?php
$n = trim(fgets(STDIN));
$line = explode(" ",fgets(STDIN));
function dp(array $array)
{
    $length = count($array);
    if ($length==1)
    {
        return $array[0];
    }
    elseif ($length==0)
    {
        return 0;
    }
    else
    {
        return max(dp(array_slice($array,0,$length-1)),dp(array_slice($array,0,$length-2))+$array[$length-1]);
    }
}
echo dp($line);

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        //$x = (int) $key * $value;
        $x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
        if (!isset($arr_N[$key+1])) {
            break;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
print_r($arr_N);

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

        if (!isset($arr_N[$key-1])) {
            if (isset($arr_N[$key+2])) {
                continue;
            } else {
                $imax =($key * $value > ($key+1) * $arr_N[$key+1])? $key : $key + 1;
                break;
            }
        }

        $x = (int) $key * $value;
        $x -= (int) ($key - 1) * $arr_N[$key-1];
        $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($x > 0) {
            $imax = $key;
        } else {
            $imax = $key - 1;
        }
        break;
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
echo "arrCount=$arrCount \n";
print_r($arr_N);

}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {
        $x = (int) $key * $value;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount --;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        $x = (int) $key * $value;
        //$x = 0;
        //if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        //if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        // $x = (int) $key * $value;
        $x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        $x = (int) $key * $value;
        //$x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
        if (!isset($arr_N[$key+1])) {
            break;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

        $x = (int) $key * $value;
        //$x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($x > 0) {
            $imax = $key;
        }
        if (!isset($arr_N[$key+1])) {
            break;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
print_r($arr_N);

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        $x = (int) $key * $value;
        //$x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
        if (!isset($arr_N[$key+1])) {
            break;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);

if ($n == 100) {
    foreach ($arr_N as $key => $value) {
        echo "key=$key value=$value ";
    }
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;

    reset($arr_N);
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

        if (!isset($arr_N[$key-1])) {
            if (isset($arr_N[$key+2])) {
                continue;
            } else {
                $imax =($key * $value > ($key+1) * $arr_N[$key+1])? $key : $key + 1;
                break;
            }
        }

        $x = (int) $key * $value;
        $x -= (int) ($key - 1) * $arr_N[$key-1];
        $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($x > 0) {
            $imax = $key;
        } else {
            $imax = $key - 1;
        }
        break;
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
//echo "arrCount=$arrCount \n";
//print_r($arr_N);

}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$n_1 = 0;
$n_2 = 0;

foreach ($arr_N as $key => $value) {
    $n_tmp = $n_1;
    $n_1 = max($n_1, $n_2 + $key * $value);
    $n_2 = $n_tmp;
}

echo "$n_1\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 2;
$arrCount = count($arr_N);
while ( $arrCount > 0) {
   $arrCount --;

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {
        $x = ((int) $key * $value) - ((int) ($key - 1) * $arr_N[$key-1]) - ((int) ($key + 1) * $arr_N[$key+1]);
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }
/*
    $col += $imax * $arr_N[$imax];
    //echo " imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount --;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
*/
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        $x = (int) $key * $value;
        //$x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
        if (!isset($arr_N[$key+1])) {
            break;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {
        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
        }

        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;

    reset($arr_N);
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

        if (!isset($arr_N[$key-1])) {
            if (isset($arr_N[$key+2])) {
                continue;
            } else {
                $imax =($key * $value > ($key+1) * $arr_N[$key+1])? $key : $key + 1;
                break;
            }
        }

        $x = (int) $key * $value;
        $x -= (int) ($key - 1) * $arr_N[$key-1];
        $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($x > 0) {
            $imax = $key;
        } else {
            $imax = $key - 1;
        }
        break;
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
//echo "arrCount=$arrCount \n";
//print_r($arr_N);

}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$n_1 = 0;
$n_2 = 0;

ksort($arr_N);

foreach ($arr_N as $key => $value) {
    $n_tmp = $n_1;
    $n_1 = max($n_1, $n_2 + $key * $value);
    $n_2 = $n_tmp;
}

echo "$n_1\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

        if (!isset($arr_N[$key-1])) {
            if (isset($arr_N[$key+2])) {
                continue;
            } else {
                $imax =($key * $value > ($key+1) * $arr_N[$key+1])? $key : $key + 1;
                break;
            }
        }

        $x = (int) $key * $value;
        $x -= (int) ($key - 1) * $arr_N[$key-1];
        $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($x > 0) {
            $imax = $key;
        } else {
            $imax = $key - 1;
        }
        break;
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
//echo "arrCount=$arrCount \n";
//print_r($arr_N);

}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {

        if (!(isset($arr_N[$key-1]) or isset($arr_N[$key+1]))) {
            $col += $key * $value;
            $arrCount -- ;
            unset($arr_N[$key]);
            continue;
        }

/*        $x = (int) $key;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
*/
        $x = (int) $key * $value;
        $x = 0;
        if (isset($arr_N[$key-1])) $x -= (int) ($key - 1) * $arr_N[$key-1];
        if (isset($arr_N[$key+1])) $x -= (int) ($key + 1) * $arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo "\n imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount -- ;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$n_2 = 0;
$keys = array_keys($arr_N);
$n_1 = $arr_N[$keys[0]];
$n_0 = $n_1;

foreach ($arr_N as $key => $value) {

    if ($key = $keys[0]) continue;

    $n_tmp = $n_0;
    $n_0 = max($n_1, $n_2 + $value);
    $n_2 = $n_1;
    $n_1 = $n_tmp;
}

echo "$n_0\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");
//print_r($arr_N);
/*
$arr_N = array();
$arr_N[1] = 7;
$arr_N[2] = 9;
$arr_N[3] = 6;
$arr_N[4] = 12;
$arr_N[5] = 11;
$arr_N[6] = 10;
$arr_N[7] = 16;
$arr_N[8] = 10;
$arr_N[9] = 10;
$arr_N[10] = 9;
*/
$n_1 = 0;
$n_2 = 0;

foreach ($arr_N as $key => $value) {
    $n_tmp = $n_1;
    $n_1 = max($n_1, $n_2 + $key * $value);
    $n_2 = $n_tmp;
    //echo "\n n_2=$n_2 n_1=$n_1 key=$key value=$value\n";

}

echo "$n_1\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

$n_1 = 0;
$n_2 = 0;

function cmp($a, $b)
{
    if ((int) $a > (int) $b) {
        return 1;
    } elseif ((int) $a < (int) $b) {
        return -1;
    } elseif ((int) $a == (int) $b) {
        return 0;
    }
}

uksort($arr_N, "cmp");

foreach ($arr_N as $key => $value) {
    $n_tmp = $n_1;
    $n_1 = max($n_1, $n_2 + $key * $value);
    $n_2 = $n_tmp;
}

echo "$n_1\n";

?>

<?php
// 455A     Скука
$in = fopen("php://stdin", 'r');

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

$input_line = fgets($in);
$arr = preg_split('/ /', substr($input_line, 0, strlen($input_line)-2), -1, PREG_SPLIT_NO_EMPTY);

$arr_N = array();

foreach ($arr as $key => $value) {
    $i = (int) $value;
    $arr_N[$i] ++;
}

echo "2\n";
/*
$col = 0;
$arrCount = count($arr_N);
while ( $arrCount > 0) {

    $max = - 2000000000;
    $imax = -1;
    foreach ($arr_N as $key => $value) {
        $x = (int) $key * $value - ($key - 1) * arr_N[$key-1] - ($key + 1) * arr_N[$key+1];
        if ($max < $x) {
            $max = $x;
            $imax = $key;
        }
    }

    $col += $imax * $arr_N[$imax];
    //echo " imax=$imax max=$max col=$col arrCount=$arrCount\n";

    $arrCount --;
    unset($arr_N[$imax]);
    if (isset($arr_N[$imax-1])) {
        unset($arr_N[$imax-1]);
        $arrCount --;
    }
    if (isset($arr_N[$imax+1])) {
        unset($arr_N[$imax+1]);
        $arrCount --;
    }
}

echo "$col\n";
*/
?>


Polycarp is playing a new computer game. This game has $$$n$$$ stones in a row. The stone on the position $$$i$$$ has integer power $$$a_i$$$. The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.For example, if $$$n = 5$$$ and $$$a = [1, 5, 4, 3, 2]$$$, then Polycarp could make the following moves:   Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$;  Destroy the rightmost stone. After this move $$$a = [5, 4, 3]$$$;  Destroy the leftmost stone. After this move $$$a = [4, 3]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example:   Destroy the leftmost stone. After this move $$$a = [5, 4, 3, 2]$$$;  Destroy the leftmost stone. After this move $$$a = [4, 3, 2]$$$. Polycarp destroyed the stones with the greatest and least power, so he can end the game.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 100$$$) — the number of stones. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the power of the stones.

For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power.

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

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

for ($i = 0; $i < $t; $i++) {
  $n = trim(fgets($file));
  $a = explode(" ", trim(fgets($file)));
  $d = 0; $c = 0;
  $min = min($a);
  $max = max($a);
  $b = $a;

  $key_min = array_search($min, $a);
  $key_max = array_search($max, $a);

  $k_min = min($key_min, $key_max);
  $k_max = max($key_min, $key_max);

  $f1 = $k_max + 1;

  $f2 = ($k_min + 1) + $n - $k_min + 1;

  $f4 = $k_min + 1;
  $f5 = $k_max + 1;

  $b = array_reverse($b);
  $key_min = array_search($min, $b);
  $key_max = array_search($max, $b);

  $k_min = min($key_min, $key_max);
  $k_max = max($key_min, $key_max);
  $f3 = $k_max + 1;

  $f4 += $k_min + 1;
  $f5 += $k_max + 1;

  echo min($f1, $f2, $f3, $f4, $f5).PHP_EOL;
}

?>


Is it rated?Here it is. The Ultimate Question of Competitive Programming, Codeforces, and Everything. And you are here to answer it.Another Codeforces round has been conducted. No two participants have the same number of points. For each participant, from the top to the bottom of the standings, their rating before and after the round is known.It's known that if at least one participant's rating has changed, then the round was rated for sure.It's also known that if the round was rated and a participant with lower rating took a better place in the standings than a participant with higher rating, then at least one round participant's rating has changed.In this problem, you should not make any other assumptions about the rating system.Determine if the current round is rated, unrated, or it's impossible to determine whether it is rated of not.

The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of round participants. Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 4126) — the rating of the i-th participant before and after the round, respectively. The participants are listed in order from the top to the bottom of the standings.

If the round is rated for sure, print "rated". If the round is unrated for sure, print "unrated". If it's impossible to determine whether the round is rated or not, print "maybe".

NoteIn the first example, the ratings of the participants in the third and fifth places have changed, therefore, the round was rated.In the second example, no one's rating has changed, but the participant in the second place has lower rating than the participant in the fourth place. Therefore, if the round was rated, someone's rating would've changed for sure.In the third example, no one's rating has changed, and the participants took places in non-increasing order of their rating. Therefore, it's impossible to determine whether the round is rated or not.

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

for($i=0;$i<$n;$i++)
{
    $tmp = trim(fgets(STDIN));
    $token = strtok($tmp," ");
    $a[$i] = $token;
    $token = strtok(" ");
    $b[$i] = $token;
    if($i>0 and $a[$i]>$a[$i-1])
        $flag = true;
}
$flag2 = false;
for($i=0;$i<$n;$i++)
    if($a[$i]!=$b[$i])
        $flag2 = true;
if($flag2==true)
    fwrite(STDOUT, "rated\n");
else if($flag==true)
    fwrite(STDOUT, "unrated\n");
else
    fwrite(STDOUT, "maybe\n");

?>

<?php
$in = fopen('php://stdin', "r");
$out = fopen('php://stdout', "w");

fscanf($in, "%d", $n);
$unrated=0;
$rated  =0;
$maybe  =0;
if($n<2){
    exit();
}
for($i=1;$i<=$n;$i++){
    fscanf($in,"%d %d",$rating[$i]['before'],$rating[$i]['after']);
    if(($rating[$i]['after']<1 or $rating[$i]['before']<1) || ($rating[$i]['after']>4126 or $rating[$i]['before']>4126)){ exit();}
    if($rating[$i]['before']!=$rating[$i]['after']){

        $rated=1;
        $unrated=0;
        $maybe=0;
        break;
    }

    if($i>1){

             if($rating[$i]['after']<=$rating[$i-1]['after']){
                $maybe+=1;
             }else{
                 $unrated+=1;
             }

    }
}

if($rated >$unrated){
    fwrite($out,'rated');
}elseif ($unrated>$rated){
    fwrite($out, 'unrated');
}else{
    fwrite($out,'maybe');
}
fclose($out);
fclose($in);


Screen resolution of Polycarp's monitor is $$$a \times b$$$ pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates $$$(x, y)$$$ ($$$0 \le x < a, 0 \le y < b$$$). You can consider columns of pixels to be numbered from $$$0$$$ to $$$a-1$$$, and rows — from $$$0$$$ to $$$b-1$$$.Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.

In the first line you are given an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. In the next lines you are given descriptions of $$$t$$$ test cases. Each test case contains a single line which consists of $$$4$$$ integers $$$a, b, x$$$ and $$$y$$$ ($$$1 \le a, b \le 10^4$$$; $$$0 \le x < a$$$; $$$0 \le y < b$$$) — the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that $$$a+b>2$$$ (e.g. $$$a=b=1$$$ is impossible).

Print $$$t$$$ integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.

NoteIn the first test case, the screen resolution is $$$8 \times 8$$$, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.


The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.

The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.

If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).

NoteIn the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.

<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$n = readNum($stream);
	$dayCount = readStringExplodeNum($stream, ' ', true);

	$dayCheck = 0;
	$isFail = false;
	while (isset($dayCount[$dayCheck])) {
		if ($dayCount[$dayCheck] % 2 == 0) {
			$dayCount[$dayCheck]%=2;
		} elseif ($dayCount[$dayCheck] > 0 && $dayCount[$dayCheck + 1] > 0) {
			$dayCount[$dayCheck] --;
			$dayCount[$dayCheck + 1] --;
		} elseif ($dayCount[$dayCheck] > 0) {
			$isFail = true;
			echo 'NO';
			break;
		}

		if ($dayCount[$dayCheck] == 0) {
			$dayCheck++;
		}
	}
	if ($isFail === false) {
		echo 'YES';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$n = readNum($stream);
	$dayCount = readStringExplodeNum($stream, ' ', true);
	$input = array_chunk($dayCount, 100);
	echo '-----------------' . "\n";
	foreach ($input as $in) {
		echo implode(',', $in) . "\n";
	}
	echo '-----------------' . "\n";
	$dayCheck = 0;
	$isFail = false;
	while (isset($dayCount[$dayCheck])) {
		if ($dayCount[$dayCheck] >= 2) {
			$dayCount[$dayCheck]-=2;
		} elseif ($dayCount[$dayCheck] > 0 && $dayCount[$dayCheck + 1] > 0) {
			$dayCount[$dayCheck] --;
			$dayCount[$dayCheck + 1] --;
		} elseif ($dayCount[$dayCheck] > 0) {
			$isFail = true;
			echo 'NO';
			break;
		}

		if ($dayCount[0] == 0) {
			$dayCheck++;
		}
	}
	if ($isFail === false) {
		echo 'YES';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$n = readNum($stream);
	$dayCount = readStringExplodeNum($stream, ' ', true);
	$dayCheck = 0;
	$isFail = false;
	while (isset($dayCount[$dayCheck])) {
		if ($dayCount[$dayCheck] > 0 && $dayCount[$dayCheck + 1] > 0) {
			$dayCount[$dayCheck] --;
			$dayCount[$dayCheck + 1] --;
		} elseif ($dayCount[$dayCheck] >= 2) {
			$dayCount[$dayCheck]-=2;
		} elseif ($dayCount[$dayCheck] > 0) {
			$isFail = true;
			echo 'NO';
			break;
		}

		if ($dayCount[0] == 0) {
			$dayCheck++;
		}
	}
	if ($isFail === false) {
		echo 'YES';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$n = readNum($stream);
	$dayCount = readStringExplodeNum($stream, ' ', true);
	if ($n > 1000) {
		$input = array_chunk($dayCount, 100);
		echo '-----------------' . "\n";
		foreach ($input as $in) {
			echo implode(',', $in) . "\n";
		}
		echo '-----------------' . "\n";
	}
	$dayCheck = 0;
	$isFail = false;
	while (isset($dayCount[$dayCheck])) {
		if ($dayCount[$dayCheck] >= 2) {
			$dayCount[$dayCheck]-=2;
		} elseif ($dayCount[$dayCheck] > 0 && $dayCount[$dayCheck + 1] > 0) {
			$dayCount[$dayCheck] --;
			$dayCount[$dayCheck + 1] --;
		} elseif ($dayCount[$dayCheck] > 0) {
			$isFail = true;
			echo 'NO';
			break;
		}

		if ($dayCount[0] == 0) {
			$dayCheck++;
		}
	}
	if ($isFail === false) {
		echo 'YES';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();

<?php

###### IO ######
error_reporting(E_ALL);
error_reporting(0);

function readString($f) {
	$r = array(
		"\r" => '',
		"\n" => '',
	);
	return strtr(fgets($f), $r);
}

function readNumString($f) {
	return trim(fgets($f));
}

function stringToArray($str) {
	return str_split($str);
}

function readNum($f, $is_int = false) {
	$v = readNumString($f);
	return $is_int ? intval($v) : floatval($v);
}

function readStringExplode($f, $delimiter = ' ') {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = 'trim';
	return array_map($callf, $return);
}

function readStringExplodeNum($f, $delimiter = ' ', $is_int = false) {
	$v = readNumString($f);
	if ($delimiter == '') {
		$return = stringToArray($v);
	} else {
		$return = explode($delimiter, $v);
	}
	$callf = $is_int ? 'intval' : 'floatval';
	return array_map($callf, $return);
}

###### /IO ######

function getAnswer($is_test = false, $f = false) {
	if ($is_test)
		$stream = $f;
	else
		$stream = STDIN;
	/* begin body */
	$n = readNum($stream);
	$dayCount = readStringExplodeNum($stream, ' ', true);
	$dayCheck = 0;
	$isFail = false;
	while (isset($dayCount[$dayCheck])) {
		if ($dayCount[$dayCheck] >= 2) {
			$dayCount[$dayCheck]-=2;
		} elseif ($dayCount[$dayCheck] > 0 && $dayCount[$dayCheck + 1] > 0) {
			$dayCount[$dayCheck] --;
			$dayCount[$dayCheck + 1] --;
		} elseif ($dayCount[$dayCheck] > 0) {
			$isFail = true;
			echo 'NO';
			break;
		}

		if ($dayCount[0] == 0) {
			$dayCheck++;
		}
	}
	if ($isFail === false) {
		echo 'YES';
	}
	/* / end body */
}

if (empty($is_test))
	getAnswer();


You are given two positive integers $$$a$$$ and $$$b$$$.In one move, you can change $$$a$$$ in the following way:  Choose any positive odd integer $$$x$$$ ($$$x > 0$$$) and replace $$$a$$$ with $$$a+x$$$;  choose any positive even integer $$$y$$$ ($$$y > 0$$$) and replace $$$a$$$ with $$$a-y$$$. You can perform as many such operations as you want. You can choose the same numbers $$$x$$$ and $$$y$$$ in different moves.Your task is to find the minimum number of moves required to obtain $$$b$$$ from $$$a$$$. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.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. Then $$$t$$$ test cases follow. Each test case is given as two space-separated integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^9$$$).

For each test case, print the answer — the minimum number of moves required to obtain $$$b$$$ from $$$a$$$ if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain $$$b$$$ from $$$a$$$.

NoteIn the first test case, you can just add $$$1$$$.In the second test case, you don't need to do anything.In the third test case, you can add $$$1$$$ two times.In the fourth test case, you can subtract $$$4$$$ and add $$$1$$$.In the fifth test case, you can just subtract $$$6$$$.

<?php

$n = (int) fgets(STDIN);

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

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

    foreach($input as &$val){
        $val = (int) trim($val);
    }

    $answer = numberOfMoves($input[0], $input[1]);

    print($answer);
    echo "\n";
}

function numberOfMoves($a, $b){

    $diff = abs($a - $b);

    $ans = $a == $b ? 0 : 1;

    $ans = $b > $a && $diff % 2 == 0 ? 2 : $ans;
    $ans = $b < $a && $diff % 2 == 1 ? 2 : $ans;

    return $ans;
}

<?php
list($m) = explode(' ', trim(fgets(STDIN)));
for($k = 0; $k < $m; $k++) {
    list($a,$b) = explode(' ', trim(fgets(STDIN)));
    if ($a == $b) {
        echo "0\n";
    } else {
        $aFlag = false;
        $bFlag = false;
        if ($a%2 == 0) {
            $aFlag = true;
        }
        if ($b%2 == 0) {
            $bFlag = true;
        }
        if ($a < $b) {
            if ($aFlag == $bFlag && $aFlag) {
                echo "2\n";
            } elseif ($aFlag == $bFlag && !$aFlag) {
                echo "1\n";
            } else {
                if ($aFlag) {
                    echo "1\n";
                } else {
                    echo "2\n";
                }
            }
        } else {
            if ($aFlag == $bFlag && $aFlag) {
                echo "1\n";
            } elseif ($aFlag == $bFlag && !$aFlag) {
                echo "1\n";
            } else {
                if ($aFlag) {
                    echo "1\n";
                } else {
                    echo "2\n";
                }
            }
        }
    }
   // echo "k = $k\n";
}

<?php
list($m) = explode(' ', trim(fgets(STDIN)));
for($k = 0; $k < $m; $k++) {
    list($a,$b) = explode(' ', trim(fgets(STDIN)));
    if ($a == $b) {
        echo "0\n";
    } else {
        $aFlag = false;
        $bFlag = false;
        if ($a%2 == 0) {
            $aFlag = true;
        }
        if ($b%2 == 0) {
            $bFlag = true;
        }
        $c = $a - $b;
        if ($b > $a) {
            if ($aFlag == $bFlag && $bFlag) {
                echo "2\n";
            } elseif ($aFlag == $bFlag && !$bFlag) {
                echo "1\n";
            } else {
                if ($aFlag) {
                    echo "1\n";
                } else {
                    echo "2\n";
                }
            }
        } else {
            if ($aFlag == $bFlag && $bFlag) {
                echo "2\n";
            } elseif ($aFlag == $bFlag && !$bFlag) {
                echo "1\n";
            } else {
                if ($aFlag) {
                    echo "1\n";
                } else {
                    echo "2\n";
                }
            }
        }
    }
   // echo "k = $k\n";
}

<?php
list($m) = explode(' ', trim(fgets(STDIN)));
for($k = 0; $k < $m; $k++) {
    list($a,$b) = explode(' ', trim(fgets(STDIN)));
    if ($a == $b) {
        echo "0\n";
    } else {
        $aFlag = false;
        $bFlag = false;
        if ($a%2 == 0) {
            $aFlag = true;
        }
        if ($b%2 == 0) {
            $bFlag = true;
        }
        if ($a < $b) {
            if ($aFlag == $bFlag && $aFlag) {
                echo "1\n";
            } elseif ($aFlag == $bFlag && !$aFlag) {
                echo "1\n";
            } else {
                if ($aFlag) {
                    echo "1\n";
                } else {
                    echo "2\n";
                }
            }
        } else {
            if ($aFlag == $bFlag && $aFlag) {
                echo "1\n";
            } elseif ($aFlag == $bFlag && !$aFlag) {
                echo "1\n";
            } else {
                if ($aFlag) {
                    echo "1\n";
                } else {
                    echo "2\n";
                }
            }
        }
    }
   // echo "k = $k\n";
}


Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x × y matrix c which has the following properties:  the upper half of matrix c (rows with numbers from 1 to x) exactly matches b;  the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1). Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?

The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.

In the single line, print the answer to the problem — the minimum number of rows of matrix b.

NoteIn the first test sample the answer is a 2 × 3 matrix b:001110If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:001110110001

<?php
$in = fopen('php://stdin', 'r');
//$in = fopen('B.in', 'r');

$mn = explode(" ", rtrim(fgets($in)));
$m = $mn[0];
$n = $mn[1];

if($m%2==1){
    echo $m;
}
else{
    $c= array();
    for($l=0; $l<$m;$l++){
        $c[] = explode(" ", rtrim(fgets($in)));
    }
    while($m%2==0){
        $d = $m/2;
        if(array_slice($c,0,$d) == array_reverse(array_slice($c,$d,$d)))
            $m = $d;
        else
            break;
    }
    echo $m;
}


There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 ≤ i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible. Help the children and tell them how to form the line.

The single line of the input contains two integers n and m (1 ≤ n, m ≤ 100), separated by a space.

Print a line of n + m characters. Print on the i-th position of the line character "B", if the i-th position of your arrangement should have a boy and "G", if it should have a girl.  Of course, the number of characters "B" should equal n and the number of characters "G" should equal m. If there are multiple optimal solutions, print any of them.

NoteIn the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal.

<?php
    //$in = fopen("php://stdin","r");
    $in = fopen("input.txt","r");
    $out = fopen("output.txt","w");

    $line = explode(" ",fgets($in));
    $n = (int)$line[0];
    $m = (int)$line[1];

    while ($n>0 && $m>0) {
        if ($n>$m) {
            fwrite($out,"BG");
        } else {
            fwrite($out,"GB");
        }
        $n--;
        $m--;
    }
    while($n>0) {
        fwrite($out,"B");
        $n--;
    }
    while($m>0) {
        fwrite($out,"G");
        $m--;
    }
    fclose($out);
?>

<?php
    $fin = fopen('input.txt', 'r');
    $fout = fopen('output.txt', 'w');
    fscanf($fin, "%d%d", $n, $m);
    if ($n < $m)
    {
        fprintf($fout, "G");
        for ($i = 1; $i <= $n; $i++)
            fprintf($fout, "BG");
        for ($i = 1; $i <= $m-$n-1; $i++)
            fprintf($fout, "G");
    }
    else if ($n > $m)
    {
        fprintf($fout, "B");
        for ($i = 1; $i <= $m; $i++)
            fprintf($fout, "GB");
        for ($i = 1; $i <= $n-$m-1; $i++)
            fprintf($fout, "B");
    }
    else
    {
        for ($i = 1; $i <= $n; $i++)
            fprintf($fout, "GB");
    }
?>

<?php

$f = fopen('input.txt', 'r');

fscanf($f, '%d %d', $n, $m);
$output = '';

$min = min(array($n, $m));
$max = max(array($n, $m));
for ($i = 0; $i < $min; $i++) {
    if ($n === $max) {
        $output .= 'BG';
    } else {
        $output .= 'GB';
    }
}

for ($i = $min; $i < $max; $i++) {
    if ($n === $max) {
        $output .= 'B';
    } else {
        $output .= 'G';
    }
}

$output .= PHP_EOL;

file_put_contents('output.txt', $output);


The grasshopper is located on the numeric axis at the point with coordinate $$$x_0$$$.Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate $$$x$$$ with a distance $$$d$$$ to the left moves the grasshopper to a point with a coordinate $$$x - d$$$, while jumping to the right moves him to a point with a coordinate $$$x + d$$$.The grasshopper is very fond of positive integers, so for each integer $$$i$$$ starting with $$$1$$$ the following holds: exactly $$$i$$$ minutes after the start he makes a jump with a distance of exactly $$$i$$$. So, in the first minutes he jumps by $$$1$$$, then by $$$2$$$, and so on.The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right.For example, if after $$$18$$$ consecutive jumps he arrives at the point with a coordinate $$$7$$$, he will jump by a distance of $$$19$$$ to the right, since $$$7$$$ is an odd number, and will end up at a point $$$7 + 19 = 26$$$. Since $$$26$$$ is an even number, the next jump the grasshopper will make to the left by a distance of $$$20$$$, and it will move him to the point $$$26 - 20 = 6$$$.Find exactly which point the grasshopper will be at after exactly $$$n$$$ jumps.

The first line of input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$x_0$$$ ($$$-10^{14} \leq x_0 \leq 10^{14}$$$) and $$$n$$$ ($$$0 \leq n \leq 10^{14}$$$) — the coordinate of the grasshopper's initial position and the number of jumps.

Print exactly $$$t$$$ lines. On the $$$i$$$-th line print one integer — the answer to the $$$i$$$-th test case — the coordinate of the point the grasshopper will be at after making $$$n$$$ jumps from the point $$$x_0$$$.

NoteThe first two test cases in the example correspond to the first two jumps from the point $$$x_0 = 0$$$. Since $$$0$$$ is an even number, the first jump of length $$$1$$$ is made to the left, and the grasshopper ends up at the point $$$0 - 1 = -1$$$.Then, since $$$-1$$$ is an odd number, a jump of length $$$2$$$ is made to the right, bringing the grasshopper to the point with coordinate $$$-1 + 2 = 1$$$.

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

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

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

	$v = bcsub($n, bcmod($n, 4)) + 1;

	for ($j = $v; $j <= $n; $j++) {
	  if (bcmod($x, 2) == 0) {
	    $x = bcsub($x, $j);
	  } else {
	    $x = bcadd($x, $j);
	  }
	}
	echo $x.PHP_EOL;
}

?>

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

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

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

	$v = $n - $n % 4 + 1;

	for ($j = $v; $j <= $n; $j++) {
	  if (bcmod($x, 2) == 0) {
	    $x -= $j;
	  } else {
	    $x += $j;
	  }
	}
	echo $x.PHP_EOL;
}

?>

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

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

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

	for ($j = $n - $n % 4 + 1; $j <= $n; $j++) {
	  if ($x % 2 == 0) {
	    $x -= $j;
	  } else {
	    $x += $j;
	  }
	}
	echo $x.PHP_EOL;
}

?>


And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All n participants who have made it to the finals found themselves in a huge m-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?".The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then — to the 3-rd floor and so on until it reaches the m-th floor. After that the elevator moves to floor m - 1, then to floor m - 2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.For each of the n participant you are given si, which represents the floor where the i-th participant starts, fi, which represents the floor the i-th participant wants to reach, and ti, which represents the time when the i-th participant starts on the floor si.For each participant print the minimum time of his/her arrival to the floor fi. If the elevator stops on the floor si at the time ti, then the i-th participant can enter the elevator immediately. If the participant starts on the floor si and that's the floor he wanted to reach initially (si = fi), then the time of arrival to the floor fi for this participant is considered equal to ti.

The first line contains two space-separated integers n and m (1 ≤ n ≤ 105, 2 ≤ m ≤ 108).  Next n lines contain information about the participants in the form of three space-separated integers si fi ti (1 ≤ si, fi ≤ m, 0 ≤ ti ≤ 108), described in the problem statement.

Print n lines each containing one integer — the time of the arrival for each participant to the required floor.

NoteLet's consider the first sample. The first participant starts at floor s = 2 by the time equal to t = 3. To get to the floor f = 4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor f at time equal to 9. The second participant starts at the time t = 0 on the floor s = 1, enters the elevator immediately, and arrives to the floor f = 2. The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts.

<?php
    $IN = STDIN;
    //$IN=fopen("/Volumes/EXTENDED/olimpiads/test/input.txt","r");
    fscanf($IN, "%d %d",$n, $m);
    while (fscanf($IN, "%d %d %d",$s, $f, $t)){
      $USERLIST[] = Array($s, $f, $t);
      $times[] = $t;
    }

foreach ($USERLIST as $USERPARAM){

    $ust = $USERPARAM[0];
    $ufin = $USERPARAM[1];
    $utime = $USERPARAM[2];

    if ($ust==$ufin){
      echo $utime."\n";
    } elseif ($ust>$ufin) { // вниз ждем
      $j= (($utime - 2*$m +$ust+1)/(2*$m-2));
      $j = (j<0)?0:ceil($j);
      $check2 =  2*$m-$ust-1+$j*(2*$m-2);

      echo ($check2+($ust-$ufin))."\n";

    } elseif ($ust<$ufin){ //вверх ждем
       $j = ceil(($utime - $ust+1)/(2*$m-2));
       $check1 =  $ust-1+$j*(2*$m-2);
       echo ($check1+($ufin-$ust))."\n";
    }
}
?>

<?php
    $IN = STDIN;
    //$IN=fopen("/Volumes/EXTENDED/olimpiads/test/input.txt","r");
    fscanf($IN, "%d %d",$n, $m);
    while (fscanf($IN, "%d %d %d",$ust, $ufin, $utime)){
    if ($ust==$ufin){
      echo $utime."\n";
    } elseif ($ust>$ufin) { // вниз ждем
      $j= (($utime - 2*$m +$ust+1)/(2*$m-2));
      $j = (j<0)?0:ceil($j);
      $check2 =  2*$m-$ust-1+$j*(2*$m-2);
      echo ($check2+($ust-$ufin))."\n";
    } elseif ($ust<$ufin){ //вверх ждем
       $j = ceil(($utime - $ust+1)/(2*$m-2));
       $check1 =  $ust-1+$j*(2*$m-2);
       echo ($check1+($ufin-$ust))."\n";
    }
}
?>

<?php
    $IN = STDIN;
    fscanf($IN, "%d %d",$n, $m);
    while (fscanf($IN, "%d %d %d",$s, $f, $t)){
      $USERLIST[] = Array($s, $f, $t);
    }

foreach ($USERLIST as $USERPARAM){
    $ust = $USERPARAM[0];
    $ufin = $USERPARAM[1];
    $utime = $USERPARAM[2];

    $i = 1;
    $step = 1;
    $time = 0;
    $elapsed = 0;
    $startcount = false;

    if ($ufin==$ust){
      echo $time."\n";
    } else {

        while (true){

            if ($time>=$utime)
              if ($i==$ust)
                $startcount = true;

            if (($startcount)&&($i==$ufin)){
                echo $time."\n";
                break;
              }

            $i += $step;
            if ($i==$m)
                $step = -1;
            elseif ($i==1)
                $step = 1;

            $time++;
        }
    }
}
?>

<?php
    $IN = STDIN;
    //$IN=fopen("/Volumes/EXTENDED/olimpiads/test/input.txt","r");
    fscanf($IN, "%d %d",$n, $m);
    while (fscanf($IN, "%d %d %d",$s, $f, $t)){
      $USERLIST[] = Array($s, $f, $t);
    }

foreach ($USERLIST as $USERPARAM){
    $ust = $USERPARAM[0];
    $ufin = $USERPARAM[1];
    $utime = $USERPARAM[2];

    if ($utime>(2*$m-1)){
        $time = floor($utime/(2*$m-1))*(2*$m-1);
    } else{
        $time = 0;
    }

    $i = 1;
    $step = 1;
    $elapsed = 0;
    $startcount = false;

    if ($ufin==$ust){
      echo $utime."\n";
    } else {

        while (true){

            if ($time>=$utime)
              if ($i==$ust)
                $startcount = true;

            if (($startcount)&&($i==$ufin)){
                echo $time."\n";
                break;
              }

            $i += $step;
            if ($i==$m)
                $step = -1;
            elseif ($i==1)
                $step = 1;

            $time++;
        }
    }
}
?>

<?php
    $IN = STDIN;
    $IN=fopen("/Volumes/EXTENDED/olimpiads/test/input.txt","r");
    fscanf($IN, "%d %d",$n, $m);
    while (fscanf($IN, "%d %d %d",$s, $f, $t)){
      $USERLIST[] = Array($s, $f, $t);
      $times[] = $t;
    }

foreach ($USERLIST as $USERPARAM){

    $ust = $USERPARAM[0];
    $ufin = $USERPARAM[1];
    $utime = $USERPARAM[2];

    if ($ust==$ufin){
      echo $utime."\n";
    } elseif ($ust>$ufin) { // вниз ждем
      $j= (($utime - 2*$m +$ust+1)/(2*$m-2));
      $j = (j<0)?0:ceil($j);
      $check2 =  2*$m-$ust-1+$j*(2*$m-2);

      echo ($check2+($ust-$ufin))."\n";

    } elseif ($ust<$ufin){ //вверх ждем
       $j = ceil(($utime - $ust+1)/(2*$m-2));
       $check1 =  $ust-1+$j*(2*$m-2);
       echo ($check1+($ufin-$ust))."\n";
    }
}
?>

<?php
    $IN = STDIN;
    $IN=fopen("/Volumes/EXTENDED/olimpiads/test/input.txt","r");
    fscanf($IN, "%d %d",$n, $m);
    while (fscanf($IN, "%d %d %d",$s, $f, $t)){
      $USERLIST[] = Array($s, $f, $t);
    }

foreach ($USERLIST as $USERPARAM){
    $ust = $USERPARAM[0];
    $ufin = $USERPARAM[1];
    $utime = $USERPARAM[2];

    if ($utime>(2*$m-1)){
        $time = floor($utime/(2*$m-1))*(2*$m-1);
    } else{
        $time = 0;
    }

    $i = 1;
    $step = 1;
    $elapsed = 0;
    $startcount = false;

    if ($ufin==$ust){
      echo $utime."\n";
    } else {

        while (true){

            if ($time>=$utime)
              if ($i==$ust)
                $startcount = true;

            if (($startcount)&&($i==$ufin)){
                echo $time."\n";
                break;
              }

            $i += $step;
            if ($i==$m)
                $step = -1;
            elseif ($i==1)
                $step = 1;

            $time++;
        }
    }
}
?>