Score : 100 points

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.
Two people can directly communicate when the distance between them is at most d meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

Constraints

1 ≤ a,b,c ≤ 100

1 ≤ d ≤ 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

If A and C can communicate, print Yes; if they cannot, print No.

Sample Input 1

4 7 9 3

Sample Output 1

Yes

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

Sample Input 2

100 10 1 2

Sample Output 2

No

They cannot communicate in this case.

Sample Input 3

10 10 10 1

Sample Output 3

Yes

There can be multiple people at the same position.

Sample Input 4

1 100 2 10

Sample Output 4

Yes


<?php
  fscanf(STDIN, "%d%d%d%d", $a, $b, $c, $d);

  if(abs($a - $c) <= $d) {
    echo "Yes";
  }
  elseif(abs($a - $b) <= $d && abs($b - $c) <= $d) {
    echo "Yes";
  }
  else {
    echo "No";
  }
?>

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


<?php
fscanf(STDIN, '%d %d %d %d', $a, $b, $c, $d);

if ((abs($a - $b) <= $d && abs($b - $c) <= $d) || abs($a - $c) <= $d) {
    echo 'Yes' . PHP_EOL;
} else {
    echo 'No' . PHP_EOL;
}


Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


Score : 300 points

Problem Statement

You are given a string s.
Among the different substrings of s, print the K-th lexicographically smallest one.

A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not.
Also, we say that substrings are different when they are different as strings.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

s has five substrings: a, b, ab, ba and aba.
Among them, we should print the fourth smallest one, b.
Note that we do not count a twice.

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z


<?php
// D
class UnionFind
{
    private $par; // par[$i]:$iの親 自分が根の場合は自身のindexとなる
    private $size;

    function __construct($n)
    {
        for ($i = 0; $i < $n; $i++) {
            $this->par[$i] = $i; // 最初は全てが根
            $this->size[$i] = 1;
        }
    }

    function root($x)
    { // 根を返す
        if ($this->par[$x] === $x) return $x; // 根の場合
        return $this->par[$x] = $this->root($this->par[$x]); // 全ての枝の親を根にしながら再帰処理
    }

    function unite($x, $y)
    { // $xと$yの木を併合
        $ix = $this->root($x);
        $iy = $this->root($y);
        if ($ix === $iy) return;
        if ($this->size[$ix] < $this->size[$iy]) list($ix, $iy) = [$iy, $ix];
        $this->size[$ix] += $this->size[$iy];
        $this->par[$iy] = $ix; // $yの根を$xの根に付ける
    }

    function isSame($x, $y)
    { // $xと$yの根が同じか
        return $this->root($x) === $this->root($y);
    }

    function size($x)
    {
        return $this->size[$this->root($x)];
    }
}

fscanf(STDIN, '%d %d', $n, $m);
$p = array_map('intval', explode(' ', trim(fgets(STDIN))));

$tree = new UnionFind($n);

for ($i  = 0; $i < $m; $i++) {
    fscanf(STDIN, '%d %d', $x, $y);
    $x--;
    $y--; // 0 ~ $n-1に合わせる
    $tree->unite($x, $y);
}

$cnt = 0;
for ($i = 0; $i < $n; $i++) {
    if ($i === $p[$i] - 1 || $tree->isSame($i, $p[$i] - 1)) $cnt++;
}

echo $cnt . PHP_EOL;

Score : 400 points

Problem Statement

We have a permutation of the integers from 1 through N, p_1, p_2, .., p_N.
We also have M pairs of two integers between 1 and N (inclusive), represented as (x_1,y_1), (x_2,y_2), .., (x_M,y_M).
AtCoDeer the deer is going to perform the following operation on p as many times as desired so that the number of i (1 ≤ i ≤ N) such that p_i = i is maximized:

Choose j such that 1 ≤ j ≤ M, and swap p_{x_j} and p_{y_j}.

Find the maximum possible number of i such that p_i = i after operations.

Constraints

2 ≤ N ≤ 10^5

1 ≤ M ≤ 10^5

p is a permutation of integers from 1 through N.

1 ≤ x_j,y_j ≤ N

x_j ≠ y_j

If i ≠ j, \{x_i,y_i\} ≠ \{x_j,y_j\}.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
p_1 p_2 .. p_N
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the maximum possible number of i such that p_i = i after operations.

Sample Input 1

5 2
5 3 1 4 2
1 3
5 4

Sample Output 1

2

If we perform the operation by choosing j=1, p becomes 1 3 5 4 2, which is optimal, so the answer is 2.

Sample Input 2

3 2
3 2 1
1 2
2 3

Sample Output 2

3

If we perform the operation by, for example, choosing j=1, j=2, j=1 in this order, p becomes 1 2 3, which is obviously optimal.
Note that we may choose the same j any number of times.

Sample Input 3

10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9

Sample Output 3

8

Sample Input 4

5 1
1 2 3 4 5
1 5

Sample Output 4

5

We do not have to perform the operation.


<?php

fscanf(STDIN,"%d %d",$a,$b);
if(a>b){
echo a-1;
}else{
echo a;
}

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


<?php
fscanf(STDIN, "%d %d", $a, $b);
if ($a <= $b){
    print("$a\n");
} else {
    $ans = $a-1;
    print("$ans\n");
}
?>


Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


<?php

fscanf(STDIN,"%d %d",$N,$X);
$M = [];
for($i=1;$i<=$N;$i++){
  fscanf(STDIN,"%d",$M[$i]);
}

$min_cost_donut = min($M);

$count = $N;

$rest = $X-array_sum($M);
if($rest == 0){
  echo $count;
  return;
}

while($rest-$min_cost_donut >0){
  $rest -= $min_cost_donut;
  $count++;
}

echo $count;


<?php

$input = explode(" ",trim(fgets(STDIN)));
$count = $input[0];//3
$g = $input[1]; //1000
$sum = 0;
$c = 0;
$n = 0;

for($i=0; $i<=$count-1; $i++){//ドーナツの種類分ループさせる
    $total = trim(fgets(STDIN));//標準入力
    $c += $total;//3つの合計を出す
    $min[] = $total;//最小値を求めるため配列に入れる
}
$nokori = $g - $c;//あと何グラム残るか計算する
// var_dump($min);
$min = min($min);//最小値を出す
$v = floor($nokori / $min);//切り捨てで何個作れるか求める
echo $v + $count;//出力

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


<?php
fscanf(STDIN,'%d%d%d',$e,$n,$x);
$a=explode(" ",trim(fgets(STDIN)));
$ans=0;
for($i=0;$i<$n;++$i){
  $a[$i]<$x?++$ans:$i+=$n;
}
echo min($ans,$n-$ans);

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


