Magic Slayer
Time limit8sMemory limit512 MB
Given N monsters with hit points and M spells that damage one or all monsters at some power cost, find the minimum total power to defeat every monster.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Brute force, Implementation
- Solved
- No attempts yet
Problem
You are in a fantasy world overrun by monsters. You are a slayer who fights monsters with magic spells.
Each monster has hit points, which represent its vitality. You can decrease their hit points with magic spells: each spell deals a certain amount of damage, which reduces the hit points of either one monster or all monsters in front of you, depending on the spell. A monster is defeated when its hit points drop to zero or below. On the other hand, each spell may consume a certain amount of your magic power. Since your magic power is limited, you want to defeat the monsters using as little power as possible.
Write a program for this purpose.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
HP1
HP2
...
HPN
M
Name1 MP1 Target1 Damage1
Name2 MP2 Target2 Damage2
...
NameM MPM TargetM DamageM
N is the number of monsters in front of you (1 ≤ N ≤ 100). HPi is the hit points of the i-th monster (1 ≤ HPi ≤ 100000). M is the number of available magic spells (1 ≤ M ≤ 100). Namej is the name of the j-th spell and consists of up to 16 uppercase and lowercase letters. MPj is the amount of magic power consumed by the j-th spell (0 ≤ MPj ≤ 99). Targetj is either "Single" or "All", indicating that the j-th spell deals damage to just a single monster or to all monsters respectively. Damagej is the amount of damage dealt by the j-th spell (per monster in the case of "All") (0 ≤ Damagej ≤ 999999).
All numbers in the input are integers. There is at least one spell that deals non-zero damage to monsters.
The last dataset is followed by a line containing a single zero. This line is not part of any dataset and is not processed.
Output
For each dataset, print on one line the minimum amount of magic power consumed to defeat all the monsters in the input.