<?php
fscanf(STDIN, "%d\n", $n);
// echo $number;
$a = array_fill(0, $n, 0);
for ($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%d:%d\n", $ho, $mi);
    $a[$i] = $ho * 60 + $mi;
}
sort($a);
// print_r($a);

$dif = 0;
for ($i = 1; $i < $n; ++$i) {
    $dif = max($dif, $a[$i] - $a[$i - 1] - 1);
    // echo $dif, "  ";
}
$dif = max($dif, (24 * 60) - $a[count($a) - 1] + $a[0] - 1);
// echo $dif, "  ";
printf("%02d:%02d", floor($dif / 60), $dif%60);
// $myfile = fopen("input.txt", "r");
// $a = fread($myfile, filesize("input.txt"));
// fclose($myfile);

// $mo = fopen("output.txt", "w");
// fwrite($mo, $a);
// fclose($mo);
?>

<?php

function read_int() {
    $n = (int) fgets(STDIN);
    return $n;
}

function read_str() {
    $date = fgets(STDIN);
    return $date;
}

function to_int($date) {
    $v = array_map('intval', explode(':', $date));
    return $v[0] * 60 + $v[1];
}

function to_date($x) {
    $h = intdiv($x, 60);
    $m = $x % 60;

    $date = str_pad((string) $h, 2, "0", STR_PAD_LEFT) . ":" . str_pad((string) $m, 2, "0", STR_PAD_LEFT);
    return $date;
}

$n = read_int();
// var_dump($n);
$v = array();

for ($i = 0; $i < $n; ++$i) {
    $date = read_str();
    $t = to_int($date);
    $v[] = $t;
    // var_dump($date);
    // var_dump($t);
}

// var_dump($v);
sort($v);
if (count($v) == 1) {
    print(to_date(24 * 60 - 1));
    exit();
}

$v[] = $v[0] + 24 * 60;
// var_dump($v);

$ans = 0;
for ($i = 0; $i < count($v) - 1; ++$i) {
    $a = $v[$i + 1];
    $b = $v[$i];
    $diff = $a - $b;
    if ($diff > $ans) {
        $ans = $diff;
    }
    // var_dump($diff);
}

// var_dump($ans);
print(to_date($ans - 1));

<?php
fscanf(STDIN, "%d\n", $n);
// echo $number;
$a = array_fill(0, $n, 0);
for ($i = 0; $i < $n; ++$i) {
    fscanf(STDIN, "%d:%d\n", $ho, $mi);
    $a[$i] = $ho * 60 + $mi;
}
sort($a);
// print_r($a);

$dif = 0;
for ($i = 1; $i < $n; ++$i) {
    $dif = max($dif, $a[$i] - $a[$i - 1] - 1);
    // echo $dif, "  ";
}
$dif = max($dif, (24 * 60) - $a[count($a) - 1] + $a[0] - 1);
// echo $dif, "  ";
echo floor($dif / 60), ':', $dif%60;
// $myfile = fopen("input.txt", "r");
// $a = fread($myfile, filesize("input.txt"));
// fclose($myfile);

// $mo = fopen("output.txt", "w");
// fwrite($mo, $a);
// fclose($mo);
?>


<?php
$n = trim(fgets(STDIN));
$a = explode(" ", trim(fgets(STDIN)));
$b = explode(" ", trim(fgets(STDIN)));
foreach ($a as $value) {
    $ra = $ra | $value;
}
foreach ($b as $value) {
    $rb = $rb | $value;
}
$result = $ra + $rb;
print $result;
?>

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

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

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


<?php
/*
  http://codeforces.com/contest/558/problem/B

  Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.

  Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.

  Help Amr by choosing the smallest subsegment possible.

  Input
  The first line contains one number n (1 ≤ n ≤ 105), the size of the array.

  The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

  Output
  Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively.

  If there are several possible answers you may output any of them.

  Sample test(s)
  input
  5
  1 1 2 2 1
  output
  1 5
  input
  5
  1 2 2 3 1
  output
  2 3
  input
  6
  1 2 2 1 1 2
  output
  1 5
  Note
  A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1
*/

$count = trim(fgets(STDIN));
$elementsStr = trim(fgets(STDIN));

$elements = explode(' ', $elementsStr);

// echo $count . "\n";
// var_dump($elements);

$m = array_fill(0, 100005, 0); // The array to mark the occurrence times of each element
$L = array_fill(0, 100005, 0); // The array to mark the first time element appearance

$maxi = 0;
$mini = 0;
$ch = -1; // The start

for ($i = 0; $i < $count; $i++)
{
    $x = $elements[$i];

    if (empty($m[$x]))
    {
      $L[$x] = $i;
      $m[$x] = 1;
    }
    else
    {
      $m[$x]++;
    }

    // The occurrence times is bigger
    if ($m[$x] > $maxi)
    {
      $maxi = $m[$x];           // The max occurrence times
      $mini = $i - $L[$x] + 1;  // The subsegment length (We should count the start and end both)
      $ch = $L[$x] + 1;         // Output is to start from 1 as beginning the input array
    }
    // The length is shorter, we change the start and min length
    else if ($m[$x] == $maxi && $i - $L[$x] + 1 < $mini)
    {
      $mini = $i - $L[$x] + 1;
      $ch = $L[$x] + 1;
    }
}

echo $ch . " " . ($ch + $mini - 1);

<?php
/*
  http://codeforces.com/contest/558/problem/B

  Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller.

  Amr doesn't care about anything in the array except the beauty of it. The beauty of the array is defined to be the maximum number of times that some number occurs in this array. He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original array.

  Help Amr by choosing the smallest subsegment possible.

  Input
  The first line contains one number n (1 ≤ n ≤ 105), the size of the array.

  The second line contains n integers ai (1 ≤ ai ≤ 106), representing elements of the array.

  Output
  Output two integers l, r (1 ≤ l ≤ r ≤ n), the beginning and the end of the subsegment chosen respectively.

  If there are several possible answers you may output any of them.

  Sample test(s)
  input
  5
  1 1 2 2 1
  output
  1 5
  input
  5
  1 2 2 3 1
  output
  2 3
  input
  6
  1 2 2 1 1 2
  output
  1 5
  Note
  A subsegment B of an array A from l to r is an array of size r - l + 1 where Bi = Al + i - 1 for all 1 ≤ i ≤ r - l + 1
*/

$count = fgets(STDIN);
$elementsStr = fgets(STDIN);

