Score : 400 points

Problem Statement

Snuke has a large collection of cards. Each card has an integer between 1 and N, inclusive, written on it.
He has A_i cards with an integer i.

Two cards can form a pair if the absolute value of the difference of the integers written on them is at most 1.

Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.

Constraints

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)

All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum number of pairs that Snuke can create.

Sample Input 1

4
4
0
3
2

Sample Output 1

4

For example, Snuke can create the following four pairs: (1,1),(1,1),(3,4),(3,4).

Sample Input 2

8
2
0
1
6
0
8
2
1

Sample Output 2

9


<?php
$inputs = str_split(file_get_contents("php://stdin"));
echo array_reduce($inputs, function($prev, $cur) {
  if ($cur == "B") {
    return strlen($prev) == 0 ? $prev : substr($prev, 0, -1);
  } else if ($cur == "0" || $cur == "1"){
    return $prev.$cur;
  }
}, "");

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

Score : 100 points

Problem Statement

There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N-th child. How many candies will be necessary in total?

Constraints

1≦N≦100

Input

The input is given from Standard Input in the following format:

N

Output

Print the necessary number of candies in total.

Sample Input 1

3

Sample Output 1

6

The answer is 1+2+3=6.

Sample Input 2

10

Sample Output 2

55

The sum of the integers from 1 to 10 is 55.

Sample Input 3

1

Sample Output 3

1

Only one child. The answer is 1 in this case.

Submitted source-code file size, in bytes.

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

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


<?php
fscanf(STDIN, "%s", $initial_string);
$length = strlen($initial_string);
$result = '';
$current_count = 0;
for($i = 0; $i < $length; $i++) {
    $char = $initial_string[$i];
    if ($char == '0') {
        $result[] = $char;
    } else if ($char == 'B') {
        $current_count = count($result);
        $pre_index = $current_count - 1 ;
        unset($result[$pre_index]);
        $result = array_values($result);
        echo "(" . implode("", $result) . ")";
    } else if ($char == '1') {
        $result[] = $char;
    }
}
echo implode("", $result);
?>


<?php
  fscanf(STDIN,"%s",$s);
$re = "";
 for($i=0;$i<strlen($s);$i++){
   switch ($s[$i]) {
     case "0":
       $re .= "0";
       break;
     case "1":
       $re .= "1";
       break;
     case "B":
       if(!empty($re)){
         $re = substr($re, 0, -1);
       }
       break;
   }
 }

echo $re."\n";

Score : 200 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys several times. You are given a string s, which is a record of his keystrokes in order. In this string, the letter 0 stands for the 0 key, the letter 1 stands for the 1 key and the letter B stands for the backspace key. What string is displayed in the editor now?

Constraints

1 ≦ |s| ≦ 10 (|s| denotes the length of s)

s consists of the letters 0, 1 and B.

The correct answer is not an empty string.

Input

The input is given from Standard Input in the following format:

s

Output

Print the string displayed in the editor in the end.

Sample Input 1

01B0

Sample Output 1

00

Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

Sample Input 2

0BB1

Sample Output 2

1

Each time the key is pressed, the string in the editor will change as follows: 0, (empty), (empty), 1.


<?php
$input = fgets(STDIN);
$arr = str_split(strval($input));
$str = "";
foreach($arr as $value) {
  if ($value === "B") {
    if (strlen($str) > 1) {
      $str = substr($str, 0, strlen($str) - 1);
    } else {
      $str = "";
    }
  } else {
    $str .= $value;
  }
}
echo $str


<?php

while ($stdin = fgets(STDIN)) {
    $line[] = trim($stdin);
}
foreach ($line as $key => $value) {
    $data[] = explode(' ', $value);
}

//foreach ($data as &$line) {
//    $line = array_map(function($val) { return (int)$val; }, $line);
//}

$s = $data[0][0];
$length = strlen($s);

$unbalance = false;
$unbalancePoint = null;
for ($i = 0; $i < $length - 1; $i++) {
var_dump(substr($s, $i, 1) . '/' . substr($s, $i+1, 1));
    if (substr($s, $i, 1) === substr($s, $i+1, 1)) {
        $unbalance = true;
        $unbalancePoint = [$i+1, $i+2];
    }
}

