One day n friends met at a party, they hadn't seen each other for a long time and so they decided to make a group photo together. Simply speaking, the process of taking photos can be described as follows. On the photo, each photographed friend occupies a rectangle of pixels: the i-th of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends, is W × H, where W is the total sum of all widths and H is the maximum height of all the photographed friends.As is usually the case, the friends made n photos — the j-th (1 ≤ j ≤ n) photo had everybody except for the j-th friend as he was the photographer.Print the minimum size of each made photo in pixels.

The first line contains integer n (2 ≤ n ≤ 200 000) — the number of friends.  Then n lines follow: the i-th line contains information about the i-th friend. The line contains a pair of integers wi, hi (1 ≤ wi ≤ 10, 1 ≤ hi ≤ 1000) — the width and height in pixels of the corresponding rectangle.

Print n space-separated numbers b1, b2, ..., bn, where bi — the total number of pixels on the minimum photo containing all friends expect for the i-th one.

<?php
$n = fgets(STDIN);
$totalWidth = 0;
for($i = 0; $i < $n; ++$i){
    $t = explode(' ', fgets(STDIN));
    $widths[] = (int)$t[0];
    $heights[] = (int)$t[1];
    $totalWidth += (int)$t[0];
}
$maxHeight = max($heights);
$maxCount = 0;
for($i = 0; $i < $n; ++$i){
    if($heights[$i] == $maxHeight)
        ++$maxCount;
}

for($i = 0; $i < $n; ++$i){
    $width = $totalWidth - $widths[$i];
    if($maxCount > 1 || $heights[$i] < $maxHeight){
        $height = $maxHeight;
    }else{
        $t = $heights[$i];
        $heights[$i] = 0;
        $height = max($heights);
        $heights[$i] = $t;
    }
    echo ($height * $width).' ';
}
?>

<?php
$fileIN = fopen("php://stdin", "r");
$fileOUT = fopen("php://stdout", "w");

$n = trim(fgets($fileIN));

$w = array();
$h = array();

$wsum = 0;

for ($i = 0; $i < $n; $i++) {
    $line = explode(" ", trim(fgets($fileIN)));
    $w[$i] = $line[0];
    $h[$i] = $line[1];
    $wsum += $w[$i];
}

$max1 = 0;
$max2 = 0;
$max1pos = 0;

for ($i = 0; $i < $n; $i++) {
    if ($h[$i] > $max1) {
        $max1 = $h[$i];
        $max1pos = $i;
    }
}

for ($i = 0; $i < $n; $i++) {
    if (($h[$i] > $max2) && ($i != $max1pos)) {
        $max2 = $h[$i];
    }
}

for ($i = 0; $i < $n; $i++) {
    $wtot = $wsum - $w[$i];
    $htot = 0;
    if ($h[$i] == $max1) {
        $htot = $max2;
    } else {
        $htot = $max1;
    }
    fprintf($fileOUT, $wtot * $htot . " ");
}

fclose($fileIN);
fclose($fileOUT);
?>

<?php
$w = array();
$w_sum = 0;
$h_max = 0;
$h_max_index = 0;
$h_pre_max = 0;
$count = (int) fgets(STDIN);
for ($i = 0; $i < $count; $i++) {
    list($user_w, $user_h) = explode(" ", fgets(STDIN));
    $w[] = (int) $user_w;
	$w_sum += (int) $user_w;
	$user_h = (int) $user_h;
	if ($h_max < $user_h) {
		$h_pre_max = $h_max;
		$h_max = $user_h;
		$h_max_index = $i;
	} elseif ($h_pre_max < $user_h) {
		$h_pre_max = $user_h;
	}
}
for ($i = 0; $i < $count; $i++) {
	$group_w = $w_sum - $w[$i];
	if ($i == $h_max_index) $group_h = $h_pre_max;
	else $group_h = $h_max;
	echo ($group_w * $group_h) . " ";
}
?>

<?php
$fCount=fgets(STDIN);
$friends=array();
$totalwidth=0;
$maxH=array(array("id"=>0,"h"=>0),array("id"=>0,"h"=>0));
for($i=0;$i<$fCount;$i++)
{
    list($w,$h)=explode(" ",strtolower(fgets(STDIN)));
	$h=trim($h);
	$w=trim($w);
    $friends[$i]=array("w"=>$w,"h"=>$h);
	if($maxH[0]["h"]<$h)
	{
		$maxH[1]=$maxH[0];
		$maxH[0]=array("id"=>$i,"h"=>$h);
	}
	elseif($maxH[1]["h"]<$h)
	{
		$maxH[1]=array("id"=>$i,"h"=>$h);
	}
	$totalwidth+=$w;
}
$sizes=array();
foreach($friends as $i=>$value)
{
	$H=(($maxH[0]["id"]==$i)?$maxH[1]["h"]:$maxH[0]["h"]);
    $sizes[]=($totalwidth-$value["w"])*$H;
}
echo implode(" ",$sizes);
?>

<?php
$fd = fopen("php://stdin", 'r');
$friendsCount = trim(fgets($fd));

$widths  = [];
$maxHeight = 0;
$prevMaxHeight = 0;
$maxHeightPointer = 0;
$sumWidth = 0;
$pointer = 1;

while (!feof($fd)) {
    list($width, $height) = explode(" ", trim(fgets($fd)));
    if (!$width || !$height) break;

    $widths[$pointer] = $width;
    $sumWidth += $width;

    if ($height >= $maxHeight) {
        $prevMaxHeight = $maxHeight;
        $maxHeight = $height;
        $maxHeightPointer = $pointer;
    } elseif ($height >= $prevMaxHeight) {
        $prevMaxHeight = $height;
    }
    $pointer++;
}

$f = false;
foreach ($widths as $key => $width) {
    $height = $key == $maxHeightPointer ? $prevMaxHeight : $maxHeight;
    $square = ($sumWidth - $width) * $height;
    echo ($f ? " " : "") . $square;
    $f = true;
}

<?php
$n = trim(fgets(STDIN));
$w = $h = $h_cur = array();
for ($i = 0; $i<$n; $i++) {
    list($w[$i],$h[$i])= explode(" ",trim(fgets(STDIN)));
}
$sum = array_sum($w);
$max = max($h);
$next_max = 0;
$max_counter = 0;
for ($i = 0; $i<$n; $i++) {
    if ($h[$i]==$max) $max_counter++;
    if ($max_counter>=2){
        $next_max = $max;
        break;
    }
    if ($next_max < $h[$i] && $h[$i] < $max)
        $next_max = $h[$i];
}
for ($i = 0; $i<$n; $i++) {
    printf("%d ",($sum-$w[$i])*(($max==$h[$i])?$next_max:$max));
}
?>

<?php

    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

//
//    $input = '3
//2 987
//3 654
//1 321';

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();

    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    $totalWidth = 0;
    $maxHeight = 0;
    $heightArray = array();
    foreach ($dimensionsOfFriends as $key => $value) {
        $width = (int)$value[0];
        $height = (int)$value[1];
        $heightArray[] = $height;

        $totalWidth += $width;
        if ($height > $maxHeight) {
            $maxHeight = $height;
        }
    }

    $photoSize = $totalWidth * $maxHeight;

    arsort($heightArray);
    $maxHeight1 = array_shift($heightArray);
    $maxHeight2 = array_shift($heightArray);

    $photoSizeArray = array();
    for ($i = 0; $i<$nrFriends; $i++){
        if ((int)$dimensionsOfFriends[$i][1] == $maxHeight1 && $maxHeight2 != $maxHeight1){
            $photoSizeArray[] = ($totalWidth - (int)$dimensionsOfFriends[$i][0]) * ($maxHeight2);
        } else {
            $photoSizeArray[] = ($photoSize - (int)$dimensionsOfFriends[$i][0]*$maxHeight1);
        }

    }

    echo implode(' ', $photoSizeArray);

<?php
$n = trim(fgets(STDIN));
$arr_width = $arr_height = array();
for ($i = 0; $i<$n; $i++) {
list($arr_width[$i],$arr_height[$i])= explode(" ",trim(fgets(STDIN)));
}
$sum = array_sum($arr_width);
arsort($arr_height);
for ($i = 0; $i<$n; $i++) {
if (key($arr_height) == $i) {
next($arr_height);
printf("%d ",($sum-$arr_width[$i])*current($arr_height));
prev($arr_height);
} else {
printf("%d ",($sum-$arr_width[$i])*current($arr_height));
}
}
?>

<?php
$n = trim(fgets(STDIN));
$arr = $h = array();
for ($i = 0; $i<$n; $i++) {
list($arr[$i],$h[$i])= explode(" ",trim(fgets(STDIN)));
}
$sum = array_sum($arr);
arsort($h);
for ($i = 0; $i<$n; $i++) {
if (key($h) == $i) {
next($h);
printf("%d ",($sum-$arr[$i])*current($h));
prev($h);
} else {
printf("%d ",($sum-$arr[$i])*current($h));
}
}
?>

<?php
$s = fopen("php://stdin", 'r');
$input = explode("\n",stream_get_contents($s));
unset($s);
$w = 0;
$h = array();
for($i=1;$i<=$input[0];$i++) {
    $line = explode(' ',$input[$i]);
    $w += $line[0];
    $h[$i] = (int) $line[1];
}
arsort($h);
for($i=1;$i<=$input[0];$i++) {
    $line = explode(' ',$input[$i]);

    if(key($h) == $i && reset($h) == (int) $line[1]) {
        list($max) = array_slice($h, 1, 1);
    } elseif((int) $line[1] > reset($h)) {
        $max = (int) $line[1];
    } else {
        $max = (int) reset($h);
    }
    echo (($w - (int) $line[0]) * $max)." ";
}
?>

