Cylinders
Time limit1sMemory limit128 MB
Two identical cylinders with the same n scale marks start empty; find the minimum number of fill, drain, and pour actions to leave exactly l millilitres in one cylinder, or report it impossible.
- Level
Medium7 of 10
- Topics
- BFS, Graph, Math, Simulation
- Solved
- No attempts yet
Problem
Byteasar urgently needs to measure exactly millilitres of water, but the only glassware he could buy was a pair of identical defective cylinders. Each cylinder has a capacity of millilitres and is marked with scales at exactly the same values (in millilitres); the water level can only be read at these marked heights.
Both cylinders start empty. In one unit of time Byteasar may perform any one of the following actions:
- fill a cylinder from the tap until its water level reaches one of that cylinder's scales;
- pour water from a cylinder into the sink until its level reaches one of that cylinder's scales;
- pour water from one cylinder into the other until the source cylinder's level reaches one of its scales;
- pour water from one cylinder into the other until the destination cylinder's level reaches one of its scales.
A transfer between cylinders is only possible when the destination does not overflow. Determine the minimum number of actions needed to obtain exactly millilitres of water in one of the cylinders, or report that it is impossible.
Input
The first line contains one integer (): the number of scales on each cylinder.
The second line contains a strictly increasing sequence of integers separated by single spaces: the scale values. It is guaranteed that and that () equals the capacity of each cylinder.
The third line contains one integer (): the amount of water to be measured.
Output
If Byteasar cannot measure exactly millilitres, output a single line containing the word NIE. Otherwise, output a single integer: the minimum number of actions required.