if (!$unbalance) {
    for ($i = 0; $i < $length - 2; $i++) {
        $threestr = substr($s, $i, 3);
        $one = substr($threestr, 0, 1);
        $two = substr($threestr, 1, 1);
        $three = substr($threestr, 2, 1);
        if ($one === $two || $one === $three || $two === $three) {
            $unbalance = true;
            $unbalancePoint = [$i+1, $i+3];
        }
    }
}

$ans = $unbalance ? $unbalancePoint[0] . ' ' . $unbalancePoint[1] : '-1 -1';

printf('%s', $ans);

Score : 400 points

Problem Statement

Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.

You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.

Constraints

2 ≦ |s| ≦ 10^5

s consists of lowercase letters.

Partial Score

200 points will be awarded for passing the test set satisfying 2 ≦ N ≦ 100.

Input

The input is given from Standard Input in the following format:

s

Output

If there exists no unbalanced substring of s, print -1 -1.

If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print a b. If there exists more than one such substring, any of them will be accepted.

Sample Input 1

needed

Sample Output 1

2 5

The string s_2 s_3 s_4 s_5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.

Sample Input 2

atcoder

Sample Output 2

-1 -1

The string atcoder contains no unbalanced substring.


<?php
/*
    Problem URL : http://agc002.contest.atcoder.jp/tasks/agc002_c

    Score  :
    Result :
    Time   : ms
    Memory : KB
 */

fscanf(STDIN, "%d %d", $N, $L);
$a = explode(" ", trim(fgets(STDIN)));
$p = null;
$mmax = 0;

for ($i = 0; $i < $N - 1; $i++) {
    $tmp = $a[$i] + $a[$i+1];
    $mmax = max($mmax, $tmp);
    if ($tmp >= $L) {
        $p = $i;
    }
}

if ($mmax >= $L) {
    echo "Possible\n";
} else {
    exit("Impossible\n");
}

$arr1 = $p > 0 ? range(1, $p) : array();
$arr2 = range($N-1, $p+1);
echo implode(PHP_EOL, array_merge($arr1, $arr2)) . PHP_EOL;

Problem Statement

We have N pieces of ropes, numbered 1 through N. The length of piece i is a_i.

At first, for each i (1≤i≤N-1), piece i and piece i+1 are tied at the ends, forming one long rope with N-1 knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:

Choose a (connected) rope with a total length of at least L, then untie one of its knots.

Is it possible to untie all of the N-1 knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.

Constraints

2≤N≤10^5

1≤L≤10^9

1≤a_i≤10^9

All input values are integers.

Input

The input is given from Standard Input in the following format:

N L
a_1 a_2 ... a_n

Output

If it is not possible to untie all of the N-1 knots, print Impossible.

If it is possible to untie all of the knots, print Possible, then print another N-1 lines that describe a possible order to untie the knots. The j-th of those N-1 lines should contain the index of the knot that is untied in the j-th operation. Here, the index of the knot connecting piece i and piece i+1 is i.

If there is more than one solution, output any.

Sample Input 1

3 50
30 20 10

Sample Output 1

Possible
2
1

If the knot 1 is untied first, the knot 2 will become impossible to untie.

Sample Input 2

2 21
10 10

Sample Output 2

Impossible

Sample Input 3

5 50
10 20 30 40 50

Sample Output 3

Possible
1
2
3
4

Another example of a possible solution is 3, 4, 1, 2.


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

    $count1 = 0;
    $count2 = 0;

    foreach($list as $val){
        if($val == '7' ){
            $count1++;
        }elseif($val=='5'){
            $count2++;
        }
    }

    if($count1 == 1 && $count2 == 2){
        echo "YES\n";
    }else{
        echo "NO\n";
    }

Score : 100 points

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

Constraints

1≦A,B,C≦10

Input

The input is given from Standard Input in the following format:

A B C

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.

Sample Input 1

5 5 7

Sample Output 1

YES

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.

Sample Input 2

7 7 5