<?php
list($N,$M,$X) = explode(" ",trim(fgets(STDIN)));
$A = explode(" ",trim(fgets(STDIN)));

$FEE = 0;
$fee = 0;
$x = $X;

while($X<$N){
	foreach($A as $value){
  if($value ==$X){
    $FEE++;
  }
}
$X++;
}

while($x>0){
	foreach($A as $value){
  if($value ==$x){
    $fee++;
  }
}
$x--;
}

if($FEE<$fee){
echo $FEE;
}else{
echo $fee;
}


<?php

fscanf(STDIN, "%s", $s);

if((strpos($s, 'a')!==false)&&(strpos($s, 'b')!==false)&&(strpos($s, 'c')!==false)){
    echo 'Yes';
}else{
    echo 'No';
}

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


<?php
fscanf(STDIN, "%d %d %d", $a, $b, $k);
$factors = [];
if ($b-$a >= 2*$k){
    for ($tmp = $a; $tmp < $a+$k; $tmp++){
        array_push($factors, $tmp);
    }
    for ($tmp = $b-$k+1; $tmp <= $b; $tmp++){
        array_push($factors, $tmp);
    }
} else {
    for ($tmp = $a; $tmp <= $b; $tmp++){
        array_push($factors, $tmp);
    }
}
sort($factors);
for ($tmp = 0; $tmp < count($factors); $tmp++){
    print("{$factors[$tmp]}\n");
}
?>

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

1 \leq A \leq B \leq 10^9

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


<?php
//Request Question
function getq($get_word=null){
	if($get_word===null){
		return str_replace("\n","",fgets(STDIN));
	}else if($get_word===1){
		return explode(" ",str_replace("\n","",fgets(STDIN)));
	}
}

/*		START		*/
$ABC=getq(1);
$temp = (max($ABC)*3-($ABC[0]+$ABC[1]+$ABC[2]));
if($temp%2==0){
	echo ($temp+1)."\n";
}else {
	echo $temp."\n";
}
?>

Score : 300 points

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.

Choose one among A, B and C, then increase it by 2.

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0 \leq A,B,C \leq 50

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.

Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23


<?php
fscanf(STDIN, "%d %d %d", $a, $b, $c);

$array = [$a, $b, $c];
sort($array);

$min = $array[0];
$mid = $array[1];
$max = $array[2];

echo $max - $mid + ceil(($mid - $min)/2) + ($mid-$min)%2, "\n";


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

// ？
$ans = 0;
$ans = min([$a, $b]) + min([$c, $d]);

echo($ans."\n");

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


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

  $ans = $x;
  for ($i=0; $i<$n; $i++) {
    fscanf(STDIN, "%d", $a);
    $ans += ceil($d/$a);
  }

  echo (int)$ans . "\n";
?>

Score : 200 points

Problem Statement

Some number of chocolate pieces were prepared for a training camp.
The camp had N participants and lasted for D days.
The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on.
As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

1 \leq N \leq 100

1 \leq D \leq 100

1 \leq X \leq 100

1 \leq A_i \leq 100 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

Sample Input 1

3
7 1
2
5
10

Sample Output 1

8

The camp has 3 participants and lasts for 7 days.
Each participant eats chocolate pieces as follows:

The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.

The second participant eats one chocolate piece on Day 1 and 6, for a total of two.

The third participant eats one chocolate piece only on Day 1, for a total of one.

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.

Sample Input 2

2
8 20
1
10

Sample Output 2

29

Sample Input 3

5
30 44
26
18
81
18
6

Sample Output 3

56


<?php
  $n=fgets(STDIN);
  fscanf(STDIN,'%d%d',$d,$x);
  for($i=0;$i<$n;$i++)$x+=ceil($d/fgets(STDIN));
  echo $x;


<?php
error_reporting(0);
//$N = explode(" ",trim(fgets(STDIN)));
//list($D,$X) = explode(" ",trim(fgets(STDIN)));
$N=trim(fgets(STDIN));
list($D,$X) = explode(" ",trim(fgets(STDIN)));
for($i=0;$i<$N;$i++) {
  $A[]=trim(fgets(STDIN));
}
$sum = 0;
foreach($A as $a) {
  $amari = $D % $a;
  if ($amari != 0) {
    $amari = 1;
  }
  if ($a == 1) {
    //日数分
    $sum += $D;
  }
  else {
    $sum += floor($D/$a)+$amari;
  }
}
printf("%d\n",$sum+$X);


<?php

$sc = new Scanner();
$out = new Out();

$N = $sc->nextInt();
$A = [];
for($i = 0; $i < $N; $i++) {
    $A[] = $sc->nextInt();
    $cost[$i] = 0;
}

$dp = [];
$dp2 = [];
for($i = 0; $i < $N; $i++) {
    $p = 0;
    for($j = 0; $j < $N; $j++) {
        if ($i == $j) {
            continue;
        }
        if ($j == 0) {
            if (isset($dp[$i - 1])) {
                $cost[$i] = $dp[$i - 1];
                $j = $i - 2;
                $p = $A[$j];
                continue;
            }
        }
        $cost[$i] += $dp2[$p][$j] ?? $dp2[$p][$j] = abs($p - $A[$j]);
        if ($i - 1 == $j) {
            $dp[$i] = $cost[$i];
        }
        $p = $A[$j];
    }
    $cost[$i] += abs($p);
    $out->println($cost[$i]);
}

class Scanner {
    private $arr = [];
    private $count = 0;
    private $pointer = 0;
    public function next() {
        if($this->pointer >= $this->count) {
            $str = trim(fgets(STDIN));
            $this->arr = explode(' ', $str);
            $this->count = count($this->arr);
            $this->pointer = 0;
        }
        $result = $this->arr[$this->pointer];
        $this->pointer++;
        return $result;
    }
    public function hasNext() {
        return $this->pointer < $this->count;
    }
    public function nextInt() {
        return (int)$this->next();
    }
    public function nextDouble() {
        return (double)$this->next();
    }
}
class Out {
    public static function println($str = "") {
        echo $str . PHP_EOL;
    }
}

Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288


Score : 500 points

Problem Statement

You are given two integers A and B.

Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:

Let the size of the grid be h \times w (h vertical, w horizontal). Both h and w are at most 100.

The set of the squares painted white is divided into exactly A connected components.

The set of the squares painted black is divided into exactly B connected components.

It can be proved that there always exist one or more solutions under the conditions specified in Constraints section.
If there are multiple solutions, any of them may be printed.

Notes

Two squares painted white, c_1 and c_2, are called connected when the square c_2 can be reached from the square c_1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.

A set of squares painted white, S, forms a connected component when the following conditions are met:

Any two squares in S are connected.

No pair of a square painted white that is not included in S and a square included in S is connected.

A connected component of squares painted black is defined similarly.

Constraints

1 \leq A \leq 500

1 \leq B \leq 500

Input

Input is given from Standard Input in the following format:

A B

Output

Output should be in the following format:

In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.

Then, print h more lines. The i-th (1 \leq i \leq h) of these lines should contain a string s_i as follows:

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted white, the j-th character in s_i should be ..

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted black, the j-th character in s_i should be #.

Sample Input 1

2 3

Sample Output 1

3 3
##.
..#
#.#

This output corresponds to the grid below:

Sample Input 2

7 8

Sample Output 2

3 5
#.#.#
.#.#.
#.#.#

Sample Input 3

1 1

Sample Output 3

4 2
..
#.
##
##

Sample Input 4

3 14

Sample Output 4

8 18
..................
..................
....##.......####.
....#.#.....#.....
...#...#....#.....
..#.###.#...#.....
.#.......#..#.....
#.........#..####.


<?php
fscanf(STDIN, "%d %d %d", $Ayen, $Byen, $Cyen);
if($Cyen-($Ayen+$Byen) <= 0){
    echo "Yes";
}
else{
    echo "No";
}

Score : 100 points

Problem Statement

An elementary school student Takahashi has come to a variety store.

He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?

Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

If Takahashi can buy the toy, print Yes; if he cannot, print No.

Sample Input 1

50 100 120

Sample Output 1

Yes

He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.

Sample Input 2

500 100 1000

Sample Output 2

No

He has 500 + 100 = 600 yen, but he cannot buy the 1000-yen toy.

Sample Input 3

19 123 143

Sample Output 3

No

There are 19-yen and 123-yen coins in Takahashi Kingdom, which are rather hard to use.

Sample Input 4

19 123 142

Sample Output 4

Yes


Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

3
2 0
3 1
1 3
4 2
0 4
5 5

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

3
0 0
1 1
5 2
2 3
3 4
4 5

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

5
0 0
7 3
2 2
4 8
1 6
8 5
6 9
5 4
9 1
3 7

Sample Output 4

5

Sample Input 5

5
0 0
1 1
5 5
6 6
7 7
2 2
3 3
4 4
8 8
9 9

Sample Output 5

4


Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


<?php
$input=explode(' ',trim(fgets(STDIN))); // // 1 2 3 ……
//print_r($input);

if ($input[0]>2 and $input[1]>2) {
    echo( ($input[0]-2) * ($input[1]-2) );
    exit();
}elseif ($input[0]==2 or $input[1]==2) {
    exit("0");
}elseif ($input[0]==1 and $input[1]>2) {
    echo($input[1]-2);
    exit();
}elseif ($input[0]>2 and $input[1]==1) {
    echo($input[0]-2);
    exit();
}
?>


<?php
list($n) = ints();
echo intdiv($n, 3);
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


<?php
  fscanf(STDIN,'%d',$a);print(floor($a/3));


<?php

fscanf(STDIN,"%d",$n);
$s = explode(' ',trim(fgets(STDIN)));

$rsl = array_search("Y",$s,true);

if ($rsl === true){
    echo "Four";
}else{
    echo "Three";
}

Score : 200 points

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

We have a bag that contains N hina arare. (From here, we call them arare.)

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

There always exist i, j and k such that S_i=P, S_j=W and S_k=G.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

The bag contained arare in four colors, so you should print Four.

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

The bag contained arare in three colors, so you should print Three.

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


<?php
$n=trim(fgets(STDIN));
$a=explode(' ',fgets(STDIN));
while($n>0){
  --$n;
  if($a[$n]=='Y'){
    echo 'Four';
    exit;
  }
}
echo 'Three';


Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


<?php
$num = trim(fgets(STDIN));
$max = 0;
for($j = 0; $j < strlen($num) ; $j++){//9にする桁数
    $sum = $j * 9;
    for($i = 0; $i < strlen($num) - $j; $i++){
        $sum+=$num[$i];
    }
    if($j != 0){
        $sum -= 1;
    }
    $max = max($sum, $max);
}
echo $max;

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


<?php
fscanf(STDIN, "%d", $N);
fscanf(STDIN, "%d", $A);

$pittari = $N %500;
$haraeru = $pittari - $A;

if($pittari == 0 || $haraeru < 0) {
    echo "Yes";
} else {
    echo "No";
}

?>

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


<?php
  $n = trim(fgets(STDIN));
  $a = explode(" ", trim(fgets(STDIN)));
  rsort($a);
  $alice = 0;
  for($i=0; $i<$n; $i++){
    if($i%2 == 0){
      $alice += $a[$i];
    }else{
      $alice -= $a[$i];
    }
  }
  echo $alice."\n";

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


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

for ($i = 0; $i < count($a); $i++) {
    $a[$i] = (int)$a[$i];
}

$alice = 0;
$bob = 0;

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

    for ($j = 0; $j < count($a); $j++) {
        if ($a[$j] === $max) {
            array_splice($a, $j, 1);
            break;
        }
    }

    if ($i % 2 === 0) {
        $alice += $max;
    } else {
        $bob += $max;
    }
}

echo abs($alice - $bob) . "\n";


Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


<?php
// 縦h x 横w の迷路を木構造に変換（壁=# 道=.）
define('BLACK', '#');
define('WHITE', '.');
fscanf(STDIN, '%d %d', $h, $w);
for ($i  = 0; $i < $h; $i++) {
    $s[] = trim(fgets(STDIN));
}
$g = array_fill(0, $h * $w, []);
$cnt_w = 0;
for ($i  = 0; $i < $h * $w; $i++) {
    $l = intval(floor($i / $w));
    $m = $i % $w;

    if ($s[$l][$m] === BLACK) continue;

    $cnt_w++;
    if ($m < $w - 1 && $s[$l][$m + 1] === WHITE) {
        $g[$i][] = $i + 1;
        $g[$i + 1][] = $i;
    }

    if ($l < $h - 1 && $s[$l + 1][$m] === WHITE) {
        $g[$i][] = $i + $w;
        $g[$i + $w][] = $i;
    }
}

// BFS（幅優先探索）：最短経路アルゴリズムとして活用
$n = $h * $w;
$q = new SplQueue(); // キューを用意
$dist = array_fill(0, $n, -1); // 距離を格納する配列（-1の場合はその頂点が未発見）

$dist[0] = 0; // 頂点0からの距離格納配列
$q->enqueue(0); // キューに0を追加