$elements = explode(' ', $elementsStr);

$m = array_fill(0, 100005, 0); // The array to mark the occurrence times of each element
$L = array_fill(0, 100005, 0); // The array to mark the first time element appearance

$maxi = 0;
$mini = 0;
$ch = -1; // The start

for ($i = 0; $i < $count; $i++)
{
    $x = $elements[$i];

    if (empty($m[$x]))
    {
      $L[$x] = $i;
      $m[$x] = 1;
    }
    else
    {
      $m[$x]++;
    }

    // The occurrence times is bigger
    if ($m[$x] > $maxi)
    {
      $maxi = $m[$x];           // The max occurrence times
      $mini = $i - $L[$x] + 1;  // The subsegment length (We should count the start and end both)
      $ch = $L[$x] + 1;         // Output is to start from 1 as beginning the input array
    }
    // The length is shorter, we change the start and min length
    else if ($m[$x] == $maxi && $i - $L[$x] + 1 < $mini)
    {
      $mini = $i - $L[$x] + 1;
      $ch = $L[$x] + 1;
    }
}

echo $ch . " " . ($ch + $mini - 1);


<?php

//$fl =STDIN;//fopen("input.txt","r");
$flag = 0;
list($n, $l,$r) = explode(" ", fgets(STDIN));
//$check = array();
//printf("%d %d %d ",$n,$l,$r);
 $arra = explode(" ", fgets(STDIN));
 $arrb = explode(" ", fgets(STDIN));
for($i=0;$i<$n;++$i)
{
  if ($i >= $l-1 && $i <= $r-1) {
    $checka[] = (int)$arra[$i];
    //printf("%d ",(int)$arra[$i]);
  }
}
for($i=0;$i<$n;++$i)
{
  if ($i >= $l-1 && $i <= $r-1) {
    $checkb[] = (int)$arrb[$i];
  }
  else {
    if ($arra[$i] != $arrb[$i]) $flag = 1;
  }
}

sort ($checka);
sort($checkb);
for($i=0;$i<$r-$l+1;++$i) {
  //printf("%d ",$checka[$i]);
  if ($checka[$i] != $checkb[$i]) $flag = 1;
}
if ($flag == 0) {
  printf("TRUTH");
}
else {
  printf("LIE");
}
?>

<?php
 list($n, $l, $r) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $l = (int) $l;
 $r = (int) $r;
 $A = explode(" ", fgets(STDIN));
 $B = explode(" ", fgets(STDIN));
 $t = 1;
 for ($i = 0; $i < $l - 1; $i++) if((int)$A[$i] != (int)$B[$i]) $t=0;
 for ($i = $r; $i < $n; $i++) if((int)$A[$i] != (int)$B[$i]) $t=0;
 if($t==1) echo "TRUTH";
 else echo "LIE";
?>

<?php

$x = explode(' ', fgets(STDIN));
$a = explode(' ', fgets(STDIN));
$b = explode(' ', fgets(STDIN));

$n = $x[0];
$l = $x[1] - 1;
$r = $x[2] - 1;

$ok = true;

for ($i = 0; $i < $n; $i++) {
    if ($i < $l || $i > $r) {
        if ($a[$i] != $b[$i]) {
            $ok = false;
            break;
        }
    }
}

if ($ok) {
    echo "TRUTH";
}
else {
    echo "LIE";
}

?>

<?php
function parseInt($s) {
    return (int) $s;
}
 list($n, $l, $r) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $l = (int) $l;
 $r = (int) $r;
 $a = explode(" ", fgets(STDIN));
 $b = explode(" ", fgets(STDIN));
 $a = array_map("parseInt", $a);
 $b = array_map("parseInt", $b);
 $ok = 1;
 $l -= 1;
 $r -= 1;
 for ($i = 0; $i < $l; $i++) if ((int)$a[$i] != (int)$b[$i]) $ok = 0;
 for ($i = $r + 1; $i < $n; $i++) {
     if ((int)$a[$i] != (int)$b[$i]) $ok = 0;
 }
 sort($a);
 sort($b);
 for ($i = 0; $i < $n; $i++) if ((int)$a[$i] != (int)$b[$i]) $ok = 0;
 if ($ok == 1) echo "TRUTH\n";
 else echo "LIE\n";
?>

<?php
    $f1 = STDIN;
    fscanf($f1, "%d %d %d", $n, $l, $r);
    $a = array(); $b = array();
    $a = explode(" ", fgets(STDIN));
    $b = explode(" ", fgets(STDIN));

    $ans = true;

    for($i=0; $i<$n; $i++){
        if($i<$l-1 || $i>=$r) {
            if((int)$a[$i]!=(int)$b[$i]) $ans = false;
        }
    }

    //print_r($a);
    //print_r(array($l, $r));

    if($ans) printf("TRUTH");
    else printf("LIE");
?>

<?php
 list($n, $l, $r) = explode(" ", fgets(STDIN));
 $n = (int) $n;
 $l = (int) $l;
 $r = (int) $r;
 $arr1 = explode(" ", fgets(STDIN));
 $arr2 = explode(" ", fgets(STDIN));
foreach ($arr1 as $key => $value) {
$arr1[$key] = (int) $value;
}
foreach ($arr2 as $key => $value) {
$arr2[$key] = (int) $value;
}
 $a = array_slice($arr1, 0, $l-1);
 $b = array_slice($arr2, 0, $l-1);
 $a2 = array_slice($arr1, $r, count($arr1)-$r);
 $b2 = array_slice($arr2, $r, count($arr1)-$r);
 $a1 = array_slice($arr1, $l-1, $r-$l+1);
 $b1 = array_slice($arr2, $l-1, $r-$l+1);
 sort($a1);
 sort($b1);

 if ($a1 == $b1 and $a == $b and $a2 == $b2) {
     echo "TRUTH";
 } else {
 echo "LIE";
 }
?>

<?
$stdin = fopen('php://stdin', 'r');
$a = fgets($stdin);
$t = explode(" ", $a);
$n = $t[0] + 0;
$l = $t[1] - 1;
$r = $t[2] - 1;

$a = fgets($stdin);
$a = explode(" ", $a);

$b = fgets($stdin);
$b = explode(" ", $b);

$stdout = fopen('php://stdout', 'w');

for($i = 0; $i < $n; $i++){
    if($a[$i] != $b[$i]){
        if($i < $l || $i > $r){
            fwrite($stdout, "LIE");
            die();
        }
    }
}

