There is a bus stop near the university. The lessons are over, and n students come to the stop. The i-th student will appear at the bus stop at time ti (all ti's are distinct).We shall assume that the stop is located on the coordinate axis Ox, at point x = 0, and the bus goes along the ray Ox, that is, towards the positive direction of the coordinate axis, and back. The i-th student needs to get to the point with coordinate xi (xi > 0).The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to m passengers. At the moment when m students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (n-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance y in time y.Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1 + [k / 2] units of time to get off the bus, where k is the number of students who leave at this point. Expression [k / 2] denotes rounded down k / 2. As soon as the last student leaves the bus, the bus turns around and goes back to the point x = 0. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated.If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students.Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off).

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of students and the number of passengers the bus can transport, correspondingly. Next n lines contain descriptions of the students, one per line. Each line contains a pair of integers ti, xi (1 ≤ ti ≤ 105, 1 ≤ xi ≤ 104). The lines are given in the order of strict increasing of ti. Values of xi can coincide.

Print n numbers w1, w2, ..., wn, wi — the moment of time when the i-th student got off the bus. Print the numbers on one line and separate them with single spaces.

NoteIn the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3 + 5 = 8.In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1 + [1 / 2] = 1 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14 + 5 = 19. In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1 + [4 / 2] = 3 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time.

<?php
error_reporting(0);

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

$time = 0;
$taxi = array();
$exit = array();
$seatBusy = 0;
$i = 1;
while( $n-- ){
	list($t, $x) = explode( " ",trim( str_replace( "\r\n", '', fgets($fp) ) ) );
	if( $time < $t )
	{
		$time = $t;
	}
	$seatBusy++;
	$taxi[$x][] = $i;
	if( $seatBusy == $m || $n == 0 )
	{
		ksort($taxi);
		$extTime = 0;
		foreach($taxi as $key => $val)
		{
			$countEl = count($val);
			foreach($val as $v)
			{
				$exit[$v] = $key + $time + $extTime;
			}
			$extTime += floor(1 + $countEl / 2);
		}
		$time += $extTime + $key * 2;
		$seatBusy = 0;
		$taxi = array();
	}
	$i++;
}
ksort($exit);
echo implode(' ', $exit);
fclose($fp);
?>

<?php
    define ("TEST", false);
    if (TEST) $GLOBALS['file'] = fopen('input.txt', 'r');
    else $GLOBALS['file'] = fopen('php://stdin', 'r');
    function debug($var){echo '<pre>'; print_r($var); echo '</pre>';}
    function read_str(){return trim(fgets($GLOBALS['file']));}
    function read_array(){return explode(' ', read_str());}
    /***************************************************************/
    list($n, $m) = read_array();
    $timer = 0;
    $result=array();
    $start = microtime(true);

    $students = array();
    for ($i=1; $i<=$n; $i++)
    {
        $student = array();
        list($student['time'], $student['loc']) = read_array();
        $student['i'] = $i;
        $students[] = $student;
    }

    $chunks = array_chunk($students, $m);

    function sf($a,$b)
    {
        if ($a['loc'] < $b['loc']) return -1;
        if ($a['loc'] > $b['loc']) return 1;
        return 0;
    }

    foreach($chunks as $chunk)
    {
        $last_time = end($chunk); $last_time = $last_time['time'];if ($last_time > $timer) $timer += $last_time - $timer;
        //uasort($chunk, 'sf');

        $locs = array();
        foreach($chunk as $st)
        {
            $l = $st['loc'];
            $locs[$l][] = $st['i'];
        }
        ksort($locs);

        $prev_loc = 0;
        foreach($locs as $loc=>$sts)
        {
            $count = count($sts);
            $timer += $loc - $prev_loc;
            $prev_loc = $loc;
            foreach($sts as $i) $result[$i] = $timer;

            $timer += 1 + floor($count/2);
        }
        $timer += $loc;
    }

    for($i=1; $i<=$n; $i++) echo $result[$i].' ';


You are given two strings $$$s$$$ and $$$t$$$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $$$1$$$. You can't choose a string if it is empty.For example:  by applying a move to the string "where", the result is the string "here",  by applying a move to the string "a", the result is an empty string "". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings.Write a program that finds the minimum number of moves to make two given strings $$$s$$$ and $$$t$$$ equal.

The first line of the input contains $$$s$$$. In the second line of the input contains $$$t$$$. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and $$$2\cdot10^5$$$, inclusive.

Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings.

NoteIn the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est".In the second example, the move should be applied to the string "codeforces" $$$8$$$ times. As a result, the string becomes "codeforces" $$$\to$$$ "es". The move should be applied to the string "yes" once. The result is the same string "yes" $$$\to$$$ "es".In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty.In the fourth example, the first character of the second string should be deleted.

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));
$c = 0;
if(strlen($a) < strlen($b))
{
     $c += strlen($b) - strlen($a);
     $b = substr($b, $c);
}
elseif(strlen($a) > strlen($b))
{
     $c += strlen($a) - strlen($b);
     $a = substr($a, $c);
}
$c += strlen($a) * 2;
for($x = strlen($a) - 1; $x >= 0; $x--)
{
     if($a[$x] == $b[$x])
     {
          $c -= 2;
     }
     else
     {
          break;
     }
}
print $c;
?>

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
for($j=$t;$j>=1;$j=$j-100) {
 if (substr($a,-$j) == substr($b, -$j)) {
   break;
  }
}

for($i=$j+100;$i>=1;$i--) {
  if ($i <= $t && substr($a,-$i) == substr($b, -$i)) {
    break;
  }
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
// main:
$s1 = readline ();
$s2 = readline ();
// Get their size
$s1l = strlen ($s1);
$s2l = strlen ($s2);
// Reverse both
$s1 = strrev ($s1);
$s2 = strrev ($s2);

// Potentials
$potential_moves = $s1l + $s2l;
$length = ($s1l > $s2l) ? $s2l : $s1l;

// Get valid
$i = 0;
for (; $i < $length; ++$i)
{
        if ($s1[$i] != $s2[$i]) {
                break;
        }
}

// Moves
echo $potential_moves - 2*$i;

<?php
$str1=trim(fgets(STDIN));
$str2=trim(fgets(STDIN));

if (strlen($str2)>strlen($str1))
{
	$temp=$str2;
	$str2=$str1;
	$str1=$temp;
}

$lstr1=strlen($str1);
$lstr2=strlen($str2);
//echo "$lstr1 $lstr2";
if($str1[$lstr1-1]!=$str2[$lstr2-1])
	echo $lstr1+$lstr2;
else
{
		$diff=$lstr1-$lstr2;
		$count=0;
		for ($i=$diff,$j=0; $i <$lstr1 ; $i++)
		{
		  if($str1[$i]!=$str2[$j++])
		  {
		  	$count+=2;
		  }
		}
		echo $count+$diff."\n";
}

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j+$m;$i>=$j;$i--) {
  if ($i <= $t && substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
echo $b;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j;$i<$j+$m;$i++) {
  if (substr($a,-$i) == substr($b, -$i)) {
    $num = $i;
  }
}
break;
}
}
echo strlen($a) - $num + strlen($b) - $num;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
echo substr($a,-10);
echo "\n";
echo substr($b,-10);
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j;$i<$j+$m;$i++) {
  if (substr($a,-$i) == substr($b, -$i)) {
    $num = $i;
  }
}
break;
}
}
echo strlen($a) - $num + strlen($b) - $num;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen(a);

for($i=$t;$i<1;$i--) {
  if (substr($a,-$i) == substr($b, -$i)) {
    break;
  }
}

echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 10) {
$m = 10;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j+$m;$i>=$j;$i--) {
  if ($i <= $t && substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j+$m;$i<$j;$i--) {
  if (substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;

Last login: Thu Jul 12 09:43:57 on console
xiaoxiaode-Mac-mini:~ eooe$ cd firebase/
xiaoxiaode-Mac-mini:firebase eooe$ cd hahamut/
xiaoxiaode-Mac-mini:hahamut eooe$ l
total 32
drwxr-xr-x  6 eooe  staff   204  7  6 11:04 .
drwxr-xr-x  8 eooe  staff   272  3 23 16:25 ..
-rw-r--r--  1 eooe  staff    54 10  2  2017 .firebaserc
-rw-r--r--  1 eooe  staff  6009 11 29  2017 a.js
-rw-r--r--  1 eooe  staff     3 10  2  2017 firebase.json
drwxr-xr-x  9 eooe  staff   306  7 10 15:15 functions
xiaoxiaode-Mac-mini:hahamut eooe$ cd functions/
xiaoxiaode-Mac-mini:functions eooe$ l
total 7696
drwxr-xr-x    9 eooe  staff      306  7 10 15:15 .
drwxr-xr-x    6 eooe  staff      204  7  6 11:04 ..
-rw-r--r--    1 eooe  staff  3597255  3 27 14:20 go-pear.phar
-rw-r--r--    1 eooe  staff    62023  7 10 15:14 index.js
drwxr-xr-x  351 eooe  staff    11934  3 12 18:20 node_modules
-rw-r--r--    1 eooe  staff   221196  3 12 18:20 package-lock.json
-rw-r--r--    1 eooe  staff      352  3 12 18:25 package.json
-rw-r--r--    1 eooe  staff    17795 10 23  2017 pk.js
-rw-r--r--    1 eooe  staff    22541  6  6 17:47 pk2.js
xiaoxiaode-Mac-mini:functions eooe$ vim index.js
xiaoxiaode-Mac-mini:functions eooe$ firebase deploy --only functions:messages_create

=== Deploying to 'hahamut-8888'...

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

/Users/eooe/firebase/hahamut/functions/index.js:497
                 ) {
                 ^

SyntaxError: Unexpected token )
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
xiaoxiaode-Mac-mini:functions eooe$ vim index.js
xiaoxiaode-Mac-mini:functions eooe$ firebase deploy --only functions:messages_create

┌──────────────────────────────────────────────┐
│ Update available: 3.19.3 (current: 3.17.6)   │
│ Run npm install -g firebase-tools to update. │
└──────────────────────────────────────────────┘

=== Deploying to 'hahamut-8888'...

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (842.32 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: current functions in project: abuse, badfriend, bot_message_get, bottalk, chatListCreate, chatListDelete, chatlist_field_update_delete_member_cd, chatlist_field_update_favorite_order, chatlist_field_update_has_chat, chatlist_field_update_is_favorite, chatlist_field_update_is_friend, chatlist_field_update_is_inviting, chatlist_field_update_is_mute, chatlist_field_update_last_read_time, chatlist_field_update_name, chatlist_field_update_noreadnum, chatlist_field_update_related_acg_name, chatlist_field_update_related_c1, chatlist_field_update_type, chatlist_is_mute, crontab, fansbadfriend, fcmToken_change, fcmToken_new, hala_promotion_create, has_new_message, hot_forum, imgthumb, infoChange, info_name_update, isHotForum, is_friendUpdate, messageSend, messages_create, messages_create2, messages_delete, pk, pkRD, pkgirl, pking, pktest, reflashChatlist, sticker_check, sticker_create, sticker_ctime, sticker_delete, sticker_update, testfcm, testpubsub, time5s, userInfo, userNameChange, user_create
i  functions: uploading functions in project: messages_create
i  functions: updating function messages_create...
✔  functions[messages_create]: Successful update operation.

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/hahamut-8888/overview
xiaoxiaode-Mac-mini:functions eooe$ cd -
/Users/eooe/firebase/hahamut
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php

^C
xiaoxiaode-Mac-mini:hahamut eooe$
xiaoxiaode-Mac-mini:hahamut eooe$
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
PHP Notice:  Use of undefined constant a - assumed 'a' in /Users/eooe/firebase/hahamut/a.php on line 7

Notice: Use of undefined constant a - assumed 'a' in /Users/eooe/firebase/hahamut/a.php on line 7
6xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
0xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
0xiaoxiaode-Mac-mini:hahamut eooe$ err
-bash: err: command not found
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
40xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
4west--test
est--est
2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
west
test
2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
west
test
PHP Notice:  Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 7

Notice: Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 7
PHP Notice:  Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 7

Notice: Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 7
PHP Notice:  Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15

Notice: Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15
PHP Notice:  Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15

Notice: Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15
8xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
PHP Notice:  Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15

Notice: Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15
PHP Notice:  Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15

Notice: Undefined variable: i in /Users/eooe/firebase/hahamut/a.php on line 15
8xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
6xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
6xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
3216xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
3216xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
332xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
4xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
4xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
test
west
432xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
-2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
2
-2xiaoxiaode-Mac-mini:hahamut eooe$
xiaoxiaode-Mac-mini:hahamut eooe$
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
-2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
1-2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
1-2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
xz
z
1-1xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
1-2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
1-2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
1210xiaoxiaode-Mac-mini:hahamut eooe$
xiaoxiaode-Mac-mini:hahamut eooe$
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
zzzzzzzzzz
61xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
z
z
0xiaoxiaode-Mac-mini:hahamut eooe$ php a.php
west
test
2xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php
xiaoxiaode-Mac-mini:hahamut eooe$ vim a.php

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j;$i<$j+$m;$i++) {
  if (substr($a,-$i) == substr($b, -$i)) {
    $num = $i;
  }
}
break;
}
}

if( $num == 0 ) {
  for($i=100;$i<=1;$i--) {
    if (substr($a,-$i) == substr($b, -$i)) {
      $num = $i;
      break;
    }
  }
}
echo strlen($a) - $num + strlen($b) - $num;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
for($j=$t;$j>=1;$j=$j-100) {
 if (substr($a,-$j) == substr($b, -$j)) {
   break;
  }
}

for($i=$j+100;$i>=$j;$i--) {
  if ($i <= $t && substr($a,-$i) == substr($b, -$i)) {
    break;
  }
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j+$m;$i>$j;$i--) {
  if (substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j;$i<$j+$m;$i++) {
  if (substr($a,-$i) == substr($b, -$i)) {
    $num = $i;
  }
}
break;
}
}
echo strlen($a) - $num + strlen($b) - $num;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j;$i<=$j+$m;$i++) {
  if (substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
echo strlen($a).','.substr($a,-10);
echo "\n";
echo strlen($b).','.substr($b,-10);
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j;$i<$j+$m;$i++) {
  if (substr($a,-$i) == substr($b, -$i)) {
    $num = $i;
  }
}
break;
}
}
echo strlen($a) - $num + strlen($b) - $num;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j+m;$i<=$j;$i--) {
  if (substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;

<?php
$a = trim(fgets(STDIN));
$b = trim(fgets(STDIN));

$t = (strlen($a) > strlen($b)) ? strlen($b) : strlen($a);
if ($t > 100) {
$m = 100;
}else{
$m = 1;
}
for($j=$t;$j>=1;$j=$j-$m) {
if (substr($a,-$j) == substr($b, -$j)) {
for($i=$j+$m;$i<=$j;$i--) {
  if (substr($a,-$i) == substr($b, -$i)) {
    break 2;
  }
}
}
}
echo strlen($a) - $i + strlen($b) - $i;


There are $$$n$$$ points on the plane, $$$(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$$$.You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle or on the side of the triangle). Calculate the minimum length of the shorter side of the triangle.

First line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \leq x_i,y_i \leq 10^9$$$).

Print the minimum length of the shorter side of the triangle. It can be proved that it's always an integer.

NoteIllustration for the first example: Illustration for the second example:

<?php
$a = trim(fgets(STDIN));
$b = 0;
for($x = 0; $x < $a; $x++)
{
     list($c, $d) = explode(" ", trim(fgets(STDIN)));
     $b = max($b, $c + $d);
}
print $b;
?>

<?php
	// $in = fopen("input.txt", "r");
	// $out = fopen("output.txt", "w");
	$i = 0;
	$res = -1001000;
	$a = array();

	$n = (int) fgets(STDIN);

	while ($i < $n) {

		$str = fgets(STDIN);
		$a = explode(" ", $str);
		$i++;

		$x = $a[0];
		$y = $a[1];

		if ($x + $y > $res) {
			$res = $x + $y;
		}
	}
	print($res);

	// fclose($in);
	// fclose($out);
 ?>

<?php

fscanf(STDIN, "%d\n", $number);
bcscale(0);

$max = "0";
for ($i = 0; $i < $number; $i++) {
    fscanf(STDIN, "%s %s\n", $x, $y);
    $sum = bcadd($x, $y);
    if (bccomp($sum, $max) > 0) {
        $max = $sum;
    }
}

echo $max;

<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, '%d', $n);

$max = 0;

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

    fscanf($handle, '%d %d', $x, $y);

    $m = $x + $y;
    if ($m > $max) {
        $max = $m;
    }
}

echo $max;

<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, '%d', $n);

$max_x_x = 0;
$max_x_y = 0;

$max_y_x = 0;
$max_y_y = 0;

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

    fscanf($handle, '%d %d', $x, $y);

    if ($x >= $max_x_x) {

        if ($y >= $max_x_y) {
            $max_x_x = $x;
            $max_x_y = $y;
        }
    }

    if ($y >= $max_y_y) {

        if ($x >= $max_y_x) {

            $max_y_x = $x;
            $max_y_y = $y;
        }

    }
}

echo max($max_x_y + $max_x_x, $max_y_x + $max_y_y);


You are given $$$n$$$ numbers $$$a_1, a_2, \ldots, a_n$$$. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?For example, for the array $$$[1, 4, 5, 6, 7, 8]$$$, the arrangement on the left is valid, while arrangement on the right is not, as $$$5\ge 4 + 1$$$ and $$$8> 1 + 6$$$.

The first line contains a single integer $$$n$$$ ($$$3\le n \le 10^5$$$) — the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \le 10^9$$$) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).

If there is no solution, output "NO" in the first line.  If there is a solution, output "YES" in the first line. In the second line output $$$n$$$ numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.

NoteOne of the possible arrangements is shown in the first example: $$$4< 2 + 3$$$;$$$2 < 4 + 3$$$;$$$3< 4 + 2$$$.One of the possible arrangements is shown in the second example.No matter how we arrange $$$13, 8, 5$$$ in a circle in the third example, $$$13$$$ will have $$$8$$$ and $$$5$$$ as neighbors, but $$$13\ge 8 + 5$$$. There is no solution in the fourth example.

<?php
#1189B
$n = trim(fgets(STDIN));
$arr = explode(' ', trim(fgets(STDIN)));
sort($arr);
$last = $arr[$n - 1];
for ($i = 0; $i < $n; $i++) {
    $prev = $arr[$n - 2];
    $prevPrev = $arr[$n - 3];
    if ($arr[$i] >= ($prev + $prevPrev)) {
        die('NO');
    }
}
echo 'YES' . PHP_EOL . $arr[$n - 2] . ' ';
unset ($arr[$n - 2]);
echo implode(' ', $arr);

<?php
#1189B
$n = trim(fgets(STDIN));
$arr = explode(' ', trim(fgets(STDIN)));
sort($arr);
if ($arr[$n - 1] >= ($arr[$n - 2] + $arr[$n - 3])) {
    die('NO');
}
echo 'YES' . PHP_EOL . $arr[$n - 2] . ' ';
unset ($arr[$n - 2]);
echo implode(' ', $arr);

<?php

$n = trim(fgets(STDIN));

$numbers = explode(" ", trim(fgets(STDIN)));

sort($numbers);

$lastIndex = count($numbers) - 1;

$temp = $numbers[$lastIndex];

$numbers[$lastIndex] = $numbers[$lastIndex-1];
$numbers[$lastIndex-1] = $temp;

$flag = false;

if($numbers[$lastIndex-1] < $numbers[$lastIndex-2] + $numbers[$lastIndex]) $flag = true;

if($flag){
    print("YES \n");
    foreach($numbers as $num){
        print($num." ");
    }
}
else{
    print("NO");
}

<?php
#1189B
$n = trim(fgets(STDIN));
$arr = explode(' ', trim(fgets(STDIN)));
sort($arr);
$last = $arr[$n - 1];
for ($i = 0; $i < $n; $i++) {
    $left = $arr[$i - 1] ?? $last;
    $right = $arr[$i + 1] ?? $arr[0];
    if ($arr[$i] >= (($arr[$i - 1] ?? $last) + ($arr[$i + 1] ?? $arr[0]))) {
        die('NO');
    }
}
echo 'YES' . PHP_EOL . implode(' ', $arr);

