#P1244. Frog Crossing
Frog Crossing
Frog Crossing
A team of frogs, each of a different size, are initially arranged on the left bank A in a vertical stack. The largest frog is at the bottom (standing directly on A) and every other frog is placed on the back of the frog immediately larger than itself. They wish to cross the river to the right bank D following the rules below:
- Each frog may only land on a legal landing point, which is either a lily pad, a stone, or the back of a frog which is \(1\) unit larger (i.e. exactly one size larger).
- A frog can only jump if the frog it is currently standing on (or the landing point itself) has no frog on its back.
- Frogs are allowed to jump directly from the left bank A to any stone, lily pad in the river, and even directly to a stone on the right bank D. Similarly, frogs on a lily pad or a stone in the river are allowed to jump directly to a stone on the right bank D.
- In the river, frogs can jump freely among stones, among lily pads, or between a stone and a lily pad.
- Once a frog leaves the left bank A, it cannot return. Similarly, once a frog reaches the right bank D it cannot jump back out.
- Although the stones in the river can support any number of frogs (since their weight capacity is large), because of their limited surface area, at most one frog can stand directly on a stone. Any additional frogs on a stone must be standing on top of a frog (following the rule of landing on a frog that is exactly one size larger). For lily pads, due to both their small size and limited weight capacity, at most one frog may stand on a lily pad.
- Only one frog moves per step and after each move the formation must satisfy the above rules.
- Initially, all frogs are on the left bank A, arranged in a single stack as described.
The river contains \(m\) lily pads labeled \(Y_1, \dots, Y_m\) and \(n\) stones labeled \(S_1, \dots, S_n\) in the middle. The frogs wish to finally reassemble (in the described stacking order) on the right bank D. Determine the maximum number of frogs that can cross the river from A to D obeying all these rules.
Observation: A valid landing point is available only at the lily pads and stones (and of course the banks A and D). The maximum number of frogs that can cross is limited by the number of available landing points. In fact, one may show that the answer is \(n + m + 2\), where the additional 2 count the two banks A and D.
inputFormat
The input consists of a single line containing two non-negative integers n
and m
(0 ≤ n, m ≤ 10^9
), where n
is the number of stones in the river and m
is the number of lily pads.
Note: The ordering of the input is n
first, then m
.
outputFormat
Output a single integer, the maximum number of frogs that can successfully cross from bank A to bank D following the rules.
Formula (in \(\LaTeX\)): \(\text{Answer} = n + m + 2\)
sample
0 0
2