FLOOD
Problem
You are given a system of water tanks connected by
pipes.
Each tank is numbered with an integer
where (
) and has a known capacity
- meaning how many units of water it can hold.
Each pipe connects two tanks at different heights, and water flows from the higher tank to the lower tank through these pipes (never the other way around).
When a tank overflows, the excess water is directed through the pipes to lower-connected tanks. If multiple pipes connect the tank to lower tanks, the excess water is distributed as follows: the lower tanks are sorted by their numbers in ascending order. The first unit of excess water goes to the first tank in this sorted order, the second unit to the second tank, and so on. If there is more excess water than tanks, this process repeats from the beginning until all excess water is allocated.
However, if a tank overflows and there are no pipes connecting it to a lower tank, the system floods. This scenario must be avoided.
In this problem, you will answer queries about the same system of water tanks.
Each query
(where
) will be independent of the others and will have the form:
If all tanks are initially empty, what is the maximum amount of water I can pour into tank without flooding the system?
Input:
The first line of the input will contain three integers ,
, and
, separated in pairs by a single space: the number of tanks, the number of pipes, and the number of queries to be answered.
The second line will contain
integers separated in pairs by a single space: the capacities
of the tanks.
Each of the next
lines will contain two integers
and
, separated by a single space: indicating there is a pipe directed from tank
(which is at a higher position) to tank
(which is at a lower position).
The last line of the input will contain
numbers separated in pairs by a single space: the initial tanks
for the queries to be answered.
Output:
The output should consist of lines, where each line contains exactly one integer: the answer to the corresponding query in order.
Constraints
- Each pipe connects two different valid tanks where the first is positioned higher than the second. (In other words, it will be possible for each tank to correspond to a height in such a way that all pipes are consistent.) The same pipe will not be given more than once.
- Time Limit:
sec.
- Memory Limit:
MB.
Attention: For large values of and
, the answer to the queries may exceed
.
- For test cases with a total value of
, it will be Q \le 10 and no number in the input file will exceed
.
- For test cases with a total value of
, each tank will have at most one pipe leading to it.
Example of Input - Output Files:
STDIN (flood.in)
9 10 7
5 3 1 7 9 5 19 2 4
1 2
2 3
3 7
4 5
4 8
1 4
8 9
4 3
4 6
5 7
1 2 3 4 5 6 8
STDOUT (flood.out)
44
23
20
29
28
5
6
Explanation of the example
The system of tanks is shown in the above diagram, where the circles represent the tanks. Inside each circle is the tank number, while outside is written its capacity.
For the first query, if we pour units of water into tank
(the highest one), the following will occur:
- Tank
receives
units of water and has a capacity of
units, so
units of water will overflow. From these,
units will go to tank
and
units to tank
.
- Tank
receives
units of water and has a capacity of
units, so
units of water will overflow and go to tank
.
- Tank
receives
units of water and has a capacity of
units, so
units of water will overflow. These will go to tanks
,
,
, and
, each receiving
units.
- Tank
receives
units of water and has a capacity of
units, so
unit of water will overflow and go to tank
.
- Tank
receives
unit of water and has a capacity of
units, so it will not overflow.
- Tank
receives a total of
units of water:
from tank
and
from tank
. It has a capacity of
unit, so
units will overflow and go to tank
.
- Tank
receives
units of water and has a capacity of
units, so it will not overflow.
Therefore, by pouring units of water into tank
, the system does not flood.
However, if we add even one more unit of water, tank
will receive more than
units of water, causing the system to flood.
Note that if we change the numbering of the tanks in the above system and ask exactly the same questions, the answers may be different due to the way excess water is distributed in case a tank overflows. The second example you will see in Hellenico is the same as the above, but with a different numbering of the tanks.
Comments