id
stringlengths 6
6
| slug_name
stringlengths 1
105
| pretty_content
stringlengths 1
27.2k
| test_cases
sequencelengths 1
1
|
---|---|---|---|
p03456 | AtCoder Beginner Contest 086 - 1 21 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>AtCoDeer the deer has found two positive integers, <var>a</var> and <var>b</var>.
Determine whether the concatenation of <var>a</var> and <var>b</var> in this order is a square number.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>a,b</var> <var>≤</var> <var>100</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>a</var> <var>b</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the concatenation of <var>a</var> and <var>b</var> in this order is a square number, 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 21
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>As <var>121</var> <var>=</var> <var>11</var> × <var>11</var>, it is a square number.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p><var>100100</var> is not a square number.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>12 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"1 21\n",
"1 21\n"
]
] |
p03457 | AtCoder Beginner Contest 086 - Traveling | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point <var>(0, 0)</var> at time <var>0</var>, then for each <var>i</var> between <var>1</var> and <var>N</var> (inclusive), he will visit point <var>(x_i,y_i)</var> at time <var>t_i</var>.</p>
<p>If AtCoDeer is at point <var>(x, y)</var> at time <var>t</var>, he can be at one of the following points at time <var>t+1</var>: <var>(x+1,y)</var>, <var>(x-1,y)</var>, <var>(x,y+1)</var> and <var>(x,y-1)</var>.
Note that <strong>he cannot stay at his place</strong>.
Determine whether he can carry out his plan.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li>
<li><var>0</var> <var>≤</var> <var>x_i</var> <var>≤</var> <var>10^5</var></li>
<li><var>0</var> <var>≤</var> <var>y_i</var> <var>≤</var> <var>10^5</var></li>
<li><var>1</var> <var>≤</var> <var>t_i</var> <var>≤</var> <var>10^5</var></li>
<li><var>t_i</var> <var><</var> <var>t_{i+1}</var> (<var>1</var> <var>≤</var> <var>i</var> <var>≤</var> <var>N-1</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>t_1</var> <var>x_1</var> <var>y_1</var>
<var>t_2</var> <var>x_2</var> <var>y_2</var>
<var>:</var>
<var>t_N</var> <var>x_N</var> <var>y_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If AtCoDeer can carry out his plan, print <code>Yes</code>; if he cannot, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
3 1 2
6 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>For example, he can travel as follows: <var>(0,0)</var>, <var>(0,1)</var>, <var>(1,1)</var>, <var>(1,2)</var>, <var>(1,1)</var>, <var>(1,0)</var>, then <var>(1,1)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
2 100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p>It is impossible to be at <var>(100,100)</var> two seconds after being at <var>(0,0)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
5 1 1
100 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"2\n3 1 2\n6 1 1\n",
"2\n3 1 2\n6 1 1\n"
]
] |
p03458 | AtCoder Beginner Contest 086 - Checker | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>AtCoDeer is thinking of painting an infinite two-dimensional grid in a <em>checked pattern of side <var>K</var></em>.
Here, a checked pattern of side <var>K</var> is a pattern where each square is painted black or white so that each connected component of each color is a <var>K</var> <var>×</var> <var>K</var> square.
Below is an example of a checked pattern of side <var>3</var>:</p>
<div style="text-align: center;">
<img alt="cba927b2484fad94fb5ff7473e9aadef.png" src="https://img.atcoder.jp/arc089/cba927b2484fad94fb5ff7473e9aadef.png">
</img></div>
<p>AtCoDeer has <var>N</var> desires.
The <var>i</var>-th desire is represented by <var>x_i</var>, <var>y_i</var> and <var>c_i</var>.
If <var>c_i</var> is <code>B</code>, it means that he wants to paint the square <var>(x_i,y_i)</var> black; if <var>c_i</var> is <code>W</code>, he wants to paint the square <var>(x_i,y_i)</var> white.
At most how many desires can he satisfy at the same time?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li>
<li><var>1</var> <var>≤</var> <var>K</var> <var>≤</var> <var>1000</var></li>
<li><var>0</var> <var>≤</var> <var>x_i</var> <var>≤</var> <var>10^9</var></li>
<li><var>0</var> <var>≤</var> <var>y_i</var> <var>≤</var> <var>10^9</var></li>
<li>If <var>i</var> <var>≠</var> <var>j</var>, then <var>(x_i,y_i)</var> <var>≠</var> <var>(x_j,y_j)</var>.</li>
<li><var>c_i</var> is <code>B</code> or <code>W</code>.</li>
<li><var>N</var>, <var>K</var>, <var>x_i</var> and <var>y_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>K</var>
<var>x_1</var> <var>y_1</var> <var>c_1</var>
<var>x_2</var> <var>y_2</var> <var>c_2</var>
<var>:</var>
<var>x_N</var> <var>y_N</var> <var>c_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum number of desires that can be satisfied at the same time.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3
0 1 W
1 2 W
5 3 B
5 4 B
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>He can satisfy all his desires by painting as shown in the example above.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 1000
0 0 B
0 1 W
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"4 3\n0 1 W\n1 2 W\n5 3 B\n5 4 B\n",
"4 3\n0 1 W\n1 2 W\n5 3 B\n5 4 B\n"
]
] |
p03459 | AtCoder Regular Contest 089 - Traveling | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point <var>(0, 0)</var> at time <var>0</var>, then for each <var>i</var> between <var>1</var> and <var>N</var> (inclusive), he will visit point <var>(x_i,y_i)</var> at time <var>t_i</var>.</p>
<p>If AtCoDeer is at point <var>(x, y)</var> at time <var>t</var>, he can be at one of the following points at time <var>t+1</var>: <var>(x+1,y)</var>, <var>(x-1,y)</var>, <var>(x,y+1)</var> and <var>(x,y-1)</var>.
Note that <strong>he cannot stay at his place</strong>.
Determine whether he can carry out his plan.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li>
<li><var>0</var> <var>≤</var> <var>x_i</var> <var>≤</var> <var>10^5</var></li>
<li><var>0</var> <var>≤</var> <var>y_i</var> <var>≤</var> <var>10^5</var></li>
<li><var>1</var> <var>≤</var> <var>t_i</var> <var>≤</var> <var>10^5</var></li>
<li><var>t_i</var> <var><</var> <var>t_{i+1}</var> (<var>1</var> <var>≤</var> <var>i</var> <var>≤</var> <var>N-1</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>t_1</var> <var>x_1</var> <var>y_1</var>
<var>t_2</var> <var>x_2</var> <var>y_2</var>
<var>:</var>
<var>t_N</var> <var>x_N</var> <var>y_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If AtCoDeer can carry out his plan, print <code>Yes</code>; if he cannot, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
3 1 2
6 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>For example, he can travel as follows: <var>(0,0)</var>, <var>(0,1)</var>, <var>(1,1)</var>, <var>(1,2)</var>, <var>(1,1)</var>, <var>(1,0)</var>, then <var>(1,1)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
2 100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p>It is impossible to be at <var>(100,100)</var> two seconds after being at <var>(0,0)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
5 1 1
100 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"2\n3 1 2\n6 1 1\n",
"2\n3 1 2\n6 1 1\n"
]
] |
p03460 | AtCoder Regular Contest 089 - Checker | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>AtCoDeer is thinking of painting an infinite two-dimensional grid in a <em>checked pattern of side <var>K</var></em>.
Here, a checked pattern of side <var>K</var> is a pattern where each square is painted black or white so that each connected component of each color is a <var>K</var> <var>×</var> <var>K</var> square.
Below is an example of a checked pattern of side <var>3</var>:</p>
<div style="text-align: center;">
<img alt="cba927b2484fad94fb5ff7473e9aadef.png" src="https://img.atcoder.jp/arc089/cba927b2484fad94fb5ff7473e9aadef.png">
</img></div>
<p>AtCoDeer has <var>N</var> desires.
The <var>i</var>-th desire is represented by <var>x_i</var>, <var>y_i</var> and <var>c_i</var>.
If <var>c_i</var> is <code>B</code>, it means that he wants to paint the square <var>(x_i,y_i)</var> black; if <var>c_i</var> is <code>W</code>, he wants to paint the square <var>(x_i,y_i)</var> white.
At most how many desires can he satisfy at the same time?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li>
<li><var>1</var> <var>≤</var> <var>K</var> <var>≤</var> <var>1000</var></li>
<li><var>0</var> <var>≤</var> <var>x_i</var> <var>≤</var> <var>10^9</var></li>
<li><var>0</var> <var>≤</var> <var>y_i</var> <var>≤</var> <var>10^9</var></li>
<li>If <var>i</var> <var>≠</var> <var>j</var>, then <var>(x_i,y_i)</var> <var>≠</var> <var>(x_j,y_j)</var>.</li>
<li><var>c_i</var> is <code>B</code> or <code>W</code>.</li>
<li><var>N</var>, <var>K</var>, <var>x_i</var> and <var>y_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>K</var>
<var>x_1</var> <var>y_1</var> <var>c_1</var>
<var>x_2</var> <var>y_2</var> <var>c_2</var>
<var>:</var>
<var>x_N</var> <var>y_N</var> <var>c_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum number of desires that can be satisfied at the same time.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3
0 1 W
1 2 W
5 3 B
5 4 B
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>He can satisfy all his desires by painting as shown in the example above.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 1000
0 0 B
0 1 W
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"4 3\n0 1 W\n1 2 W\n5 3 B\n5 4 B\n",
"4 3\n0 1 W\n1 2 W\n5 3 B\n5 4 B\n"
]
] |
p03461 | AtCoder Regular Contest 089 - GraphXY | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>AtCoDeer the deer wants a directed graph that satisfies the following conditions:</p>
<ul>
<li>The number of vertices, <var>N</var>, is at most <var>300</var>.</li>
<li>There must not be self-loops or multiple edges.</li>
<li>The vertices are numbered from <var>1</var> through <var>N</var>.</li>
<li>Each edge has either an integer weight between <var>0</var> and <var>100</var> (inclusive), or a label <code>X</code> or <code>Y</code>.</li>
<li>For every pair of two integers <var>(x,y)</var> such that <var>1 ≤ x ≤ A</var>, <var>1 ≤ y ≤ B</var>,
the shortest distance from Vertex <var>S</var> to Vertex <var>T</var> in the graph where the edges labeled <code>X</code> have the weight <var>x</var> and the edges labeled <code>Y</code> have the weight <var>y</var>, is <var>d_{x,y}</var>.</li>
</ul>
<p>Construct such a graph (and a pair of <var>S</var> and <var>T</var>) for him, or report that it does not exist.
Refer to Output section for output format.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>A,B</var> <var>≤</var> <var>10</var></li>
<li><var>1</var> <var>≤</var> <var>d_{x,y}</var> <var>≤</var> <var>100</var> (<var>1</var> <var>≤</var> <var>x</var> <var>≤</var> <var>A</var>, <var>1</var> <var>≤</var> <var>y</var> <var>≤</var> <var>B</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>A</var> <var>B</var>
<var>d_{1,1}</var> <var>d_{1,2}</var> <var>..</var> <var>d_{1,B}</var>
<var>d_{2,1}</var> <var>d_{2,2}</var> <var>..</var> <var>d_{2,B}</var>
<var>:</var>
<var>d_{A,1}</var> <var>d_{A,2}</var> <var>..</var> <var>d_{A,B}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If no graph satisfies the condition, print <code>Impossible</code>.</p>
<p>If there exists a graph that satisfies the condition, print <code>Possible</code> in the first line.
Then, in the subsequent lines, print the constructed graph in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>u_1</var> <var>v_1</var> <var>c_1</var>
<var>u_2</var> <var>v_2</var> <var>c_2</var>
:
<var>u_M</var> <var>v_M</var> <var>c_M</var>
<var>S</var> <var>T</var>
</pre>
<p>Here, <var>M</var> is the number of the edges, and <var>u_i</var>, <var>v_i</var>, <var>c_i</var> represent edges as follows: there is an edge from Vertex <var>u_i</var> to Vertex <var>v_i</var> whose weight or label is <var>c_i</var>.</p>
<p>Also refer to Sample Outputs.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3
1 2 2
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Possible
3 4
1 2 X
2 3 1
3 2 Y
1 3 Y
1 3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 3
100 50 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Impossible
</pre></section>
</div>
</span> | [
[
"2 3\n1 2 2\n1 2 3\n",
"2 3\n1 2 2\n1 2 3\n"
]
] |
p03462 | AtCoder Regular Contest 089 - ColoringBalls | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> white balls arranged in a row, numbered <var>1,2,..,N</var> from left to right.
AtCoDeer the deer is thinking of painting some of these balls red and blue, while leaving some of them white.</p>
<p>You are given a string <var>s</var> of length <var>K</var>.
AtCoDeer performs the following operation for each <var>i</var> from <var>1</var> through <var>K</var> in order:</p>
<ul>
<li>The <var>i</var>-th operation: Choose a contiguous segment of balls (<strong>possibly empty</strong>), and paint these balls red if the <var>i</var>-th character in <var>s</var> is <code>r</code>; paint them blue if the character is <code>b</code>.</li>
</ul>
<p>Here, if a ball which is already painted is again painted, the color of the ball will be overwritten.
However, due to the properties of dyes, <strong>it is not possible to paint a white, unpainted ball directly in blue.</strong>
That is, when the <var>i</var>-th character in <var>s</var> is <code>b</code>, the chosen segment must not contain a white ball.</p>
<p>After all the operations, how many different sequences of colors of the balls are possible?
Since the count can be large, find it modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>70</var></li>
<li><var>1</var> <var>≤</var> <var>K</var> <var>≤</var> <var>70</var></li>
<li><var>|s|</var> <var>=</var> <var>K</var></li>
<li><var>s</var> consists of <code>r</code> and <code>b</code>.</li>
<li><var>N</var> and <var>K</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>K</var>
<var>s</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the different possible sequences of colors of the balls after all the operations, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
rb
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>9
</pre>
<p>There are nine possible sequences of colors of the balls, as follows:</p>
<p><code>ww</code>, <code>wr</code>, <code>rw</code>, <code>rr</code>, <code>wb</code>, <code>bw</code>, <code>bb</code>, <code>rb</code>, <code>br</code>.</p>
<p>Here, <code>r</code> represents red, <code>b</code> represents blue and <code>w</code>represents white.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 2
br
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>16
</pre>
<p>Since we cannot directly paint white balls in blue, we can only choose an empty segment in the first operation.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7 4
rbrb
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1569
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>70 70
bbrbrrbbrrbbbbrbbrbrrbbrrbbrbrrbrbrbbbbrbbrbrrbbrrbbbbrbbrbrrbbrrbbbbr
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>841634130
</pre></section>
</div>
</span> | [
[
"2 2\nrb\n",
"2 2\nrb\n"
]
] |
p03463 | AtCoder Grand Contest 020 - Move and Win | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A game is played on a strip consisting of <var>N</var> cells consecutively numbered from 1 to <var>N</var>. </p>
<p>Alice has her token on cell <var>A</var>. Borys has his token on a different cell <var>B</var>.</p>
<p>Players take turns, Alice moves first.
The moving player must shift his or her token from its current cell <var>X</var> to the neighboring cell on the left, cell <var>X-1</var>, or on the right, cell <var>X+1</var>.
Note that it's disallowed to move the token outside the strip or to the cell with the other player's token.
In one turn, the token of the moving player must be shifted exactly once.</p>
<p>The player who can't make a move loses, and the other player wins.</p>
<p>Both players want to win. Who wins if they play optimally?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var></li>
<li><var>1 \leq A < B \leq 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>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Alice</code> if Alice wins, <code>Borys</code> if Borys wins, and <code>Draw</code> if nobody wins.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 2 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Alice
</pre>
<p>Alice can move her token to cell 3.
After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5.
Then, Alice moves her token to cell 4. Borys can't make a move and loses. </p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Borys
</pre>
<p>Alice can't make the very first move and loses.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>58 23 42
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Borys
</pre></section>
</div>
</span> | [
[
"5 2 4\n",
"5 2 4\n"
]
] |
p03464 | AtCoder Grand Contest 020 - Ice Rink Game | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>An adult game master and <var>N</var> children are playing a game on an ice rink.
The game consists of <var>K</var> rounds.
In the <var>i</var>-th round, the game master announces:</p>
<ul>
<li>Form groups consisting of <var>A_i</var> children each!</li>
</ul>
<p>Then the children who are still in the game form as many groups of <var>A_i</var> children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.</p>
<p>In the end, after the <var>K</var>-th round, there are exactly two children left, and they are declared the winners.</p>
<p>You have heard the values of <var>A_1</var>, <var>A_2</var>, ..., <var>A_K</var>. You don't know <var>N</var>, but you want to estimate it.</p>
<p>Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of <var>N</var> exist.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K \leq 10^5</var></li>
<li><var>2 \leq A_i \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>K</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print two integers representing the smallest and the largest possible value of <var>N</var>, respectively,
or a single integer <var>-1</var> if the described situation is impossible.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
3 4 3 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6 8
</pre>
<p>For example, if the game starts with <var>6</var> children, then it proceeds as follows:</p>
<ul>
<li>In the first round, <var>6</var> children form <var>2</var> groups of <var>3</var> children, and nobody leaves the game.</li>
<li>In the second round, <var>6</var> children form <var>1</var> group of <var>4</var> children, and <var>2</var> children leave the game.</li>
<li>In the third round, <var>4</var> children form <var>1</var> group of <var>3</var> children, and <var>1</var> child leaves the game.</li>
<li>In the fourth round, <var>3</var> children form <var>1</var> group of <var>2</var> children, and <var>1</var> child leaves the game.</li>
</ul>
<p>The last <var>2</var> children are declared the winners.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
3 4 100 3 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
<p>This situation is impossible.
In particular, if the game starts with less than <var>100</var> children, everyone leaves after the third round.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
2 2 2 2 2 2 2 2 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2 3
</pre></section>
</div>
</span> | [
[
"4\n3 4 3 2\n",
"4\n3 4 3 2\n"
]
] |
p03465 | AtCoder Grand Contest 020 - Median Sum | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given <var>N</var> integers <var>A_1</var>, <var>A_2</var>, ..., <var>A_N</var>.</p>
<p>Consider the sums of all non-empty subsequences of <var>A</var>. There are <var>2^N - 1</var> such sums, an odd number.</p>
<p>Let the list of these sums in non-decreasing order be <var>S_1</var>, <var>S_2</var>, ..., <var>S_{2^N - 1}</var>.</p>
<p>Find the median of this list, <var>S_{2^{N-1}}</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2000</var></li>
<li><var>1 \leq A_i \leq 2000</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_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 median of the sorted list of the sums of all non-empty subsequences of <var>A</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>In this case, <var>S = (1, 1, 2, 2, 3, 3, 4)</var>. Its median is <var>S_4 = 2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
58
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>58
</pre>
<p>In this case, <var>S = (58)</var>.</p></section>
</div>
</span> | [
[
"3\n1 2 1\n",
"3\n1 2 1\n"
]
] |
p03466 | AtCoder Grand Contest 020 - Min Max Repetition | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let <var>f(A, B)</var>, where <var>A</var> and <var>B</var> are positive integers, be the string satisfying the following conditions:</p>
<ul>
<li><var>f(A, B)</var> has length <var>A + B</var>;</li>
<li><var>f(A, B)</var> contains exactly <var>A</var> letters <code>A</code> and exactly <var>B</var> letters <code>B</code>;</li>
<li>The length of the longest substring of <var>f(A, B)</var> consisting of equal letters (ex., <code>AAAAA</code> or <code>BBBB</code>) is as small as possible under the conditions above;</li>
<li><var>f(A, B)</var> is the lexicographically smallest string satisfying the conditions above.</li>
</ul>
<p>For example, <var>f(2, 3)</var> = <code>BABAB</code>, and <var>f(6, 4)</var> = <code>AABAABAABB</code>.</p>
<p>Answer <var>Q</var> queries: find the substring of <var>f(A_i, B_i)</var> from position <var>C_i</var> to position <var>D_i</var> (1-based).</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq Q \leq 10^3</var></li>
<li><var>1 \leq A_i, B_i \leq 5 \times 10^8</var></li>
<li><var>1 \leq C_i \leq D_i \leq A_i + B_i</var></li>
<li><var>D_i - C_i + 1 \leq 100</var></li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li><var>500</var> points will be awarded for passing the testset satisfying <var>1 \leq A_i, B_i \leq 10^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>Q</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>:</var>
<var>A_Q</var> <var>B_Q</var> <var>C_Q</var> <var>D_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>For each query <var>i</var> in order of input, print a line containing the substring of <var>f(A_i, B_i)</var> from position <var>C_i</var> to position <var>D_i</var> (1-based).</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
2 3 1 5
6 4 1 10
2 3 4 4
6 4 3 7
8 10 5 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>BABAB
AABAABAABB
A
BAABA
ABAB
</pre></section>
</div>
</span> | [
[
"5\n2 3 1 5\n6 4 1 10\n2 3 4 4\n6 4 3 7\n8 10 5 8\n",
"5\n2 3 1 5\n6 4 1 10\n2 3 4 4\n6 4 3 7\n8 10 5 8\n"
]
] |
p03467 | AtCoder Grand Contest 020 - Encoding Subsets | <span class="lang-en">
<p>Score : <var>1400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Consider the following set of rules for encoding strings consisting of <code>0</code> and <code>1</code>:</p>
<ul>
<li>Strings <code>0</code> and <code>1</code> can be encoded as <code>0</code> and <code>1</code>, respectively. </li>
<li>If strings <var>A</var> and <var>B</var> can be encoded as <var>P</var> and <var>Q</var>, respectively, then string <var>AB</var> can be encoded as <var>PQ</var>.</li>
<li>If string <var>A</var> can be encoded as <var>P</var> and <var>K \geq 2</var> is a positive integer, then string <var>AA...A</var> (<var>A</var> repeated <var>K</var> times) can be encoded as <code>(</code><var>P</var><code>x</code><var>K</var><code>)</code>.</li>
</ul>
<p>For example, string <code>001001001</code>, among other possibilities, can be encoded as <code>001001001</code>, <code>00(1(0x2)x2)1</code> and <code>(001x3)</code>.</p>
<p>Let's call string <var>A</var> a subset of string <var>B</var> if:</p>
<ul>
<li><var>A</var> and <var>B</var> are equal in length and consist of <code>0</code> and <code>1</code>;</li>
<li>for all indices <var>i</var> such that <var>A_i</var> = <code>1</code>, it's also true that <var>B_i</var> = <code>1</code>.</li>
</ul>
<p>You are given string <var>S</var> consisting of <code>0</code> and <code>1</code>. Find the total number of distinct encodings of all subsets of <var>S</var>, modulo <var>998244353</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 100</var></li>
<li><var>S</var> consists of <code>0</code> and <code>1</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>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the total number of distinct encodings of all subsets of <var>S</var> modulo <var>998244353</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>011
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>9
</pre>
<p>There are four subsets of <var>S</var>:</p>
<ul>
<li><code>011</code> can be encoded as <code>011</code> and <code>0(1x2)</code>;</li>
<li><code>010</code> can be encoded as <code>010</code>;</li>
<li><code>001</code> can be encoded as <code>001</code> and <code>(0x2)1</code>;</li>
<li><code>000</code> can be encoded as <code>000</code>, <code>0(0x2)</code>, <code>(0x2)0</code> and <code>(0x3)</code>.</li>
</ul>
<p>Thus, the total number of encodings of all subsets of <var>S</var> is <var>2 + 1 + 2 + 4 = 9</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>0000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>10
</pre>
<p>This time <var>S</var> has only one subset, but it can be encoded in <var>10</var> different ways.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>101110
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>156
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>001110111010110001100000100111
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>363383189
</pre>
<p>Don't forget to take the result modulo <var>998244353</var>.</p></section>
</div>
</span> | [
[
"011\n",
"011\n"
]
] |
p03468 | AtCoder Grand Contest 020 - Arcs on a Circle | <span class="lang-en">
<p>Score : <var>2100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You have a circle of length <var>C</var>, and you are placing <var>N</var> arcs on it. Arc <var>i</var> has length <var>L_i</var>.</p>
<p>Every arc <var>i</var> is placed on the circle uniformly at random:
a random real point on the circle is chosen, then an arc of length <var>L_i</var> centered at this point appears.</p>
<p>Note that the arcs are placed independently. For example, they may intersect or contain each other.</p>
<p>What is the probability that every real point of the circle will be covered by at least one arc?
Assume that an arc covers its ends.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 6</var></li>
<li><var>2 \leq C \leq 50</var></li>
<li><var>1 \leq L_i < C</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>C</var>
<var>L_1</var> <var>L_2</var> <var>...</var> <var>L_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the probability that every real point of the circle will be covered by at least one arc.
Your answer will be considered correct if its absolute error doesn't exceed <var>10^{-11}</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>0.3333333333333333
</pre>
<p>The centers of the two arcs must be at distance at least <var>1</var>. The probability of this on a circle of length <var>3</var> is <var>1 / 3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 10
1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0.0000000000000000
</pre>
<p>Even though the total length of the arcs is exactly <var>C</var> and it's possible that every real point of the circle is covered by at least one arc,
the probability of this event is <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4 2
1 1 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0.5000000000000000
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3 5
2 2 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>0.4000000000000000
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>4 6
4 1 3 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>0.3148148148148148
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>6 49
22 13 27 8 2 19
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>0.2832340720702695
</pre></section>
</div>
</span> | [
[
"2 3\n2 2\n",
"2 3\n2 2\n"
]
] |
p03469 | AtCoder Beginner Contest 085 - Already 2018 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>On some day in January <var>2018</var>, Takaki is writing a document. The document has a column where the current date is written in <code>yyyy/mm/dd</code> format. For example, January <var>23</var>, <var>2018</var> should be written as <code>2018/01/23</code>.</p>
<p>After finishing the document, she noticed that she had mistakenly wrote <code>2017</code> at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, <var>S</var>, is given as input, modifies the first four characters in <var>S</var> to <code>2018</code> and prints it.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>S</var> is a string of length <var>10</var>.</li>
<li>The first eight characters in <var>S</var> are <code>2017/01/</code>.</li>
<li>The last two characters in <var>S</var> are digits and represent an integer between <var>1</var> and <var>31</var> (inclusive).</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>Replace the first four characters in <var>S</var> with <code>2018</code> and print it.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2017/01/07
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2018/01/07
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2017/01/31
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2018/01/31
</pre></section>
</div>
</span> | [
[
"2017/01/07\n",
"2017/01/07\n"
]
] |
p03470 | AtCoder Beginner Contest 085 - Kagami Mochi | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>An <em><var>X</var>-layered kagami mochi</em> <var>(X ≥ 1)</var> is a pile of <var>X</var> round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of <var>10</var>, <var>8</var> and <var>6</var> centimeters from bottom to top in this order, you have a <var>3</var>-layered kagami mochi; if you put just one mochi, you have a <var>1</var>-layered kagami mochi.</p>
<p>Lunlun the dachshund has <var>N</var> round mochi, and the diameter of the <var>i</var>-th mochi is <var>d_i</var> centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 100</var></li>
<li><var>1 ≤ d_i ≤ 100</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>d_1</var>
<var>:</var>
<var>d_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum number of layers in a kagami mochi that can be made.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
10
8
8
6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>If we stack the mochi with diameters of <var>10</var>, <var>8</var> and <var>6</var> centimeters from bottom to top in this order, we have a <var>3</var>-layered kagami mochi, which is the maximum number of layers.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
15
15
15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>When all the mochi have the same diameter, we can only have a <var>1</var>-layered kagami mochi.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7
50
30
50
100
50
80
30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"4\n10\n8\n8\n6\n",
"4\n10\n8\n8\n6\n"
]
] |
p03471 | AtCoder Beginner Contest 085 - Otoshidama | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>The commonly used bills in Japan are <var>10000</var>-yen, <var>5000</var>-yen and <var>1000</var>-yen bills. Below, the word "bill" refers to only these.</p>
<p>According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained <var>N</var> bills for a total of <var>Y</var> yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 2000</var></li>
<li><var>1000 ≤ Y ≤ 2 × 10^7</var></li>
<li><var>N</var> is an integer.</li>
<li><var>Y</var> is a multiple of <var>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>Y</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the total value of <var>N</var> bills cannot be <var>Y</var> yen, print <code>-1 -1 -1</code>.</p>
<p>If the total value of <var>N</var> bills can be <var>Y</var> yen, let one such set of bills be "<var>x</var> <var>10000</var>-yen bills, <var>y</var> <var>5000</var>-yen bills and <var>z</var> <var>1000</var>-yen bills", and print <var>x</var>, <var>y</var>, <var>z</var> with spaces in between. If there are multiple possibilities, any of them may be printed.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>9 45000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4 0 5
</pre>
<p>If the envelope contained <var>4</var> <var>10000</var>-yen bills and <var>5</var> <var>1000</var>-yen bills, he had <var>9</var> bills and <var>45000</var> yen in total. It is also possible that the envelope contained <var>9</var> <var>5000</var>-yen bills, so the output <code>0 9 0</code> is also correct.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>20 196000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1 -1 -1
</pre>
<p>When the envelope contained <var>20</var> bills in total, the total value would be <var>200000</var> yen if all the bills were <var>10000</var>-yen bills, and would be at most <var>195000</var> yen otherwise, so it would never be <var>196000</var> yen.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1000 1234000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>14 27 959
</pre>
<p>There are also many other possibilities.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>2000 20000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>2000 0 0
</pre></section>
</div>
</span> | [
[
"9 45000\n",
"9 45000\n"
]
] |
p03472 | AtCoder Beginner Contest 085 - Katana Thrower | <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 a monster. Fortunately, you have <var>N</var> katana (swords), Katana <var>1</var>, Katana <var>2</var>, <var>…</var>, Katana <var>N</var>, and can perform the following two kinds of attacks in any order:</p>
<ul>
<li>Wield one of the katana you have. When you wield Katana <var>i</var> <var>(1 ≤ i ≤ N)</var>, the monster receives <var>a_i</var> points of damage. The same katana can be wielded any number of times.</li>
<li>Throw one of the katana you have. When you throw Katana <var>i</var> <var>(1 ≤ i ≤ N)</var> at the monster, it receives <var>b_i</var> points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.</li>
</ul>
<p>The monster will vanish when the total damage it has received is <var>H</var> points or more. At least how many attacks do you need in order to vanish it in total?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ H ≤ 10^9</var></li>
<li><var>1 ≤ a_i ≤ b_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>H</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 minimum total number of attacks required to vanish the monster.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1 10
3 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>You have one katana. Wielding it deals <var>3</var> points of damage, and throwing it deals <var>5</var> points of damage. By wielding it twice and then throwing it, you will deal <var>3 + 3 + 5 = 11</var> points of damage in a total of three attacks, vanishing the monster.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 10
3 5
2 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p>In addition to the katana above, you also have another katana. Wielding it deals <var>2</var> points of damage, and throwing it deals <var>6</var> points of damage. By throwing both katana, you will deal <var>5 + 6 = 11</var> points of damage in two attacks, vanishing the monster.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4 1000000000
1 1
1 10000000
1 30000000
1 99999999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>860000004
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>5 500
35 44
28 83
46 62
31 79
40 43
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>9
</pre></section>
</div>
</span> | [
[
"1 10\n3 5\n",
"1 10\n3 5\n"
]
] |
p03473 | AtCoder Beginner Contest 084 - New Year | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>How many hours do we have until New Year at <var>M</var> o'clock (24-hour notation) on <var>30</var>th, December?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤M≤23</var></li>
<li><var>M</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>M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If we have <var>x</var> hours until New Year at <var>M</var> o'clock on <var>30</var>th, December, print <var>x</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>21
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>27
</pre>
<p>We have <var>27</var> hours until New Year at <var>21</var> o'clock on <var>30</var>th, December.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>12
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>36
</pre></section>
</div>
</span> | [
[
"21\n",
"21\n"
]
] |
p03474 | AtCoder Beginner Contest 084 - Postal Code | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>The postal code in Atcoder Kingdom is <var>A+B+1</var> characters long, its <var>(A+1)</var>-th character is a hyphen <code>-</code>, and the other characters are digits from <code>0</code> through <code>9</code>.</p>
<p>You are given a string <var>S</var>. Determine whether it follows the postal code format in Atcoder Kingdom.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤A,B≤5</var></li>
<li><var>|S|=A+B+1</var></li>
<li><var>S</var> consists of <code>-</code> and digits from <code>0</code> through <code>9</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>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Yes</code> if <var>S</var> follows the postal code format in AtCoder Kingdom; print <code>No</code> otherwise.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 4
269-6650
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>The <var>(A+1)</var>-th character of <var>S</var> is <code>-</code>, and the other characters are digits from <code>0</code> through <code>9</code>, so it follows the format.</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>No
</pre>
<p><var>S</var> contains unnecessary <code>-</code>s other than the <var>(A+1)</var>-th character, so it does not follow the format.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 2
7444
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"3 4\n269-6650\n",
"3 4\n269-6650\n"
]
] |
p03475 | AtCoder Beginner Contest 084 - Special Trains | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A railroad running from west to east in Atcoder Kingdom is now complete.</p>
<p>There are <var>N</var> stations on the railroad, numbered <var>1</var> through <var>N</var> from west to east.</p>
<p>Tomorrow, the opening ceremony of the railroad will take place.</p>
<p>On this railroad, for each integer <var>i</var> such that <var>1≤i≤N-1</var>, there will be trains that run from Station <var>i</var> to Station <var>i+1</var> in <var>C_i</var> seconds. No other trains will be operated.</p>
<p>The first train from Station <var>i</var> to Station <var>i+1</var> will depart Station <var>i</var> <var>S_i</var> seconds after the ceremony begins. Thereafter, there will be a train that departs Station <var>i</var> every <var>F_i</var> seconds.</p>
<p>Here, it is guaranteed that <var>F_i</var> divides <var>S_i</var>.</p>
<p>That is, for each Time <var>t</var> satisfying <var>S_i≤t</var> and <var>t%F_i=0</var>, there will be a train that departs Station <var>i</var> <var>t</var> seconds after the ceremony begins and arrives at Station <var>i+1</var> <var>t+C_i</var> seconds after the ceremony begins, where <var>A%B</var> denotes <var>A</var> modulo <var>B</var>, and there will be no other trains.</p>
<p>For each <var>i</var>, find the earliest possible time we can reach Station <var>N</var> if we are at Station <var>i</var> when the ceremony begins, ignoring the time needed to change trains.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤N≤500</var></li>
<li><var>1≤C_i≤100</var></li>
<li><var>1≤S_i≤10^5</var></li>
<li><var>1≤F_i≤10</var></li>
<li><var>S_i%F_i=0</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>C_1</var> <var>S_1</var> <var>F_1</var>
<var>:</var>
<var>C_{N-1}</var> <var>S_{N-1}</var> <var>F_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>N</var> lines. Assuming that we are at Station <var>i</var> <var>(1≤i≤N)</var> when the ceremony begins, if the earliest possible time we can reach Station <var>N</var> is <var>x</var> seconds after the ceremony begins, the <var>i</var>-th line should contain <var>x</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
6 5 1
1 10 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>12
11
0
</pre>
<p>We will travel from Station <var>1</var> as follows:</p>
<ul>
<li><var>5</var> seconds after the beginning: take the train to Station <var>2</var>.</li>
<li><var>11</var> seconds: arrive at Station <var>2</var>.</li>
<li><var>11</var> seconds: take the train to Station <var>3</var>.</li>
<li><var>12</var> seconds: arrive at Station <var>3</var>.</li>
</ul>
<p>We will travel from Station <var>2</var> as follows:</p>
<ul>
<li><var>10</var> seconds: take the train to Station <var>3</var>.</li>
<li><var>11</var> seconds: arrive at Station <var>3</var>.</li>
</ul>
<p>Note that we should print <var>0</var> for Station <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
12 24 6
52 16 4
99 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>187
167
101
0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4
12 13 1
44 17 17
66 4096 64
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4162
4162
4162
0
</pre></section>
</div>
</span> | [
[
"3\n6 5 1\n1 10 1\n",
"3\n6 5 1\n1 10 1\n"
]
] |
p03476 | AtCoder Beginner Contest 084 - 2017-like Number | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We say that a odd number <var>N</var> is <em>similar to 2017</em> when both <var>N</var> and <var>(N+1)/2</var> are prime.</p>
<p>You are given <var>Q</var> queries.</p>
<p>In the <var>i</var>-th query, given two odd numbers <var>l_i</var> and <var>r_i</var>, find the number of odd numbers <var>x</var> similar to 2017 such that <var>l_i ≤ x ≤ r_i</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤Q≤10^5</var></li>
<li><var>1≤l_i≤r_i≤10^5</var></li>
<li><var>l_i</var> and <var>r_i</var> are odd.</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>Q</var>
<var>l_1</var> <var>r_1</var>
<var>:</var>
<var>l_Q</var> <var>r_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line <var>(1≤i≤Q)</var> should contain the response to the <var>i</var>-th query.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1
3 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<ul>
<li><var>3</var> is similar to 2017, since both <var>3</var> and <var>(3+1)/2=2</var> are prime.</li>
<li><var>5</var> is similar to 2017, since both <var>5</var> and <var>(5+1)/2=3</var> are prime.</li>
<li><var>7</var> is not similar to 2017, since <var>(7+1)/2=4</var> is not prime, although <var>7</var> is prime.</li>
</ul>
<p>Thus, the response to the first query should be <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
13 13
7 11
7 11
2017 2017
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
0
0
1
</pre>
<p>Note that <var>2017</var> is also similar to 2017.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
1 53
13 91
37 55
19 51
73 91
13 49
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
4
1
1
1
2
</pre></section>
</div>
</span> | [
[
"1\n3 7\n",
"1\n3 7\n"
]
] |
p03477 | AtCoder Beginner Contest 083 - Libra | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A balance scale tips to the left if <var>L>R</var>, where <var>L</var> is the total weight of the masses on the left pan and <var>R</var> is the total weight of the masses on the right pan. Similarly, it balances if <var>L=R</var>, and tips to the right if <var>L<R</var>.</p>
<p>Takahashi placed a mass of weight <var>A</var> and a mass of weight <var>B</var> on the left pan of a balance scale, and placed a mass of weight <var>C</var> and a mass of weight <var>D</var> on the right pan.</p>
<p>Print <code>Left</code> if the balance scale tips to the left; print <code>Balanced</code> if it balances; print <code>Right</code> if it tips to the right.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1\leq A,B,C,D \leq 10</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>A</var> <var>B</var> <var>C</var> <var>D</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Left</code> if the balance scale tips to the left; print <code>Balanced</code> if it balances; print <code>Right</code> if it tips to the right.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 8 7 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Left
</pre>
<p>The total weight of the masses on the left pan is <var>11</var>, and the total weight of the masses on the right pan is <var>8</var>. Since <var>11>8</var>, we should print <code>Left</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 4 5 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Balanced
</pre>
<p>The total weight of the masses on the left pan is <var>7</var>, and the total weight of the masses on the right pan is <var>7</var>. Since <var>7=7</var>, we should print <code>Balanced</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 7 6 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Right
</pre>
<p>The total weight of the masses on the left pan is <var>8</var>, and the total weight of the masses on the right pan is <var>10</var>. Since <var>8<10</var>, we should print <code>Right</code>.</p></section>
</div>
</span> | [
[
"3 8 7 1\n",
"3 8 7 1\n"
]
] |
p03478 | AtCoder Beginner Contest 083 - Some Sums | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Find the sum of the integers between <var>1</var> and <var>N</var> (inclusive), whose sum of digits written in base <var>10</var> is between <var>A</var> and <var>B</var> (inclusive).</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^4</var></li>
<li><var>1 \leq A \leq B \leq 36</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>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of the integers between <var>1</var> and <var>N</var> (inclusive), whose sum of digits written in base <var>10</var> is between <var>A</var> and <var>B</var> (inclusive).</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>20 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>84
</pre>
<p>Among the integers not greater than <var>20</var>, the ones whose sums of digits are between <var>2</var> and <var>5</var>, are: <var>2,3,4,5,11,12,13,14</var> and <var>20</var>. We should print the sum of these, <var>84</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>13
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100 4 16
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4554
</pre></section>
</div>
</span> | [
[
"20 2 5\n",
"20 2 5\n"
]
] |
p03479 | AtCoder Beginner Contest 083 - Multiple Gift | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence <var>A</var> needs to satisfy the conditions below:</p>
<ul>
<li><var>A</var> consists of integers between <var>X</var> and <var>Y</var> (inclusive).</li>
<li>For each <var>1\leq i \leq |A|-1</var>, <var>A_{i+1}</var> is a multiple of <var>A_i</var> and strictly greater than <var>A_i</var>.</li>
</ul>
<p>Find the maximum possible length of the sequence.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq X \leq Y \leq 10^{18}</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>X</var> <var>Y</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible length of the sequence.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 20
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The sequence <var>3,6,18</var> satisfies the conditions.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>25 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>314159265 358979323846264338
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>31
</pre></section>
</div>
</span> | [
[
"3 20\n",
"3 20\n"
]
] |
p03480 | AtCoder Beginner Contest 083 - Wide Flip | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of <code>0</code> and <code>1</code>.
Find the maximum integer <var>K</var> not greater than <var>|S|</var> such that we can turn all the characters of <var>S</var> into <code>0</code> by repeating the following operation some number of times.</p>
<ul>
<li>Choose a contiguous segment <var>[l,r]</var> in <var>S</var> whose length is at least <var>K</var> (that is, <var>r-l+1\geq K</var> must be satisfied). For each integer <var>i</var> such that <var>l\leq i\leq r</var>, do the following: if <var>S_i</var> is <code>0</code>, replace it with <code>1</code>; if <var>S_i</var> is <code>1</code>, replace it with <code>0</code>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1\leq |S|\leq 10^5</var></li>
<li><var>S_i(1\leq i\leq N)</var> is either <code>0</code> or <code>1</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>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum integer <var>K</var> such that we can turn all the characters of <var>S</var> into <code>0</code> by repeating the operation some number of times.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>010
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>We can turn all the characters of <var>S</var> into <code>0</code> by the following operations:</p>
<ul>
<li>Perform the operation on the segment <var>S[1,3]</var> with length <var>3</var>. <var>S</var> is now <code>101</code>.</li>
<li>Perform the operation on the segment <var>S[1,2]</var> with length <var>2</var>. <var>S</var> is now <code>011</code>.</li>
<li>Perform the operation on the segment <var>S[2,3]</var> with length <var>2</var>. <var>S</var> is now <code>000</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>8
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>00001111
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"010\n",
"010\n"
]
] |
p03481 | AtCoder Regular Contest 088 - Multiple Gift | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence <var>A</var> needs to satisfy the conditions below:</p>
<ul>
<li><var>A</var> consists of integers between <var>X</var> and <var>Y</var> (inclusive).</li>
<li>For each <var>1\leq i \leq |A|-1</var>, <var>A_{i+1}</var> is a multiple of <var>A_i</var> and strictly greater than <var>A_i</var>.</li>
</ul>
<p>Find the maximum possible length of the sequence.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq X \leq Y \leq 10^{18}</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>X</var> <var>Y</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible length of the sequence.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 20
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The sequence <var>3,6,18</var> satisfies the conditions.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>25 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>314159265 358979323846264338
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>31
</pre></section>
</div>
</span> | [
[
"3 20\n",
"3 20\n"
]
] |
p03482 | AtCoder Regular Contest 088 - Wide Flip | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of <code>0</code> and <code>1</code>.
Find the maximum integer <var>K</var> not greater than <var>|S|</var> such that we can turn all the characters of <var>S</var> into <code>0</code> by repeating the following operation some number of times.</p>
<ul>
<li>Choose a contiguous segment <var>[l,r]</var> in <var>S</var> whose length is at least <var>K</var> (that is, <var>r-l+1\geq K</var> must be satisfied). For each integer <var>i</var> such that <var>l\leq i\leq r</var>, do the following: if <var>S_i</var> is <code>0</code>, replace it with <code>1</code>; if <var>S_i</var> is <code>1</code>, replace it with <code>0</code>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1\leq |S|\leq 10^5</var></li>
<li><var>S_i(1\leq i\leq N)</var> is either <code>0</code> or <code>1</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>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum integer <var>K</var> such that we can turn all the characters of <var>S</var> into <code>0</code> by repeating the operation some number of times.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>010
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>We can turn all the characters of <var>S</var> into <code>0</code> by the following operations:</p>
<ul>
<li>Perform the operation on the segment <var>S[1,3]</var> with length <var>3</var>. <var>S</var> is now <code>101</code>.</li>
<li>Perform the operation on the segment <var>S[1,2]</var> with length <var>2</var>. <var>S</var> is now <code>011</code>.</li>
<li>Perform the operation on the segment <var>S[2,3]</var> with length <var>2</var>. <var>S</var> is now <code>000</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>8
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>00001111
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"010\n",
"010\n"
]
] |
p03483 | AtCoder Regular Contest 088 - Papple Sort | <span class="lang-en">
<p>Score : <var>800</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 we can turn <var>S</var> into a palindrome by repeating the operation of swapping two adjacent characters. If it is possible, find the minimum required number of operations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 2 × 10^5</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 we cannot turn <var>S</var> into a palindrome, print <code>-1</code>. Otherwise, print the minimum required number of operations.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>eel
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>We can turn <var>S</var> into a palindrome by the following operation:</p>
<ul>
<li>Swap the <var>2</var>-nd and <var>3</var>-rd characters. <var>S</var> is now <code>ele</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>ataatmma
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>4
</pre>
<p>We can turn <var>S</var> into a palindrome by the following operation:</p>
<ul>
<li>Swap the <var>5</var>-th and <var>6</var>-th characters. <var>S</var> is now <code>ataamtma</code>.</li>
<li>Swap the <var>4</var>-th and <var>5</var>-th characters. <var>S</var> is now <code>atamatma</code>.</li>
<li>Swap the <var>3</var>-rd and <var>4</var>-th characters. <var>S</var> is now <code>atmaatma</code>.</li>
<li>Swap the <var>2</var>-nd and <var>3</var>-rd characters. <var>S</var> is now <code>amtaatma</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>snuke
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-1
</pre>
<p>We cannot turn <var>S</var> into a palindrome.</p></section>
</div>
</span> | [
[
"eel\n",
"eel\n"
]
] |
p03484 | AtCoder Regular Contest 088 - Christmas Tree | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has decided to make a <em>Christmas Tree</em> for the Christmas party in AtCoder, Inc.</p>
<p>A Christmas Tree is a tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var> and <var>N-1</var> edges, whose <var>i</var>-th edge <var>(1\leq i\leq N-1)</var> connects Vertex <var>a_i</var> and <var>b_i</var>.</p>
<p>He would like to make one as follows:</p>
<ul>
<li>Specify two non-negative integers <var>A</var> and <var>B</var>.</li>
<li>Prepare <var>A</var> <em>Christmas Paths</em> whose lengths are at most <var>B</var>. Here, a Christmas Path of length <var>X</var> is a graph with <var>X+1</var> vertices and <var>X</var> edges such that, if we properly number the vertices <var>1</var> through <var>X+1</var>, the <var>i</var>-th edge <var>(1\leq i\leq X)</var> will connect Vertex <var>i</var> and <var>i+1</var>.</li>
<li>Repeat the following operation until he has one connected tree:<ul>
<li>Select two vertices <var>x</var> and <var>y</var> that belong to different connected components. Combine <var>x</var> and <var>y</var> into one vertex. More precisely, for each edge <var>(p,y)</var> incident to the vertex <var>y</var>, add the edge <var>(p,x)</var>. Then, delete the vertex <var>y</var> and all the edges incident to <var>y</var>.</li>
</ul>
</li>
<li>Properly number the vertices in the tree.</li>
</ul>
<p>Takahashi would like to find the lexicographically smallest pair <var>(A,B)</var> such that he can make a Christmas Tree, that is, find the smallest <var>A</var>, and find the smallest <var>B</var> under the condition that <var>A</var> is minimized.</p>
<p>Solve this problem for him.</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>a_{N-1}</var> <var>b_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>For the lexicographically smallest <var>(A,B)</var>, print <var>A</var> and <var>B</var> with a space in between.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7
1 2
2 3
2 4
4 5
4 6
6 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3 2
</pre>
<p>We can make a Christmas Tree as shown in the figure below:</p>
<p><img alt="" src="https://img.atcoder.jp/arc088/96f78221624d6a13628f6052f5db697d.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8
1 2
2 3
3 4
4 5
5 6
5 7
5 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2 5
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
1 2
2 3
3 4
2 5
6 5
6 7
7 8
5 9
10 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3 4
</pre></section>
</div>
</span> | [
[
"7\n1 2\n2 3\n2 4\n4 5\n4 6\n6 7\n",
"7\n1 2\n2 3\n2 4\n4 5\n4 6\n6 7\n"
]
] |
p03485 | AtCoder Beginner Contest 082 - Round Up the Mean | <span class="lang-en">
<p>Score : <var>100</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>.
Let <var>x</var> be the average of <var>a</var> and <var>b</var>.
Print <var>x</var> rounded up to the nearest integer.</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 \leq a, b \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>a</var> <var>b</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>x</var> rounded up to the nearest integer.</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>2
</pre>
<p>The average of <var>1</var> and <var>3</var> is <var>2.0</var>, and it will be rounded up to the nearest integer, <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>7 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
</pre>
<p>The average of <var>7</var> and <var>4</var> is <var>5.5</var>, and it will be rounded up to the nearest integer, <var>6</var>.</p>
</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>5
</pre></section>
</div>
</span> | [
[
"1 3\n",
"1 3\n"
]
] |
p03486 | AtCoder Beginner Contest 082 - Two Anagrams | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given strings <var>s</var> and <var>t</var>, consisting of lowercase English letters.
You will create a string <var>s'</var> by freely rearranging the characters in <var>s</var>.
You will also create a string <var>t'</var> by freely rearranging the characters in <var>t</var>.
Determine whether it is possible to satisfy <var>s' < t'</var> for the lexicographic order.</p>
</section>
</div>
<div class="part">
<section>
<h3>Notes</h3><p>For a string <var>a = a_1 a_2 ... a_N</var> of length <var>N</var> and a string <var>b = b_1 b_2 ... b_M</var> of length <var>M</var>, we say <var>a < b</var> for the lexicographic order if either one of the following two conditions holds true:</p>
<ul>
<li><var>N < M</var> and <var>a_1 = b_1</var>, <var>a_2 = b_2</var>, ..., <var>a_N = b_N</var>.</li>
<li>There exists <var>i</var> (<var>1 \leq i \leq N, M</var>) such that <var>a_1 = b_1</var>, <var>a_2 = b_2</var>, ..., <var>a_{i - 1} = b_{i - 1}</var> and <var>a_i < b_i</var>. Here, letters are compared using alphabetical order.</li>
</ul>
<p>For example, <code>xy</code> <var><</var> <code>xya</code> and <code>atcoder</code> <var><</var> <code>atlas</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>The lengths of <var>s</var> and <var>t</var> are between <var>1</var> and <var>100</var> (inclusive).</li>
<li><var>s</var> and <var>t</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>
<var>t</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If it is possible to satisfy <var>s' < t'</var>, print <code>Yes</code>; if it is not, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>yx
axy
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>We can, for example, rearrange <code>yx</code> into <code>xy</code> and <code>axy</code> into <code>yxa</code>. Then, <code>xy</code> <var><</var> <code>yxa</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>ratcode
atlas
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yes
</pre>
<p>We can, for example, rearrange <code>ratcode</code> into <code>acdeort</code> and <code>atlas</code> into <code>tslaa</code>. Then, <code>acdeort</code> <var><</var> <code>tslaa</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>cd
abc
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre>
<p>No matter how we rearrange <code>cd</code> and <code>abc</code>, we cannot achieve our objective.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>w
ww
</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>zzz
zzz
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>No
</pre></section>
</div>
</span> | [
[
"yx\naxy\n",
"yx\naxy\n"
]
] |
p03487 | AtCoder Beginner Contest 082 - Good Sequence | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a sequence of positive integers of length <var>N</var>, <var>a = (a_1, a_2, ..., a_N)</var>.
Your objective is to remove some of the elements in <var>a</var> so that <var>a</var> will be a <strong>good sequence</strong>.</p>
<p>Here, an sequence <var>b</var> is a <strong>good sequence</strong> when the following condition holds true:</p>
<ul>
<li>For each element <var>x</var> in <var>b</var>, the value <var>x</var> occurs exactly <var>x</var> times in <var>b</var>.</li>
</ul>
<p>For example, <var>(3, 3, 3)</var>, <var>(4, 2, 4, 1, 4, 2, 4)</var> and <var>()</var> (an empty sequence) are good sequences, while <var>(3, 3, 3, 3)</var> and <var>(2, 4, 1, 4, 2)</var> are not.</p>
<p>Find the minimum number of elements that needs to be removed so that <var>a</var> will be a good sequence.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>a_i</var> is an integer.</li>
<li><var>1 \leq a_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>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of elements that needs to be removed so that <var>a</var> will be a good sequence.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
3 3 3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>We can, for example, remove one occurrence of <var>3</var>. Then, <var>(3, 3, 3)</var> is a good sequence.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
2 4 1 4 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p>We can, for example, remove two occurrences of <var>4</var>. Then, <var>(2, 1, 2)</var> is a good sequence.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
1 2 2 3 3 3
</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
1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>1
</pre>
<p>Remove one occurrence of <var>10^9</var>. Then, <var>()</var> is a good sequence.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>8
2 7 1 8 2 8 1 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>5
</pre></section>
</div>
</span> | [
[
"4\n3 3 3 3\n",
"4\n3 3 3 3\n"
]
] |
p03488 | AtCoder Beginner Contest 082 - FT Robot | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive <var>x</var>-axis direction.</p>
<p>This robot will be given an instruction sequence <var>s</var>.
<var>s</var> consists of the following two kinds of letters, and will be executed in order from front to back.</p>
<ul>
<li><code>F</code> : Move in the current direction by distance <var>1</var>.</li>
<li><code>T</code> : Turn <var>90</var> degrees, either clockwise or counterclockwise.</li>
</ul>
<p>The objective of the robot is to be at coordinates <var>(x, y)</var> after all the instructions are executed.
Determine whether this objective is achievable.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>s</var> consists of <code>F</code> and <code>T</code>.</li>
<li><var>1 \leq |s| \leq 8</var> <var>000</var></li>
<li><var>x</var> and <var>y</var> are integers.</li>
<li><var>|x|, |y| \leq |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>
<var>x</var> <var>y</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the objective is achievable, print <code>Yes</code>; if it is not, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>FTFFTFFF
4 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>The objective can be achieved by, for example, turning counterclockwise in the first <code>T</code> and turning clockwise in the second <code>T</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>FTFFTFFF
-2 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yes
</pre>
<p>The objective can be achieved by, for example, turning clockwise in the first <code>T</code> and turning clockwise in the second <code>T</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>FF
1 0
</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>TF
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>FFTTFF
0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>Yes
</pre>
<p>The objective can be achieved by, for example, turning counterclockwise in the first <code>T</code> and turning counterclockwise in the second <code>T</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>TTTT
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>No
</pre></section>
</div>
</span> | [
[
"FTFFTFFF\n4 2\n",
"FTFFTFFF\n4 2\n"
]
] |
p03489 | AtCoder Regular Contest 087 - Good Sequence | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a sequence of positive integers of length <var>N</var>, <var>a = (a_1, a_2, ..., a_N)</var>.
Your objective is to remove some of the elements in <var>a</var> so that <var>a</var> will be a <strong>good sequence</strong>.</p>
<p>Here, an sequence <var>b</var> is a <strong>good sequence</strong> when the following condition holds true:</p>
<ul>
<li>For each element <var>x</var> in <var>b</var>, the value <var>x</var> occurs exactly <var>x</var> times in <var>b</var>.</li>
</ul>
<p>For example, <var>(3, 3, 3)</var>, <var>(4, 2, 4, 1, 4, 2, 4)</var> and <var>()</var> (an empty sequence) are good sequences, while <var>(3, 3, 3, 3)</var> and <var>(2, 4, 1, 4, 2)</var> are not.</p>
<p>Find the minimum number of elements that needs to be removed so that <var>a</var> will be a good sequence.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>a_i</var> is an integer.</li>
<li><var>1 \leq a_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>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of elements that needs to be removed so that <var>a</var> will be a good sequence.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
3 3 3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>We can, for example, remove one occurrence of <var>3</var>. Then, <var>(3, 3, 3)</var> is a good sequence.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
2 4 1 4 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p>We can, for example, remove two occurrences of <var>4</var>. Then, <var>(2, 1, 2)</var> is a good sequence.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
1 2 2 3 3 3
</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
1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>1
</pre>
<p>Remove one occurrence of <var>10^9</var>. Then, <var>()</var> is a good sequence.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>8
2 7 1 8 2 8 1 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>5
</pre></section>
</div>
</span> | [
[
"4\n3 3 3 3\n",
"4\n3 3 3 3\n"
]
] |
p03490 | AtCoder Regular Contest 087 - FT Robot | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive <var>x</var>-axis direction.</p>
<p>This robot will be given an instruction sequence <var>s</var>.
<var>s</var> consists of the following two kinds of letters, and will be executed in order from front to back.</p>
<ul>
<li><code>F</code> : Move in the current direction by distance <var>1</var>.</li>
<li><code>T</code> : Turn <var>90</var> degrees, either clockwise or counterclockwise.</li>
</ul>
<p>The objective of the robot is to be at coordinates <var>(x, y)</var> after all the instructions are executed.
Determine whether this objective is achievable.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>s</var> consists of <code>F</code> and <code>T</code>.</li>
<li><var>1 \leq |s| \leq 8</var> <var>000</var></li>
<li><var>x</var> and <var>y</var> are integers.</li>
<li><var>|x|, |y| \leq |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>
<var>x</var> <var>y</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the objective is achievable, print <code>Yes</code>; if it is not, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>FTFFTFFF
4 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>The objective can be achieved by, for example, turning counterclockwise in the first <code>T</code> and turning clockwise in the second <code>T</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>FTFFTFFF
-2 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yes
</pre>
<p>The objective can be achieved by, for example, turning clockwise in the first <code>T</code> and turning clockwise in the second <code>T</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>FF
1 0
</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>TF
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>FFTTFF
0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>Yes
</pre>
<p>The objective can be achieved by, for example, turning counterclockwise in the first <code>T</code> and turning counterclockwise in the second <code>T</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>TTTT
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>No
</pre></section>
</div>
</span> | [
[
"FTFFTFFF\n4 2\n",
"FTFFTFFF\n4 2\n"
]
] |
p03491 | AtCoder Regular Contest 087 - Prefix-free Game | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>For strings <var>s</var> and <var>t</var>, we will say that <var>s</var> and <var>t</var> are <em>prefix-free</em> when neither is a prefix of the other.</p>
<p>Let <var>L</var> be a positive integer. A set of strings <var>S</var> is a <em>good string set</em> when the following conditions hold true:</p>
<ul>
<li>Each string in <var>S</var> has a length between <var>1</var> and <var>L</var> (inclusive) and consists of the characters <code>0</code> and <code>1</code>.</li>
<li>Any two distinct strings in <var>S</var> are prefix-free.</li>
</ul>
<p>We have a good string set <var>S = \{ s_1, s_2, ..., s_N \}</var>. Alice and Bob will play a game against each other. They will alternately perform the following operation, starting from Alice:</p>
<ul>
<li>Add a new string to <var>S</var>. After addition, <var>S</var> must still be a good string set.</li>
</ul>
<p>The first player who becomes unable to perform the operation loses the game. Determine the winner of the game when both players play optimally.</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 L \leq 10^{18}</var></li>
<li><var>s_1</var>, <var>s_2</var>, ..., <var>s_N</var> are all distinct.</li>
<li>{ <var>s_1</var>, <var>s_2</var>, ..., <var>s_N</var> } is a good string set.</li>
<li><var>|s_1| + |s_2| + ... + |s_N| \leq 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>L</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>If Alice will win, print <code>Alice</code>; if Bob will win, print <code>Bob</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
00
01
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Alice
</pre>
<p>If Alice adds <code>1</code>, Bob will be unable to add a new string.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 2
00
11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Bob
</pre>
<p>There are two strings that Alice can add on the first turn: <code>01</code> and <code>10</code>.
In case she adds <code>01</code>, if Bob add <code>10</code>, she will be unable to add a new string.
Also, in case she adds <code>10</code>, if Bob add <code>01</code>, she will be unable to add a new string.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3 3
0
10
110
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Alice
</pre>
<p>If Alice adds <code>111</code>, Bob will be unable to add a new string.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>2 1
0
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>Bob
</pre>
<p>Alice is unable to add a new string on the first turn.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>1 2
11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>Alice
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>2 3
101
11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>Bob
</pre></section>
</div>
</span> | [
[
"2 2\n00\n01\n",
"2 2\n00\n01\n"
]
] |
p03492 | AtCoder Regular Contest 087 - Squirrel Migration | <span class="lang-en">
<p>Score : <var>800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices. The vertices are numbered <var>1</var> through <var>N</var>. The <var>i</var>-th edge (<var>1 \leq i \leq N - 1</var>) connects Vertex <var>x_i</var> and <var>y_i</var>. For vertices <var>v</var> and <var>w</var> (<var>1 \leq v, w \leq N</var>), we will define the distance between <var>v</var> and <var>w</var> <var>d(v, w)</var> as "the number of edges contained in the path <var>v</var>-<var>w</var>".</p>
<p>A squirrel lives in each vertex of the tree. They are planning to move, as follows. First, they will freely choose a permutation of <var>(1, 2, ..., N)</var>, <var>p = (p_1, p_2, ..., p_N)</var>. Then, for each <var>1 \leq i \leq N</var>, the squirrel that lived in Vertex <var>i</var> will move to Vertex <var>p_i</var>.</p>
<p>Since they like long travels, they have decided to maximize the total distance they traveled during the process. That is, they will choose <var>p</var> so that <var>d(1, p_1) + d(2, p_2) + ... + d(N, p_N)</var> will be maximized. How many such ways are there to choose <var>p</var>, modulo <var>10^9 + 7</var>?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 5,000</var></li>
<li><var>1 \leq x_i, y_i \leq N</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>x_1</var> <var>y_1</var>
<var>x_2</var> <var>y_2</var>
<var>:</var>
<var>x_{N - 1}</var> <var>y_{N - 1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the ways to choose <var>p</var> so that the condition is satisfied, modulo <var>10^9 + 7</var>.</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>3
</pre>
<p>The maximum possible distance traveled by squirrels is <var>4</var>.
There are three choices of <var>p</var> that achieve it, as follows:</p>
<ul>
<li><var>(2, 3, 1)</var></li>
<li><var>(3, 1, 2)</var></li>
<li><var>(3, 2, 1)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 2
1 3
1 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>11
</pre>
<p>The maximum possible distance traveled by squirrels is <var>6</var>.
For example, <var>p = (2, 1, 4, 3)</var> achieves it.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
1 2
1 3
1 4
2 5
2 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>36
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>7
1 2
6 3
4 5
1 7
1 5
2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>396
</pre></section>
</div>
</span> | [
[
"3\n1 2\n2 3\n",
"3\n1 2\n2 3\n"
]
] |
p03493 | AtCoder Beginner Contest 081 - Placing Marbles | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has a grid consisting of three squares numbered <var>1</var>, <var>2</var> and <var>3</var>.
In each square, either <code>0</code> or <code>1</code> is written. The number written in Square <var>i</var> is <var>s_i</var>.</p>
<p>Snuke will place a marble on each square that says <code>1</code>.
Find the number of squares on which Snuke will place a marble.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>Each of <var>s_1</var>, <var>s_2</var> and <var>s_3</var> is either <code>1</code> or <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>s_{1}s_{2}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>101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<ul>
<li>A marble will be placed on Square <var>1</var> and <var>3</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<ul>
<li>No marble will be placed on any square.</li>
</ul></section>
</div>
</span> | [
[
"101\n",
"101\n"
]
] |
p03494 | AtCoder Beginner Contest 081 - Shift only | <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> positive integers written on a blackboard: <var>A_1, ..., A_N</var>.</p>
<p>Snuke can perform the following operation when all integers on the blackboard are even:</p>
<ul>
<li>Replace each integer <var>X</var> on the blackboard by <var>X</var> divided by <var>2</var>.</li>
</ul>
<p>Find the maximum possible number of operations that Snuke can perform.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 200</var></li>
<li><var>1 \leq A_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>A_2</var> ... <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible number of operations that Snuke can perform.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
8 12 40
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Initially, <var>[8, 12, 40]</var> are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.</p>
<p>After the operation is performed once, <var>[4, 6, 20]</var> are written on the blackboard.
Since all those integers are again even, he can perform the operation.</p>
<p>After the operation is performed twice, <var>[2, 3, 10]</var> are written on the blackboard.
Now, there is an odd number <var>3</var> on the blackboard, so he cannot perform the operation any more.</p>
<p>Thus, Snuke can perform the operation at most twice.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
5 6 8 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since there is an odd number <var>5</var> on the blackboard already in the beginning, Snuke cannot perform the operation at all.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
382253568 723152896 37802240 379425024 404894720 471526144
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre></section>
</div>
</span> | [
[
"3\n8 12 40\n",
"3\n8 12 40\n"
]
] |
p03495 | AtCoder Beginner Contest 081 - Not so Diverse | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has <var>N</var> balls. Initially, an integer <var>A_i</var> is written on the <var>i</var>-th ball.</p>
<p>He would like to rewrite the integer on some balls so that there are at most <var>K</var> different integers written on the <var>N</var> balls.</p>
<p>Find the minimum number of balls that Takahashi needs to rewrite the integers on them.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K \leq N \leq 200000</var></li>
<li><var>1 \leq A_i \leq 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>A_2</var> ... <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of balls that Takahashi needs to rewrite the integers on them.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 2
1 1 2 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>For example, if we rewrite the integer on the fifth ball to <var>2</var>, there are two different integers written on the balls: <var>1</var> and <var>2</var>.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 4
1 1 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 3
5 1 3 2 4 1 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre></section>
</div>
</span> | [
[
"5 2\n1 1 2 2 5\n",
"5 2\n1 1 2 2 5\n"
]
] |
p03496 | AtCoder Beginner Contest 081 - Non-decreasing | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence, <var>a</var>, of length <var>N</var>. The <var>i</var>-th element of <var>a</var> (<var>1</var>-indexed) is <var>a_{i}</var>.</p>
<p>He can perform the following operation any number of times:</p>
<ul>
<li>Operation: Choose integers <var>x</var> and <var>y</var> between <var>1</var> and <var>N</var> (inclusive), and add <var>a_x</var> to <var>a_y</var>.</li>
</ul>
<p>He would like to perform this operation between <var>0</var> and <var>2N</var> times (inclusive) so that <var>a</var> satisfies the condition below. Show one such sequence of operations.
It can be proved that such a sequence of operations always exists under the constraints in this problem.</p>
<ul>
<li>Condition: <var>a_1 \leq a_2 \leq ... \leq a_{N}</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 50</var></li>
<li><var>-10^{6} \leq a_i \leq 10^{6}</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_1</var> <var>a_2</var> <var>...</var> <var>a_{N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Let <var>m</var> be the number of operations in your solution. In the first line, print <var>m</var>.
In the <var>i</var>-th of the subsequent <var>m</var> lines, print the numbers <var>x</var> and <var>y</var> chosen in the <var>i</var>-th operation, with a space in between.
The output will be considered correct if <var>m</var> is between <var>0</var> and <var>2N</var> (inclusive) and <var>a</var> satisfies the condition after the <var>m</var> operations.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
-2 5 -1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
2 3
3 3
</pre>
<ul>
<li>After the first operation, <var>a = (-2,5,4)</var>.</li>
<li>After the second operation, <var>a = (-2,5,8)</var>, and the condition is now satisfied.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
-1 -3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
2 1
</pre>
<ul>
<li>After the first operation, <var>a = (-4,-3)</var> and the condition is now satisfied.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
0 0 0 0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<ul>
<li>The condition is satisfied already in the beginning.</li>
</ul></section>
</div>
</span> | [
[
"3\n-2 5 -1\n",
"3\n-2 5 -1\n"
]
] |
p03497 | AtCoder Regular Contest 086 - Not so Diverse | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has <var>N</var> balls. Initially, an integer <var>A_i</var> is written on the <var>i</var>-th ball.</p>
<p>He would like to rewrite the integer on some balls so that there are at most <var>K</var> different integers written on the <var>N</var> balls.</p>
<p>Find the minimum number of balls that Takahashi needs to rewrite the integers on them.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K \leq N \leq 200000</var></li>
<li><var>1 \leq A_i \leq 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>A_2</var> ... <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of balls that Takahashi needs to rewrite the integers on them.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 2
1 1 2 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>For example, if we rewrite the integer on the fifth ball to <var>2</var>, there are two different integers written on the balls: <var>1</var> and <var>2</var>.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 4
1 1 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 3
5 1 3 2 4 1 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre></section>
</div>
</span> | [
[
"5 2\n1 1 2 2 5\n",
"5 2\n1 1 2 2 5\n"
]
] |
p03498 | AtCoder Regular Contest 086 - Non-decreasing | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence, <var>a</var>, of length <var>N</var>. The <var>i</var>-th element of <var>a</var> (<var>1</var>-indexed) is <var>a_{i}</var>.</p>
<p>He can perform the following operation any number of times:</p>
<ul>
<li>Operation: Choose integers <var>x</var> and <var>y</var> between <var>1</var> and <var>N</var> (inclusive), and add <var>a_x</var> to <var>a_y</var>.</li>
</ul>
<p>He would like to perform this operation between <var>0</var> and <var>2N</var> times (inclusive) so that <var>a</var> satisfies the condition below. Show one such sequence of operations.
It can be proved that such a sequence of operations always exists under the constraints in this problem.</p>
<ul>
<li>Condition: <var>a_1 \leq a_2 \leq ... \leq a_{N}</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 50</var></li>
<li><var>-10^{6} \leq a_i \leq 10^{6}</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_1</var> <var>a_2</var> <var>...</var> <var>a_{N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Let <var>m</var> be the number of operations in your solution. In the first line, print <var>m</var>.
In the <var>i</var>-th of the subsequent <var>m</var> lines, print the numbers <var>x</var> and <var>y</var> chosen in the <var>i</var>-th operation, with a space in between.
The output will be considered correct if <var>m</var> is between <var>0</var> and <var>2N</var> (inclusive) and <var>a</var> satisfies the condition after the <var>m</var> operations.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
-2 5 -1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
2 3
3 3
</pre>
<ul>
<li>After the first operation, <var>a = (-2,5,4)</var>.</li>
<li>After the second operation, <var>a = (-2,5,8)</var>, and the condition is now satisfied.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
-1 -3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
2 1
</pre>
<ul>
<li>After the first operation, <var>a = (-4,-3)</var> and the condition is now satisfied.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
0 0 0 0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<ul>
<li>The condition is satisfied already in the beginning.</li>
</ul></section>
</div>
</span> | [
[
"3\n-2 5 -1\n",
"3\n-2 5 -1\n"
]
] |
p03499 | AtCoder Regular Contest 086 - Smuggling Marbles | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has a rooted tree with <var>N+1</var> vertices.
The vertices are numbered <var>0</var> through <var>N</var>, and Vertex <var>0</var> is the root of the tree.
The parent of Vertex <var>i</var> <var>(1 \leq i \leq N)</var> is Vertex <var>p_i</var>.</p>
<p>Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them.
The play begins with placing one marble on some of the vertices, then proceeds as follows:</p>
<ol>
<li>If there is a marble on Vertex <var>0</var>, move the marble into the box.</li>
<li>Move each marble from the vertex to its parent (all at once).</li>
<li>For each vertex occupied by two or more marbles, remove all the marbles from the vertex.</li>
<li>If there exists a vertex with some marbles, go to Step 1. Otherwise, end the play.</li>
</ol>
<p>There are <var>2^{N+1}</var> ways to place marbles on some of the vertices.
For each of them, find <strong>the number of marbles that will be in the box at the end of the play</strong>, and compute the sum of all those numbers modulo <var>1,000,000,007</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N < 2 \times 10^{5}</var></li>
<li><var>0 \leq p_i < i</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>400</var> points, <var>N < 2{,}000</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>...</var> <var>p_{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>2
0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>8
</pre>
<p>When we place a marble on both Vertex <var>1</var> and <var>2</var>, there will be multiple marbles on Vertex <var>0</var> by step 2. In such a case, these marbles will be removed instead of being moved to the box.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
0 1 1 0 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>96
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>31
0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>730395550
</pre>
<p>Be sure to compute the sum modulo <var>1,000,000,007</var>.</p></section>
</div>
</span> | [
[
"2\n0 0\n",
"2\n0 0\n"
]
] |
p03500 | AtCoder Regular Contest 086 - Shift and Decrement | <span class="lang-en">
<p>Score : <var>1200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> non-negative integers written on the blackboard: <var>A_1, ..., A_N</var>.</p>
<p>Snuke can perform the following two operations at most <var>K</var> times in total in any order:</p>
<ul>
<li>Operation A: Replace each integer <var>X</var> on the blackboard with <var>X</var> divided by <var>2</var>, rounded down to the nearest integer.</li>
<li>Operation B: Replace each integer <var>X</var> on the blackboard with <var>X</var> minus <var>1</var>. This operation cannot be performed if one or more <var>0</var>s are written on the blackboard.</li>
</ul>
<p>Find the number of the different possible combinations of integers written on the blackboard after Snuke performs the operations, modulo <var>1,000,000,007</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 200</var></li>
<li><var>1 \leq A_i \leq 10^{18}</var></li>
<li><var>1 \leq K \leq 10^{18}</var></li>
<li><var>A_i</var> and <var>K</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>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 different possible combinations of integers written on the blackboard after Snuke performs the operations, modulo <var>1,000,000,007</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
5 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<p>There are six possible combinations of integers on the blackboard: <var>(1, 1)</var>, <var>(1, 2)</var>, <var>(2, 3)</var>, <var>(3, 5)</var>, <var>(4, 6)</var> and <var>(5, 7)</var>.
For example, <var>(1, 2)</var> can be obtained by performing Operation A and Operation B in this order.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 4
10 13 22
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>20
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 100
10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>11
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10 123456789012345678
228344079825412349 478465001534875275 398048921164061989 329102208281783917 779698519704384319 617456682030809556 561259383338846380 254083246422083141 458181156833851984 502254767369499613
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>164286011
</pre></section>
</div>
</span> | [
[
"2 2\n5 7\n",
"2 2\n5 7\n"
]
] |
p03501 | AtCoder Beginner Contest 080 - Parking | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are parking at a parking lot. You can choose from the following two fee plans:</p>
<ul>
<li>Plan <var>1</var>: The fee will be <var>A×T</var> yen (the currency of Japan) when you park for <var>T</var> hours.</li>
<li>Plan <var>2</var>: The fee will be <var>B</var> yen, regardless of the duration.</li>
</ul>
<p>Find the minimum fee when you park for <var>N</var> hours.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤N≤20</var></li>
<li><var>1≤A≤100</var></li>
<li><var>1≤B≤2000</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>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>When the minimum fee is <var>x</var> yen, print the value of <var>x</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7 17 120
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>119
</pre>
<ul>
<li>If you choose Plan <var>1</var>, the fee will be <var>7×17=119</var> yen.</li>
<li>If you choose Plan <var>2</var>, the fee will be <var>120</var> yen.</li>
</ul>
<p>Thus, the minimum fee is <var>119</var> yen.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 20 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>100
</pre>
<p>The fee might be the same in the two plans.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 18 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>100
</pre></section>
</div>
</span> | [
[
"7 17 120\n",
"7 17 120\n"
]
] |
p03502 | AtCoder Beginner Contest 080 - Harshad Number | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>An integer <var>X</var> is called a Harshad number if <var>X</var> is divisible by <var>f(X)</var>, where <var>f(X)</var> is the sum of the digits in <var>X</var> when written in base <var>10</var>.</p>
<p>Given an integer <var>N</var>, determine whether it is a Harshad number.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1?N?10^8</var></li>
<li><var>N</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>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Yes</code> if <var>N</var> is a Harshad number; print <code>No</code> otherwise.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>12
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p><var>f(12)=1+2=3</var>. Since <var>12</var> is divisible by <var>3</var>, <var>12</var> is a Harshad number.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>57
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p><var>f(57)=5+7=12</var>. Since <var>57</var> is not divisible by <var>12</var>, <var>12</var> is not a Harshad number.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>148
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"12\n",
"12\n"
]
] |
p03503 | AtCoder Beginner Contest 080 - Shopping Street | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Joisino is planning to open a shop in a shopping street.</p>
<p>Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.</p>
<p>There are already <var>N</var> stores in the street, numbered <var>1</var> through <var>N</var>.</p>
<p>You are given information of the business hours of those shops, <var>F_{i,j,k}</var>. If <var>F_{i,j,k}=1</var>, Shop <var>i</var> is open during Period <var>k</var> on Day <var>j</var> (this notation is explained below); if <var>F_{i,j,k}=0</var>, Shop <var>i</var> is closed during that period. Here, the days of the week are denoted as follows. Monday: Day <var>1</var>, Tuesday: Day <var>2</var>, Wednesday: Day <var>3</var>, Thursday: Day <var>4</var>, Friday: Day <var>5</var>. Also, the morning is denoted as Period <var>1</var>, and the afternoon is denoted as Period <var>2</var>.</p>
<p>Let <var>c_i</var> be the number of periods during which both Shop <var>i</var> and Joisino's shop are open. Then, the profit of Joisino's shop will be <var>P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}</var>.</p>
<p>Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤N≤100</var></li>
<li><var>0≤F_{i,j,k}≤1</var></li>
<li>For every integer <var>i</var> such that <var>1≤i≤N</var>, there exists at least one pair <var>(j,k)</var> such that <var>F_{i,j,k}=1</var>.</li>
<li><var>-10^7≤P_{i,j}≤10^7</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>F_{1,1,1}</var> <var>F_{1,1,2}</var> <var>...</var> <var>F_{1,5,1}</var> <var>F_{1,5,2}</var>
<var>:</var>
<var>F_{N,1,1}</var> <var>F_{N,1,2}</var> <var>...</var> <var>F_{N,5,1}</var> <var>F_{N,5,2}</var>
<var>P_{1,0}</var> <var>...</var> <var>P_{1,10}</var>
<var>:</var>
<var>P_{N,0}</var> <var>...</var> <var>P_{N,10}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible profit of Joisino's shop.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1
1 1 0 1 0 0 0 1 0 1
3 4 5 6 7 8 9 -2 -3 4 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>8
</pre>
<p>If her shop is open only during the periods when Shop <var>1</var> is opened, the profit will be <var>8</var>, which is the maximum possible profit.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-2
</pre>
<p>Note that a shop must be open during at least one period, and the profit may be negative.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
1 1 1 1 1 1 0 0 1 1
0 1 0 1 1 1 1 0 1 0
1 0 1 1 0 1 0 1 0 1
-8 6 -2 -8 -8 4 8 7 -6 2 2
-9 2 0 1 7 -5 0 -2 -6 5 5
6 -6 7 -9 6 -5 8 0 -9 -7 -7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>23
</pre></section>
</div>
</span> | [
[
"1\n1 1 0 1 0 0 0 1 0 1\n3 4 5 6 7 8 9 -2 -3 4 -2\n",
"1\n1 1 0 1 0 0 0 1 0 1\n3 4 5 6 7 8 9 -2 -3 4 -2\n"
]
] |
p03504 | AtCoder Beginner Contest 080 - Recording | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Joisino is planning to record <var>N</var> TV programs with recorders.</p>
<p>The TV can receive <var>C</var> channels numbered <var>1</var> through <var>C</var>.</p>
<p>The <var>i</var>-th program that she wants to record will be broadcast from time <var>s_i</var> to time <var>t_i</var> (including time <var>s_i</var> but not <var>t_i</var>) on Channel <var>c_i</var>.</p>
<p>Here, there will never be more than one program that are broadcast on the same channel at the same time.</p>
<p>When the recorder is recording a channel from time <var>S</var> to time <var>T</var> (including time <var>S</var> but not <var>T</var>), it cannot record other channels from time <var>S-0.5</var> to time <var>T</var> (including time <var>S-0.5</var> but not <var>T</var>).</p>
<p>Find the minimum number of recorders required to record the channels so that all the <var>N</var> programs are completely recorded.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤N≤10^5</var></li>
<li><var>1≤C≤30</var></li>
<li><var>1≤s_i<t_i≤10^5</var></li>
<li><var>1≤c_i≤C</var></li>
<li>If <var>c_i=c_j</var> and <var>i≠j</var>, either <var>t_i≤s_j</var> or <var>s_i≥t_j</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>C</var>
<var>s_1</var> <var>t_1</var> <var>c_1</var>
<var>:</var>
<var>s_N</var> <var>t_N</var> <var>c_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>When the minimum required number of recorders is <var>x</var>, print the value of <var>x</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2
1 7 2
7 8 1
8 12 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Two recorders can record all the programs, for example, as follows:</p>
<ul>
<li>With the first recorder, record Channel <var>2</var> from time <var>1</var> to time <var>7</var>. The first program will be recorded. Note that this recorder will be unable to record other channels from time <var>0.5</var> to time <var>7</var>.</li>
<li>With the second recorder, record Channel <var>1</var> from time <var>7</var> to time <var>12</var>. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time <var>6.5</var> to time <var>12</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 4
1 3 2
3 4 4
1 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>There may be a channel where there is no program to record.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre></section>
</div>
</span> | [
[
"3 2\n1 7 2\n7 8 1\n8 12 1\n",
"3 2\n1 7 2\n7 8 1\n8 12 1\n"
]
] |
p03505 | Code Festival Team Relay (Parallel) - Kaiden | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><em>ButCoder Inc.</em> runs a programming competition site called <em>ButCoder</em>. In this site, a user is given an integer value called rating that represents his/her skill, which changes each time he/she participates in a contest. The initial value of a new user's rating is <var>0</var>, and a user whose rating reaches <var>K</var> or higher is called <em>Kaiden</em> ("total transmission"). Note that a user's rating may become negative.</p>
<p>Hikuhashi is a new user in ButCoder. It is estimated that, his rating increases by <var>A</var> in each of his odd-numbered contests (first, third, fifth, ...), and decreases by <var>B</var> in each of his even-numbered contests (second, fourth, sixth, ...).</p>
<p>According to this estimate, after how many contests will he become Kaiden for the first time, or will he never become Kaiden?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ K, A, B ≤ 10^{18}</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>K</var> <var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If it is estimated that Hikuhashi will never become Kaiden, print <code>-1</code>. Otherwise, print the estimated number of contests before he become Kaiden for the first time.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4000 2000 500
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
</pre>
<p>Each time Hikuhashi participates in a contest, his rating is estimated to change as <var>0</var> → <var>2000</var> → <var>1500</var> → <var>3500</var> → <var>3000</var> → <var>5000</var> → <var>…</var> After his fifth contest, his rating will reach <var>4000</var> or higher for the first time.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4000 500 2000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
<p>Each time Hikuhashi participates in a contest, his rating is estimated to change as <var>0</var> → <var>500</var> → <var>-1500</var> → <var>-1000</var> → <var>-3000</var> → <var>-2500</var> → <var>…</var> He will never become Kaiden.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1000000000000000000 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1999999999999999997
</pre>
<p>The input and output values may not fit into <var>32</var>-bit integers.</p></section>
</div>
</span> | [
[
"4000 2000 500\n",
"4000 2000 500\n"
]
] |
p03506 | Code Festival Team Relay (Parallel) - Evergrowing Tree | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Consider an infinite <var>N</var>-ary tree as shown below:</p>
<div style="text-align: center;">
<img src="https://img.atcoder.jp/relay2/c76baa50b0acf28062688597724a54b9.png">
<p>Figure: an infinite <var>N</var>-ary tree for the case <var>N = 3</var></p>
</img></div>
<p>As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index <var>1</var>. For the remaining vertices, vertices in the upper row have smaller indices than those in the lower row. Among the vertices in the same row, a vertex that is more to the left has a smaller index.</p>
<p>Regarding this tree, process <var>Q</var> queries. The <var>i</var>-th query is as follows:</p>
<ul>
<li>Find the index of the lowest common ancestor (see Notes) of Vertex <var>v_i</var> and <var>w_i</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Notes</h3><ul>
<li>In a rooted tree, the <em>lowest common ancestor</em> (LCA) of Vertex <var>v</var> and <var>w</var> is the farthest vertex from the root that is an ancestor of both Vertex <var>v</var> and <var>w</var>. Here, a vertex is considered to be an ancestor of itself. For example, in the tree shown in Problem Statement, the LCA of Vertex <var>5</var> and <var>7</var> is Vertex <var>2</var>, the LCA of Vertex <var>8</var> and <var>11</var> is Vertex <var>1</var>, and the LCA of Vertex <var>3</var> and <var>9</var> is Vertex <var>3</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^9</var></li>
<li><var>1 ≤ Q ≤ 10^5</var></li>
<li><var>1 ≤ v_i < w_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>Q</var>
<var>v_1</var> <var>w_1</var>
<var>:</var>
<var>v_Q</var> <var>w_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line <var>(1 ≤ i ≤ Q)</var> must contain the index of the lowest common ancestor of Vertex <var>v_i</var> and <var>w_i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
5 7
8 11
3 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
1
3
</pre>
<p>The queries in this case correspond to the examples shown in Notes.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100000 2
1 2
3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
1
</pre></section>
</div>
</span> | [
[
"3 3\n5 7\n8 11\n3 9\n",
"3 3\n5 7\n8 11\n3 9\n"
]
] |
p03507 | Code Festival Team Relay (Parallel) - Garden | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In your garden, there is a long and narrow flowerbed that stretches infinitely to the east. You have decided to plant <var>N</var> kinds of flowers in this empty flowerbed. For convenience, we will call these <var>N</var> kinds of flowers Flower <var>1,</var> <var>2,</var> <var>…,</var> <var>N</var>. Also, we will call the position that is <var>p</var> centimeters from the west end of the flowerbed Position <var>p</var>.</p>
<p>You will plant Flower <var>i</var> <var>(1 ≤ i ≤ N)</var> as follows: first, plant one at Position <var>w_i</var>, then plant one every <var>d_i</var> centimeters endlessly toward the east. That is, Flower <var>i</var> will be planted at the positions <var>w_i,</var> <var>w_i + d_i,</var> <var>w_i + 2 d_i,</var> <var>…</var> Note that more than one flower may be planted at the same position.</p>
<p>Find the position at which the <var>K</var>-th flower from the west is planted. If more than one flower is planted at the same position, they are counted individually.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ K ≤ 10^9</var></li>
<li><var>1 ≤ w_i ≤ 10^{18}</var></li>
<li><var>1 ≤ d_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>K</var>
<var>w_1</var> <var>d_1</var>
<var>:</var>
<var>w_N</var> <var>d_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>When the <var>K</var>-th flower from the west is planted at Position <var>X</var>, print the value of <var>X</var>. (The westmost flower is counted as the <var>1</var>-st flower.)</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 6
20 10
25 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>50
</pre>
<p>Two kinds of flowers are planted at the following positions:</p>
<ul>
<li>Flower <var>1</var>: Position <var>20,</var> <var>30,</var> <var>40,</var> <var>50,</var> <var>60,</var> <var>…</var></li>
<li>Flower <var>2</var>: Position <var>25,</var> <var>40,</var> <var>55,</var> <var>70,</var> <var>85,</var> <var>…</var></li>
</ul>
<p>The sixth flower from the west is the Flower <var>1</var> planted at Position <var>50</var>. Note that the two flowers planted at Position <var>40</var> are counted individually.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 9
10 10
10 10
10 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>30
</pre>
<p>Three flowers are planted at each of the positions <var>10,</var> <var>20,</var> <var>30,</var> <var>…</var> Thus, the ninth flower from the west is planted at Position <var>30</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1000000000
1000000000000000000 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1999999999000000000
</pre></section>
</div>
</span> | [
[
"2 6\n20 10\n25 15\n",
"2 6\n20 10\n25 15\n"
]
] |
p03508 | Code Festival Team Relay (Parallel) - Shock | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an undirected graph <var>G</var>. <var>G</var> has <var>N</var> vertices and <var>M</var> edges. The vertices are numbered from <var>1</var> through <var>N</var>, and the <var>i</var>-th edge <var>(1 ≤ i ≤ M)</var> connects Vertex <var>a_i</var> and <var>b_i</var>. <var>G</var> does not have self-loops and multiple edges.</p>
<p>You can repeatedly perform the operation of adding an edge between two vertices. However, <var>G</var> must not have self-loops or multiple edges as the result. Also, if Vertex <var>1</var> and <var>2</var> are connected directly or indirectly by edges, your body will be exposed to a voltage of <var>1000000007</var> volts. This must also be avoided.</p>
<p>Under these conditions, at most how many edges can you add? Note that Vertex <var>1</var> and <var>2</var> are never connected directly or indirectly in the beginning.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 10^5</var></li>
<li><var>0 ≤ M ≤ 10^5</var></li>
<li><var>1 ≤ a_i < b_i ≤ N</var></li>
<li>All pairs <var>(a_i, b_i)</var> are distinct.</li>
<li>Vertex <var>1</var> and <var>2</var> in <var>G</var> are not connected directly or indirectly by edges.</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 maximum number of edges that can be added.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 1
1 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p><img alt="" src="https://img.atcoder.jp/relay2/ff912c5f290ee6a475d4c8a2ac29bfff.png"/></p>
<p>As shown above, two edges can be added. It is not possible to add three or more edges.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p><img alt="" src="https://img.atcoder.jp/relay2/b8da065e6d2dfe3bc9ea98095cf9f3de.png"/></p>
<p>No edge can be added.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 6
1 4
1 8
2 5
3 6
4 8
5 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>12
</pre></section>
</div>
</span> | [
[
"4 1\n1 3\n",
"4 1\n1 3\n"
]
] |
p03509 | Code Festival Team Relay (Parallel) - White and Blue | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo Kingdom Congress is voting on a bill.</p>
<p><var>N</var> members are present, and the <var>i</var>-th member <var>(1 ≤ i ≤ N)</var> has <var>w_i</var> white ballots and <var>b_i</var> blue ballots. Each member <var>i</var> will put all the <var>w_i</var> white ballots into the box if he/she is in favor of the bill, and put all the <var>b_i</var> blue ballots into the box if he/she is not in favor of the bill. No other action is allowed. For example, a member must not forfeit voting, or put only a part of his/her white ballots or a part of his/her blue ballots into the box.</p>
<p>After all the members vote, if at least <var>P</var> percent of the ballots in the box is white, the bill is passed; if less than <var>P</var> percent of the ballots is white, the bill is rejected.</p>
<p>In order for the bill to pass, at least how many members must be in favor of it?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ P ≤ 100</var></li>
<li><var>1 ≤ w_i ≤ 10^9</var></li>
<li><var>1 ≤ b_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>P</var>
<var>w_1</var> <var>b_1</var>
<var>w_2</var> <var>b_2</var>
<var>:</var>
<var>w_N</var> <var>b_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of members in favor of the bill required for passage.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 75
1 1
1 1
1 1
1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>This is a "normal" vote, where each of the four members has one white ballot and one blue ballot. For the bill to pass, at least <var>75</var> percent of the four, that is, at least three must be in favor of it.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 75
1 1
1 1
1 1
100 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>The "yes" vote of the member with <var>100</var> white ballots alone is enough to pass the bill.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5 60
6 3
5 9
3 4
7 8
4 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre></section>
</div>
</span> | [
[
"4 75\n1 1\n1 1\n1 1\n1 1\n",
"4 75\n1 1\n1 1\n1 1\n1 1\n"
]
] |
p03510 | Code Festival Team Relay (Parallel) - Capture | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In a long narrow forest stretching east-west, there are <var>N</var> beasts. Below, we will call the point that is <var>p</var> meters from the west end Point <var>p</var>. The <var>i</var>-th beast from the west <var>(1 ≤ i ≤ N)</var> is at Point <var>x_i</var>, and can be sold for <var>s_i</var> yen (the currency of Japan) if captured.</p>
<p>You will choose two integers <var>L</var> and <var>R</var> <var>(L ≤ R)</var>, and throw a net to cover the range from Point <var>L</var> to Point <var>R</var> including both ends, <var>[L, R]</var>. Then, all the beasts in the range will be captured. However, the net costs <var>R - L</var> yen and your profit will be <var>(</var>the sum of <var>s_i</var> over all captured beasts <var>i) - (R - L)</var> yen.</p>
<p>What is the maximum profit that can be earned by throwing a net only once?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 2 × 10^5</var></li>
<li><var>1 ≤ x_1 < x_2 < ... < x_N ≤ 10^{15}</var></li>
<li><var>1 ≤ s_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>s_1</var>
<var>x_2</var> <var>s_2</var>
<var>:</var>
<var>x_N</var> <var>s_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>When the maximum profit is <var>X</var> yen, print the value of <var>X</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
10 20
40 50
60 30
70 40
90 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>90
</pre>
<p>If you throw a net covering the range <var>[L = 40, R = 70]</var>, the second, third and fourth beasts from the west will be captured, generating the profit of <var>s_2 + s_3 + s_4 - (R - L) = 90</var> yen. It is not possible to earn <var>91</var> yen or more.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
10 2
40 5
60 3
70 4
90 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5
</pre>
<p>The positions of the beasts are the same as Sample 1, but the selling prices dropped significantly, so you should not aim for two or more beasts. By throwing a net covering the range <var>[L = 40, R = 40]</var>, you can earn <var>s_2 - (R - L) = 5</var> yen, and this is the maximum possible profit.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4
1 100
3 200
999999999999999 150
1000000000000000 150
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>299
</pre></section>
</div>
</span> | [
[
"5\n10 20\n40 50\n60 30\n70 40\n90 10\n",
"5\n10 20\n40 50\n60 30\n70 40\n90 10\n"
]
] |
p03511 | Code Festival Team Relay (Parallel) - Coinage | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two strings <var>s</var> and <var>t</var> consisting of lowercase English letters and an integer <var>L</var>.</p>
<p>We will consider generating a string of length <var>L</var> by concatenating one or more copies of <var>s</var> and <var>t</var>. Here, it is allowed to use the same string more than once.</p>
<p>For example, when <var>s =</var> <code>at</code><var>,</var> <var>t =</var> <code>code</code> and <var>L = 6,</var> the strings <code>atatat</code><var>,</var> <code>atcode</code> and <code>codeat</code> can be generated.</p>
<p>Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length <var>L</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ L ≤ 2 × 10^5</var></li>
<li><var>1 ≤ |s|, |t| ≤ L</var></li>
<li><var>s</var> and <var>t</var> consist of lowercase English letters.</li>
<li>It is possible to generate a string of length <var>L</var> in the way described in Problem Statement.</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>s_1</var>
<var>x_2</var> <var>s_2</var>
<var>:</var>
<var>x_N</var> <var>s_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
at
code
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>atatat
</pre>
<p>This input corresponds to the example shown in Problem Statement.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8
coding
festival
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>festival
</pre>
<p>It is possible that either <var>s</var> or <var>t</var> cannot be used at all in generating a string of length <var>L</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8
same
same
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>samesame
</pre>
<p>It is also possible that <var>s = t</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10
coin
age
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>ageagecoin
</pre></section>
</div>
</span> | [
[
"6\nat\ncode\n",
"6\nat\ncode\n"
]
] |
p03512 | Code Festival Team Relay (Parallel) - Akashic Records | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Consider an infinite sequence <var>a_1,</var> <var>a_2,</var> <var>…</var> Initially, the values of all the terms are <var>0</var>, and from this state we will sequentially perform <var>Q</var> operations. The <var>i</var>-th operation <var>(1 ≤ i ≤ Q)</var> is as follows:</p>
<ul>
<li>For every positive integer <var>j</var>, add <var>x_i</var> to the value of <var>a_{j × m_i}</var>.</li>
</ul>
<p>Find the value of the largest term after these <var>Q</var> operations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ Q ≤ 299</var></li>
<li><var>2 ≤ m_i ≤ 300</var></li>
<li><var>-10^6 ≤ x_i ≤ 10^6</var></li>
<li>All <var>m_i</var> 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>Q</var>
<var>m_1</var> <var>x_1</var>
<var>:</var>
<var>m_Q</var> <var>x_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the value of the largest term after the <var>Q</var> operations.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
2 10
3 -20
6 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p>The values of each terms in the sequence <var>a_1,</var> <var>a_2,</var> <var>…</var> change as follows:</p>
<ul>
<li>Before the operations: <var>0,</var> <var>0,</var> <var>0,</var> <var>0,</var> <var>0,</var> <var>0,</var> <var>…</var></li>
<li>After the <var>1</var>-st operation: <var>0,</var> <var>10,</var> <var>0,</var> <var>10,</var> <var>0,</var> <var>10,</var> <var>…</var></li>
<li>After the <var>2</var>-nd operation: <var>0,</var> <var>10,</var> <var>-20,</var> <var>10,</var> <var>0,</var> <var>-10,</var> <var>…</var></li>
<li>After the <var>3</var>-rd operation: <var>0,</var> <var>10,</var> <var>-20,</var> <var>10,</var> <var>0,</var> <var>5,</var> <var>…</var></li>
</ul>
<p>The value of the largest term after all the operations is <var>10</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
10 -3
50 4
100 -5
</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>5
56 114834
72 -149861
100 190757
192 -132693
240 133108
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>438699
</pre></section>
</div>
</span> | [
[
"3\n2 10\n3 -20\n6 15\n",
"3\n2 10\n3 -20\n6 15\n"
]
] |
p03513 | Code Festival Team Relay (Parallel) - Nice to Meet You | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> islands floating in Ringo Sea, and <var>M</var> travel agents operate ships between these islands. For convenience, we will call these islands Island <var>1,</var> <var>2,</var> <var>…,</var> <var>N,</var> and call these agents Agent <var>1,</var> <var>2,</var> <var>…,</var> <var>M</var>.</p>
<p>The sea currents in Ringo Sea change significantly each day. Depending on the state of the sea on the day, Agent <var>i</var> <var>(1 ≤ i ≤ M)</var> operates ships from Island <var>a_i</var> to <var>b_i</var>, or Island <var>b_i</var> to <var>a_i</var>, but not both at the same time. Assume that the direction of the ships of each agent is independently selected with equal probability.</p>
<p>Now, Takahashi is on Island <var>1</var>, and Hikuhashi is on Island <var>2</var>. Let <var>P</var> be the probability that Takahashi and Hikuhashi can travel to the same island in the day by ships operated by the <var>M</var> agents, ignoring the factors such as the travel time for ships. Then, <var>P × 2^M</var> is an integer. Find <var>P × 2^M</var> 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 ≤ a_i < b_i ≤ N</var></li>
<li>All pairs <var>(a_i, b_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>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 value <var>P × 2^M</var> modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3
1 3
2 3
3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<div style="text-align: center;">
<img alt="36cba65088d9b1224a6ce9665aa44048.png" src="https://img.atcoder.jp/relay2/36cba65088d9b1224a6ce9665aa44048.png">
</img></div>
<p>The <var>2^M = 8</var> scenarios shown above occur with equal probability, and Takahashi and Hikuhashi can meet on the same island in <var>6</var> of them. Thus, <var>P = 6/2^M</var> and <var>P × 2^M = 6</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 5
1 3
2 4
3 4
3 5
4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>18
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 6
1 2
2 3
3 4
4 5
5 6
1 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>64
</pre></section>
</div>
</span> | [
[
"4 3\n1 3\n2 3\n3 4\n",
"4 3\n1 3\n2 3\n3 4\n"
]
] |
p03514 | Code Festival Team Relay (Parallel) - Indifferent | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have <var>2N</var> pots. The market price of the <var>i</var>-th pot <var>(1 ≤ i ≤ 2N)</var> is <var>p_i</var> yen (the currency of Japan).</p>
<p>Now, you and Lunlun the dachshund will alternately take one pot. You will go first, and we will continue until all the pots are taken by you or Lunlun. Since Lunlun does not know the market prices of the pots, she will always choose a pot randomly from the remaining pots with equal probability. You know this behavior of Lunlun, and the market prices of the pots.</p>
<p>Let the sum of the market prices of the pots you take be <var>S</var> yen. Your objective is to maximize the expected value of <var>S</var>. Find the expected value of <var>S</var> when the optimal strategy is adopted.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ p_i ≤ 2 × 10^5</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>p_1</var>
<var>:</var>
<var>p_{2N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the expected value of <var>S</var> when the strategy to maximize the expected value of <var>S</var> is adopted. The output is considered correct if its absolute or relative error from the judge's output is at most <var>10^{-9}</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1
150000
108
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>150000.0
</pre>
<p>Naturally, you should choose the <var>150000</var> yen pot.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
50000
50000
100000
100000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>183333.3333333333
</pre>
<p>First, you will take one of the <var>100000</var> yen pots. The other <var>100000</var> yen pot will become yours if it is not taken in Lunlun's next turn, with probability <var>2/3</var>. If it is taken, you will have to settle for a <var>50000</var> yen pot. Thus, the expected value of <var>S</var> when the optimal strategy is adopted is <var>2/3 × (100000 + 100000) + 1/3 × (100000 + 50000) = 183333.3333…</var></p></section>
</div>
</span> | [
[
"1\n150000\n108\n",
"1\n150000\n108\n"
]
] |
p03515 | CODE FESTIVAL 2017 Elimination Tournament Round 3 (Parallel) - Black Cats Deployment | <span class="lang-en">
<p>Score : <var>800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke Festival 2017 will be held in a tree with <var>N</var> vertices numbered <var>1,2, ...,N</var>.
The <var>i</var>-th edge connects Vertex <var>a_i</var> and <var>b_i</var>, and has <em>joyfulness</em> <var>c_i</var>.</p>
<p>The staff is Snuke and <var>N-1</var> black cats.
Snuke will set up the headquarters in some vertex, and from there he will deploy a cat to each of the other <var>N-1</var> vertices.</p>
<p>For each vertex, calculate the <em>niceness</em> when the headquarters are set up in that vertex.
The niceness when the headquarters are set up in Vertex <var>i</var> is calculated as follows:</p>
<ul>
<li>Let <var>X=0</var>.</li>
<li>For each integer <var>j</var> between <var>1</var> and <var>N</var> (inclusive) except <var>i</var>, do the following:<ul>
<li>Add <var>c</var> to <var>X</var>, where <var>c</var> is the smallest joyfulness of an edge on the path from Vertex <var>i</var> to Vertex <var>j</var>.</li>
</ul>
</li>
<li>The niceness is the final value of <var>X</var>.</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,b_i \leq N</var></li>
<li><var>1 \leq c_i \leq 10^{9}</var></li>
<li>The given graph is a tree.</li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>200</var> points, <var>N \leq 1000</var>.</li>
<li>In the test set worth <var>200</var> points, <var>c_i \leq 2</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>c_1</var>
<var>:</var>
<var>a_{N-1}</var> <var>b_{N-1}</var> <var>c_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line must contain the niceness when the headquarters are set up in Vertex <var>i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 10
2 3 20
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>20
30
30
</pre>
<ul>
<li>The figure below shows the case when headquarters are set up in each of the vertices <var>1</var>, <var>2</var> and <var>3</var>.</li>
<li>The number on top of an edge denotes the joyfulness of the edge, and the number below an vertex denotes the smallest joyfulness of an edge on the path from the headquarters to that vertex.</li>
</ul>
<div style="text-align: center;">
<img alt="1ee10aa2a1bf5e43e05161f37d88bdc1.png" src="https://atcoder.jp/img/asaporo2/1ee10aa2a1bf5e43e05161f37d88bdc1.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>15
6 3 2
13 3 1
1 13 2
7 1 2
8 1 1
2 8 2
2 12 2
5 2 2
2 11 2
10 2 2
10 9 1
9 14 2
4 14 1
11 15 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>16
20
15
14
20
15
16
20
15
20
20
20
16
15
20
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>19
19 14 48
11 19 23
17 14 30
7 11 15
2 19 15
2 18 21
19 10 43
12 11 25
3 11 4
5 19 50
4 11 19
9 12 29
14 13 3
14 6 12
14 15 14
5 1 6
8 18 13
7 16 14
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>103
237
71
263
370
193
231
207
299
358
295
299
54
368
220
220
319
237
370
</pre></section>
</div>
</span> | [
[
"3\n1 2 10\n2 3 20\n",
"3\n1 2 10\n2 3 20\n"
]
] |
p03516 | CODE FESTIVAL 2017 Elimination Tournament Round 3 (Parallel) - Unicyclic Graph Counting | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has come up with the following problem.</p>
<blockquote>
<p>You are given a sequence <var>d</var> of length <var>N</var>.
Find the number of the undirected graphs with <var>N</var> vertices labeled <var>1,2,...,N</var> satisfying the following conditions, modulo <var>10^{9} + 7</var>:</p>
<ul>
<li>The graph is simple and connected.</li>
<li>The degree of Vertex <var>i</var> is <var>d_i</var>.</li>
</ul>
</blockquote>
<p><strong>When <var>2 \leq N, 1 \leq d_i \leq N-1, {\rm Σ} d_i = 2(N-1)</var>,</strong> it can be proved that the answer to the problem is <var>\frac{(N-2)!}{(d_{1} -1)!(d_{2} - 1)! ... (d_{N}-1)!}</var>.</p>
<p>Snuke is wondering what the answer is <strong>when <var>3 \leq N, 1 \leq d_i \leq N-1, { \rm Σ} d_i = 2N</var>.</strong>
Solve the problem under this condition for him.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 \leq N \leq 300</var></li>
<li><var>1 \leq d_i \leq N-1</var></li>
<li><var>{ \rm Σ} d_i = 2N</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>200</var> points, <var>N \leq 5</var>.</li>
<li>In the test set worth another <var>200</var> points, <var>N \leq 18</var>.</li>
<li>In the test set worth another <var>300</var> points, <var>N \leq 50</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>d_1</var> <var>d_2</var> <var>...</var> <var>d_{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
1 2 2 3 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<ul>
<li>There are six graphs as shown below:</li>
</ul>
<div style="text-align: center;">
<img alt="51367cdb21c64bfb07113b300921d52c.png" src="https://atcoder.jp/img/asaporo2/51367cdb21c64bfb07113b300921d52c.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>16
2 1 3 1 2 1 4 1 1 2 1 1 3 2 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>555275958
</pre>
<ul>
<li>Be sure to find the answer modulo <var>10^{9} + 7</var>.</li>
</ul></section>
</div>
</span> | [
[
"5\n1 2 2 3 2\n",
"5\n1 2 2 3 2\n"
]
] |
p03517 | CODE FESTIVAL 2017 Elimination Tournament Round 2 (Parallel) - Colorful MST | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo has an undirected graph <var>G</var> with <var>N</var> vertices numbered <var>1,2,...,N</var> and <var>M</var> edges numbered <var>1,2,...,M</var>.
Edge <var>i</var> connects Vertex <var>a_{i}</var> and <var>b_{i}</var> and has a length of <var>w_i</var>.</p>
<p>Now, he is in the middle of painting these <var>N</var> vertices in <var>K</var> colors numbered <var>1,2,...,K</var>. Vertex <var>i</var> is already painted in Color <var>c_i</var>, except when <var>c_i = 0</var>, in which case Vertex <var>i</var> is not yet painted.</p>
<p>After he paints each vertex that is not yet painted in one of the <var>K</var> colors, he will give <var>G</var> to Snuke.</p>
<p>Based on <var>G</var>, Snuke will make another undirected graph <var>G'</var> with <var>K</var> vertices numbered <var>1,2,...,K</var> and <var>M</var> edges.
Initially, there is no edge in <var>G'</var>. The <var>i</var>-th edge will be added as follows:</p>
<ul>
<li>Let <var>x</var> and <var>y</var> be the colors of the two vertices connected by Edge <var>i</var> in <var>G</var>.</li>
<li>Add an edge of length <var>w_i</var> connecting Vertex <var>x</var> and <var>y</var> in <var>G'</var>.</li>
</ul>
<p>What is the minimum possible sum of the lengths of the edges in the minimum spanning tree of <var>G'</var>? If <var>G'</var> will not be connected regardless of how Ringo paints the vertices, print <var>-1</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,M \leq 10^{5}</var></li>
<li><var>1 \leq K \leq N</var></li>
<li><var>0 \leq c_i \leq K</var></li>
<li><var>1 \leq a_i,b_i \leq N</var></li>
<li><var>1 \leq w_i \leq 10^{9}</var></li>
<li>The given graph may NOT be simple or connected.</li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>100</var> points, <var>N = K</var> and <var>c_i = i</var>.</li>
<li>In the test set worth another <var>100</var> points, <var>c_i \neq 0</var>.</li>
<li>In the test set worth another <var>200</var> points, <var>c_i = 0</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>c_1</var> <var>c_2</var> <var>...</var> <var>c_{N}</var>
<var>a_1</var> <var>b_1</var> <var>w_1</var>
<var>:</var>
<var>a_M</var> <var>b_M</var> <var>w_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 3 3
1 0 1 2
1 2 10
2 3 20
2 4 50
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>60
</pre>
<p><var>G'</var> will only be connected when Vertex <var>2</var> is painted in Color <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 2 4
0 0 0 0 0
1 2 10
2 3 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
<p>Regardless of how Ringo paints the vertices, two edges is not enough to connect four vertices as one.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 12 9
1 2 3 4 5 6 7 8 9
6 9 9
8 9 6
6 7 85
9 5 545631016
2 1 321545
1 6 33562944
7 3 84946329
9 7 15926167
4 7 53386480
5 8 70476
4 6 4549
4 8 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>118901402
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>18 37 12
5 0 4 10 8 7 2 10 6 0 9 12 12 11 11 11 0 1
17 1 1
11 16 7575
11 15 9
10 10 289938980
5 10 17376
18 4 1866625
8 11 959154208
18 13 200
16 13 2
2 7 982223
12 12 9331
13 12 8861390
14 13 743
2 10 162440
2 4 981849
7 9 1
14 17 2800
2 7 7225452
3 7 85
5 17 4
2 13 1
10 3 45
1 15 973
14 7 56553306
16 17 70476
7 18 9
9 13 27911
18 14 7788322
11 11 8925
9 13 654295
2 10 9
10 1 545631016
3 4 5
17 12 1929
2 11 57
1 5 4
1 17 7807368
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>171
</pre></section>
</div>
</span> | [
[
"4 3 3\n1 0 1 2\n1 2 10\n2 3 20\n2 4 50\n",
"4 3 3\n1 0 1 2\n1 2 10\n2 3 20\n2 4 50\n"
]
] |
p03518 | CODE FESTIVAL 2017 Elimination Tournament Round 2 (Parallel) - Many Swaps Sorting | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has a sequence <var>p</var>, which is a permutation of <var>(0,1,2, ...,N-1)</var>.
The <var>i</var>-th element (<var>0</var>-indexed) in <var>p</var> is <var>p_i</var>.</p>
<p>He can perform <var>N-1</var> kinds of operations labeled <var>1,2,...,N-1</var> any number of times in any order.
When the operation labeled <var>k</var> is executed, the procedure represented by the following code will be performed:</p>
<pre>for(int i=k;i<N;i++)
swap(p[i],p[i-k]);
</pre>
<p>He would like to sort <var>p</var> in increasing order using between <var>0</var> and <var>10^{5}</var> operations (inclusive).
Show one such sequence of operations.
It can be proved that there always exists such a sequence of operations under the constraints in this problem.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 200</var></li>
<li><var>0 \leq p_i \leq N-1</var></li>
<li><var>p</var> is a permutation of <var>(0,1,2,...,N-1)</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>300</var> points, <var>N \leq 7</var>.</li>
<li>In the test set worth another <var>400</var> points, <var>N \leq 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>p_0</var> <var>p_1</var> <var>...</var> <var>p_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Let <var>m</var> be the number of operations in your solution. In the first line, print <var>m</var>.
In the <var>i</var>-th of the following <var>m</var> lines, print the label of the <var>i</var>-th executed operation.
The solution will be accepted if <var>m</var> is at most <var>10^5</var> and <var>p</var> is in increasing order after the execution of the <var>m</var> operations.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
4 2 0 1 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
2
3
1
2
</pre>
<ul>
<li>Each operation changes <var>p</var> as shown below:</li>
</ul>
<div style="text-align: center;">
<img alt="9f3b51eb1fe742848f407bdeb7b772e1.png" src="https://atcoder.jp/img/asaporo2/9f3b51eb1fe742848f407bdeb7b772e1.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9
1 0 4 3 5 6 2 8 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>11
3
6
1
3
5
2
4
7
8
6
3
</pre></section>
</div>
</span> | [
[
"5\n4 2 0 1 3\n",
"5\n4 2 0 1 3\n"
]
] |
p03519 | CODE FESTIVAL 2017 Elimination Tournament Round 1 (Parallel) - Paired Parentheses | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two sequences <var>a</var> and <var>b</var>, both of length <var>2N</var>. The <var>i</var>-th elements in <var>a</var> and <var>b</var> are <var>a_i</var> and <var>b_i</var>, respectively.
Using these sequences, Snuke is doing the job of calculating the <em>beauty</em> of pairs of <strong>balanced sequences of parentheses</strong> (defined below) of length <var>2N</var>.
The beauty of a pair <var>(s,t)</var> is calculated as follows:</p>
<ul>
<li>Let <var>X=0</var>.</li>
<li>For each <var>i</var> between <var>1</var> and <var>2N</var> (inclusive), increment <var>X</var> by <var>a_i</var> if <var>s_i = t_i</var>, and increment <var>X</var> by <var>b_i</var> otherwise.</li>
<li>The beauty of <var>(s,t)</var> is the final value of <var>X</var>.</li>
</ul>
<p>You will be given <var>Q</var> queries. Process them in order.
In the <var>i</var>-th query, update the value of <var>a_{p_i}</var> to <var>x_i</var>, and the value of <var>b_{p_i}</var> to <var>y_i</var>. Then, find the maximum possible beauty of a pair of balanced sequences of parentheses.</p>
<p>In this problem, only the sequences below are defined to be balanced sequences of parentheses.</p>
<ul>
<li>An empty string</li>
<li>The concatenation of <code>(</code>, <var>s</var>, <code>)</code> in this order, where <var>s</var> is a balanced sequence of parentheses</li>
<li>The concatenation of <var>s</var>, <var>t</var> in this order, where <var>s</var> and <var>t</var> are balanced sequences of parentheses</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,Q \leq 10^{5}</var></li>
<li><var>-10^{9} \leq a_i,b_i,x_i,y_i \leq 10^{9}</var></li>
<li><var>1 \leq p_i \leq 2N</var></li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>200</var> points, <var>N \leq 5</var> and <var>Q \leq 10</var>.</li>
<li>In the test set worth <var>300</var> points, <var>Q = 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>Q</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{2N}</var>
<var>b_1</var> <var>b_2</var> <var>...</var> <var>b_{2N}</var>
<var>p_1</var> <var>x_1</var> <var>y_1</var>
<var>:</var>
<var>p_Q</var> <var>x_Q</var> <var>y_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 the response to the <var>i</var>-th query.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
1 1 7 3
4 2 3 3
2 4 6
3 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>15
15
</pre>
<ul>
<li>The first query updates <var>a</var> and <var>b</var> to <var>a=(1,4,7,3),b=(4,6,3,3)</var>. The maximum possible beauty is <var>15</var> for <var>(s,t) =(</var><code>()()</code>,<code>()()</code><var>)</var>.</li>
<li>The second query updates <var>a</var> and <var>b</var> to <var>a=(1,4,2,3),b=(4,6,5,3)</var>. The maximum possible beauty is <var>15</var> for <var>(s,t) =(</var><code>()()</code>,<code>(())</code><var>)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>7 7
34 -20 -27 42 44 29 9 11 20 44 27 19 -31 -29
46 -50 -11 20 28 46 12 13 33 -22 -48 -27 35 -17
7 27 34
12 -2 22
4 -50 -12
3 -32 15
8 -7 23
3 -30 11
4 -2 23
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>311
312
260
286
296
292
327
</pre></section>
</div>
</span> | [
[
"2 2\n1 1 7 3\n4 2 3 3\n2 4 6\n3 2 5\n",
"2 2\n1 1 7 3\n4 2 3 3\n2 4 6\n3 2 5\n"
]
] |
p03520 | CODE FESTIVAL 2017 Elimination Tournament Round 1 (Parallel) - Ancient Tree Record | <span class="lang-en">
<p>Score : <var>800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke found a record of a tree with <var>N</var> vertices in ancient ruins.
The findings are as follows:</p>
<ul>
<li>The vertices of the tree were numbered <var>1,2,...,N</var>, and the edges were numbered <var>1,2,...,N-1</var>.</li>
<li>Edge <var>i</var> connected Vertex <var>a_i</var> and <var>b_i</var>.</li>
<li>The length of each edge was an integer between <var>1</var> and <var>10^{18}</var> (inclusive).</li>
<li>The sum of the shortest distances from Vertex <var>i</var> to Vertex <var>1,...,N</var> was <var>s_i</var>.</li>
</ul>
<p>From the information above, restore the length of each edge.
<strong>The input guarantees that it is possible to determine the lengths of the edges consistently with the record.</strong>
Furthermore, it can be proved that the length of each edge is uniquely determined in such a case.</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><var>1 \leq s_i \leq 10^{18}</var></li>
<li>The given graph is a tree.</li>
<li>All input values are integers.</li>
<li>It is possible to consistently restore the lengths of the edges.</li>
<li>In the restored graph, the length of each edge is an integer between <var>1</var> and <var>10^{18}</var> (inclusive).</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>300</var> points, <var>a_i = i, b_i = i+1</var>.</li>
<li>In the test set worth <var>200</var> points, <var>N \geq 3, a_i = 1, b_i = i+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>b_1</var>
<var>:</var>
<var>a_{N-1}</var> <var>b_{N-1}</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 <var>N-1</var> lines. The <var>i</var>-th line must contain the length of Edge <var>i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1 2
2 3
3 4
8 6 6 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
2
1
</pre>
<ul>
<li>The given record corresponds to the tree shown below:</li>
</ul>
<div style="text-align: center;">
<img alt="010664dd33d69063a99075c0f7a391f8.png" src="https://atcoder.jp/img/asaporo2/010664dd33d69063a99075c0f7a391f8.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
1 2
1 3
1 4
1 5
10 13 16 19 22
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
2
3
4
</pre>
<ul>
<li>The given record corresponds to the tree shown below:</li>
</ul>
<div style="text-align: center;">
<img alt="41891e0c5dc01850fd29636b200f7f49.png" src="https://atcoder.jp/img/asaporo2/41891e0c5dc01850fd29636b200f7f49.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>15
9 10
9 15
15 4
4 13
13 2
13 11
2 14
13 6
11 1
1 12
12 3
12 7
2 5
14 8
1154 890 2240 883 2047 2076 1590 1104 1726 1791 1091 1226 841 1000 901
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>5
75
2
6
7
50
10
95
9
8
78
28
89
8
</pre></section>
</div>
</span> | [
[
"4\n1 2\n2 3\n3 4\n8 6 6 8\n",
"4\n1 2\n2 3\n3 4\n8 6 6 8\n"
]
] |
p03521 | CODE FESTIVAL 2017 Exhibition (Parallel) - Awkward | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><em>ButCoder Inc.</em> is a startup company whose main business is the development and operation of the programming competition site "<em>ButCoder</em>".</p>
<p>There are <var>N</var> members of the company including the president, and each member except the president has exactly one direct boss. Each member has a unique ID number from <var>1</var> to <var>N</var>, and the member with the ID <var>i</var> is called Member <var>i</var>. The president is Member <var>1</var>, and the direct boss of Member <var>i</var> <var>(2 ≤ i ≤ N)</var> is Member <var>b_i</var> <var>(1 ≤ b_i < i)</var>.</p>
<p>All the members in ButCoder have gathered in the great hall in the main office to take a group photo. The hall is very large, and all <var>N</var> people can stand in one line. However, they have been unable to decide the order in which they stand. For some reason, all of them except the president want to avoid standing next to their direct bosses.</p>
<p>How many ways are there for them to stand in a line that satisfy their desires? Find the count modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 2000</var></li>
<li><var>1 ≤ b_i < i</var> <var>(2 ≤ 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>b_2</var>
<var>b_3</var>
<var>:</var>
<var>b_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of ways for the <var>N</var> members to stand in a line, such that no member except the president is next to his/her direct boss, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1
2
3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Below, we will write <var>A → B</var> to denote the fact that Member <var>A</var> is the direct boss of Member <var>B</var>.</p>
<p>In this case, the hierarchy of the members is <var>1 → 2 → 3 → 4</var>. There are two ways for them to stand in a line that satisfy their desires:</p>
<ul>
<li><var>2, 4, 1, 3</var></li>
<li><var>3, 1, 4, 2</var></li>
</ul>
<p>Note that the latter is the reverse of the former, but we count them individually.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>The hierarchy of the members is <var>1 → 2 → 3</var>. No matter what order they stand in, at least one of the desires of Member <var>2</var> and <var>3</var> is not satisfied, so the answer is <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
1
1
3
3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre>
<p>The hierarchy of the members is shown below:</p>
<p><img alt="" src="https://img.atcoder.jp/cf17-exhibition/88bc845e074e0a3fecd96e2db9f3b1a5.png"/></p>
<p>There are eight ways for them to stand in a line that satisfy their desires:</p>
<ul>
<li><var>1, 4, 5, 2, 3</var></li>
<li><var>1, 5, 4, 2, 3</var></li>
<li><var>3, 2, 4, 1, 5</var></li>
<li><var>3, 2, 4, 5, 1</var></li>
<li><var>3, 2, 5, 1, 4</var></li>
<li><var>3, 2, 5, 4, 1</var></li>
<li><var>4, 1, 5, 2, 3</var></li>
<li><var>5, 1, 4, 2, 3</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>15
1
2
3
1
4
2
7
1
8
2
8
1
8
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>97193524
</pre></section>
</div>
</span> | [
[
"4\n1\n2\n3\n",
"4\n1\n2\n3\n"
]
] |
p03522 | CODE FESTIVAL 2017 Exhibition (Parallel) - Increment and Swap | <span class="lang-en">
<p>Score : <var>1500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a sequence <var>A</var> of length <var>N</var>.</p>
<p>On this sequence, we can perform the following two kinds of operations:</p>
<ul>
<li>
<p>Swap two adjacent elements.</p>
</li>
<li>
<p>Select one element, and increment it by <var>1</var>.</p>
</li>
</ul>
<p>We will repeatedly perform these operations so that <var>A</var> will be a non-decreasing sequence. Find the minimum required number of operations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 200000</var></li>
<li><var>1 ≤ A_i ≤ 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 minimum number of operations required to turn <var>A</var> into a non-decreasing sequence.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
4
1
8
8
7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>We can turn <var>A</var> into a non-decreasing sequence in two operations:</p>
<ul>
<li>Initially, <var>A = \{4, 1, 8, 8, 7\}</var>.</li>
<li>Swap the first two elements. Now, <var>A = \{1, 4, 8, 8, 7\}</var>.</li>
<li>Increment the last element by <var>1</var>. Now, <var>A = \{1, 4, 8, 8, 8\}</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>20
8
2
9
7
4
6
7
9
7
4
7
4
4
3
6
2
3
4
4
9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>62
</pre></section>
</div>
</span> | [
[
"5\n4\n1\n8\n8\n7\n",
"5\n4\n1\n8\n8\n7\n"
]
] |
p03523 | CODE FESTIVAL 2017 Final - AKIBA | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var>.</p>
<p>Takahashi can insert the character <code>A</code> at any position in this string any number of times.</p>
<p>Can he change <var>S</var> into <code>AKIHABARA</code>?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 50</var></li>
<li><var>S</var> consists of uppercase 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 it is possible to change <var>S</var> into <code>AKIHABARA</code>, print <code>YES</code>; otherwise, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>KIHBR
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>Insert one <code>A</code> at each of the four positions: the beginning, immediately after <code>H</code>, immediately after <code>B</code> and the end.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>AKIBAHARA
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>NO
</pre>
<p>The correct spell is <code>AKIHABARA</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>AAKIAHBAARA
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>NO
</pre></section>
</div>
</span> | [
[
"KIHBR\n",
"KIHBR\n"
]
] |
p03524 | CODE FESTIVAL 2017 Final - Palindrome-phobia | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has a string <var>S</var> consisting of three kinds of letters: <code>a</code>, <code>b</code> and <code>c</code>.</p>
<p>He has a phobia for palindromes, and wants to permute the characters in <var>S</var> so that <var>S</var> will not contain a palindrome of length <var>2</var> or more as a substring. Determine whether this is possible.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 10^5</var></li>
<li><var>S</var> consists of <code>a</code>, <code>b</code> and <code>c</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>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the objective is achievable, print <code>YES</code>; if it is unachievable, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>abac
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>As it stands now, <var>S</var> contains a palindrome <code>aba</code>, but we can permute the characters to get <code>acba</code>, for example, that does not contain a palindrome of length <var>2</var> or more.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>aba
</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>babacccabab
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
</pre></section>
</div>
</span> | [
[
"abac\n",
"abac\n"
]
] |
p03525 | CODE FESTIVAL 2017 Final - Time Gap | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In CODE FESTIVAL XXXX, there are <var>N+1</var> participants from all over the world, including Takahashi.</p>
<p>Takahashi checked and found that the <em>time gap</em> (defined below) between the local times in his city and the <var>i</var>-th person's city was <var>D_i</var> hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is <var>d</var> o'clock at the moment when the local time in city A is <var>0</var> o'clock, then the time gap between these two cities is defined to be <var>min(d,24-d)</var> hours.
Here, we are using <var>24</var>-hour notation.
That is, the local time in the <var>i</var>-th person's city is either <var>d</var> o'clock or <var>24-d</var> o'clock at the moment when the local time in Takahashi's city is <var>0</var> o'clock, for example.</p>
<p>Then, for each pair of two people chosen from the <var>N+1</var> people, he wrote out the time gap between their cities. Let the smallest time gap among them be <var>s</var> hours.</p>
<p>Find the maximum possible value of <var>s</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 50</var></li>
<li><var>0 \leq D_i \leq 12</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>D_1</var> <var>D_2</var> <var>...</var> <var>D_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible value of <var>s</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
7 12 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>For example, consider the situation where it is <var>7</var>, <var>12</var> and <var>16</var> o'clock in each person's city at the moment when it is <var>0</var> o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is <var>4</var> hours.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
11 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1
0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<p>Note that Takahashi himself is also a participant.</p></section>
</div>
</span> | [
[
"3\n7 12 8\n",
"3\n7 12 8\n"
]
] |
p03526 | CODE FESTIVAL 2017 Final - Zabuton | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In the final of CODE FESTIVAL in some year, there are <var>N</var> participants.
The <em>height</em> and <em>power</em> of Participant <var>i</var> is <var>H_i</var> and <var>P_i</var>, respectively.</p>
<p>Ringo is hosting a game of stacking zabuton (cushions).</p>
<p>The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton.
Initially, the stack is empty.
When it is Participant <var>i</var>'s turn, if there are <var>H_i</var> or less zabuton already stacked, he/she will add exactly <var>P_i</var> zabuton to the stack. Otherwise, he/she will give up and do nothing.</p>
<p>Ringo wants to maximize the number of participants who can add zabuton to the stack.
How many participants can add zabuton to the stack in the optimal order of participants?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 5000</var></li>
<li><var>0 \leq H_i \leq 10^9</var></li>
<li><var>1 \leq P_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>H_1</var> <var>P_1</var>
<var>H_2</var> <var>P_2</var>
<var>:</var>
<var>H_N</var> <var>P_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum number of participants who can add zabuton to the stack.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
0 2
1 3
3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>When the participants line up in the same order as the input, Participants <var>1</var> and <var>3</var> will be able to add zabuton.</p>
<p>On the other hand, there is no order such that all three participants can add zabuton. Thus, the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
2 4
3 1
4 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>When the participants line up in the order <var>2</var>, <var>3</var>, <var>1</var>, all of them will be able to add zabuton.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>5
</pre></section>
</div>
</span> | [
[
"3\n0 2\n1 3\n3 4\n",
"3\n0 2\n1 3\n3 4\n"
]
] |
p03527 | CODE FESTIVAL 2017 Final - Combination Lock | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo has a string <var>S</var>.</p>
<p>He can perform the following <var>N</var> kinds of operations any number of times in any order.</p>
<ul>
<li>Operation <var>i</var>: For each of the characters from the <var>L_i</var>-th through the <var>R_i</var>-th characters in <var>S</var>, replace it with its succeeding letter in the English alphabet. (That is, replace <code>a</code> with <code>b</code>, replace <code>b</code> with <code>c</code> and so on.) For <code>z</code>, we assume that its succeeding letter is <code>a</code>.</li>
</ul>
<p>Ringo loves palindromes and wants to turn <var>S</var> into a palindrome.
Determine whether this is possible.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 10^5</var></li>
<li><var>S</var> consists of lowercase English letters.</li>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>1 \leq L_i \leq R_i \leq |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>
<var>N</var>
<var>L_1</var> <var>R_1</var>
<var>L_2</var> <var>R_2</var>
<var>:</var>
<var>L_N</var> <var>R_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>YES</code> if it is possible to turn <var>S</var> into a palindrome; print <code>NO</code> if it is impossible.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>bixzja
2
2 3
3 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>For example, if we perform Operation <var>1</var>, <var>2</var> and <var>1</var> in this order, <var>S</var> changes as <code>bixzja</code> → <code>bjyzja</code> → <code>bjzakb</code> → <code>bkaakb</code> and becomes a palindrome.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>abc
1
2 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>cassert
4
1 2
3 4
1 1
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
</pre></section>
</div>
</span> | [
[
"bixzja\n2\n2 3\n3 6\n",
"bixzja\n2\n2 3\n3 6\n"
]
] |
p03528 | CODE FESTIVAL 2017 Final - Distribute Numbers | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Select any integer <var>N</var> between <var>1000</var> and <var>2000</var> (inclusive), and any integer <var>K</var> not less than <var>1</var>, then solve the problem below.</p>
<h4>Problem</h4>
<p>We have <var>N</var> sheets of paper.
Write <var>K</var> integers on each of them to satisfy the following conditions:</p>
<ul>
<li>Each integer written must be between <var>1</var> and <var>N</var> (inclusive).</li>
<li>The <var>K</var> integers written on the same sheet must be all different.</li>
<li>Each of the integers between <var>1</var> and <var>N</var> must be written on exactly <var>K</var> sheets.</li>
<li>For any two sheet, there is exactly one integer that appears on both.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>There is no input in this problem.</p>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>In the first line, print <var>N</var> and <var>K</var> separated by a space.</p>
<p>In the subsequent <var>N</var> lines, print your solution.
The <var>i</var>-th of these lines must contain the <var>K</var> integers written on the <var>i</var>-th sheet, with spaces in between.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Output</h3><pre>3 2
1 2
2 3
3 1
</pre>
<p>This is an example of a solution for <var>N = 3</var> and <var>K = 2</var>.</p>
<p>Note that this output will be judged as incorrect, since the constraint on <var>N</var> is not satisfied.</p></section>
</div>
</span> | [
[
"",
""
]
] |
p03529 | CODE FESTIVAL 2017 Final - Mancala | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Consider the following game:</p>
<ul>
<li>The game is played using a row of <var>N</var> squares and many stones.</li>
<li>First, <var>a_i</var> stones are put in Square <var>i\ (1 \leq i \leq N)</var>.</li>
<li>A player can perform the following operation as many time as desired: "Select an integer <var>i</var> such that Square <var>i</var> contains exactly <var>i</var> stones. Remove all the stones from Square <var>i</var>, and add one stone to each of the <var>i-1</var> squares from Square <var>1</var> to Square <var>i-1</var>."</li>
<li>The final score of the player is the total number of the stones remaining in the squares.</li>
</ul>
<p>For a sequence <var>a</var> of length <var>N</var>, let <var>f(a)</var> be the minimum score that can be obtained when the game is played on <var>a</var>.</p>
<p>Find the sum of <var>f(a)</var> over all sequences <var>a</var> of length <var>N</var> where each element is between <var>0</var> and <var>K</var> (inclusive).
Since it can be extremely large, find the answer modulo <var>1000000007 (= 10^9+7)</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq K \leq 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>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of <var>f(a)</var> modulo <var>1000000007 (= 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>10
</pre>
<p>There are nine sequences of length <var>2</var> where each element is between <var>0</var> and <var>2</var>. For each of them, the value of <var>f(a)</var> and how to achieve it is as follows:</p>
<ul>
<li><var>f(\{0,0\})</var>: <var>0</var> (Nothing can be done)</li>
<li><var>f(\{0,1\})</var>: <var>1</var> (Nothing can be done)</li>
<li><var>f(\{0,2\})</var>: <var>0</var> (Select Square <var>2</var>, then Square <var>1</var>)</li>
<li><var>f(\{1,0\})</var>: <var>0</var> (Select Square <var>1</var>)</li>
<li><var>f(\{1,1\})</var>: <var>1</var> (Select Square <var>1</var>)</li>
<li><var>f(\{1,2\})</var>: <var>0</var> (Select Square <var>1</var>, Square <var>2</var>, then Square <var>1</var>)</li>
<li><var>f(\{2,0\})</var>: <var>2</var> (Nothing can be done)</li>
<li><var>f(\{2,1\})</var>: <var>3</var> (Nothing can be done)</li>
<li><var>f(\{2,2\})</var>: <var>3</var> (Select Square <var>2</var>)</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>20 17
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>983853488
</pre></section>
</div>
</span> | [
[
"2 2\n",
"2 2\n"
]
] |
p03530 | CODE FESTIVAL 2017 Final - Poor Penguin | <span class="lang-en">
<p>Score : <var>1600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In some place in the Arctic Ocean, there are <var>H</var> rows and <var>W</var> columns of ice pieces floating on the sea.
We regard this area as a grid, and denote the square at the <var>i</var>-th row and <var>j</var>-th column as Square <var>(i,j)</var>.
The ice piece floating in each square is either thin ice or an iceberg, and a penguin lives in one of the squares that contain thin ice.
There are no ice pieces floating outside the grid.</p>
<p>The ice piece at Square <var>(i,j)</var> is represented by the character <var>S_{i,j}</var>.
<var>S_{i,j}</var> is <code>+</code>, <code>#</code> or <code>P</code>, each of which means the following:</p>
<ul>
<li><code>+</code>: Occupied by thin ice.</li>
<li><code>#</code>: Occupied by an iceberg.</li>
<li><code>P</code>: Occupied by thin ice. The penguin lives here.</li>
</ul>
<p>When summer comes, unstable thin ice that is not held between some pieces of ice collapses one after another.
Formally, thin ice at Square <var>(i,j)</var> will collapse when it does NOT satisfy either of the following conditions:</p>
<ul>
<li>Both Square <var>(i-1,j)</var> and Square <var>(i+1,j)</var> are occupied by an iceberg or uncollapsed thin ice.</li>
<li>Both Square <var>(i,j-1)</var> and Square <var>(i,j+1)</var> are occupied by an iceberg or uncollapsed thin ice.</li>
</ul>
<p>When a collapse happens, it may cause another. Note that icebergs do not collapse.</p>
<p>Now, a mischievous tourist comes here. He will do a little work so that, when summer comes, the thin ice inhabited by the penguin will collapse.
He can smash an iceberg with a hammer to turn it to thin ice.
At least how many icebergs does he need to smash?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq H,W \leq 40</var></li>
<li><var>S_{i,j}</var> is <code>+</code>, <code>#</code> or <code>P</code>.</li>
<li><var>S</var> contains exactly one <code>P</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>H</var> <var>W</var>
<var>S_{1,1}</var><var>S_{1,2}</var><var>...</var><var>S_{1,W}</var>
<var>S_{2,1}</var><var>S_{2,2}</var><var>...</var><var>S_{2,W}</var>
<var>:</var>
<var>S_{H,1}</var><var>S_{H,2}</var><var>...</var><var>S_{H,W}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of icebergs that needs to be changed to thin ice in order to cause the collapse of the thin ice inhabited by the penguin when summer comes.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
+#+
#P#
+#+
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>For example, when the right and bottom icebergs are changed to thin ice, collapses happen as follows:</p>
<pre>+#+ .#. .#. .#.
#P+ -> #P+ -> #P. -> #..
+++ .+. ... ...
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 6
#+++++
+++#++
#+++++
+++P+#
+##+++
++++#+
</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>40 40
#++#+++++#+#+#+##+++++++##+#+++#++##++##
+##++++++++++#+###+##++++#+++++++++#++##
+++#+++++#++#++####+++#+#+###+++##+++#++
+++#+######++##+#+##+#+++#+++++++++#++#+
+++##+#+#++#+++#++++##+++++++++#++#+#+#+
#++#+++#+#++++##+#+#+++##+#+##+#++++##++
++#+##+++#++####+#++##++#+++#+#+#++++#++
+#+###++++++##++++++#++##+#####++#++##++
##+##+#+++#+#+##++#+###+######++++#+###+
+++#+++##+#####+#+#++++#+#+++++#+##++##+
#+++#+##+++++++#++#++++++++++###+#++#+#+
##+++##++#+++++#++++#++#+##++#+#+#++##+#
##+++#+###+++++##++#+#+++####+#+++++#+++
+++#++#++#+++++++++#++###++++++++###+##+
++#+++#++++++#####++##++#+++#+++++#++++#
++#++#+##++++#####+###+++####+#+#+######
++++++##+++++##+++++#++###++#++##+++++++
+#++++##++++++#++++#+#++++#++++##+++##+#
+++++++#+#++##+##+#+++++++###+###++##+++
++++++#++###+#+#+++##+#++++++#++#+#++#+#
##+##++++++#+++++#++#+#++##+++#+#+++##+#
#+++#+#+##+#+##++#P#++#++++++##++#+#++##
#+++#++##+##+#++++#++#++##++++++#+#+#+++
++++####+#++#####+++#+###+#++###++++#++#
#+#++####++##++#+#+#+##+#+#+##++++##++#+
+###+###+#+##+++#++++++#+#++++###+#+++++
+++#+++++#+++#+++++##++++++++###++#+#+++
+#+#++#+#++++++###+#++##+#+##+##+#+#####
#++++++++#+#+###+######++#++#+++++++++++
##+++##+#+#++#++#++#++++++#++##+#+#++###
+#+#+#+++++++#+++++++######+##++#++##+##
++#+++#+###+#++###+++#+++#+#++++#+###+++
#+#+###++#+#####+++++#+####++#++#+###+++
+#+##+#++#++##+++++++######++#++++++++++
+####+#+#+++++##+#+#++#+#++#+++##++++#+#
#++##++#+#+++++##+#++++####+++++###+#+#+
##+#++#++#+##+#+#++##++###+###+#+++++##+
##++###+###+#+#++#++#########+++###+#+##
+++#+++#++++++++++#+#+++#++#++###+####+#
++##+###+++++++##+++++#++#++++++++++++++
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>151
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>1 1
P
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>0
</pre></section>
</div>
</span> | [
[
"3 3\n+#+\n#P#\n+#+\n",
"3 3\n+#+\n#P#\n+#+\n"
]
] |
p03531 | CODE FESTIVAL 2017 Final - Full Tournament | <span class="lang-en">
<p>Score : <var>1600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><var>2^N</var> players competed in a tournament.
Each player has a unique ID number from <var>1</var> through <var>2^N</var>. When two players play a match, the player with the smaller ID always wins.</p>
<p>This tournament was a little special, in which losers are not eliminated to fully rank all the players.</p>
<p>We will call such a tournament that involves <var>2^n</var> players a <em>full tournament of level <var>n</var></em>.
In a full tournament of level <var>n</var>, the players are ranked as follows:</p>
<ul>
<li>In a full tournament of level <var>0</var>, the only player is ranked first.</li>
<li>In a full tournament of level <var>n\ (1 \leq n)</var>, initially all <var>2^n</var> players are lined up in a row. Then:</li>
<li>First, starting from the two leftmost players, the players are successively divided into <var>2^{n-1}</var> pairs of two players.</li>
<li>Each of the pairs plays a match. The winner enters the "Won" group, and the loser enters the "Lost" group.</li>
<li>The players in the "Won" group are lined up in a row, maintaining the relative order in the previous row. Then, a full tournament of level <var>n-1</var> is held to fully rank these players.</li>
<li>The players in the "Lost" group are also ranked in the same manner, then the rank of each of these players increases by <var>2^{n-1}</var>.</li>
</ul>
<p>For example, the figure below shows how a full tournament of level <var>3</var> progresses when eight players are lined up in the order <var>3,4,8,6,2,1,7,5</var>. The list of the players sorted by the final ranking will be <var>1,3,5,6,2,4,7,8</var>.</p>
<p><img alt="" src="https://img.atcoder.jp/cf17-final/e93269f0dfb68bcdff175a3b634ab0d8.png"/></p>
<p>Takahashi has a sheet of paper with the list of the players sorted by the final ranking in the tournament, but some of it blurred and became unreadable.
You are given the information on the sheet as a sequence <var>A</var> of length <var>N</var>. When <var>A_i</var> is <var>1</var> or greater, it means that the <var>i</var>-th ranked player had the ID <var>A_i</var>. If <var>A_i</var> is <var>0</var>, it means that the ID of the <var>i</var>-th ranked player is lost.</p>
<p>Determine whether there exists a valid order in the first phase of the tournament which is consistent with the sheet. If it exists, provide one such order.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 18</var></li>
<li><var>0 \leq A_i \leq 2^N</var></li>
<li>No integer, except <var>0</var>, occurs more than once in <var>A_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>A_1</var> <var>A_2</var> <var>...</var> <var>A_{2^N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there exists a valid order in the first phase of the tournament, print <code>YES</code> first, then in the subsequent line, print the IDs of the players sorted by the final ranking, with spaces in between.
If there is no such order, print <code>NO</code> instead.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
0 3 0 6 0 0 0 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
3 4 8 6 2 1 7 5
</pre>
<p>This is the same order as the one in the statement.</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>NO
</pre></section>
</div>
</span> | [
[
"3\n0 3 0 6 0 0 0 8\n",
"3\n0 3 0 6 0 0 0 8\n"
]
] |
p03532 | CODE FESTIVAL 2017 Final - Tree MST | <span class="lang-en">
<p>Score : <var>1900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo has a tree with <var>N</var> vertices.
The <var>i</var>-th of the <var>N-1</var> edges in this tree connects Vertex <var>A_i</var> and Vertex <var>B_i</var> and has a weight of <var>C_i</var>.
Additionally, Vertex <var>i</var> has a weight of <var>X_i</var>.</p>
<p>Here, we define <var>f(u,v)</var> as the distance between Vertex <var>u</var> and Vertex <var>v</var>, plus <var>X_u + X_v</var>.</p>
<p>We will consider a complete graph <var>G</var> with <var>N</var> vertices.
The cost of its edge that connects Vertex <var>u</var> and Vertex <var>v</var> is <var>f(u,v)</var>.
Find the minimum spanning tree of <var>G</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 200,000</var></li>
<li><var>1 \leq X_i \leq 10^9</var></li>
<li><var>1 \leq A_i,B_i \leq N</var></li>
<li><var>1 \leq C_i \leq 10^9</var></li>
<li>The given graph is a tree.</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>X_2</var> <var>...</var> <var>X_N</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_{N-1}</var> <var>B_{N-1}</var> <var>C_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the cost of the minimum spanning tree of <var>G</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1 3 5 1
1 2 1
2 3 2
3 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>22
</pre>
<p>We connect the following pairs: Vertex <var>1</var> and <var>2</var>, Vertex <var>1</var> and <var>4</var>, Vertex <var>3</var> and <var>4</var>. The costs are <var>5</var>, <var>8</var> and <var>9</var>, respectively, for a total of <var>22</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>359
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
1000000000 1000000000
2 1 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3000000000
</pre></section>
</div>
</span> | [
[
"4\n1 3 5 1\n1 2 1\n2 3 2\n3 4 3\n",
"4\n1 3 5 1\n1 2 1\n2 3 2\n3 4 3\n"
]
] |
p03533 | CODE FESTIVAL 2017 Final (Parallel) - AKIBA | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var>.</p>
<p>Takahashi can insert the character <code>A</code> at any position in this string any number of times.</p>
<p>Can he change <var>S</var> into <code>AKIHABARA</code>?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 50</var></li>
<li><var>S</var> consists of uppercase 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 it is possible to change <var>S</var> into <code>AKIHABARA</code>, print <code>YES</code>; otherwise, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>KIHBR
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>Insert one <code>A</code> at each of the four positions: the beginning, immediately after <code>H</code>, immediately after <code>B</code> and the end.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>AKIBAHARA
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>NO
</pre>
<p>The correct spell is <code>AKIHABARA</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>AAKIAHBAARA
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>NO
</pre></section>
</div>
</span> | [
[
"KIHBR\n",
"KIHBR\n"
]
] |
p03534 | CODE FESTIVAL 2017 Final (Parallel) - Palindrome-phobia | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has a string <var>S</var> consisting of three kinds of letters: <code>a</code>, <code>b</code> and <code>c</code>.</p>
<p>He has a phobia for palindromes, and wants to permute the characters in <var>S</var> so that <var>S</var> will not contain a palindrome of length <var>2</var> or more as a substring. Determine whether this is possible.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 10^5</var></li>
<li><var>S</var> consists of <code>a</code>, <code>b</code> and <code>c</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>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the objective is achievable, print <code>YES</code>; if it is unachievable, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>abac
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>As it stands now, <var>S</var> contains a palindrome <code>aba</code>, but we can permute the characters to get <code>acba</code>, for example, that does not contain a palindrome of length <var>2</var> or more.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>aba
</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>babacccabab
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
</pre></section>
</div>
</span> | [
[
"abac\n",
"abac\n"
]
] |
p03535 | CODE FESTIVAL 2017 Final (Parallel) - Time Gap | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In CODE FESTIVAL XXXX, there are <var>N+1</var> participants from all over the world, including Takahashi.</p>
<p>Takahashi checked and found that the <em>time gap</em> (defined below) between the local times in his city and the <var>i</var>-th person's city was <var>D_i</var> hours.
The time gap between two cities is defined as follows. For two cities A and B, if the local time in city B is <var>d</var> o'clock at the moment when the local time in city A is <var>0</var> o'clock, then the time gap between these two cities is defined to be <var>min(d,24-d)</var> hours.
Here, we are using <var>24</var>-hour notation.
That is, the local time in the <var>i</var>-th person's city is either <var>d</var> o'clock or <var>24-d</var> o'clock at the moment when the local time in Takahashi's city is <var>0</var> o'clock, for example.</p>
<p>Then, for each pair of two people chosen from the <var>N+1</var> people, he wrote out the time gap between their cities. Let the smallest time gap among them be <var>s</var> hours.</p>
<p>Find the maximum possible value of <var>s</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 50</var></li>
<li><var>0 \leq D_i \leq 12</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>D_1</var> <var>D_2</var> <var>...</var> <var>D_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible value of <var>s</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
7 12 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>For example, consider the situation where it is <var>7</var>, <var>12</var> and <var>16</var> o'clock in each person's city at the moment when it is <var>0</var> o'clock in Takahashi's city. In this case, the time gap between the second and third persons' cities is <var>4</var> hours.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
11 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1
0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<p>Note that Takahashi himself is also a participant.</p></section>
</div>
</span> | [
[
"3\n7 12 8\n",
"3\n7 12 8\n"
]
] |
p03536 | CODE FESTIVAL 2017 Final (Parallel) - Zabuton | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In the final of CODE FESTIVAL in some year, there are <var>N</var> participants.
The <em>height</em> and <em>power</em> of Participant <var>i</var> is <var>H_i</var> and <var>P_i</var>, respectively.</p>
<p>Ringo is hosting a game of stacking zabuton (cushions).</p>
<p>The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton.
Initially, the stack is empty.
When it is Participant <var>i</var>'s turn, if there are <var>H_i</var> or less zabuton already stacked, he/she will add exactly <var>P_i</var> zabuton to the stack. Otherwise, he/she will give up and do nothing.</p>
<p>Ringo wants to maximize the number of participants who can add zabuton to the stack.
How many participants can add zabuton to the stack in the optimal order of participants?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 5000</var></li>
<li><var>0 \leq H_i \leq 10^9</var></li>
<li><var>1 \leq P_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>H_1</var> <var>P_1</var>
<var>H_2</var> <var>P_2</var>
<var>:</var>
<var>H_N</var> <var>P_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum number of participants who can add zabuton to the stack.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
0 2
1 3
3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>When the participants line up in the same order as the input, Participants <var>1</var> and <var>3</var> will be able to add zabuton.</p>
<p>On the other hand, there is no order such that all three participants can add zabuton. Thus, the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
2 4
3 1
4 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>When the participants line up in the order <var>2</var>, <var>3</var>, <var>1</var>, all of them will be able to add zabuton.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>5
</pre></section>
</div>
</span> | [
[
"3\n0 2\n1 3\n3 4\n",
"3\n0 2\n1 3\n3 4\n"
]
] |
p03537 | CODE FESTIVAL 2017 Final (Parallel) - Combination Lock | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo has a string <var>S</var>.</p>
<p>He can perform the following <var>N</var> kinds of operations any number of times in any order.</p>
<ul>
<li>Operation <var>i</var>: For each of the characters from the <var>L_i</var>-th through the <var>R_i</var>-th characters in <var>S</var>, replace it with its succeeding letter in the English alphabet. (That is, replace <code>a</code> with <code>b</code>, replace <code>b</code> with <code>c</code> and so on.) For <code>z</code>, we assume that its succeeding letter is <code>a</code>.</li>
</ul>
<p>Ringo loves palindromes and wants to turn <var>S</var> into a palindrome.
Determine whether this is possible.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |S| \leq 10^5</var></li>
<li><var>S</var> consists of lowercase English letters.</li>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>1 \leq L_i \leq R_i \leq |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>
<var>N</var>
<var>L_1</var> <var>R_1</var>
<var>L_2</var> <var>R_2</var>
<var>:</var>
<var>L_N</var> <var>R_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>YES</code> if it is possible to turn <var>S</var> into a palindrome; print <code>NO</code> if it is impossible.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>bixzja
2
2 3
3 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>For example, if we perform Operation <var>1</var>, <var>2</var> and <var>1</var> in this order, <var>S</var> changes as <code>bixzja</code> → <code>bjyzja</code> → <code>bjzakb</code> → <code>bkaakb</code> and becomes a palindrome.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>abc
1
2 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>cassert
4
1 2
3 4
1 1
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
</pre></section>
</div>
</span> | [
[
"bixzja\n2\n2 3\n3 6\n",
"bixzja\n2\n2 3\n3 6\n"
]
] |
p03538 | CODE FESTIVAL 2017 Final (Parallel) - Distribute Numbers | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Select any integer <var>N</var> between <var>1000</var> and <var>2000</var> (inclusive), and any integer <var>K</var> not less than <var>1</var>, then solve the problem below.</p>
<h4>Problem</h4>
<p>We have <var>N</var> sheets of paper.
Write <var>K</var> integers on each of them to satisfy the following conditions:</p>
<ul>
<li>Each integer written must be between <var>1</var> and <var>N</var> (inclusive).</li>
<li>The <var>K</var> integers written on the same sheet must be all different.</li>
<li>Each of the integers between <var>1</var> and <var>N</var> must be written on exactly <var>K</var> sheets.</li>
<li>For any two sheet, there is exactly one integer that appears on both.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>There is no input in this problem.</p>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>In the first line, print <var>N</var> and <var>K</var> separated by a space.</p>
<p>In the subsequent <var>N</var> lines, print your solution.
The <var>i</var>-th of these lines must contain the <var>K</var> integers written on the <var>i</var>-th sheet, with spaces in between.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Output</h3><pre>3 2
1 2
2 3
3 1
</pre>
<p>This is an example of a solution for <var>N = 3</var> and <var>K = 2</var>.</p>
<p>Note that this output will be judged as incorrect, since the constraint on <var>N</var> is not satisfied.</p></section>
</div>
</span> | [
[
"",
""
]
] |
p03539 | CODE FESTIVAL 2017 Final (Parallel) - Mancala | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Consider the following game:</p>
<ul>
<li>The game is played using a row of <var>N</var> squares and many stones.</li>
<li>First, <var>a_i</var> stones are put in Square <var>i\ (1 \leq i \leq N)</var>.</li>
<li>A player can perform the following operation as many time as desired: "Select an integer <var>i</var> such that Square <var>i</var> contains exactly <var>i</var> stones. Remove all the stones from Square <var>i</var>, and add one stone to each of the <var>i-1</var> squares from Square <var>1</var> to Square <var>i-1</var>."</li>
<li>The final score of the player is the total number of the stones remaining in the squares.</li>
</ul>
<p>For a sequence <var>a</var> of length <var>N</var>, let <var>f(a)</var> be the minimum score that can be obtained when the game is played on <var>a</var>.</p>
<p>Find the sum of <var>f(a)</var> over all sequences <var>a</var> of length <var>N</var> where each element is between <var>0</var> and <var>K</var> (inclusive).
Since it can be extremely large, find the answer modulo <var>1000000007 (= 10^9+7)</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq K \leq 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>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of <var>f(a)</var> modulo <var>1000000007 (= 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>10
</pre>
<p>There are nine sequences of length <var>2</var> where each element is between <var>0</var> and <var>2</var>. For each of them, the value of <var>f(a)</var> and how to achieve it is as follows:</p>
<ul>
<li><var>f(\{0,0\})</var>: <var>0</var> (Nothing can be done)</li>
<li><var>f(\{0,1\})</var>: <var>1</var> (Nothing can be done)</li>
<li><var>f(\{0,2\})</var>: <var>0</var> (Select Square <var>2</var>, then Square <var>1</var>)</li>
<li><var>f(\{1,0\})</var>: <var>0</var> (Select Square <var>1</var>)</li>
<li><var>f(\{1,1\})</var>: <var>1</var> (Select Square <var>1</var>)</li>
<li><var>f(\{1,2\})</var>: <var>0</var> (Select Square <var>1</var>, Square <var>2</var>, then Square <var>1</var>)</li>
<li><var>f(\{2,0\})</var>: <var>2</var> (Nothing can be done)</li>
<li><var>f(\{2,1\})</var>: <var>3</var> (Nothing can be done)</li>
<li><var>f(\{2,2\})</var>: <var>3</var> (Select Square <var>2</var>)</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>20 17
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>983853488
</pre></section>
</div>
</span> | [
[
"2 2\n",
"2 2\n"
]
] |
p03540 | CODE FESTIVAL 2017 Final (Parallel) - Poor Penguin | <span class="lang-en">
<p>Score : <var>1600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In some place in the Arctic Ocean, there are <var>H</var> rows and <var>W</var> columns of ice pieces floating on the sea.
We regard this area as a grid, and denote the square at the <var>i</var>-th row and <var>j</var>-th column as Square <var>(i,j)</var>.
The ice piece floating in each square is either thin ice or an iceberg, and a penguin lives in one of the squares that contain thin ice.
There are no ice pieces floating outside the grid.</p>
<p>The ice piece at Square <var>(i,j)</var> is represented by the character <var>S_{i,j}</var>.
<var>S_{i,j}</var> is <code>+</code>, <code>#</code> or <code>P</code>, each of which means the following:</p>
<ul>
<li><code>+</code>: Occupied by thin ice.</li>
<li><code>#</code>: Occupied by an iceberg.</li>
<li><code>P</code>: Occupied by thin ice. The penguin lives here.</li>
</ul>
<p>When summer comes, unstable thin ice that is not held between some pieces of ice collapses one after another.
Formally, thin ice at Square <var>(i,j)</var> will collapse when it does NOT satisfy either of the following conditions:</p>
<ul>
<li>Both Square <var>(i-1,j)</var> and Square <var>(i+1,j)</var> are occupied by an iceberg or uncollapsed thin ice.</li>
<li>Both Square <var>(i,j-1)</var> and Square <var>(i,j+1)</var> are occupied by an iceberg or uncollapsed thin ice.</li>
</ul>
<p>When a collapse happens, it may cause another. Note that icebergs do not collapse.</p>
<p>Now, a mischievous tourist comes here. He will do a little work so that, when summer comes, the thin ice inhabited by the penguin will collapse.
He can smash an iceberg with a hammer to turn it to thin ice.
At least how many icebergs does he need to smash?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq H,W \leq 40</var></li>
<li><var>S_{i,j}</var> is <code>+</code>, <code>#</code> or <code>P</code>.</li>
<li><var>S</var> contains exactly one <code>P</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>H</var> <var>W</var>
<var>S_{1,1}</var><var>S_{1,2}</var><var>...</var><var>S_{1,W}</var>
<var>S_{2,1}</var><var>S_{2,2}</var><var>...</var><var>S_{2,W}</var>
<var>:</var>
<var>S_{H,1}</var><var>S_{H,2}</var><var>...</var><var>S_{H,W}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of icebergs that needs to be changed to thin ice in order to cause the collapse of the thin ice inhabited by the penguin when summer comes.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
+#+
#P#
+#+
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>For example, when the right and bottom icebergs are changed to thin ice, collapses happen as follows:</p>
<pre>+#+ .#. .#. .#.
#P+ -> #P+ -> #P. -> #..
+++ .+. ... ...
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 6
#+++++
+++#++
#+++++
+++P+#
+##+++
++++#+
</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>40 40
#++#+++++#+#+#+##+++++++##+#+++#++##++##
+##++++++++++#+###+##++++#+++++++++#++##
+++#+++++#++#++####+++#+#+###+++##+++#++
+++#+######++##+#+##+#+++#+++++++++#++#+
+++##+#+#++#+++#++++##+++++++++#++#+#+#+
#++#+++#+#++++##+#+#+++##+#+##+#++++##++
++#+##+++#++####+#++##++#+++#+#+#++++#++
+#+###++++++##++++++#++##+#####++#++##++
##+##+#+++#+#+##++#+###+######++++#+###+
+++#+++##+#####+#+#++++#+#+++++#+##++##+
#+++#+##+++++++#++#++++++++++###+#++#+#+
##+++##++#+++++#++++#++#+##++#+#+#++##+#
##+++#+###+++++##++#+#+++####+#+++++#+++
+++#++#++#+++++++++#++###++++++++###+##+
++#+++#++++++#####++##++#+++#+++++#++++#
++#++#+##++++#####+###+++####+#+#+######
++++++##+++++##+++++#++###++#++##+++++++
+#++++##++++++#++++#+#++++#++++##+++##+#
+++++++#+#++##+##+#+++++++###+###++##+++
++++++#++###+#+#+++##+#++++++#++#+#++#+#
##+##++++++#+++++#++#+#++##+++#+#+++##+#
#+++#+#+##+#+##++#P#++#++++++##++#+#++##
#+++#++##+##+#++++#++#++##++++++#+#+#+++
++++####+#++#####+++#+###+#++###++++#++#
#+#++####++##++#+#+#+##+#+#+##++++##++#+
+###+###+#+##+++#++++++#+#++++###+#+++++
+++#+++++#+++#+++++##++++++++###++#+#+++
+#+#++#+#++++++###+#++##+#+##+##+#+#####
#++++++++#+#+###+######++#++#+++++++++++
##+++##+#+#++#++#++#++++++#++##+#+#++###
+#+#+#+++++++#+++++++######+##++#++##+##
++#+++#+###+#++###+++#+++#+#++++#+###+++
#+#+###++#+#####+++++#+####++#++#+###+++
+#+##+#++#++##+++++++######++#++++++++++
+####+#+#+++++##+#+#++#+#++#+++##++++#+#
#++##++#+#+++++##+#++++####+++++###+#+#+
##+#++#++#+##+#+#++##++###+###+#+++++##+
##++###+###+#+#++#++#########+++###+#+##
+++#+++#++++++++++#+#+++#++#++###+####+#
++##+###+++++++##+++++#++#++++++++++++++
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>151
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>1 1
P
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>0
</pre></section>
</div>
</span> | [
[
"3 3\n+#+\n#P#\n+#+\n",
"3 3\n+#+\n#P#\n+#+\n"
]
] |
p03541 | CODE FESTIVAL 2017 Final (Parallel) - Full Tournament | <span class="lang-en">
<p>Score : <var>1600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><var>2^N</var> players competed in a tournament.
Each player has a unique ID number from <var>1</var> through <var>2^N</var>. When two players play a match, the player with the smaller ID always wins.</p>
<p>This tournament was a little special, in which losers are not eliminated to fully rank all the players.</p>
<p>We will call such a tournament that involves <var>2^n</var> players a <em>full tournament of level <var>n</var></em>.
In a full tournament of level <var>n</var>, the players are ranked as follows:</p>
<ul>
<li>In a full tournament of level <var>0</var>, the only player is ranked first.</li>
<li>In a full tournament of level <var>n\ (1 \leq n)</var>, initially all <var>2^n</var> players are lined up in a row. Then:</li>
<li>First, starting from the two leftmost players, the players are successively divided into <var>2^{n-1}</var> pairs of two players.</li>
<li>Each of the pairs plays a match. The winner enters the "Won" group, and the loser enters the "Lost" group.</li>
<li>The players in the "Won" group are lined up in a row, maintaining the relative order in the previous row. Then, a full tournament of level <var>n-1</var> is held to fully rank these players.</li>
<li>The players in the "Lost" group are also ranked in the same manner, then the rank of each of these players increases by <var>2^{n-1}</var>.</li>
</ul>
<p>For example, the figure below shows how a full tournament of level <var>3</var> progresses when eight players are lined up in the order <var>3,4,8,6,2,1,7,5</var>. The list of the players sorted by the final ranking will be <var>1,3,5,6,2,4,7,8</var>.</p>
<p><img alt="" src="https://img.atcoder.jp/cf17-final/e93269f0dfb68bcdff175a3b634ab0d8.png"/></p>
<p>Takahashi has a sheet of paper with the list of the players sorted by the final ranking in the tournament, but some of it blurred and became unreadable.
You are given the information on the sheet as a sequence <var>A</var> of length <var>N</var>. When <var>A_i</var> is <var>1</var> or greater, it means that the <var>i</var>-th ranked player had the ID <var>A_i</var>. If <var>A_i</var> is <var>0</var>, it means that the ID of the <var>i</var>-th ranked player is lost.</p>
<p>Determine whether there exists a valid order in the first phase of the tournament which is consistent with the sheet. If it exists, provide one such order.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 18</var></li>
<li><var>0 \leq A_i \leq 2^N</var></li>
<li>No integer, except <var>0</var>, occurs more than once in <var>A_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>A_1</var> <var>A_2</var> <var>...</var> <var>A_{2^N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there exists a valid order in the first phase of the tournament, print <code>YES</code> first, then in the subsequent line, print the IDs of the players sorted by the final ranking, with spaces in between.
If there is no such order, print <code>NO</code> instead.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
0 3 0 6 0 0 0 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
3 4 8 6 2 1 7 5
</pre>
<p>This is the same order as the one in the statement.</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>NO
</pre></section>
</div>
</span> | [
[
"3\n0 3 0 6 0 0 0 8\n",
"3\n0 3 0 6 0 0 0 8\n"
]
] |
p03542 | CODE FESTIVAL 2017 Final (Parallel) - Tree MST | <span class="lang-en">
<p>Score : <var>1900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo has a tree with <var>N</var> vertices.
The <var>i</var>-th of the <var>N-1</var> edges in this tree connects Vertex <var>A_i</var> and Vertex <var>B_i</var> and has a weight of <var>C_i</var>.
Additionally, Vertex <var>i</var> has a weight of <var>X_i</var>.</p>
<p>Here, we define <var>f(u,v)</var> as the distance between Vertex <var>u</var> and Vertex <var>v</var>, plus <var>X_u + X_v</var>.</p>
<p>We will consider a complete graph <var>G</var> with <var>N</var> vertices.
The cost of its edge that connects Vertex <var>u</var> and Vertex <var>v</var> is <var>f(u,v)</var>.
Find the minimum spanning tree of <var>G</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 200,000</var></li>
<li><var>1 \leq X_i \leq 10^9</var></li>
<li><var>1 \leq A_i,B_i \leq N</var></li>
<li><var>1 \leq C_i \leq 10^9</var></li>
<li>The given graph is a tree.</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>X_2</var> <var>...</var> <var>X_N</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_{N-1}</var> <var>B_{N-1}</var> <var>C_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the cost of the minimum spanning tree of <var>G</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1 3 5 1
1 2 1
2 3 2
3 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>22
</pre>
<p>We connect the following pairs: Vertex <var>1</var> and <var>2</var>, Vertex <var>1</var> and <var>4</var>, Vertex <var>3</var> and <var>4</var>. The costs are <var>5</var>, <var>8</var> and <var>9</var>, respectively, for a total of <var>22</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>359
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
1000000000 1000000000
2 1 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3000000000
</pre></section>
</div>
</span> | [
[
"4\n1 3 5 1\n1 2 1\n2 3 2\n3 4 3\n",
"4\n1 3 5 1\n1 2 1\n2 3 2\n3 4 3\n"
]
] |
p03543 | AtCoder Beginner Contest 079 - Good Integer | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We call a <var>4</var>-digit integer with three or more consecutive same digits, such as <var>1118</var>, <strong>good</strong>.</p>
<p>You are given a <var>4</var>-digit integer <var>N</var>. Answer the question: Is <var>N</var> <strong>good</strong>?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1000 ≤ N ≤ 9999</var></li>
<li><var>N</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>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>N</var> is <strong>good</strong>, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1118
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p><var>N</var> is <strong>good</strong>, since it contains three consecutive <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>7777
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yes
</pre>
<p>An integer is also <strong>good</strong> when all the digits are the same.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1234
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"1118\n",
"1118\n"
]
] |
p03544 | AtCoder Beginner Contest 079 - Lucas Number | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>It is November <var>18</var> now in Japan. By the way, <var>11</var> and <var>18</var> are adjacent Lucas numbers.</p>
<p>You are given an integer <var>N</var>. Find the <var>N</var>-th Lucas number.</p>
<p>Here, the <var>i</var>-th Lucas number <var>L_i</var> is defined as follows:</p>
<ul>
<li><var>L_0=2</var></li>
<li><var>L_1=1</var></li>
<li><var>L_i=L_{i-1}+L_{i-2} (i≥2)</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤N≤86</var></li>
<li>It is guaranteed that the answer is less than <var>10^{18}</var>.</li>
<li><var>N</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>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the <var>N</var>-th Lucas number.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>11
</pre>
<ul>
<li><var>L_0=2</var></li>
<li><var>L_1=1</var></li>
<li><var>L_2=L_0+L_1=3</var></li>
<li><var>L_3=L_1+L_2=4</var></li>
<li><var>L_4=L_2+L_3=7</var></li>
<li><var>L_5=L_3+L_4=11</var></li>
</ul>
<p>Thus, the <var>5</var>-th Lucas number is <var>11</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>86
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>939587134549734843
</pre></section>
</div>
</span> | [
[
"5\n",
"5\n"
]
] |
p03545 | AtCoder Beginner Contest 079 - Train Ticket | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Sitting in a station waiting room, Joisino is gazing at her train ticket.</p>
<p>The ticket is numbered with four digits <var>A</var>, <var>B</var>, <var>C</var> and <var>D</var> in this order, each between <var>0</var> and <var>9</var> (inclusive).</p>
<p>In the formula <var>A</var> <var>op1</var> <var>B</var> <var>op2</var> <var>C</var> <var>op3</var> <var>D</var> <var>=</var> <var>7</var>, replace each of the symbols <var>op1</var>, <var>op2</var> and <var>op3</var> with <code>+</code> or <code>-</code> so that the formula holds.</p>
<p>The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0≤A,B,C,D≤9</var></li>
<li>All input values are integers.</li>
<li>It is guaranteed that there is a solution.</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>ABCD</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the formula you made, including the part <code>=7</code>.</p>
<p>Use the signs <code>+</code> and <code>-</code>.</p>
<p>Do not print a space between a digit and a sign.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1222
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1+2+2+2=7
</pre>
<p>This is the only valid solution.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>0290
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0-2+9+0=7
</pre>
<p><var>0 - 2 + 9 - 0 = 7</var> is also a valid solution.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3242
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3+2+4-2=7
</pre></section>
</div>
</span> | [
[
"1222\n",
"1222\n"
]
] |
p03546 | AtCoder Beginner Contest 079 - Wall | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Joisino the magical girl has decided to turn every single digit that exists on this world into <var>1</var>.</p>
<p>Rewriting a digit <var>i</var> with <var>j</var> <var>(0≤i,j≤9)</var> costs <var>c_{i,j}</var> MP (Magic Points).</p>
<p>She is now standing before a wall. The wall is divided into <var>HW</var> squares in <var>H</var> rows and <var>W</var> columns, and at least one square contains a digit between <var>0</var> and <var>9</var> (inclusive).</p>
<p>You are given <var>A_{i,j}</var> that describes the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left, as follows:</p>
<ul>
<li>If <var>A_{i,j}≠-1</var>, the square contains a digit <var>A_{i,j}</var>.</li>
<li>If <var>A_{i,j}=-1</var>, the square does not contain a digit.</li>
</ul>
<p>Find the minimum total amount of MP required to turn every digit on this wall into <var>1</var> in the end.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤H,W≤200</var></li>
<li><var>1≤c_{i,j}≤10^3</var> <var>(i≠j)</var></li>
<li><var>c_{i,j}=0</var> <var>(i=j)</var></li>
<li><var>-1≤A_{i,j}≤9</var></li>
<li>All input values are integers.</li>
<li>There is at least one digit on the wall.</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>c_{0,0}</var> <var>...</var> <var>c_{0,9}</var>
<var>:</var>
<var>c_{9,0}</var> <var>...</var> <var>c_{9,9}</var>
<var>A_{1,1}</var> <var>...</var> <var>A_{1,W}</var>
<var>:</var>
<var>A_{H,1}</var> <var>...</var> <var>A_{H,W}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum total amount of MP required to turn every digit on the wall into <var>1</var> in the end.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>12
</pre>
<p>To turn a single <var>8</var> into <var>1</var>, it is optimal to first turn <var>8</var> into <var>4</var>, then turn <var>4</var> into <var>9</var>, and finally turn <var>9</var> into <var>1</var>, costing <var>6</var> MP.</p>
<p>The wall contains two <var>8</var>s, so the minimum total MP required is <var>6×2=12</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Note that she may not need to change any digit.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3 5
0 4 3 6 2 7 2 5 3 3
4 0 5 3 7 5 3 7 2 7
5 7 0 7 2 9 3 2 9 1
3 6 2 0 2 4 6 4 2 3
3 5 7 4 0 6 9 7 6 7
9 8 5 2 2 0 4 7 6 5
5 4 6 3 2 3 0 5 4 3
3 6 2 3 4 2 4 0 8 9
4 6 5 4 3 5 3 2 0 8
2 1 3 4 5 7 8 6 4 0
3 5 2 6 1
2 5 3 2 1
6 9 2 5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>47
</pre></section>
</div>
</span> | [
[
"2 4\n0 9 9 9 9 9 9 9 9 9\n9 0 9 9 9 9 9 9 9 9\n9 9 0 9 9 9 9 9 9 9\n9 9 9 0 9 9 9 9 9 9\n9 9 9 9 0 9 9 9 9 2\n9 9 9 9 9 0 9 9 9 9\n9 9 9 9 9 9 0 9 9 9\n9 9 9 9 9 9 9 0 9 9\n9 9 9 9 2 9 9 9 0 9\n9 2 9 9 9 9 9 9 9 0\n-1 -1 -1 -1\n8 1 1 8\n",
"2 4\n0 9 9 9 9 9 9 9 9 9\n9 0 9 9 9 9 9 9 9 9\n9 9 0 9 9 9 9 9 9 9\n9 9 9 0 9 9 9 9 9 9\n9 9 9 9 0 9 9 9 9 2\n9 9 9 9 9 0 9 9 9 9\n9 9 9 9 9 9 0 9 9 9\n9 9 9 9 9 9 9 0 9 9\n9 9 9 9 2 9 9 9 0 9\n9 2 9 9 9 9 9 9 9 0\n-1 -1 -1 -1\n8 1 1 8\n"
]
] |
p03547 | AtCoder Beginner Contest 078 - HEX | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In programming, hexadecimal notation is often used.</p>
<p>In hexadecimal notation, besides the ten digits <var>0, 1, ..., 9</var>, the six letters <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>E</code> and <code>F</code> are used to represent the values <var>10, 11, 12, 13, 14</var> and <var>15</var>, respectively.</p>
<p>In this problem, you are given two letters <var>X</var> and <var>Y</var>. Each <var>X</var> and <var>Y</var> is <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>E</code> or <code>F</code>.</p>
<p>When <var>X</var> and <var>Y</var> are seen as hexadecimal numbers, which is larger?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>Each <var>X</var> and <var>Y</var> is <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>E</code> or <code>F</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>X</var> <var>Y</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>X</var> is smaller, print <code><</code>; if <var>Y</var> is smaller, print <code>></code>; if they are equal, print <code>=</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>A B
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre><
</pre>
<p><var>10 < 11</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>E C
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>>
</pre>
<p><var>14 > 12</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>F F
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>=
</pre>
<p><var>15 = 15</var>.</p></section>
</div>
</span> | [
[
"A B\n",
"A B\n"
]
] |
p03548 | AtCoder Beginner Contest 078 - ISU | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a long seat of width <var>X</var> centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length <var>Y</var> centimeters.</p>
<p>We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least <var>Z</var> centimeters between two people, and between the end of the seat and a person.</p>
<p>At most how many people can sit on the seat?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq X, Y, Z \leq 10^5</var></li>
<li><var>Y+2Z \leq X</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> <var>Z</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>13 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>There is just enough room for three, as shown below:</p>
<div style="text-align: center;">
<img src="https://img.atcoder.jp/abc078/4a35302937c3cbc2f625156e7834d27f.png">
<p>Figure</p>
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>12 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100000 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>49999
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>64146 123 456
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>110
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>64145 123 456
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>109
</pre></section>
</div>
</span> | [
[
"13 3 1\n",
"13 3 1\n"
]
] |
p03549 | AtCoder Beginner Contest 078 - HSI | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is <code>YES</code> or <code>NO</code>.</p>
<p>When he checked the detailed status of the submission, there were <var>N</var> test cases in the problem, and the code received TLE in <var>M</var> of those cases.</p>
<p>Then, he rewrote the code to correctly solve each of those <var>M</var> cases with <var>1/2</var> probability in <var>1900</var> milliseconds, and correctly solve each of the other <var>N-M</var> cases without fail in <var>100</var> milliseconds.</p>
<p>Now, he goes through the following process:</p>
<ul>
<li>Submit the code.</li>
<li>Wait until the code finishes execution on all the cases.</li>
<li>If the code fails to correctly solve some of the <var>M</var> cases, submit it again.</li>
<li>Repeat until the code correctly solve all the cases in one submission.</li>
</ul>
<p>Let the expected value of the total execution time of the code be <var>X</var> milliseconds. Print <var>X</var> (as an integer).</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq M \leq {\rm min}(N, 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 <var>X</var>, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, <var>X</var> is an integer not exceeding <var>10^9</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3800
</pre>
<p>In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with <var>1/2</var> probability in <var>1900</var> milliseconds.</p>
<p>The code will succeed in one attempt with <var>1/2</var> probability, in two attempts with <var>1/4</var> probability, and in three attempts with <var>1/8</var> probability, and so on.</p>
<p>Thus, the answer is <var>1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>18400
</pre>
<p>The code will take <var>1900</var> milliseconds in each of the <var>2</var> cases, and <var>100</var> milliseconds in each of the <var>10-2=8</var> cases. The probability of the code correctly solving all the cases is <var>1/2 \times 1/2 = 1/4</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>608000
</pre></section>
</div>
</span> | [
[
"1 1\n",
"1 1\n"
]
] |
p03550 | AtCoder Beginner Contest 078 - ABS | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a deck consisting of <var>N</var> cards. Each card has an integer written on it. The integer on the <var>i</var>-th card from the top is <var>a_i</var>.</p>
<p>Two people X and Y will play a game using this deck. Initially, X has a card with <var>Z</var> written on it in his hand, and Y has a card with <var>W</var> written on it in his hand. Then, starting from X, they will alternately perform the following action:</p>
<ul>
<li>Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.</li>
</ul>
<p>The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.</p>
<p>X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq N \leq 2000</var></li>
<li><var>1 \leq Z, W, a_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>Z</var> <var>W</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 score.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 100 100
10 1000 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>900
</pre>
<p>If X draws two cards first, Y will draw the last card, and the score will be <var>|1000 - 100| = 900</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 100 1000
10 100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>900
</pre>
<p>If X draws all the cards first, the score will be <var>|1000 - 100| = 900</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5 1 1
1 1 1 1 1
</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 1 1
1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>999999999
</pre></section>
</div>
</span> | [
[
"3 100 100\n10 1000 100\n",
"3 100 100\n10 1000 100\n"
]
] |
p03551 | AtCoder Regular Contest 085 - HSI | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is <code>YES</code> or <code>NO</code>.</p>
<p>When he checked the detailed status of the submission, there were <var>N</var> test cases in the problem, and the code received TLE in <var>M</var> of those cases.</p>
<p>Then, he rewrote the code to correctly solve each of those <var>M</var> cases with <var>1/2</var> probability in <var>1900</var> milliseconds, and correctly solve each of the other <var>N-M</var> cases without fail in <var>100</var> milliseconds.</p>
<p>Now, he goes through the following process:</p>
<ul>
<li>Submit the code.</li>
<li>Wait until the code finishes execution on all the cases.</li>
<li>If the code fails to correctly solve some of the <var>M</var> cases, submit it again.</li>
<li>Repeat until the code correctly solve all the cases in one submission.</li>
</ul>
<p>Let the expected value of the total execution time of the code be <var>X</var> milliseconds. Print <var>X</var> (as an integer).</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq M \leq {\rm min}(N, 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 <var>X</var>, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, <var>X</var> is an integer not exceeding <var>10^9</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3800
</pre>
<p>In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with <var>1/2</var> probability in <var>1900</var> milliseconds.</p>
<p>The code will succeed in one attempt with <var>1/2</var> probability, in two attempts with <var>1/4</var> probability, and in three attempts with <var>1/8</var> probability, and so on.</p>
<p>Thus, the answer is <var>1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>18400
</pre>
<p>The code will take <var>1900</var> milliseconds in each of the <var>2</var> cases, and <var>100</var> milliseconds in each of the <var>10-2=8</var> cases. The probability of the code correctly solving all the cases is <var>1/2 \times 1/2 = 1/4</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>608000
</pre></section>
</div>
</span> | [
[
"1 1\n",
"1 1\n"
]
] |
p03552 | AtCoder Regular Contest 085 - ABS | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a deck consisting of <var>N</var> cards. Each card has an integer written on it. The integer on the <var>i</var>-th card from the top is <var>a_i</var>.</p>
<p>Two people X and Y will play a game using this deck. Initially, X has a card with <var>Z</var> written on it in his hand, and Y has a card with <var>W</var> written on it in his hand. Then, starting from X, they will alternately perform the following action:</p>
<ul>
<li>Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.</li>
</ul>
<p>The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.</p>
<p>X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq N \leq 2000</var></li>
<li><var>1 \leq Z, W, a_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>Z</var> <var>W</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 score.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 100 100
10 1000 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>900
</pre>
<p>If X draws two cards first, Y will draw the last card, and the score will be <var>|1000 - 100| = 900</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 100 1000
10 100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>900
</pre>
<p>If X draws all the cards first, the score will be <var>|1000 - 100| = 900</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5 1 1
1 1 1 1 1
</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 1 1
1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>999999999
</pre></section>
</div>
</span> | [
[
"3 100 100\n10 1000 100\n",
"3 100 100\n10 1000 100\n"
]
] |
p03553 | AtCoder Regular Contest 085 - MUL | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have <var>N</var> gemstones labeled <var>1</var> through <var>N</var>.</p>
<p>You can perform the following operation any number of times (possibly zero).</p>
<ul>
<li>Select a positive integer <var>x</var>, and smash all the gems labeled with multiples of <var>x</var>.</li>
</ul>
<p>Then, for each <var>i</var>, if the gem labeled <var>i</var> remains without getting smashed, you will receive <var>a_i</var> yen (the currency of Japan).
However, <var>a_i</var> may be negative, in which case you will be charged money.</p>
<p>By optimally performing the operation, how much yen can you earn?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq N \leq 100</var></li>
<li><var>|a_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>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum amount of money that can be earned.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
1 2 -6 4 5 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>12
</pre>
<p>It is optimal to smash Gem <var>3</var> and <var>6</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
100 -100 -100 -100 100 -100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>200
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
-1 -2 -3 -4 -5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<p>It is optimal to smash all the gems.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>2
-1000 100000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>99000
</pre></section>
</div>
</span> | [
[
"6\n1 2 -6 4 5 3\n",
"6\n1 2 -6 4 5 3\n"
]
] |
p03554 | AtCoder Regular Contest 085 - NRE | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a sequence <var>a = \{a_1, ..., a_N\}</var> with all zeros, and a sequence <var>b = \{b_1, ..., b_N\}</var> consisting of <var>0</var> and <var>1</var>. The length of both is <var>N</var>.</p>
<p>You can perform <var>Q</var> kinds of operations. The <var>i</var>-th operation is as follows:</p>
<ul>
<li>Replace each of <var>a_{l_i}, a_{l_i + 1}, ..., a_{r_i}</var> with <var>1</var>.</li>
</ul>
<p>Minimize the hamming distance between <var>a</var> and <var>b</var>, that is, the number of <var>i</var> such that <var>a_i \neq b_i</var>, by performing some of the <var>Q</var> operations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 200,000</var></li>
<li><var>b</var> consists of <var>0</var> and <var>1</var>.</li>
<li><var>1 \leq Q \leq 200,000</var></li>
<li><var>1 \leq l_i \leq r_i \leq N</var></li>
<li>If <var>i \neq j</var>, either <var>l_i \neq l_j</var> or <var>r_i \neq r_j</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>b_1</var> <var>b_2</var> <var>...</var> <var>b_N</var>
<var>Q</var>
<var>l_1</var> <var>r_1</var>
<var>l_2</var> <var>r_2</var>
<var>:</var>
<var>l_Q</var> <var>r_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible hamming distance.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 0 1
1
1 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>If you choose to perform the operation, <var>a</var> will become <var>\{1, 1, 1\}</var>, for a hamming distance of <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 0 1
2
1 1
3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>If both operations are performed, <var>a</var> will become <var>\{1, 0, 1\}</var>, for a hamming distance of <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
1 0 1
2
1 1
2 3
</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>5
0 1 0 1 0
1
1 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>2
</pre>
<p>It may be optimal to perform no operation.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>9
0 1 0 1 1 1 0 1 0
3
1 4
5 8
6 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>15
1 1 0 0 0 0 0 0 1 0 1 1 1 0 0
9
4 10
13 14
1 7
4 14
9 11
2 6
7 8
3 12
7 13
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>5
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 7</h3><pre>10
0 0 0 1 0 0 1 1 1 0
7
1 4
2 5
1 3
6 7
9 9
1 5
7 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 7</h3><pre>1
</pre></section>
</div>
</span> | [
[
"3\n1 0 1\n1\n1 3\n",
"3\n1 0 1\n1\n1 3\n"
]
] |
p03555 | AtCoder Beginner Contest 077 - Rotation | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a grid with <var>2</var> rows and <var>3</var> columns of squares.
The color of the square at the <var>i</var>-th row and <var>j</var>-th column is represented by the character <var>C_{ij}</var>.</p>
<p>Write a program that prints <code>YES</code> if this grid remains the same when rotated <var>180</var> degrees, and prints <code>NO</code> otherwise.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3)</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>C_{11}C_{12}C_{13}</var>
<var>C_{21}C_{22}C_{23}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>YES</code> if this grid remains the same when rotated <var>180</var> degrees; print <code>NO</code> otherwise.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>pot
top
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>This grid remains the same when rotated <var>180</var> degrees.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>tab
bet
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>NO
</pre>
<p>This grid does not remain the same when rotated <var>180</var> degrees.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>eye
eel
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>NO
</pre></section>
</div>
</span> | [
[
"pot\ntop\n",
"pot\ntop\n"
]
] |