while (!$q->isEmpty()) {
    $v = $q->dequeue();

    foreach ($g[$v] as $next_v) {
        if ($dist[$next_v] !== -1) continue; // 発見済み

        $dist[$next_v] = $dist[$v] + 1;
        $q->enqueue($next_v);
    }
}
if ($dist[$h * $w - 1] === -1) exit('-1');
echo $cnt_w - 2 - ($dist[$h * $w - 1] - 1);

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


<?php

function parsenums($str) {
    return array_map(function ($a) {
        return intval($a);
    }, preg_split("/ /", $str));
}

$hm = parsenums(fgets(STDIN));

foreach(range(0, $hm[0] - 1) as $i) {
    $map[$i] = str_split(rtrim(fgets(STDIN)));
}

// ゴールへの最短距離を求める
// 到達不可能の場合-1を返す
function shortest_dist($map, $goalx, $goaly) {
    global $hm;

    $openlist = [];

    array_push($openlist,
               array('v' => $map[0][0], 'x' => 0, 'y' => 0, 'cnt' => 1));
    while(true) {
        $next = array_shift($openlist);

        if($next == NULL) {
            return -1;
        }

        if($next['x'] == $goalx && $next['y'] == $goaly) {
            return $next['cnt'];
        }

        $x = $next['x'];
        $y = $next['y'];
        // はじっこはだめだぞ
        if(($hm[1] - 1) > $x &&
            $map[$y][$x + 1] != '#' &&
            !isset($visited[$y][$x + 1])) {
            array_push($openlist,
                array(
                    'v' => $map[$y][$x + 1],
                    'x' => $x + 1,
                    'y' => $y,
                    'cnt' => $next['cnt'] + 1
                )
            );
            $visited[$y][$x + 1] = true;
        }
        if(($hm[0] - 1) > $y &&
            $map[$y + 1][$x] != '#' &&
            !isset($visited[$y + 1][$x])) {
            array_push($openlist,
                array(
                    'v' => $map[$y + 1][$x],
                    'x' => $x,
                    'y' => $y + 1,
                    'cnt' => $next['cnt'] + 1
                )
            );
            $visited[$y + 1][$x] = true;
        }
        if(0 < $x &&
            $map[$y][$x - 1] != '#' &&
            !isset($visited[$y][$x - 1])) {
            array_push($openlist,
                array(
                    'v' => $map[$y][$x - 1],
                    'x' => $x - 1,
                    'y' => $y,
                    'cnt' => $next['cnt'] + 1
                )
            );
            $visited[$y][$x - 1] = true;
        }
        if(0 < $y &&
            $map[$y - 1][$x] != '#' &&
            !isset($visited[$y - 1][$x])) {
            array_push($openlist,
                array(
                    'v' => $map[$y - 1][$x],
                    'x' => $x,
                    'y' => $y - 1,
                    'cnt' => $next['cnt'] + 1
                )
            );
            $visited[$y - 1][$x] = true;
        }
    }
}

$dist = shortest_dist($map, $hm[1] - 1, $hm[0] - 1);
if($dist == -1) {
    echo "-1\n";
    return;
}

$black = 0;
foreach($map as $line) {
    foreach($line as $cell) {
        if ($cell == '#') {
            $black++;
        }
    }
}

$result = $hm[0] * $hm[1] - $black - $dist;
echo "$result\n";


<?php

# スペース区切りの整数の入力
fscanf(STDIN, "%d %d", $b, $c);

if( $b%$c==0 ){
    echo "-1\n";
}else{
    echo $b."\n";
}

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


<?php
error_reporting(0);
//$X = explode(" ",trim(fgets(STDIN)));
$N = trim(fgets(STDIN));
//list($X,$Y) = explode(" ",trim(fgets(STDIN)));
$a = explode(" ",trim(fgets(STDIN)));
$b = explode(" ",trim(fgets(STDIN)));

sort($a);
sort($b);
for($i=0;$i<$N;$i++) {
  if ($a[$i]>$b[$i]) {
    printf("No\n");
    exit;
  }
}
printf("Yes\n");

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


<?php

$x = fgets(STDIN);
$a = fgets(STDIN);
$b = fgets(STDIN);

echo ($x-$a) % $b;

?>

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


<?php
# 整数の入力
fscanf(STDIN, "%d", $x);
fscanf(STDIN, "%d", $a);
fscanf(STDIN, "%d", $b);

$ans = ($x - $a) % $b;
echo $ans;
?>


<?php

$val = explode(" ", trim(fgets(STDIN)));
$a = $val[0];
$b = $val[1];
$c = (int)($a.$b);
echo ($c - pow((int)sqrt($c), 2) == 0) ? "Yes" : "No";
echo PHP_EOL;

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


#!/usr/bin/env php
<?php
const WHITE = 'W';
const BLACK = 'B';
function genInputs() {
    while(($input = rtrim(fgets(STDIN))) !== false) {
        foreach (explode(' ', $input) as $v) {
            yield $v;
        }
    }
}
function getNext(Generator $gen) {
    $value = $gen->current();
    $gen->next();
    return $value;
}
$gen = genInputs();

$N = (int)getNext($gen);
$K = (int)getNext($gen);
$sq = [];
for ($i = 0; $i < $N; $i++) {
    $x = (int)getNext($gen);
    $y = (int)getNext($gen);
    $c = (string)getNext($gen);

    $x = $x % 6;
    $y = $y % 6;
    if ($x < 0) { $x += 6; }
    if ($y < 0) { $y += 6; }
    for ($j = 0; $j < 18; $j++) {
        $a = (int)($j / 3);
        $b = $j % 3;
        $diff_x = $x - $a;
        $diff_y = $y - $b;
        $is_ok = false;
        if ($diff_x >= 0) {
            if ($diff_y >= 0) {
                if ($diff_x < 3 && $diff_y < 3) {
                    $is_ok = true;
                } elseif ($diff_x >= 3 && $diff_y >= 3) {
                    $is_ok = true;
                }
            } else {
                if ($diff_x < 3 && $diff_y < -3) {
                    $is_ok = true;
                } elseif ($diff_x >= 3 && $diff_y >= -3) {
                    $is_ok = true;
                }
            }
        } else {
            if ($diff_y >= 0) {
                if ($diff_x >= -3 && $diff_y >= 3) {
                    $is_ok = true;
                } elseif ($diff_x < -3 && $diff_y < 3) {
                    $is_ok = true;
                }
            } else {
                if ($diff_x >= -3 && $diff_y >= -3) {
                    $is_ok = true;
                } elseif ($diff_x < -3 && $diff_y < -3) {
                    $is_ok = true;
                }
            }
        }
        if (($is_ok && $c == WHITE) || (!$is_ok && $c == BLACK)) {
            if (!isset($sq[$j])) {
                $sq[$j] = 0;
            }
            $sq[$j]++;
        }
    }
}
echo max($sq).PHP_EOL;

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


