id
stringlengths
6
6
slug_name
stringlengths
1
105
pretty_content
stringlengths
1
27.2k
test_cases
sequencelengths
1
1
p03656
AtCoder Grand Contest 018 - Two Trees
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are two rooted trees, each with <var>N</var> vertices. The vertices of each tree are numbered <var>1</var> through <var>N</var>. In the first tree, the parent of Vertex <var>i</var> is Vertex <var>A_i</var>. Here, <var>A_i=-1</var> if Vertex <var>i</var> is the root of the first tree. In the second tree, the parent of Vertex <var>i</var> is Vertex <var>B_i</var>. Here, <var>B_i=-1</var> if Vertex <var>i</var> is the root of the second tree.</p> <p>Snuke would like to construct an integer sequence of length <var>N</var>, <var>X_1</var> , <var>X_2</var> , <var>...</var> , <var>X_N</var>, that satisfies the following condition:</p> <ul> <li>For each vertex on each tree, let the indices of its descendants including itself be <var>a_1</var> , <var>a_2</var> , <var>...</var>, <var>a_k</var>. Then, <var>abs(X_{a_1} + X_{a_2} + ... + X_{a_k})=1</var> holds.</li> </ul> <p>Determine whether it is possible to construct such a sequence. If the answer is possible, find one such sequence.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>1 \leq A_i \leq N</var>, if Vertex <var>i</var> is not the root in the first tree.</li> <li><var>A_i = -1</var>, if Vertex <var>i</var> is the root in the first tree.</li> <li><var>1 \leq B_i \leq N</var>, if Vertex <var>i</var> is not the root in the second tree.</li> <li><var>B_i = -1</var>, if Vertex <var>i</var> is the root in the second tree.</li> <li>Input corresponds to valid rooted trees.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>..</var> <var>A_N</var> <var>B_1</var> <var>B_2</var> <var>..</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is not possible to construct an integer sequence that satisfies the condition, print <code>IMPOSSIBLE</code>. If it is possible, print <code>POSSIBLE</code> in the first line. Then, in the second line, print <var>X_1</var> , <var>X_2</var> , <var>...</var> , <var>X_N</var>, an integer sequence that satisfies the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 3 4 -1 4 4 4 1 -1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>POSSIBLE 1 -1 -1 3 -1 </pre> <p>For example, the indices of the descendants of Vertex <var>3</var> of the first tree including itself, is <var>3,1,2</var>. It can be seen that the sample output holds <var>abs(X_3+X_1+X_2)=abs((-1)+(1)+(-1))=abs(-1)=1</var>. Similarly, the condition is also satisfied for other vertices.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 -1 5 1 5 1 3 6 5 5 3 -1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>IMPOSSIBLE </pre> <p>In this case, constructing a sequence that satisfies the condition is <code>IMPOSSIBLE</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 2 7 1 2 2 1 -1 4 4 -1 4 7 4 4 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>POSSIBLE 1 2 -1 0 -1 1 0 -1 </pre></section> </div> </span>
[ [ "5\n3 3 4 -1 4\n4 4 1 -1 1\n", "5\n3 3 4 -1 4\n4 4 1 -1 1\n" ] ]
p03657
AtCoder Beginner Contest 067 - Sharing Cookies
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is giving cookies to his three goats.</p> <p>He has two cookie tins. One contains <var>A</var> cookies, and the other contains <var>B</var> cookies. He can thus give <var>A</var> cookies, <var>B</var> cookies or <var>A+B</var> cookies to his goats (he cannot open the tins).</p> <p>Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A,B \leq 100</var></li> <li>Both <var>A</var> and <var>B</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to give cookies so that each of the three goats can have the same number of cookies, print <code>Possible</code>; otherwise, print <code>Impossible</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Possible </pre> <p>If Snuke gives nine cookies, each of the three goats can have three cookies.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Impossible </pre> <p>Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.</p></section> </div> </span>
[ [ "4 5\n", "4 5\n" ] ]
p03658
AtCoder Beginner Contest 067 - Snake Toy
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has <var>N</var> sticks. The length of the <var>i</var>-th stick is <var>l_i</var>.</p> <p>Snuke is making a snake toy by joining <var>K</var> of the sticks together.</p> <p>The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq K \leq N \leq 50</var></li> <li><var>1 \leq l_i \leq 50</var></li> <li><var>l_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>l_1</var> <var>l_2</var> <var>l_3</var> <var>...</var> <var>l_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 1 2 3 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>You can make a toy of length <var>12</var> by joining the sticks of lengths <var>3</var>, <var>4</var> and <var>5</var>, which is the maximum possible length.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>15 14 50 26 27 21 41 7 42 35 7 5 5 36 39 1 45 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>386 </pre></section> </div> </span>
[ [ "5 3\n1 2 3 4 5\n", "5 3\n1 2 3 4 5\n" ] ]
p03659
AtCoder Beginner Contest 067 - Splitting Pile
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke and Raccoon have a heap of <var>N</var> cards. The <var>i</var>-th card from the top has the integer <var>a_i</var> written on it.</p> <p>They will share these cards. First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards. Here, both Snuke and Raccoon have to take at least one card.</p> <p>Let the sum of the integers on Snuke's cards and Raccoon's cards be <var>x</var> and <var>y</var>, respectively. They would like to minimize <var>|x-y|</var>. Find the minimum possible value of <var>|x-y|</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 2 \times 10^5</var></li> <li><var>-10^{9} \leq a_i \leq 10^{9}</var></li> <li><var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 2 3 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, <var>x=10</var>, <var>y=11</var>, and thus <var>|x-y|=1</var>. This is the minimum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 10 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre> <p>Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, <var>x=10</var>, <var>y=-10</var>, and thus <var>|x-y|=20</var>.</p></section> </div> </span>
[ [ "6\n1 2 3 4 5 6\n", "6\n1 2 3 4 5 6\n" ] ]
p03660
AtCoder Beginner Contest 067 - Fennec VS. Snuke
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Fennec and Snuke are playing a board game.</p> <p>On the board, there are <var>N</var> cells numbered <var>1</var> through <var>N</var>, and <var>N-1</var> roads, each connecting two cells. Cell <var>a_i</var> is adjacent to Cell <var>b_i</var> through the <var>i</var>-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.</p> <p>Initially, Cell <var>1</var> is painted black, and Cell <var>N</var> is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:</p> <ul> <li>Fennec: selects an uncolored cell that is adjacent to a <strong>black</strong> cell, and paints it <strong>black</strong>.</li> <li>Snuke: selects an uncolored cell that is adjacent to a <strong>white</strong> cell, and paints it <strong>white</strong>.</li> </ul> <p>A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>1 \leq a_i, b_i \leq N</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Fennec wins, print <code>Fennec</code>; if Snuke wins, print <code>Snuke</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 3 6 1 2 3 1 7 4 5 7 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Fennec </pre> <p>For example, if Fennec first paints Cell <var>2</var> black, she will win regardless of Snuke's moves.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 4 4 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Snuke </pre></section> </div> </span>
[ [ "7\n3 6\n1 2\n3 1\n7 4\n5 7\n1 4\n", "7\n3 6\n1 2\n3 1\n7 4\n5 7\n1 4\n" ] ]
p03661
AtCoder Regular Contest 078 - Splitting Pile
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke and Raccoon have a heap of <var>N</var> cards. The <var>i</var>-th card from the top has the integer <var>a_i</var> written on it.</p> <p>They will share these cards. First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards. Here, both Snuke and Raccoon have to take at least one card.</p> <p>Let the sum of the integers on Snuke's cards and Raccoon's cards be <var>x</var> and <var>y</var>, respectively. They would like to minimize <var>|x-y|</var>. Find the minimum possible value of <var>|x-y|</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 2 \times 10^5</var></li> <li><var>-10^{9} \leq a_i \leq 10^{9}</var></li> <li><var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 2 3 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, <var>x=10</var>, <var>y=11</var>, and thus <var>|x-y|=1</var>. This is the minimum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 10 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre> <p>Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, <var>x=10</var>, <var>y=-10</var>, and thus <var>|x-y|=20</var>.</p></section> </div> </span>
[ [ "6\n1 2 3 4 5 6\n", "6\n1 2 3 4 5 6\n" ] ]
p03662
AtCoder Regular Contest 078 - Fennec VS. Snuke
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Fennec and Snuke are playing a board game.</p> <p>On the board, there are <var>N</var> cells numbered <var>1</var> through <var>N</var>, and <var>N-1</var> roads, each connecting two cells. Cell <var>a_i</var> is adjacent to Cell <var>b_i</var> through the <var>i</var>-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.</p> <p>Initially, Cell <var>1</var> is painted black, and Cell <var>N</var> is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:</p> <ul> <li>Fennec: selects an uncolored cell that is adjacent to a <strong>black</strong> cell, and paints it <strong>black</strong>.</li> <li>Snuke: selects an uncolored cell that is adjacent to a <strong>white</strong> cell, and paints it <strong>white</strong>.</li> </ul> <p>A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>1 \leq a_i, b_i \leq N</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Fennec wins, print <code>Fennec</code>; if Snuke wins, print <code>Snuke</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 3 6 1 2 3 1 7 4 5 7 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Fennec </pre> <p>For example, if Fennec first paints Cell <var>2</var> black, she will win regardless of Snuke's moves.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 4 4 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Snuke </pre></section> </div> </span>
[ [ "7\n3 6\n1 2\n3 1\n7 4\n5 7\n1 4\n", "7\n3 6\n1 2\n3 1\n7 4\n5 7\n1 4\n" ] ]
p03663
AtCoder Regular Contest 078 - Awkward Response
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><font color="red"><strong>This is an interactive task.</strong></font></p> <p>Snuke has a favorite positive integer, <var>N</var>. You can ask him the following type of question at most <var>64</var> times: "Is <var>n</var> your favorite integer?" Identify <var>N</var>.</p> <p>Snuke is twisted, and when asked "Is <var>n</var> your favorite integer?", he answers "Yes" if one of the two conditions below is satisfied, and answers "No" otherwise:</p> <ul> <li>Both <var>n \leq N</var> and <var>str(n) \leq str(N)</var> hold.</li> <li>Both <var>n &gt; N</var> and <var>str(n) &gt; str(N)</var> hold.</li> </ul> <p>Here, <var>str(x)</var> is the decimal representation of <var>x</var> (without leading zeros) as a string. For example, <var>str(123) =</var> <code>123</code> and <var>str(2000)</var> = <code>2000</code>. Strings are compared lexicographically. For example, <code>11111</code> <var>&lt;</var> <code>123</code> and <code>123456789</code> <var>&lt;</var> <code>9</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^{9}</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Input and Output</h3><p>Write your question to Standard Output in the following format:</p> <pre>? <var>n</var> </pre> <p>Here, <var>n</var> must be an integer between <var>1</var> and <var>10^{18}</var> (inclusive).</p> <p>Then, the response to the question shall be given from Standard Input in the following format:</p> <pre><var>ans</var> </pre> <p>Here, <var>ans</var> is either <code>Y</code> or <code>N</code>. <code>Y</code> represents "Yes"; <code>N</code> represents "No".</p> <p>Finally, write your answer in the following format:</p> <pre>! <var>n</var> </pre> <p>Here, <var>n=N</var> must hold.</p> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Judging</h3><ul> <li><font color="red"><strong>After each output, you must flush Standard Output.</strong></font> Otherwise you may get <code>TLE</code>.</li> <li>After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.</li> <li>When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give <code>WA</code>).</li> </ul> </section> </div> <div class="part"> <section> <h3>Sample</h3><p>Below is a sample communication for the case <var>N=123</var>:</p> <table class="table table-bordered"> <thead> <tr> <th>Input</th> <th>Output</th> </tr> </thead> <tbody> <tr> <td></td> <td><code>? 1</code></td> </tr> <tr> <td><code>Y</code></td> <td></td> </tr> <tr> <td></td> <td><code>? 32</code></td> </tr> <tr> <td><code>N</code></td> <td></td> </tr> <tr> <td></td> <td><code>? 1010</code></td> </tr> <tr> <td><code>N</code></td> <td></td> </tr> <tr> <td></td> <td><code>? 999</code></td> </tr> <tr> <td><code>Y</code></td> <td></td> </tr> <tr> <td></td> <td><code>! 123</code></td> </tr> </tbody> </table> <ul> <li>Since <var>1 \leq 123</var> and <var>str(1) \leq str(123)</var>, the first response is "Yes".</li> <li>Since <var>32 \leq 123</var> but <var>str(32) &gt; str(123)</var>, the second response is "No".</li> <li>Since <var>1010 &gt; 123</var> but <var>str(1010) \leq str(123)</var>, the third response is "No".</li> <li>Since <var>999 \geq 123</var> and <var>str(999) &gt; str(123)</var>, the fourth response is "Yes".</li> <li>The program successfully identifies <var>N=123</var> in four questions, and thus passes the case.</li> </ul></section> </div> </div> </span>
[ [ "", "" ] ]
p03664
AtCoder Regular Contest 078 - Mole and Abandoned Mine
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of <var>N</var> vertices numbered <var>1</var> through <var>N</var> and <var>M</var> edges. The <var>i</var>-th edge connects Vertices <var>a_i</var> and <var>b_i</var>, and it costs <var>c_i</var> yen (the currency of Japan) to remove it.</p> <p>Mole would like to remove some of the edges so that there is exactly one path from Vertex <var>1</var> to Vertex <var>N</var> that does not visit the same vertex more than once. Find the minimum budget needed to achieve this.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 15</var></li> <li><var>N-1 \leq M \leq N(N-1)/2</var></li> <li><var>1 \leq a_i, b_i \leq N</var></li> <li><var>1 \leq c_i \leq 10^{6}</var></li> <li>There are neither multiple edges nor self-loops in the given graph.</li> <li>The given graph is connected.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>:</var> <var>a_M</var> <var>b_M</var> <var>c_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 6 1 2 100 3 1 100 2 4 100 4 3 100 1 4 100 3 2 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>200 </pre> <p>By removing the two edges represented by the red dotted lines in the figure below, the objective can be achieved for a cost of <var>200</var> yen.</p> <div style="text-align: center;"> <img alt="45c15676bb602ca3b762561fc014ecd0.png" src="https://atcoder.jp/img/arc078/45c15676bb602ca3b762561fc014ecd0.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>It is possible that there is already only one path from Vertex <var>1</var> to Vertex <var>N</var> in the beginning.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 22 8 13 33418 14 15 55849 7 10 15207 4 6 64328 6 9 86902 15 7 46978 8 14 53526 1 2 8720 14 12 37748 8 3 61543 6 5 32425 4 11 20932 3 12 55123 8 2 45333 9 12 77796 3 9 71922 12 15 70793 2 4 25485 11 6 1436 2 7 81563 7 11 97843 3 1 40491 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>133677 </pre></section> </div> </span>
[ [ "4 6\n1 2 100\n3 1 100\n2 4 100\n4 3 100\n1 4 100\n3 2 100\n", "4 6\n1 2 100\n3 1 100\n2 4 100\n4 3 100\n1 4 100\n3 2 100\n" ] ]
p03665
AtCoder Grand Contest 017 - Biscuits
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> bags of biscuits. The <var>i</var>-th bag contains <var>A_i</var> biscuits.</p> <p>Takaki will select some of these bags and eat all of the biscuits inside. Here, it is also possible to select all or none of the bags.</p> <p>He would like to select bags so that the total number of biscuits inside is congruent to <var>P</var> modulo <var>2</var>. How many such ways to select bags there are?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>P = 0</var> or <var>1</var></li> <li><var>1 \leq A_i \leq 100</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>P</var> <var>A_1</var> <var>A_2</var> ... <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to select bags so that the total number of biscuits inside is congruent to <var>P</var> modulo <var>2</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 0 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are two ways to select bags so that the total number of biscuits inside is congruent to <var>0</var> modulo <var>2</var>:</p> <ul> <li>Select neither bag. The total number of biscuits is <var>0</var>.</li> <li>Select both bags. The total number of biscuits is <var>4</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 50 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 0 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre> <p>Two bags are distinguished even if they contain the same number of biscuits.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>45 1 17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>17592186044416 </pre></section> </div> </span>
[ [ "2 0\n1 3\n", "2 0\n1 3\n" ] ]
p03666
AtCoder Grand Contest 017 - Moderate Differences
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares in a row. The leftmost square contains the integer <var>A</var>, and the rightmost contains the integer <var>B</var>. The other squares are empty.</p> <p>Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:</p> <ul> <li>For any two adjacent squares, the (absolute) difference of the two integers in those squares is between <var>C</var> and <var>D</var> (inclusive).</li> </ul> <p>As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares. Determine whether it is possible to fill the squares under the condition.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq N \leq 500000</var></li> <li><var>0 \leq A \leq 10^9</var></li> <li><var>0 \leq B \leq 10^9</var></li> <li><var>0 \leq C \leq D \leq 10^9</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>C</var> <var>D</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> if it is possible to fill the squares under the condition; print <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 5 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>For example, fill the squares with the following integers: <var>1</var>, <var>-1</var>, <var>3</var>, <var>7</var>, <var>5</var>, from left to right.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 7 6 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>48792 105960835 681218449 90629745 90632170 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>491995 412925347 825318103 59999126 59999339 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>YES </pre></section> </div> </span>
[ [ "5 1 5 2 4\n", "5 1 5 2 4\n" ] ]
p03667
AtCoder Grand Contest 017 - Snuke and Spells
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> balls in a row. Initially, the <var>i</var>-th ball from the left has the integer <var>A_i</var> written on it.</p> <p>When Snuke cast a spell, the following happens:</p> <ul> <li>Let the current number of balls be <var>k</var>. All the balls with <var>k</var> written on them disappear at the same time.</li> </ul> <p>Snuke's objective is to vanish all the balls by casting the spell some number of times. This may not be possible as it is. If that is the case, he would like to modify the integers on the minimum number of balls to make his objective achievable.</p> <p>By the way, the integers on these balls sometimes change by themselves. There will be <var>M</var> such changes. In the <var>j</var>-th change, the integer on the <var>X_j</var>-th ball from the left will change into <var>Y_j</var>.</p> <p>After each change, find the minimum number of modifications of integers on the balls Snuke needs to make if he wishes to achieve his objective before the next change occurs. We will assume that he is quick enough in modifying integers. Here, note that he does not actually perform those necessary modifications and leaves them as they are.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 200000</var></li> <li><var>1 \leq M \leq 200000</var></li> <li><var>1 \leq A_i \leq N</var></li> <li><var>1 \leq X_j \leq N</var></li> <li><var>1 \leq Y_j \leq N</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Subscore</h3><ul> <li>In the test set worth <var>500</var> points, <var>N \leq 200</var> and <var>M \leq 200</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> ... <var>A_N</var> <var>X_1</var> <var>Y_1</var> <var>X_2</var> <var>Y_2</var> : <var>X_M</var> <var>Y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>M</var> lines. The <var>j</var>-th line should contain the minimum necessary number of modifications of integers on the balls to make Snuke's objective achievable.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 1 1 3 4 5 1 2 2 5 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 1 1 </pre> <ul> <li>After the first change, the integers on the balls become <var>2</var>, <var>1</var>, <var>3</var>, <var>4</var>, <var>5</var>, from left to right. Here, all the balls can be vanished by casting the spell five times. Thus, no modification is necessary.</li> <li>After the second change, the integers on the balls become <var>2</var>, <var>5</var>, <var>3</var>, <var>4</var>, <var>5</var>, from left to right. In this case, at least one modification must be made. One optimal solution is to modify the integer on the fifth ball from the left to <var>2</var>, and cast the spell four times.</li> <li>After the third change, the integers on the balls become <var>2</var>, <var>5</var>, <var>3</var>, <var>4</var>, <var>4</var>, from left to right. Also in this case, at least one modification must be made. One optimal solution is to modify the integer on the third ball from the left to <var>2</var>, and cast the spell three times.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 4 4 4 4 4 4 1 3 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 10 8 7 2 9 10 6 6 5 5 4 8 1 6 3 6 2 7 10 9 7 9 9 2 4 8 1 1 8 7 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 0 1 2 2 3 3 3 3 2 </pre></section> </div> </span>
[ [ "5 3\n1 1 3 4 5\n1 2\n2 5\n5 4\n", "5 3\n1 1 3 4 5\n1 2\n2 5\n5 4\n" ] ]
p03668
AtCoder Grand Contest 017 - Game on Tree
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices numbered <var>1, 2, ..., N</var>. The edges of the tree are denoted by <var>(x_i, y_i)</var>.</p> <p>On this tree, Alice and Bob play a game against each other. Starting from Alice, they alternately perform the following operation:</p> <ul> <li>Select an existing edge and remove it from the tree, disconnecting it into two separate connected components. Then, remove the component that does not contain Vertex <var>1</var>.</li> </ul> <p>A player loses the game when he/she is unable to perform the operation. Determine the winner of the game assuming that both players play optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 100000</var></li> <li><var>1 \leq x_i, y_i \leq N</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> : <var>x_{N-1}</var> <var>y_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Alice</code> if Alice wins; print <code>Bob</code> if Bob wins.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 2 3 2 4 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Alice </pre> <p>If Alice first removes the edge connecting Vertices <var>1</var> and <var>2</var>, the tree becomes a single vertex tree containing only Vertex <var>1</var>. Since there is no edge anymore, Bob cannot perform the operation and Alice wins.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 2 2 3 1 4 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Bob </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 2 2 4 5 1 6 3 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Alice </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>7 1 2 3 7 4 6 2 3 2 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Bob </pre></section> </div> </span>
[ [ "5\n1 2\n2 3\n2 4\n4 5\n", "5\n1 2\n2 3\n2 4\n4 5\n" ] ]
p03669
AtCoder Grand Contest 017 - Jigsaw
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> irregular jigsaw pieces. Each piece is composed of three rectangular parts of width <var>1</var> and various heights joined together. More specifically:</p> <ul> <li>The <var>i</var>-th piece is a part of height <var>H</var>, with another part of height <var>A_i</var> joined to the left, and yet another part of height <var>B_i</var> joined to the right, as shown below. Here, the bottom sides of the left and right parts are respectively at <var>C_i</var> and <var>D_i</var> units length above the bottom side of the center part.</li> </ul> <p><img alt="" src="https://atcoder.jp/img/agc017/2b6cd7f4500d3621bc18de407f167522.png"/></p> <p>Snuke is arranging these pieces on a square table of side <var>10^{100}</var>. Here, the following conditions must be held:</p> <ul> <li>All pieces must be put on the table.</li> <li>The entire bottom side of the center part of each piece must touch the front side of the table.</li> <li>The entire bottom side of the non-center parts of each piece must either touch the front side of the table, or touch the top side of a part of some other piece.</li> <li>The pieces must not be rotated or flipped.</li> </ul> <p>Determine whether such an arrangement is possible.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100000</var></li> <li><var>1 \leq H \leq 200</var></li> <li><var>1 \leq A_i \leq H</var></li> <li><var>1 \leq B_i \leq H</var></li> <li><var>0 \leq C_i \leq H - A_i</var></li> <li><var>0 \leq D_i \leq H - B_i</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>H</var> <var>A_1</var> <var>B_1</var> <var>C_1</var> <var>D_1</var> <var>A_2</var> <var>B_2</var> <var>C_2</var> <var>D_2</var> : <var>A_N</var> <var>B_N</var> <var>C_N</var> <var>D_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to arrange the pieces under the conditions, print <code>YES</code>; if it is impossible, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 1 1 0 0 2 2 0 1 3 3 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>The figure below shows a possible arrangement.</p> <p><img alt="" src="https://atcoder.jp/img/agc017/27db184b6924d4cec5077a54b505706a.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 4 1 1 0 3 2 3 2 0 1 2 3 0 2 1 0 0 3 2 0 2 1 1 3 0 3 2 0 0 1 3 2 0 1 1 1 3 2 3 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre></section> </div> </span>
[ [ "3 4\n1 1 0 0\n2 2 0 1\n3 3 1 0\n", "3 4\n1 1 0 0\n2 2 0 1\n3 3 1 0\n" ] ]
p03670
AtCoder Grand Contest 017 - Zigzag
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N(N+1)/2</var> dots arranged to form an equilateral triangle whose sides consist of <var>N</var> dots, as shown below. The <var>j</var>-th dot from the left in the <var>i</var>-th row from the top is denoted by <var>(i, j)</var> (<var>1 \leq i \leq N</var>, <var>1 \leq j \leq i</var>). Also, we will call <var>(i+1, j)</var> immediately lower-left to <var>(i, j)</var>, and <var>(i+1, j+1)</var> immediately lower-right to <var>(i, j)</var>.</p> <p><img alt="" src="https://atcoder.jp/img/agc017/8d354fb1a389a0aa5b64ba93f6ca7801.png"/></p> <p>Takahashi is drawing <var>M</var> polygonal lines <var>L_1, L_2, ..., L_M</var> by connecting these dots. Each <var>L_i</var> starts at <var>(1, 1)</var>, and visits the dot that is immediately lower-left or lower-right to the current dots <var>N-1</var> times. More formally, there exist <var>X_{i,1}, ..., X_{i,N}</var> such that:</p> <ul> <li><var>L_i</var> connects the <var>N</var> points <var>(1, X_{i,1}), (2, X_{i,2}), ..., (N, X_{i,N})</var>, in this order.</li> <li>For each <var>j=1, 2, ..., N-1</var>, either <var>X_{i,j+1} = X_{i,j}</var> or <var>X_{i,j+1} = X_{i,j}+1</var> holds.</li> </ul> <p>Takahashi would like to draw these lines so that no part of <var>L_{i+1}</var> is to the left of <var>L_{i}</var>. That is, for each <var>j=1, 2, ..., N</var>, <var>X_{1,j} \leq X_{2,j} \leq ... \leq X_{M,j}</var> must hold.</p> <p>Additionally, there are <var>K</var> conditions on the shape of the lines that must be followed. The <var>i</var>-th condition is denoted by <var>(A_i, B_i, C_i)</var>, which means:</p> <ul> <li>If <var>C_i=0</var>, <var>L_{A_i}</var> must visit the immediately lower-left dot for the <var>B_i</var>-th move.</li> <li>If <var>C_i=1</var>, <var>L_{A_i}</var> must visit the immediately lower-right dot for the <var>B_i</var>-th move.</li> </ul> <p>That is, <var>X_{A_i, {B_i}+1} = X_{A_i, B_i} + C_i</var> must hold.</p> <p>In how many ways can Takahashi draw <var>M</var> polygonal lines? Find the count modulo <var>1000000007</var>.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>Before submission, it is strongly recommended to measure the execution time of your code using "Custom Test".</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 20</var></li> <li><var>1 \leq M \leq 20</var></li> <li><var>0 \leq K \leq (N-1)M</var></li> <li><var>1 \leq A_i \leq M</var></li> <li><var>1 \leq B_i \leq N-1</var></li> <li><var>C_i = 0</var> or <var>1</var></li> <li>No pair appears more than once as <var>(A_i, B_i)</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>C_1</var> <var>A_2</var> <var>B_2</var> <var>C_2</var> : <var>A_K</var> <var>B_K</var> <var>C_K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways for Takahashi to draw <var>M</var> polygonal lines, modulo <var>1000000007</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 1 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>There are six ways to draw lines, as shown below. Here, red lines represent <var>L_1</var>, and green lines represent <var>L_2</var>.</p> <p><img alt="" src="https://atcoder.jp/img/agc017/75921b6e5a59ab17b4c07ada848b9f14.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 2 1 1 1 2 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 4 2 1 3 1 4 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>172 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>20 20 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>881396682 </pre></section> </div> </span>
[ [ "3 2 1\n1 2 0\n", "3 2 1\n1 2 0\n" ] ]
p03671
AtCoder Beginner Contest 066 - ringring
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is buying a bicycle. The bicycle of his choice does not come with a bell, so he has to buy one separately.</p> <p>He has very high awareness of safety, and decides to buy two bells, one for each hand.</p> <p>The store sells three kinds of bells for the price of <var>a</var>, <var>b</var> and <var>c</var> yen (the currency of Japan), respectively. Find the minimum total price of two different bells.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq a,b,c \leq 10000</var></li> <li><var>a</var>, <var>b</var> and <var>c</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>a</var> <var>b</var> <var>c</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum total price of two different bells.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>700 600 780 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1300 </pre> <ul> <li>Buying a <var>700</var>-yen bell and a <var>600</var>-yen bell costs <var>1300</var> yen.</li> <li>Buying a <var>700</var>-yen bell and a <var>780</var>-yen bell costs <var>1480</var> yen.</li> <li>Buying a <var>600</var>-yen bell and a <var>780</var>-yen bell costs <var>1380</var> yen.</li> </ul> <p>The minimum among these is <var>1300</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10000 10000 10000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20000 </pre> <p>Buying any two bells costs <var>20000</var> yen.</p></section> </div> </span>
[ [ "700 600 780\n", "700 600 780\n" ] ]
p03672
AtCoder Beginner Contest 066 - ss
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will call a string that can be obtained by concatenating two equal strings an <em>even</em> string. For example, <code>xyzxyz</code> and <code>aaaaaa</code> are even, while <code>ababab</code> and <code>xyzxy</code> are not.</p> <p>You are given an even string <var>S</var> consisting of lowercase English letters. Find the length of the longest even string that can be obtained by deleting one or more characters from the end of <var>S</var>. It is guaranteed that such a non-empty string exists for a given input.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq |S| \leq 200</var></li> <li><var>S</var> is an even string consisting of lowercase English letters.</li> <li>There exists a non-empty even string that can be obtained by deleting one or more characters from the end of <var>S</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the length of the longest even string that can be obtained.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>abaababaab </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <ul> <li><code>abaababaab</code> itself is even, but we need to delete at least one character.</li> <li><code>abaababaa</code> is not even.</li> <li><code>abaababa</code> is not even.</li> <li><code>abaabab</code> is not even.</li> <li><code>abaaba</code> is even. Thus, we should print its length, <var>6</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>xxxx </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <ul> <li><code>xxx</code> is not even.</li> <li><code>xx</code> is even.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>abcabcabcabc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6 </pre> <p>The longest even string that can be obtained is <code>abcabc</code>, whose length is <var>6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>akasakaakasakasakaakas </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>14 </pre> <p>The longest even string that can be obtained is <code>akasakaakasaka</code>, whose length is <var>14</var>.</p></section> </div> </span>
[ [ "abaababaab\n", "abaababaab\n" ] ]
p03673
AtCoder Beginner Contest 066 - pushpush
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>n</var>, <var>a_1, ..., a_n</var>. Let us consider performing the following <var>n</var> operations on an empty sequence <var>b</var>.</p> <p>The <var>i</var>-th operation is as follows:</p> <ol> <li>Append <var>a_i</var> to the end of <var>b</var>.</li> <li>Reverse the order of the elements in <var>b</var>.</li> </ol> <p>Find the sequence <var>b</var> obtained after these <var>n</var> operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 2\times 10^5</var></li> <li><var>0 \leq a_i \leq 10^9</var></li> <li><var>n</var> and <var>a_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>n</var> integers in a line with spaces in between. The <var>i</var>-th integer should be <var>b_i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 2 1 3 </pre> <ul> <li>After step 1 of the first operation, <var>b</var> becomes: <var>1</var>.</li> <li>After step 2 of the first operation, <var>b</var> becomes: <var>1</var>.</li> <li>After step 1 of the second operation, <var>b</var> becomes: <var>1, 2</var>.</li> <li>After step 2 of the second operation, <var>b</var> becomes: <var>2, 1</var>.</li> <li>After step 1 of the third operation, <var>b</var> becomes: <var>2, 1, 3</var>.</li> <li>After step 2 of the third operation, <var>b</var> becomes: <var>3, 1, 2</var>.</li> <li>After step 1 of the fourth operation, <var>b</var> becomes: <var>3, 1, 2, 4</var>.</li> <li>After step 2 of the fourth operation, <var>b</var> becomes: <var>4, 2, 1, 3</var>.</li> </ul> <p>Thus, the answer is <code>4 2 1 3</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 1 2 </pre> <p>As shown above in Sample Output 1, <var>b</var> becomes <var>3, 1, 2</var> after step 2 of the third operation. Thus, the answer is <code>3 1 2</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1000000000 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>6 0 6 7 6 7 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 6 6 0 7 7 </pre></section> </div> </span>
[ [ "4\n1 2 3 4\n", "4\n1 2 3 4\n" ] ]
p03674
AtCoder Beginner Contest 066 - 11
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>n+1</var>, <var>a_1,a_2,...,a_{n+1}</var>, which consists of the <var>n</var> integers <var>1,...,n</var>. It is known that each of the <var>n</var> integers <var>1,...,n</var> appears at least once in this sequence.</p> <p>For each integer <var>k=1,...,n+1</var>, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length <var>k</var>, modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><ul> <li> <p>If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.</p> </li> <li> <p>A subsequence of a sequence <var>a</var> with length <var>k</var> is a sequence obtained by selecting <var>k</var> of the elements of <var>a</var> and arranging them without changing their relative order. For example, the sequences <var>1,3,5</var> and <var>1,2,3</var> are subsequences of <var>1,2,3,4,5</var>, while <var>3,1,2</var> and <var>1,10,100</var> are not.</p> </li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 10^5</var></li> <li><var>1 \leq a_i \leq n</var></li> <li>Each of the integers <var>1,...,n</var> appears in the sequence.</li> <li><var>n</var> and <var>a_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> ... <var>a_{n+1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>n+1</var> lines. The <var>k</var>-th line should contain the number of the different subsequences of the given sequence with length <var>k</var>, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 5 4 1 </pre> <p>There are three subsequences with length <var>1</var>: <var>1</var> and <var>2</var> and <var>3</var>.</p> <p>There are five subsequences with length <var>2</var>: <var>1,1</var> and <var>1,2</var> and <var>1,3</var> and <var>2,1</var> and <var>2,3</var>.</p> <p>There are four subsequences with length <var>3</var>: <var>1,1,3</var> and <var>1,2,1</var> and <var>1,2,3</var> and <var>2,1,3</var>.</p> <p>There is one subsequence with length <var>4</var>: <var>1,2,1,3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 1 </pre> <p>There is one subsequence with length <var>1</var>: <var>1</var>.</p> <p>There is one subsequence with length <var>2</var>: <var>1,1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>32 29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>32 525 5453 40919 237336 1107568 4272048 13884156 38567100 92561040 193536720 354817320 573166440 818809200 37158313 166803103 166803103 37158313 818809200 573166440 354817320 193536720 92561040 38567100 13884156 4272048 1107568 237336 40920 5456 528 33 1 </pre> <p>Be sure to print the numbers modulo <var>10^9+7</var>.</p></section> </div> </span>
[ [ "3\n1 2 1 3\n", "3\n1 2 1 3\n" ] ]
p03675
AtCoder Regular Contest 077 - pushpush
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>n</var>, <var>a_1, ..., a_n</var>. Let us consider performing the following <var>n</var> operations on an empty sequence <var>b</var>.</p> <p>The <var>i</var>-th operation is as follows:</p> <ol> <li>Append <var>a_i</var> to the end of <var>b</var>.</li> <li>Reverse the order of the elements in <var>b</var>.</li> </ol> <p>Find the sequence <var>b</var> obtained after these <var>n</var> operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 2\times 10^5</var></li> <li><var>0 \leq a_i \leq 10^9</var></li> <li><var>n</var> and <var>a_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>n</var> integers in a line with spaces in between. The <var>i</var>-th integer should be <var>b_i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 2 1 3 </pre> <ul> <li>After step 1 of the first operation, <var>b</var> becomes: <var>1</var>.</li> <li>After step 2 of the first operation, <var>b</var> becomes: <var>1</var>.</li> <li>After step 1 of the second operation, <var>b</var> becomes: <var>1, 2</var>.</li> <li>After step 2 of the second operation, <var>b</var> becomes: <var>2, 1</var>.</li> <li>After step 1 of the third operation, <var>b</var> becomes: <var>2, 1, 3</var>.</li> <li>After step 2 of the third operation, <var>b</var> becomes: <var>3, 1, 2</var>.</li> <li>After step 1 of the fourth operation, <var>b</var> becomes: <var>3, 1, 2, 4</var>.</li> <li>After step 2 of the fourth operation, <var>b</var> becomes: <var>4, 2, 1, 3</var>.</li> </ul> <p>Thus, the answer is <code>4 2 1 3</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 1 2 </pre> <p>As shown above in Sample Output 1, <var>b</var> becomes <var>3, 1, 2</var> after step 2 of the third operation. Thus, the answer is <code>3 1 2</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1000000000 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>6 0 6 7 6 7 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 6 6 0 7 7 </pre></section> </div> </span>
[ [ "4\n1 2 3 4\n", "4\n1 2 3 4\n" ] ]
p03676
AtCoder Regular Contest 077 - 11
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>n+1</var>, <var>a_1,a_2,...,a_{n+1}</var>, which consists of the <var>n</var> integers <var>1,...,n</var>. It is known that each of the <var>n</var> integers <var>1,...,n</var> appears at least once in this sequence.</p> <p>For each integer <var>k=1,...,n+1</var>, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length <var>k</var>, modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><ul> <li> <p>If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.</p> </li> <li> <p>A subsequence of a sequence <var>a</var> with length <var>k</var> is a sequence obtained by selecting <var>k</var> of the elements of <var>a</var> and arranging them without changing their relative order. For example, the sequences <var>1,3,5</var> and <var>1,2,3</var> are subsequences of <var>1,2,3,4,5</var>, while <var>3,1,2</var> and <var>1,10,100</var> are not.</p> </li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 10^5</var></li> <li><var>1 \leq a_i \leq n</var></li> <li>Each of the integers <var>1,...,n</var> appears in the sequence.</li> <li><var>n</var> and <var>a_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> ... <var>a_{n+1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>n+1</var> lines. The <var>k</var>-th line should contain the number of the different subsequences of the given sequence with length <var>k</var>, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 5 4 1 </pre> <p>There are three subsequences with length <var>1</var>: <var>1</var> and <var>2</var> and <var>3</var>.</p> <p>There are five subsequences with length <var>2</var>: <var>1,1</var> and <var>1,2</var> and <var>1,3</var> and <var>2,1</var> and <var>2,3</var>.</p> <p>There are four subsequences with length <var>3</var>: <var>1,1,3</var> and <var>1,2,1</var> and <var>1,2,3</var> and <var>2,1,3</var>.</p> <p>There is one subsequence with length <var>4</var>: <var>1,2,1,3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 1 </pre> <p>There is one subsequence with length <var>1</var>: <var>1</var>.</p> <p>There is one subsequence with length <var>2</var>: <var>1,1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>32 29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>32 525 5453 40919 237336 1107568 4272048 13884156 38567100 92561040 193536720 354817320 573166440 818809200 37158313 166803103 166803103 37158313 818809200 573166440 354817320 193536720 92561040 38567100 13884156 4272048 1107568 237336 40920 5456 528 33 1 </pre> <p>Be sure to print the numbers modulo <var>10^9+7</var>.</p></section> </div> </span>
[ [ "3\n1 2 1 3\n", "3\n1 2 1 3\n" ] ]
p03677
AtCoder Regular Contest 077 - guruguru
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is buying a lamp. The light of the lamp can be adjusted to <var>m</var> levels of brightness, represented by integers from <var>1</var> through <var>m</var>, by the two buttons on the remote control.</p> <p>The first button is a "forward" button. When this button is pressed, the brightness level is increased by <var>1</var>, except when the brightness level is <var>m</var>, in which case the brightness level becomes <var>1</var>.</p> <p>The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level <var>x</var>, which is set when the lamp is purchased.</p> <p>Snuke is thinking of setting the favorite brightness level <var>x</var> so that he can efficiently adjust the brightness. He is planning to change the brightness <var>n-1</var> times. In the <var>i</var>-th change, the brightness level is changed from <var>a_i</var> to <var>a_{i+1}</var>. The initial brightness level is <var>a_1</var>. Find the number of times Snuke needs to press the buttons when <var>x</var> is set to minimize this number.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq n,m \leq 10^5</var></li> <li><var>1 \leq a_i\leq m</var></li> <li><var>a_i \neq a_{i+1}</var></li> <li><var>n</var>, <var>m</var> and <var>a_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>m</var> <var>a_1</var> <var>a_2</var> … <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of times Snuke needs to press the buttons.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 6 1 5 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>When the favorite brightness level is set to <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>, <var>5</var> and <var>6</var>, Snuke needs to press the buttons <var>8</var>, <var>9</var>, <var>7</var>, <var>5</var>, <var>6</var> and <var>9</var> times, respectively. Thus, Snuke should set the favorite brightness level to <var>4</var>. In this case, the brightness is adjusted as follows:</p> <ul> <li>In the first change, press the favorite button once, then press the forward button once.</li> <li>In the second change, press the forward button twice.</li> <li>In the third change, press the favorite button once.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 10 10 9 8 7 6 5 4 3 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>45 </pre></section> </div> </span>
[ [ "4 6\n1 5 1 4\n", "4 6\n1 5 1 4\n" ] ]
p03678
AtCoder Regular Contest 077 - SS
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will call a string that can be obtained by concatenating two equal strings an <em>even</em> string. For example, <code>xyzxyz</code> and <code>aaaaaa</code> are even, while <code>ababab</code> and <code>xyzxy</code> are not.</p> <p>For a non-empty string <var>S</var>, we will define <var>f(S)</var> as the shortest even string that can be obtained by appending one or more characters to the end of <var>S</var>. For example, <var>f(</var><code>abaaba</code><var>)=</var><code>abaababaab</code>. It can be shown that <var>f(S)</var> is uniquely determined for a non-empty string <var>S</var>.</p> <p>You are given an even string <var>S</var> consisting of lowercase English letters. For each letter in the lowercase English alphabet, find the number of its occurrences from the <var>l</var>-th character through the <var>r</var>-th character of <var>f^{10^{100}} (S)</var>.</p> <p>Here, <var>f^{10^{100}} (S)</var> is the string <var>f(f(f( ... f(S) ... )))</var> obtained by applying <var>f</var> to <var>S</var> <var>10^{100}</var> times.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq |S| \leq 2\times 10^5</var></li> <li><var>1 \leq l \leq r \leq 10^{18}</var></li> <li><var>S</var> is an even string consisting of lowercase English letters.</li> <li><var>l</var> and <var>r</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> <var>l</var> <var>r</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>26</var> integers in a line with spaces in between. The <var>i</var>-th integer should be the number of the occurrences of the <var>i</var>-th letter in the lowercase English alphabet from the <var>l</var>-th character through the <var>r</var>-th character of <var>f^{10^{100}} (S)</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>abaaba 6 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 </pre> <p>Since <var>f(</var><code>abaaba</code><var>)=</var><code>abaababaab</code>, the first ten characters in <var>f^{10^{100}}(S)</var> is also <code>abaababaab</code>. Thus, the sixth through the tenth characters are <code>abaab</code>. In this string, <code>a</code> appears three times, <code>b</code> appears twice and no other letters appear, and thus the output should be <var>3</var> and <var>2</var> followed by twenty-four <var>0</var>s.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>xx 1 1000000000000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1000000000000000000 0 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>vgxgpuamkvgxgvgxgpuamkvgxg 1 1000000000000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>87167725689669676 0 0 0 0 0 282080685775825810 0 0 0 87167725689669676 0 87167725689669676 0 0 87167725689669676 0 0 0 0 87167725689669676 141040342887912905 0 141040342887912905 0 0 </pre></section> </div> </span>
[ [ "abaaba\n6 10\n", "abaaba\n6 10\n" ] ]
p03679
AtCoder Beginner Contest 065 - Expired?
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most <var>X</var> days earlier. He gets a stomachache if the "best-by" date of the food is <var>X+1</var> or more days earlier, though.</p> <p>Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.</p> <p>Takahashi bought some food <var>A</var> days before the "best-by" date, and ate it <var>B</var> days after he bought it.</p> <p>Write a program that outputs <code>delicious</code> if he found it delicious, <code>safe</code> if he did not found it delicious but did not get a stomachache either, and <code>dangerous</code> if he got a stomachache.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ X,A,B ≤ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>delicious</code> if Takahashi found the food delicious; print <code>safe</code> if he neither found it delicious nor got a stomachache; print <code>dangerous</code> if he got a stomachache.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>safe </pre> <p>He ate the food three days after the "best-by" date. It was not delicious or harmful for him.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>delicious </pre> <p>He ate the food by the "best-by" date. It was delicious for him.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 7 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>dangerous </pre> <p>He ate the food five days after the "best-by" date. It was harmful for him.</p></section> </div> </span>
[ [ "4 3 6\n", "4 3 6\n" ] ]
p03680
AtCoder Beginner Contest 065 - Trained?
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.</p> <p>The exercise machine at the gym has <var>N</var> buttons, and exactly one of the buttons is lighten up. These buttons are numbered <var>1</var> through <var>N</var>. When Button <var>i</var> is lighten up and you press it, the light is turned off, and then Button <var>a_i</var> will be lighten up. It is possible that <var>i=a_i</var>. When Button <var>i</var> is not lighten up, nothing will happen by pressing it.</p> <p>Initially, Button <var>1</var> is lighten up. Takahashi wants to quit pressing buttons when Button <var>2</var> is lighten up.</p> <p>Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ a_i ≤ N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> : <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>-1</var> if it is impossible to lighten up Button <var>2</var>. Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button <var>2</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Press Button <var>1</var>, then Button <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 4 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>Pressing Button <var>1</var> lightens up Button <var>3</var>, and vice versa, so Button <var>2</var> will never be lighten up.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3 3 4 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "3\n3\n1\n2\n", "3\n3\n1\n2\n" ] ]
p03681
AtCoder Beginner Contest 065 - Reconciled?
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has <var>N</var> dogs and <var>M</var> monkeys. He wants them to line up in a row.</p> <p>As a Japanese saying goes, these dogs and monkeys are on bad terms. <em>("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.)</em> Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.</p> <p>How many such arrangements there are? Find the count modulo <var>10^9+7</var> (since animals cannot understand numbers larger than that). Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 10^5</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of possible arrangements, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>We will denote the dogs by <code>A</code> and <code>B</code>, and the monkeys by <code>C</code> and <code>D</code>. There are eight possible arrangements: <code>ACBD</code>, <code>ADBC</code>, <code>BCAD</code>, <code>BDAC</code>, <code>CADB</code>, <code>CBDA</code>, <code>DACB</code> and <code>DBCA</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>12 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>530123477 </pre></section> </div> </span>
[ [ "2 2\n", "2 2\n" ] ]
p03682
AtCoder Beginner Contest 065 - Built?
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> towns on a plane. The <var>i</var>-th town is located at the coordinates <var>(x_i,y_i)</var>. There may be more than one town at the same coordinates.</p> <p>You can build a road between two towns at coordinates <var>(a,b)</var> and <var>(c,d)</var> for a cost of <var>min(|a-c|,|b-d|)</var> yen (the currency of Japan). It is not possible to build other types of roads.</p> <p>Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>0 ≤ x_i,y_i ≤ 10^9</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> : <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 5 3 9 7 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>Build a road between Towns <var>1</var> and <var>2</var>, and another between Towns <var>2</var> and <var>3</var>. The total cost is <var>2+1=3</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 8 3 4 9 12 19 18 1 13 5 7 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8 </pre></section> </div> </span>
[ [ "3\n1 5\n3 9\n7 8\n", "3\n1 5\n3 9\n7 8\n" ] ]
p03683
AtCoder Regular Contest 076 - Reconciled?
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has <var>N</var> dogs and <var>M</var> monkeys. He wants them to line up in a row.</p> <p>As a Japanese saying goes, these dogs and monkeys are on bad terms. <em>("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.)</em> Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.</p> <p>How many such arrangements there are? Find the count modulo <var>10^9+7</var> (since animals cannot understand numbers larger than that). Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 10^5</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of possible arrangements, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>We will denote the dogs by <code>A</code> and <code>B</code>, and the monkeys by <code>C</code> and <code>D</code>. There are eight possible arrangements: <code>ACBD</code>, <code>ADBC</code>, <code>BCAD</code>, <code>BDAC</code>, <code>CADB</code>, <code>CBDA</code>, <code>DACB</code> and <code>DBCA</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>12 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>530123477 </pre></section> </div> </span>
[ [ "2 2\n", "2 2\n" ] ]
p03684
AtCoder Regular Contest 076 - Built?
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> towns on a plane. The <var>i</var>-th town is located at the coordinates <var>(x_i,y_i)</var>. There may be more than one town at the same coordinates.</p> <p>You can build a road between two towns at coordinates <var>(a,b)</var> and <var>(c,d)</var> for a cost of <var>min(|a-c|,|b-d|)</var> yen (the currency of Japan). It is not possible to build other types of roads.</p> <p>Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>0 ≤ x_i,y_i ≤ 10^9</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> : <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 5 3 9 7 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>Build a road between Towns <var>1</var> and <var>2</var>, and another between Towns <var>2</var> and <var>3</var>. The total cost is <var>2+1=3</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 8 3 4 9 12 19 18 1 13 5 7 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8 </pre></section> </div> </span>
[ [ "3\n1 5\n3 9\n7 8\n", "3\n1 5\n3 9\n7 8\n" ] ]
p03685
AtCoder Regular Contest 076 - Connected?
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is playing a puzzle game. In this game, you are given a rectangular board of dimensions <var>R × C</var>, filled with numbers. Each integer <var>i</var> from <var>1</var> through <var>N</var> is written twice, at the coordinates <var>(x_{i,1},y_{i,1})</var> and <var>(x_{i,2},y_{i,2})</var>.</p> <p>The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from <var>1</var> through <var>N</var>. Here, the curves may not go outside the board or cross each other.</p> <p>Determine whether this is possible.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ R,C ≤ 10^8</var></li> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>0 ≤ x_{i,1},x_{i,2} ≤ R(1 ≤ i ≤ N)</var></li> <li><var>0 ≤ y_{i,1},y_{i,2} ≤ C(1 ≤ i ≤ N)</var></li> <li>All given points are distinct.</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>R</var> <var>C</var> <var>N</var> <var>x_{1,1}</var> <var>y_{1,1}</var> <var>x_{1,2}</var> <var>y_{1,2}</var> : <var>x_{N,1}</var> <var>y_{N,1}</var> <var>x_{N,2}</var> <var>y_{N,2}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> if the objective is achievable; print <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 3 0 1 3 1 1 1 4 1 2 0 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p><img alt="" src="https://atcoder.jp/img/arc076/hogehogehoge.png"/></p> <p>The above figure shows a possible solution.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 4 0 0 2 2 2 0 0 1 0 2 1 2 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 7 0 0 2 4 2 3 4 5 3 5 5 2 5 5 5 4 0 3 5 1 2 2 4 4 0 5 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 1 2 0 0 1 1 1 0 0 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>NO </pre></section> </div> </span>
[ [ "4 2 3\n0 1 3 1\n1 1 4 1\n2 0 2 2\n", "4 2 3\n0 1 3 1\n1 1 4 1\n2 0 2 2\n" ] ]
p03686
AtCoder Regular Contest 076 - Exhausted?
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>M</var> chairs arranged in a line. The coordinate of the <var>i</var>-th chair <var>(1 ≤ i ≤ M)</var> is <var>i</var>.</p> <p><var>N</var> people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the <var>i</var>-th person wishes to sit in a chair whose coordinate is not greater than <var>L_i</var>, or not less than <var>R_i</var>. Naturally, only one person can sit in the same chair.</p> <p>It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions.</p> <p>Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 2 × 10^5</var></li> <li><var>0 ≤ L_i &lt; R_i ≤ M + 1(1 ≤ i ≤ N)</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>L_1</var> <var>R_1</var> <var>:</var> <var>L_N</var> <var>R_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum required number of additional chairs.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 4 0 3 2 3 1 3 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>The four people can sit in chairs at the coordinates <var>3</var>, <var>2</var>, <var>1</var> and <var>4</var>, respectively, and no more chair is needed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>If we place additional chairs at the coordinates <var>0</var> and <var>2.5</var>, the seven people can sit at coordinates <var>0</var>, <var>5</var>, <var>3</var>, <var>2</var>, <var>6</var>, <var>1</var> and <var>2.5</var>, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 1 1 2 1 2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>6 6 1 6 1 6 1 5 1 5 2 6 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>2 </pre></section> </div> </span>
[ [ "4 4\n0 3\n2 3\n1 3\n3 4\n", "4 4\n0 3\n2 3\n1 3\n3 4\n" ] ]
p03687
AtCoder Grand Contest 016 - Shrinking
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke can change a string <var>t</var> of length <var>N</var> into a string <var>t'</var> of length <var>N - 1</var> under the following rule:</p> <ul> <li>For each <var>i</var> (<var>1 ≤ i ≤ N - 1</var>), the <var>i</var>-th character of <var>t'</var> must be either the <var>i</var>-th or <var>(i + 1)</var>-th character of <var>t</var>.</li> </ul> <p>There is a string <var>s</var> consisting of lowercase English letters. Snuke's objective is to apply the above operation to <var>s</var> repeatedly so that all the characters in <var>s</var> are the same. Find the minimum necessary number of operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ |s| ≤ 100</var></li> <li><var>s</var> consists of lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum necessary number of operations to achieve the objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>serval </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>One solution is: <code>serval</code> → <code>srvvl</code> → <code>svvv</code> → <code>vvv</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>jackal </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>One solution is: <code>jackal</code> → <code>aacaa</code> → <code>aaaa</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>zzz </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>All the characters in <var>s</var> are the same from the beginning.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>whbrjpjyhsrywlqjxdbrbaomnw </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>8 </pre> <p>In <var>8</var> operations, he can change <var>s</var> to <code>rrrrrrrrrrrrrrrrrr</code>.</p></section> </div> </span>
[ [ "serval\n", "serval\n" ] ]
p03688
AtCoder Grand Contest 016 - Colorful Hats
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> cats. We number them from <var>1</var> through <var>N</var>.</p> <p>Each of the cats wears a hat. Cat <var>i</var> says: "there are exactly <var>a_i</var> different colors among the <var>N - 1</var> hats worn by the cats except me."</p> <p>Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ a_i ≤ N-1</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print <code>No</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>For example, if cat <var>1</var>, <var>2</var> and <var>3</var> wears red, blue and blue hats, respectively, it is consistent with the remarks of the cats.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>From the remark of cat <var>1</var>, we can see that cat <var>2</var> and <var>3</var> wear hats of the same color. Also, from the remark of cat <var>2</var>, we can see that cat <var>1</var> and <var>3</var> wear hats of the same color. Therefore, cat <var>1</var> and <var>2</var> wear hats of the same color, which contradicts the remark of cat <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 4 3 4 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>4 2 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 6</h3><pre>5 3 3 3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 6</h3><pre>No </pre></section> </div> </span>
[ [ "3\n1 2 2\n", "3\n1 2 2\n" ] ]
p03689
AtCoder Grand Contest 016 - +/- Rectangle
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given four integers: <var>H</var>, <var>W</var>, <var>h</var> and <var>w</var> (<var>1 ≤ h ≤ H</var>, <var>1 ≤ w ≤ W</var>). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:</p> <ul> <li>The matrix has <var>H</var> rows and <var>W</var> columns.</li> <li>Each element of the matrix is an integer between <var>-10^9</var> and <var>10^9</var> (inclusive).</li> <li>The sum of all the elements of the matrix is positive.</li> <li>The sum of all the elements within every subrectangle with <var>h</var> rows and <var>w</var> columns in the matrix is negative.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ h ≤ H ≤ 500</var></li> <li><var>1 ≤ w ≤ W ≤ 500</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>h</var> <var>w</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there does not exist a matrix that satisfies all of the conditions, print <code>No</code>.</p> <p>Otherwise, print <code>Yes</code> in the first line, and print a matrix in the subsequent lines in the following format:</p> <pre><var>a_{11}</var> <var>...</var> <var>a_{1W}</var> <var>:</var> <var>a_{H1}</var> <var>...</var> <var>a_{HW}</var> </pre> <p>Here, <var>a_{ij}</var> represents the <var>(i,\ j)</var> element of the matrix.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes 1 1 1 1 -4 1 1 1 1 </pre> <p>The sum of all the elements of this matrix is <var>4</var>, which is positive. Also, in this matrix, there are four subrectangles with <var>2</var> rows and <var>2</var> columns as shown below. For each of them, the sum of all the elements inside is <var>-1</var>, which is negative.</p> <div style="text-align: center;"> <img alt="bbdb651fa1f05996886da9f0c4d8090a.png" src="https://atcoder.jp/img/agc016/bbdb651fa1f05996886da9f0c4d8090a.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 4 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7 </pre></section> </div> </span>
[ [ "3 3 2 2\n", "3 3 2 2\n" ] ]
p03690
AtCoder Grand Contest 016 - XOR Replace
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a sequence of length <var>N</var>: <var>a = (a_1, a_2, ..., a_N)</var>. Here, each <var>a_i</var> is a non-negative integer.</p> <p>Snuke can repeatedly perform the following operation:</p> <ul> <li>Let the XOR of all the elements in <var>a</var> be <var>x</var>. Select an integer <var>i</var> (<var>1 ≤ i ≤ N</var>) and replace <var>a_i</var> with <var>x</var>.</li> </ul> <p>Snuke's objective is to match <var>a</var> with another sequence <var>b = (b_1, b_2, ..., b_N)</var>. Here, each <var>b_i</var> is a non-negative integer.</p> <p>Determine whether the objective is achievable, and find the minimum necessary number of operations if the answer is positive.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>a_i</var> and <var>b_i</var> are integers.</li> <li><var>0 ≤ a_i, b_i &lt; 2^{30}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> <var>b_1</var> <var>b_2</var> <var>...</var> <var>b_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the objective is achievable, print the minimum necessary number of operations. Otherwise, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 0 1 2 3 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>At first, the XOR of all the elements of <var>a</var> is <var>3</var>. If we replace <var>a_1</var> with <var>3</var>, <var>a</var> becomes <var>(3, 1, 2)</var>.</p> <p>Now, the XOR of all the elements of <var>a</var> is <var>0</var>. If we replace <var>a_3</var> with <var>0</var>, <var>a</var> becomes <var>(3, 1, 0)</var>, which matches <var>b</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 0 1 2 0 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 1 1 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 0 1 2 3 1 0 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre></section> </div> </span>
[ [ "3\n0 1 2\n3 1 0\n", "3\n0 1 2\n3 1 0\n" ] ]
p03691
AtCoder Grand Contest 016 - Poor Turkeys
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> turkeys. We number them from <var>1</var> through <var>N</var>.</p> <p><var>M</var> men will visit here one by one. The <var>i</var>-th man to visit will take the following action:</p> <ul> <li>If both turkeys <var>x_i</var> and <var>y_i</var> are alive: selects one of them with equal probability, then eats it.</li> <li>If either turkey <var>x_i</var> or <var>y_i</var> is alive (but not both): eats the alive one.</li> <li>If neither turkey <var>x_i</var> nor <var>y_i</var> is alive: does nothing.</li> </ul> <p>Find the number of pairs <var>(i,\ j)</var> (<var>1 ≤ i &lt; j ≤ N</var>) such that the following condition is held:</p> <ul> <li>The probability of both turkeys <var>i</var> and <var>j</var> being alive after all the men took actions, is greater than <var>0</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 400</var></li> <li><var>1 ≤ M ≤ 10^5</var></li> <li><var>1 ≤ x_i &lt; y_i ≤ N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_M</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of pairs <var>(i,\ j)</var> (<var>1 ≤ i &lt; j ≤ N</var>) such that the condition is held.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p><var>(i,\ j) = (1,\ 3), (2,\ 3)</var> satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 1 2 3 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p><var>(i,\ j) = (1,\ 4)</var> satisfies the condition. Both turkeys <var>1</var> and <var>4</var> are alive if:</p> <ul> <li>The first man eats turkey <var>2</var>.</li> <li>The second man eats turkey <var>3</var>.</li> <li>The third man does nothing.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 2 1 2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 10 8 9 2 8 4 6 4 9 7 8 2 8 1 8 3 4 3 4 2 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre></section> </div> </span>
[ [ "3 1\n1 2\n", "3 1\n1 2\n" ] ]
p03692
AtCoder Grand Contest 016 - Games on DAG
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a directed graph <var>G</var> with <var>N</var> vertices and <var>M</var> edges. The vertices are numbered <var>1</var> through <var>N</var>, and the edges are numbered <var>1</var> through <var>M</var>. Edge <var>i</var> is directed from <var>x_i</var> to <var>y_i</var>. Here, <var>x_i &lt; y_i</var> holds. Also, there are no multiple edges in <var>G</var>.</p> <p>Consider selecting a subset of the set of the <var>M</var> edges in <var>G</var>, and removing these edges from <var>G</var> to obtain another graph <var>G'</var>. There are <var>2^M</var> different possible graphs as <var>G'</var>.</p> <p>Alice and Bob play against each other in the following game played on <var>G'</var>. First, place two pieces on vertices <var>1</var> and <var>2</var>, one on each. Then, starting from Alice, Alice and Bob alternately perform the following operation:</p> <ul> <li>Select an edge <var>i</var> such that there is a piece placed on vertex <var>x_i</var>, and move the piece to vertex <var>y_i</var> (if there are two pieces on vertex <var>x_i</var>, only move one). The two pieces are allowed to be placed on the same vertex.</li> </ul> <p>The player loses when he/she becomes unable to perform the operation. We assume that both players play optimally.</p> <p>Among the <var>2^M</var> different possible graphs as <var>G'</var>, how many lead to Alice's victory? Find the count modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 15</var></li> <li><var>1 ≤ M ≤ N(N-1)/2</var></li> <li><var>1 ≤ x_i &lt; y_i ≤ N</var></li> <li>All <var>(x_i,\ y_i)</var> are distinct.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_M</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of <var>G'</var> that lead to Alice's victory, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>The figure below shows the two possible graphs as <var>G'</var>. A graph marked with ○ leads to Alice's victory, and a graph marked with × leads to Bob's victory.</p> <div style="text-align: center;"> <img alt="b250f23c38d0f5ec2204bd714e7c1516.png" src="https://atcoder.jp/img/agc016/b250f23c38d0f5ec2204bd714e7c1516.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 1 2 1 3 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 </pre> <p>The figure below shows the eight possible graphs as <var>G'</var>.</p> <div style="text-align: center;"> <img alt="8192fd32f894f708c5e4a60dcdea9d35.png" src="https://atcoder.jp/img/agc016/8192fd32f894f708c5e4a60dcdea9d35.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 2 1 3 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 10 2 4 3 4 2 5 2 3 1 2 3 5 1 3 1 5 4 5 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>816 </pre></section> </div> </span>
[ [ "2 1\n1 2\n", "2 1\n1 2\n" ] ]
p03693
AtCoder Beginner Contest 064 - RGB Cards
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer has three cards, one red, one green and one blue.<br/> An integer between <var>1</var> and <var>9</var> (inclusive) is written on each card: <var>r</var> on the red card, <var>g</var> on the green card and <var>b</var> on the blue card.<br/> We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.<br/> Is this integer a multiple of <var>4</var>? </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ r, g, b ≤ 9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>r</var> <var>g</var> <var>b</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the three-digit integer is a multiple of <var>4</var>, print <code>YES</code> (case-sensitive); otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p><var>432</var> is a multiple of <var>4</var>, and thus <code>YES</code> should be printed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> <p><var>234</var> is not a multiple of <var>4</var>, and thus <code>NO</code> should be printed.</p></section> </div> </span>
[ [ "4 3 2\n", "4 3 2\n" ] ]
p03694
AtCoder Beginner Contest 064 - Traveling AtCoDeer Problem
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.<br/> There are <var>N</var> houses along <em>TopCoDeer street</em>. The <var>i</var>-th house is located at coordinate <var>a_i</var>. He has decided to deliver gifts to all these houses.<br/> Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 100</var></li> <li><var>0 ≤ a_i ≤ 1000</var></li> <li><var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum distance to be traveled. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 3 7 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>The travel distance of <var>7</var> can be achieved by starting at coordinate <var>9</var> and traveling straight to coordinate <var>2</var>.<br/> It is not possible to do with a travel distance of less than <var>7</var>, and thus <var>7</var> is the minimum distance to be traveled. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>8 3 1 4 1 5 9 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8 </pre> <p>There may be more than one house at a position. </p></section> </div> </span>
[ [ "4\n2 3 7 9\n", "4\n2 3 7 9\n" ] ]
p03695
AtCoder Beginner Contest 064 - Colorful Leaderboard
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In AtCoder, a person who has participated in a contest receives a <em>color</em>, which corresponds to the person's rating as follows: </p> <ul> <li>Rating <var>1</var>-<var>399</var> : gray</li> <li>Rating <var>400</var>-<var>799</var> : brown</li> <li>Rating <var>800</var>-<var>1199</var> : green</li> <li>Rating <var>1200</var>-<var>1599</var> : cyan</li> <li>Rating <var>1600</var>-<var>1999</var> : blue</li> <li>Rating <var>2000</var>-<var>2399</var> : yellow</li> <li>Rating <var>2400</var>-<var>2799</var> : orange</li> <li>Rating <var>2800</var>-<var>3199</var> : red</li> </ul> <p>Other than the above, a person whose rating is <var>3200</var> or higher can freely pick his/her color, which can be one of the eight colors above or not.<br/> Currently, there are <var>N</var> users who have participated in a contest in AtCoder, and the <var>i</var>-th user has a rating of <var>a_i</var>.<br/> Find the minimum and maximum possible numbers of different colors of the users. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 100</var></li> <li><var>1 ≤ a_i ≤ 4800</var></li> <li><var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2100 2500 2700 2700 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 2 </pre> <p>The user with rating <var>2100</var> is "yellow", and the others are "orange". There are two different colors. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1100 1900 2800 3200 3200 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 5 </pre> <p>The user with rating <var>1100</var> is "green", the user with rating <var>1900</var> is blue and the user with rating <var>2800</var> is "red".<br/> If the fourth user picks "red", and the fifth user picks "blue", there are three different colors. This is one possible scenario for the minimum number of colors.<br/> If the fourth user picks "purple", and the fifth user picks "black", there are five different colors. This is one possible scenario for the maximum number of colors. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>20 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 1 </pre> <p>All the users are "green", and thus there is one color.</p></section> </div> </span>
[ [ "4\n2100 2500 2700 2700\n", "4\n2100 2500 2700 2700\n" ] ]
p03696
AtCoder Beginner Contest 064 - Insertion
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>N</var> consisting of <code>(</code> and <code>)</code>. Your task is to insert some number of <code>(</code> and <code>)</code> into <var>S</var> to obtain a <em>correct bracket sequence</em>.<br/> Here, a correct bracket sequence is defined as follows: </p> <ul> <li><code>()</code> is a correct bracket sequence.</li> <li>If <var>X</var> is a correct bracket sequence, the concatenation of <code>(</code>, <var>X</var> and <code>)</code> in this order is also a correct bracket sequence.</li> <li>If <var>X</var> and <var>Y</var> are correct bracket sequences, the concatenation of <var>X</var> and <var>Y</var> in this order is also a correct bracket sequence.</li> <li>Every correct bracket sequence can be derived from the rules above.</li> </ul> <p>Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>The length of <var>S</var> is <var>N</var>.</li> <li><var>1 ≤ N ≤ 100</var></li> <li><var>S</var> consists of <code>(</code> and <code>)</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of <code>(</code> and <code>)</code> into <var>S</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 ()) </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>(()) </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 )))()) </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>(((()))()) </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 ))))(((( </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>(((())))(((()))) </pre></section> </div> </span>
[ [ "3\n())\n", "3\n())\n" ] ]
p03697
AtCoder Beginner Contest 063 - Restricted
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>A</var> and <var>B</var> as the input. Output the value of <var>A + B</var>.</p> <p>However, if <var>A + B</var> is <var>10</var> or greater, output <code>error</code> instead.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>A</var> and <var>B</var> are integers.</li> <li><var>1 ≤ A, B ≤ 9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>A + B</var> is <var>10</var> or greater, print the string <code>error</code> (case-sensitive); otherwise, print the value of <var>A + B</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>9 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>error </pre></section> </div> </span>
[ [ "6 3\n", "6 3\n" ] ]
p03698
AtCoder Beginner Contest 063 - Varied
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of lowercase English letters. Determine whether all the characters in <var>S</var> are different.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ |S| ≤ 26</var>, where <var>|S|</var> denotes the length of <var>S</var>.</li> <li><var>S</var> consists of lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If all the characters in <var>S</var> are different, print <code>yes</code> (case-sensitive); otherwise, print <code>no</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>uncopyrightable </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>different </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>no </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>no </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>yes </pre></section> </div> </span>
[ [ "uncopyrightable\n", "uncopyrightable\n" ] ]
p03699
AtCoder Beginner Contest 063 - Bugged
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are taking a computer-based examination. The examination consists of <var>N</var> questions, and the score allocated to the <var>i</var>-th question is <var>s_i</var>. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.</p> <p>However, the examination system is actually flawed, and if your grade is a multiple of <var>10</var>, the system displays <var>0</var> as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≤ N ≤ 100</var></li> <li><var>1 ≤ s_i ≤ 100</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s_1</var> <var>s_2</var> <var>:</var> <var>s_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum value that can be displayed as your grade.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 10 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>25 </pre> <p>Your grade will be <var>25</var> if the <var>10</var>-point and <var>15</var>-point questions are answered correctly and the <var>5</var>-point question is not, and this grade will be displayed correctly. Your grade will become <var>30</var> if the <var>5</var>-point question is also answered correctly, but this grade will be incorrectly displayed as <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 10 10 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>35 </pre> <p>Your grade will be <var>35</var> if all the questions are answered correctly, and this grade will be displayed correctly.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 10 20 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>Regardless of whether each question is answered correctly or not, your grade will be a multiple of <var>10</var> and displayed as <var>0</var>.</p></section> </div> </span>
[ [ "3\n5\n10\n15\n", "3\n5\n10\n15\n" ] ]
p03700
AtCoder Beginner Contest 063 - Widespread
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are going out for a walk, when you suddenly encounter <var>N</var> monsters. Each monster has a parameter called <em>health</em>, and the health of the <var>i</var>-th monster is <var>h_i</var> at the moment of encounter. A monster will vanish immediately when its health drops to <var>0</var> or below.</p> <p>Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:</p> <ul> <li>Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by <var>A</var>, and the health of each of the other monsters will decrease by <var>B</var>. Here, <var>A</var> and <var>B</var> are predetermined parameters, and <var>A &gt; B</var> holds.</li> </ul> <p>At least how many explosions do you need to cause in order to vanish all the monsters?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ B &lt; A ≤ 10^9</var></li> <li><var>1 ≤ h_i ≤ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>h_1</var> <var>h_2</var> <var>:</var> <var>h_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 3 8 7 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>You can vanish all the monsters in two explosion, as follows:</p> <ul> <li>First, cause an explosion centered at the monster with <var>8</var> health. The healths of the four monsters become <var>3</var>, <var>4</var>, <var>1</var> and <var>-1</var>, respectively, and the last monster vanishes.</li> <li>Second, cause an explosion centered at the monster with <var>4</var> health remaining. The healths of the three remaining monsters become <var>0</var>, <var>-1</var> and <var>-2</var>, respectively, and all the monsters are now vanished.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 10 4 20 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> <p>You need to cause two explosions centered at each monster, for a total of four.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 2 1 900000000 900000000 1000000000 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>800000000 </pre></section> </div> </span>
[ [ "4 5 3\n8\n7\n4\n2\n", "4 5 3\n8\n7\n4\n2\n" ] ]
p03701
AtCoder Regular Contest 075 - Bugged
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are taking a computer-based examination. The examination consists of <var>N</var> questions, and the score allocated to the <var>i</var>-th question is <var>s_i</var>. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.</p> <p>However, the examination system is actually flawed, and if your grade is a multiple of <var>10</var>, the system displays <var>0</var> as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≤ N ≤ 100</var></li> <li><var>1 ≤ s_i ≤ 100</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s_1</var> <var>s_2</var> <var>:</var> <var>s_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum value that can be displayed as your grade.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 10 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>25 </pre> <p>Your grade will be <var>25</var> if the <var>10</var>-point and <var>15</var>-point questions are answered correctly and the <var>5</var>-point question is not, and this grade will be displayed correctly. Your grade will become <var>30</var> if the <var>5</var>-point question is also answered correctly, but this grade will be incorrectly displayed as <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 10 10 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>35 </pre> <p>Your grade will be <var>35</var> if all the questions are answered correctly, and this grade will be displayed correctly.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 10 20 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>Regardless of whether each question is answered correctly or not, your grade will be a multiple of <var>10</var> and displayed as <var>0</var>.</p></section> </div> </span>
[ [ "3\n5\n10\n15\n", "3\n5\n10\n15\n" ] ]
p03702
AtCoder Regular Contest 075 - Widespread
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are going out for a walk, when you suddenly encounter <var>N</var> monsters. Each monster has a parameter called <em>health</em>, and the health of the <var>i</var>-th monster is <var>h_i</var> at the moment of encounter. A monster will vanish immediately when its health drops to <var>0</var> or below.</p> <p>Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:</p> <ul> <li>Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by <var>A</var>, and the health of each of the other monsters will decrease by <var>B</var>. Here, <var>A</var> and <var>B</var> are predetermined parameters, and <var>A &gt; B</var> holds.</li> </ul> <p>At least how many explosions do you need to cause in order to vanish all the monsters?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ B &lt; A ≤ 10^9</var></li> <li><var>1 ≤ h_i ≤ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>h_1</var> <var>h_2</var> <var>:</var> <var>h_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 3 8 7 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>You can vanish all the monsters in two explosion, as follows:</p> <ul> <li>First, cause an explosion centered at the monster with <var>8</var> health. The healths of the four monsters become <var>3</var>, <var>4</var>, <var>1</var> and <var>-1</var>, respectively, and the last monster vanishes.</li> <li>Second, cause an explosion centered at the monster with <var>4</var> health remaining. The healths of the three remaining monsters become <var>0</var>, <var>-1</var> and <var>-2</var>, respectively, and all the monsters are now vanished.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 10 4 20 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> <p>You need to cause two explosions centered at each monster, for a total of four.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 2 1 900000000 900000000 1000000000 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>800000000 </pre></section> </div> </span>
[ [ "4 5 3\n8\n7\n4\n2\n", "4 5 3\n8\n7\n4\n2\n" ] ]
p03703
AtCoder Regular Contest 075 - Meaningful Mean
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>N</var>, <var>a =</var> {<var>a_1, a_2, …, a_N</var>}, and an integer <var>K</var>.</p> <p><var>a</var> has <var>N(N+1)/2</var> non-empty contiguous subsequences, {<var>a_l, a_{l+1}, …, a_r</var>} <var>(1 ≤ l ≤ r ≤ N)</var>. Among them, how many have an arithmetic mean that is greater than or equal to <var>K</var>?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≤ N ≤ 2 \times 10^5</var></li> <li><var>1 ≤ K ≤ 10^9</var></li> <li><var>1 ≤ a_i ≤ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> <var>:</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to <var>K</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 6 7 5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>All the non-empty contiguous subsequences of <var>a</var> are listed below:</p> <ul> <li>{<var>a_1</var>} = {<var>7</var>}</li> <li>{<var>a_1, a_2</var>} = {<var>7, 5</var>}</li> <li>{<var>a_1, a_2, a_3</var>} = {<var>7, 5, 7</var>}</li> <li>{<var>a_2</var>} = {<var>5</var>}</li> <li>{<var>a_2, a_3</var>} = {<var>5, 7</var>}</li> <li>{<var>a_3</var>} = {<var>7</var>}</li> </ul> <p>Their means are <var>7</var>, <var>6</var>, <var>19/3</var>, <var>5</var>, <var>6</var> and <var>7</var>, respectively, and five among them are <var>6</var> or greater. Note that {<var>a_1</var>} and {<var>a_3</var>} are indistinguishable by the values of their elements, but we count them individually.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 26 10 20 30 40 30 20 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>13 </pre></section> </div> </span>
[ [ "3 6\n7\n5\n7\n", "3 6\n7\n5\n7\n" ] ]
p03704
AtCoder Regular Contest 075 - Mirrored
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>For a positive integer <var>n</var>, we denote the integer obtained by reversing the decimal notation of <var>n</var> (without leading zeroes) by <var>rev(n)</var>. For example, <var>rev(123) = 321</var> and <var>rev(4000) = 4</var>.</p> <p>You are given a positive integer <var>D</var>. How many positive integers <var>N</var> satisfy <var>rev(N) = N + D</var>?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>D</var> is an integer.</li> <li><var>1 ≤ D &lt; 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>D</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the positive integers <var>N</var> such that <var>rev(N) = N + D</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>63 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are two positive integers <var>N</var> such that <var>rev(N) = N + 63</var>: <var>N = 18</var> and <var>29</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>75 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>There are no positive integers <var>N</var> such that <var>rev(N) = N + 75</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>864197532 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1920 </pre></section> </div> </span>
[ [ "63\n", "63\n" ] ]
p03705
AtCoder Grand Contest 015 - A+...+B Problem
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has <var>N</var> integers. Among them, the smallest is <var>A</var>, and the largest is <var>B</var>. We are interested in the sum of those <var>N</var> integers. How many different possible sums there are?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,A,B ≤ 10^9</var></li> <li><var>A</var> and <var>B</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different possible sums.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 4 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>There are five possible sums: <var>18=4+4+4+6</var>, <var>19=4+4+5+6</var>, <var>20=4+5+5+6</var>, <var>21=4+5+6+6</var> and <var>22=4+6+6+6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 7 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre></section> </div> </span>
[ [ "4 4 6\n", "4 4 6\n" ] ]
p03706
AtCoder Grand Contest 015 - Evilator
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Skenu constructed a building that has <var>N</var> floors. The building has an elevator that stops at every floor.</p> <p>There are buttons to control the elevator, but Skenu thoughtlessly installed only one button on each floor - up or down. This means that, from each floor, one can only go in one direction. If <var>S_i</var> is <code>U</code>, only "up" button is installed on the <var>i</var>-th floor and one can only go up; if <var>S_i</var> is <code>D</code>, only "down" button is installed on the <var>i</var>-th floor and one can only go down.</p> <p>The residents have no choice but to go to their destination floors via other floors if necessary. Find the sum of the following numbers over all ordered pairs of two floors <var>(i,j)</var>: the minimum number of times one needs to take the elevator to get to the <var>j</var>-th floor from the <var>i</var>-th floor.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ |S| ≤ 10^5</var></li> <li><var>S_i</var> is either <code>U</code> or <code>D</code>.</li> <li><var>S_1</var> is <code>U</code>.</li> <li><var>S_{|S|}</var> is <code>D</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S_1S_2...S_{|S|}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the following numbers over all ordered pairs of two floors <var>(i,j)</var>: the minimum number of times one needs to take the elevator to get to the <var>j</var>-th floor from the <var>i</var>-th floor.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>UUD </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>From the <var>1</var>-st floor, one can get to the <var>2</var>-nd floor by taking the elevator once.</p> <p>From the <var>1</var>-st floor, one can get to the <var>3</var>-rd floor by taking the elevator once.</p> <p>From the <var>2</var>-nd floor, one can get to the <var>1</var>-st floor by taking the elevator twice.</p> <p>From the <var>2</var>-nd floor, one can get to the <var>3</var>-rd floor by taking the elevator once.</p> <p>From the <var>3</var>-rd floor, one can get to the <var>1</var>-st floor by taking the elevator once.</p> <p>From the <var>3</var>-rd floor, one can get to the <var>2</var>-nd floor by taking the elevator once.</p> <p>The sum of these numbers of times, <var>7</var>, should be printed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>UUDUUDUD </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>77 </pre></section> </div> </span>
[ [ "UUD\n", "UUD\n" ] ]
p03707
AtCoder Grand Contest 015 - Nuske vs Phantom Thnook
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Nuske has a grid with <var>N</var> rows and <var>M</var> columns of squares. The rows are numbered <var>1</var> through <var>N</var> from top to bottom, and the columns are numbered <var>1</var> through <var>M</var> from left to right. Each square in the grid is painted in either blue or white. If <var>S_{i,j}</var> is <var>1</var>, the square at the <var>i</var>-th row and <var>j</var>-th column is blue; if <var>S_{i,j}</var> is <var>0</var>, the square is white. For every pair of two blue square <var>a</var> and <var>b</var>, there is at most one path that starts from <var>a</var>, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches <var>b</var>, without traversing the same square more than once.</p> <p>Phantom Thnook, Nuske's eternal rival, gives <var>Q</var> queries to Nuske. The <var>i</var>-th query consists of four integers <var>x_{i,1}</var>, <var>y_{i,1}</var>, <var>x_{i,2}</var> and <var>y_{i,2}</var> and asks him the following: when the rectangular region of the grid bounded by (and including) the <var>x_{i,1}</var>-th row, <var>x_{i,2}</var>-th row, <var>y_{i,1}</var>-th column and <var>y_{i,2}</var>-th column is cut out, how many connected components consisting of blue squares there are in the region?</p> <p>Process all the queries.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 2000</var></li> <li><var>1 ≤ Q ≤ 200000</var></li> <li><var>S_{i,j}</var> is either <var>0</var> or <var>1</var>.</li> <li><var>S_{i,j}</var> satisfies the condition explained in the statement.</li> <li><var>1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q)</var></li> <li><var>1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>Q</var> <var>S_{1,1}</var>..<var>S_{1,M}</var> : <var>S_{N,1}</var>..<var>S_{N,M}</var> <var>x_{1,1}</var> <var>y_{i,1}</var> <var>x_{i,2}</var> <var>y_{i,2}</var> : <var>x_{Q,1}</var> <var>y_{Q,1}</var> <var>x_{Q,2}</var> <var>y_{Q,2}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each query, print the number of the connected components consisting of blue squares in the region.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 4 1101 0110 1101 1 1 3 4 1 1 3 1 2 2 3 4 1 2 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 2 2 2 </pre> <p><img alt="" src="https://atcoder.jp/img/agc015/7aa4a2252f50a19fc18e0cec01368a2a.png"/></p> <p>In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus <var>3</var> should be printed.</p> <p>In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus <var>2</var> should be printed. Note that squares that belong to the same component in the original grid may belong to different components.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 5 6 11010 01110 10101 11101 01010 1 1 5 5 1 2 4 5 2 3 3 4 3 3 3 3 3 1 3 5 1 1 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 2 1 1 3 2 </pre></section> </div> </span>
[ [ "3 4 4\n1101\n0110\n1101\n1 1 3 4\n1 1 3 1\n2 2 3 4\n1 2 2 4\n", "3 4 4\n1101\n0110\n1101\n1 1 3 4\n1 1 3 1\n2 2 3 4\n1 2 2 4\n" ] ]
p03708
AtCoder Grand Contest 015 - A or...or B Problem
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Nukes has an integer that can be represented as the bitwise OR of one or more integers between <var>A</var> and <var>B</var> (inclusive). How many possible candidates of the value of Nukes's integer there are?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ A ≤ B &lt; 2^{60}</var></li> <li><var>A</var> and <var>B</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of possible candidates of the value of Nukes's integer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>In this case, <var>A=7</var> and <var>B=9</var>. There are four integers that can be represented as the bitwise OR of a non-empty subset of {<var>7</var>, <var>8</var>, <var>9</var>}: <var>7</var>, <var>8</var>, <var>9</var> and <var>15</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>65 98 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>63 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>271828182845904523 314159265358979323 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>68833183630578410 </pre></section> </div> </span>
[ [ "7\n9\n", "7\n9\n" ] ]
p03709
AtCoder Grand Contest 015 - Mr.Aoki Incubator
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is an expert of Clone Jutsu, a secret art that creates copies of his body.</p> <p>On a number line, there are <var>N</var> copies of Takahashi, numbered <var>1</var> through <var>N</var>. The <var>i</var>-th copy is located at position <var>X_i</var> and starts walking with velocity <var>V_i</var> in the positive direction at time <var>0</var>.</p> <p>Kenus is a master of Transformation Jutsu, and not only can he change into a person other than himself, but he can also transform another person into someone else.</p> <p>Kenus can select some of the copies of Takahashi at time <var>0</var>, and transform them into copies of Aoki, another Ninja. The walking velocity of a copy does not change when it transforms. From then on, whenever a copy of Takahashi and a copy of Aoki are at the same coordinate, that copy of Takahashi transforms into a copy of Aoki.</p> <p>Among the <var>2^N</var> ways to transform some copies of Takahashi into copies of Aoki at time <var>0</var>, in how many ways will all the copies of Takahashi become copies of Aoki after a sufficiently long time? Find the count modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 200000</var></li> <li><var>1 ≤ X_i,V_i ≤ 10^9(1 ≤ i ≤ N)</var></li> <li><var>X_i</var> and <var>V_i</var> are integers.</li> <li>All <var>X_i</var> are distinct.</li> <li>All <var>V_i</var> are distinct.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X_1</var> <var>V_1</var> : <var>X_N</var> <var>V_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the ways that cause all the copies of Takahashi to turn into copies of Aoki after a sufficiently long time, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 5 6 1 3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>All copies of Takahashi will turn into copies of Aoki after a sufficiently long time if Kenus transforms one of the following sets of copies of Takahashi into copies of Aoki: <var>(2)</var>, <var>(3)</var>, <var>(1,2)</var>, <var>(1,3)</var>, <var>(2,3)</var> and <var>(1,2,3)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 7 2 9 8 16 10 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>9 </pre></section> </div> </span>
[ [ "3\n2 5\n6 1\n3 7\n", "3\n2 5\n6 1\n3 7\n" ] ]
p03710
AtCoder Grand Contest 015 - Kenus the Ancient Greek
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Kenus, the organizer of <em>International Euclidean Olympiad</em>, is seeking a pair of two integers that requires many steps to find its greatest common divisor using the Euclidean algorithm.</p> <p>You are given <var>Q</var> queries. The <var>i</var>-th query is represented as a pair of two integers <var>X_i</var> and <var>Y_i</var>, and asks you the following: among all pairs of two integers <var>(x,y)</var> such that <var>1 ≤ x ≤ X_i</var> and <var>1 ≤ y ≤ Y_i</var>, find the maximum <em>Euclidean step count</em> (defined below), and how many pairs have the maximum step count, modulo <var>10^9+7</var>.</p> <p>Process all the queries. Here, the Euclidean step count of a pair of two non-negative integers <var>(a,b)</var> is defined as follows:</p> <ul> <li><var>(a,b)</var> and <var>(b,a)</var> have the same Euclidean step count.</li> <li><var>(0,a)</var> has a Euclidean step count of <var>0</var>.</li> <li>If <var>a &gt; 0</var> and <var>a ≤ b</var>, let <var>p</var> and <var>q</var> be a unique pair of integers such that <var>b=pa+q</var> and <var>0 ≤ q &lt; a</var>. Then, the Euclidean step count of <var>(a,b)</var> is the Euclidean step count of <var>(q,a)</var> plus <var>1</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ Q ≤ 3 × 10^5</var></li> <li><var>1 ≤ X_i,Y_i ≤ 10^{18}(1 ≤ i ≤ Q)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>Q</var> <var>X_1</var> <var>Y_1</var> <var>:</var> <var>X_Q</var> <var>Y_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each query, print the maximum Euclidean step count, and the number of the pairs that have the maximum step count, modulo <var>10^9+7</var>, with a space in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 4 6 10 12 11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 4 4 1 4 7 </pre> <p>In the first query, <var>(2,3)</var>, <var>(3,2)</var>, <var>(3,4)</var> and <var>(4,3)</var> have a Euclidean step count of <var>2</var>. No pairs have a step count of more than <var>2</var>.</p> <p>In the second query, <var>(5,8)</var> has a Euclidean step count of <var>4</var>.</p> <p>In the third query, <var>(5,8)</var>, <var>(8,5)</var>, <var>(7,11)</var>, <var>(8,11)</var>, <var>(11,7)</var>, <var>(11,8)</var>, <var>(12,7)</var> have a Euclidean step count of <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 1 1 2 2 5 1000000000000000000 7 3 1 334334334334334334 23847657 23458792534 111111111 111111111 7 7 4 19 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 1 1 4 4 600000013 3 1 1 993994017 35 37447 38 2 3 6 3 9 4 2 </pre></section> </div> </span>
[ [ "3\n4 4\n6 10\n12 11\n", "3\n4 4\n6 10\n12 11\n" ] ]
p03711
AtCoder Beginner Contest 062 - Grouping
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Based on some criterion, Snuke divided the integers from <var>1</var> through <var>12</var> into three groups as shown in the figure below. Given two integers <var>x</var> and <var>y</var> (<var>1 ≤ x &lt; y ≤ 12</var>), determine whether they belong to the same group.</p> <div style="text-align: center;"> <img alt="b4ab979900ed647703389d4349eb84ee.png" src="https://atcoder.jp/img/arc074/b4ab979900ed647703389d4349eb84ee.png"> </img></div> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>x</var> and <var>y</var> are integers.</li> <li><var>1 ≤ x &lt; y ≤ 12</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>x</var> <var>y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>x</var> and <var>y</var> belong to the same group, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre></section> </div> </span>
[ [ "1 3\n", "1 3\n" ] ]
p03712
AtCoder Beginner Contest 062 - Picture Frame
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a image with a height of <var>H</var> pixels and a width of <var>W</var> pixels. Each pixel is represented by a lowercase English letter. The pixel at the <var>i</var>-th row from the top and <var>j</var>-th column from the left is <var>a_{ij}</var>.</p> <p>Put a box around this image and output the result. The box should consist of <code>#</code> and have a thickness of <var>1</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ H, W ≤ 100</var></li> <li><var>a_{ij}</var> is a lowercase English letter.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>a_{11}</var> <var>...</var> <var>a_{1W}</var> <var>:</var> <var>a_{H1}</var> <var>...</var> <var>a_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the image surrounded by a box that consists of <code>#</code> and has a thickness of <var>1</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 abc arc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>##### #abc# #arc# ##### </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 z </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>### #z# ### </pre></section> </div> </span>
[ [ "2 3\nabc\narc\n", "2 3\nabc\narc\n" ] ]
p03713
AtCoder Beginner Contest 062 - Chocolate Bar
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a bar of chocolate with a height of <var>H</var> blocks and a width of <var>W</var> blocks. Snuke is dividing this bar into exactly three pieces. He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.</p> <p>Snuke is trying to divide the bar as evenly as possible. More specifically, he is trying to minimize <var>S_{max}</var> - <var>S_{min}</var>, where <var>S_{max}</var> is the area (the number of blocks contained) of the largest piece, and <var>S_{min}</var> is the area of the smallest piece. Find the minimum possible value of <var>S_{max} - S_{min}</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ H, W ≤ 10^5</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible value of <var>S_{max} - S_{min}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>In the division below, <var>S_{max} - S_{min} = 5 - 5 = 0</var>.</p> <div style="text-align: center;"> <img alt="2a9b2ef47b750c0b7ba3e865d4fb4203.png" src="https://atcoder.jp/img/arc074/2a9b2ef47b750c0b7ba3e865d4fb4203.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>In the division below, <var>S_{max} - S_{min} = 8 - 6 = 2</var>.</p> <div style="text-align: center;"> <img alt="a42aae7aaaadc4640ac5cdf88684d913.png" src="https://atcoder.jp/img/arc074/a42aae7aaaadc4640ac5cdf88684d913.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre> <p>In the division below, <var>S_{max} - S_{min} = 10 - 6 = 4</var>.</p> <div style="text-align: center;"> <img alt="eb0ad0cb3185b7ae418e21c472ff7f26.png" src="https://atcoder.jp/img/arc074/eb0ad0cb3185b7ae418e21c472ff7f26.png"/> </div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>100000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>50000 </pre></section> </div> </span>
[ [ "3 5\n", "3 5\n" ] ]
p03714
AtCoder Beginner Contest 062 - 3N Numbers
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>N</var> be a positive integer.</p> <p>There is a numerical sequence of length <var>3N</var>, <var>a = (a_1, a_2, ..., a_{3N})</var>. Snuke is constructing a new sequence of length <var>2N</var>, <var>a'</var>, by removing exactly <var>N</var> elements from <var>a</var> without changing the order of the remaining elements. Here, the score of <var>a'</var> is defined as follows: <var>(</var>the sum of the elements in the first half of <var>a') - (</var>the sum of the elements in the second half of <var>a')</var>.</p> <p>Find the maximum possible score of <var>a'</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>a_i</var> is an integer.</li> <li><var>1 ≤ a_i ≤ 10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In the test set worth <var>300</var> points, <var>N ≤ 1000</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{3N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible score of <var>a'</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 4 1 5 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>When <var>a_2</var> and <var>a_6</var> are removed, <var>a'</var> will be <var>(3, 4, 1, 5)</var>, which has a score of <var>(3 + 4) - (1 + 5) = 1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>For example, when <var>a_1</var> are removed, <var>a'</var> will be <var>(2, 3)</var>, which has a score of <var>2 - 3 = -1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 8 2 2 7 4 6 5 3 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre> <p>For example, when <var>a_2</var>, <var>a_3</var> and <var>a_9</var> are removed, <var>a'</var> will be <var>(8, 7, 4, 6, 5, 3)</var>, which has a score of <var>(8 + 7 + 4) - (6 + 5 + 3) = 5</var>.</p></section> </div> </span>
[ [ "2\n3 1 4 1 5 9\n", "2\n3 1 4 1 5 9\n" ] ]
p03715
AtCoder Regular Contest 074 - Chocolate Bar
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a bar of chocolate with a height of <var>H</var> blocks and a width of <var>W</var> blocks. Snuke is dividing this bar into exactly three pieces. He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.</p> <p>Snuke is trying to divide the bar as evenly as possible. More specifically, he is trying to minimize <var>S_{max}</var> - <var>S_{min}</var>, where <var>S_{max}</var> is the area (the number of blocks contained) of the largest piece, and <var>S_{min}</var> is the area of the smallest piece. Find the minimum possible value of <var>S_{max} - S_{min}</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ H, W ≤ 10^5</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible value of <var>S_{max} - S_{min}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>In the division below, <var>S_{max} - S_{min} = 5 - 5 = 0</var>.</p> <div style="text-align: center;"> <img alt="2a9b2ef47b750c0b7ba3e865d4fb4203.png" src="https://atcoder.jp/img/arc074/2a9b2ef47b750c0b7ba3e865d4fb4203.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>In the division below, <var>S_{max} - S_{min} = 8 - 6 = 2</var>.</p> <div style="text-align: center;"> <img alt="a42aae7aaaadc4640ac5cdf88684d913.png" src="https://atcoder.jp/img/arc074/a42aae7aaaadc4640ac5cdf88684d913.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre> <p>In the division below, <var>S_{max} - S_{min} = 10 - 6 = 4</var>.</p> <div style="text-align: center;"> <img alt="eb0ad0cb3185b7ae418e21c472ff7f26.png" src="https://atcoder.jp/img/arc074/eb0ad0cb3185b7ae418e21c472ff7f26.png"/> </div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>100000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>50000 </pre></section> </div> </span>
[ [ "3 5\n", "3 5\n" ] ]
p03716
AtCoder Regular Contest 074 - 3N Numbers
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>N</var> be a positive integer.</p> <p>There is a numerical sequence of length <var>3N</var>, <var>a = (a_1, a_2, ..., a_{3N})</var>. Snuke is constructing a new sequence of length <var>2N</var>, <var>a'</var>, by removing exactly <var>N</var> elements from <var>a</var> without changing the order of the remaining elements. Here, the score of <var>a'</var> is defined as follows: <var>(</var>the sum of the elements in the first half of <var>a') - (</var>the sum of the elements in the second half of <var>a')</var>.</p> <p>Find the maximum possible score of <var>a'</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>a_i</var> is an integer.</li> <li><var>1 ≤ a_i ≤ 10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In the test set worth <var>300</var> points, <var>N ≤ 1000</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{3N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible score of <var>a'</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 4 1 5 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>When <var>a_2</var> and <var>a_6</var> are removed, <var>a'</var> will be <var>(3, 4, 1, 5)</var>, which has a score of <var>(3 + 4) - (1 + 5) = 1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>For example, when <var>a_1</var> are removed, <var>a'</var> will be <var>(2, 3)</var>, which has a score of <var>2 - 3 = -1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 8 2 2 7 4 6 5 3 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre> <p>For example, when <var>a_2</var>, <var>a_3</var> and <var>a_9</var> are removed, <var>a'</var> will be <var>(8, 7, 4, 6, 5, 3)</var>, which has a score of <var>(8 + 7 + 4) - (6 + 5 + 3) = 5</var>.</p></section> </div> </span>
[ [ "2\n3 1 4 1 5 9\n", "2\n3 1 4 1 5 9\n" ] ]
p03717
AtCoder Regular Contest 074 - RGB Sequence
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares arranged in a row. The squares are numbered <var>1</var>, <var>2</var>, <var>...</var>, <var>N</var>, from left to right.</p> <p>Snuke is painting each square in red, green or blue. According to his aesthetic sense, the following <var>M</var> conditions must all be satisfied. The <var>i</var>-th condition is:</p> <ul> <li>There are exactly <var>x_i</var> different colors among squares <var>l_i</var>, <var>l_i + 1</var>, <var>...</var>, <var>r_i</var>.</li> </ul> <p>In how many ways can the squares be painted to satisfy all the conditions? Find the count modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 300</var></li> <li><var>1 ≤ M ≤ 300</var></li> <li><var>1 ≤ l_i ≤ r_i ≤ N</var></li> <li><var>1 ≤ x_i ≤ 3</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>l_1</var> <var>r_1</var> <var>x_1</var> <var>l_2</var> <var>r_2</var> <var>x_2</var> <var>:</var> <var>l_M</var> <var>r_M</var> <var>x_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to paint the squares to satisfy all the conditions, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>The six ways are:</p> <ul> <li>RGB</li> <li>RBG</li> <li>GRB</li> <li>GBR</li> <li>BRG</li> <li>BGR</li> </ul> <p>where R, G and B correspond to red, green and blue squares, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 1 3 1 2 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 </pre> <p>The six ways are:</p> <ul> <li>RRRG</li> <li>RRRB</li> <li>GGGR</li> <li>GGGB</li> <li>BBBR</li> <li>BBBG</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 3 1 1 1 1 1 2 1 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>There are zero ways.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>8 10 2 6 2 5 5 1 3 5 2 4 7 3 4 4 1 2 3 1 7 7 1 1 5 2 1 7 3 3 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>108 </pre></section> </div> </span>
[ [ "3 1\n1 3 3\n", "3 1\n1 3 3\n" ] ]
p03718
AtCoder Regular Contest 074 - Lotus Leaves
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a pond with a rectangular shape. The pond is divided into a grid with <var>H</var> rows and <var>W</var> columns of squares. We will denote the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left by <var>(i,\ j)</var>.</p> <p>Some of the squares in the pond contains a lotus leaf floating on the water. On one of those leaves, <var>S</var>, there is a frog trying to get to another leaf <var>T</var>. The state of square <var>(i,\ j)</var> is given to you by a character <var>a_{ij}</var>, as follows:</p> <ul> <li><code>.</code> : A square without a leaf.</li> <li><code>o</code> : A square with a leaf floating on the water.</li> <li><code>S</code> : A square with the leaf <var>S</var>.</li> <li><code>T</code> : A square with the leaf <var>T</var>.</li> </ul> <p>The frog will repeatedly perform the following action to get to the leaf <var>T</var>: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."</p> <p>Snuke is trying to remove some of the leaves, other than <var>S</var> and <var>T</var>, so that the frog cannot get to the leaf <var>T</var>. Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ H, W ≤ 100</var></li> <li><var>a_{ij}</var> is <code>.</code>, <code>o</code>, <code>S</code> or <code>T</code>.</li> <li>There is exactly one <code>S</code> among <var>a_{ij}</var>.</li> <li>There is exactly one <code>T</code> among <var>a_{ij}</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>a_{11}</var> <var>...</var> <var>a_{1W}</var> <var>:</var> <var>a_{H1}</var> <var>...</var> <var>a_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the objective is achievable, print the minimum necessary number of leaves to remove. Otherwise, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 S.o .o. o.T </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Remove the upper-right and lower-left leaves.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 4 S... .oo. ...T </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 3 .S. .o. .o. .T. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 10 .o...o..o. ....o..... ....oo.oo. ..oooo..o. ....oo.... ..o..o.... o..o....So o....T.... ....o..... ........oo </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre></section> </div> </span>
[ [ "3 3\nS.o\n.o.\no.T\n", "3 3\nS.o\n.o.\no.T\n" ] ]
p03719
AtCoder Beginner Contest 061 - Between Two Integers
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three integers <var>A</var>, <var>B</var> and <var>C</var>. Determine whether <var>C</var> is not less than <var>A</var> and not greater than <var>B</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>-100≤A,B,C≤100</var> </li> <li><var>A</var>, <var>B</var> and <var>C</var> are all integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format: </p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the condition is satisfied, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p><var>C=2</var> is not less than <var>A=1</var> and not greater than <var>B=3</var>, and thus the output should be <code>Yes</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p><var>C=4</var> is less than <var>A=6</var>, and thus the output should be <code>No</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre></section> </div> </span>
[ [ "1 3 2\n", "1 3 2\n" ] ]
p03720
AtCoder Beginner Contest 061 - Counting Roads
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> cities and <var>M</var> roads. The <var>i</var>-th road <var>(1≤i≤M)</var> connects two cities <var>a_i</var> and <var>b_i</var> <var>(1≤a_i,b_i≤N)</var> bidirectionally. There may be more than one road that connects the same pair of two cities. For each city, how many roads are connected to the city?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N,M≤50</var></li> <li><var>1≤a_i,b_i≤N</var></li> <li><var>a_i ≠ b_i</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format: </p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_M</var> <var>b_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer in <var>N</var> lines. In the <var>i</var>-th line <var>(1≤i≤N)</var>, print the number of roads connected to city <var>i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 1 2 2 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 2 1 1 </pre> <ul> <li>City <var>1</var> is connected to the <var>1</var>-st and <var>3</var>-rd roads.</li> <li>City <var>2</var> is connected to the <var>1</var>-st and <var>2</var>-nd roads.</li> <li>City <var>3</var> is connected to the <var>2</var>-nd road.</li> <li>City <var>4</var> is connected to the <var>3</var>-rd road.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 5 1 2 2 1 1 2 2 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 8 1 2 3 4 1 5 2 8 3 7 5 2 4 1 6 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 3 2 2 2 1 1 2 </pre></section> </div> </span>
[ [ "4 3\n1 2\n2 3\n1 4\n", "4 3\n1 2\n2 3\n1 4\n" ] ]
p03721
AtCoder Beginner Contest 061 - Big Array
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is an empty array. The following <var>N</var> operations will be performed to insert integers into the array. In the <var>i</var>-th operation <var>(1≤i≤N)</var>, <var>b_i</var> copies of an integer <var>a_i</var> are inserted into the array. Find the <var>K</var>-th smallest integer in the array after the <var>N</var> operations. For example, the <var>4</var>-th smallest integer in the array <var>\{1,2,2,3,3,3\}</var> is <var>3</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^5</var> </li> <li><var>1≤a_i,b_i≤10^5</var> </li> <li><var>1≤K≤b_1…+…b_n</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format: </p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_N</var> <var>b_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the <var>K</var>-th smallest integer in the array after the <var>N</var> operations. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 1 1 2 2 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The resulting array is the same as the one in the problem statement.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 500000 1 100000 1 100000 1 100000 1 100000 1 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre></section> </div> </span>
[ [ "3 4\n1 1\n2 2\n3 3\n", "3 4\n1 1\n2 2\n3 3\n" ] ]
p03722
AtCoder Beginner Contest 061 - Score Attack
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a directed graph with <var>N</var> vertices and <var>M</var> edges. The <var>i</var>-th edge <var>(1≤i≤M)</var> points from vertex <var>a_i</var> to vertex <var>b_i</var>, and has a weight <var>c_i</var>. We will play the following single-player game using this graph and a piece.</p> <p>Initially, the piece is placed at vertex <var>1</var>, and the score of the player is set to <var>0</var>. The player can move the piece as follows:</p> <ul> <li>When the piece is placed at vertex <var>a_i</var>, move the piece along the <var>i</var>-th edge to vertex <var>b_i</var>. After this move, the score of the player is increased by <var>c_i</var>.</li> </ul> <p>The player can end the game only when the piece is placed at vertex <var>N</var>. The given graph guarantees that it is possible to traverse from vertex <var>1</var> to vertex <var>N</var>.</p> <p>When the player acts optimally to maximize the score at the end of the game, what will the score be? If it is possible to increase the score indefinitely, print <code>inf</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤1000</var> </li> <li><var>1≤M≤min(N(N-1),2000)</var> </li> <li><var>1≤a_i,b_i≤N (1≤i≤M)</var> </li> <li><var>a_i≠b_i (1≤i≤M)</var> </li> <li><var>a_i≠a_j</var> or <var>b_i≠b_j (1≤i&lt;j≤M)</var> </li> <li><var>-10^9≤c_i≤10^9 (1≤i≤M)</var></li> <li><var>c_i</var> is an integer.</li> <li>In the given graph, there exists a path from vertex <var>1</var> to vertex <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format: </p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>a_2</var> <var>b_2</var> <var>c_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> <var>c_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print <code>inf</code>. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 4 2 3 3 1 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>There are two ways to move the piece to vertex <var>N=3</var>:</p> <ul> <li>vertex <var>1</var> → vertex <var>2</var> → vertex <var>3</var> : score <var>4+3=7</var></li> <li>vertex <var>1</var> → vertex <var>3</var> : score <var>5</var></li> </ul> <p>Thus, the maximum possible score at the end of the game is <var>7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 1 2 1 2 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>inf </pre> <p>It is possible to increase the score indefinitely by alternating between vertex <var>1</var> and <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 5 1 2 -1000000000 2 3 -1000000000 3 4 -1000000000 4 5 -1000000000 5 6 -1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-5000000000 </pre></section> </div> </span>
[ [ "3 3\n1 2 4\n2 3 3\n1 3 5\n", "3 3\n1 2 4\n2 3 3\n1 3 5\n" ] ]
p03723
AtCoder Grand Contest 014 - Cookie Exchanges
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi, Aoki and Snuke love cookies. They have <var>A</var>, <var>B</var> and <var>C</var> cookies, respectively. Now, they will exchange those cookies by repeating the action below:</p> <ul> <li>Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.</li> </ul> <p>This action will be repeated until there is a person with odd number of cookies in hand.</p> <p>How many times will they repeat this action? Note that the answer may not be finite.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ A,B,C ≤ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of times the action will be performed by the three people, if this number is finite. If it is infinite, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 12 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>Initially, Takahashi, Aoki and Snuke have <var>4</var>, <var>12</var> and <var>20</var> cookies. Then,</p> <ul> <li>After the first action, they have <var>16</var>, <var>12</var> and <var>8</var>.</li> <li>After the second action, they have <var>10</var>, <var>12</var> and <var>14</var>.</li> <li>After the third action, they have <var>13</var>, <var>12</var> and <var>11</var>.</li> </ul> <p>Now, Takahashi and Snuke have odd number of cookies, and therefore the answer is <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>14 14 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>454 414 444 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre></section> </div> </span>
[ [ "4 12 20\n", "4 12 20\n" ] ]
p03724
AtCoder Grand Contest 014 - Unplanned Queries
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is not good at problems about trees in programming contests, and Aoki is helping him practice.</p> <p>First, Takahashi created a tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>, and wrote <var>0</var> at each edge.</p> <p>Then, Aoki gave him <var>M</var> queries. The <var>i</var>-th of them is as follows:</p> <ul> <li>Increment the number written at each edge along the path connecting vertices <var>a_i</var> and <var>b_i</var>, by one.</li> </ul> <p>After Takahashi executed all of the queries, he told Aoki that, for every edge, the written number became an even number. However, Aoki forgot to confirm that the graph Takahashi created was actually a tree, and it is possible that Takahashi made a mistake in creating a tree or executing queries.</p> <p>Determine whether there exists a tree that has the property mentioned by Takahashi.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ M ≤ 10^5</var></li> <li><var>1 ≤ a_i,b_i ≤ N</var></li> <li><var>a_i ≠ b_i</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> : <var>a_M</var> <var>b_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> if there exists a tree that has the property mentioned by Takahashi; print <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 4 1 2 2 4 1 3 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>For example, Takahashi's graph has the property mentioned by him if it has the following edges: <var>1-2</var>, <var>1-3</var> and <var>1-4</var>. In this case, the number written at every edge will become <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 5 1 2 3 5 5 1 3 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre></section> </div> </span>
[ [ "4 4\n1 2\n2 4\n1 3\n3 4\n", "4 4\n1 2\n2 4\n1 3\n3 4\n" ] ]
p03725
AtCoder Grand Contest 014 - Closed Rooms
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi is locked within a building.</p> <p>This building consists of <var>H×W</var> rooms, arranged in <var>H</var> rows and <var>W</var> columns. We will denote the room at the <var>i</var>-th row and <var>j</var>-th column as <var>(i,j)</var>. The state of this room is represented by a character <var>A_{i,j}</var>. If <var>A_{i,j}=</var> <code>#</code>, the room is locked and cannot be entered; if <var>A_{i,j}=</var> <code>.</code>, the room is not locked and can be freely entered. Takahashi is currently at the room where <var>A_{i,j}=</var> <code>S</code>, which can also be freely entered.</p> <p>Each room in the <var>1</var>-st row, <var>1</var>-st column, <var>H</var>-th row or <var>W</var>-th column, has an exit. Each of the other rooms <var>(i,j)</var> is connected to four rooms: <var>(i-1,j)</var>, <var>(i+1,j)</var>, <var>(i,j-1)</var> and <var>(i,j+1)</var>.</p> <p>Takahashi will use his magic to get out of the building. In one cast, he can do the following:</p> <ul> <li>Move to an adjacent room at most <var>K</var> times, possibly zero. Here, locked rooms cannot be entered.</li> <li>Then, select and unlock at most <var>K</var> locked rooms, possibly zero. Those rooms will remain unlocked from then on.</li> </ul> <p>His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.</p> <p>It is guaranteed that Takahashi is initially at a room without an exit.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 ≤ H ≤ 800</var></li> <li><var>3 ≤ W ≤ 800</var></li> <li><var>1 ≤ K ≤ H×W</var></li> <li>Each <var>A_{i,j}</var> is <code>#</code> , <code>.</code> or <code>S</code>.</li> <li>There uniquely exists <var>(i,j)</var> such that <var>A_{i,j}=</var> <code>S</code>, and it satisfies <var>2 ≤ i ≤ H-1</var> and <var>2 ≤ j ≤ W-1</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>K</var> <var>A_{1,1}A_{1,2}</var>...<var>A_{1,W}</var> : <var>A_{H,1}A_{H,2}</var>...<var>A_{H,W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum necessary number of casts.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 3 #.# #S. ### </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>Takahashi can move to room <var>(1,2)</var> in one cast.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 3 ### #S# ### </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>Takahashi cannot move in the first cast, but can unlock room <var>(1,2)</var>. Then, he can move to room <var>(1,2)</var> in the next cast, achieving the objective in two casts.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 7 2 ####### ####### ##...## ###S### ##.#.## ###.### ####### </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre></section> </div> </span>
[ [ "3 3 3\n#.#\n#S.\n###\n", "3 3 3\n#.#\n#S.\n###\n" ] ]
p03726
AtCoder Grand Contest 014 - Black and White Tree
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. The <var>i</var>-th of the <var>N-1</var> edges connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>Initially, each vertex is uncolored.</p> <p>Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:</p> <ul> <li>Select a vertex that is not painted yet.</li> <li>If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.</li> </ul> <p>Then, after all the vertices are colored, the following procedure takes place:</p> <ul> <li>Repaint every white vertex that is adjacent to a black vertex, in black.</li> </ul> <p>Note that all such white vertices are repainted simultaneously, not one at a time.</p> <p>If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins. Determine the winner of the game, assuming that both persons play optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ a_i,b_i ≤ N</var></li> <li><var>a_i ≠ b_i</var></li> <li>The input graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> : <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>First</code> if Takahashi wins; print <code>Second</code> if Aoki wins.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>First </pre> <p>Below is a possible progress of the game:</p> <ul> <li>First, Takahashi paint vertex <var>2</var> white.</li> <li>Then, Aoki paint vertex <var>1</var> black.</li> <li>Lastly, Takahashi paint vertex <var>3</var> white.</li> </ul> <p>In this case, the colors of vertices <var>1</var>, <var>2</var> and <var>3</var> after the final procedure are black, black and white, resulting in Takahashi's victory.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 2 2 3 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>First </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 2 2 3 3 4 2 5 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Second </pre></section> </div> </span>
[ [ "3\n1 2\n2 3\n", "3\n1 2\n2 3\n" ] ]
p03727
AtCoder Grand Contest 014 - Blue and Red Tree
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. The <var>i</var>-th of the <var>N-1</var> edges connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>Initially, each edge is painted blue. Takahashi will convert this blue tree into a red tree, by performing the following operation <var>N-1</var> times:</p> <ul> <li>Select a simple path that consists of only blue edges, and remove one of those edges.</li> <li>Then, span a new red edge between the two endpoints of the selected path.</li> </ul> <p>His objective is to obtain a tree that has a red edge connecting vertices <var>c_i</var> and <var>d_i</var>, for each <var>i</var>.</p> <p>Determine whether this is achievable.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ a_i,b_i,c_i,d_i ≤ N</var></li> <li><var>a_i ≠ b_i</var></li> <li><var>c_i ≠ d_i</var></li> <li>Both input graphs are trees.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> : <var>a_{N-1}</var> <var>b_{N-1}</var> <var>c_1</var> <var>d_1</var> : <var>c_{N-1}</var> <var>d_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> if the objective is achievable; print <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 2 3 1 3 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>The objective is achievable, as below:</p> <ul> <li>First, select the path connecting vertices <var>1</var> and <var>3</var>, and remove a blue edge <var>1-2</var>. Then, span a new red edge <var>1-3</var>.</li> <li>Next, select the path connecting vertices <var>2</var> and <var>3</var>, and remove a blue edge <var>2-3</var>. Then, span a new red edge <var>2-3</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 2 2 3 3 4 4 5 3 4 2 4 1 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>YES </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 2 3 5 4 6 1 6 5 1 5 3 1 4 2 6 4 3 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre></section> </div> </span>
[ [ "3\n1 2\n2 3\n1 3\n3 2\n", "3\n1 2\n2 3\n1 3\n3 2\n" ] ]
p03728
AtCoder Grand Contest 014 - Strange Sorting
<span class="lang-en"> <p>Score : <var>2400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi loves sorting.</p> <p>He has a permutation <var>(p_1,p_2,...,p_N)</var> of the integers from <var>1</var> through <var>N</var>. Now, he will repeat the following operation until the permutation becomes <var>(1,2,...,N)</var>:</p> <ul> <li>First, we will define <em>high</em> and <em>low</em> elements in the permutation, as follows. The <var>i</var>-th element in the permutation is high if the maximum element between the <var>1</var>-st and <var>i</var>-th elements, inclusive, is the <var>i</var>-th element itself, and otherwise the <var>i</var>-th element is low.</li> <li>Then, let <var>a_1,a_2,...,a_k</var> be the values of the high elements, and <var>b_1,b_2,...,b_{N-k}</var> be the values of the low elements in the current permutation, <strong>in the order they appear in it</strong>.</li> <li>Lastly, rearrange the permutation into <var>(b_1,b_2,...,b_{N-k},a_1,a_2,...,a_k)</var>.</li> </ul> <p>How many operations are necessary until the permutation is sorted?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 2×10^5</var></li> <li><var>(p_1,p_2,...,p_N)</var> is a permutation of the integers from <var>1</var> through <var>N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>p_1</var> <var>p_2</var> ... <var>p_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of operations that are necessary until the permutation is sorted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 5 1 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The initial permutation is <var>(3,5,1,2,4)</var>, and each operation changes it as follows:</p> <ul> <li>In the first operation, the <var>1</var>-st and <var>2</var>-nd elements are high, and the <var>3</var>-rd, <var>4</var>-th and <var>5</var>-th are low. The permutation becomes: <var>(1,2,4,3,5)</var>.</li> <li>In the second operation, the <var>1</var>-st, <var>2</var>-nd, <var>3</var>-rd and <var>5</var>-th elements are high, and the <var>4</var>-th is low. The permutation becomes: <var>(3,1,2,4,5)</var>.</li> <li>In the third operation, the <var>1</var>-st, <var>4</var>-th and <var>5</var>-th elements are high, and the <var>2</var>-nd and <var>3</var>-rd and <var>5</var>-th are low. The permutation becomes: <var>(1,2,3,4,5)</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 5 4 3 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 2 10 5 7 3 6 4 9 8 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6 </pre></section> </div> </span>
[ [ "5\n3 5 1 2 4\n", "5\n3 5 1 2 4\n" ] ]
p03729
AtCoder Beginner Contest 060 - Shiritori
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three strings <var>A</var>, <var>B</var> and <var>C</var>. Check whether they form a <em>word chain</em>.</p> <p>More formally, determine whether both of the following are true:</p> <ul> <li>The last character in <var>A</var> and the initial character in <var>B</var> are the same.</li> <li>The last character in <var>B</var> and the initial character in <var>C</var> are the same.</li> </ul> <p>If both are true, print <code>YES</code>. Otherwise, print <code>NO</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>A</var>, <var>B</var> and <var>C</var> are all composed of lowercase English letters (<code>a</code> - <code>z</code>).</li> <li><var>1 ≤ |A|, |B|, |C| ≤ 10</var>, where <var>|A|</var>, <var>|B|</var> and <var>|C|</var> are the lengths of <var>A</var>, <var>B</var> and <var>C</var>, respectively.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> or <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>rng gorilla apple </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>They form a word chain.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>yakiniku unagi sushi </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> <p><var>A</var> and <var>B</var> form a word chain, but <var>B</var> and <var>C</var> do not.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>a a a </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>aaaaaaaaab aaaaaaaaaa aaaaaaaaab </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>NO </pre></section> </div> </span>
[ [ "rng gorilla apple\n", "rng gorilla apple\n" ] ]
p03730
AtCoder Beginner Contest 060 - Choose Integers
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We ask you to select some number of positive integers, and calculate the sum of them.</p> <p>It is allowed to select as many integers as you like, and as large integers as you wish. You have to follow these, however: each selected integer needs to be a multiple of <var>A</var>, and you need to select at least one integer.</p> <p>Your objective is to make the sum congruent to <var>C</var> modulo <var>B</var>. Determine whether this is possible.</p> <p>If the objective is achievable, print <code>YES</code>. Otherwise, print <code>NO</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ A ≤ 100</var></li> <li><var>1 ≤ B ≤ 100</var></li> <li><var>0 ≤ C &lt; B</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> or <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>For example, if you select <var>7</var> and <var>14</var>, the sum <var>21</var> is congruent to <var>1</var> modulo <var>5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> <p>The sum of even numbers, no matter how many, is never odd.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 100 97 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre> <p>You can select <var>97</var>, since you may select multiples of <var>1</var>, that is, all integers.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>40 98 58 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>YES </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>77 42 36 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>NO </pre></section> </div> </span>
[ [ "7 5 1\n", "7 5 1\n" ] ]
p03731
AtCoder Beginner Contest 060 - Sentou
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In a public bath, there is a shower which emits water for <var>T</var> seconds when the switch is pushed.</p> <p>If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for <var>T</var> seconds. Note that it does not mean that the shower emits water for <var>T</var> additional seconds.</p> <p><var>N</var> people will push the switch while passing by the shower. The <var>i</var>-th person will push the switch <var>t_i</var> seconds after the first person pushes it.</p> <p>How long will the shower emit water in total?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 200,000</var></li> <li><var>1 ≤ T ≤ 10^9</var></li> <li><var>0 = t_1 &lt; t_2 &lt; t_3 &lt; , ..., &lt; t_{N-1} &lt; t_N ≤ 10^9</var></li> <li><var>T</var> and each <var>t_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>T</var> <var>t_1</var> <var>t_2</var> ... <var>t_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Assume that the shower will emit water for a total of <var>X</var> seconds. Print <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 0 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 4 0 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8 </pre> <p>One second after the shower stops emission of water triggered by the first person, the switch is pushed again.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 1000000000 0 1000 1000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2000000000 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>9 10 0 3 5 7 100 110 200 300 311 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>67 </pre></section> </div> </span>
[ [ "2 4\n0 3\n", "2 4\n0 3\n" ] ]
p03732
AtCoder Beginner Contest 060 - Simple Knapsack
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have <var>N</var> items and a bag of strength <var>W</var>. The <var>i</var>-th item has a weight of <var>w_i</var> and a value of <var>v_i</var>.</p> <p>You will select some of the items and put them in the bag. Here, the total weight of the selected items needs to be at most <var>W</var>.</p> <p>Your objective is to maximize the total value of the selected items.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 100</var></li> <li><var>1 ≤ W ≤ 10^9</var></li> <li><var>1 ≤ w_i ≤ 10^9</var></li> <li>For each <var>i = 2,3,...,N</var>, <var>w_1 ≤ w_i ≤ w_1 + 3</var>.</li> <li><var>1 ≤ v_i ≤ 10^7</var></li> <li><var>W</var>, each <var>w_i</var> and <var>v_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>W</var> <var>w_1</var> <var>v_1</var> <var>w_2</var> <var>v_2</var> : <var>w_N</var> <var>v_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible total value of the selected items.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 6 2 1 3 4 4 10 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 </pre> <p>The first and third items should be selected.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 6 2 1 3 7 4 10 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>13 </pre> <p>The second and fourth items should be selected.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 10 1 100 1 100 1 100 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>400 </pre> <p>You can take everything.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 1 10 100 10 100 10 100 10 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>You can take nothing.</p></section> </div> </span>
[ [ "4 6\n2 1\n3 4\n4 10\n3 4\n", "4 6\n2 1\n3 4\n4 10\n3 4\n" ] ]
p03733
AtCoder Regular Contest 073 - Sentou
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In a public bath, there is a shower which emits water for <var>T</var> seconds when the switch is pushed.</p> <p>If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for <var>T</var> seconds. Note that it does not mean that the shower emits water for <var>T</var> additional seconds.</p> <p><var>N</var> people will push the switch while passing by the shower. The <var>i</var>-th person will push the switch <var>t_i</var> seconds after the first person pushes it.</p> <p>How long will the shower emit water in total?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 200,000</var></li> <li><var>1 ≤ T ≤ 10^9</var></li> <li><var>0 = t_1 &lt; t_2 &lt; t_3 &lt; , ..., &lt; t_{N-1} &lt; t_N ≤ 10^9</var></li> <li><var>T</var> and each <var>t_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>T</var> <var>t_1</var> <var>t_2</var> ... <var>t_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Assume that the shower will emit water for a total of <var>X</var> seconds. Print <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 0 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 4 0 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8 </pre> <p>One second after the shower stops emission of water triggered by the first person, the switch is pushed again.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 1000000000 0 1000 1000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2000000000 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>9 10 0 3 5 7 100 110 200 300 311 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>67 </pre></section> </div> </span>
[ [ "2 4\n0 3\n", "2 4\n0 3\n" ] ]
p03734
AtCoder Regular Contest 073 - Simple Knapsack
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have <var>N</var> items and a bag of strength <var>W</var>. The <var>i</var>-th item has a weight of <var>w_i</var> and a value of <var>v_i</var>.</p> <p>You will select some of the items and put them in the bag. Here, the total weight of the selected items needs to be at most <var>W</var>.</p> <p>Your objective is to maximize the total value of the selected items.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 100</var></li> <li><var>1 ≤ W ≤ 10^9</var></li> <li><var>1 ≤ w_i ≤ 10^9</var></li> <li>For each <var>i = 2,3,...,N</var>, <var>w_1 ≤ w_i ≤ w_1 + 3</var>.</li> <li><var>1 ≤ v_i ≤ 10^7</var></li> <li><var>W</var>, each <var>w_i</var> and <var>v_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>W</var> <var>w_1</var> <var>v_1</var> <var>w_2</var> <var>v_2</var> : <var>w_N</var> <var>v_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible total value of the selected items.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 6 2 1 3 4 4 10 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 </pre> <p>The first and third items should be selected.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 6 2 1 3 7 4 10 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>13 </pre> <p>The second and fourth items should be selected.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 10 1 100 1 100 1 100 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>400 </pre> <p>You can take everything.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 1 10 100 10 100 10 100 10 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>You can take nothing.</p></section> </div> </span>
[ [ "4 6\n2 1\n3 4\n4 10\n3 4\n", "4 6\n2 1\n3 4\n4 10\n3 4\n" ] ]
p03735
AtCoder Regular Contest 073 - Ball Coloring
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> bags, each containing two white balls. The <var>i</var>-th box contains two balls with integers <var>x_i</var> and <var>y_i</var> written on them, respectively.</p> <p>For each of these bags, you will paint one of the balls red, and paint the other blue.</p> <p>Afterwards, the <var>2N</var> balls will be classified according to color.</p> <p>Then, we will define the following:</p> <ul> <li><var>R_{max}</var>: the maximum integer written on a ball painted in red</li> <li><var>R_{min}</var>: the minimum integer written on a ball painted in red</li> <li><var>B_{max}</var>: the maximum integer written on a ball painted in blue</li> <li><var>B_{min}</var>: the minimum integer written on a ball painted in blue</li> </ul> <p>Find the minimum possible value of <var>(R_{max} - R_{min}) \times (B_{max} - B_{min})</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 200,000</var></li> <li><var>1 ≤ x_i, y_i ≤ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> : <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible value.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>15 </pre> <p>The optimal solution is to paint the balls with <var>x_1</var>, <var>x_2</var>, <var>y_3</var> red, and paint the balls with <var>y_1</var>, <var>y_2</var>, <var>x_3</var> blue.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1010 10 1000 1 20 1020 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>380 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 1 1 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>999999998000000001 </pre></section> </div> </span>
[ [ "3\n1 2\n3 4\n5 6\n", "3\n1 2\n3 4\n5 6\n" ] ]
p03736
AtCoder Regular Contest 073 - Many Moves
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares in a row. The squares are numbered <var>1, 2, ..., N</var> from left to right.</p> <p>You have two pieces, initially placed on square <var>A</var> and <var>B</var>, respectively. You will be asked to process <var>Q</var> queries of the following kind, in the order received:</p> <ul> <li>Given an integer <var>x_i</var>, move one of the two pieces of your choice to square <var>x_i</var>.</li> </ul> <p>Here, it takes you one second to move a piece one square. That is, the time it takes to move a piece from square <var>X</var> to <var>Y</var> is <var>|X-Y|</var> seconds.</p> <p>Your objective is to process all the queries in the shortest possible time.</p> <p>You may only move the pieces in response to queries, and you may not move both pieces at the same time. Also, it is not allowed to rearrange the order in which queries are given. It is, however, allowed to have both pieces in the same square at the same time.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N, Q ≤ 200,000</var></li> <li><var>1 ≤ A, B ≤ N</var></li> <li><var>1 ≤ x_i ≤ N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>Q</var> <var>A</var> <var>B</var> <var>x_1</var> <var>x_2</var> ... <var>x_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Let the shortest possible time to process all the queries be <var>X</var> seconds. Print <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 3 1 8 3 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>All the queries can be processed in seven seconds, by:</p> <ul> <li>moving the piece at square <var>1</var> to <var>3</var></li> <li>moving the piece at square <var>8</var> to <var>5</var></li> <li>moving the piece at square <var>3</var> to <var>1</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9 2 1 9 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> <p>The piece at square <var>9</var> should be moved first.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9 2 1 9 5 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre> <p>The piece at square <var>1</var> should be moved first.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>11 16 8 1 1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>21 </pre></section> </div> </span>
[ [ "8 3 1 8\n3 5 1\n", "8 3 1 8\n3 5 1\n" ] ]
p03737
AtCoder Beginner Contest 059 - Three-letter acronym
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three words <var>s_1</var>, <var>s_2</var> and <var>s_3</var>, each composed of lowercase English letters, with spaces in between. Print the acronym formed from the uppercased initial letters of the words.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>s_1</var>, <var>s_2</var> and <var>s_3</var> are composed of lowercase English letters.</li> <li><var>1 ≤ |s_i| ≤ 10 (1≤i≤3)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>s_1</var> <var>s_2</var> <var>s_3</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>atcoder beginner contest </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>ABC </pre> <p>The initial letters of <code>atcoder</code>, <code>beginner</code> and <code>contest</code> are <code>a</code>, <code>b</code> and <code>c</code>. Uppercase and concatenate them to obtain <code>ABC</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>resident register number </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>RRN </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>k nearest neighbor </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>KNN </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>async layered coding </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>ALC </pre></section> </div> </span>
[ [ "atcoder beginner contest\n", "atcoder beginner contest\n" ] ]
p03738
AtCoder Beginner Contest 059 - Comparison
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two positive integers <var>A</var> and <var>B</var>. Compare the magnitudes of these numbers.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ A, B ≤ 10^{100}</var></li> <li>Neither <var>A</var> nor <var>B</var> begins with a <code>0</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>GREATER</code> if <var>A&gt;B</var>, <code>LESS</code> if <var>A&lt;B</var> and <code>EQUAL</code> if <var>A=B</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>36 24 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>GREATER </pre> <p>Since <var>36&gt;24</var>, print <code>GREATER</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>850 3777 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>LESS </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9720246 22516266 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>LESS </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>123456789012345678901234567890 234567890123456789012345678901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>LESS </pre></section> </div> </span>
[ [ "36\n24\n", "36\n24\n" ] ]
p03739
AtCoder Beginner Contest 059 - Sequence
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>N</var>. The <var>i</var>-th term in the sequence is <var>a_i</var>. In one operation, you can select a term and either increment or decrement it by one.</p> <p>At least how many operations are necessary to satisfy the following conditions?</p> <ul> <li>For every <var>i</var> <var>(1≤i≤n)</var>, the sum of the terms from the <var>1</var>-st through <var>i</var>-th term is not zero.</li> <li>For every <var>i</var> <var>(1≤i≤n-1)</var>, the sign of the sum of the terms from the <var>1</var>-st through <var>i</var>-th term, is different from the sign of the sum of the terms from the <var>1</var>-st through <var>(i+1)</var>-th term.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ n ≤ 10^5</var></li> <li><var>|a_i| ≤ 10^9</var></li> <li>Each <var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum necessary count of operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 -3 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>For example, the given sequence can be transformed into <var>1, -2, 2, -2</var> by four operations. The sums of the first one, two, three and four terms are <var>1, -1, 1</var> and <var>-1</var>, respectively, which satisfy the conditions.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 3 -6 4 -5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The given sequence already satisfies the conditions.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 -1 4 3 2 -5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8 </pre></section> </div> </span>
[ [ "4\n1 -3 1 0\n", "4\n1 -3 1 0\n" ] ]
p03740
AtCoder Beginner Contest 059 - Alice&Brown
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice and Brown loves games. Today, they will play the following game.</p> <p>In this game, there are two piles initially consisting of <var>X</var> and <var>Y</var> stones, respectively. Alice and Bob alternately perform the following operation, starting from Alice:</p> <ul> <li>Take <var>2i</var> stones from one of the piles. Then, throw away <var>i</var> of them, and put the remaining <var>i</var> in the other pile. Here, the integer <var>i</var> <var>(1≤i)</var> can be freely chosen as long as there is a sufficient number of stones in the pile.</li> </ul> <p>The player who becomes unable to perform the operation, loses the game.</p> <p>Given <var>X</var> and <var>Y</var>, determine the winner of the game, assuming that both players play optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 ≤ X, Y ≤ 10^{18}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the winner: either <code>Alice</code> or <code>Brown</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Brown </pre> <p>Alice can do nothing but taking two stones from the pile containing two stones. As a result, the piles consist of zero and two stones, respectively. Then, Brown will take the two stones, and the piles will consist of one and zero stones, respectively. Alice will be unable to perform the operation anymore, which means Brown's victory.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Alice </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Brown </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Alice </pre></section> </div> </span>
[ [ "2 1\n", "2 1\n" ] ]
p03741
AtCoder Regular Contest 072 - Sequence
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>N</var>. The <var>i</var>-th term in the sequence is <var>a_i</var>. In one operation, you can select a term and either increment or decrement it by one.</p> <p>At least how many operations are necessary to satisfy the following conditions?</p> <ul> <li>For every <var>i</var> <var>(1≤i≤n)</var>, the sum of the terms from the <var>1</var>-st through <var>i</var>-th term is not zero.</li> <li>For every <var>i</var> <var>(1≤i≤n-1)</var>, the sign of the sum of the terms from the <var>1</var>-st through <var>i</var>-th term, is different from the sign of the sum of the terms from the <var>1</var>-st through <var>(i+1)</var>-th term.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ n ≤ 10^5</var></li> <li><var>|a_i| ≤ 10^9</var></li> <li>Each <var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum necessary count of operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 -3 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>For example, the given sequence can be transformed into <var>1, -2, 2, -2</var> by four operations. The sums of the first one, two, three and four terms are <var>1, -1, 1</var> and <var>-1</var>, respectively, which satisfy the conditions.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 3 -6 4 -5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The given sequence already satisfies the conditions.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 -1 4 3 2 -5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8 </pre></section> </div> </span>
[ [ "4\n1 -3 1 0\n", "4\n1 -3 1 0\n" ] ]
p03742
AtCoder Regular Contest 072 - Alice&Brown
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice and Brown loves games. Today, they will play the following game.</p> <p>In this game, there are two piles initially consisting of <var>X</var> and <var>Y</var> stones, respectively. Alice and Bob alternately perform the following operation, starting from Alice:</p> <ul> <li>Take <var>2i</var> stones from one of the piles. Then, throw away <var>i</var> of them, and put the remaining <var>i</var> in the other pile. Here, the integer <var>i</var> <var>(1≤i)</var> can be freely chosen as long as there is a sufficient number of stones in the pile.</li> </ul> <p>The player who becomes unable to perform the operation, loses the game.</p> <p>Given <var>X</var> and <var>Y</var>, determine the winner of the game, assuming that both players play optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 ≤ X, Y ≤ 10^{18}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the winner: either <code>Alice</code> or <code>Brown</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Brown </pre> <p>Alice can do nothing but taking two stones from the pile containing two stones. As a result, the piles consist of zero and two stones, respectively. Then, Brown will take the two stones, and the piles will consist of one and zero stones, respectively. Alice will be unable to perform the operation anymore, which means Brown's victory.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Alice </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Brown </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Alice </pre></section> </div> </span>
[ [ "2 1\n", "2 1\n" ] ]
p03743
AtCoder Regular Contest 072 - Alice in linear land
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice lives on a line. Today, she will travel to some place in a mysterious vehicle. Initially, the distance between Alice and her destination is <var>D</var>. When she input a number <var>x</var> to the vehicle, it will travel in the direction of the destination by a distance of <var>x</var> if this move would shorten the distance between the vehicle and the destination, and it will stay at its position otherwise. Note that the vehicle may go past the destination when the distance between the vehicle and the destination is less than <var>x</var>.</p> <p>Alice made a list of <var>N</var> numbers. The <var>i</var>-th number in this list is <var>d_i</var>. She will insert these numbers to the vehicle one by one.</p> <p>However, a mischievous witch appeared. She is thinking of rewriting one number in the list so that Alice will not reach the destination after <var>N</var> moves.</p> <p>She has <var>Q</var> plans to do this, as follows:</p> <ul> <li>Rewrite only the <var>q_i</var>-th number in the list with some integer so that Alice will not reach the destination.</li> </ul> <p>Write a program to determine whether each plan is feasible.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤ N ≤ 5*10^5</var></li> <li><var>1≤ Q ≤ 5*10^5</var></li> <li><var>1≤ D ≤ 10^9</var></li> <li><var>1≤ d_i ≤ 10^9(1≤i≤N)</var></li> <li><var>1≤ q_i ≤ N(1≤i≤Q)</var></li> <li>D and each <var>d_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>D</var> <var>d_1</var> <var>d_2</var> <var>...</var> <var>d_N</var> <var>Q</var> <var>q_1</var> <var>q_2</var> <var>...</var> <var>q_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line should contain <code>YES</code> if the <var>i</var>-th plan is feasible, and <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 10 3 4 3 3 2 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>NO YES </pre> <p>For the first plan, Alice will already arrive at the destination by the first three moves, and therefore the answer is <code>NO</code>. For the second plan, rewriting the third number in the list with <var>5</var> will prevent Alice from reaching the destination as shown in the following figure, and thus the answer is <code>YES</code>.</p> <p><img alt="" src="https://atcoder.jp/img/arc072/f6a4307ef86847bc8fa68d0952f7127c.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 9 4 4 2 3 2 5 1 4 2 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>YES YES YES YES YES </pre> <p>Alice will not reach the destination as it is, and therefore all the plans are feasible.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 15 4 3 5 4 2 1 6 1 2 3 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO NO YES NO NO YES </pre></section> </div> </span>
[ [ "4 10\n3 4 3 3\n2\n4 3\n", "4 10\n3 4 3 3\n2\n4 3\n" ] ]
p03744
AtCoder Regular Contest 072 - Dam
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are in charge of controlling a dam. The dam can store at most <var>L</var> liters of water. Initially, the dam is empty. Some amount of water flows into the dam every morning, and any amount of water may be discharged every night, but this amount needs to be set so that no water overflows the dam the next morning.</p> <p>It is known that <var>v_i</var> liters of water at <var>t_i</var> degrees Celsius will flow into the dam on the morning of the <var>i</var>-th day. You are wondering about the maximum possible temperature of water in the dam at noon of each day, under the condition that there needs to be exactly <var>L</var> liters of water in the dam at that time. For each <var>i</var>, find the maximum possible temperature of water in the dam at noon of the <var>i</var>-th day. Here, consider each maximization separately, that is, the amount of water discharged for the maximization of the temperature on the <var>i</var>-th day, may be different from the amount of water discharged for the maximization of the temperature on the <var>j</var>-th day <var>(j≠i)</var>.</p> <p>Also, assume that the temperature of water is not affected by anything but new water that flows into the dam. That is, when <var>V_1</var> liters of water at <var>T_1</var> degrees Celsius and <var>V_2</var> liters of water at <var>T_2</var> degrees Celsius are mixed together, they will become <var>V_1+V_2</var> liters of water at <var>\frac{T_1*V_1+T_2*V_2}{V_1+V_2}</var> degrees Celsius, and the volume and temperature of water are not affected by any other factors.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤ N ≤ 5*10^5</var></li> <li><var>1≤ L ≤ 10^9</var></li> <li><var>0≤ t_i ≤ 10^9(1≤i≤N)</var></li> <li><var>1≤ v_i ≤ L(1≤i≤N)</var></li> <li><var>v_1 = L</var></li> <li><var>L</var>, each <var>t_i</var> and <var>v_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>L</var> <var>t_1</var> <var>v_1</var> <var>t_2</var> <var>v_2</var> <var>:</var> <var>t_N</var> <var>v_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line should contain the maximum temperature such that it is possible to store <var>L</var> liters of water at that temperature in the dam at noon of the <var>i</var>-th day.</p> <p>Each of these values is accepted if the absolute or relative error is at most <var>10^{-6}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 10 10 10 20 5 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10.0000000 15.0000000 13.2000000 </pre> <ul> <li> <p>On the first day, the temperature of water in the dam is always <var>10</var> degrees: the temperature of the only water that flows into the dam on the first day.</p> </li> <li> <p><var>10</var> liters of water at <var>15</var> degrees of Celsius can be stored on the second day, by discharging <var>5</var> liters of water on the night of the first day, and mix the remaining water with the water that flows into the dam on the second day.</p> </li> <li> <p><var>10</var> liters of water at <var>13.2</var> degrees of Celsius can be stored on the third day, by discharging <var>8</var> liters of water on the night of the first day, and mix the remaining water with the water that flows into the dam on the second and third days.</p> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 15 0 15 2 5 3 6 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0.0000000 0.6666667 1.8666667 2.9333333 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 15 1000000000 15 9 5 8 6 7 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1000000000.0000000 666666669.6666666 400000005.0000000 293333338.8666667 </pre> <p>Although the temperature of water may exceed <var>100</var> degrees Celsius, we assume that water does not vaporize.</p></section> </div> </span>
[ [ "3 10\n10 10\n20 5\n4 3\n", "3 10\n10 10\n20 5\n4 3\n" ] ]
p03745
AtCoder Grand Contest 013 - Sorted Arrays
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an array <var>A</var> of length <var>N</var>. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide <var>A</var> into?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>1 \leq A_i \leq 10^9</var></li> <li>Each <var>A_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible number of subarrays after division of <var>A</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 2 3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>One optimal solution is to divide the array into <var>[1,2,3]</var> and <var>[2,2,1]</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9 1 2 1 2 1 2 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 1 2 3 2 1 999999999 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "6\n1 2 3 2 2 1\n", "6\n1 2 3 2 2 1\n" ] ]
p03746
AtCoder Grand Contest 013 - Hamiltonish Path
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a connected undirected simple graph, which has <var>N</var> vertices and <var>M</var> edges. The vertices are numbered <var>1</var> through <var>N</var>, and the edges are numbered <var>1</var> through <var>M</var>. Edge <var>i</var> connects vertices <var>A_i</var> and <var>B_i</var>. Your task is to find a path that satisfies the following conditions:</p> <ul> <li>The path traverses two or more vertices.</li> <li>The path does not traverse the same vertex more than once.</li> <li>A vertex directly connected to at least one of the endpoints of the path, is always contained in the path.</li> </ul> <p>It can be proved that such a path always exists. Also, if there are more than one solution, any of them will be accepted.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>1 \leq M \leq 10^5</var></li> <li><var>1 \leq A_i &lt; B_i \leq N</var></li> <li>The given graph is connected and simple (that is, for every pair of vertices, there is at most one edge that directly connects them).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> <var>:</var> <var>A_M</var> <var>B_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Find one path that satisfies the conditions, and print it in the following format. In the first line, print the count of the vertices contained in the path. In the second line, print a space-separated list of the indices of the vertices, in order of appearance in the path.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 6 1 3 1 4 2 3 1 5 3 5 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 2 3 1 4 </pre> <p>There are two vertices directly connected to vertex <var>2</var>: vertices <var>3</var> and <var>4</var>. There are also two vertices directly connected to vertex <var>4</var>: vertices <var>1</var> and <var>2</var>. Hence, the path <var>2</var> → <var>3</var> → <var>1</var> → <var>4</var> satisfies the conditions.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 8 1 2 2 3 3 4 4 5 5 6 6 7 3 5 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 1 2 3 4 5 6 7 </pre></section> </div> </span>
[ [ "5 6\n1 3\n1 4\n2 3\n1 5\n3 5\n2 4\n", "5 6\n1 3\n1 4\n2 3\n1 5\n3 5\n2 4\n" ] ]
p03747
AtCoder Grand Contest 013 - Ants on a Circle
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a circle with a circumference of <var>L</var>. Each point on the circumference has a coordinate value, which represents the arc length from a certain reference point clockwise to the point. On this circumference, there are <var>N</var> ants. These ants are numbered <var>1</var> through <var>N</var> in order of increasing coordinate, and ant <var>i</var> is at coordinate <var>X_i</var>.</p> <p>The <var>N</var> ants have just started walking. For each ant <var>i</var>, you are given the initial direction <var>W_i</var>. Ant <var>i</var> is initially walking clockwise if <var>W_i</var> is <var>1</var>; counterclockwise if <var>W_i</var> is <var>2</var>. Every ant walks at a constant speed of <var>1</var> per second. Sometimes, two ants bump into each other. Each of these two ants will then turn around and start walking in the opposite direction.</p> <p>For each ant, find its position after <var>T</var> seconds.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 \leq N \leq 10^5</var></li> <li><var>1 \leq L \leq 10^9</var></li> <li><var>1 \leq T \leq 10^9</var></li> <li><var>0 \leq X_1 &lt; X_2 &lt; ... &lt; X_N \leq L - 1</var></li> <li><var>1 \leq W_i \leq 2</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>L</var> <var>T</var> <var>X_1</var> <var>W_1</var> <var>X_2</var> <var>W_2</var> <var>:</var> <var>X_N</var> <var>W_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line should contain the coordinate of ant <var>i</var> after <var>T</var> seconds. Here, each coordinate must be between <var>0</var> and <var>L-1</var>, inclusive.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 8 3 0 1 3 2 6 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 3 0 </pre> <p><var>1.5</var> seconds after the ants start walking, ant <var>1</var> and <var>2</var> bump into each other at coordinate <var>1.5</var>. <var>1</var> second after that, ant <var>1</var> and <var>3</var> bump into each other at coordinate <var>0.5</var>. <var>0.5</var> seconds after that, that is, <var>3</var> seconds after the ants start walking, ants <var>1</var>, <var>2</var> and <var>3</var> are at coordinates <var>1</var>, <var>3</var> and <var>0</var>, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 20 9 7 2 9 1 12 1 18 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 18 18 1 </pre></section> </div> </span>
[ [ "3 8 3\n0 1\n3 2\n6 1\n", "3 8 3\n0 1\n3 2\n6 1\n" ] ]
p03748
AtCoder Grand Contest 013 - Piling Up
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino has a lot of red and blue bricks and a large box. She will build a tower of these bricks in the following manner.</p> <p>First, she will pick a total of <var>N</var> bricks and put them into the box. Here, there may be any number of bricks of each color in the box, as long as there are <var>N</var> bricks in total. Particularly, there may be zero red bricks or zero blue bricks. Then, she will repeat an operation <var>M</var> times, which consists of the following three steps:</p> <ul> <li>Take out an arbitrary brick from the box.</li> <li>Put one red brick and one blue brick into the box.</li> <li>Take out another arbitrary brick from the box.</li> </ul> <p>After the <var>M</var> operations, Joisino will build a tower by stacking the <var>2 \times M</var> bricks removed from the box, in the order they are taken out. She is interested in the following question: how many different sequences of colors of these <var>2 \times M</var> bricks are possible? Write a program to find the answer. Since it can be extremely large, print the count modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 3000</var></li> <li><var>1 \leq M \leq 3000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the count of the different possible sequences of colors of <var>2 \times M</var> bricks that will be stacked, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>56 </pre> <p>A total of six bricks will be removed from the box. The only impossible sequences of colors of these bricks are the ones where the colors of the second, third, fourth and fifth bricks are all the same. Therefore, there are <var>2^6 - 2 \times 2 \times 2 = 56</var> possible sequences of colors.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1000 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1048576 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000 3000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>693347555 </pre></section> </div> </span>
[ [ "2 3\n", "2 3\n" ] ]
p03749
AtCoder Grand Contest 013 - Placing Squares
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino has a bar of length <var>N</var>, which has <var>M</var> marks on it. The distance from the left end of the bar to the <var>i</var>-th mark is <var>X_i</var>.</p> <p>She will place several squares on this bar. Here, the following conditions must be met:</p> <ul> <li>Only squares with integral length sides can be placed.</li> <li>Each square must be placed so that its bottom side touches the bar.</li> <li>The bar must be completely covered by squares. That is, no square may stick out of the bar, and no part of the bar may be left uncovered.</li> <li>The boundary line of two squares may not be directly above a mark.</li> </ul> <div style="text-align: center;"> <img src="https://atcoder.jp/img/agc013/placing_example.jpg"> <p>Examples of arrangements that satisfy/violate the conditions</p> </img></div> <p>The <em>beauty</em> of an arrangement of squares is defined as the <strong>product</strong> of the areas of all the squares placed. Joisino is interested in the sum of the beauty over all possible arrangements that satisfy the conditions. Write a program to find it. Since it can be extremely large, print the sum modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 \leq N \leq 10^9</var></li> <li><var>0 \leq M \leq 10^5</var></li> <li><var>1 \leq X_1 &lt; X_2 &lt; ... &lt; X_{M-1} &lt; X_M \leq N-1</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>X_1</var> <var>X_2</var> <var>...</var> <var>X_{M-1}</var> <var>X_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the beauty over all possible arrangements that satisfy the conditions, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>13 </pre> <p>There are two possible arrangements:</p> <ul> <li>Place a square of side length <var>1</var> to the left, and place another square of side length <var>2</var> to the right</li> <li>Place a square of side length <var>3</var></li> </ul> <p>The sum of the beauty of these arrangements is <var>(1 \times 1 \times 2 \times 2) + (3 \times 3) = 13</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>66 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 9 1 2 3 4 5 6 7 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>100 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1000000000 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>693316425 </pre></section> </div> </span>
[ [ "3 1\n2\n", "3 1\n2\n" ] ]
p03750
AtCoder Grand Contest 013 - Two Faced Cards
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> cards. The two sides of each of these cards are distinguishable. The <var>i</var>-th of these cards has an integer <var>A_i</var> printed on the front side, and another integer <var>B_i</var> printed on the back side. We will call the deck of these cards <var>X</var>. There are also <var>N+1</var> cards of another kind. The <var>i</var>-th of these cards has an integer <var>C_i</var> printed on the front side, and nothing is printed on the back side. We will call this another deck of cards <var>Y</var>.</p> <p>You will play <var>Q</var> rounds of a game. Each of these rounds is played independently. In the <var>i</var>-th round, you are given a new card. The two sides of this card are distinguishable. It has an integer <var>D_i</var> printed on the front side, and another integer <var>E_i</var> printed on the back side. A new deck of cards <var>Z</var> is created by adding this card to <var>X</var>. Then, you are asked to form <var>N+1</var> pairs of cards, each consisting of one card from <var>Z</var> and one card from <var>Y</var>. Each card must belong to exactly one of the pairs. Additionally, for each card from <var>Z</var>, you need to specify which side to <em>use</em>. For each pair, the following condition must be met:</p> <ul> <li>(The integer printed on the used side of the card from <var>Z</var>) <var> \leq </var> (The integer printed on the card from <var>Y</var>)</li> </ul> <p>If it is not possible to satisfy this condition regardless of how the pairs are formed and which sides are used, the score for the round will be <var>-1</var>. Otherwise, the score for the round will be the count of the cards from <var>Z</var> whose front side is used.</p> <p>Find the maximum possible score for each round.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 \leq N \leq 10^5</var></li> <li><var>1 \leq Q \leq 10^5</var></li> <li><var>1 \leq A_i ,B_i ,C_i ,D_i ,E_i \leq 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> <var>:</var> <var>A_N</var> <var>B_N</var> <var>C_1</var> <var>C_2</var> <var>..</var> <var>C_{N+1}</var> <var>Q</var> <var>D_1</var> <var>E_1</var> <var>D_2</var> <var>E_2</var> <var>:</var> <var>D_Q</var> <var>E_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each round, print the maximum possible score in its own line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 1 5 3 3 1 1 2 3 4 3 5 4 4 3 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 1 2 </pre> <p>For example, in the third round, the cards in <var>Z</var> are <var>(4,1),(5,3),(3,1),(2,3)</var>. The score of <var>2</var> can be obtained by using front, back, back and front sides of them, and pair them with the fourth, third, first and second cards in <var>Y</var>, respectively. It is not possible to obtain a score of <var>3</var> or greater, and thus the answer is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 7 1 9 7 13 13 11 8 12 9 16 7 8 6 9 11 7 6 11 7 10 9 3 12 9 18 16 8 9 10 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 3 3 1 -1 3 2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9 89 67 37 14 6 1 42 25 61 22 23 1 63 60 93 62 14 2 67 96 26 17 1 62 56 92 13 38 11 93 97 17 93 61 57 88 62 98 29 49 1 5 1 1 77 34 1 63 27 22 66 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>7 9 8 7 7 9 9 10 9 7 9 </pre></section> </div> </span>
[ [ "3\n4 1\n5 3\n3 1\n1 2 3 4\n3\n5 4\n4 3\n2 3\n", "3\n4 1\n5 3\n3 1\n1 2 3 4\n3\n5 4\n4 3\n2 3\n" ] ]
p03751
square869120Contest #4 - Atcoder Handles
<span class="lang-en lang-child hidden-lang"> <div class="part"> Max Score: <var>250</var> Points <br/> <section> <h3>Problem Statement</h3> Mr.X, who the handle name is <var>T</var>, looked at the list which written <var>N</var> handle names, <var>S_1, S_2, ..., S_N</var>. <br/> But he couldn't see some parts of the list. Invisible part is denoted <code>?</code>. <br/> <br/> Please calculate all possible index of the handle name of Mr.X when you sort <var>N+1</var> handle names (<var>S_1, S_2, ..., S_N</var> and <var>T</var>) in lexicographical order. <br/> Note: If there are pair of people with same handle name, either one may come first. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> The input is given from standard input in the following format.<br/> <pre> <var>N</var> <var>S_1</var> <var>S_2</var> : <var>S_N</var> <var>T</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <ul> <li>Calculate the possible index and print in sorted order. The output should be separated with a space. Don't print a space after last number.</li> <li>Put a line break in the end.</li> </ul> </section> <section> <h3>Constraints</h3> <ul> <li><var>1 ≤ N ≤ 10000</var></li> <li><var>1 ≤ |S_i|, |T| ≤ 20</var> (<var>|A|</var> is the length of <var>A</var>.)</li> <li><var>S_i</var> consists from lower-case alphabet and <code>?</code>.</li> <li><var>T</var> consists from lower-case alphabet.</li> </ul> </section> <section> <h3>Scoring</h3> <p>Subtask 1 [ <var>130</var> points ]</p> <ul> <li>There are no <code>?</code>'s.</li> </ul> <p>Subtask 2 [ <var>120</var> points ]</p> <ul> <li>There are no additional constraints.</li> </ul> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 2 tourist petr e </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 1 </pre> </section> </div> <div class="part"> <section> There are no invisible part, so there are only one possibility. The sorted handle names are <code>e</code>, <code>petr</code>, <code>tourist</code>, so <code>e</code> comes first.<br/> </section> </div> <div class="part"> <h3>Sample Input 2</h3> <pre> 2 ?o?r?s? ?et? e </pre> </div></span>
[ [ "2\ntourist\npetr\ne\n", "2\ntourist\npetr\ne\n" ] ]
p03752
square869120Contest #4 - Buildings are Colorful!
<span class="lang-en lang-child hidden-lang"> <div id="task-statement"> Max Score: <var>350</var> Points <br/> <section> <h3>Problem Statement</h3> There are <var>N</var> buildings along the line. The <var>i</var>-th building from the left is colored in color <var>i</var>, and its height is currently <var>a_i</var> meters. <br/> Chokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least <var>K</var> buildings from the left. <br/> <br/> You can increase height of buildings, but it costs <var>1</var> yens to increase <var>1</var> meters. It means you cannot make building that height is not integer. <br/> You cannot decrease height of buildings. <br/> Calculate the minimum cost of satisfying Chokudai's objective. <br/> Note: "Building <var>i</var> can see from the left" means there are no <var>j</var> exists that (height of building <var>j</var>) ≥ (height of building <var>i</var>) and <var>j &lt; i</var>. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input Format</h3> <pre> <var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> <var>a_3</var> ... <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output Format</h3> Print the minimum cost in one line. In the end put a line break.<br/> </section> <section> <h3>Constraints</h3> <ul> <li><var>1 ≤ K ≤ N ≤ 15</var></li> <li><var>1 ≤ a_i ≤ 10^9</var></li> </ul> </section> <section> <h3>Scoring</h3> Subtask 1 [<var>120</var> points] <br/> <ul> <li><var>N = K</var></li> </ul> Subtask 2 [<var>90</var> points] <br/> <ul> <li><var>N ≤ 5</var></li> <li><var>a_i ≤ 7</var></li> </ul> Subtask 3 [<var>140</var> points] <br/> <ul> <li>There are no additional constraints.</li> </ul> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 5 5 3949 3774 3598 3469 3424 </pre> </section> <section> <h3>Sample Output 1</h3> <pre> 1541 </pre> The optimal solution is (height of buildings from the left) <var>= [3949, 3950, 3951, 3952, 3953]</var>.<br/> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 5 3 7 4 2 6 4 </pre> </section> <section> <h3>Sample Output 2</h3> <pre> 7 </pre> The optimal solution is (height of buildings from the left) <var>= [7, 8, 2, 9, 4]</var>.<br/> </section> </div> </span>
[ [ "5 5\n3949 3774 3598 3469 3424\n", "5 5\n3949 3774 3598 3469 3424\n" ] ]
p03753
square869120Contest #4 - Calendar 2
<span class="lang-en lang-child hidden-lang"> <div id="task-statement"> Max Score: <var>500</var> Points <br/> <section> <h3>Problem Statement</h3> We have a grid with <var>n</var> rows and <var>7</var> columns. We call it a calendar. The cell at <var>i</var>-th row and <var>j</var>-th column is denoted <var>(i, j)</var>. <br/> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/1bef402eeaddb846eb2ea4d386a1ed3d.png" width="500"/> </div> Initially, each cell at <var>(i, j)</var> contains the integer <var>7i + j - 8</var>, and each cell is white. <br/> <br/> Snuke likes painting, so he decided integer <var>m</var>, and did <var>q</var> operations with a calendar. <br/> ・In <var>i</var>-th operation, he paint black on the cell in which an integer is written such remainder of dividing by <var>m</var> is <var>a_i</var>. <br/> <br/> Please count the number of connected white parts. <br/> Note that if two adjacent cells are white, the cells belong to the same connected part. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input Format</h3> The input format is following:<br/> <pre> <var>n</var> <var>m</var> <var>q</var> <var>a_1</var> <var>a_2</var> ... <var>a_q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output Format</h3> Print the number of connected part in one line.<br/> </section> <section> <h3>Constraints</h3> <ul> <li><var>n</var> ≤ <var>10^{12}</var></li> <li><var>7n</var> is divisible by <var>m</var>.</li> <li>1 ≤ <var>q</var> ≤ <var>m</var> ≤ <var>10^5</var></li> <li><var>0</var> ≤ <var>a_1</var> &lt; <var>a_2</var> &lt; ... &lt; <var>a_q</var> &lt; m</li> </ul> </section> <section> <h3>Scoring</h3> Subtask 1 [<var>100</var> points] <br/> <ul> <li><var>n</var> ≤ <var>100000</var>.</li> </ul> Subtask 2 [<var>90</var> points] <br/> <ul> <li><var>m</var> is divisible by <var>7</var>.</li> <li><var>a_{i + 1} - a_i = 1</var>.</li> </ul> Subtask 3 [<var>200</var> points] <br/> <ul> <li><var>m</var> is divisible by <var>7</var>.</li> </ul> Subtask 4 [<var>110</var> points] <br/> <ul> <li>There are no additional constraints.</li> </ul> </section> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 7 7 3 1 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 4 </pre> The calendar looks like this: <br/> <!-- ここにカレンダーの画像を貼る --> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/b7ae283b5c0a566376d05aeff63b100a.png" width="500"/> </div> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 10 14 8 5 6 7 8 9 10 11 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 10 </pre> The calendar looks like this: <br/> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/42a03c02d737a1c97005360832707eaa.png" width="500"/> </div> </section> </div> </div> </span>
[ [ "7 7 3\n1 3 5\n", "7 7 3\n1 3 5\n" ] ]
p03754
square869120Contest #4 - Driving on a Tree
<span class="lang-en lang-child hidden-lang"> <div class="part"> Max Score: $800$ Points <br/> <section> <h3>Problem Statement</h3> There is an undirected connected graph with $N$ vertices and $N-1$ edges. The <var>i</var>-th edge connects <var>u_i</var> and <var>v_i</var>.<br/> <br/> E869120 the coder moves in the graph as follows:<br/> <ul> <li>He move to adjacent vertex, but he can't a visit vertex two or more times.</li> <li>He ends move when there is no way to move.</li> <li>Otherwise, he moves randomly. (equal probability) If he has $p$ way to move this turn, he choose each vertex with $1/p$ probability.</li> </ul> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/5973dad11843e9098d9225dd431c9084.png" width="500"/> </div> Calculate the expected value of the number of turns, when E869120 starts from vertex <var>i</var>, for all <var>i (1 ≤ i ≤ N)</var>. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> The input is given from standard input in the following format. <br/> <br/> <pre> <var>N</var> <var>u_1</var> <var>v_1</var> <var>u_2</var> <var>v_2</var> : <var>u_{N-1}</var> <var>v_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> In <var>i</var>-th <var>(1 ≤ i ≤ N)</var> line, print the expected vaule of the number of turns E869120 moves.<br/> The relative error or absolute error of output should be within <var>10^{-6}</var>. <br/> </section> <section> <h3>Constraints</h3> <ul> <li>$1 \le N \le 150,000$</li> <li>The graph is connected.</li> </ul> </section> <section> <h3>Subtasks</h3> <p>Subtask 1 [ $190$ points ]</p> <ul> <li>There is no vertex which degree is more than 2.</li> <li>This means the graph looks like a list.</li> </ul> <br/> <p>Subtask 2 [ $220$ points ]</p> <ul> <li><var>1 ≤ N ≤ 1000</var>.</li> </ul> <br/> <p>Subtask 3 [ $390$ points ]</p> <ul> <li>There are no additional constraints.</li> </ul> <br/> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 4 1 2 2 3 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 2.0 1.0 2.0 2.0 </pre> </section> </div> <div class="part"> <h3>Sample Input 2</h3> <pre> 4 1 2 2 4 4 3 </pre> </div></span>
[ [ "4\n1 2\n2 3\n2 4\n", "4\n1 2\n2 3\n2 4\n" ] ]
p03755
square869120Contest #4 - Enormous Atcoder Railroad
<span class="lang-en lang-child hidden-lang"> <div id="task-statement"> Max Score: <var>1000</var> Points <br/> <section> <h3>Problem Statement</h3> There is a railroad company in Atcoder Kingdom, "Atcoder Railroad". <br/> There are <var>N + 1</var> stations numbered <var>0, 1, 2, ..., N</var> along a railway. <br/> Currently, two kinds of train are operated, local and express. <br/> A local train stops at every station, and it takes one minute from station <var>i</var> to <var>i + 1</var>, and vice versa. <br/> An express train only stops at station <var>S_0, S_1, S_2, ..., S_{K-1} (0 = S_0 &lt; S_1 &lt; S_2 &lt; ... &lt; S_{K-1} = N)</var>. It takes one minute from station <var>S_i</var> to <var>S_{i + 1}</var>, and vice versa. <br/> But the president of Atcoder Railroad, Semiexp said it is not very convenient so he planned to operate one more kind of train, "semi-express". <br/> The stations where the semi-express stops (This is <var>T_0, T_1, T_2, ..., T_{L-1}</var>, <var>0 = T_0 &lt; T_1 &lt; T_2 &lt; ... &lt; T_{L-1} = N</var>) have to follow following conditions: <br/> From station <var>T_i</var> to <var>T_{i+1}</var> takes 1 minutes, and vice versa. <br/> <ul> <li>The center of Atcoder Kingdom is station <var>0</var>, and you have to be able to go to station <var>i</var> atmost <var>X</var> minutes.</li> <li>If the express stops at the station, semi-express should stops at the station.</li> </ul> Print the number of ways of the set of the station where semi-express stops (sequence <var>T</var>). <br/> Since the answer can be large, print the number modulo <var>10^9 + 7</var>. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input Format</h3> <pre> <var>N</var> <var>K</var> <var>X</var> <var>S_0</var> <var>S_1</var> <var>S_2</var> ... <var>S_{K-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output Format</h3> Print the number of ways of the set of the station where semi-express stops, mod <var>10^9 + 7</var> in one line. <br/> Print <code>\n</code> (line break) in the end. <br/> </section> <section> <h3>Constraints</h3> <ul> <li><var>2 ≤ K ≤ 2500</var>.</li> <li><var>1 ≤ X ≤ 2500</var>.</li> <li><var>S_0 = 0, S_{K-1} = N</var>.</li> <li><var>1 ≤ S_{i + 1} - S_i ≤ 10000</var>.</li> </ul> </section> <section> <h3>Scoring</h3> Subtask 1 [<var>120</var> points] <br/> <ul> <li><var>N, K, X ≤ 15</var>.</li> </ul> Subtask 2 [<var>90</var> points] <br/> <ul> <li><var>K, X ≤ 15</var>.</li> <li><var>S_{i + 1} - S_i ≤ 15</var>.</li> </ul> Subtask 3 [<var>260</var> points] <br/> <ul> <li><var>K, X ≤ 40</var>.</li> <li><var>S_{i + 1} - S_i ≤ 40</var>.</li> </ul> Subtask 4 [<var>160</var> points] <br/> <ul> <li><var>K, X ≤ 300</var>.</li> <li><var>S_{i + 1} - S_i ≤ 300</var>.</li> </ul> Subtask 5 [<var>370</var> points] <br/> <ul> <li>There are no additional constraints.</li> </ul> </section> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 7 2 3 0 7 </pre> </section> <section> <h3>Sample Output 1</h3> <pre> 55 </pre> The set of trains that stops station <var>0</var> and <var>7</var>, and can't satisfy the condition is: <br/> <var>[0, 7], [0, 1, 7], [0, 1, 2, 7], [0, 1, 6, 7], [0, 1, 2, 6, 7], [0, 1, 2, 3, 6, 7], [0, 1, 2, 5, 6, 7], [0, 1, 2, 3, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7]</var>, <var>9</var> ways.<br/> Therefore, the number of ways is <var>2^6 - 9 = 55</var>. <br/> </section> </div> </div> </span>
[ [ "7 2 3\n0 7\n", "7 2 3\n0 7\n" ] ]