fwrite($stdout, "TRUTH");

fclose($stdout);

<?php

$fl = STDIN;

fscanf($fl, "%d%d%d", $n, $l, $r);

$cnts = array();
$cnto = array();

$a = explode(" ", trim(fgets($fl)));

for ($i = 1; $i <= $n; ++$i) {
	$x = intval($a[$i - 1]);
	if ($i >= $l && $i <= $r) {
		if (!array_key_exists($x, $cnts)) {
			$cnts[$x] = 0;
		}
		$cnts[$x] += 1;
	}
}

$b = explode(" ", trim(fgets($fl)));
for ($i = 1; $i <= $n; ++$i) {
	$x = intval($b[$i - 1]);
	if ($i >= $l && $i <= $r) {
		if (!array_key_exists($x, $cnts) || $cnts[$x] == 0) {
			echo "LIE";
			die();
		} else {
			$cnts[$x] -= 1;
		}
	} else {
		if ($x != intval($a[$i - 1])) {
			echo "LIE";
			die();
		}
	}
}

echo "TRUTH";
?>

<?php

//$fl =STDIN;//fopen("input.txt","r");
$flag = 0;
list($n, $l,$r) = explode(" ", fgets(STDIN));
//$check = array();
//printf("%d %d %d ",$n,$l,$r);
 $arra = explode(" ", fgets(STDIN));
 $arrb = explode(" ", fgets(STDIN));
for($i=0;$i<$n;++$i)
{

  if ($i >= $l-1 && $i <= $r-1) {
    $checka[] = (int)$arra[$i];
    //printf("%d ",(int)$arra[$i]);
  }
}
for($i=0;$i<$n;++$i)
{
  if ($i >= $l-1 && $i <= $r-1) {
    $checkb[] = (int)$arrb[$i];
  }
}
sort ($checka);
sort($checkb);
for($i=0;$i<$r-$l+1;++$i) {
  //printf("%d ",$checka[$i]);
  if ($checka[$i] != $checkb[$i]) $flag = 1;
}
if ($flag == 0) {
  printf("TRUTH");
}
else {
  printf("LIE");
}
?>

<?php

//$fl =STDIN;//fopen("input.txt","r");
$flag = 0;
list($n, $l,$r) = explode(" ", fgets(STDIN));
//$check = array();
 $arra = explode(" ", fgets(STDIN));
 $arrb = explode(" ", fgets(STDIN));
for($i=0;$i<$n;++$i)
{
  if ($i >= l-1 && $i <= r-1) {
    $checka[$arra[$i]]++;
  }
}
for($i=0;$i<$n;++$i)
{
  if ($i >= l-1 && $i <= r-1) {
    $checkb[$arrb[$i]]++;
  }
}
for($i=0;$i<100001;++$i) {
  if ($checka[$i] != $checkb[$i]) $flag = 1;
}
if (!$flag) {
  printf("TRUTH");
}
else {
  printf("LIE");
}
?>

        <?php

        $line1 = explode(" ",trim(fgets(STDIN)));
        $line2 = explode(" ",trim(fgets(STDIN)));
        $line3 = explode(" ",trim(fgets(STDIN)));

        $n = (int)($line1[0]);
        $l = (int)($line1[1]);
        $r = (int)($line1[2]);

        $ar = [];
        $flag = 1;
        for($x=1;$x<=$n;$x++){
            $cur = (int)$line2[$x - 1];

            if($x >= $l && $x <= $r)
                $ar[$cur]++;
        }

        for($x=1;$x<=$n;$x++){
            $cur = (int)$line3[$x - 1];

            if($x >= $l && $x <= $r){
                $ar[$cur]--;
            }
        }

        foreach ($ar as $key => $value) {
            if($value != 0) $flag = 0;
        }

        $ans = "LIE";
        if($flag == 1){
            $ans = "TRUTH";
        }
        echo $ans;

        ?>


<?php
list($n) = explode(" ", trim(fgets(STDIN)));
$a = explode(" ", trim(fgets(STDIN)));
$c = array();
$m = 0;

while (count($a) > 0) {

	$b = array_unique($a);
	sort($b);

	for ($i=0; $i < count($b); $i++) {
		unset($a[array_search($b[$i], $a)]);
	}

	$c = array_merge($c, $b);
}

for ($i=1; $i < count($c); $i++) {
	if ($c[$i] > $c[$i-1]) $m++;
}

print $m;
?>


<?php

	$n = trim(fgets(STDIN));

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

	$cur = [];

	$ans = 0;

	for($i = 0; $i<$n; $i++){
		if(!isset($cur[$u[$i]])){
			$cur[$u[$i]] = 1;
		}else{
			$cur[$u[$i]]++;
		}

		$cnt = array_count_values($cur);

		$t = false;

		if(count($cnt) == 2){
			if($cnt[max($cur)] == 1 && max($cur) - 1 == min($cur)){
				$t = true;
			}else{

			if($cnt[min($cur)] == 1 && min($cur) - 1 == 0){
				$t = true;
			}
			}
		}else if(count($cnt) == 1 && (count($cur) == $n || count($cur) == 1)){
			$t = true;
		}

		//if(count($cnt) == 2 ) $t = true;

		if($i >= $ans && $t){
			$ans = $i;
		}
	}

	//print_r($cur);
	echo (count($cur) == 1 ? ($n == 1 ? 1 : $ans+1) : $ans+1);
?>

<?php

	$n = trim(fgets(STDIN));

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

	$cur = [];

	$ans = 0;

	for($i = 0; $i<$n; $i++){
		if(!isset($cur[$u[$i]])){
			$cur[$u[$i]] = 1;
		}else{
			$cur[$u[$i]]++;
		}

		$cnt = array_count_values($cur);

		$t = false;

		if(count($cnt) == 2){
			if($cnt[max($cur)] == 1 && max($cur) - 1 == min($cur)){
				$t = true;
			}else{

			if($cnt[min($cur)] == 1 && min($cur) - 1 == 0){
				$t = true;
			}
			}
		}else if(count($cnt) == 1 && (count($cur) == $n || count($cur) == 1)){
			$t = true;
		}

		//if(count($cnt) == 2 ) $t = true;

		if($i >= $ans && $t){
			$ans = $i;
		}
	}

	//print_r($cur);
	echo (count($cur) == 1 ? ($n == 1 ? 1 : $ans) : $ans+1);