<?php

class Test
{
    const yen10000 = 10000;
    const yen5000  = 5000;
    const yen1000  = 1000;

    public function calc($sheets, $balance)
    {
        if(!$this->validation($sheets, $balance)){
            return '-1 -1 -1';
        }
        for($x = 0; $x <= $sheets; $x++){
            for($y = 0; $y <= $sheets; $y++){
                $z = $sheets - $x - $y;
                if($sum = $x * 10000 + $y * 5000 + $z * 1000){
                    return printf('%d %d %d', $x, $y, $z);
                }
            }
        }
        return '-1 -1 -1';
    }

    private function validation($n, $y)
    {
        if(!is_integer($n)){
            return false;
        }

        if(!is_integer($y)){
            return false;
        }
        if($n < 1 || 2000 < $n){
            // 制約に違反
            return false;
        }

        if($y < self::yen1000){
            // 制約に違反（1000 <= Y
            return false;
        }

        if((2 * pow(10, 7) < $y)){
            // 制約に違反（Y <= 2 * 10の7乗
            return false;
        }

        if(($y % self::yen1000) !== 0){
            // 制約に違反（Yは1000の倍数である
            return false;
        }
        return true;
    }
}

$input = fgets(STDIN);
list($sheets, $balance) = explode(' ', $input);

$test = new Test();
echo $test->calc($sheets, $balance);

Score : 100 points

Problem Statement

On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.

After finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.

Constraints

S is a string of length 10.

The first eight characters in S are 2017/01/.

The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace the first four characters in S with 2018 and print it.

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


<?php

fscanf(STDIN, "%s", $S);

$output = str_replace("2017", "2018", $S);
echo $output;


<?php
$data =trim(fgets(STDIN));
$replace= str_replace(2017,2018,$data);
echo $replace;
?>


<?php

class Scanner {
    private $arr = [];
    private $count = 0;
    private $pointer = 0;
    public function next() {
        if($this->pointer >= $this->count) {
            $str = trim(fgets(STDIN));
            $this->arr = explode(' ', $str);
            $this->count = count($this->arr);
            $this->pointer = 0;
        }
        $result = $this->arr[$this->pointer];
        $this->pointer++;
        return $result;
    }
    public function hasNext() {
        return $this->pointer < $this->count;
    }
    public function nextInt() {
        return (int)$this->next();
    }
    public function nextDouble() {
        return (double)$this->next();
    }
}

class out {
    public static function println($str = "") {
        echo $str . PHP_EOL;
    }
}

$sc = new Scanner();
$str = $sc->next();
$str[3] = '8';
out::println($str);


<?php
$n = fgets(STDIN);

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

echo count(array_unique($arr));

?>

Score : 200 points

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


<?php
  $N = trim(fgets(STDIN));
  $array = array();

  for($i = 0; $i < $N; $i++){
    $num = trim(fgets(STDIN));
    $exist = false;
    for($j = 0; $j < count($array); $j++){
      if($array[$j] == $num){
        $exist = true;
      }
    }
    if(!$exist){
      $array[] = $num;
    }
  }

  echo count($array);
?>


<?php
fscanf(STDIN, '%d', $x);
echo ($x + 24). PHP_EOL;

Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


<?php
fscanf(STDIN, "%d %d", $a, $b);
fscanf(STDIN, "%s", $s);
$pos = strpos($s, '-');
//var_dump($pos);
if($pos === $a) {
    if(is_numeric($a) && is_numeric($b)) {
        echo 'Yes'. "\n";
    } else {
        echo 'No'. "\n";
    }
} else {
    echo 'No'. "\n";
}

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


<?php
//error_reporting(0);
//$A = explode(" ",trim(fgets(STDIN)));
$Q = trim(fgets(STDIN));
//list($A,$B) = explode(" ",trim(fgets(STDIN)));
$cache=array();
for($i=0;$i<$Q;$i++) {
  $l[]=explode(" ",trim(fgets(STDIN)));
}
for($i=0;$i<$Q;$i++) {
  $r = 0;
  for($j=$l[$i][0];$j<=$l[$i][1];$j=$j+2) {
    if (isset($cache[$j]) && $cache[$j]==true) {
      $r++;
      continue;
    }
    $res = is2017($j);
    $cache[$j]=$res;
    if ($res) {
      $r++;
    }
  }
  printf("%d\n",$r);
}

function is2017($x) {
  global $cache;
  if (isPrime($x) && isPrime(($x+1)/2)) return true;
  return false;
}
function isPrime($x) {
    if ($x == 1) return False;
    $bCheck = True;
    $highestIntegralSquareRoot = floor(sqrt($x));
    for ($i = 2; $i <= $highestIntegralSquareRoot; $i++)
    {
        if ($x % $i == 0)
        {
             $bCheck = False;
             break;
        }
    }
    return $bCheck;
}

Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


<?php

fscanf(STDIN, '%d %d %d %d', $A, $B, $C, $D);

if ($A + $B > $C + $D) {
    echo 'Left';
} elseif ($A + $B < $C + $D) {
    echo 'Right';
} else {
    echo 'Balanced';
}

echo PHP_EOL;

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


<?php
list($a, $b, $c, $d) = ints();
if ($a + $b > $c + $d) exit('Left');
if ($a + $b === $c + $d) exit('Balanced');
echo 'Right';
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}


<?php
    fscanf(STDIN, "%d %d %d %d", $a, $b, $c, $d);
    $leftWeight = $a + $b;
    $rightWeight = $c + $d;
    switch(true){
      case $leftWeight > $rightWeight:
      echo 'Left';
      break;

      case $leftWeight < $rightWeight:
      echo 'Right';
      break;

      case $leftWeight === $rightWeight:
      echo 'Balanced';
      break;
    }

  ?>


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

$left = $num[0] + $num[1];
$right = $num[2] + $num[3];

if ( $left > $right )
{
    echo "Left"."\n";
}
elseif ( $left < $right )
{
    echo "Right"."\n";
}
else
{
    echo "Balanced"."\n";
}
?>


Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c=0;
while($a <= $b){
    $a*=2;
    $c++;
}
echo $c;


<?php
	$array = explode(' ', trim(fgets(STDIN)));
	echo ceil(array_sum($array) / count($array))
?>

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


<?php
list($s) = strs();
list($t) = strs();
$s = str_split($s);
$t = str_split($t);
natsort($s);
natsort($t);
$s = implode('', $s);
$t = strrev(implode('', $t));
echo strnatcmp($s, $t) < 0 ? 'Yes' : 'No';
function strs()
{
    return explode(' ', trim(fgets(STDIN)));
}

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


