<?php
	fscanf(STDIN, "%d", $n);
	for ($i=0; $i<$n; $i++) {
      fscanf(STDIN, "%s %d", $s, $p);
	  $states[] = $s;
      $points[] = $p;
      $stores[] = $i + 1;
    }

	# states を昇順、 points を降順でソート、 store も一緒に並べる。
	array_multisort($states, SORT_ASC, $points, SORT_DESC, $stores);

	for ($i=0; $i<$n; $i++) {
      echo $stores[$i] . "\n";
    }
?>

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


<?php
list($N, $M) = explode(" ", trim(fgets(STDIN)));
for($k = 0; $k < $M; $k++){
    $map[$k] = explode(" ", trim(fgets(STDIN)));
    array_shift($map[$k]);
}
$odd = explode(" ", trim(fgets(STDIN)));

$count = 0;
for($i = 0; $i < 2**$N; $i++){
    $flags = sprintf("%0".$N."d", decbin($i));
    for($k = 0; $k < $M; $k++){
        $on = $odd[$k];
        foreach($map[$k] as $switch){
            $on += $flags[$switch - 1];
        }
        if($on % 2 == 1)break;
        if($k == $M-1){
            $count++;
        }
    }
}
echo $count;

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

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8

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.


Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.


Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


<?php

# A - erris Wheel

fscanf(STDIN, "%d %d", $A, $B);
if ($A <= 5 ) {
    echo 0;
} elseif ($A >= 13) {
    echo $B;
} else {
    echo $B / 2;
}


<?php

$input_line = fgets(STDIN);
$array = explode(" ", $input_line);
$r = $array[0];
$d = $array[1];
$xi = $array[2];

$x;
$tmp;
for ($i = 0; $i < 10; $i ++) {
  $x = $r * $xi - $d;
  $xi = $x;

  echo $x . "\n";
}

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


<?php
list($n,$m)=explode(" ",trim(fgets(STDIN)));
$l=1;
$r=$n;
$i=0;
while($i<$m){
  list($a,$b)=explode(" ",trim(fgets(STDIN)));
  $l=max($l,$a);
  $r=min($r,$b);
  ++$i;
}
echo max($r-$l+1,0);

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


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

$n = (int)$input[0];
$m = (int)$input[1];

$input = explode(' ', trim(fgets(STDIN)));
$as = [];
foreach ($input as $v) {
	$as[] = (int)$v;
}
sort($as);

$cmds = [];
for ($i=0; $i < $m; $i++) {
	$input = explode(' ', trim(fgets(STDIN)));
	if (isset($cmds[(int)$input[1]])) {
		$cmds[(int)$input[1]] += (int)$input[0];
	}else{
		$cmds[(int)$input[1]] = (int)$input[0];
	}
}
krsort($cmds);

// var_dump($cmds);
$sum = 0;
$n_idx = 0;
$end_flg = false;
foreach ($cmds as $c => $b) {
	$limit = $n_idx + $b;
	for ($j=$n_idx; $j < $limit; $j++) {
		echo $c . '-' . $j . PHP_EOL;
		++$n_idx;
		if ($as[$j] < $c) {
			$sum += $c;
		}else{
			$end_flg = true;
			$sum += $as[$j];
			break;
		}
	}
	if ($end_flg) {
		break;
	}
}
for ($i=$n_idx; $i < $n; $i++) {
	$sum += $as[$i];
}
echo $sum;

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

The output may not fit into a 32-bit integer type.


<?php
fscanf(STDIN, '%d %d', $n, $m);
$cards = explode(' ', trim(fgets(STDIN)));
for($i = 0; $i < $m; $i++) {
    fscanf(STDIN, '%d %d', $b, $c);
    for($j = 0; $j < $b; $j++) {
        sort($cards);
        $min = min($cards);
        if($min < $c) $cards[$j] = $c;
    }
}
echo array_sum($cards), PHP_EOL;


<?php

fscanf(STDIN, "%d %d", $card_num, $operation_times);

$str_cards = trim(fgets(STDIN));
$card = explode(" ",$str_cards);

sort($card);
$new = [];
for($i=0;$i<$operation_times;++$i) {
    fscanf(STDIN, "%d %d", $max_rewrite, $rewrite_number);
    //$rewrite_number = (string)$rewrite_number;
    if(!isset($new[$rewrite_number])){
        $new[$rewrite_number] = $max_rewrite;
    }else{
        $new[$rewrite_number] += $max_rewrite;
    }
}
krsort($new);
$t =0;

foreach ($new as $key => $value){
    $end  =$t + $value;
    for($i = $t;$i<$end; ++$i){
        if($card[$i] < $key){
            $card[$i] = $key;
        }else{
            var_dump($card);
            echo array_sum($card);
            exit;
        }
    }
    $t +=$key;

}
echo array_sum($card);


<?php

fscanf(STDIN, "%d %d", $card_num, $operation_times);

$str_cards = trim(fgets(STDIN));
$card = explode(" ",$str_cards);

sort($card);
for($i=0;$i<$operation_times;++$i){
    fscanf(STDIN, "%d %d", $max_rewrite, $rewrite_number);

    for($k=0;$k<$max_rewrite;++$k){
        if($card[$k] < $rewrite_number){
            $card[$k] = $rewrite_number;
        }else{
            break;
        }
    }
    sort($card);
}
echo array_sum($card);


Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


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

  $cp = substr($s, $k-1,1);
  $sstr = strtolower($cp);

  echo substr_replace(&s, $sstr, $k-1, 1);


<?php

$int = explode(" ",trim(fgets(STDIN)));
$char = str_split(trim(fgets(STDIN)));
$result;
for($i=0;$i<$int[0];$i++){
  if($i === $int[1]){
    $result .= mb_strtolower($char[$i]);
  }
  $result = $result.$char[$i];
}

echo $result;

?>


Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


<?php
fscanf(STDIN, "%s", $S);
$AA = intval(substr($S, 0, 2));
$BB = intval(substr($S, 2, 2));
$CC = ($AA >= 1 && $AA <= 12) * 2 + ($BB >= 1 && $BB <= 12);
switch ($CC) {
    case 0:
        echo "NA";
        break;
    case 1:
        echo "YYMM";
        break;
    case 2:
        echo "MMYY";
        break;
    case 3:
        echo "AMBIGUOUS";
        break;
}


<?php

function checkMonth($n){
    $n = (int)$n;
    if($n > 0 && $n < 13){
        return(true);
    }   else{
        return(false);
    }
}
$n = 0;
# 整数の入力
fscanf(STDIN, "%s", $n);