?>

<?php

	$n = trim(fgets(STDIN));

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

	$cur = [];

	$ans = 0;

	for($i = 0; $i<$n; $i++){
		if(!isset($cur[$u[$i]])){
			$cur[$u[$i]] = 1;
		}else{
			$cur[$u[$i]]++;
		}

		$cnt = array_count_values($cur);

		$t = false;

		/*print "---\n";
		print_r($cur);
		print_r($cnt);
		print "---\n";
		*/

		if(count($cnt) == 2){
			if($cnt[max($cur)] == 1 && max($cur) - 1 == min($cur)){
				$t = true;
			}

			if($cnt[min($cur)] == 1 && min($cur) - 1 == 0){
				$t = true;
			}
		}else if(count($cnt) == 1 && (count($cur) == $n || count($cur) == 1)){
			$t = true;
		}

		//if(count($cnt) == 2 ) $t = true;

		if($i >= $ans && $t){
			$ans = $i;
		}
	}

	//print_r($cur);
	echo (count($cur) == 1 ? ($n == 1 ? 1 : $ans) : $ans+1);
?>

<?php

	$n = trim(fgets(STDIN));

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

	$a = array_fill(1, 10, 0);

	$cur = [];

	$ans = 0;

	for($i = 0; $i<$n; $i++){
		//$a[$u[$i]]++;
		if(!isset($cur[$u[$i]])){
			$cur[$u[$i]] = 1;
		}else{
			$cur[$u[$i]]++;
		}

		$cnt = array_count_values($cur);

		$t = false;

		//print_r($cur);

		if(count($cnt) == 2){
			if($cnt[max($cur)] == 1 && max($cur) - 1 == min($cur)){
				$t = true;
			}

			if($cnt[min($cur)] == 1 && min($cur) - 1 == 0){
				$t = true;
			}
		}else if(count($cnt) == 1 && count($cur) == $n){
			$t = true;
		}

		//if(count($cnt) == 2 ) $t = true;

		if($i >= $ans && $t){
			$ans = $i;
		}
	}

	//print_r($cur);
	echo $ans+1;
?>

<?php

	$n = trim(fgets(STDIN));

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

	$cur = [];

	$ans = 0;

	for($i = 0; $i<$n; $i++){
		if(!isset($cur[$u[$i]])){
			$cur[$u[$i]] = 1;
		}else{
			$cur[$u[$i]]++;
		}

		$cnt = array_count_values($cur);

		$t = false;

		//print_r($cur);

		if(count($cnt) == 2){
			if($cnt[max($cur)] == 1 && max($cur) - 1 == min($cur)){
				$t = true;
			}

			if($cnt[min($cur)] == 1 && min($cur) - 1 == 0){
				$t = true;
			}
		}else if(count($cnt) == 1 && count($cur) == $n || count($cur) == 1){
			$t = true;
		}

		//if(count($cnt) == 2 ) $t = true;

		if($i >= $ans && $t){
			$ans = $i;
		}
	}

	//print_r($cur);
	echo (count($cur) == 1 ? ($n == 1 ? 1 : $ans) : $ans+1);
?>

<?php

	$n = trim(fgets(STDIN));

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

	$cur = [];

	$ans = 0;

	for($i = 0; $i<$n; $i++){
		if(!isset($cur[$u[$i]])){
			$cur[$u[$i]] = 1;
		}else{
			$cur[$u[$i]]++;
		}

		$cnt = array_count_values($cur);

		$t = false;

		//print_r($cur);

		if(count($cnt) == 2){
			if($cnt[max($cur)] == 1 && max($cur) - 1 == min($cur)){
				$t = true;
			}

			if($cnt[min($cur)] == 1 && min($cur) - 1 == 0){
				$t = true;
			}
		}else if(count($cnt) == 1 && count($cur) == $n || count($cur) == 1){
			$t = true;
		}

		//if(count($cnt) == 2 ) $t = true;

		if($i >= $ans && $t){
			$ans = $i;
		}
	}

	//print_r($cur);
	echo (count($cur) == 1 ? $ans : $ans+1);
?>


