#C4820. Minimum Missiles Needed
Minimum Missiles Needed
Minimum Missiles Needed
You are given a number of test cases. For each test case, you are given:
- An integer
n
representing the number of enemies. - Three integers
f
,w
, ande
representing the number of Fire, Water, and Earth missiles available, respectively. - A list of
n
integers representing enemy types. The enemy types are encoded as follows:- 1: Dragon
- 2: Mermaid
- 3: Elf
- 4: Troll
The missile effectiveness is described by the following rules:
- Dragons (type 1) can only be eliminated by Fire missiles.
- Mermaids (type 2) can only be eliminated by Water missiles.
- Elves (type 3) can be eliminated by either Water or Earth missiles. When eliminating elves, use available Water missiles first, and the remaining must be eliminated with Earth missiles.
- Trolls (type 4) can be eliminated by either Fire or Earth missiles. When eliminating trolls, use the remaining Fire missiles (after eliminating dragons) first, and the rest with Earth missiles.
In other words, if we denote:
- $d$ = number of Dragons,
- $m$ = number of Mermaids,
- $l$ = number of Elves,
- $t$ = number of Trolls,
Then the elimination constraints are:
- $d \le f$
- $m \le w$
- $l \le (w - m) + e$
- $t \le (f - d) + (e - (l - \min(l, w-m)))$
If all enemies can be eliminated under these constraints, the minimum number of missiles needed is simply n
(one missile per enemy). Otherwise, output -1
to indicate it is impossible.
Your task is to determine, for each test case, the minimum number of missiles needed or -1
if elimination is impossible.
inputFormat
The input is read from standard input (stdin
) and has the following format:
T n f w e enemy1 enemy2 ... enemyn ... (repeated for each of the T test cases)
Here, T
is the number of test cases. For each test case, the first line contains four integers separated by spaces, and the next line contains n
space-separated integers representing the enemy types.
outputFormat
For each test case, output on a new line a single integer representing the minimum number of missiles needed to eliminate all enemies, or -1
if it is impossible.
The output is written to standard output (stdout
).
3
4 2 1 1
1 2 3 4
3 1 1 1
1 2 3
5 3 2 1
4 4 4 4 4
4
3
-1
</p>