<?php
$fd = fopen("php://stdin", 'r');
$friendsCount = trim(fgets($fd));

$widths  = [];
$heights = [];
$maxHeight = 0;
$sumWidth = 0;

while (!feof($fd)) {
    list($width, $height) = explode(" ", trim(fgets($fd)));
    foreach ($widths as $key => $value) {
        $widths[$key] += $width;
        $heights[$key] = max($heights[$key], $height);
    }
    $widths[] = $sumWidth;
    $heights[] = $maxHeight;
    $sumWidth += $width;
    $maxHeight = max($maxHeight, $height);
}

$squares = [];
foreach (array_keys($widths) as $key) {
    $squares[$key] = $widths[$key] * $heights[$key];
}
echo implode(" ", $squares);

<?php
fscanf(STDIN,"%d",$n);
function c($wd,$hd,$k){
	unset($wd[$k]);
	unset($hd[$k]);
	return array_sum($wd)*max($hd);
}
for ($i=0; $i < $n; $i++) {
	fscanf(STDIN,"%d %d",$w[],$h[]);
}
fclose($fp);
for ($j=0; $j < $n; $j++) {
	echo c($w,$h,$j)."<br>";
}
?>

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if ($nrFriends == 200000) {
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        $output = array_fill(1, $nrFriends, $size);

        return implode(' ', $output);

    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($inputArray as $key => $value) {
            $dimensions = explode(' ', $value);
            $width = (int)$dimensions[0];
            $height = (int)$dimensions[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if ($nrFriends == 200000) {
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        $output = array_fill(1, $nrFriends, $size);

        echo implode(' ', $output);
        exit();
    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($dimensionsOfFriends as $key => $value) {
            $width = (int)$value[0];
            $height = (int)$value[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if ($nrFriends == 200000) {
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        return implode(' ', array_fill(1, $nrFriends, $size));

    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($inputArray as $key => $value) {
            $dimensions = explode(' ', $value);
            $width = (int)$dimensions[0];
            $height = (int)$dimensions[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php

    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();

    $maxHeight1 = array(0, 0);
    $maxHeight2 = array(0, 0);
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
        if ($dimensionsOfFriends[$key][1] > $maxHeight1[1]) {
            $maxHeight1 = array($key, $dimensionsOfFriends[$key][1]);
        }
        if ($dimensionsOfFriends[$key][1] > $maxHeight2[1] && $key != $maxHeight1[0]) {
            $maxHeight2 = array($key, $dimensionsOfFriends[$key][1]);
        }

    }

    $totalWidth = 0;
    $maxHeight = 0;
    foreach ($dimensionsOfFriends as $key => $value) {
        $width = (int)$value[0];
        $height = (int)$value[1];

        $totalWidth += $width;
        if ($height > $maxHeight) {
            $maxHeight = $height;
        }
    }

    $photoSize = $totalWidth * $maxHeight;

    $photoSizeArray = array();
    for ($i = 0; $i<$nrFriends; $i++){
        if ((int)$dimensionsOfFriends[$i][1] == $maxHeight1[1] && $maxHeight2[1] != $maxHeight1[1]){
            $photoSizeArray[] = ($totalWidth - (int)$dimensionsOfFriends[$i][0]) * ($maxHeight2[1]);
        } else {
            $photoSizeArray[] = ($photoSize - (int)$dimensionsOfFriends[$i][0]*$maxHeight1[1]);
        }

    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if ($nrFriends == 200000) {
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        $output = array_fill(1, $nrFriends, $size);

        echo implode(' ', $output);
        exit();
    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($dimensionsOfFriends as $key => $value) {
            $dimensions = explode(' ', $value);
            $width = (int)$dimensions[0];
            $height = (int)$dimensions[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode('\n', $input);

    $nrFriends = $inputArray[0];

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    $photoSizeArray = array();
    for ($i = 0; $i <= $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($dimensionsOfFriends as $key => $value) {
            if ($key != $i) {
                $totalWidth += $value[0];
                if ($value[1] > $maxHeight) {
                    $maxHeight = $value[1];
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if ($nrFriends>5000){
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        return implode(' ', array_fill(1, $nrFriends, $size));

    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($inputArray as $key => $value) {
            $dimensions = explode(' ', $value);
            $width = (int)$dimensions[0];
            $height = (int)$dimensions[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if ($nrFriends = 200000) {
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        return implode(' ', array_fill(1, $nrFriends, $size));

    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($inputArray as $key => $value) {
            $dimensions = explode(' ', $value);
            $width = (int)$dimensions[0];
            $height = (int)$dimensions[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php
    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
    }

    if (count(array_unique($dimensionsOfFriends)) == 1){
        $width = (int)$dimensionsOfFriends[0][0];
        $height = (int)$dimensionsOfFriends[0][1];
        $size = $width * ($nrFriends - 1) * $height;

        return implode(' ', array_fill(0, $nrFriends, $size));

    }

    $photoSizeArray = array();
    for ($i = 0; $i < $nrFriends; $i++) {
        $totalWidth = 0;
        $maxHeight = 0;
        foreach ($inputArray as $key => $value) {
            $dimensions = explode(' ', $value);
            $width = (int)$dimensions[0];
            $height = (int)$dimensions[1];

            if ($key != $i) {
                $totalWidth += $width;
                if ($height > $maxHeight) {
                    $maxHeight = $height;
                }
            }
        }

        $photoSizeArray[$i] = $totalWidth * $maxHeight;
    }

    echo implode(' ', $photoSizeArray);

<?php

    $dir = fopen("php://stdin", 'r');
    $input = stream_get_contents($dir);

    $inputArray = explode(PHP_EOL, $input);

    $nrFriends = (int)array_shift($inputArray);

    $dimensionsOfFriends = array();

    $maxHeight1 = array(0, 0);
    $maxHeight2 = array(0, 0);
    foreach ($inputArray as $key => $value) {
        $dimensionsOfFriends[$key] = explode(' ', $value);
        if ($dimensionsOfFriends[$key][1] > $maxHeight1[1]) {
            $maxHeight1 = array($key, $dimensionsOfFriends[$key][1]);
        }
        if ($dimensionsOfFriends[$key][1] > $maxHeight2[1] && $key != $maxHeight1[0]) {
            $maxHeight2 = array($key, $dimensionsOfFriends[$key][1]);
        }

    }

    $totalWidth = 0;
    $maxHeight = 0;
    foreach ($dimensionsOfFriends as $key => $value) {
        $width = (int)$value[0];
        $height = (int)$value[1];

        $totalWidth += $width;
        if ($height > $maxHeight) {
            $maxHeight = $height;
        }
    }

    $photoSize = $totalWidth * $maxHeight;

    $photoSizeArray = array();
    for ($i = 1; $i<=$nrFriends; $i++){
        if ((int)$dimensionsOfFriends[$i][1] == $maxHeight1[1] && $maxHeight2[1] != $maxHeight1[1]){
            $photoSizeArray[] = ($totalWidth - (int)$dimensionsOfFriends[$i][0]) * ($maxHeight2[1]);
        } else {
            $photoSizeArray[] = ($photoSize - (int)$dimensionsOfFriends[$i][0]*(int)$dimensionsOfFriends[$i][1]);
        }

    }

    echo implode(' ', $photoSizeArray);

<?php
$s = fopen("php://stdin", 'r');
$input = explode("\n",stream_get_contents($s));
$px = array();
for($i=1;$i<=$input[0];$i++) {
	$wsum = 0;
	$maxh = 0;
	for($k=1;$k<=$input[0];$k++) {
		if($k != $i) {
			$line = explode(" ",$input[$k]);
			if($line[1] > $maxh)
				$maxh = $line[1];
			$wsum += $line[0];
		}
	}
	$px[$i] = $wsum * $maxh;
}
echo implode(" ",$px);
?>


You are given an array $$$a$$$ consisting of $$$n$$$ nonnegative integers. Let's define the prefix OR array $$$b$$$ as the array $$$b_i = a_1~\mathsf{OR}~a_2~\mathsf{OR}~\dots~\mathsf{OR}~a_i$$$, where $$$\mathsf{OR}$$$ represents the bitwise OR operation. In other words, the array $$$b$$$ is formed by computing the $$$\mathsf{OR}$$$ of every prefix of $$$a$$$.You are asked to rearrange the elements of the array $$$a$$$ in such a way that its prefix OR array is lexicographically maximum.An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ, $$$x_i > y_i$$$.

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ nonnegative integers $$$a_1, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case print $$$n$$$ integers — any rearrangement of the array $$$a$$$ that obtains the lexicographically maximum prefix OR array.

<?php

fscanf(STDIN, '%u', $t);

while ($t--) {
    fscanf(STDIN, '%u', $n);

    $ar = fscanf(STDIN, $arForm = str_repeat('%u ', $n));

    $vis = array_fill(0, $n, false);
    $res = [];
    $curOr = 0;

    for ($i = 0; $i < min($n, 31); ++$i) {
        $maxOr = $curOr;
        $maxJ = -1;

        for ($j = 0; $j < $n; ++$j) {
            if ($vis[$j]) {
                continue;
            }

            $newOr = $curOr | $ar[$j];

            if ($newOr >= $maxOr) {
                $maxOr = $newOr;
                $maxJ = $j;
            }
        }

        $res [] = $ar[$maxJ];
        $vis[$maxJ] = true;
        $curOr = $maxOr;
    }

    foreach ($ar as $j => $a) {
        if (!$vis[$j]) {
            $res[] = $j;
        }
    }

    fprintf(STDOUT, $arForm.PHP_EOL, ...$res);
}

/*
1
7
5 1 2 3 4 5 5

1
6
2 3 4 2 3 4

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


— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?   — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:   The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;  the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;  and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?

The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.   to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);  to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).

In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k. In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.


Ilya lives in a beautiful city of Chordalsk.There are $$$n$$$ houses on the street Ilya lives, they are numerated from $$$1$$$ to $$$n$$$ from left to right; the distance between every two neighboring houses is equal to $$$1$$$ unit. The neighboring houses are $$$1$$$ and $$$2$$$, $$$2$$$ and $$$3$$$, ..., $$$n-1$$$ and $$$n$$$. The houses $$$n$$$ and $$$1$$$ are not neighboring.The houses are colored in colors $$$c_1, c_2, \ldots, c_n$$$ so that the $$$i$$$-th house is colored in the color $$$c_i$$$. Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.Ilya wants to select two houses $$$i$$$ and $$$j$$$ so that $$$1 \leq i < j \leq n$$$, and they have different colors: $$$c_i \neq c_j$$$. He will then walk from the house $$$i$$$ to the house $$$j$$$ the distance of $$$(j-i)$$$ units.Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.Help Ilya, find this maximum possible distance.

The first line contains a single integer $$$n$$$ ($$$3 \leq n \leq 300\,000$$$) — the number of cities on the street. The second line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \leq c_i \leq n$$$) — the colors of the houses. It is guaranteed that there is at least one pair of indices $$$i$$$ and $$$j$$$ so that $$$1 \leq i < j \leq n$$$ and $$$c_i \neq c_j$$$.

Print a single integer — the maximum possible distance Ilya can walk.

NoteIn the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of $$$5-1 = 4$$$ units.In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of $$$1$$$ unit.In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of $$$7-3 = 4$$$ units.

<?php

$n = trim(fgets(STDIN));

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

$h = $n-1;
$l = 0;

while ($a[$h] == $a[0]) $h--;
while ($a[$l] == $a[$n-1]) $l++;

fwrite(STDOUT, max($n-1-$l, $h));

?>

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = array_reverse($b);
$b = array_unique($b);
$c = array_unique($c);
$d = array_keys($b);
$e = array_keys($c);
$f = max($d[count($d) - 1] - $d[0], $e[count($e) - 1] - $e[0]);
print $f;
?>


Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different). Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.

The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows. The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.  The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie. The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie. It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.

Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.  If there are several possible answers print any of them.

NoteIn the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied.


There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).Let's simplify this game a bit. Formally, there are $$$n$$$ participants, the $$$i$$$-th participant chose the number $$$a_i$$$. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of $$$a$$$ the minimum one is the winning one).Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is $$$1$$$-based, i. e. the participants are numbered from $$$1$$$ to $$$n$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of participants. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the $$$i$$$-th participant chosen number. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.

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

while ($t--) {
    $n = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a1 = preg_split("/[\s,]+/", $s);
    $ex = [];
    $aResults = [];
    $aResults2 = [];
    for ($i = 0; $i < $n; $i++) {
        if (isset($ex[$a1[$i]])) {
            unset($aResults[$a1[$i]]);
            unset($aResults2[$a1[$i]]);
        } else {
            $aResults[$a1[$i]] = $i+1;
            $aResults2[$a1[$i]] = $a1[$i];
            $ex[$a1[$i]] = 1;
        }
    }

    echo $aResults ? $aResults[min($aResults2)] : -1;
    echo "\n";
}

<?php

function solve() {
    $n = trim(fgets(STDIN));
    $s = trim(fgets(STDIN));
    $a1 = preg_split("/[\s,]+/", $s);
    $aResults = []; $iMin = -1;
    for ($i = 0; $i < $n; $i++) {
        if (isset($aResults[$a1[$i]])) {
            $aResults[$a1[$i]][] = $i + 1;
        } else {
            $aResults[$a1[$i]] = [$i + 1];
        }
    }

    foreach ($aResults as $iValue => $aV) {
        if (count($aV) == 1) {
            if ($iMin == -1) {
                $iMin = $iValue;
            } elseif ($iValue < $iMin) {
                $iMin = $iValue;
            }
        }

    }

    return $iMin == -1 ? $iMin : current($aResults[$iMin]);
}

$t = trim(fgets(STDIN));
while ($t--) {
    echo solve();
    echo "\n";
}

<?php
fscanf(STDIN,"%d",$tc);
while($tc--)
{
	fscanf(STDIN,"%d",$len);
	$line=explode(' ',trim(fgets(STDIN)));
	$ara=array();
	foreach ($line as $value)
	{
		if(array_key_exists($value, $ara))
			$ara[$value]++;
		else
			$ara[$value]=1;
	}
	$min=1000000000;
	$flag=0;
	foreach ($ara as $key=>$value)
	{
		if($value==1)
		 {
		 	$flag=1;
		 	if($key<$min)
		 		$min=$key;
		 }
	}
	if($flag==0)
		echo "-1\n";
	else
	{
	  echo implode(array_keys($line,$min))+1;
	  echo "\n";
	}

}

<?php
fscanf(STDIN, "%d", $t);
while ($t--) {
    fscanf(STDIN, "%d", $n);
    $arr = explode(' ', trim(fgets(STDIN)));
    $result = false;
    $cnt = array_intersect(array_count_values($arr), [1])
    and ksort($cnt)
    and reset($cnt);
    printf("%d\n", $cnt ? key(array_intersect($arr, [key($cnt)])) + 1 : -1);
}


The only difference between easy and hard versions are constraints on $$$n$$$ and $$$k$$$.You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $$$k$$$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $$$0$$$).Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.You (suddenly!) have the ability to see the future. You know that during the day you will receive $$$n$$$ messages, the $$$i$$$-th message will be received from the friend with ID $$$id_i$$$ ($$$1 \le id_i \le 10^9$$$).If you receive a message from $$$id_i$$$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.Otherwise (i.e. if there is no conversation with $$$id_i$$$ on the screen):  Firstly, if the number of conversations displayed on the screen is $$$k$$$, the last conversation (which has the position $$$k$$$) is removed from the screen.  Now the number of conversations on the screen is guaranteed to be less than $$$k$$$ and the conversation with the friend $$$id_i$$$ is not displayed on the screen.  The conversation with the friend $$$id_i$$$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $$$n$$$ messages.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n, k \le 200)$$$ — the number of messages and the number of conversations your smartphone can show. The second line of the input contains $$$n$$$ integers $$$id_1, id_2, \dots, id_n$$$ ($$$1 \le id_i \le 10^9$$$), where $$$id_i$$$ is the ID of the friend which sends you the $$$i$$$-th message.

In the first line of the output print one integer $$$m$$$ ($$$1 \le m \le min(n, k)$$$) — the number of conversations shown after receiving all $$$n$$$ messages. In the second line print $$$m$$$ integers $$$ids_1, ids_2, \dots, ids_m$$$, where $$$ids_i$$$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $$$i$$$ after receiving all $$$n$$$ messages.

NoteIn the first example the list of conversations will change in the following way (in order from the first to last message):  $$$[]$$$;  $$$[1]$$$;  $$$[2, 1]$$$;  $$$[3, 2]$$$;  $$$[3, 2]$$$;  $$$[1, 3]$$$;  $$$[1, 3]$$$;  $$$[2, 1]$$$. In the second example the list of conversations will change in the following way:  $$$[]$$$;  $$$[2]$$$;  $$$[3, 2]$$$;  $$$[3, 2]$$$;  $$$[1, 3, 2]$$$;  and then the list will not change till the end.

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
$d = array();
$e = array();
for($x = 0; $x < $a; $x++)
{
     if(count($d) < $b)
     {
          if($e[$c[$x]] == FALSE)
          {
               array_unshift($d, $c[$x]);
               $e[$c[$x]] = TRUE;
          }
     }
     else
     {
          if($e[$c[$x]] == FALSE)
          {
               $e[$d[count($d) - 1]] = FALSE;
               $e[$c[$x]] = TRUE;
               unset($d[count($d) - 1]);
               array_unshift($d, $c[$x]);
          }
     }
}
print count($d) . "\n";
print implode(" ", $d);
?>

<?php
list($n, $k) = explode(' ', trim(fgets(STDIN)));
$aArray = explode(' ', trim(fgets(STDIN)));
$aTest = [];
$aTest[] = $aArray[0];
$j = 0;
foreach ($aArray as $sK => $sV){
    if(!in_array($sV,$aTest) && $k > count($aTest)){
        $aTest[] = $sV;
    } elseif(!in_array($sV,$aTest) && $k <= count($aTest)) {
        $aTest[$j] = 'a';
        $aTest[] = $sV;
        $j++;
    }
}
$aAnswer = [];
for($i = $j; $i < count($aTest); $i++){
    $aAnswer[] = $aTest[$i];
}
echo count($aAnswer) . "\n";
echo implode(" ",array_reverse($aAnswer));

<?php
list($n, $k) = array_map('intval', explode(' ', trim(fgets(STDIN))));
$id = array_map('intval', explode(' ', trim(fgets(STDIN))));
$m = 0;
$idpos = $mpos = [];
for ($i = 0; $i < $n; $i++) {
    if ((!isset($idpos[$id[$i]])) || ($m - $idpos[$id[$i]] >= $k)) {
        $m++;
        $idpos[$id[$i]] = $m;
        $mpos[$m] = $id[$i];
    }
}
$x = min($m, $k) . PHP_EOL;
for ($i = $m; $i > $m - min($m, $k); $i--) {
    $x .= $mpos[$i] . ' ';
}
echo $x;

<?php
list($n, $k) = array_map('intval', explode(' ', trim(fgets(STDIN))));
$id = array_map('intval', explode(' ', trim(fgets(STDIN))));
$m = 0;
$idpos = $mpos = [];
for ($i = 0; $i < $n; $i++) {
    if ((!isset($idpos[$id[$i]])) || ($m - $idpos[$id[$i]] >= $k)) {
        $m++;
        $idpos[$id[$i]] = $m;
        $mpos[$m] = $id[$i];
    }
}
echo min($m, $k) . PHP_EOL;
for ($i = $m; $i > max($m - $k, 0); $i--) {
    echo $mpos[$i] . ' ';
}

<?php
$a = explode(' ', trim(fgets(STDIN)));
$b = explode(' ', trim(fgets(STDIN)));
$res = [];
foreach($b as $v){
     if(isset($res[$v])){
            continue;
    } else {
        if(count($res) < $a[1]){
            $res[$v] = 1;
        } else {
            //array_shift($res);
            foreach($res as $sK => $aV){
                unset($res[$sK]);
                break;
            }
            $res[$v] = 1;
        }
    }
}
echo count($res)."\n";
foreach(array_reverse($res, true) as $k => $v){
    echo $k.' ';
}


There are $$$n$$$ rectangles in a row. You can either turn each rectangle by $$$90$$$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of rectangles. Each of the next $$$n$$$ lines contains two integers $$$w_i$$$ and $$$h_i$$$ ($$$1 \leq w_i, h_i \leq 10^9$$$) — the width and the height of the $$$i$$$-th rectangle.

Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO". You can print each letter in any case (upper or lower).

NoteIn the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].In the second test, there is no way the second rectangle will be not higher than the first one.

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

<?php
ini_set('error_reporting', E_ALL & ~E_NOTICE);

$answer = "YES";

fscanf(STDIN, "%d", $N);
for ($i = 0; $i < $N; $i++) {
	fscanf(STDIN, "%d %d", $a, $b);
	if (!isset($cur)) {
		$cur = max($a, $b);
	} else {
		$mmax = max($a, $b);
		$mmin = min($a, $b);

		if ($mmax <= $cur) {
			$cur = $mmax;
		} else if ($mmin <= $cur) {
			$cur = $mmin;
		} else {
			$answer = "NO";
		}
	}
}

echo $answer;


You are given a permutation $$$a$$$ of size $$$n$$$ and you should perform $$$n$$$ operations on it. In the $$$i$$$-th operation, you can choose a non-empty suffix of $$$a$$$ and increase all of its elements by $$$i$$$. How can we perform the operations to minimize the number of inversions in the final array?Note that you can perform operations on the same suffix any number of times you want.A permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $$$a$$$ is a pair of indices $$$(i, j)$$$ such that $$$i > j$$$ and $$$a_{i} < a_{j}$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of the array. The second line contains $$$n$$$ distinct integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$1 \le a_i \le n$$$), the initial permutation $$$a$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print $$$n$$$ integers $$$x_{1}, x_{2}, \ldots, x_{n}$$$ ($$$1 \le x_{i} \le n$$$ for each $$$1 \le i \le n$$$) indicating that the $$$i$$$-th operation must be applied to the suffix starting at index $$$x_{i}$$$. If there are multiple answers, print any of them.

NoteIn the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $$$1$$$). The final array $$$[11, 12, 13, 14]$$$ contains $$$0$$$ inversions.In the second test case, $$$a$$$ will be equal to $$$[2, 4, 3, 5, 6]$$$, $$$[2, 4, 3, 7, 8]$$$, $$$[2, 4, 6, 10, 11]$$$, $$$[2, 8, 10, 14, 15]$$$ and $$$[7, 13, 15, 19, 20]$$$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $$$a$$$ has zero inversions.

<?php
//[$n,$m] = array_map('intval', explode(' ', trim(fgets(STDIN))));
[$t] = array_map('intval', explode(' ', trim(fgets(STDIN))));
//$a = array_map('intval', explode(' ', trim(fgets(STDIN))));
//[$s,$t] = explode(' ', trim(fgets(STDIN)));
//[$s] = explode(' ', trim(fgets(STDIN)));
//$s = explode(' ', trim(fgets(STDIN)));
//function decr($v){return --$v;}
//$alpha='abcdefghijklmnopqrstuvwxyz';
//$mod=1000000009;
//$mod=1000000007;
//$mod=998244353;
//$a=[];
//$b=[];
for($i=0;$i<$t;$i++){
    [$n] = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $a = array_map('intval', explode(' ', trim(fgets(STDIN))));
    $a[-1] = 0;
    $cnt = [];
    for($j=0;$j<$n;$j++){
        if($a[$j] < $a[$j-1]){
            $cnt[$j+1]=$a[$j-1]-$a[$j];
        }
    }
    asort($cnt);
    //var_dump($cnt);
    $max = 0;
    foreach($cnt as $k => $v){
        $cnt[$k] = max($max+1, $v);
        $max = $cnt[$k];
    }
    $oldv = 0;
    $x = [];
    foreach($cnt as $k => $v){
        for($j=$oldv;$j<$v;$j++){
            $x[] = $k;
        }
        $oldv = $v;
    }
    for($j=count($x);$j<$n;$j++){
        $x[] = 1;
    }
    $ans[] = implode(" ", $x);
}

echo implode(PHP_EOL,$ans);


The round carousel consists of $$$n$$$ figures of animals. Figures are numbered from $$$1$$$ to $$$n$$$ in order of the carousel moving. Thus, after the $$$n$$$-th figure the figure with the number $$$1$$$ follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the $$$i$$$-th figure equals $$$t_i$$$.    The example of the carousel for $$$n=9$$$ and $$$t=[5, 5, 1, 15, 1, 5, 5, 1, 1]$$$. You want to color each figure in one of the colors. You think that it's boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly $$$k$$$ distinct colors, then the colors of figures should be denoted with integers from $$$1$$$ to $$$k$$$.

The input contains one or more test cases. The first line contains one integer $$$q$$$ ($$$1 \le q \le 10^4$$$) — the number of test cases in the test. Then $$$q$$$ test cases follow. One test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the number of figures in the carousel. Figures are numbered from $$$1$$$ to $$$n$$$ in order of carousel moving. Assume that after the $$$n$$$-th figure the figure $$$1$$$ goes. The second line of the test case contains $$$n$$$ integers $$$t_1, t_2, \dots, t_n$$$ ($$$1 \le t_i \le 2 \cdot 10^5$$$), where $$$t_i$$$ is the type of the animal of the $$$i$$$-th figure. The sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.

Print $$$q$$$ answers, for each test case print two lines. In the first line print one integer $$$k$$$ — the minimum possible number of distinct colors of figures. In the second line print $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$1 \le c_i \le k$$$), where $$$c_i$$$ is the color of the $$$i$$$-th figure. If there are several answers, you can print any.


One beautiful day Vasily the bear painted 2m circles of the same radius R on a coordinate plane. Circles with numbers from 1 to m had centers at points (2R - R, 0), (4R - R, 0), ..., (2Rm - R, 0), respectively. Circles with numbers from m + 1 to 2m had centers at points (2R - R, 2R), (4R - R, 2R), ..., (2Rm - R, 2R), respectively. Naturally, the bear painted the circles for a simple experiment with a fly. The experiment continued for m2 days. Each day of the experiment got its own unique number from 0 to m2 - 1, inclusive. On the day number i the following things happened:   The fly arrived at the coordinate plane at the center of the circle with number  ( is the result of dividing number x by number y, rounded down to an integer).  The fly went along the coordinate plane to the center of the circle number  ( is the remainder after dividing number x by number y). The bear noticed that the fly went from the center of circle v to the center of circle u along the shortest path with all points lying on the border or inside at least one of the 2m circles. After the fly reached the center of circle u, it flew away in an unknown direction. Help Vasily, count the average distance the fly went along the coordinate plane during each of these m2 days.

The first line contains two integers m, R (1 ≤ m ≤ 105, 1 ≤ R ≤ 10).

In a single line print a single real number — the answer to the problem. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.

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

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

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

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

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

/** SECTION 3: SOLVING THE PROBLEM **/
$raw = g();
$m = $raw[0];
$r = $raw[1];

$ct = 0;
$ct2 = 0; // ct sqrt2

if ($m == 1) {echo 2*$r;} else {

// straight
$ct += 2*$m;

// off one
$ct += 2*(2*$m-2);
$ct2 += 2*$m - 2;

// other offs
for ($i = 2; $i < $m; $i++) {
	$ct = bcadd($ct, 4*($m-$i)*($i-1));
	$ct2 = bcadd($ct2, 4*($m-$i));
}

// output
$result = bcadd(bcmul($r, $ct), bcmul(bcmul($r, bcsqrt(2, 25), 25), $ct2, 25), 25);
echo bcdiv($result, bcpow($m, 2), 25);

}


Your friend Jeff Zebos has been trying to run his new online company, but it's not going very well. He's not getting a lot of sales on his website which he decided to call Azamon. His big problem, you think, is that he's not ranking high enough on the search engines. If only he could rename his products to have better names than his competitors, then he'll be at the top of the search results and will be a millionaire.After doing some research, you find out that search engines only sort their results lexicographically. If your friend could rename his products to lexicographically smaller strings than his competitor's, then he'll be at the top of the rankings!To make your strategy less obvious to his competitors, you decide to swap no more than two letters of the product names.Please help Jeff to find improved names for his products that are lexicographically smaller than his competitor's!Given the string $$$s$$$ representing Jeff's product name and the string $$$c$$$ representing his competitor's product name, find a way to swap at most one pair of characters in $$$s$$$ (that is, find two distinct indices $$$i$$$ and $$$j$$$ and swap $$$s_i$$$ and $$$s_j$$$) such that the resulting new name becomes strictly lexicographically smaller than $$$c$$$, or determine that it is impossible.Note: String $$$a$$$ is strictly lexicographically smaller than string $$$b$$$ if and only if one of the following holds:  $$$a$$$ is a proper prefix of $$$b$$$, that is, $$$a$$$ is a prefix of $$$b$$$ such that $$$a \neq b$$$;  There exists an integer $$$1 \le i \le \min{(|a|, |b|)}$$$ such that $$$a_i < b_i$$$ and $$$a_j = b_j$$$ for $$$1 \le j < i$$$.

The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 1500$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing two space-separated strings $$$s$$$ and $$$c$$$ ($$$2 \le |s| \le 5000, 1 \le |c| \le 5000$$$). The strings $$$s$$$ and $$$c$$$ consists of uppercase English letters. It is guaranteed that the sum of $$$|s|$$$ in the input is at most $$$5000$$$ and the sum of the $$$|c|$$$ in the input is at most $$$5000$$$.

For each test case, output a single line containing a single string, which is either   the new name which is obtained after swapping no more than one pair of characters that is strictly lexicographically smaller than $$$c$$$. In case there are many possible such strings, you can output any of them;  three dashes (the string "---" without quotes) if it is impossible.

3
AZAMON APPLE
AZAMON AAAAAAAAAAALIBABA
APPLE BANANA

NoteIn the first test case, it is possible to swap the second and the fourth letters of the string and the resulting string "AMAZON" is lexicographically smaller than "APPLE".It is impossible to improve the product's name in the second test case and satisfy all conditions.In the third test case, it is possible not to swap a pair of characters. The name "APPLE" is lexicographically smaller than "BANANA". Note that there are other valid answers, e.g., "APPEL".


For an array $$$[b_1, b_2, \ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 \le i < j \le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$.You are given a permutation $$$[p_1, p_2, \ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$)  — the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$2\cdot 10^5$$$.

For each test case output a single integer  — the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays.

NoteIn the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays.In the second test case, we can split our permutation into subarrays $$$[4, 3], [2, 1]$$$, both of which are odd since their numbers of inversions are $$$1$$$.In the fourth test case, we can split our permutation into a single subarray $$$[2, 1]$$$, which is odd.In the fifth test case, we can split our permutation into subarrays $$$[4, 5], [6, 1, 2, 3]$$$. The first subarray has $$$0$$$ inversions, and the second has $$$3$$$, so it is odd.

<?php

$t = trim(fgets(STDIN));
for ($t1=1; $t1<=$t; $t1++) {
    $n = trim(fgets(STDIN));
    $p = explode(' ', trim(fgets(STDIN)));

    $iSum = 0;

    for ($i=1; $i<$n; $i++) {
        if ($p[$i-1] > $p[$i]) {
            $iSum++;
            $i = $i + 1;
        }
    }
    echo $iSum . PHP_EOL;
}

<?php
$no = 'NO';
$yes = 'YES';
$mod = 1000000007;
$intMaxValue = 2147483646; //2147483647
$alph = 'abcdefghijklmnopqrstuvwxyz';
$inter = "\n";
define("INTMAXVALUE",     2147483646);

 $loop = true;
 $t = $loop ? IO::getInt() : 1;

 while($t--) {
      solve();
 }

 function solve()
 {
      $ans = 0;
      $n = IO::getInt();
      $a = IO::getArray();

      for($i=0; $i < $n-1; $i++) {
           if($a[$i] > $a[$i+1]) {
                $ans += 1;// 'YES';
                $i += 1;
           }
      }
      IO::pr($ans);
 }

//   $ans = 0;
//        list($n, $x) = IO::getArray();

//        $len = strlen($x);
//        if($len > $n) {
//           $ans = -1;
//        } elseif($len < $n) {
//           $maxV = maxVal($x);
//           if($maxV == '1') {
//              $ans = -1;
//           } else {
//                $ind = [0, 1];

//                foreach(str_split($x) as $ch) {
//                     IO::pr($ch);
//                }

//           }

//        }

//        IO::pr($ans);

//  function temp($x, $$aTemp, $ans) {
//       while($n > strlen($x)) {
//         IO::pr($ans . " " . strlen($x) . " " . $x . " " . $maxV);
//         if($maxV < 9 && in_array('3', str_split($x))) {
//              $maxV = 3;
//         }
//           $x = bcmul($x, $maxV);
//           $ans++;
//           $maxV = IO::getMaxValue(maxVal($x) , $maxV);
//      }
//      return $ans;
//  }
function maxVal($x) {
     $max = $x[0];
          for($i=1; $i<strlen($x); $i++) {
              $max = IO::getMaxValue($max, $x[$i]);
              if($max == 9) {
                   break;
              }
          }
          return $max;
}

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

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

 class Algor {
    public static function isPrime($n) {
         $d = 2;
         while ($n % $d != 0) {
         $d += 1;
         }

        return $d == $n;
    }

     public static function fabicon($n, $a) {
         if($n <= 1) {
             return $a[$n];
         }
         for($i=2; $i<=$n; $i++) {
             $a[$i] = $a[$i-1] + $a[$i-2];
         }

         return $a[$n];
     }

     function getRotate($a, $s, $e, $ind) {
         $ar = [];
         for ($i= $s; $i<=$e; $i++) {
             $ar[$i] = $a[$i];
             $j = $i + $ind > $e ? $i + $ind - $e - 1 : $i + $ind;
             // echo $i, ' ', $j, ' ', $j < $i ? $ar[$j] : $a[$j] , "\n";
             $a[$i] = $j < $i ? $ar[$j] : $a[$j];
         }
         return $a;
     }

}

class bigOperation {
    public static function  isBigNumber($n)
    {
        return bccomp($n, INTMAXVALUE) == 1;
    }
    // bcadd — Сложить 2 числа произвольной точности
    public static function  bcAdd($x, $y)
    {
        return bcadd($x, $y);
    }
// bccomp — Сравнение двух чисел произвольной точности
//return 1 if x>y, 0= x==y, -1 x < y
    public static function  bcComp($x, $y)
    {
        return bccomp($x, $y);
    }
// bcdiv — Операция деления для чисел произвольной точности
    public static function  bcDiv($x, $y)
    {
        return bcdiv($x, $y);
    }
// bcmod — Получает остаток от деления чисел с произвольной точностью
    public static function  bcMod($x, $y)
    {
        return bcmod($x, $y);
    }
// bcmul — Умножение двух чисел с произвольной точностью
    public static function  bcMul($x, $y)
    {
        return bcmul($x, $y);
    }
// bcpow — Возведение в степень чисел с произвольной точностью
    public static function  bcPow($x, $y)
    {
        return bcpow($x, $y);
    }
// bcpowmod — Возводит одно число в степень другого и возвращает остаток от деления результата на третье число
// bcscale — Задает количество чисел после десятичной точки по умолчанию для всех bc math функций.
// bcsqrt — Извлекает квадратный корень из числа с произвольной точностью
    public static function  bcSqrt($x, $y)
    {
        return bcsqrt($x, $y);
    }
// bcsub — Вычитает одно число с произвольной точностью из другого
    public static function  bcSub($x, $y)
    {
        return bcsub($x, $y);
    }
    public static function getMaxValue($x, $y) {
        return bccomp($x, $y) == 1 ? $x : $y;
    }
    public static function getMinValue($x, $y) {
        return bccomp($x, $y) == 1 ? $y : $x;
    }

    public static function getDareje($n, $k) {
        if($k == 0) {
            return 1;
        }
        return bcmul($n, self::getDareje($n, $k-1));
    }
}

class Sorting
{
    public static function mergeSort($aArray) {
        if(count($aArray) <= 1) {
            return $aArray;
        }
        $aLeft = [];
        $aRight = [];
        $n =floor(count($aArray) / 2);
        for($i = 0; $i <$n; $i++) {
            $aLeft[] = $aArray[$i];
        }
        for($i = $n; $i <count($aArray); $i++) {
            $aRight[] = $aArray[$i];
        }

        return self::merge(self::mergeSort($aLeft), self::mergeSort($aRight));
    }

    public static function merge($aLeft, $aRight) {
        $aResult = [];
        $iL = 0;
        $iR = 0;

        while($iL < count($aLeft) && $iR < count($aRight)) {
            if($aLeft[$iL] <= $aRight[$iR]) {
                $aResult[] = $aLeft[$iL];
                $iL++;
            } else {
                $aResult[] = $aRight[$iR];
                $iR++;
            }
        }

        while($iL < count($aLeft)) {
            $aResult[] = $aLeft[$iL];
            $iL++;
        }
        while($iR < count($aRight)) {
            $aResult[] = $aRight[$iR];
            $iR++;
        }

        return $aResult;

    }
}

class SolveArchive
{

}


— Hey folks, how do you like this problem?— That'll do it. BThero is a powerful magician. He has got $$$n$$$ piles of candies, the $$$i$$$-th pile initially contains $$$a_i$$$ candies. BThero can cast a copy-paste spell as follows:   He chooses two piles $$$(i, j)$$$ such that $$$1 \le i, j \le n$$$ and $$$i \ne j$$$.  All candies from pile $$$i$$$ are copied into pile $$$j$$$. Formally, the operation $$$a_j := a_j + a_i$$$ is performed. BThero can cast this spell any number of times he wants to — but unfortunately, if some pile contains strictly more than $$$k$$$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power?

The first line contains one integer $$$T$$$ ($$$1 \le T \le 500$$$) — the number of test cases. Each test case consists of two lines:    the first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 1000$$$, $$$2 \le k \le 10^4$$$);  the second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le k$$$).  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

For each test case, print one integer — the maximum number of times BThero can cast the spell without losing his magic power.

NoteIn the first test case we get either $$$a = [1, 2]$$$ or $$$a = [2, 1]$$$ after casting the spell for the first time, and it is impossible to cast it again.

<?php
    $t = trim(fgets(STDIN));
    for($i = 0; $i < $t; $i++){
        $nk = explode(" ", trim(fgets(STDIN)));
        $n = $nk[0];
        $k = $nk[1];
        $a = explode(" ", trim(fgets(STDIN)));
        $minK = min($a);
        $minI = array_search($minK, $a);
        $count = 0;
        for ($b = 0; $b < count($a); $b++) {
            if ($b == $minI) {
                continue;
            }
            $count += floor(($k - $a[$b]) / $minK);
        }
        echo $count . "\n";

    }

?>


Polycarpus has been working in the analytic department of the "F.R.A.U.D." company for as much as n days. Right now his task is to make a series of reports about the company's performance for the last n days. We know that the main information in a day report is value ai, the company's profit on the i-th day. If ai is negative, then the company suffered losses on the i-th day.Polycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the n days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on.It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (ai < 0), he loses his temper and his wrath is terrible.Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal.Write a program that, given sequence ai, will print the minimum number of folders.

The first line contains integer n (1 ≤ n ≤ 100), n is the number of days. The second line contains a sequence of integers a1, a2, ..., an (|ai| ≤ 100), where ai means the company profit on the i-th day. It is possible that the company has no days with the negative ai.

Print an integer k — the required minimum number of folders. In the second line print a sequence of integers b1, b2, ..., bk, where bj is the number of day reports in the j-th folder. If there are multiple ways to sort the reports into k days, print any of them.

NoteHere goes a way to sort the reports from the first sample into three folders:  1 2 3 -4 -5 | -6 5 -5 | -6 -7 6 In the second sample you can put all five reports in one folder.


You are given a string $$$s$$$. You have to determine whether it is possible to build the string $$$s$$$ out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example:  aaaabbb can be built as aa $$$+$$$ aa $$$+$$$ bbb;  bbaaaaabbb can be built as bb $$$+$$$ aaa $$$+$$$ aa $$$+$$$ bbb;  aaaaaa can be built as aa $$$+$$$ aa $$$+$$$ aa;  abab cannot be built from aa, aaa, bb and/or bbb.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 50$$$), consisting of characters a and/or b.

For each test case, print YES if it is possible to build the string $$$s$$$. Otherwise, print NO. You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).

8

aaaabbb

bbaaaaabbb

aaaaaa

abab

a

b

aaaab

bbaaa

NoteThe first four test cases of the example are described in the statement.

<?php
$no = 'NO';
$yes = 'YES';
$mod = 1000000007;
$alph = 'abcdefghijklmnopqrstuvwxyz';
$inter = "\n";

  $t = getT();

  for($q=0; $q <$t; $q++) {
      $res = $yes;
      $s = getT();
      $s .='.';
      $g = '';
    for($i=0; $i<strlen($s)-1; $i++) {
        $g .=$s[$i];
        if($s[$i] != $s[$i+1]) {
            if(strlen($g) <= 1) {
                $res = $no;
                break;
            }
            $g = '';
        }
    }
       echo $res , $inter;
  }

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


There are $$$n$$$ emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The $$$i$$$-th emote increases the opponent's happiness by $$$a_i$$$ units (we all know that emotes in this game are used to make opponents happy).You have time to use some emotes only $$$m$$$ times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than $$$k$$$ times in a row (otherwise the opponent will think that you're trolling him).Note that two emotes $$$i$$$ and $$$j$$$ ($$$i \ne j$$$) such that $$$a_i = a_j$$$ are considered different.You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.

The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$1 \le k \le m \le 2 \cdot 10^9$$$) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is value of the happiness of the $$$i$$$-th emote.

Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.

NoteIn the first example you may use emotes in the following sequence: $$$4, 4, 5, 4, 4, 5, 4, 4, 5$$$.

<?php
list($a, $b, $c) = explode(" ", trim(fgets(STDIN)));
$d = explode(" ", trim(fgets(STDIN)));
rsort($d);
$e2 = bcadd($c, 1);
$e3 = bcdiv($b, $e2);
$e = floor($e3);
$f2 = bcadd($c, 1);
$f = bcmod($b, $f2);
$g2 = bcmul($c, $d[0]);
$g3 = bcadd($g2, $d[1]);
$g4 = bcmul($g3, $e);
$g5 = bcmul($f, $d[0]);
$g = bcadd($g4, $g5);
print $g;
?>

<?php

list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
$aArrayA = explode(' ', trim(fgets(STDIN)));
$aArrayB = $aArrayA;
$iMaxArrayA = max($aArrayA);
$iMaxKey = array_keys($aArrayA, $iMaxArrayA);
unset($aArrayA[current($iMaxKey)]);
$iMaxArrayB = max($aArrayA);
$iSumPoint = 0;
if($k >= $m){
    $iSumPoint = bcmul($iMaxArrayA , $m);
} else {
    $iAb = floor($m/($k + 1));
    $iAc = $m - ($iAb)*(($k + 1));
    $iSumPoint = bcmul(($iAb*$k),$iMaxArrayA);
    $iSumPoint = bcadd($iSumPoint,bcmul($iAb,$iMaxArrayB));
    if($iAc > 0){
        $iSumPoint = bcadd($iSumPoint,bcmul($iAc,$iMaxArrayA));
    }
}
echo $iSumPoint;

<?php

$str = trim(fgets(STDIN));

$str1 = trim(fgets(STDIN));

$data = explode(' ', $str);

$n = $data[0];
$m = $data[1];
$k = $data[2];

$emo = explode(' ', $str1);

rsort($emo);
$c = 0;
$sum = 0;

$p = floor($m/($k+1));

$a = bcmul($emo[0], $k);
$b = bcadd($a, $emo[1]);

$sum = bcmul($p, $b);

$c += $p*($k+1);

$sum = bcadd($sum, bcmul($emo[0], ($m - $c)));

fwrite(STDOUT, sprintf("%s", $sum));

?>

<?php

list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
$aArrayA = explode(' ', trim(fgets(STDIN)));
$aArrayB = $aArrayA;
$iMaxArrayA = max($aArrayA);
$iMaxKey = array_keys($aArrayA, $iMaxArrayA);
unset($aArrayA[current($iMaxKey)]);
$iMaxArrayB = max($aArrayA);
$iSumPoint = 0;
if($k >= $m){
    $iSumPoint = $iMaxArrayA * $m;
} else {
    $iAb = floor($m/($k + 1));
    $iAc = $m - ($iAb)*(($k + 1));
    $iSumPoint = bcmul(($iAb*$k),$iMaxArrayA);
    $iSumPoint = bcadd($iSumPoint,bcmul($iAb,$iMaxArrayB));
    if($iAc > 0){
        $iSumPoint = bcadd($iSumPoint,bcmul($iAc,$iMaxArrayA));
    }
}
echo $iSumPoint;

<?php

list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
$aArrayA = explode(' ', trim(fgets(STDIN)));
$aArrayB = $aArrayA;
$iMaxArrayA = max($aArrayA);
$iMaxKey = array_keys($aArrayA, $iMaxArrayA);
unset($aArrayA[current($iMaxKey)]);
$iMaxArrayB = max($aArrayA);
$iSumPoint = 0;
if($k >= $m){
    $iSumPoint = $iMaxArrayA * $m;
} else {
    $iAb = floor($m/($k + 1));
    $iAc = $m - ($iAb)*(($k + 1));
    $iSumPoint = bcmul(($iAb*$k),$iMaxArrayA);
    $iSumPoint = bcadd($iSumPoint,bcmul($iAb,$iMaxArrayB));
    $iSumPoint = bcadd($iSumPoint,bcmul($iAc,$iMaxArrayA));
}
echo $iSumPoint;

<?php

list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
$aArrayA = explode(' ', trim(fgets(STDIN)));
$aArrayB = $aArrayA;
$iMaxArrayA = max($aArrayA);
$iMaxKey = array_keys($aArrayA, $iMaxArrayA);
unset($aArrayA[current($iMaxKey)]);
$iMaxArrayB = max($aArrayA);
$iSumPoint = 0;
if($k >= $m){
    $iSumPoint = $iMaxArrayA * $m;
} else {
    $iAb = floor($m/($k + 1));
    $iSumPoint = strval(($iAb*$k)*$iMaxArrayA);
    $iSumPoint += strval($iAb*$iMaxArrayB);
    $iAc = $m - ($iAb)*(($k + 1));
    $iSumPoint += strval($iAc*$iMaxArrayA);
}
echo $iSumPoint;

<?php

list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
$aArrayA = explode(' ', trim(fgets(STDIN)));
$aArrayB = $aArrayA;
$iMaxArrayA = max($aArrayA);
$iMaxKey = array_keys($aArrayA, $iMaxArrayA);
unset($aArrayA[current($iMaxKey)]);
$iMaxArrayB = max($aArrayA);
$iSumPoint = 0;
if($k >= $m){
    $iSumPoint = $iMaxArrayA * $m;
} else {
    $iAb = floor($m/($k + 1));
    $iSumPoint = ($iAb*$k)*$iMaxArrayA;
    $iSumPoint += $iAb*$iMaxArrayB;
}
echo $iSumPoint;

<?php

list($n, $m, $k) = explode(' ', trim(fgets(STDIN)));
$aArrayA = explode(' ', trim(fgets(STDIN)));
$aArrayB = $aArrayA;
$iMaxArrayA = max($aArrayA);
$iMaxKey = array_keys($aArrayA, $iMaxArrayA);
unset($aArrayA[current($iMaxKey)]);
$iMaxArrayB = max($aArrayA);
$iSumPoint = 0;
if($k >= $m){
    $iSumPoint = $iMaxArrayA * $m;
} else {
    $iAb = floor($m/($k + 1));
    $iSumPoint = ($iAb*$k)*$iMaxArrayA;
    $iSumPoint += $iAb*$iMaxArrayB;
    $iAc = $m - ($iAb)*(($k + 1));
    $iSumPoint += $iAc*$iMaxArrayA;
}
echo $iSumPoint;

<?php

$str = trim(fgets(STDIN));

$str1 = trim(fgets(STDIN));

$data = explode(' ', $str);

$n = $data[0];
$m = $data[1];
$k = $data[2];

$emo = explode(' ', $str1);

rsort($emo);
$c = 0;
$sum = 0;

$p = floor($m/($k+1));

$a = bcmul($emo[0], $k);
$b = bcmul($a, $emo[1]);

$sum = bcmul($p, $b);

$c += $p*($k+1);

$d = bcmul($emo[0], ($m - $c));

$sum = bcadd($sum, $d);

print $sum;

?>

<?php

$str = trim(fgets(STDIN));

$str1 = trim(fgets(STDIN));

$data = explode(' ', $str);

$n = $data[0];
$m = $data[1];
$k = $data[2];

$emo = explode(' ', $str1);

rsort($emo);
$c = 0;
$sum = 0;
$less = 0;

$p = floor($m/($k+1));

$sum += $p*($emo[0]*$k + $emo[1]);
$c += $p*($k+1);

$sum += $emo[0]*($m - $c);

fwrite(STDOUT, (int)$sum);

?>

<?php

$str = trim(fgets(STDIN));

$str1 = trim(fgets(STDIN));

$data = explode(' ', $str);

$n = $data[0];
$m = $data[1];
$k = $data[2];

$emo = explode(' ', $str1);

rsort($emo);
$c = 0;
$sum = 0;
$less = 0;

$p = floor($m/($k+1));

$sum += $p*($emo[0]*$k + $emo[1]);
$c += $p*($k+1);

$sum += $emo[0]*($m - $c);

fwrite(STDOUT, sprintf("%u", $sum));

?>

<?php

$str = trim(fgets(STDIN));

$str1 = trim(fgets(STDIN));

$data = explode(' ', $str);

$n = $data[0];
$m = $data[1];
$k = $data[2];

$emo = explode(' ', $str1);

rsort($emo);
$c = 0;
$sum = 0;
$less = 0;

$p = floor($m/($k+1));

$sum += $p*($emo[0]*$k + $emo[1]);
$c += $p*($k+1);

$sum += $emo[0]*($m - $c);

fwrite(STDOUT, sprintf("%d", $sum));

?>

<?php

$str = trim(fgets(STDIN));

$str1 = trim(fgets(STDIN));

$data = explode(' ', $str);

$n = $data[0];
$m = $data[1];
$k = $data[2];

$emo = explode(' ', $str1);

rsort($emo);
$c = 0;
$sum = 0;
$less = 0;

$p = floor($m/($k+1));

		$sum += $p*($emo[0]*$k + $emo[1]);
		$c += $p*($k+1);

$sum += $emo[0]*($m - $c);

fwrite(STDOUT, $sum);

?>


Vasya has a multiset $$$s$$$ consisting of $$$n$$$ integer numbers. Vasya calls some number $$$x$$$ nice if it appears in the multiset exactly once. For example, multiset $$$\{1, 1, 2, 3, 3, 3, 4\}$$$ contains nice numbers $$$2$$$ and $$$4$$$.Vasya wants to split multiset $$$s$$$ into two multisets $$$a$$$ and $$$b$$$ (one of which may be empty) in such a way that the quantity of nice numbers in multiset $$$a$$$ would be the same as the quantity of nice numbers in multiset $$$b$$$ (the quantity of numbers to appear exactly once in multiset $$$a$$$ and the quantity of numbers to appear exactly once in multiset $$$b$$$).

The first line contains a single integer $$$n~(2 \le n \le 100)$$$. The second line contains $$$n$$$ integers $$$s_1, s_2, \dots s_n~(1 \le s_i \le 100)$$$ — the multiset $$$s$$$.

If there exists no split of $$$s$$$ to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of $$$n$$$ characters. $$$i$$$-th character should be equal to 'A' if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$a$$$ and 'B' if if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$b$$$. Elements are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there exist multiple solutions, then print any of them.


During the hypnosis session, Nicholas suddenly remembered a positive integer $$$n$$$, which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?For some numbers doing so is impossible: for example, for number $$$53$$$ it's impossible to delete some of its digits to obtain a not prime integer. However, for all $$$n$$$ in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.Note that you cannot remove all the digits from the number.A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. $$$1$$$ is neither a prime nor a composite number.

Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$k$$$ ($$$1 \le k \le 50$$$) — the number of digits in the number. The second line of each test case contains a positive integer $$$n$$$, which doesn't contain zeros in decimal notation ($$$10^{k-1} \le n < 10^{k}$$$). It is guaranteed that it is always possible to remove less than $$$k$$$ digits to make the number not prime. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^4$$$.

For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions.  If there are multiple solutions, print any.

NoteIn the first test case, you can't delete $$$2$$$ digits from the number $$$237$$$, as all the numbers $$$2$$$, $$$3$$$, and $$$7$$$ are prime. However, you can delete $$$1$$$ digit, obtaining a number $$$27 = 3^3$$$.In the second test case, you can delete all digits except one, as $$$4 = 2^2$$$ is a composite number.


An array $$$b$$$ of length $$$k$$$ is called good if its arithmetic mean is equal to $$$1$$$. More formally, if $$$$$$\frac{b_1 + \cdots + b_k}{k}=1.$$$$$$Note that the value $$$\frac{b_1+\cdots+b_k}{k}$$$ is not rounded up or down. For example, the array $$$[1,1,1,2]$$$ has an arithmetic mean of $$$1.25$$$, which is not equal to $$$1$$$.You are given an integer array $$$a$$$ of length $$$n$$$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good?We have a proof that it is always possible with finitely many operations.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 50$$$) — the length of the initial array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,\ldots,a_n$$$ ($$$-10^4\leq a_i \leq 10^4$$$), the elements of the array.

For each test case, output a single integer — the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly $$$1$$$.

NoteIn the first test case, we don't need to add any element because the arithmetic mean of the array is already $$$1$$$, so the answer is $$$0$$$.In the second test case, the arithmetic mean is not $$$1$$$ initially so we need to add at least one more number. If we add $$$0$$$ then the arithmetic mean of the whole array becomes $$$1$$$, so the answer is $$$1$$$.In the third test case, the minimum number of elements that need to be added is $$$16$$$ since only non-negative integers can be added.In the fourth test case, we can add a single integer $$$4$$$. The arithmetic mean becomes $$$\frac{-2+4}{2}$$$ which is equal to $$$1$$$.

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

   for($w=0; $w<$t; $w++) {
      $n =  trim(fgets(STDIN));
      $a =  explode(' ', trim(fgets(STDIN)));

    $count = array_sum($a);

    if($count == $n) {
        echo  "0\n";
    } elseif($count > $n ) {
        echo $count - $n . "\n";
    } else {
        echo  "1\n";
    }

}

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

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

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

  $sum = array_sum($a);
  $k = count($a);

  if ($k == $sum) { echo '0'.PHP_EOL; }
  if ($k < $sum) { $ans = $sum - $k; echo $ans.PHP_EOL; }
  if ($k > $sum) { echo '1'.PHP_EOL; }
}

?>


Roma (a popular Russian name that means 'Roman') loves the Little Lvov Elephant's lucky numbers.Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Roma's got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.

The first line contains two integers n, k (1 ≤ n, k ≤ 100). The second line contains n integers ai (1 ≤ ai ≤ 109) — the numbers that Roma has.  The numbers in the lines are separated by single spaces.

In a single line print a single integer — the answer to the problem.

NoteIn the first sample all numbers contain at most four lucky digits, so the answer is 3.In the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2.

<?php
    $fp = fopen('php://stdin', 'r');
    list($n, $k) = explode(' ', rtrim(fgets($fp)));
    $numbers = explode(' ', rtrim(fgets($fp)));
    $cnt = 0;
    for($i = 0; $i < $n; $i++) {
        $cnt += (strlen($numbers[$i]) - strlen(preg_replace('/[47]/','',$numbers[$i])) <= $k) ? 1 : 0;
    }
    echo $cnt;
?>

<?php
$input = Array();

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

while($line = fgets($fp)) {
    $input[] = $line;
}

$temp = explode(" ", $input[0]);
$n = $temp[0];
$k = $temp[1];

$nums = explode(" ", $input[1]);

$total = 0;

for($i=0; $i<$n; $i++) {
	$count = 0;
	$count += substr_count($nums[$i],'4');
	$count += substr_count($nums[$i],'7');
	if($count<=$k)
		$total++;
}

echo $total;

<?// BismiLahi Rahmani Rahim ?>

<?php

fscanf(STDIN, "%d %d", $n, $k);

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

$answer = 0;

for ($i = 0; $i < $n; $i ++) {
	$count = 0;
	$t = $a[$i];

	while ($t > 0) {
		$count += $t%10 == 4 || $t%10 == 7;
		$t = intval($t / 10);
	}

	$answer += $count <= $k;
}

printf("%d", $answer);

?>


Unfortunately, Vasya can only sum pairs of integers (a, b), such that for any decimal place at least one number has digit 0 in this place. For example, Vasya can sum numbers 505 and 50, but he cannot sum 1 and 4.Vasya has a set of k distinct non-negative integers d1, d2, ..., dk.Vasya wants to choose some integers from this set so that he could sum any two chosen numbers. What maximal number of integers can he choose in the required manner?

The first input line contains integer k (1 ≤ k ≤ 100) — the number of integers. The second line contains k distinct space-separated integers d1, d2, ..., dk (0 ≤ di ≤ 100).

In the first line print a single integer n the maximum number of the chosen integers. In the second line print n distinct non-negative integers — the required integers. If there are multiple solutions, print any of them. You can print the numbers in any order.

<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = array();
$e = array();
for($x = 0; $x < $a; $x++)
{
    $d = strpos("1" . $b[$x], "0");
    if($d == TRUE)
    {
        $c[] = $b[$x];
    }
    else
    {
        $e[] = $b[$x];
    }
}
if($a == count($c))
{
    print $a . "\n";
    print implode(" ", $b);
}
elseif($a > count($c))
{
    print count($c) + 1 . "\n";
    print $e[0] . " " . implode(" ", $c);
}
?>


While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s.  He is not sure if this is his own back-bag or someone else's. He remembered that there were exactly k messages in his own bag, each was a palindrome string and all those strings had the same length.He asked you to help him and tell him if he has worn his own back-bag. Check if the given string s is a concatenation of k palindromes of the same length.

The first line of input contains string s containing lowercase English letters (1 ≤ |s| ≤ 1000). The second line contains integer k (1 ≤ k ≤ 1000).

Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise.

NotePalindrome is a string reading the same forward and backward.In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".

<?php
	$s = trim(fgets(STDIN));
	$k = (int) (trim(fgets(STDIN)));
	$chk = true;
	if (strlen($s)%$k != 0) {
		$chk = false;
	} else {
		$r = floor(strlen($s)/$k);
		for ($i = 0; $i < strlen($s); $i += $r) {
			$t = substr($s,$i,$r);
			if ($t != strrev($t)) {
				$chk = false;
				break;
			}
		}
	}
	echo ($chk) ? 'YES' : 'NO';
?>

<?php
$str=trim(fgets(STDIN));
fscanf(STDIN,"%d",$k);

 $len=strlen($str);
 //echo "$portion\n";
 //$inc=$portion-1;
 $decision=false;
 if($len%$k==0)
    {
        $portion=$len/$k;
        $cnt=0;
		 for ($i=0; $i <$len ; $i+=$portion)
		 {
		 	$str1=substr($str,$i,$portion);
		 	if($str1==strrev($str1))
		 	{
		 		$cnt++;
		 		if($cnt==$k)
		 		{
		 			$decision=true;
		 			break;
		 		}
		 	}

		 }
    }
   echo $decision?"YES":"NO";

<?php
$str=trim(fgets(STDIN));
fscanf(STDIN,"%d",$k);

 $len=strlen($str);
 $portion=$len/$k;
 //echo "$portion\n";
 //$inc=$portion-1;
 $decision=false;
	 $cnt=0;
	 for ($i=0; $i <$len ; $i+=$portion)
	 {
	 	$str1=substr($str,$i,$portion);
	 	if($str1==strrev($str1))
	 	{
	 		$cnt++;
	 		if($cnt==$k)
	 		{
	 			$decision=true;
	 			break;
	 		}
	 	}

	 }
   echo $decision?"YES":"NO";

<?php
$str=trim(fgets(STDIN));
fscanf(STDIN,"%d",$k);
$decision=false;
if($k%2==0)
{
 $len=strlen($str);
 $portion=$len/$k;
 //echo "$portion\n";
 //$inc=$portion-1;
	 $cnt=0;
	 for ($i=0; $i <$len ; $i+=$portion)
	 {
	 	$str1=substr($str,$i,$portion);
	 	if($str1==strrev($str1))
	 	{
	 		$cnt++;
	 		if($cnt==$k)
	 		{
	 			$decision=true;
	 			break;
	 		}
	 	}

	 }
}
echo $decision?"YES":"NO";


You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.

The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.

The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise. If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.


Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:   the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays,  the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays,  the third flower adds 1·2 = 2, because he is in two of chosen subarrays,  the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,  the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays. Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100). The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], ..., a[ri]. Each subarray can encounter more than once.

Print single integer — the maximum possible value added to the Alyona's happiness.

NoteThe first example is the situation described in the statements.In the second example Alyona should choose all subarrays.The third example has answer 0 because Alyona can choose none of the subarrays.

<?php
//$data = fgets(STDIN);
//$gestor = @fopen("data.in", "r");
$data = fgets(STDIN);
$data = explode(" ", $data);
$n = $data[0];
$m = $data[1];
$arr = array();
$data = fgets(STDIN);
$data = explode(" ", $data);
for ($i = 0; $i < $n; $i++) {
    $arr[$i] = $data[$i];
}
$res = 0;
for ($i = 0; $i < $m; $i++) {
    $data = fgets(STDIN);
    $data = explode(" ", $data);
    $ans = 0;
    for ($j = $data[0] - 1; $j < $data[1]; $j++) {
        $ans += $arr[$j];
    }
    if ($ans > 0) {
        $res += $ans;
    }
}
echo $res;

?>

<?php
fscanf(STDIN, '%d %d', $n, $m);
$flowers = explode(" ", fgets(STDIN));
$list=array();
for($i=0; $i<$m; $i++) $list[$i] = explode(" ", fgets(STDIN));

$happy = 0;

for($i=0; $i<$m; $i++){
    $sum = 0;
    for($j = $list[$i][0]-1; $j<=$list[$i][1]-1; $j++){
        $sum += $flowers[$j];
    }
    if($sum > 0) $happy += $sum;
}

print $happy;
?>

<?php
fscanf(STDIN, '%d %d', $n, $m);
$flowers = explode(" ", fgets(STDIN));
$list=array();
for($i=0; $i<$m; $i++) $list[] = explode(" ", fgets(STDIN));

$happy = 0;

for($i=0; $i<$m; $i++){
    $sum = 0;
    for($j = $list[$i][0]; $j<=$list[$i][1]; $j++){
        $sum += $flowers[$j-1];
    }
    if($sum > 0) $happy += $sum;
}

print $happy;
?>


Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome", because the second half of it is not a mirror reflection of the first half.  English alphabet You are given a string s. Check if the string is "s-palindrome".

The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.

Print "TAK" if the string s is "s-palindrome" and "NIE" otherwise.


Alyona has recently bought a miniature fridge that can be represented as a matrix with $$$h$$$ rows and $$$2$$$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part.  An example of a fridge with $$$h = 7$$$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. Alyona has $$$n$$$ bottles of milk that she wants to put in the fridge. The $$$i$$$-th bottle is $$$a_i$$$ cells tall and $$$1$$$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell.Alyona is interested in the largest integer $$$k$$$ such that she can put bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. Find this largest $$$k$$$.

The first line contains two integers $$$n$$$ and $$$h$$$ ($$$1 \le n \le 10^3$$$, $$$1 \le h \le 10^9$$$) — the number of bottles and the height of the fridge. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le h$$$) — the heights of the bottles.

Print the single integer $$$k$$$ — the maximum integer such that Alyona can put the bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $$$n$$$. It is easy to see that Alyona can always put at least one bottle in the fridge.

NoteOne of optimal locations in the first example is shown on the picture in the statement.One of optimal locations in the second example is shown on the picture below.  One of optimal locations in the third example is shown on the picture below.