#abc344f. F - Earn to Advance
F - Earn to Advance
Score: points
问题陈述
存在一个 行和 列的网格。令 表示从上到下的第 行和从左到右的第 列的方格。
高桥最初在方格 处,此时他的钱为零。
当高桥处于方格 时,他可以在一个 行动 中执行以下操作之一:
- 保持在原地,并将他的钱增加 。
- 从他的钱中支付 ,然后移动到方格 。
- 从他的钱中支付 ,然后移动到方格 。
他不能做出使他的钱变为负数或使他超出网格范围的移动。
如果高桥采取最优策略,他需要多少个行动才能到达方格 ?
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There is a grid with rows and columns. Let denote the square at the -th row from the top and -th column from the left.
Takahashi is initially at square with zero money.
When Takahashi is at square , he can perform one of the following in one action:
- Stay at the same square and increase his money by .
- Pay from his money and move to square .
- Pay from his money and move to square .
He cannot make a move that would make his money negative or take him outside the grid.
If Takahashi acts optimally, how many actions does he need to reach square ?
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
1 2 3
3 1 2
2 1 1
1 2
4 3
4 2
1 5 7
5 3 3
Sample Output 1
8
It is possible to reach square in eight actions as follows:
- Stay at square and increase money by . His money is now .
- Pay money and move to square . His money is now .
- Stay at square and increase money by . His money is now .
- Stay at square and increase money by . His money is now .
- Stay at square and increase money by . His money is now .
- Pay money and move to square . His money is now .
- Pay money and move to square . His money is now .
- Pay money and move to square . His money is now .
Sample Input 2
3
1 1 1
1 1 1
1 1 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000
Sample Output 2
4000000004