$array = str_split($n,2);

if(checkMonth($array[0]) && checkMonth($array[1])){
    echo "AMBIGUOUS";
}elseif(checkMonth($array[0])){
    echo "MMYY";
}elseif(checkMonth($array[1])){
    echo "YYMM";
}else{
    echo "NA";
}


Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


<?php
$n = 0;

# 整数の入力
fscanf(STDIN, "%d", $n);
$array = [];
$tow = $kyori = 0;
$ans[1] = 0;
for($i=1;$i<$n;$i++){
    fscanf(STDIN, "%d %d %d", $one, $tow, $kyori);
    $array[$one][$tow] = $kyori;
}
ksort($array);
foreach($array as $start => $new){
    ksort($new);
    foreach($new as $end => $length){
        if(isset($ans[$start])){
            if(($length % 2) == 0){
                $ans[$end] = $ans[$start];
            }else{
                $ans[$end] = abs($ans[$start]-1);
            }
        }elseif(isset($ans[$end])){
            if(($length % 2) == 0){
                $ans[$start] = $ans[$end];
            }else{
                $ans[$start] = abs($ans[$end]-1);
            }
        }else{
            $inokori[$start][$end]=$length;
        }
    }
}
if(isset($inokori)) {
    ksort($inokori);

    foreach ($inokori as $start => $new) {
        ksort($new);
        foreach ($new as $end => $length) {
            if (isset($ans[$start])) {
                if (($length % 2) == 0) {
                    $ans[$end] = $ans[$start];
                } else {
                    $ans[$end] = abs($ans[$start] - 1);
                }
            } elseif (isset($ans[$end])) {
                if (($length % 2) == 0) {
                    $ans[$start] = $ans[$end];
                } else {
                    $ans[$start] = abs($ans[$end] - 1);
                }
            }
        }
    }
}
ksort($ans);

echo implode(PHP_EOL,$ans);

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

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

Sample Output 2

1
0
1
0
1


Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


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

$S=substr($s,$k,1);
$result="";
//echo($S);
for($i = 0; $i < strlen($s); $i++) {

  if($s[$i]==$S){
    $result.=$S;
  }else{
    $result.="*";
  }

}
echo($result);
?>

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


<?php

/*
N個の石が一列に並んでおり、すべての石は白か黒で塗られています。
石の状態は長さNの文字列Sで表され、Sのi文字目が . のとき左からi個目の石が白であり、
# のとき左からi個目の石が黒であることを表します。

高橋君は、0個以上の石の色を黒または白に変更し、
黒い石のすぐ右に白い石があるような箇所がないようにしたいです。
色を変更する必要のある石の個数の最小値を求めてください。
*/

fscanf(STDIN, "%d", $N);
$S = str_split(trim(fgets(STDIN)));

# 全部黒にする場合
$all_black = 0;
for ( $i = 0 ;$i < $N ;$i++ ) {
    if ($S[$i] == '.') {
        $all_black++;
    }
}

# 全部白にする場合
$all_white = 0;
for ( $i = 0 ;$i < $N ;$i++ ) {
    if ($S[$i] == '#') {
        $all_white++;
    }
}

# 連続した黒白の石を交互にした配列を作る
$count_white = 0;
$count_black = 0;
$sum = [];
for ( $i = 0 ;$i < $N ;$i++ ) {
    if ($S[$i] == '#') {
        $count_black++;
        if ($count_white > 0) {
            $sum[] = $count_white;
            $count_white = 0;
        }
    }
    if ($S[$i] == '.') {
        $count_white++;
        if ($count_black > 0) {
            $sum[] = $count_black;
            $count_black = 0;
        }
    }
}

# 黒から始まっている場合、先頭を白始まりにする
if ($S[0] == '#') {
    array_unshift($sum, 0);
}

$index = count($sum);
$ans = [];
for ( $i = 0 ;$i < $index; $i+=2 ) {
    $ans[] = count_white($sum, $index) + count_black($sum, $index);
}
$ans[] = $count_black;
$ans[] = $count_white;

echo min($ans);

function count_white($sum, $cut) {
    $count_white = 0;
    for ( $i = 0 ; $i < $cut ; $i+=2 ) {
        if ($i % 2 == 0) {
            $count_white+= $sum[$i];
        }
    }
    return $count_white;
}

function count_black($sum, $cut) {
    $N = count($sum);
    $count_black = 0;
    for ( $i = $cut ;$i < $N ;$i+=2 ) {
        if ($i % 2 == 0) {
            $count_black+= $sum[$i];
        }
    }
    return $count_black;
}

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

1 \leq N \leq 2\times 10^5

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


<?php

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

  $shape = false;
  $count = 0;
  for ($i=0; $i<$n; $i++) {
    if (substr($s, $i, 1) == "#") {
        $shape = true;
    }
    if (substr($s, $i, 1) == "." && $shape == true) {
        $count++;
    }
}
if($shape == true &&  $count > 0) {
    $shapeC = substr_count($s, "#");
    $dotC = substr_count($s, ".");
    if($shapeC < $dotC){
        echo $shapeC;
    }else {
        echo $dotC;
    }
}else {
    echo "0";
}
  ?>


<?php
  $N = trim(fgets(STDIN));
  $S = trim(fgets(STDIN));
  $white = 0;
  $black = 0;

  for($i = 0; $i < $N; $i++){
    if($S[$i] == "."){
      $white++;
    }else{
      $black++;
    }
  }

if($S[$N-1] == "#"){
  $black-;
}

  if($white > $black){
    echo $black."\n";
  }else{
    echo $white."\n";
  }

?>


<?php
class MyTest {

    protected $input;

    public function __construct() {
        // # 複数行
        while (!empty($input = fgets(STDIN))) {
            $this->input[] = trim($input);
        }
    }

    public function run() {
        $n = $this->input[0];
        $str = $this->input[1];
        $count = 0;
        $flg = strpos($str, '#.') !== false;
        if ($flg) {
            $str = preg_replace('/#+$/', '', $str);
            $str = preg_replace('/^\./', '', $str);
            $a = substr_count($str, '#');
            $b = substr_count($str, '.');
            $count = $a < $b ? $a : $b;
        }
        echo $count;

    }

}

$test = new MyTest();
$test->run();


Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


Score : 200 points

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

The height of the i-th mountain from the west is H_i.

You can certainly see the ocean from the inn at the top of the westmost mountain.

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.

From how many of these N inns can you see the ocean?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq H_i \leq 100

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the number of inns from which you can see the ocean.

Sample Input 1

4
6 5 6 8

