#P10430. [JOIST 2024] 鱼 3 / Fish 3
[JOIST 2024] 鱼 3 / Fish 3
Problem Description
JOI is raising fish in a large tank. The fish are numbered from to .
JOI has two types of fish food, and , and there is an unlimited supply of both. Each time one piece of food is added to the tank, exactly one fish eats it (any fish may eat it). Depending on the type of food and which fish eats it, the fishes’ intelligence changes as follows:
- When the -th fish () eats one piece of type food, the intelligence of the -th fish increases by exactly .
- When the -th fish () eats one piece of type food, the intelligence of all fish whose indices are at least increases by exactly .
Currently, the intelligence of all fish is . JOI wants the intelligence of the -th fish () to become its desired intelligence , but this is not always possible.
Therefore, he considers questions. The -th question () is as follows:
- Starting from the state where the intelligence of all fish is , by repeating the action of putting food into the tank zero or more times, is it possible to reach a state where all fish have exactly their desired intelligence values? Moreover, if it is possible, what is the minimum number of type food pieces that must be put into the tank?
Write a program that, given information about JOI’s fish and the questions, answers the questions.
Input Format
Read the following from standard input:
- ...
Output Format
Output lines. On the -th line (), if it is possible to reach a state where fish have exactly their desired intelligence values, output the minimum number of type food pieces that must be put into the tank. Otherwise, output .
4 2
3 1 2 1
1
1 3
1
4 2
0 1 0 1
3
1 2
2 3
1 1
0
-1
0
5 1
3 1 4 1 5
3
1 5
2 4
3 5
5
3
3
6 3
16 14 13 8 6 5
4
1 4
2 5
3 3
1 6
9
8
0
-1
Hint
Sample Explanation 1
For example, in the following case, fish all end up with exactly their desired intelligence values, and the number of type food pieces put into the tank is .
- At first, the intelligence of fish is , respectively.
- Next, JOI puts one piece of type food into the tank, and it is eaten by fish . As a result, the intelligence of fish becomes , respectively.
- Then, JOI puts one piece of type food into the tank, and it is eaten by fish . As a result, the intelligence of fish becomes , respectively.
- Finally, JOI puts one piece of type food into the tank, and it is eaten by fish . As a result, the intelligence of fish becomes , respectively.
- Since it is impossible to reach a state where fish have exactly their desired intelligence values without putting any type food, output .
This sample satisfies the constraints of Subtasks and .
Constraints
- .
- .
- .
- ().
- ().
- All given values are integers.
Subtasks
- (9 points) , .
- (7 points) ().
- (28 points) .
- (20 points) ().
- (36 points) No additional constraints.
Translated by ChatGPT 5