<?php
  $s = str_split(trim(fgets(STDIN)));
  $t = str_split(trim(fgets(STDIN)));
  sort($s);
  rsort($t);
  $len = max(count($s),count($t));
  for($i=0; $i<$len; $i++){
      if(isset($s[$i])) $ss = $s[$i];
      if(isset($t[$i])) $tt = $t[$i];
      if($ss < $tt){
          echo "Yes";
          exit;
      }else if($ss == $tt){
          if($i == $len-1){
              echo "Yes";
              exit;
          }
          continue;
      }else{
          echo "No";
          exit;
      }
  }
  echo "No";


<?php
$f = fopen( 'php://stdin', 'r' );
$line1 = str_replace(array("\n", "\r"), '', fgets($f));
$line2 = str_replace(array("\n", "\r"), '', fgets($f));

$stringParts1 = str_split($line1);
sort($stringParts1);
$count_pre1 = count($stringParts1);
$stringParts1 = array_unique($stringParts1);
$count_post1 = count($stringParts1);

$stringParts2 = array_filter(str_split($line2));
sort($stringParts2);
$count_pre2 = count($stringParts2);
$stringParts2 = array_unique($stringParts2);
$count_post2 = count($stringParts2);
if ($stringParts1[0] > end($stringParts2)) {
    echo('No');
    exit();
} elseif ($stringParts1[0] === end($stringParts2)) {
    if ($count_post1 === 1 && $count_post2 === 1 && $count_pre1 < $count_pre2) {
        echo('Yes');
    } else {
        echo('No');
    }
    exit();
} else {
    echo('Yes');
}


<?php

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

for($i = 0; $i < strlen($s); $i++){

    for($j = 0; $j < strlen($t); $j++){
        if($s < $t){
            $answer = true;
            break;
        }else{
            $answer = false;

        }
    }
    if($answer){
        break;
    }
}

if($answer){
    echo "Yes"."\n";
}else{
    echo "No" . "\n";
}


<?php
fscanf(STDIN, "%d", $n);
$a = explode(' ', trim(fgets(STDIN)));
$c = array_count_values($a);
$x = 0;
foreach ($c as $k => $v) {
	if ($k == $v) continue;
	if ($k < $v) {
		$x += $v - $k;
	} else {
		$x += $v;
	}
}
echo $x;

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


<?php
error_reporting(0);
//$A = explode(" ",trim(fgets(STDIN)));
$s = trim(fgets(STDIN));
$len = strlen($s);
list($x,$y) = explode(" ",trim(fgets(STDIN)));

$pos = new StdClass();
$pos->x = 0;
$pos->y = 0;
$dir = 1;

$ret = move(0,1,$pos);
if ($ret==1) {
  printf("Yes\n");
}
else {
  printf("No\n");
}
function move($cmdpos,$dir,$pos) {
  global $s,$len,$x,$y;
  //if (abs($pos->x) > 100 || abs($pos->y)>100) exit;
  if ($cmdpos == $len) {
    if ($x==$pos->x && $y == $pos->y) {
      return 1;
    }
    else {
      return 0;
    }
  }
  //printf("%d %s dir %s pos %d %d\n",$cmdpos,$s[$cmdpos],getdirStr($dir),$pos->x,$pos->y);
  if ($s[$cmdpos] == 'F') {
    $pos = updatepos($dir,$pos);
    $cmdpos++;
    $ret = move($cmdpos,$dir,$pos);
  }
  else {
    $cmdpos++;
    switch($dir) {
    case 1:
      $ret = max(move($cmdpos,4,$pos),move($cmdpos,2,$pos));
      break;
    case 2:
      $ret = max(move($cmdpos,1,$pos),move($cmdpos,3,$pos));
      break;
    case 3:
      $ret = max(move($cmdpos,2,$pos),move($cmdpos,4,$pos));
      break;
    case 4:
      $ret = max(move($cmdpos,3,$pos),move($cmdpos,1,$pos));
      break;
    }
  }
  return $ret;
}

function updatepos($dir,$p) {
  $pos = clone($p);
  switch($dir) {
  case 1:
    $pos->x += 1;
    break;
  case 2:
    $pos->y -= 1;
    break;
  case 3:
    $pos->x -= 1;
    break;
  case 4:
    $pos->y += 1;
    break;
  }
  return $pos;
}
function getdirStr($dir) {
  switch($dir) {
  case 1:return 'L';break;
  case 2:return 'D';break;
  case 3:return 'R';break;
  case 4:return 'U';break;
  }
}

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No


<?php
$s = trim(fgets(STDIN));
$n = strlen($s);
$input=explode(' ',trim(fgets(STDIN)));
$x = $input[0];
$y = $input[1];
$initial_x = 0;
$group_x = array();
$group_y = array();

$i = 0;
while($s[$i] == 'F') {
  $initial_x++;
  $i++;
}

$is_dir_x = TRUE;
$count = 0;
for(;$i<$n;$i++) {
  if($s[$i] == 'T') {
    if($count > 0) {
      if($is_dir_x) {
        $group_x[] = $count;
      } else {
        $group_y[] = $count;
      }
      $count = 0;
    }
    $is_dir_x = !$is_dir_x;
  } else {
    $count++;
  }
}
if($count > 0) {
  if($is_dir_x) {
    $group_x[] = $count;
  } else {
    $group_y[] = $count;
  }
  $count = 0;
}

$set_x = array();
$set_x[$initial_x] = TRUE;
foreach($group_x as $gx) {
  foreach($set_x as $key => $value) {
    if(!isset($set_x[$key + $gx])) {
      $set_x[$key + $gx] = TRUE;
    }
    if(!isset($set_x[$key - $gx])) {
      $set_x[$key - $gx] = TRUE;
    }
  }
}

$set_y = array();
$set_y[0] = TRUE;
foreach($group_y as $gy) {
  foreach($set_y as $key => $value) {
    if(!isset($set_y[$key + $gy])) {
      $set_y[$key + $gy] = TRUE;
    }
    if(!isset($set_y[$key - $gy])) {
      $set_y[$key - $gy] = TRUE;
    }
  }
}

if(isset($set_x[$x]) && isset($set_y[$y])) {
  echo 'Yes'.PHP_EOL;
} else {
  echo 'No'.PHP_EOL;
}

?>


<?php
list($n) = ints();
$a = ints();
$num = gcdAll($a);
$ans = 0;
while ($num % 2 === 0) {
    $ans++;
    $num /= 2;
}
echo $ans;

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