Sample Output 1

3

You can see the ocean from the first, third and fourth inns from the west.

Sample Input 2

5
4 5 3 5 4

Sample Output 2

3

Sample Input 3

5
9 5 6 8 4

Sample Output 3

1


<?php
fscanf(STDIN, "%d", $N);
$H = explode(" ", trim(fgets(STDIN)));
$mmax = 0;
$ans = 0;
for ($i = 0; $i < $N; $i++) {
    if ($mmax <= $H[$i]) {
        $mmax = $H[$i];
        $ans++;
    }
}
echo $ans;


<?php

$n = (int)trim(fgets(STDIN));
$input = explode(' ', trim(fgets(STDIN)));

$hs = [];
foreach ($input as $v) {
	$hs[] = (int)$v;
}
$max = 0;
$cnt = 0;
foreach ($hs as $h) {
	$max = max($h, $max);
	if ($h >= $max) {
		++$cnt;
	}
}
echo $cnt;


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

$max = 0;
$c = 0;
foreach($array as $h){
    if($max <= $h)$c++;
    $max = max($h, $max);
}
echo $c;


<?php
  $s = strval(trim(fgets(STDIN)));
  $array = str_split($s);
  $prev = "default";
  $change = -1;
  $length = count($array);
  foreach($array as $n){
    if($change === -1){
      $change++;
      $prev = $n;
    }elseif($n === $prev){
      $change++;
    }else{
      $prev = $n;
    }
  }
  if($change > $length){
    $change = $length - $change;
  }
  echo $change;

Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


<?php
fscanf(STDIN, "%s",$str);
$arr=str_split($str);

$check=false;
$cnt=0;
$start_no=0;

while(!$check){
	$chk_cnt=0;
	for($i=$start_no;$i<count($arr);$i++){
		if($arr[$i]==$arr[$i+1]){
			$cnt++;
			$chk_cnt=1;
			if($arr[$i+1]==0){
				$arr[$i+1]=1;
			}else{
				$arr[$i+1]=0;
			}
			if($i==(count($arr)-1)){
				$check=true;
			}
			$start_no=$i+1;
			break;
		}
	}
	if($chk_cnt==0){
		$check=true;
	}
}

echo $cnt;

?>


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

// とりあえず60個用意
const REF_LEN = 60;
const REF_0 = '010101010101010101010101010101010101010101010101010101010101';
const REF_1 = '101010101010101010101010101010101010101010101010101010101010';

$count0 = 0;
$count1 = 0;

$len = strlen($s);
$pos = 0;

while ($len > $pos) {
    $sub = substr($s, $pos, REF_LEN);
    $binSub = bindec($sub);
    $l = strlen($sub);
    $pos = $pos + $l;

    // 比較用の文字列を取得
    if ($l < REF_LEN) {
        $bin0 = bindec(substr(REF_0, 0, $l));
        $bin1 = bindec(substr(REF_1, 0, $l));
    } else {
        $sub0 = bindec(REF_0);
        $sub1 = bindec(REF_1);
    }

    // バイナリで比較
    $xor0 = decbin($binSub ^ $bin0);
    $xor1 = decbin($binSub ^ $bin1);

    $count0 = $count0 + substr_count($xor0, '1');
    $count1 = $count1 + substr_count($xor1, '1');
}

printf("%d\n", min([$count0, $count1]));


<?php
$in = [];
fscanf(STDIN, "%d", $in[]);
fscanf(STDIN, "%d", $in[]);
fscanf(STDIN, "%d", $in[]);
fscanf(STDIN, "%d", $in[]);
fscanf(STDIN, "%d", $in[]);
fscanf(STDIN, "%d", $k);

$ans = "Yay!";

for ($i = 0; $i < count($in) - 1; $i++) {
    for ($j = $i + 1; $j < count($in); $j++) {
        if ($in[$j] - $in[$i] > $k) {
            $ans = ":(";
            break;
        }
    }
}

echo $ans;

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.


for ($i = 0; $i < 5; $i++) {
  $xs[] = trim(fgets(STDIN));
}

fscanf(STDIN, '%d', $k);

echo max($xs) - min($xs) > $k ? ':(' : 'Yay!', PHP_EOL;


<?php
$result = 0;

while (!feof(STDIN) ) {
  $arg[] = trim(fgets(STDIN));
}

foreach ($arg as $minutes) {
	$trim[] = mb_substr(strval($minutes), -1);
}

$min_key = min(array_keys($trim));

foreach ($arg as $key => $value) {
	if ($key != $minkey) {
		$value = ceil(($value/10))*10;
	}
	$result = $value + $result;
}

echo $result;

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


<?php

while ($line = fgets(STDIN)) {
   $stdin[] = trim($line);
}
#$v = str_split($stdin[0]);
#$v = explode(" ", $stdin[0]);

$total = $stdin[0];

unset($stdin[0]);
$min = min($stdin);

$ans = ceil($total / $min) + 4;

echo $ans . PHP_EOL;

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

Note that the input or output may not fit into a 32-bit integer type.


<?php

$inputs = [];

for ($i = 0; $i < 6; $i++) {
    fscanf(STDIN, "%d", $inputs[$i]);
}
$number = array_shift($inputs);

$plus = (int)floor($number / min($inputs));
$min = 5 + $plus;

echo $min;


Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


<?php
fscanf(STDIN, "%s %s %s", $A, $B,$C);

if ($A == $B && $A == $C) {
	echo 'Yes';
}
else
{
	echo 'No';
}


<?php
$array = explode(" ", trim(fgets(STDIN)));
echo min($array) == max($array) ? "Yes" : "No";


<?php

$n = (int)trim(fgets(STDIN));
$s = trim(fgets(STDIN));

$red = 0;
$blue = 0;
for ($i=0; $i < $n; $i++) {
	if ($s[$i] == 'R') {
		++$red;
	}else{
		++$blue;
	}
}
if ($red > $blue) {
	echo 'Yes';
}else{
	echo "No";
}

Score : 200 points

Problem Statement

There are N people numbered 1 to N. Each person wears a red hat or a blue hat.

You are given a string s representing the colors of the people. Person i wears a red hat if s_i is R, and a blue hat if s_i is B.

Determine if there are more people wearing a red hat than people wearing a blue hat.

Constraints

1 \leq N \leq 100

|s| = N

s_i is R or B.

Input

Input is given from Standard Input in the following format:

N
s

Output

If there are more people wearing a red hat than there are people wearing a blue hat, print Yes; otherwise, print No.

Sample Input 1

4
RRBR

Sample Output 1