<?php

$n = trim(fgets(STDIN));

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

sort($numbers);

$lastIndex = count($numbers) - 1;

$temp = $numbers[$lastIndex];

$numbers[$lastIndex] = $numbers[$lastIndex-1];
$numbers[$lastIndex-1] = $temp;

$flag = false;

if($numbers[$lastIndex-1] < $numbers[$lastIndex-2] + $numbers[$lastIndex]) $flag = true;

if($flag){
    print("YES \n");
    foreach($numbers as $num){
        print($num." ");
    }
}
else{
    print("NO");
}


You are given a set of all integers from $$$l$$$ to $$$r$$$ inclusive, $$$l < r$$$, $$$(r - l + 1) \le 3 \cdot 10^5$$$ and $$$(r - l)$$$ is always odd.You want to split these numbers into exactly $$$\frac{r - l + 1}{2}$$$ pairs in such a way that for each pair $$$(i, j)$$$ the greatest common divisor of $$$i$$$ and $$$j$$$ is equal to $$$1$$$. Each number should appear in exactly one of the pairs.Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them.

If any solution exists, print "YES" in the first line. Each of the next $$$\frac{r - l + 1}{2}$$$ lines should contain some pair of integers. GCD of numbers in each pair should be equal to $$$1$$$. All $$$(r - l + 1)$$$ numbers should be pairwise distinct and should have values from $$$l$$$ to $$$r$$$ inclusive. If there are multiple solutions, print any of them. If there exists no solution, print "NO".

<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = bcdiv(bcadd(bcsub($b, $a), 1), 2);
$d = $a;
$e = bcadd($a, 1);
print "YES\n";
for($x = 1; $x <= $c; $x++)
{
     print $d . " " . $e . "\n";
     $d = bcadd($d, 2);
     $e = bcadd($e, 2);
}
?>


Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.Public transport is not free. There are 4 types of tickets:   A ticket for one ride on some bus or trolley. It costs c1 burles;  A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles;  A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles;  A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.

The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.

Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.

NoteIn the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles.In the second sample the profitable strategy is to buy one ticket of the fourth type.In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.

<?php
$l1 = trim(fgets(STDIN));
$l2 = trim(fgets(STDIN));
$l3 = trim(fgets(STDIN));
$l4 = trim(fgets(STDIN));

list($c1, $c2, $c3, $c4) = explode(' ', $l1);
list($n, $m) = explode(' ', $l2);
$buses = explode(' ', $l3);
$trolley = explode(' ', $l4);
$btmp = 0;
$ttmp = 0;
foreach ($buses as $b) {
    if (($b * $c1) > $c2) {
        $btmp += $c2;
    } else {
        $btmp += $b * $c1;
    }
}
if ($btmp > $c3) {
    $btmp = $c3;
}
foreach ($trolley as $t) {
    if (($t * $c1) > $c2) {
        $ttmp += $c2;
    } else {
        $ttmp += $t * $c1;
    }
}
if ($btmp > $c3) {
    $btmp = $c3;
}
if ($ttmp > $c3) {
    $ttmp = $c3;
}
if (($btmp + $ttmp) > $c4) {
    echo $c4;
} else {
    echo $btmp + $ttmp;
}

<?
list($c1,$c2,$c3, $c4)=explode(' ', trim(fgets(STDIN)));
list($m,$n) = explode(' ', trim(fgets(STDIN)));
$a = explode(' ', trim(fgets(STDIN)));
$b = explode(' ', trim(fgets(STDIN)));
//$start_time = microtime(true);
foreach($a as $a1){
    $bablo_a += min($a1*$c1,$c2,$c3,$c4);
    if($bablo_a>=$c4 || $bablo_a>=$c3){
        $bablo_a=min($c3,$c4);
        break;
    }
}
if($bablo_a<$c4){
    foreach ($b as $b1) {
        $bablo_t += min($b1*$c1,$c2,$c3,$c4);
        if($bablo_t>=$c4 || $bablo_t>=$c3){
            $bablo_t=min($c3,$c4);
            break;
        }
    }
}
$bablo=min($bablo_a+$bablo_t,$c3+$c3,$c4);
print $bablo;

<?php
list($c1, $c2, $c3, $c4) = explode(" ", trim(fgets(STDIN)));
list($n, $m) = explode(" ", trim(fgets(STDIN)));
$nv=explode(" ", trim(fgets(STDIN)));
$mv=explode(" ", trim(fgets(STDIN)));
$Bus=0;
$Train=0;
$i=0;
$l=0;
while($n!==0){
$f1=$nv[$i]*$c1;
$f2=$c2;
$Bus=$Bus+min($f1,$f2);
$i++;
$n--;
        }
$Bustotal=min($Bus,$c3);

while($m!==0){
$t1=$mv[$l]*$c1;
$t2=$c2;
$Train=$Train+min($t1,$t2);
$l++;
$m--;
        }
$Traintotal=min($Train,$c3);

echo min($Traintotal+$Bustotal,$c4) ;

?>

<?
        list($c1,$c2,$c3, $c4)=explode(' ', fgets(STDIN));
        list($m,$n) = explode(' ', fgets(STDIN));
        $a = explode(' ', fgets(STDIN));
        $b = explode(' ', fgets(STDIN));

        //$start_time = microtime(true);
        foreach($a as $a1){
            $bablo_a += min($a1*$c1,$c2,$c3,$c4);
            if($bablo_a>=$c4 || $bablo_a>=$c3){
                $bablo_a=min($c3,$c4);
                break;
            }
        }
        if($bablo_a<$c4){
            foreach ($b as $b1) {
                $bablo_t += min($b1*$c1,$c2,$c3,$c4);
                if($bablo_t>=$c4 || $bablo_t>=$c3){
                    $bablo_t=min($c3,$c4);
                    break;
                }
            }
        }
        $bablo=min($bablo_a+$bablo_t,$c3+$c3,$c4);
        print $bablo;

<?php
echo"
<form method=post action=#>
c1  <input type=text name=c1> , c2  <input type=text name=c2>, c3  <input type=text name=c3>, c4  <input type=text name=c4>

n  <input type=text name=n>   , m  <input type=text name=m>

a  <input type=text name=a>

b  <input type=text name=b>
<input type=submit name=submit>
</form>
";
if(isset($_POST["submit"])){
$c1=$_POST["c1"];
$c2=$_POST["c2"];
$c3=$_POST["c3"];
$c4=$_POST["c4"];
$n=$_POST["n"];
$m=$_POST["m"];
$a=$_POST["a"];
$Bus=0;
$Train=0;
$i=0;
$l=0;
$nv=explode(" ",$_POST["a"]);
$b=$_POST["b"];
$mv=explode(" ",$_POST["b"]);
while($n!==0){
$f1=$nv[$i]*$c1;
$f2=$c2;
$Bus=$Bus+min($f1,$f2);
$i++;
$n--;
        }
$Bustotal=min($Bus,$c3);

while($m!==0){
$t1=$mv[$l]*$c1;
$t2=$c2;
$Train=$Train+min($t1,$t2);
$l++;
$m--;
        }
$Traintotal=min($Train,$c3);

echo min($Traintotal+$Bustotal,$c4) ;

							}

?>

<?php
if(isset($_POST["submit"])){
$c1=$_POST["c1"];
$c2=$_POST["c2"];
$c3=$_POST["c3"];
$c4=$_POST["c4"];
$n=$_POST["n"];
$m=$_POST["m"];
$a=$_POST["a"];
$Bus=0;
$Train=0;
$i=0;
$l=0;
$nv=explode(" ",$_POST["a"]);
$b=$_POST["b"];
$mv=explode(" ",$_POST["b"]);
while($n!==0){
$f1=$nv[$i]*$c1;
$f2=$c2;
$Bus=$Bus+min($f1,$f2);
$i++;
$n--;
        }
$Bustotal=min($Bus,$c3);

while($m!==0){
$t1=$mv[$l]*$c1;
$t2=$c2;
$Train=$Train+min($t1,$t2);
$l++;
$m--;
        }
$Traintotal=min($Train,$c3);

echo min($Traintotal+$Bustotal,$c4) ;

							}else{
							echo"
<form method=post action=#>
c1  <input type=text name=c1> , c2  <input type=text name=c2>, c3  <input type=text name=c3>, c4  <input type=text name=c4>

n  <input type=text name=n>   , m  <input type=text name=m>

a  <input type=text name=a>

b  <input type=text name=b>
<input type=submit name=submit>
</form>
";}

?>


Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle. As you spend lots of time "glued to the screen", your vision is impaired. So you have to write a program that will pass the check for you.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of mannequins. Next n lines contain two space-separated integers each: xi, yi (|xi|, |yi| ≤ 1000) — the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane.

Print a single real number — the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10 - 6.

NoteSolution for the first sample test is shown below:   Solution for the second sample test is shown below:   Solution for the third sample test is shown below:   Solution for the fourth sample test is shown below:


As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting of cubes of the same size. Tower with number i consists of ai cubes stacked one on top of the other. Petya defines the instability of a set of towers as a value equal to the difference between the heights of the highest and the lowest of the towers. For example, if Petya built five cube towers with heights (8, 3, 2, 6, 3), the instability of this set is equal to 6 (the highest tower has height 8, the lowest one has height 2). The boy wants the instability of his set of towers to be as low as possible. All he can do is to perform the following operation several times: take the top cube from some tower and put it on top of some other tower of his set. Please note that Petya would never put the cube on the same tower from which it was removed because he thinks it's a waste of time. Before going to school, the boy will have time to perform no more than k such operations. Petya does not want to be late for class, so you have to help him accomplish this task.

The first line contains two space-separated positive integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1000) — the number of towers in the given set and the maximum number of operations Petya can perform. The second line contains n space-separated positive integers ai (1 ≤ ai ≤ 104) — the towers' initial heights.

In the first line print two space-separated non-negative integers s and m (m ≤ k). The first number is the value of the minimum possible instability that can be obtained after performing at most k operations, the second number is the number of operations needed for that. In the next m lines print the description of each operation as two positive integers i and j, each of them lies within limits from 1 to n. They represent that Petya took the top cube from the i-th tower and put in on the j-th one (i ≠ j). Note that in the process of performing operations the heights of some towers can become equal to zero. If there are multiple correct sequences at which the minimum possible instability is achieved, you are allowed to print any of them.

