#2394. A 鸭子与按钮
A 鸭子与按钮
Duck Button Game
Background
Qiqi and her friends are playing a game on a one-dimensional grid consisting of cells numbered from to left to right.
Problem Description
Each cell has a button. If at any moment there are at least ducks on cell , the button on that cell is permanently pressed. Once pressed, it stays pressed even if the ducks leave. To win the game, all buttons must be pressed.
Initially, there are ducks on cell . In each move, a single duck may move one cell to the left or to the right.
Determine the minimum total number of moves required to win the game. It is guaranteed that there exists a way to win the game.
Input Format
The input is given from standard input in the following format.
Output Format
Print a single integer, the minimum total number of moves to win the game.
Sample
2 199
175 42
42
5 3
1 1 0 1 0
3
5 7
2 2 2 2 2
8
7 5
1 3 3 4 5 5 5
30
8 9
7 6 6 6 3 3 3 1
28
8 5
2 3 5 1 4 2 1 0
21
4 1000000000
1 1 1 999999999
2999999997
Constraints
up to .
up to .
for all .
It is guaranteed that there exists a way to win the game.