luogu#P7974. [KSN2021] Delivering Balls
[KSN2021] Delivering Balls
Problem Description
You are given a sequence of length and queries.
In the -th query, you start at column and row , and you want to reach column and row .
You may make several moves. In each move, you can choose the following two parameters:
- Column , column unchanged, column .
- Row , row unchanged, row .
If you choose row , it costs stamina. If you choose row unchanged, it costs stamina. If you choose row , it costs stamina.
You must ensure that after each move, your column index is within , and your row index is not less than .
For each query, find the minimum stamina cost.
Input Format
The first line contains a positive integer .
The second line contains positive integers .
The third line contains a positive integer .
The next lines each contain two positive integers .
Output Format
For each query, output one line containing an integer, the minimum stamina cost.
4
9 1 8 2
2
1 3
4 2
3
31
9
1 2 3 2 1 2 3 2 1
4
1 9
4 6
2 6
5 2
18
4
9
9
Hint
Sample Explanation
The following figures illustrate the two queries in the first sample:
Constraints
- Subtask 1 (7 points): There is only one set of testdata, where , , , and are , , , and in order.
- Subtask 2 (5 points): .
- Subtask 3 (6 points): .
- Subtask 4 (18 points): .
- Subtask 5 (24 points): .
- Subtask 6 (13 points): .
- Subtask 7 (27 points): No special restrictions.
For all testdata, , , , and .
Translated by ChatGPT 5