// 最大公約数（2つ）
function gcd($m, $n)
{
    if (!$n) return $m;
    return gcd($n, $m % $n);
}

// 最大公約数（3つ以上）
function gcdAll($arr)
{
    $gcd = array_pop($arr);
    foreach ($arr as $a) {
        $gcd = gcd($gcd, $a);
    }
    return $gcd;
}

// 最小公倍数（2つ）
function lcm($m, $n)
{
    return $m * $n / gcd($m, $n);
}

// 最小公倍数（3つ以上）
function lcmAll($arr)
{
    $lcm = array_pop($arr);
    foreach ($arr as $a) {
        $lcm = lcm($lcm, $a);
    }
    return $lcm;
}

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


<?php
fscanf(STDIN, '%n', $n);
$Aarray = explode(' ', trim(fgets(STDIN)));
$count = -1;
$tf = array_map('checkint', $Aarray);
while(! in_array(false, $tf)){
    $Aarray = array_map('divtwo', $Aarray);
    $tf = array_map('checkint', $Aarray);
    $count++;
}
print $count;

function divtwo($num){
    return $num/2;
}

function checkint($num){
    return filter_var($num, FILTER_VALIDATE_INT);
}


<?php
error_reporting(0);
//$A = explode(" ",trim(fgets(STDIN)));
//list($N,$K) = explode(" ",trim(fgets(STDIN)));
$N = trim(fgets(STDIN));
$A = explode(" ",trim(fgets(STDIN)));
$low = min($A);
$lowkey = array_search($low,$A);
$high = max($A);
$highkey = array_search($high,$A);
$B = $A;
//どんどん増やすかどんどん減らすかのどちらか？
$sousa=array();
if ($high<0) {
  $res = herasu($A,$N,$low,$lowkey);
}
else {
  $res = fuyasu($A,$N,$high,$highkey);
}
if (chk($res)) {
  printf("%d\n",count($sousa));
  foreach($sousa as $s) {
    printf("%s\n",$s);
  }
}

function chk($ary) {
  $len = count($ary);
  for($i=0;$i<$len-1;$i++) {
    if ($ary[$i]>$ary[$i+1]) return false;
  }
  return true;
}
function herasu($A,$N,$low,$lowkey) {
  global $sousa;
  $i=0;
  while(1) {
    if ($A[$i]>$A[$i+1]) {
      $sousa[]=sprintf("%d %d",$lowkey+1 , $i + 1);
      $A[$i]+=$low;
      if ($A[$i]<$low) {
        $low = $A[$i];
        $lowkey = $i;
      }
    }
    else {
      $i++;
    }
    if ($i==$N-1) break;
  }
  return $A;
}
function fuyasu($A,$N,$high,$highkey) {
  global $sousa;
$i=0;
while(1) {
  if ($A[$i]>$A[$i+1]) {
    $sousa[]=sprintf("%d %d",$highkey+1 , $i + 2);
    $A[$i+1]+=$high;
    if ($A[$i+1]>$high) {
      $high = $A[$i+1];
      $highkey = $i+1;
    }
  }
  else {
    $i++;
  }
  if ($i==$N-1) break;
}
return $A;
}

Score : 600 points

Problem Statement

Snuke has an integer sequence, a, of length N. The i-th element of a (1-indexed) is a_{i}.

He can perform the following operation any number of times:

Operation: Choose integers x and y between 1 and N (inclusive), and add a_x to a_y.

He would like to perform this operation between 0 and 2N times (inclusive) so that a satisfies the condition below. Show one such sequence of operations.
It can be proved that such a sequence of operations always exists under the constraints in this problem.

Condition: a_1 \leq a_2 \leq ... \leq a_{N}

Constraints

2 \leq N \leq 50

-10^{6} \leq a_i \leq 10^{6}

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Let m be the number of operations in your solution. In the first line, print m.
In the i-th of the subsequent m lines, print the numbers x and y chosen in the i-th operation, with a space in between.
The output will be considered correct if m is between 0 and 2N (inclusive) and a satisfies the condition after the m operations.

Sample Input 1

3
-2 5 -1

Sample Output 1

2
2 3
3 3

After the first operation, a = (-2,5,4).

After the second operation, a = (-2,5,8), and the condition is now satisfied.

Sample Input 2

2
-1 -3

Sample Output 2

1
2 1

After the first operation, a = (-4,-3) and the condition is now satisfied.

Sample Input 3

5
0 0 0 0 0

Sample Output 3

0

The condition is satisfied already in the beginning.


<?php
fscanf(STDIN,"%d %d",$n, $k);
$result = 0;
$a = explode(" ", rtrim(fgets(STDIN)));
for($i=0; $i<$n; $i++)
{
    $count[$a[$i]]++;
}
sort($count);
for($i=0; $i<sizeof($count)-$k; $i++)
{
    $result+=$count[$i];
}

echo($result);

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

10 3
5 1 3 2 4 1 1 2 3 4

Sample Output 3

3


<?php
list($n) = ints();
echo isHarshad($n) ? 'Yes' : 'No';

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

function isHarshad($n)
{
    return $n % array_sum(array_map('intval', str_split((string) $n))) === 0;
}

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


<?php
// Your code here!
$N = trim(fgets(STDIN));
$result = 0;
foreach(str_split(strval($N)) as $n){
    $result += intval($n);
}
echo ($result % $N) == 0 ? 'No' : 'Yes';
?>


<?php

fscanf(STDIN, "%d", $n);
for($i = 0; $i < $n; $i++){
    $f[] = array_map('intval', explode(' ', fgets(STDIN)));
}
for($i = 0; $i < $n; $i++){
    $p[] = array_map('intval', explode(' ', fgets(STDIN)));
}
$a = [];
$ans = -100000000;

function calc(){
    global $ans;
    global $a;
    global $n;
    global $f;
    global $p;
    $r = 0;
    for($i = 0; $i < $n; $i++){
        $cnt = 0;
        for($j = 0; $j < count($f[$i]); $j++){
            if($f[$i][$j] == 1 && $a[$j] == 1) $cnt++;
        }
        $r += $p[$i][$cnt];
    }
    $ans = max($ans, $r);
}

function dfs($pos){
    global $a;
    if($pos == 10){
        if(array_sum($a) == 0) return;
        calc();
        return;
    }
    $a[$pos] = 0; dfs($pos + 1);
    $a[$pos] = 1; dfs($pos + 1);
}

dfs(0);
echo $ans;

Score : 300 points

Problem Statement

Joisino is planning to open a shop in a shopping street.

Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.

There are already N stores in the street, numbered 1 through N.

You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.

Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.

Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.

Constraints

1≤N≤100

0≤F_{i,j,k}≤1