NoteIn the first sample you need to move the cubes two times, from the second tower to the third one and from the second one to the first one. Then the heights of the towers are all the same and equal to 6.


Ridbit starts with an integer $$$n$$$.In one move, he can perform one of the following operations:   divide $$$n$$$ by one of its proper divisors, or  subtract $$$1$$$ from $$$n$$$ if $$$n$$$ is greater than $$$1$$$. A proper divisor is a divisor of a number, excluding itself. For example, $$$1$$$, $$$2$$$, $$$4$$$, $$$5$$$, and $$$10$$$ are proper divisors of $$$20$$$, but $$$20$$$ itself is not.What is the minimum number of moves Ridbit is required to make to reduce $$$n$$$ to $$$1$$$?

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^9$$$).

For each test case, output the minimum number of moves required to reduce $$$n$$$ to $$$1$$$.

NoteFor the test cases in the example, $$$n$$$ may be reduced to $$$1$$$ using the following operations in sequence$$$1$$$$$$2 \xrightarrow{} 1$$$$$$3 \xrightarrow{} 2 \xrightarrow{} 1$$$$$$4 \xrightarrow{} 2 \xrightarrow{} 1$$$$$$6 \xrightarrow{} 2 \xrightarrow{} 1$$$$$$9 \xrightarrow{} 3 \xrightarrow{} 2\xrightarrow{} 1$$$

<?php
fscanf(STDIN, "%d", $count);
$result = array();
for ($i = 0; $i < $count; $i++) {
    fscanf(STDIN, "%d", $number);
    $turns_number = 0;
    if ($number == 1)
        $turns_number = 0;
    else if ($number == 2)
        $turns_number = 1;
    else if ($number == 3)
        $turns_number = 2;
    else if ($number % 2 == 0)
        $turns_number = 2;
    else if ($number % 2 != 0)
        $turns_number = 3;
    array_push($result, $turns_number);
}

for ($i = 0; $i < $count; $i++){
    echo $result[$i];
    if ($i < $count - 1)
        echo "\n";
}
?>

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

   for ($i = 0; $i < $t; $i++) {
        $n = intval(fgets(STDIN));
        $iCount = 0;
        while ($n !== 1) {
            $div = printDivisors($n);
            if ($div == 0 ) {
                $n = $n -1;
                $iCount++;
            } else {
                $n = $n /$div;
                $iCount++;
            }
        }
        echo $iCount."\n";
   }

function printDivisors($n){
    // Note that this loop
    // runs till square root
    $iMax = 0;
    for ($i = 2; $i <= sqrt($n); $i++) {
        if ($n%$i == 0) {
            $i2 = $n/$i;
            if ($i2 > $iMax) {
                $iMax = $i2;
            }
            if ($i > $iMax) {
                $iMax = $i;
            }
        }
    }
    return $iMax;
}

?>

<?php
fscanf(STDIN, "%d", $count);
$result = array();
for ($i = 0; $i < $count; $i++) {
    fscanf(STDIN, "%d", $number);
    $turns_number = 0;
    while ($number > 1) {
        $success = false;
        for ($j = 2; $j <= sqrt($number); $j++) {
            if ($number % $j == 0) {
                $number = $j;
                $turns_number++;
                $success = true;
                break;
            }
        }
        if (!$success){
            $number--;
            $turns_number++;
        }
    }
    array_push($result, $turns_number);
}

for ($i = 0; $i < $count; $i++){
    echo $result[$i];
    if ($i < $count - 1)
        echo "\n";
}
?>


Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game.Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, or a - b, or a × b.After n - 1 steps there is only one number left. Can you make this number equal to 24?

The first line contains a single integer n (1 ≤ n ≤ 105).

If it's possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes). If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: "a op b = c". Where a and b are the numbers you've picked at this operation; op is either "+", or "-", or "*"; c is the result of corresponding operation. Note, that the absolute value of c mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces. If there are multiple valid answers, you may print any of them.

<?php
error_reporting("off");
//469DC: 24 Game
$n = trim(fgets(STDIN));
if ($n < 4) {
	echo "NO";
} elseif ($n % 2 == 0) {
	echo "YES\n";
	$switch = false;
	$zerocount = 0;
	for ($i = $n; $i > 4; $i = $i - 2) {
		$minus = $i - 1;
		echo $i." - ".$minus." =  1\n";
		if ($switch == true) {
			echo "1 - 1 = 0\n";
			$zerocount++;
			$switch = false;
		} else {
			$switch = true;
		}
	}
	if ($switch == true) {
		echo "1 * 1 = 1\n";
	}
	echo str_repeat("0 + 0 = 0\n", $zerocount - 1);
	if ($n > 6) {  echo "1 + 0 = 1\n"; }
	echo "4 * 3 = 12\n";
	echo "12 * 2 = 24\n";
	echo "24 * 1 = 24\n";
} else {
	echo "YES\n";
	$switch = false;
	$zerocount = 0;
	for ($i = $n; $i > 5; $i = $i - 2) {
		$minus = $i - 1;
		echo $i." - ".$minus." =  1\n";
		if ($switch == true) {
			echo "1 - 1 = 0\n";
			$zerocount++;
			$switch = false;
		} else {
			$switch = true;
		}
	}
	if ($switch == true) {
		echo "1 * 1 = 1\n";
	}
	echo str_repeat("0 + 0 = 0\n", $zerocount - 1);
	if ($n > 7) {  echo "1 + 0 = 1\n"; }
	echo "2 * 4 = 8\n";
	echo "3 - 1 = 2\n";
	echo "5 - 2 = 3\n";
	echo "8 * 3 = 24\n";
}
?>


At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.Fortunately, Picks remembers something about his set S:  its elements were distinct integers from 1 to limit;  the value of  was equal to sum; here lowbit(x) equals 2k where k is the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary representation). Can you help Picks and find any set S, that satisfies all the above conditions?

The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 105).

In the first line print an integer n (1 ≤ n ≤ 105), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them. If it's impossible to find a suitable set, print -1.

NoteIn sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4.

<?php

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

list($sum,$lim) = explode(" ", rtrim(fgets($in)));
$r = array();
for($i=$lim; $i>0; $i--){

    $lb = lowbit($i);

    if($sum>=$lb){
        $r[] = $i;
        $sum -= $lb;
    }

    if($sum==0)
        break;
}
if($sum==0){
    $s = count($r);
    echo $s."\n";
    for($i=0; $i<$s; $i++){
        echo $r[$i]." ";
    }
}
else
    echo "-1";
function lowbit($n){
    $s = decbin($n);
    $v=1;
    $i = strlen($s)-1;
    while($s[$i]==0){
        $v*=2;
        $i--;
    }
    return $v;
}


There are n points on a straight line, and the i-th point among them is located at xi. All these coordinates are distinct.Determine the number m — the smallest number of points you should add on the line to make the distances between all neighboring points equal.

The first line contains a single integer n (3 ≤ n ≤ 100 000) — the number of points. The second line contains a sequence of integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order.

Print a single integer m — the smallest number of points you should add on the line to make the distances between all neighboring points equal.

NoteIn the first example you can add one point with coordinate 0.In the second example the distances between all neighboring points are already equal, so you shouldn't add anything.

<?php

function euclid($a, $b) {
  if ($b == 0) return $a;
  return euclid($b, $a % $b);
}

$n = intval(trim(fgets(STDIN)));
$astr = explode(" ", trim(fgets(STDIN)));
$a = array();
for ($i = 0; $i < $n; ++$i) {
  array_push($a, intval($astr[$i]));
}

sort($a);

$gcd = -1;
for ($i = 1; $i < $n; ++$i) {
  if ($gcd == -1) {
    $gcd = $a[1] - $a[0];
  } else {
    $gcd = euclid($gcd, $a[$i] - $a[$i - 1]);
  }
}

$rs = 0;
for ($i = 1; $i < $n; ++$i) {
  $rs += ($a[$i] - $a[$i - 1]) / $gcd - 1;
}

fwrite(STDOUT, strval($rs) . "\n");

?>

<?php

function gcd($a, $b) {
    if ($b === 0) {
        return abs($a);
    }
    return gcd($b, $a % $b);
}

fscanf(STDIN, "%d", $n);
foreach (explode(" ", fgets(STDIN)) as $i => $str) {
    $arr[$i] = intval($str);
}
sort($arr);
$min = $arr[0];
$max = $arr[$n - 1];
$res = $max - $min;
foreach ($arr as $x) {
    $res = gcd($res, $x - $min);
}
print(($max - $min) / $res - $n + 1);
print("\n");

<?php
$n = (int)fgets(STDIN);
$a = array_map('intval',explode(' ',trim(fgets(STDIN))));
sort($a);

$gcd = 0;
$gcdf = false;

function fgcd($a,$b) {
    return ($a % $b) ? fgcd($b,$a % $b) : $b;
}

for($i = 0; $i < ($n) - 1; $i++) {
    $delt = $a[($i)+1]-$a[0];
    if(!$gcdf) {
        $gcd = $delt;
        $gcdf = true;
    } else {
        $gcd = fgcd($gcd, $delt);
    }
}
$ans = 1 + intdiv(($a[$n - 1] - $a[0]),$gcd) - $n;

echo $ans;

<?php
$fl =STDIN;//fopen("input.txt","r");
fscanf($fl,"%d\n",$n);
$numbers = explode(" ", trim(fgets($fl)));
$fi = false;
$se = false;
$l = 0;
$ans = 0;
function gcd ($a, $b) {
    return $b ? gcd($b, $a % $b) : $a;
}
sort($numbers);
foreach ($numbers as &$number)
{
    $x = intval($number);
    if($fi) {
        if($se) {
            $ans = gcd($ans, $x-$l);
        }else{
            $ans = $x-$l;
        }
        $se = true;
    }
    $fi = true;
    $l = $x;
}
printf("%d\n", 1+($numbers[$n-1]-$numbers[0])/$ans - $n);
?>

<?php
$fl =STDIN;//fopen("input.txt","r");
$n = trim(fgets(STDIN));
$c = explode(" ", trim(fgets(STDIN)));
sort($c);
$arr = array();