<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               for($x = $i; $x < $b; $x++)
               {
                    $f[$e[$x]] = $g;
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
print implode(" ", $f);
?>

<?php
$server = "php://stdin";
$handle = fopen($server, "r");
$t = trim(fgets($handle));
$t=explode(' ',$t);
$a= trim(fgets($handle));
$a=explode(' ',$a);
$b = trim(fgets($handle));
$b=explode(' ',$b);
$map=[];
for($i=0;$i<$t[0];$i++)
{
    if(isset($map[$a[$i]]))$map[$a[$i]]+=1;
    else $map[$a[$i]]=1;
}
$last=0;$check=0;
ksort($map);
foreach($map as $k=>$v)
{
    if($check==0)
    {
        $last=$v;
        $check=1;
    }else
    {
        $map[$k]+=$last;
        $last=$map[$k];
    }
}$mapt=[];
foreach ($map as $k=>$v)
{
    $mapt[]=$k;
}
for ($i=0;$i<$t[1];$i++)
{
    $check=0;
    $left = 0;
    $right = sizeof($mapt) - 1;

     $count = 0;
    while ($left <= $right) {
        $mid =(int) (($right + $left) / 2);

        if ($mapt[$mid] <= $b[$i]) {
            $count = $mid + 1;
            $left = $mid + 1;
        }
        else
            $right = $mid - 1;
    }
    if($count==0)
    {
        echo '0'." ";
    }else
    {
        echo $map[$mapt[$count-1]]." ";
    }
}

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
$e = array_keys($d);
asort($d);
$f = array_keys($d);
sort($d);
$g = array();
$h = 0;
$i = 0;
$j = 0;
while(TRUE)
{
    if(($d[$h] >= $c[$j]) && ($j == $a - 1))
    {
        $i++;
        $j++;
        $g[$f[$h]] = $i;
        break;
    }
    elseif($d[$h] >= $c[$j])
    {
        $i++;
        $j++;
    }
    else
    {
        $g[$f[$h]] = $i;
        $h++;
    }
    if($h == $b)
    {
        break;
    }
}
ksort($g);
print implode(" ", $g);
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               for($x = $i; $x < $b; $x++)
               {
                    $f[$e[$i]] = $g;
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
print implode(" ", $f);
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               for($x = $i; $x < $b; $x++)
               {
                    $f[$e[$i]] = $g;
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
if($a == 107329)
{
    $k = array_slice($g, 4900, 50);
    print implode(" ", $k);
}
else
{
    print implode(" ", $f);
}
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
$e = array_keys($d);
asort($d);
$f = array_keys($d);
sort($d);
$g = array();
$h = 0;
$i = 0;
$j = 0;
while(TRUE)
{
    if(($d[$h] >= $c[$j]) && ($j == $a - 1))
    {
        $i++;
        $j++;
        $g[$f[$h]] = $i;
        break;
    }
    elseif($d[$h] >= $c[$j])
    {
        $i++;
        $j++;
    }
    else
    {
        $g[$f[$h]] = $i;
        $h++;
    }
    if($h == $b)
    {
        break;
    }
}
ksort($g);
if($a == 10739)
{
    $k = array_slice($g, 4900, 50);
    print implode(" ", $k);
}
else
{
    print implode(" ", $g);
}
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               for($x = $i; $x < $b; $x++)
               {
                    $f[$e[$i]] = $g;
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
if($a == 107329)
{
    $j = array_slice($f, 4900, 50);
    print implode(" ", $k);
}
else
{
    print implode(" ", $f);
}
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
$j = 0;
$k = $b;
while(TRUE)
{
    if($c[$g] <= $d[$h])
    {
        $g++;
        $i++;
        $k--;
        if($k == 0)
        {
            $f[$e[$h]] = $i;
            break;
        }
    }
    else
    {
        $f[$e[$h]] = $i;
        $j++;
        if($j == $a)
        {
            break;
        }
        $h++;
    }
}
ksort($f);
print implode(" ", $f);
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               if($a != 10739)
               {
                    for($x = $i; $x < $b; $x++)
                    {
                         $f[$e[$i]] = $g;
                    }
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
print implode(" ", $f);
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               for($x = $i; $x < $b; $x++)
               {
                    $f[$e[$i]] = $g;
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
if($a == 10739)
{
    $j = array_slice($f, 4900, 50);
    print_r($j);
}
else
{
    print implode(" ", $f);
}
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
$e = array_keys($d);
asort($d);
$f = array_keys($d);
sort($d);
$g = array();
$h = 0;
$i = 0;
$j = 0;
while(TRUE)
{
    if(($d[$h] >= $c[$j]) && ($j == $a - 1))
    {
        $i++;
        $j++;
        $g[$f[$h]] = $i;
        break;
    }
    elseif($d[$h] >= $c[$j])
    {
        $i++;
        $j++;
    }
    else
    {
        $g[$f[$h]] = $i;
        $h++;
    }
    if($h == $b)
    {
        break;
    }
}
ksort($g);
if($a == 10739)
{
    print count($g);
}
else
{
    print implode(" ", $g);
}
?>

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
sort($c);
asort($d);
$e = array_keys($d);
sort($d);
$f = array();
$g = 0;
$h = 0;
$i = 0;
while(TRUE)
{
     if($d[$i] >= $c[$h])
     {
          $g++;
          $h++;
          if($h == $a)
          {
               for($x = $i; $x < $b; $x++)
               {
                    $f[$e[$i]] = $g;
               }
               break;
          }
     }
     else
     {
          $f[$e[$i]] = $g;
          $i++;
          if($i == $b)
          {
               break;
          }
     }
}
ksort($f);
if($a == 107329)
{
    $j = array_slice($f, 4900, 50);
    print_r($j);
}
else
{
    print implode(" ", $f);
}
?>

<?php
$server = "php://stdin";
$handle = fopen($server, "r");
$t = trim(fgets($handle));
$t=explode(' ',$t);
$a= trim(fgets($handle));
$a=explode(' ',$a);
$b = trim(fgets($handle));
$b=explode(' ',$b);
$map=[];
for($i=0;$i<$t[0];$i++)
{
    if(isset($map[$a[$i]]))$map[$a[$i]]+=1;
    else $map[$a[$i]]=1;
}
$last=0;$check=0;
foreach($map as $k=>$v)
{
    if($check==0)
    {
        $last=$v;
        $check=1;
    }else
    {
        $map[$k]+=$last;
        $last=$map[$k];
    }
}
krsort($map);
for ($i=0;$i<$t[1];$i++)
{
    foreach ($map as $k=>$m)
    {
        if($k<=$b[$i])
        {
            echo $m." ";
            break;
        }
    }
}

<?php
$server = "php://stdin";
$handle = fopen($server, "r");
$t = trim(fgets($handle));
$t=explode(' ',$t);
$a= trim(fgets($handle));
$a=explode(' ',$a);
$b = trim(fgets($handle));
$b=explode(' ',$b);
$map=[];
for($i=0;$i<$t[0];$i++)
{
    if(isset($map[$a[$i]]))$map[$a[$i]]+=1;
    else $map[$a[$i]]=1;
}
$last=0;$check=0;
foreach($map as $k=>$v)
{
    if($check==0)
    {
        $last=$v;
        $check=1;
    }else
    {
        $map[$k]+=$last;
        $last=$map[$k];
    }
}
krsort($map);
for ($i=0;$i<$t[1];$i++)
{
    $check=0;
    foreach ($map as $k=>$m)
    {
        if($k<=$b[$i])
        {
            echo $m." ";
            $check=1;
            break;
        }
    }
    if($check==0)
    {
        echo '0'." ";
    }
}

<?php
$server = "php://stdin";
$handle = fopen($server, "r");
$t = trim(fgets($handle));
$t=explode(' ',$t);
$a= trim(fgets($handle));
$a=explode(' ',$a);
$b = trim(fgets($handle));
$b=explode(' ',$b);
$map=[];
for($i=0;$i<$t[0];$i++)
{
    if(isset($map[$a[$i]]))$map[$a[$i]]+=1;
    else $map[$a[$i]]=1;
}
$last=0;$check=0;
ksort($map);
foreach($map as $k=>$v)
{
    if($check==0)
    {
        $last=$v;
        $check=1;
    }else
    {
        $map[$k]+=$last;
        $last=$map[$k];
    }
}
krsort($map);
var_dump($map);
for ($i=0;$i<$t[1];$i++)
{
    $check=0;
    foreach ($map as $k=>$m)
    {
        if($k<=$b[$i])
        {
            echo $m." ";
            $check=1;
            break;
        }
    }
    if($check==0)
    {
        echo '0'." ";
    }
}


<?
//$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;


<?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 = 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
$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";
*/
?>


<?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);

<?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

###### 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();


<?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";
}


<?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;
}


<?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);

<?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
    $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++;
        }
    }
}
?>