Sample Output 2

NO


<?php

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

$d = explode(' ', rtrim(fgets(STDIN),PHP_EOL));

//var_dump($d);exit;
//$flg = false;
for ($i=$n;$i<10000;$i++) {

    $str = str_split($i);

    foreach ($str as $word) {
        if (in_array($word, $d)){
            continue 2;
        }
    }

    echo $i.PHP_EOL;
    exit;

}

Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K.

She is shopping, and now paying at the cashier.
Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

1 ≦ N < 10000

1 ≦ K < 10

0 ≦ D_1 < D_2 < … < D_K≦9

\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}

Input

The input is given from Standard Input in the following format:

N K
D_1 D_2 … D_K

Output

Print the amount of money that Iroha will hand to the cashier.

Sample Input 1

1000 8
1 3 4 5 6 7 8 9

Sample Output 1

2000

She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

Sample Input 2

9999 1
0

Sample Output 2

9999


Score : 500 points

Problem Statement

Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light.

Three mirrors of length N are set so that they form an equilateral triangle.
Let the vertices of the triangle be a, b and c.

Inside the triangle, the rifle is placed at the point p on segment ab such that ap = X.
(The size of the rifle is negligible.)
Now, the rifle is about to fire a ray of Mysterious Light in the direction of bc.

The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light.
There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror!
When the ray comes back to the rifle, the ray will be absorbed.

The following image shows the ray's trajectory where N = 5 and X = 2.

It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of N and X.
Find the total length of the ray's trajectory.

Constraints

2≦N≦10^{12}

1≦X≦N-1

N and X are integers.

Partial Points

300 points will be awarded for passing the test set satisfying N≦1000.

Another 200 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N X

Output

Print the total length of the ray's trajectory.

Sample Input 1

5 2

Sample Output 1

12

Refer to the image in the Problem Statement section.
The total length of the trajectory is 2+3+2+2+1+1+1 = 12.


<?php
    /*
        Problem URL : http://agc001.contest.atcoder.jp/tasks/agc001_b

        Score  :
        Result :
        Time   : ms
        Memory : KB
     */

    ini_set('display_errors', 'Off');
    ini_set('error_reporting', E_ALL & ~E_NOTICE);

    fscanf(STDIN, "%d %d", $N, $X);

    $a = $X;
    $b = 0;
    $direction = "b";
    $answer = 0;

    // そこを通ったか
    $m[$a][$b] = -1;
    // 航跡
    $n[$a][$b] = "";

    while($m[$X][0] < 0) {
        switch($direction) {
            case "a":
                $a++;
                $answer++;
                break;

            case "b":
                $b++;
                $answer++;
                break;

            case "-ab":
                $a--;
                $b--;
                $answer++;
                break;

        }
        // echo "{$direction} a:{$a} b:{$b} ans:{$answer}\n";

        // インクリメント
        $m[$a][$b]++;
        // 右の壁にぶつかった
        if ($a == $b) {
            $direction = "a";
        }
        // 下の壁にぶつかった
        elseif ($a == $N) {
            $direction = "-ab";
        }
        // 左の壁にぶつかった
        elseif ($b == 0) {
            $direction = "b";
        }
        // すでに航跡がある
        elseif ($m[$a][$b] > 0) {
            // echo "aru {$n[$a][$b]}\n\n";
            if ($direction == "b" && $n[$a][$b] == "a") {
                $direction = "-ab";
            }
            elseif ($direction == "b" && $n[$a][$b] == "-ab") {
                $direction = "a";
            }
            elseif ($direction == "-ab" && $n[$a][$b] == "b") {
                $direction = "a";
            }
            elseif ($direction == "-ab" && $n[$a][$b] == "a") {
                $direction = "b";
            }
            elseif ($direction == "a" && $n[$a][$b] == "b") {
                $direction = "-ab";
            }
            elseif ($direction == "a" && $n[$a][$b] == "-ab") {
                $direction = "b";
            }
        }
        $n[$a][$b] = $direction;
    }
    // echo "direct:{$direction}\n";
    // echo "ENDE\n";

    echo $answer . "\n";