for($i=0;$i<$n;++$i)
{
    if ($i > 0) {
        $arr[] = ($c[$i] - $c[$i-1]);
    }
}
function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return abs( max(abs($a), abs($b)) );

    $r = $a % $b;
    return ($r != 0) ?
        gcd($b, $r) :
        abs($b);
}
/*
function gcd_array($array, $a = 0)
{
    $b = array_pop($array);
    return ($b === null) ?
        (int)$a :
        gcd_array($array, gcd($a, $b));
}*/
//$arrgcd = gcd_array($arr);
$arrgcd = $arr[0];
for ($i = 0; $i < $n-1; $i++)
    $arrgcd = gcd($arrgcd,$arr[$i]);
$ans = 0;
for ($i=1; $i<$n;++$i) {
    $dif = $c[$i] - $c[$i-1];
    $ans += ($dif / $arrgcd) - 1;
}
printf("%d",$ans);
?>

<?php
$n = 0;
fscanf(STDIN, "%d", $n);

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

$b = [];
for ($i = 1; $i < $n; ++$i) {
    ++$b[$a[$i] - $a[$i - 1]];
}

function gcd($a, $b) {
    if (!$b) return $a;
    else return gcd($b, $a % $b);
}

$gcd = 0;
foreach ($b as $bi => $ci) {
    $gcd = gcd($gcd, $bi);
}

$ans = 0;
foreach ($b as $bi => $ci) {
    $ans += ($bi / $gcd - 1) * $ci;
}
echo $ans;

<?php

function euclid($a, $b) {
  if ($b == 0) return $a;
  return euclid($b, $a % $b);
}

$n = intval(trim(fgets(STDIN)));
$astr = explode(" ", trim(fgets(STDIN)));
$a = array();
for ($i = 0; $i < $n; ++$i) {
  array_push($a, intval($astr[$i]));
}

sort($a);

$gcd = -1;
for ($i = 1; $i < $n; ++$i) {
  if ($gcd == -1) {
    $gcd = $a[1] - $a[0];
  } else {
    $gcd = euclid($gcd, $a[$i] - $a[$i - 1]);
  }
}

$rs = 0;
for ($i = 1; $i < $n; ++$i) {
  $rs += ($a[$i] - $a[$i - 1]) / $gcd - 1;
}

printf("%d\n",$rs);
?>

<?php
function gcd($num1, $num2) {
   while ($num2 != 0){
     $t = $num1 % $num2;
     $num1 = $num2;
     $num2 = $t;
   }
   return $num1;
}

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

$inp = array();

$array = array();
$prev = 0;
$curs = explode(" ", fgets(STDIN));
for($i = 0; $i< $n; ++$i)
{
 $cur = (int)$curs[$i];

 array_push($inp, $cur);
}

sort($inp);

for($i = 0; $i< $n; ++$i)
{
 if ($i != 0)
 {
    $dist = abs($inp[$i] - $prev);
    array_push($array, $dist);
 }

 $prev = $inp[$i];
}

$sz = count($array);

for($i = 0; $i< $sz; ++$i)
{
 if ($i != 0)
 {
  $g = gcd($g, $array[$i]);
 }
 else
 {
  $g = $array[$i];
 }
}

$result = 0;

for($i=0; $i< $sz; ++$i)
{
 $result += (($array[$i]) / $g) - 1;
}

echo $result;

?>

<?php
$n = (int)fgets(STDIN);
$a = array_map('intval',explode(' ',trim(fgets(STDIN))));
sort($a);

$gcd = 0;
$gcdf = false;

function fgcd($a,$b) {
    return ($a % $b) ? fgcd($b,$a % $b) : $b;
}

for($i = 0; $i < ($n) - 1; $i++) {
    $delt = $a[($i)+1]-$a[$i];
    if(!$gcdf) {
        $gcd = delt;
        $gcdf = true;
    } else {
        $gcd = fgcd($gcd, $delt);
    }
}
$ans = 1 + intdiv(($a[$n - 1] - $a[0]),$gcd) - $n;

echo $ans;

<?php
$n = (int)fgets(STDIN);
$a = array_map('intval',explode(' ',trim(fgets(STDIN))));
sort($a);

$gcd = 0;
$gcdf = false;

function fgcd($a,$b) {
    return ($a % $b) ? fgcd($b,$a % $b) : $b;
}

for($i = 0; $i < ($n) - 1; $i++) {
    $delt = $a[($i)+1]-$a[0];
    if(!$gcdf) {
        $gcd = delt;
        $gcdf = true;
    } else {
        $gcd = fgcd($gcd, $delt);
    }
}
$ans = 1 + intdiv(($a[$n - 1] - $a[0]),$gcd) - $n;

echo $ans;

function euclid($a, $b) {
  if ($b == 0) return $a;
  return euclid($b, $a % $b);
}

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

$n = intval(fgets($handle));
$astr = explode(" ", fgets($handle));
$a = array();
for ($i = 0; $i < $n; ++$i) {
  array_push($a, intval($astr[$i]));
}

sort($a);

$gcd = -1;
for ($i = 1; $i < $n; ++$i) {
  if ($gcd == -1) {
    $gcd = $a[1] - $a[0];
  } else {
    $gcd = euclid($gcd, $a[$i] - $a[$i - 1]);
  }
}

$rs = 0;
for ($i = 1; $i < $n; ++$i) {
  $rs += ($a[$i] - $a[$i - 1]) / $gcd - 1;
}

print(strval($rs));

function euclid($a, $b) {
  if ($b == 0) return $a;
  return euclid($b, $a % $b);
}

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

$n = intval(fgets($handle));
$astr = explode(" ", fgets($handle));
$a = array();
for ($i = 0; $i < $n; ++$i) {
  array_push($a, intval($astr[$i]));
}

sort($a);

$gcd = -1;
for ($i = 1; $i < $n; ++$i) {
  if ($gcd == -1) {
    $gcd = $a[1] - $a[0];
  } else {
    $gcd = euclid($gcd, $a[$i] - $a[$i - 1]);
  }
}

$rs = 0;
for ($i = 1; $i < $n; ++$i) {
  $rs += ($a[$i] - $a[$i - 1]) / $gcd - 1;
}

fwrite(STDOUT, strval($rs));

function euclid($a, $b) {
  if ($b == 0) return $a;
  return euclid($b, $a % $b);
}

$n = intval(readline());
$astr = explode(" ", readline());
$a = array();
for ($i = 0; $i < $n; ++$i) {
  array_push($a, intval($astr[$i]));
}

sort($a);

$gcd = -1;
for ($i = 1; $i < $n; ++$i) {
  if ($gcd == -1) {
    $gcd = $a[1] - $a[0];
  } else {
    $gcd = euclid($gcd, $a[$i] - $a[$i - 1]);
  }
}

$rs = 0;
for ($i = 1; $i < $n; ++$i) {
  $rs += ($a[$i] - $a[$i - 1]) / $gcd - 1;
}

print(strval($rs));

<?php
$fl =STDIN;//fopen("input.txt","r");
$n = trim(fgets(STDIN));
$c = explode(" ", trim(fgets(STDIN)));
sort($c);
$arr = array();

for($i=0;$i<$n;++$i)
{
    if ($i > 0) {
        $arr[] = ($c[$i] - $c[$i-1]);
    }
}
function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return abs( max(abs($a), abs($b)) );

    $r = $a % $b;
    return ($r != 0) ?
        gcd($b, $r) :
        abs($b);
}
/*
function gcd_array($array, $a = 0)
{
    $b = array_pop($array);
    return ($b === null) ?
        (int)$a :
        gcd_array($array, gcd($a, $b));
}*/
//$arrgcd = gcd_array($arr);
$arrgcd = 0;
for ($i = 0; $i < $n; $i++)
    $arrgcd = gcd($arrgcd,$c[$i]);
$ans = 0;
for ($i=1; $i<$n;++$i) {
    $dif = $c[$i] - $c[$i-1];

    $ans += ($dif / $arrgcd) - 1;
}
printf("%d",$ans);
?>

<?php
$fl =STDIN;//fopen("input.txt","r");
$n = trim(fgets(STDIN));
$c = explode(" ", trim(fgets(STDIN)));
sort($c);
$arr = array();

for($i=0;$i<$n;++$i)
{
    if ($i > 0) {
        $arr[] = ($c[$i] - $c[$i-1]);
    }
}
function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return abs( max(abs($a), abs($b)) );

    $r = $a % $b;
    return ($r != 0) ?
        gcd($b, $r) :
        abs($b);
}
/*
function gcd_array($array, $a = 0)
{
    $b = array_pop($array);
    return ($b === null) ?
        (int)$a :
        gcd_array($array, gcd($a, $b));
}*/
//$arrgcd = gcd_array($arr);
$arrgcd = $c[0];
for ($i = 0; $i < $n; $i++)
    $arrgcd = gcd($arrgcd,$c[$i]);
$ans = 0;
for ($i=1; $i<$n;++$i) {
    $dif = $c[$i] - $c[$i-1];

    $ans += ($dif / $arrgcd) - 1;
}
printf("%d",$ans);
?>


There are n servers in a laboratory, each of them can perform tasks. Each server has a unique id — integer from 1 to n.It is known that during the day q tasks will come, the i-th of them is characterized with three integers: ti — the moment in seconds in which the task will come, ki — the number of servers needed to perform it, and di — the time needed to perform this task in seconds. All ti are distinct.To perform the i-th task you need ki servers which are unoccupied in the second ti. After the servers begin to perform the task, each of them will be busy over the next di seconds. Thus, they will be busy in seconds ti, ti + 1, ..., ti + di - 1. For performing the task, ki servers with the smallest ids will be chosen from all the unoccupied servers. If in the second ti there are not enough unoccupied servers, the task is ignored.Write the program that determines which tasks will be performed and which will be ignored.

The first line contains two positive integers n and q (1 ≤ n ≤ 100, 1 ≤ q ≤ 105) — the number of servers and the number of tasks.  Next q lines contains three integers each, the i-th line contains integers ti, ki and di (1 ≤ ti ≤ 106, 1 ≤ ki ≤ n, 1 ≤ di ≤ 1000) — the moment in seconds in which the i-th task will come, the number of servers needed to perform it, and the time needed to perform this task in seconds. The tasks are given in a chronological order and they will come in distinct seconds.

Print q lines. If the i-th task will be performed by the servers, print in the i-th line the sum of servers' ids on which this task will be performed. Otherwise, print -1.

NoteIn the first example in the second 1 the first task will come, it will be performed on the servers with ids 1, 2 and 3 (the sum of the ids equals 6) during two seconds. In the second 2 the second task will come, it will be ignored, because only the server 4 will be unoccupied at that second. In the second 3 the third task will come. By this time, servers with the ids 1, 2 and 3 will be unoccupied again, so the third task will be done on all the servers with the ids 1, 2, 3 and 4 (the sum of the ids is 10).In the second example in the second 3 the first task will come, it will be performed on the servers with ids 1 and 2 (the sum of the ids is 3) during three seconds. In the second 5 the second task will come, it will be performed on the server 3, because the first two servers will be busy performing the first task.


In this problem you will have to deal with a real algorithm that is used in the VK social network.As in any other company that creates high-loaded websites, the VK developers have to deal with request statistics regularly. An important indicator reflecting the load of the site is the mean number of requests for a certain period of time of T seconds (for example, T = 60 seconds = 1 min and T = 86400 seconds = 1 day). For example, if this value drops dramatically, that shows that the site has access problem. If this value grows, that may be a reason to analyze the cause for the growth and add more servers to the website if it is really needed.However, even such a natural problem as counting the mean number of queries for some period of time can be a challenge when you process the amount of data of a huge social network. That's why the developers have to use original techniques to solve problems approximately, but more effectively at the same time.Let's consider the following formal model. We have a service that works for n seconds. We know the number of queries to this resource at at each moment of time t (1 ≤ t ≤ n). Let's formulate the following algorithm of calculating the mean with exponential decay. Let c be some real number, strictly larger than one.// setting this constant value correctly can adjust   // the time range for which statistics will be calculateddouble c = some constant value; // as the result of the algorithm's performance this variable will contain // the mean number of queries for the last // T seconds by the current moment of timedouble mean = 0.0; for t = 1..n: // at each second, we do the following:    // at is the number of queries that came at the last second;    mean = (mean + at / T) / c;Thus, the mean variable is recalculated each second using the number of queries that came at that second. We can make some mathematical calculations and prove that choosing the value of constant c correctly will make the value of mean not very different from the real mean value ax at t - T + 1 ≤ x ≤ t. The advantage of such approach is that it only uses the number of requests at the current moment of time and doesn't require storing the history of requests for a large time range. Also, it considers the recent values with the weight larger than the weight of the old ones, which helps to react to dramatic change in values quicker.However before using the new theoretical approach in industrial programming, there is an obligatory step to make, that is, to test its credibility practically on given test data sets. Your task is to compare the data obtained as a result of the work of an approximate algorithm to the real data. You are given n values at, integer T and real number c. Also, you are given m moments pj (1 ≤ j ≤ m), where we are interested in the mean value of the number of queries for the last T seconds. Implement two algorithms. The first one should calculate the required value by definition, i.e. by the formula . The second algorithm should calculate the mean value as is described above. Print both values and calculate the relative error of the second algorithm by the formula , where approx is the approximate value, obtained by the second algorithm, and real is the exact value obtained by the first algorithm.

The first line contains integer n (1 ≤ n ≤ 2·105), integer T (1 ≤ T ≤ n) and real number c (1 < c ≤ 100) — the time range when the resource should work, the length of the time range during which we need the mean number of requests and the coefficient c of the work of approximate algorithm. Number c is given with exactly six digits after the decimal point. The next line contains n integers at (1 ≤ at ≤ 106) — the number of queries to the service at each moment of time. The next line contains integer m (1 ≤ m ≤ n) — the number of moments of time when we are interested in the mean number of queries for the last T seconds. The next line contains m integers pj (T ≤ pj ≤ n), representing another moment of time for which we need statistics. Moments pj are strictly increasing.

Print m lines. The j-th line must contain three numbers real, approx and error, where:    is the real mean number of queries for the last T seconds;  approx is calculated by the given algorithm and equals mean at the moment of time t = pj (that is, after implementing the pj-th iteration of the cycle);   is the relative error of the approximate algorithm.  The numbers you printed will be compared to the correct numbers with the relative or absolute error 10 - 4. It is recommended to print the numbers with at least five digits after the decimal point.


Petya has n positive integers a1, a2, ..., an. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.

The first line contains a single integer n (1 ≤ n ≤ 1 000) — the number of Petya's numbers. Each of the following lines contains non-empty string si consisting of lowercase Latin letters from 'a' to 'j' — the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters.

Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.

NoteIn the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3]. In the second example the numbers after the restoration can look like: [11, 22, 11].


Almost every text editor has a built-in function of center text alignment. The developers of the popular in Berland text editor «Textpad» decided to introduce this functionality into the fourth release of the product.You are to implement the alignment in the shortest possible time. Good luck!

The input file consists of one or more lines, each of the lines contains Latin letters, digits and/or spaces. The lines cannot start or end with a space. It is guaranteed that at least one of the lines has positive length. The length of each line and the total amount of the lines do not exceed 1000.

Format the given text, aligning it center. Frame the whole text with characters «*» of the minimum size. If a line cannot be aligned perfectly (for example, the line has even length, while the width of the block is uneven), you should place such lines rounding down the distance to the left or to the right edge and bringing them closer left or right alternatively (you should start with bringing left). Study the sample tests carefully to understand the output format better.

welcome to the
Codeforces
Beta
Round 5

and
good luck

<?php
$array = array();

// Разбиваем по принципу по строка = 1000 символов
for($x = 1; $x <= 1000; $x++)
{
    $string = fgets(STDIN);
    if($string)
    {
        // Вставляем данные в массив, заодно избавлясь от переносов строк
        array_push($array, trim($string));
    }
}

// Проходимся по всем элементам массива функций strlen (длина строки) и формируем массив с результатами
$array_length = array_map('strlen', $array);

// Длина самой большой строки
$max_length = max($array_length);

// Первая и последная строки
$first_and_end_string = str_repeat("*", $max_length+2);

// Выводим результат
printf('%s', $first_and_end_string."\n");

$w=true;
foreach ($array as $key => $text) {
    // Длина строки
    $length = strlen($text);

    // Генерируем пробелы, проверяя на четность деления
    $space_left = ceil(($max_length-$length)/2);
    $space_right = $space_left;
    if( ($max_length - $length) % 2 !=0 ) {
        if($w) {
            $space_left--;
            $w=false;
        } else {
            $space_right--;
            $w=true;
        }
    }

    $sting = '*' . str_repeat(" ", $space_left) . $text . str_repeat(" ", $space_right) . '*';
    printf('%s', $sting."\n");
}
printf('%s', $first_and_end_string."\n");
?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt ="This  is

// Codeforces
// Beta
// Round
// 5
// ";
$text = array_map('trim',explode("\n",$datatxt));
unset($text[count($text)-1]);
$maxlen = max(array_map('strlen', $text))+2;
echo echo_line($maxlen,"*");
echo "\n";
// var_dump($text);
$not = true;
foreach($text as $value) {
$right = ceil(($maxlen - strlen($value))/2)-1;
$left = $right;
if (($maxlen - strlen($value))%2 !=0) {
if ($not){
$left--;
$not=false;
}else {
$right--;
$not=true;
}
}
echo "*".echo_line($left," ").$value.echo_line($right," ")."*"."\n";
}
echo echo_line($maxlen,"*");
function echo_line($number,$str){
$re = "";
for($i=0;$i<$number;$i++){
$re.=$str;
}
return $re;
}
?>

<?php
	define('ENDL', "\r\n");
	define('SP', ' ');
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	#$fr = fopen("input.txt","r");
	while ( $input = fgets($fr) )
		$text[] = trim($input);
	fclose ($fr);

	foreach ( $text as $line )
		$width = max($width, strlen($line));

	$bAlignLeft = true;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			$bAlignLeft = !$bAlignLeft;
			echo SP;
			echo str_pad($line, $width - 1, SP, STR_PAD_BOTH);
		}
		else
		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			$bAlignLeft = !$bAlignLeft;
			echo str_pad($line, $width - 1, SP, STR_PAD_BOTH);
			echo SP;
		}
		else
			echo str_pad($line, $width, SP, STR_PAD_BOTH);

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2);
?>

<?php
$array =  explode("\n", fgets(STDIN));

// Проходимся по всем элементам массива функций strlen (длина строки) и формируем массив с результатами
$array_length = array_map('strlen', $array);

// Длина самой большой строки
$max_length = max($array_length);

// Первая и последная строки
$first_and_end_string = str_repeat("*", $max_length+2);

printf('%s', $first_and_end_string."\n");
foreach ($array as $key => $text) {
    // Длина строки
    $length = strlen($text);
    // Пробелы до
    $space_left = ceil(($max_length-$length)/2);
    // Пробелы после
    $space_right = ($max_length-$length) - $space_left;
    $sting = '*' . str_repeat(" ", $space_left) . $text . str_repeat(" ", $space_right) . '*';
    printf('%s', $sting."\n");
}
printf('%s', $first_and_end_string."\n");
?>

<?php
$array = array();

// Разбиваем по принципу по строка = 1000 символов
for($x = 1; $x <= 1000; $x++)
{
    $string = fgets(STDIN);
    if($string)
    {
        // Вставляем данные в массив, заодно избавлясь от переносов строк
        array_push($array, trim($string));
    }
}

// Проходимся по всем элементам массива функций strlen (длина строки) и формируем массив с результатами
$array_length = array_map('strlen', $array);

// Длина самой большой строки
$max_length = max($array_length);

// Первая и последная строки
$first_and_end_string = str_repeat("*", $max_length+2);

// Выводим результат
printf('%s', $first_and_end_string."\n");
foreach ($array as $key => $text) {
    // Длина строки
    $length = strlen($text);

    // Пробелы до
    $space_left = ceil(($max_length-$length)/2);

    // Пробелы после
    $space_right = ($max_length-$length) - $space_left;
    $sting = '*' . str_repeat(" ", $space_left) . $text . str_repeat(" ", $space_right) . '*';
    printf('%s', $sting."\n");
}
printf('%s', $first_and_end_string."\n");
?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt ="This  is