Yes

There are three people wearing a red hat, and one person wearing a blue hat.

Since there are more people wearing a red hat than people wearing a blue hat, the answer is Yes.

Sample Input 2

4
BRBR

Sample Output 2

No

There are two people wearing a red hat, and two people wearing a blue hat.

Since there are as many people wearing a red hat as people wearing a blue hat, the answer is No.


Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


<?php

$input = explode(' ', trim(fgets(STDIN)));
$n = (int)$input[0];
$q = (int)$input[1];

$s = trim(fgets(STDIN));

$qs = [];
for ($j=1; $j <= $q; $j++) {
	$input = explode(' ', trim(fgets(STDIN)));
	$t = $input[0];
	$d = $input[1];
	$qs[] = [
		't' => $t,
		'd' => $d,
	];
}

$left = 0;
$right = $n;
$l_idx = -1;
while ($left < $right) {
	$mid = (int)floor(($left+$right)/2);
	if (toLeft($mid)) {
		$left = $mid + 1;
		$l_idx = max($l_idx, $mid);
	}else{
		$right = $mid;
	}
}
$left = 0;
$right = $n;
$r_idx = $n;
while ($left < $right) {
	$mid = (int)floor(($left+$right)/2);
	if (toRight($mid)) {
		$right = $mid;
		$r_idx = min($r_idx, $mid);
	}else{
		$left = $mid + 1;
	}
}
// echo 'l: ' . $l_idx . PHP_EOL;
// echo 'r: ' . $r_idx . PHP_EOL;
if ($l_idx > $r_idx) {
	echo 0;
}else{
	echo $n - ($l_idx+1 + ($n - $r_idx));
}

function toLeft($p){
	global $qs;
	global $s;
	global $n;
	$res = false;
	for ($i=0; $i < count($qs); $i++) {
		$tmp_s = $s[$p];
		$q = $qs[$i];
		if ($q['t'] === $tmp_s) {
			if ($q['d'] === 'L') {
				--$p;
			}else{
				++$p;
			}
		}
		if ($p < 0 || $p > $n-1) {
			if ($p < 0) {
				$res = true;
			}
			break;
		}
	}
	return $res;
}
function toRight($p){
	global $qs;
	global $s;
	global $n;
	$res = false;
	for ($i=0; $i < count($qs); $i++) {
		$tmp_s = $s[$p];
		$q = $qs[$i];
		if ($q['t'] === $tmp_s) {
			if ($q['d'] === 'L') {
				--$p;
			}else{
				++$p;
			}
		}
		if ($p < 0 || $p > $n-1) {
			if ($p > $n-1) {
				$res = true;
			}
			break;
		}
	}
	return $res;
}


<?php

$inputs = [];
while (($item = fgets(STDIN)) !== false) {
    $inputs[] = $item;
}

$map = [
    'A' => 'T',
    'T' => 'A',
    'C' => 'G',
    'G' => 'C',
];
$x = trim(array_shift($inputs));
echo $map[$x];

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


<?php
$s = trim(fgets(STDIN));
$arr = str_split($s);

$count = 0;
$check = [];
for($i=0; $i<count($arr); $i++){
    if($arr[$i] == 'A' || $arr[$i] == 'C' || $arr[$i] == 'G' || $arr[$i] == 'T'){
        $count++;
    }else{
        $check[] = $count;
        $count = 0;
    }
}
echo max($check);
?>

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

S is a string of length between 1 and 10 (inclusive).

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


<?php
$data = trim(fgets(STDIN));
$checkList = array( "A"=>true,"T"=>true, "C"=>true,"G"=>true );

$max = 0;
$dataList = str_split($data);
$count = count( $dataList );
for( $i=0; $i<$count; $i++ )
{
  $tmp = 0;
  for( $j=$i; $j<$count; $j++ )
  {
    if( isset($checkList[$dataList[$j]]) )
    { $tmp++; }
    else
    {
      if( $max < $tmp )
      {
        $max = $tmp;
        break;
      }
    }

  }
}

echo $max;
?>


<?php
fscanf(STDIN, "%s", $s);
$length = strlen($s);

for($i = 0; $i < $length; $i++){
    for($j = $i; $j <= $length; $j++){
        $judge = false;
        $s_kari = substr($s, $i, $j);
        //echo $s_kari;
        for($k = 0; $k < strlen($s_kari); $k++){
            if(!preg_match('/[ACGT]/',$s_kari[$k])){
                $judge = true;
            }
        }
        if(!$judge){
            $sarr[] = $s_kari;
            //echo "[".$i.", ".$j."]:".$s_kari."=OK!";
        }
    }
}

$count = count($sarr);
$carr = [];
for($i = 0; $i < $count; $i++){
    $carr[] = strlen($sarr[$i]);
}

echo max($carr);

?>


<?php

$S = trim(fgets(STDIN));
//$S  = "A B C D E";
$pieces = explode(" ", $S);
$a = 0;
foreach ($pieces as $key) {
	if($key == "A" || $key == "C" || $key == "G" || $key == "T"){
		$a = $a + 1;
	}
}

echo $a;

?>


Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


<?php
fscanf(STDIN, "%s %s", $N,$Q);
fscanf(STDIN, "%s", $s);

for ($i = 0; $i<$Q; $i++) {
    fscanf(STDIN, "%s %s",$l,$r);
    $t = substr($s, $l - 1, $r - $l + 1);

    test($t);
}

function test($a) {
    $a = preg_split('//', $a, -1, PREG_SPLIT_NO_EMPTY);
        $ss = 0;
    $is_A_flag = 0;
    foreach ($a as $s) {
        if ($s == 'A'){
            $is_A_flag = 1;
        } else if ($s == 'C' && $is_A_flag){
            $ss++;
            $is_A_flag = 0;
        } else {
            $is_A_flag = 0;
        }
    }

    echo $ss;
}


<?php
list($n) = ints();
$b = ints();
while (count($b) > 0) {
    for ($i = count($b) - 1; $i >= 0; --$i) {
        if ($i + 1 === $b[$i]) {
            $a[] = $b[$i];
            unset($b[$i]);
            $b = array_values($b);
            continue 2;
        }
    }
    exit('-1');
}
echo implode(PHP_EOL, array_reverse($a));
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}

Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1


<?php

$memo = [];