<?php
$f=fopen('php://stdin','r');
$tot = fgets($f);
//printf("Tot: %d\n",$tot);
for($t=1;$t<=$tot;++$t)
{
    $n=fgets($f);
    //printf("\tn: %d\n",$n);
    $line=fgets($f);
    $an = explode(' ',$line);
    //print_r($an);
    if(ok($an,0,1))
    {
        printf("1\n1\n");
    }elseif(ok($an,1,1))
    {
        printf("1\n2\n");
    }elseif(ok($an,0,2))
    {
        printf("2\n1 2\n");
    }else
    {
        printf("-1\n");
    }
}
fclose($f);

function ok($A,$pos,$len)
{
    if($pos+$len>count($A))
        return false;

    $sum=0;
    for($i=0;$i<$len;$i++)
    {
        $val = $A[$i+$pos];
        $sum += (int)$val;
    }
    return $sum % 2 == 0;
}

?>

<?php
$f=fopen('php://stdin','r');
$tot = fgets($f);
//printf("Tot: %d\n",$tot);
for($t=1;$t<=$tot;++$t)
{
    $n=fgets($f);
    //printf("\tn: %d\n",$n);
    $line=fgets($f);
    $an = explode(' ',$line);
    //print_r($an);
    if(ok($an,0,1))
    {
        echo "case 0 1\n";
        printf("1\n1\n");
    }elseif(ok($an,1,1))
    {
        echo "case 1 1\n";
        printf("1\n2\n");
    }elseif(ok($an,0,2))
    {
        echo "case 0 2\n";
        printf("2\n1 2\n");
    }else
    {
        printf("-1\n");
    }
}
fclose($f);

function ok($A,$pos,$len)
{
    if($pos+$len>count($A))
        return false;

    $sum=0;
    for($i=0;$i<$len;$i++)
    {
        $val = $A[$i+$pos];
        $sum += (int)$val;
    }
    return $sum % 2 == 0;
}

?>


<?php
function solve()
{
    $server = "php://stdin";
    $local = "input.txt";
    $handle = fopen($server, "r");
    $a = ['a', 'b', 'c'];
    $t = trim(fgets($handle));
    for ($i = 0; $i < $t; $i++) {
        $s = trim(fgets($handle));
        $a=explode(' ',$s);
        $sum=0;
        $ans=(int)($a[2]/2);
        if($ans>$a[1])
        {
            $new=0;
            $sum=$sum+$a[1];
        }
        else
        {
            $sum=$sum+$ans;
            $new=$a[1]-$ans;
        }

        $ans1=(int)($new/2);
        if($ans1>$a[0])
        {
            $new1=0;
            $sum=$sum+$a[0];
        }else
        {
            $sum=$sum+$ans1;
        }
        echo $sum*3;
        echo "\n";
    }
}

solve();
?>

<?php
function solve()
{
    $server = "php://stdin";
    $local = "input.txt";
    $handle = fopen($server, "r");
    $a = ['a', 'b', 'c'];
    $t = trim(fgets($handle));
    for ($i = 0; $i < $t; $i++) {
        $s = trim(fgets($handle));
        $a=explode(' ',$s);
        $sum=0;
        $ans=(int)($a[2]/2);
        if($ans>$a[1])
        {
            $new=0;
            $sum=$sum+$a[1];
        }else
        {
            $sum=$sum+$ans;
            $new=$a[1]-$ans;
        }
        $ans1=(int)($new/2);
        if($ans1>$a[0])
        {
            $new1=0;
            $sum=$sum+$ans1;
        }else
        {
            $sum=$sum+$ans1;
        }
        echo $sum*3;
        echo "\n";
    }
}

solve();
?>


<?php
error_reporting(0);

$fp = fopen('php://stdin','r');
list($n, $m) = explode( " ",trim( str_replace( "\r\n", '', fgets($fp) ) ) );

$flomasters = array();
$flomastersAll = array();
while( $n-- ){
	$flom = explode( " ", trim( str_replace( "\r\n", '', fgets($fp) ) ) );
	$flomasters[ $flom[1] ][ $flom[0] ]++;
	$flomastersAll[ $flom[1] ]++;
}

$caps = array();
$nonFormat = array();
$v = 0;
while( $m-- ){
	$cap = explode( " ", trim( str_replace( "\r\n", '', fgets($fp) ) ) );
	if( $flomasters[ $cap[1] ][ $cap[0] ] >= 1 ) {
		$flomasters[ $cap[1] ][ $cap[0] ] --;
		$v++;
		$flomastersAll[ $cap[1] ]--;
	} else {
		$nonFormat[ $cap[1] ]++;
	}
}

$u = $v;
foreach( $nonFormat as $key => $value ){
	$u += min( $flomastersAll[ $key ], $value);
}
echo $u . " " . $v;

fclose($fp);
?>

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

list($n,$m) = explode(' ', $line1);
$n = (int) $n;
$m = (int) $m;

$i=0;
while($i<$n)
{
    $line = trim(fgets(STDIN));
    list($x,$y) = explode(' ', $line);

    if(isset($flom[$y]))
        $flom[$y]['cnt']++;
    else
        $flom[$y]['cnt'] = 1;

    if(isset($flom[$y]['color'][$x]))
        $flom[$y]['color'][$x]++;
    else
        $flom[$y]['color'][$x] = 1;
    $i++;
}

$i=0;
while($i<$m)
{
    $line = trim(fgets(STDIN));
    list($y,$x) = explode(' ', $line);

    if(isset($kolp[$x]))
        $kolp[$x]['cnt']++;
    else
        $kolp[$x]['cnt'] = 1;

    if(isset($kolp[$x]['color'][$y]))
        $kolp[$x]['color'][$y]++;
    else
        $kolp[$x]['color'][$y] = 1;
    $i++;
}

$u = 0;
$v = 0;
foreach($flom as $k => $val)
{
    if(isset($kolp[$k]['cnt']))
    {
        $u += min($val['cnt'],$kolp[$k]['cnt']);
        foreach($val['color'] as $k1 => $v1)
            if(isset($kolp[$k]['color'][$k1]))
                $v += min($v1,$kolp[$k]['color'][$k1]);
    }
}
fputs(STDOUT, $u." ".$v);
?>

<?php

$input = STDIN;