// Codeforces
// Beta
// Round
// 5
// ";
$text = array_map('trim',explode("\n",$datatxt));
unset($text[count($text)-1]);
$maxlen = max(array_map('strlen', $text))+2;
echo echo_line($maxlen,"*");
// echo "\n";
var_dump($text);
foreach($text as $value) {
$right = ceil(($maxlen - strlen($value))/2)-1;
$left = ($maxlen - strlen($value))%2 ==0 ? $right : $right-1;
echo "*".echo_line($left," ").$value.echo_line($right," ")."*"."\n";
}
echo echo_line($maxlen,"*");
function echo_line($number,$str){
$re = "";
for($i=0;$i<$number;$i++){
$re.=$str;
}
return $re;
}
?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt ="This  is

// Codeforces
// Beta
// Round
// 5
// ";
$text = array_map('trim',explode("\n",$datatxt));
unset($text[count($text)-1]);
$maxlen = max(array_map('strlen', $text))+2;
echo echo_line($maxlen,"*");
echo "\n";
// var_dump($text);
foreach($text as $value) {
$right = ceil(($maxlen - strlen($value))/2)-1;
$left = ($maxlen - strlen($value))%2 ==0 ? $right : $right-1;
echo "*".echo_line($left," ").$value.echo_line($right," ")."*"."\n";
}
echo echo_line($maxlen,"*");
function echo_line($number,$str){
$re = "";
for($i=0;$i<$number;$i++){
$re.=$str;
}
return $re;
}
?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt ="This  is

// Codeforces
// Beta
// Round
// 5
// ";
$text = array_map('trim',explode("\n",$datatxt));
unset($text[count($text)-1]);
$maxlen = max(array_map('strlen', $text))+2;
echo echo_line($maxlen,"*");
// echo "\n";
// var_dump($text);
foreach($text as $value) {
$right = ceil(($maxlen - strlen($value))/2)-1;
$left = ($maxlen - strlen($value))%2 ==0 ? $right : $right-1;
echo "*".echo_line($left," ").$value.echo_line($right," ")."*"."\n";
}
echo echo_line($maxlen,"*");
function echo_line($number,$str){
$re = "";
for($i=0;$i<$number;$i++){
$re.=$str;
}
return $re;
}
?>

<?php
error_reporting(0);
$dir = fopen("php://stdin", 'r');
$datatxt = stream_get_contents($dir);
// $datatxt ="h1
// b2";
$text = explode("\n",$datatxt);
unset($text[count($text)-1]);
$maxlen = max(array_map('strlen', $ary));
echo_line($maxlen+2,"*");
echo "\n";
foreach($text as $value) {
$right = ceil(($maxlen - strlen($value))/2);
$left = ($maxlen - strlen($value))%2 ==0 ? $right : $right-1;
echo "*";
echo_line($left," ");
echo $value;
echo_line($right," ");
echo "*";
echo "\n";
}
echo_line($maxlen+2,"*");
function echo_line($number,$str){
for($i=0;$i<$number;$i++){
echo $str;
}
}
?>

<?php
	define('ENDL', "\r\n");
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
			echo ' ';

		$n = (int) (($width - strlen($line)) / 2);

		echo str_repeat(' ', $n);
		echo $line;
		echo str_repeat(' ', $n);

		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
			echo ' ';

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2).ENDL;
?>

<?php
	define('ENDL', "\r\n");
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	#$fr = fopen("input.txt","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;
	$bChange = false;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			echo ' ';
			$bChange = true;
		}

		$n = (int) (($width - strlen($line)) / 2);

		echo str_repeat(' ', $n);
		echo $line;
		echo str_repeat(' ', $width - strlen($line) - $n);

		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			echo ' ';
			$bChange = true;
		}
		if ( $bChange )
			$bAlignLeft = !$bAlignLeft;

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2).ENDL;
?>

<?php
	define('ENDL', "\r\n");
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
			echo ' ';

		$n = (int) (($width - strlen($line)) / 2);

		echo str_repeat(' ', $n);
		echo $line;
		echo str_repeat(' ', $n);

		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
			echo ' ';

		echo '*'.ENDL;
	}
?>

<?php
	define('ENDL', "\r\n");
	define('SP', ' ');
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	#$fr = fopen("input.txt","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			$bAlignLeft = !$bAlignLeft;
			echo SP;
			echo str_pad($line, $width - 1, SP, STR_PAD_BOTH);
		}
		else
		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			$bAlignLeft = !$bAlignLeft;
			echo str_pad($line, $width - 1, SP, STR_PAD_BOTH);
			echo SP;
		}
		else
			echo str_pad($line, $width, SP, STR_PAD_BOTH);

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2).ENDL;
?>

<?php
	define('ENDL', "\r\n");
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	#$fr = fopen("input.txt","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;
	$bChange = false;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			echo ' ';
			$bChange = true;
		}

		$n = (int) (($width - strlen($line)) / 2);

		echo str_repeat(' ', $n);
		echo $line;
		echo str_repeat(' ', $width - strlen($line) - $n);

		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			echo ' ';
			$bChange = true;
		}
		if ( $bChange )
		{
			$bAlignLeft = !$bAlignLeft;
			$bChange = false;
		}

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2).ENDL;
?>

<?php
	define('ENDL', "\r\n");
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;
	$bChange = false;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			echo ' ';
			$bChange = true;
		}

		$n = (int) (($width - strlen($line)) / 2);

		echo str_repeat(' ', $n);
		echo $line;
		echo str_repeat(' ', $n);

		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			echo ' ';
			$bChange = true;
		}
		if ( $bChange )
			$bAlignLeft = !$bAlignLeft;

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2).ENDL;
?>

<?php
	define('ENDL', "\r\n");
	define('SP', ' ');
	$text = array();
	$width = 0;
	$fr = fopen("php://stdin","r");
	#$fr = fopen("input.txt","r");
	while ( $input = fgets($fr) )
	{
		$text[] = trim($input);
		$width = max($width, strlen($input));
	}
	fclose ($fr);

	$bAlignLeft = true;

	echo str_repeat('*', $width + 2).ENDL;
	foreach ( $text as $line )
	{
		echo '*';

		if ( !$bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			$bAlignLeft = !$bAlignLeft;
			echo SP;
			echo str_pad($line, $width - 1, SP, STR_PAD_BOTH);
		}
		else
		if ( $bAlignLeft && ((strlen($line) % 2) != ($width % 2)) )
		{
			$bAlignLeft = !$bAlignLeft;
			echo str_pad($line, $width - 1, SP, STR_PAD_BOTH);
			echo SP;
		}
		else
			echo str_pad($line, $width, SP, STR_PAD_BOTH);

		echo '*'.ENDL;
	}
	echo str_repeat('*', $width + 2);
?>


Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.Unfortunately, he has just s dollars for sugar. But that's not a reason to be sad, because there are n types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change.

The first line contains two space-separated integers n, s (1 ≤ n, s ≤ 100). The i-th of the next n lines contains two integers xi, yi (1 ≤ xi ≤ 100; 0 ≤ yi < 100), where xi represents the number of dollars and yi the number of cents needed in order to buy the i-th type of sugar.

Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.

NoteIn the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.

<?php
list($n, $s) = explode(' ',trim(fgets(STDIN)));
$max = array();
$sugar = array();

for( $i=0; $i<$n; $i++ ){
    list($d, $ds) = explode(' ',fgets(STDIN));

    if( $s >= doubleval($d.'.'.$ds) && $ds != 0 ){
        $max[] = (100 - $ds);
    }elseif($s >=$d && $ds ==0){
        $sugar[] = $ds;
    }
}

if(getArrayMax( $max ) > 0){
    print getArrayMax($max);
}elseif( count( $sugar ) > 0 ){
    print 0;
}
else{
    print '-1';
}

function getArrayMax( $array ){
    $maxV = 0;
    foreach( $array as $key=>$value ){
        if( $value > $maxV ){
            $maxV = $value;
        }
    }

    return $maxV;
}

<?php
list($n, $s) = explode(' ',trim(fgets(STDIN)));
$max = array();

for( $i=0; $i<$n; $i++ ){
    list($d, $ds) = explode(' ',fgets(STDIN));
    if( $s >= $d && $ds != 0 ){
        $max[] = (100 - $ds);
    }
}

if( count($max) == 0 ){
    print '-1';
}else{
    print getArrayMax( $max );
}

function getArrayMax( $array ){
    $maxV = 0;
    foreach( $array as $key=>$value ){
        if( $value > $maxV ){
            $maxV = $value;
        }
    }

    return $maxV;
}

<?php
list($n, $s) = explode(' ',trim(fgets(STDIN)));
$max = array();
$sugar = array();

for( $i=0; $i<$n; $i++ ){
    list($d, $ds) = explode(' ',fgets(STDIN));
    if( $s >= $d && $ds != 0 ){
        $max[] = (100 - $ds);
    }elseif($s >=$d && $ds ==0){
        $sugar[] = $ds;
    }
}

if(getArrayMax( $max ) > 0){
    print getArrayMax($max);
}elseif( count( $sugar ) > 0 ){
    print 0;
}
else{
    print '-1';
}

function getArrayMax( $array ){
    $maxV = 0;
    foreach( $array as $key=>$value ){
        if( $value > $maxV ){
            $maxV = $value;
        }
    }

    return $maxV;
}

<?php
list($n, $s) = explode(' ',trim(fgets(STDIN)));
$max = array();
$sugar = array();

for( $i=0; $i<$n; $i++ ){
    list($d, $ds) = explode(' ',fgets(STDIN));
    if( $s >= $d && $s >= $d.$ds && $ds != 0 ){
        $max[] = (100 - $ds);
    }elseif($s >=$d && $ds ==0){
        $sugar[] = $ds;
    }
}

if(getArrayMax( $max ) > 0){
    print getArrayMax($max);
}elseif( count( $sugar ) > 0 ){
    print 0;
}
else{
    print '-1';
}

function getArrayMax( $array ){
    $maxV = 0;
    foreach( $array as $key=>$value ){
        if( $value > $maxV ){
            $maxV = $value;
        }
    }

    return $maxV;
}