$back = function($b) use (&$back, $memo) {
    if (isset($memo[implode('', $b)])) {
        return $memo[implode('', $b)];
    }
    if (count($b) === 1) {
        if ($b[0] === 1) {
            return [[1], true];
        } else {
            return [[], false];
        }
    }
    $dame_flag = true;
    for ($i = 0; $i < count($b); $i++) {
        if ($b[$i] === $i + 1) {
            $dame_flag = false;
        }
    }
    if ($dame_flag) {
        $memo[implode('', $b)] = [[], false];
        return [[], false];
    }
    for ($i = count($b) - 1; $i >= 0; $i--) {
        if ($b[$i] === $i + 1) {
            $b_clone = $b;
            array_splice($b_clone, $i, 1);
            list($procedure, $is_able) = $back($b_clone);
            if ($is_able) {
                $memo[implode('', $b)] = [array_merge($procedure, [$i + 1]), true];
                return [array_merge($procedure, [$i + 1]), true];
            }
        }
    }
    $memo[implode('', $b)] = [[], false];
    return [[], false];
};

$N = (int)trim(fgets(STDIN));
$b = array_map(function($str) {
    return (int)$str;
}, explode(' ', trim(fgets(STDIN))));
list($procedure, $is_able) = $back($b);
if ($is_able) {
    for ($i = 0; $i < count($procedure); $i++) {
        echo "{$procedure[$i]}\n";
    }
} else {
    echo "-1\n";
}


<?php
$tmp = explode(' ', trim(fgets(STDIN)));
$H = (int)$tmp[0];
$W = (int)$tmp[1];
$tmp = explode(' ', trim(fgets(STDIN)));
$h = (int)$tmp[0];
$w = (int)$tmp[1];
echo (($H-$h)*($W-$w))."\n";

Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


Score : 200 points

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

Additionally, you are given integers B_1, B_2, ..., B_M and C.

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

Among the N codes, find the number of codes that correctly solve this problem.

Constraints

All values in input are integers.

1 \leq N, M \leq 20

-100 \leq A_{ij} \leq 100

-100 \leq B_i \leq 100

-100 \leq C \leq 100

Input

Input is given from Standard Input in the following format:

N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM}

Output

Print the number of codes among the given N codes that correctly solve this problem.

Sample Input 1

2 3 -10
1 2 3
3 2 1
1 2 2

Sample Output 1

1

Only the second code correctly solves this problem, as follows:

Since 3 \times 1 + 2 \times 2 + 1 \times 3 + (-10) = 0 \leq 0, the first code does not solve this problem.

1 \times 1 + 2 \times 2 + 2 \times 3 + (-10) = 1 > 0, the second code solves this problem.

Sample Input 2

5 2 -4
-2 5
100 41
100 40
-3 0
-6 -2
18 -13

Sample Output 2

2

Sample Input 3

3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Sample Output 3

0

All of them are Wrong Answer. Except yours.


<?php /*atcoder.php*/

$at = new Atcoder();
$at->main();
unset($at);

class Atcoder{
	private function arrayIn(){
		$buf = explode(" ", fgets(STDIN));
		return $this->killNewLine($buf);
	}
	private function killNewLine($buf = null){
		$buf = str_replace("\r\n", "", $buf);
		$buf = str_replace("\n", "", $buf);
		$buf = str_replace("\r", "", $buf);
		return $buf;
	}

	public function main(){
		$s = $this->arrayIn();

		$n = $s[0];
		$m = $s[1];
		$c = $s[2];

		$i = array();

		$i = $this->arrayIn();

		$ans = 0;
		$buf = 0;
		$j = array();
		for($k = 0; $k < $n; $k++){
			$j = $this->arrayIn();
			for($ii = 0; $ii < $m; $ii++){
				$buf += ($i[$ii] * $j[$ii]);
			}
			$buf += $c;
			if(0 < $buf){
				$ans++;
			}
			$buf = 0;
		}

		echo $ans . "\n";

	}
}
?>


<?php

$input = explode(' ', trim(fgets(STDIN)));
$n = (int)$input[0];
$m = (int)$input[1];
$c = (int)$input[2];

$b_array = [];
$input = explode(' ', trim(fgets(STDIN)));
foreach ($input as $v) {
	$b_array[] = (int) $v;
}

$count = 0;
for ($i=0; $i < $n; $i++) {
	$input = explode(' ', trim(fgets(STDIN)));
	$sum = $c;
	for ($j=0; $j < $m; $j++) {
		$sum += (int)$input[$j] * $b_array[$j];
	}
	if ($sum > 0) {
		++$count;
	}
}
echo $count;


Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


<?php
$stdin = trim(fgets(STDIN));
$len = strlen($stdin);

for ($i = 0; $i < $len; ++$i) {
    $arr[] = substr($stdin, $i, 1);
}

$vals = array_count_values($arr);

$cnt0 = (isset($vals[0])) ? $vals[0] : 0;
$cnt1 = (isset($vals[1])) ? $vals[1] : 0;

$cnt = ($cnt0 > $cnt1) ? $cnt1 * 2 : $cnt0 * 2;
echo $cnt;

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


<?php
fscanf(STDIN,'%s',$S);
$arr = str_split($S);
$count= count($arr);
while (true) {
  // code...

for ($i=0; $i <= count($arr) - 2 ; $i++) {
  // code...
if(isset($arr[$i])){
  if(($arr[$i]=='0'&&$arr[$i+1]=='1')||($arr[$i]=='1'&&$arr[$i+1]=='0')){
    unset($arr[$i]);
    unset($arr[$i+1]);
    }
}
}
$arrB == $arr;
$arrC = array_values($arrB);
if($arrC==$arrB){
  $arr == $arrC;
  break;
}else{
  $arr == $arrC;
}
}
$lastcount = count($arr);
echo $count- $lastcount;

 ?>


<?php
$baseDate = 20190430;
$date = fgets(STDIN);
$date = str_replace("/","",$date);
if($date <= $baseDate){
  echo "Heisei";
}else{
  echo "TBD";
}

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


<?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();
    }
    public function nextString() {
        return (string)$this->next();
    }
}

$sc = new Scanner();
$s = $sc->nextString();

$target = '2019/04/30';

if(strtotime($s) <= strtotime($target)){
  echo 'Heisei';
} else {
  echo 'TBD';
}


<?php
$N = trim(fgets(STDIN));
$ans = 0;
for($i=1; $i<=$N; $i++) {
    list($X, $Y) = preg_split("/\s/i",trim(fgets(STDIN)));
    if($Y=="BTC") {
        $ans+= $X * 380000.0;
    }else{
        $ans+= $X;
    }
}
echo $ans;

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


<?php
$N = (int)fgets(STDIN);
$total = 0;
for ($i = 0; $i < $N; $i++) {
    list($x, $u) = explode(' ', trim(fgets(STDIN)));
    if ($u == 'BTC') {
        $total += 380000.0 * $x;
    } else {
        $total += $x;
    }
}

echo $total . "\n";


<?php

fscanf(STDIN, "%d %d %d %d", $N, $A, $B, $C);
$L = [];
for ($i = 0; $i < $N; ++$i) {
    $L[] = intval(trim(fgets(STDIN)));
}

function dfs($n, $a, $b, $c) {
    global $N, $A, $B, $C, $L;

    if ($n == $N) {
        if (count($a) === 0 || count($b) === 0 || count($c) === 0) return PHP_INT_MAX;
        return sum($A, $a) + sum($B, $b) + sum($C, $c);
    }

    $min = PHP_INT_MAX;
    $d1 = dfs($n + 1, $a, $b, $c);
    $d2 = dfs($n + 1, array_merge($a, [$L[$n]]), $b, $c);
    $d3 = dfs($n + 1, $a, array_merge($b, [$L[$n]]), $c);
    $d4 = dfs($n + 1, $a, $b, array_merge($c, [$L[$n]]));

    return min($d1, $d2, $d3, $d4);
}

function sum($target, $arr) {
    $sum = 0;

    if (count($arr) > 1) {
        $sum = (count($arr) - 1) * 10;
    }

    $sum += abs($target - array_sum($arr));

    return $sum;
}

echo dfs(0, [], [], []);

Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


<?php
  $input = fgets(STDIN);
  $input = explode(" ",$input);
  if($input[1]%$input[0] == 0){
    echo $input[0]+$input[1];
  }else{
    echo $input[0]-$input[1];
  }
?>

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


<?php /*atcoder.php*/

$at = new Atcoder();
$at->main();
unset($at);

class Atcoder{
	private function arrayIn(){
		$buf = explode(" ", fgets(STDIN));
		return $this->killNewLine($buf);
	}
	private function killNewLine($buf = null){
		$buf = str_replace("\r\n", "", $buf);
		$buf = str_replace("\n", "", $buf);
		$buf = str_replace("\r", "", $buf);
		return $buf;
	}

	public function main(){
		$s = $this->arrayIn();

		if(0 == $s[1] % $s[0]){
			$ans = $s[0] + $s[1];
		}else{
			$ans = $s[1] - $s[0];
		}

		echo $ans . "\n";

	}
}
?>


Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654


<?php
    fscanf(STDIN,"%d %d",$N,$K);
    $a=$N/2+1;
    if($K<=$a){
       printf("YES\n");
    }else{
       printf("NO\n");
    }
  ?>

Score : 100 points

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

Constraints

1\leq N,K\leq 100

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If we can choose K integers as above, print YES; otherwise, print NO.

Sample Input 1

3 2

Sample Output 1

YES

We can choose 1 and 3.

Sample Input 2

5 5

Sample Output 2

NO

Sample Input 3

31 10

Sample Output 3

YES

Sample Input 4

10 90

Sample Output 4

NO


<?php

$input = trim(fgets(STDIN));

list($k, $a, $b) = explode(" ", $input);

$biscuits = 1;
$money = 0;

if ($a >= $b) {
    // 交換するより叩いたほうが儲かる
    $biscuits += $k;
    echo $biscuits;
    exit;
}

if ( $a + 1 + 1 - 1 > $k ) {
    // 交換枚数に達しないので叩くだけで終わる
    $biscuits += $k;
    echo $biscuits;
    exit;
}

// 交換枚数まで叩いて消費
$k -= ($a - 1);
$biscuits = $a;

// 残り回数で何回交換できるか
$change_num = floor($k / 2);
$amari = $k % 2;

$biscuits += (($b - $a) * $change_num);
$biscuits += $amari;

echo $biscuits;

Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.
He will perform the following operations exactly K times in total, in the order he likes:

Hit his pocket, which magically increases the number of biscuits by one.

Exchange A biscuits to 1 yen.

Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

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

K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

Hit his pocket. Now he has 2 biscuits and 0 yen.

Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.

Hit his pocket. Now he has 1 biscuits and 1 yen.

Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099


<?php
fscanf(STDIN, "%d %d", $t,$x);
echo $t / $x;
?>

Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


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


<?php
$n=trim(fgets(STDIN));
$l=explode(" ",trim(fgets(STDIN)));
$m=max($l);
$s=array_sum($l);
echo $s>$m*2?'Yes':'No';

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


<?php
$n = fgets(STDIN);
$s[] = fgets(STDIN);
$s[] = fgets(STDIN);
$s[] = fgets(STDIN);
$sum = 0;
for($i = 0; $i < $n; $i++){
  $max = max(array_count_values([$s[0][$i], $s[1][$i], $s[2][$i]]));
  $sum += 3-$max;
}
echo $sum;

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

1 \leq N \leq 100

Each of the strings A, B and C is a string of length N.

Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.

Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = west、B = east、C = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

Change the second character in A to a. A is now wast.

Change the first character in B to w. B is now wast.

Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.

Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13


<?php
fscanf(STDIN, "%d %d %d", $ab, $bc, $ca);
echo ($ab * $bc) / 2;

Score : 100 points

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

It is guaranteed that the area of the triangle ABC is an integer.

Constraints

1 \leq |AB|,|BC|,|CA| \leq 100

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


<?php
  fscanf(STDIN, $ab, $bc, $ca);
  echo $ab * $bc / 2;

  ?>


<?php
$N = fgets(STDIN);
$H = explode(" ",(fgets(STDIN)));
$result = 0;

foreach($H as $key => $v){
  if($key==0){
    if($H[$key+1]<$v){
      $result+=$v-$H[1];
    }
  }elseif($key==$N-1){
      $result+=$v;
  }else{
    if($H[$key+1]<=$v){
      $result+=$v-$H[$key+1];
    }
  }
}

print($result);

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


Score : 100 points

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

0 \leq N_1, N_2, N_3, N_4 \leq 9

N_1, N_2, N_3 and N_4 are integers.

Input

Input is given from Standard Input in the following format:

N_1 N_2 N_3 N_4

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.

Sample Input 1

1 7 9 4

Sample Output 1

YES

We can get 1974 by swapping N_2 and N_3.

Sample Input 2

1 9 7 4

Sample Output 2

YES

We already have 1974 before doing anything.

Sample Input 3

1 2 9 1

Sample Output 3

NO

Sample Input 4

4 9 0 8

Sample Output 4

NO


