
On the second day of school, the computer science teacher shows some more impressive screensavers on the computer. This time, the screensaver called :kevin: stands out to you, mainly due to the simple rules that govern :kevin:.
Upon careful analysis, :kevin: can be represented as an array of
A ball enters the array from the left end, and is moving to the right at a rate of
- If the ball hits a horizontal mirror, the ball's direction stays the same and the mirror becomes vertical.
- If the ball hits a vertical mirror, the ball will move in the opposite direction and the mirror becomes horizontal.
As a fan of :kevin:, you want to implement this screensaver yourself! Firstly, the ball tends to leave the array too quickly. Given the initial conditions, your task is to calculate the number of seconds that the ball will stay in the array. Since this number may be too small for your own taste, there will be
Constraints
In all subtasks:
Subtask | Points | ||
---|---|---|---|
1 | 25 | ||
2 | 25 | ||
3 | 50 |
Input Specification
The first line will contain two space-separated integers,
The second line will contain
On the next
Output Specification
For each of the
Sample Input
5 4
|--|-
1
1
1
4
Sample Output
7
1
7
5
Explanation for Sample Output
On the first change, the initial array is set to ---|-
.
The ball leaves the array after
On the second change, the initial array is set to |--|-
.
The ball leaves the array after
On the third change, the initial array is set to ---|-
. This is the same as the first change, so the ball leaves the array after
On the fourth change, the initial array is set to -----
, and all mirrors are horizontal. The ball leaves the array after
Comments