For every integer i such that 1≤i≤N, there exists at least one pair (j,k) such that F_{i,j,k}=1.

-10^7≤P_{i,j}≤10^7

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}
:
F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}
P_{1,0} ... P_{1,10}
:
P_{N,0} ... P_{N,10}

Output

Print the maximum possible profit of Joisino's shop.

Sample Input 1

1
1 1 0 1 0 0 0 1 0 1
3 4 5 6 7 8 9 -2 -3 4 -2

Sample Output 1

8

If her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit.

Sample Input 2

2
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1

Sample Output 2

-2

Note that a shop must be open during at least one period, and the profit may be negative.

Sample Input 3

3
1 1 1 1 1 1 0 0 1 1
0 1 0 1 1 1 1 0 1 0
1 0 1 1 0 1 0 1 0 1
-8 6 -2 -8 -8 4 8 7 -6 2 2
-9 2 0 1 7 -5 0 -2 -6 5 5
6 -6 7 -9 6 -5 8 0 -9 -7 -7

Sample Output 3

23


<?php
    fscanf(STDIN, "%d", $n);
    // $n = 1000;
    $count = 0;
    $array = str_split($n);
    for($i = 0; $i < count($array) -1; $i ++){
      if($array[$i] == $array[$i + 1]){
        $count++;
      }
    }
    if($count >= 2){
      echo 'Yes';
    }else{
      echo 'No';
    }
  ?>

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


<?php
fscanf(STDIN, "%d", $n);
    $count = 1;
    $array = str_split($n);
if($array[0] == $array[1] && $array[1] == $array[2]){
	echo 'Yes';
}elseif($array[1] == $array[2] && $array[2] == $array[3]){
	echo 'Yes';
}else{
	echo 'No';

}
?>


Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


<?php
  # めんどくせぇしどうせPHPなので果敢にevalを使う（evil <- PHPのevalが意味不明な挙動しやがるので諦めて愚直にやる
  $number = str_split(trim(fgets(STDIN)));
  $op = ["+", "-"];
  foreach($op as $x){
    foreach($op as $y){
      foreach($op as $z){
        // PHPのevalがなんか意味不明な挙動をしやがる
        // # $ans = "$number[0] $x $number[1] $y $number[2] $z $number[3]";
        // # echo "ans = $ans, sum = " . eval($ans) . PHP_EOL;
        // echo eval("return " . $ans . ";") . PHP_EOL;
        // ので諦める
        $ans = 0;
        if($x=="+"){
          $ans += intVal($number[0]) + intVal($number[1]);
        }else{
          $ans += intVal($number[0]) - intVal($number[1]);
        }
        if($y=="+"){
          $ans += intVal($number[2]);
        }else{
          $ans -= intVal($number[2]);
        }
        if($y=="+"){
          $ans += intVal($number[3]);
        }else{
          $ans -= intVal($number[3]);
        }
        if($ans == 7){
          echo "${number[0]}${x}${number[1]}${y}${number[2]}${z}${number[3]}" . "=7" . PHP_EOL;
          exit();
        }
      }
    }
  }
?>

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


<?php
// 桁数が増えてもOK！$valueと$sumを任意で変更する
$value = fgets(STDIN);
// 計算後の値
$sum = 7;
// 桁数(なぜか入力に改行(?)があるのでtrimする)
$length = strlen(trim($value));
// +,-の数
$signCount = $length - 1;
// 左から順番に1桁ずつ配列に格納
$array = array();
for ($i = 0; $i < $length; $i ++) {
    $array[] = (int) substr($value, $i, 1);
}

// 全探索(記号種類2(+,-)を計算回数分べき乗する)
// 1ならプラス、0ならマイナス
for ($i = 0; $i < pow(2, $signCount); $i ++) {
    $result = $array[0];
    $resultStr = $array[0];
    // 桁の右から比較する
    for ($j = $signCount - 1; $j >= 0; $j --) {
        // 左シフトした値と比べる
        if (($i & (1 << $j)) === (1 << $j)) {
            $result += $array[$signCount - $j];
            $resultStr .= "+" . $array[$signCount - $j];
        } else {
            $result -= $array[$signCount - $j];
            $resultStr .= "-" . $array[$signCount - $j];
        }
    }
    if ($result === $sum) {
        echo $resultStr . "=" . $sum;
        exit(0);
    }
}


<?php
  $c1 = trim(fgets(STDIN));
  $c2 = trim(fgets(STDIN));

  $rev_c1 = strrev($c1);
  $rev_c2 = strrev($c2);
  if($c1 == $rev_c2 && $c2 == $rev_c1){
    echo "YES\n";
  }else{
    echo "NO\n";
  }

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


<?php
  list($r) = explode(" ", trim(fgets(STDIN)));
  list($g) = explode(" ", trim(fgets(STDIN)));

  echo $r+2*($g-$r);

  echo PHP_EOL;

Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


<?php
fscanf(STDIN, "%d", $r);
fscanf(STDIN, "%d", $g);
echo $g - $r + $g;


<?php

$n = trim(fgets(STDIN));
$k = trim(fgets(STDIN));

$num = 1;
for($i=0; $i<$n; $i++){
  if($num > $k) {
    $num += $k;
  } else {
    $num += $num;
  }
}

echo $num;

?>

Score : 200 points

Problem Statement

Square1001 has seen an electric bulletin board displaying the integer 1.
He can perform the following operations A and B to change this value:

Operation A: The displayed value is doubled.

Operation B: The displayed value increases by K.

Square1001 needs to perform these operations N times in total.
Find the minimum possible value displayed in the board after N operations.

Constraints

1 \leq N, K \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
K

Output

Print the minimum possible value displayed in the board after N operations.

Sample Input 1

4
3

Sample Output 1

10

The value will be minimized when the operations are performed in the following order: A, A, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 7 → 10.

Sample Input 2

10
10

Sample Output 2

76

The value will be minimized when the operations are performed in the following order: A, A, A, A, B, B, B, B, B, B.

In this case, the value will change as follows: 1 → 2 → 4 → 8 → 16 → 26 → 36 → 46 → 56 → 66 → 76.

By the way, this contest is AtCoder Beginner Contest 076.


<?php
error_reporting(0);
$N = trim(fgets(STDIN));
$K = trim(fgets(STDIN));
//$A = explode(" ",trim(fgets(STDIN)));
//list($N,$M) = explode(" ",trim(fgets(STDIN)));

$n=1;
for($i=0;$i<$N;$i++) {
  $wk = $n * 2;
  if ($wk > $K + $n) {
    $n+=$K;
  }
  else {
    $n=$n*2;
  }
}
printf("%d\n",$n);


Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100