<?php
$x = str_split(trim(fgets(STDIN)));
$l = count($x);
for($i = 0;$i<8;$i++){
$a=$x;
$b =array_splice($a,$i,$l-7);
$imp = implode($a);
if($imp == 'keyence'){
echo 'YES';
exit;

}
}
echo 'NO';

Score : 200 points

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

Constraints

The length of S is between 7 and 100 (inclusive).

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a KEYENCE string, print YES; otherwise, print NO.

Sample Input 1

keyofscience

Sample Output 1

YES

keyence is an abbreviation of key of science.

Sample Input 2

mpyszsbznf

Sample Output 2

NO

Sample Input 3

ashlfyha

Sample Output 3

NO

Sample Input 4

keyence

Sample Output 4

YES


<?php
fscanf(STDIN, "%d",$n);
$a=explode(" ", rtrim(fgets(STDIN)));
$b=explode(" ", rtrim(fgets(STDIN)));
$over=0;
$under=0;
$result=0;
for ($i=0; $i < $n; $i++)
{
	if($a[$i]>=$b[$i])
		$over+=$a[$i]-$b[$i];
	else
	{
		$under+=$b[$i]-$a[$i];
		$result++;
	}
	$err[$i]=$a[$i]-$b[$i];
}
$i=0;
rsort($err);
while($under>0)
{
	$under-=$err[$i];
	$result++;
	$i++;
	if($i==$n)
	{
		$result=-1;
		break;
	}

}

printf("%d\n",$result);

Score : 400 points

Problem Statement

A university student, Takahashi, has to take N examinations and pass all of them.
Currently, his readiness for the i-th examination is A_{i}, and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i-th examination.

Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.

For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions:

The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal.

For every i, B_i \leq C_i holds.

If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

A_i and B_i 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

Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions.
If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Sample Input 1

3
2 3 5
3 4 1

Sample Output 1

3

(A_1, A_2, A_3) = (2, 3, 5) and (B_1, B_2, B_3) = (3, 4, 1). If nothing is done, he cannot pass the first and second exams.
The minimum possible number of indices i such that A_i and C_i are different, 3, is achieved when:

(C_1, C_2, C_3) = (3, 5, 2)

Sample Input 2

3
2 3 3
2 2 1

Sample Output 2

0

In this case, he has to do nothing in order to pass all the exams.

Sample Input 3

3
17 7 1
25 6 14

Sample Output 3

-1

In this case, no matter what is done, he cannot pass all the exams.

Sample Input 4

12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212

Sample Output 4

5


<?php
$N = trim(fgets(STDIN));
$A = array_map('intval', explode(' ', trim(fgets(STDIN))));
$B = array_map('intval', explode(' ', trim(fgets(STDIN))));

$suff = 0;
$ovr = 0;
$S = 0;
$arr = [];
for ($i = 0; $i < $N; $i++) {
    if ($A[$i] < $B[$i]) {
        $suff++;
        $S += $B[$i] - $A[$i];
    } else {
        $arr[] = $A[$i] - $B[$i];
        $ovr += $A[$i] - $B[$i];
    }
}
if ($suff === 0) {
    $ans = 0;
} else if ($ovr < $S) {
    $ans = -1;
} else {
    sort($arr);
    $ans = $suff;
    while ($S > 0) {
        $ans++;
        $tmp = array_pop($arr);
        $S -= $tmp;
    }
}
echo $ans;


<?php
$n = intval(fgets(STDIN));
$A = array_map("intval", explode(" ", trim(fgets(STDIN))));
$B = array_map("intval", explode(" ", trim(fgets(STDIN))));
if(array_sum($A) < array_sum($B)){
    echo "-1\n";
}else{
    $count = 0;
    $minus = 0;
    for($i = 0; $i < $n; $i++){
        if($A[$i] < $B[$i]){
            $minus += $B[$i] - $A[$i];
            $count++;
        }else{
            $plus[] = $A[$i] - $B[$i];
        }
    }
    sort($plus);
    while($minus > 0){
        $best = array_pop($plus);
        $minus -= $best;
        $count++;
    }
    echo $count;
}


<?php

$bord = (int)fgets(STDIN);
$height = (int)fgets(STDIN);
$width = (int)fgets(STDIN);

echo ($bord - $height + 1) * ($bord - $width + 1);

?>

Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

1 \leq H, W \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.

Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

### ...
### ###
... ###

Here, # represents a square covered by the notice, and . represents a square not covered.

Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8


<?php
$n = trim(fgets(STDIN));
list($m, $M) = explode(" ", trim(fgets(STDIN)));
$a = explode(" ", trim(fgets(STDIN)));
$c[0] = $c[1] = $c[2] = 0;
foreach($a as $b){
  if($b <= $m){
    $c[0]++;
  }elseif($b <= $M){
    $c[1]++;
  }else{
    $c[2]++;
  }
}
echo min($c);

Score : 200 points

Problem Statement

You have written N problems to hold programming contests.
The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests.
At most how many contests can be held?

Constraints

3 \leq N \leq 100

1 \leq P_i \leq 20 (1 \leq i \leq N)

1 \leq A < B < 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.

Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.

Sample Input 3

3
5 6
5 6 10

Sample Output 3

1


<?PHP
  $n=trim(fgets(STDIN));
for($i=0;$i<$n;++$i){
	fscanf(STDIN,"%d %d %d",$x,$y,$z);
	$a[$i]=$x;
	$b[$i]=$y;
	$c[$i]=$z;
}
		$a_history=[];
		$b_history=[];
		$c_history=[];
foreach (range(0, $n-1) as $i) {

	if($i=0){
		$a_history[$i]=$a[$i];
		$b_history[$i]=$b[$i];
		$c_history[$i]=$c[$i];
		continue;
	}

	$a_history[$i]=$a[$i]+($b_history[$i-1]>$c_history[$i-1] ? $b_history[$i-1] : $c_history[$i-1]);
	$b_history[$i]=$b[$i]+($a_history[$i-1]>$c_history[$i-1] ? $a_history[$i-1] : $c_history[$i-1]);
	$c_history[$i]=$c[$i]+($b_history[$i-1]>$a_history[$i-1] ? $b_history[$i-1] : $a_history[$i-1]);

}
echo max(end($a_history),end($b_history),end($c_history));

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.


<?php
$n = trim(fgets(STDIN));
$choice =[];
$answer = 0;
for($i=0; $i<$n; $i++){
	$happiness = fgets(STDIN);
	$array = explode(" ", $happiness);
	arsort($array);
	if($i===0){
		$choice[] = array_search(current($array), $array);
		$answer += current($array);
	}else{
		if($choice[$i-1] === array_search(current($array), $array)){
			$answer += next($array);
			$choice[] = array_search(next($array), $array);
		}else{
			$answer += current($array);
			$choice[] = array_search(current($array), $array);
		}
	}
}

