#ABC479D. 往返距离 / Back and Forth Distance
往返距离 / Back and Forth Distance
Problem Statement
There are squares arranged in a row, numbered from left to right.
When you are on square , you can make one of the following moves.
- Choose an integer such that , and move to square . The cost of this move is .
- Choose an integer such that , and move to square . The cost of this move is .
You are given queries. Answer each of them. Each query is in the following format.
- Integers and are given. Find the minimum total cost required to reach square from square by repeatedly making moves.
Constraints
- for each query.
- for each query.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Here, is the -th query, given in the following format:
Output
Output lines.
The -th line should contain the answer to the -th query.
5 7
5 6 2 9
3 1 3 8
1 2
2 1
2 4
3 2
3 5
4 3
5 2
3
3
1
5
3
2
9
We explain the first and second queries.
For the first query, if you move directly from square to square , the total cost is , which is the minimum possible total cost.
For the second query, if you move from square to square and then from square to square , the total cost is , which is the minimum possible total cost.
- Source: AtCoder ABC 479 D
相关
在下列比赛中: