#P2695. Minimum Gold Cost for Head Chopping
Minimum Gold Cost for Head Chopping
Minimum Gold Cost for Head Chopping
You are given n warriors and m heads. Each warrior i has a capability value \(z_i\) which indicates that he can chop off at most one head whose size is at most \(z_i\). However, if he chops off a head, it costs exactly \(z_i\) gold coins. Each warrior can be assigned to at most one head. You are also given the sizes of the m heads, denoted as \(s_1,s_2,\ldots,s_m\). Your task is to determine the minimum total cost to chop off all heads, or output -1 if it is impossible to chop them all off.
Note: All formulas are written in \(\LaTeX\) format.
inputFormat
The input consists of three lines:
- The first line contains two integers \(n\) and \(m\): the number of warriors and the number of heads, respectively.
- The second line contains \(n\) integers \(z_1,z_2,\ldots,z_n\), where \(z_i\) represents the capability (and cost) of the i-th warrior.
- The third line contains \(m\) integers \(s_1,s_2,\ldots,s_m\), where \(s_j\) represents the size of the j-th head.
outputFormat
If it is possible to assign warriors to chop off all heads (each warrior can chop off at most one head and must be capable of handling the head's size), output a single integer denoting the minimum total number of gold coins needed. Otherwise, output -1.
sample
3 2
3 5 7
2 3
8
</p>