echo $answer;
echo "\n";

?>


<?php
$n = int();
$A = ints();
//o($n);
//o($A);
$sum = 0;
$As[] = 0;
foreach($A as $a){
    $sum+=$a;
    $As[] = $sum;
}
for($r = 0; $r < $n; $r++){
    $dpr = &$dp[$r];
    $dpr[$r] = 0;
    for($l = $r-1; $l >= 0; $l--){
        $sum = $As[$r+1]-$As[$l];
        $min = 10**18;
        for($m = $l; $m < $r; $m++){
            $tmp = $dp[$m][$l] + $dpr[$m+1];
            if($min > $tmp){
                $min = $tmp;
            }
        }
        $dpr[$l] = $sum+$min;
    }
}
echo $dp[$n-1][0];

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

Score : 100 points

Problem Statement

There are N slimes lining up in a row.
Initially, the i-th slime from the left has a size of a_i.

Taro is trying to combine all the slimes into a larger slime.
He will perform the following operation repeatedly until there is only one slime:

Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes.

Find the minimum possible total cost incurred.

Constraints

All values in input are integers.

2 \leq N \leq 400

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 20 30 40

Sample Output 1

190

Taro should do as follows (slimes being combined are shown in bold):

(10, 20, 30, 40) → (30, 30, 40)

(30, 30, 40) → (60, 40)

(60, 40) → (100)

Sample Input 2

5
10 10 10 10 10

Sample Output 2

120

Taro should do, for example, as follows:

(10, 10, 10, 10, 10) → (20, 10, 10, 10)

(20, 10, 10, 10) → (20, 20, 10)

(20, 20, 10) → (20, 30)

(20, 30) → (50)

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

6
7 6 8 6 1 1

Sample Output 4

68

Taro should do, for example, as follows:

(7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)

(7, 6, 8, 6, 2) → (7, 6, 8, 8)

(7, 6, 8, 8) → (13, 8, 8)

(13, 8, 8) → (13, 16)

(13, 16) → (29)


<?php
$code = <<<'CODE'
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int add(int x,int y){return(x+=y)<mod?x:x-mod;}
int sub(int x,int y){return(x-=y)>= 0?x:x+mod;}
int dp[1<<21];
int n,a[21][21];
int main(){
  scanf("%d",&n);
  for(int i=0;i<n;++i){
    for(int j=0;j<n;++j){
      scanf("%d",&a[i][j]);
    }
  }
  dp[0]=1;
  for(int b=1;b<(1<<n);++b){
    int bc=__builtin_popcount(b)-1;
    for(int i=0;i<n;++i){
      if((b>>i&1)&&a[bc][i]){
        dp[b]=add(dp[b],dp[b^(1<<i)]);
      }
    }
  }
  printf("%d\n",dp[(1<<n)-1]);
}
CODE;
file_put_contents("A.cpp", $code);
while($line = fgets(STDIN)){
  $lines[] = $line;
}
file_put_contents("in", implode("\n", $lines));
exec("g++  A.cpp -o a.out");
exec("./a.out < in", $a, $b);
echo implode("\n", $a);

Score : 100 points

Problem Statement

There are N men and N women, both numbered 1, 2, \ldots, N.

For each i, j (1 \leq i, j \leq N), the compatibility of Man i and Woman j is given as an integer a_{i, j}.
If a_{i, j} = 1, Man i and Woman j are compatible; if a_{i, j} = 0, they are not.

Taro is trying to make N pairs, each consisting of a man and a woman who are compatible.
Here, each man and each woman must belong to exactly one pair.

Find the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Constraints

All values in input are integers.

1 \leq N \leq 21

a_{i, j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of ways in which Taro can make N pairs, modulo 10^9 + 7.

Sample Input 1

3
0 1 1
1 0 1
1 1 1

Sample Output 1

3

There are three ways to make pairs, as follows ((i, j) denotes a pair of Man i and Woman j):

(1, 2), (2, 1), (3, 3)

(1, 2), (2, 3), (3, 1)

(1, 3), (2, 1), (3, 2)

Sample Input 2

4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

Sample Output 2

1

There is one way to make pairs, as follows:

(1, 2), (2, 4), (3, 1), (4, 3)

Sample Input 3

1
0

Sample Output 3

0

Sample Input 4

21
0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1
1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0
0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1
0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0
1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1
0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1
0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0
0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1
1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1
0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1
1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1
0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1
0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0
1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0

Sample Output 4

102515160

Be sure to print the number modulo 10^9 + 7.


<?php
$input = explode(' ', trim(fgets(STDIN)));
$n = (int)$input[0];
$p = (int)$input[1];

$max = floor(sqrt($p));
$as = [];

if ($n === 1) {
	echo $p;
	exit();
}
for ($i=2; $i <= $max; $i++) {
	while ( $p % $i === 0) {
		if (empty($as[$i])) {
			$as[$i] = 1;
		}else{
			$as[$i] += 1;
		}
		$p = floor($p/$i);
	}
}
$min = 1;
foreach ($as as $key => $value) {
	if ($value >= $n) {
		$jo = floor($value / $n);
		$min *= pow($key, $jo);
	}
}
echo $min;

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


<?php
fscanf(STDIN, "%d", $n);
while ($n--) {
	fscanf(STDIN, "%d", $p[]);
}
echo array_sum($p) - max($p) / 2;

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


<?php
$num=trim(fgets(STDIN));
$array=[];
for($i=0;$i<$num;$i++){
 $array[]=trim(fgets(STDIN));
}

$sum=0;

rsort($array);
$sum+=intdiv($array[0],2);

unset($array[0]);
$sum+=array_sum($array);

echo $sum;
?>


<?php

$num = trim(fgets(STDIN));
for($i = 0; $i < $num; $i++){
    $a[] =  trim(fgets(STDIN));
}
echo array_sum($a) - max($a) / 2;


<?php
fscanf(STDIN, "%d %d", $level, $layer);

$dp = array();

$dp[0] = 'P';
$dp[1] = 'BPPPB';

for($i=2; $i<=$level; $i++){
	$dp[$i] =
		'B'
		.$dp[$i-1]
		.'P'
		.$dp[$i-1]
		.'B';
}

echo substr_count($dp[$level], 'P', 0, $layer);

Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

A level-0 burger is a patty.

A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

1 \leq N \leq 50

1 \leq X \leq ( the total number of layers in a level-N burger )

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.