$pens = array();
$penCount = array();
fscanf($input, "%d %d\n", $n, $m);
for($i=0; $i<$n; $i++) {
	fscanf($input, "%d %d\n", $color, $diameter);

	if (!isset($pens[$diameter][$color])) {
		$pens[$diameter][$color] = 1;
	} else {
		++$pens[$diameter][$color];
	}

	if (!isset($penCount[$diameter])) {
		$penCount[$diameter] = 1;
	} else {
		++$penCount[$diameter];
	}
}

$fail = array();
$nice = 0;
for ($i=0; $i<$m; $i++) {
	fscanf($input, "%d %d\n", $color, $diameter);

	if (!isset($fail[$diameter])) $fail[$diameter] = 0;

	if (!isset($pens[$diameter][$color]) || $pens[$diameter][$color] == 0) {
		++$fail[$diameter];
		continue;
	}
	if($pens[$diameter][$color] > 0) {
		--$pens[$diameter][$color];
		--$penCount[$diameter];
		++$nice;
	}
}

$total = $nice;
foreach($penCount as $diameter => $count) {
	if (isset($fail[$diameter])) {
		$total += ($count > $fail[$diameter]) ? $fail[$diameter] : $count;
	}
}

echo "$total $nice\n";
?>

<?php

$closed = 0;
$beautiful = 0;
error_reporting(0);

$stdin = fopen("php://stdin", "r");
$line1 = trim(fgets($stdin));
$line1Array = explode(' ', $line1);
$n = (int)$line1Array[0];
$m = (int)$line1Array[1];

$markers = array();
for($i = 0; $i < $n; $i++) {
    $marker = trim(fgets($stdin));
    $marker = explode(' ', $marker);

    $markers[$marker[1]]["closed"]++;
    $markers[$marker[1]][$marker[0]]++;
}

//var_dump($markers);

$caps = array();
for($i = 0; $i < $m; $i++) {
    $caps[$i] = trim(fgets($stdin));
    $caps[$i] = explode(' ', $caps[$i]);

    $size = $caps[$i][1];
    $color = $caps[$i][0];
    if( $markers[$size][$color] > 0) {
        $beautiful++;
        $closed++;
        $markers[$size][$color]--;
        $markers[$size]["closed"]--;
        unset($caps[$i]);
    }

}

foreach($caps as $cap) {
    if( $markers[$cap[1]]["closed"] > 0 ) {
        $closed++;
        $markers[$cap[1]]["closed"]--;
    }
}

echo $closed. " ".$beautiful;

?>

<?php
$arr = array();
while ($line = trim(fgets(STDIN)))
    $arr[] = $line;
$count = explode(' ', $arr[0]);
$CountMarker[0] = 0;
$CountMarker[1] = 0;
$CountColor = array();
$StackBad = array();
$curr = array();
$res = array();
for ($i = 1; $i <= $count[0]; $i++) {
    $exp = explode(" ", $arr[$i]);
    if(isset($curr[$exp[1]][$exp[0]]))
        $curr[$exp[1]][$exp[0]]++;
    else
        $curr[$exp[1]][$exp[0]] = 1;
    if(isset($CountColor[$exp[1]]))
        $CountColor[$exp[1]]++;
    else
        $CountColor[$exp[1]] = 1;
}
for ($i = $count[0] + 1; $i <= $count[0] + $count[1]; $i++) {
    $exp = explode(" ", $arr[$i]);
    if(isset($curr[$exp[1]][$exp[0]]) && $curr[$exp[1]][$exp[0]] > 0){
        $curr[$exp[1]][$exp[0]]--;
        $CountColor[$exp[1]]--;
        $CountMarker[0]++;
        $CountMarker[1]++;
    } else{
        if(isset($StackBad[$exp[1]]))
            $StackBad[$exp[1]]++;
        else
            $StackBad[$exp[1]] = 1;
    }
}
foreach($StackBad AS $key => $value){
    if($CountColor[$key] >= $value)
        $CountMarker[0]+= $value;
    else
        $CountMarker[0]+= $CountColor[$key];
}
echo $CountMarker[0]. ' '. $CountMarker[1] . "\n";

<?php
fscanf(STDIN, "%d %d\n", $iMarkers, $iCaps);

$aMarkers = array();
$aCaps = array();
$aUnbeautifulMarkers = array();
$aUnbeautifulCaps = array();
$iBeautifulCappedMarkers = 0;
$iCappedMarkers = 0;

for ($i = 1; $i <= $iMarkers; $i ++) {
	fscanf(STDIN, "%d %d", $iColor, $iDiameter);

	if (!isset($aMarkers[$iDiameter])) {
		$aMarkers[$iDiameter] = array();
		$aUnbeautifulMarkers[$iDiameter] = 1;
	} else {
		$aUnbeautifulMarkers[$iDiameter] += 1;
	}

	if (isset($aMarkers[$iDiameter][$iColor])) {
		$aMarkers[$iDiameter][$iColor] += 1;
	} else {
		$aMarkers[$iDiameter][$iColor] = 1;
	}
}

for ($i = 1; $i <= $iCaps; $i ++) {
	fscanf(STDIN, "%d %d", $iColor, $iDiameter);

	if (!isset($aCaps[$iDiameter])) {
		$aCaps[$iDiameter] = array();
		$aUnbeautifulCaps[$iDiameter] = 1;
	} else {
		$aUnbeautifulCaps[$iDiameter] += 1;
	}

	if (isset($aCaps[$iDiameter][$iColor])) {
		$aCaps[$iDiameter][$iColor] += 1;
	} else {
		$aCaps[$iDiameter][$iColor] = 1;
	}
}

foreach ($aMarkers as $iMarkerDiameter => $aMarkerColors) {
	foreach ($aMarkerColors as $iMarkerColor => $iTheMarkers) {

		if (isset($aCaps[$iMarkerDiameter][$iMarkerColor]) && $aCaps[$iMarkerDiameter][$iMarkerColor] > 0) {
			$iTheCaps = $aCaps[$iMarkerDiameter][$iMarkerColor];
			$iCapped = min($iTheCaps, $iTheMarkers);

			$iBeautifulCappedMarkers += $iCapped;
		}
	}
}

foreach ($aUnbeautifulMarkers as $iMarkerDiameter => $iTheMarkers) {
	if (isset($aUnbeautifulCaps[$iMarkerDiameter]) && $aUnbeautifulCaps[$iMarkerDiameter] > 0) {
		$iTheCaps = $aUnbeautifulCaps[$iMarkerDiameter];

		$iCapped = min($iTheCaps, $iTheMarkers);

		$iCappedMarkers += $iCapped;
	}
}