You are given a text consisting of $$$n$$$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $$$w_i$$$ is the $$$i$$$-th word of text. All words consist only of lowercase Latin letters.Let's denote a segment of words $$$w[i..j]$$$ as a sequence of words $$$w_i, w_{i + 1}, \dots, w_j$$$. Two segments of words $$$w[i_1 .. j_1]$$$ and $$$w[i_2 .. j_2]$$$ are considered equal if $$$j_1 - i_1 = j_2 - i_2$$$, $$$j_1 \ge i_1$$$, $$$j_2 \ge i_2$$$, and for every $$$t \in [0, j_1 - i_1]$$$ $$$w_{i_1 + t} = w_{i_2 + t}$$$. For example, for the text "to be or not to be" the segments $$$w[1..2]$$$ and $$$w[5..6]$$$ are equal, they correspond to the words "to be".An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $$$w[2..4]$$$ and $$$w[6..8]$$$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $$$w[2..5]$$$ and $$$w[6..9]$$$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".What is the minimum length of the text after at most one abbreviation?

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 300$$$) — the number of words in the text. The next line contains $$$n$$$ space-separated words of the text $$$w_1, w_2, \dots, w_n$$$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $$$10^5$$$.

Print one integer — the minimum length of the text after at most one abbreviation.

NoteIn the first example you can obtain the text "TB or not TB".In the second example you can obtain the text "a AAAB AAAB c".In the third example you can obtain the text "AB aa AB bb".

<?php
$slov = fgets(STDIN);
$string = fgets(STDIN);

$dp = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';

for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}

for ($i=0; $i<count($words); $i++) {
    for ($j=0; $j<count($words); $j++) {
        $dp[$i][$j] = 0;
    }
}

for ($i=$slov-1; $i>=0; $i--) {
    for ($j=$slov-1; $j>=0; $j--) {
        if ($words[$i]==$words[$j]) {
            if ($i==$slov-1||$j==$slov-1) {
                $dp[$i][$j] = 1;
            } else {
                $dp[$i][$j] = $dp[$i+1][$j+1]+1;
            }
        }
    }
}

$ans = $n;
for ($i=0; $i<$slov; $i++) {
    $sum = 0;
    $word = '';
    for ($length=0; $length<$slov-$i; $length++) {
        $j = 0;
        $number = 0;
        if ($length==0) {
            $word = $words[$i+$length];
            $sum = strlen($words[$i+$length]);
        } else {
             $word = $word.' '.$words[$i+$length];
            $sum = $sum + strlen($words[$i+$length]) + 1;
        }
        while ($j<$slov) {
            //echo $j.' ';
            if ($dp[$i][$j]>$length) {
                $number++;
                $j = $j + $length + 1;
            } else {
                $j++;
            }
        }
       // echo PHP_EOL;
        $res = $n + $number*($length+1) - $number*$sum;
        //echo $word.'sum= '.$sum.' n='.$n.'res='.$res.'number ='.$number.'length ='.$length.PHP_EOL;
        if ($number>1) {
            if ($ans>$res) {
                $ans=$res;
            }
        }
    }
}
echo $ans.PHP_EOL;
/*for ($i=0; $i<count($words); $i++) {
    for ($j=0; $j<count($words); $j++) {
        echo $dp[$i][$j].' ';
    }
    echo PHP_EOL;
}*/
?>

<?php
$nn = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';

for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);

for ($i=0; $i<count($words); $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<count($words); $j++) {
        $replace = $replace.strtoupper($words[$j][0]);
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        if (isset($w[$search])) {
            continue;
        }
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            //$length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = strlen(str_replace($search, $replace, $string))-2;
            if ($min>$w[$search]) {
                $min = $w[$search];
            }
        }
    }
}

echo $min;
?>

<?php
$nn = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);

for ($i=0; $i<count($words); $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<count($words); $j++) {
        $replace = $replace.$words[$j][0];
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        //echo 'search = '.$search.'  replace = '.$replace.PHP_EOL;
        /*if (isset($w[$search])) {
            continue;
        }*/
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            $length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = $length;
            if ($min>$length) {
                $min = $length;
            }
        }
    }
}
echo $min;
?>

<?php
$slov = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';

for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);
//dasdsa
for ($i=0; $i<$slov; $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<$slov; $j++) {
        $replace = $replace.strtoupper($words[$j][0]);
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        if (isset($w[$search])) {
            continue;
        }
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            //$length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = strlen(str_replace($search, $replace, $string))-2;
            if ($min>$w[$search]) {
                $min = $w[$search];
            }
        }
    }
}

echo $min;
?>

<?php
$n = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
//$n = strlen($string);
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==strlen($string)-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);
for ($i=0; $i<count($words); $i++) {
    for ($j=$i; $j<count($words); $j++) {
        $search = '';
        $replace = '';
        for ($k=$i; $k<=$j; $k++) {
            $replace = $replace.$words[$k][0];
            if ($k==$i) {
                $search = $words[$k];
            } else {
                $search = $search.' '.$words[$k];
            }
        }
        if (substr_count($string, $search)>=2) {
            $w[$i][$j] = strlen(str_replace($search, $replace, $string));
            if ($min>$w[$i][$j]) {
                $min = $w[$i][$j];
            }
        }
        //$w[$i][$j] = 'search = '.$search.' replace = '.$replace;
    }
}
print_r($min);
?>

<?php
$nn = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string);
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==strlen($string)-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);
for ($i=0; $i<count($words); $i++) {
    for ($j=$i; $j<count($words); $j++) {
        $search = '';
        $replace = '';
        for ($k=$i; $k<=$j; $k++) {
            $replace = $replace.$words[$k][0];
            if ($k==$i) {
                $search = $words[$k];
            } else {
                $search = $search.' '.$words[$k];
            }
        }
        if (substr_count($string, $search)>=2) {
            $w[$i][$j] = strlen(str_replace($search, $replace, $string));
            if ($min>$w[$i][$j]) {
                $min = $w[$i][$j];
            }
        }
        //$w[$i][$j] = 'search = '.$search.' replace = '.$replace;
    }
}
print_r($min);
?>

<?php
$slov = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';

for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);

for ($i=0; $i<$slov; $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<$slov; $j++) {
        $replace = $replace.strtoupper($words[$j][0]);
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        if (isset($w[$search])) {
            continue;
        }
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            //$length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = strlen(str_replace($search, $replace, $string))-2;
            if ($min>$w[$search]) {
                $min = $w[$search];
            }
        }
    }
}

echo $min;
?>

<?php
$nnn = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string);
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==strlen($string)-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);
for ($i=0; $i<count($words); $i++) {
    for ($j=$i; $j<count($words); $j++) {
        $search = '';
        $replace = '';
        for ($k=$i; $k<=$j; $k++) {
            $replace = $replace.$words[$k][0];
            if ($k==$i) {
                $search = $words[$k];
            } else {
                $search = $search.' '.$words[$k];
            }
        }
        if (substr_count($string, $search)>=2) {
            $w[$i][$j] = strlen(str_replace($search, $replace, $string));
            if ($min>$w[$i][$j]) {
                $min = $w[$i][$j];
            }
        }
        //$w[$i][$j] = 'search = '.$search.' replace = '.$replace;
    }
}
print_r($min);
?>

<?php
$nn = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);

for ($i=0; $i<count($words); $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<count($words); $j++) {
        $replace = $replace.$words[$j][0];
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        //echo 'search = '.$search.'  replace = '.$replace.PHP_EOL;
        /*if (isset($w[$search])) {
            continue;
        }*/
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            //$length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = strlen(str_replace($search, $replace, $string))-2;
            if ($min>$w[$search]) {
                $min = $w[$search];
            }
        }
    }
}
echo $min;
?>

<?php
$nn = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);

for ($i=0; $i<count($words); $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<count($words); $j++) {
        $replace = $replace.$words[$j][0];
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        //echo 'search = '.$search.'  replace = '.$replace.PHP_EOL;
        if (isset($w[$search])) {
            continue;
        }
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            $length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = $length;
            if ($min>$length) {
                $min = $length;
            }
        }
    }
}
print_r($min);
?>

<?php
$slov = fgets(STDIN);
$string = fgets(STDIN);

$w = [];
$n = strlen($string)-2;
$min = $n;
//запись слов
$words = [];
$word = '';

for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==$n-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);

for ($i=0; $i<$slov; $i++) {
    $replace = '';
    $search = '';
    for ($j=$i; $j<$slov; $j++) {
        $replace = $replace.strtoupper($words[$j][0]);
        if ($j==$i) {
            $search = $words[$j];
        } else {
            $search = $search.' '.$words[$j];
        }
        /*if (isset($w[$search])) {
            continue;
        }*/
        $repeat = substr_count($string, $search);
        if ($repeat>=2) {
            //$length = $n-$repeat*strlen($search) + $repeat*strlen($replace);
            $w[$search] = strlen(str_replace($search, $replace, $string))-2;
            if ($min>$w[$search]) {
                $min = $w[$search];
            }
        }
    }
}

echo $min;
?>

<?php
$string = fgets(STDIN);

$w = [];
$n = strlen($string);
$min = $n;
//запись слов
$words = [];
$word = '';
for ($i=0; $i<strlen($string); $i++) {
    if ($string[$i]==' ') {
        $words[] = $word;
        $word = '';
    } elseif ($i==strlen($string)-1) {
        $word = $word.$string[$i];
        $words[] = $word;
    } else {
        $word = $word.$string[$i];
    }
}
//print_r($words);
for ($i=0; $i<count($words); $i++) {
    for ($j=$i; $j<count($words); $j++) {
        $search = '';
        $replace = '';
        for ($k=$i; $k<=$j; $k++) {
            $replace = $replace.$words[$k][0];
            if ($k==$i) {
                $search = $words[$k];
            } else {
                $search = $search.' '.$words[$k];
            }
        }
        if (substr_count($string, $search)>=2) {
            $w[$i][$j] = strlen(str_replace($search, $replace, $string));
            if ($min>$w[$i][$j]) {
                $min = $w[$i][$j];
            }
        }
        //$w[$i][$j] = 'search = '.$search.' replace = '.$replace;
    }
}
print_r($min);
?>