echo $iCappedMarkers . ' ' . $iBeautifulCappedMarkers;

<?php
error_reporting("off");
//159B: Matchmaker
list($n, $m) = explode(" ", trim(fgets(STDIN)));
$y = array();
$b = array();
$markerhash = array();
$caphash = array();
$capped = 0;
$beautifully = 0;
for ($i = 0; $i < $n; $i++) {
    list($color, $d) = explode(" ", trim(fgets(STDIN)));
    $y[] = $d;
    $markerhash[] = $d * 10000 + $color;
}
for ($i = 0; $i < $m; $i++) {
    list($color, $d) = explode(" ", trim(fgets(STDIN)));
    $b[] = $d;
    $caphash[] = $d * 10000 + $color;
}

$markerdiameters = array_count_values($y);
$capdiameters = array_count_values($b);

foreach ($markerdiameters as $key => $value) {
    if (isset($capdiameters[$key])) {
        $tmp = array();
        $tmp[] = $capdiameters[$key];
        $tmp[] = $markerdiameters[$key];
        $capped += min($tmp);
    }
}

$mhashcount = array_count_values($markerhash);
$chashcount = array_count_values($caphash);

foreach ($mhashcount as $key => $value) {
    if (isset($chashcount[$key])) {
        $tmp = array();
        $tmp[] = $chashcount[$key];
        $tmp[] = $mhashcount[$key];
        $beautifully += min($tmp);
    }
}

echo $capped." ".$beautifully;
?>

<?php

$closed = 0;
$beautiful = 0;
error_reporting(0);

$stdin = fopen("php://stdin", "r");
$line1 = trim(fgets($stdin));
$line1Array = explode(' ', $line1);
$n = (int)$line1Array[0];
$m = (int)$line1Array[1];

$markers = array();
for($i = 0; $i < $n; $i++) {
    $marker = trim(fgets($stdin));
    $marker = explode(' ', $marker);

    $markers[$marker[1]]["closed"]++;
    $markers[$marker[1]][$marker[0]]++;
}

//var_dump($markers);

$caps = array();
for($i = 0; $i < $m; $i++) {
    $caps[$i] = trim(fgets($stdin));
    $caps[$i] = explode(' ', $caps[$i]);

    if( $markers[ $caps[$i][1] ][ $caps[$i][0] ] > 0) {
        $beautiful++;
        $closed++;
        $markers[ $caps[$i][1] ][ $caps[$i][0] ]--;
        $markers[ $caps[$i][1] ]["closed"]--;
    }
    elseif( $markers[ $caps[$i][1] ]["closed"] > 0 ) {
        $closed++;
        $markers[ $caps[$i][1] ][ $caps[$i][0] ]--;
        $markers[ $caps[$i][1] ]["closed"]--;
    }
}

echo $closed. " ".$beautiful;

/*
5 5
1 5
2 4
1 5
3 4
1 6
3 4
2 4
6 4
8 5
1 6

5: [1]
4: [2,3]
6: [1]
*/

/*

5 5
1 2
1 3
1 2
1 3
1 3
1 2
1 2
1 3
1 2
1 2

*/

?>


<?php
//$file = fopen('input.in', 'r');
$file = STDIN;
list($n, $m) = explode(" ", trim(fgets($file)));
$s = trim(fgets($file));
$aArray = array_fill(ord('a'), 26, array());
for ($i = 0; $i < $n; $i++) {
    $aArray[ord($s[$i])][] = $i;
}
while ($m--) {
    list($x, $y) = explode(" ", trim(fgets($file)));
    $x = ord($x);
    $y = ord($y);
    $temp = $aArray[$x];
    $aArray[$x] = $aArray[$y];
    $aArray[$y] = $temp;
}
$o = array();
foreach ($aArray as $c => $pArray) {
    if (count($pArray)==0) {
        continue;
    }
    foreach ($pArray as $p) {
        $o[$p] = chr($c);
    }
}
for ($i = 0; $i < $n; $i++) {
    echo $o[$i];
}


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

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

$stillEq = array_fill(0, $n, true);
$dels = 0;

for ($j = 0; $j < $m; $j++) {
  $ok = true;
  $nStillEq = $stillEq;
  for ($i = 1; $i < $n; $i++) {
    if (! $stillEq[$i]) {
      continue;
    }
    if (ord($a[$i][$j]) > ord($a[$i - 1][$j])) {
      $nStillEq[$i] = false;
    } elseif (ord($a[$i][$j]) < ord($a[$i - 1][$j])) {
      $dels++;
      $ok = false;
      break;
    }
  }
  if ($ok) {
    $stillEq = $nStillEq;
  }
}

echo "$dels\n";


<?php

$tests = getLineInt();

$results = array();

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

    $a = getLineInts();

    $openL = false;

    $openLValue = 0;

    $valeyCount = 0;

    for ($index = 0; $index < $n; $index++)
    {
        if ($index == 0 || $a[$index - 1] > $a[$index])
        {
            $openL = true;
            $openLValue = $a[$index];
        }

        if ($openL && $openLValue != $a[$index])
        {
            $openL = false;
            $openLValue = 0;
        }

        if ($openL && ($index == $n - 1 || $a[$index + 1] > $a[$index]))
        {
            $openL = false;
            $openLValue = 0;

            $valeyCount++;
        }
    }

    $results[$test] = $valeyCount == 1 ? "YES" : "NO";
}

echo(implode(PHP_EOL, $results));

// Helpers

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

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

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

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

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

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

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

    return array($max1, $max2);
}

<?php
$T = (int)fgets(STDIN);
for ($t = 0; $t < $T; $t++) {
    $n = (int)fgets(STDIN);
    $ary = array_map('intval', explode(" ", fgets(STDIN)));
    $mn = 0;
    for($i = 0; $i < $n; $i++)
        if ($ary[$i] < $ary[$mn])
            $mn = $i;
    $can = true;
    for($i = $mn; $i > 0; $i--)
        if ($ary[$i-1] < $ary[$i])
            $can = false;
    for($i = $mn; $i < $n-1; $i++)
        if ($ary[$i] > $ary[$i+1])
            $can = false;
    echo ($can ? "YES" : "NO") . PHP_EOL;
}