# Optimization | Groups with limited spots and priority

As someone at math.stackexchange helped me with this solution, its still not quite right

Problem:

list $L$ can hold $m$ items.
$p_i$ is percent of items from the group than go in $L$ (for i = 1...3)
We have 3 smal lists $g_1$, $g_2$ and $g_3$ each with their own $n$ items.
List $L$ needs to be filled with the items from $g_1$, $g_2$ and $g_3$ but has limited space. So this means not all items can be in $L$. To distribute the number of $m$ items equally over the groups i'm finding a percentage by doing the following.

$$p=\frac{m}{n_1+n_2+n_3}$$ And this to calculate the total of items $c$ for each group ( $c_1 + c_2 + c_3 = m$ ) $$c_1=n_1*p$$ $$c_2=n_2*p$$ $$c_3=n_3*p$$

To extend this, i have assigned a priority $q_i$ to each group,

\begin{eqnarray*} p_1 &=& \frac{mq_1}{n_1q_1 + n_2q_3 + n_3q_3} \\ p_2 &=& \frac{mq_2}{n_1q_1 + n_2q_3 + n_3q_3} \\ p_3 &=& \frac{mq_3}{n_1q_1 + n_2q_3 + n_3q_3} \end{eqnarray*}

There is only 1 problem with this solution. The $p_i$ is not always $\le 1$. This means a group sometimes needs to provide more items than it is holding, wich is not possible!

2 examples:

Correct output
Total of items
$Lm$ : 25
$g_1n_1$: 30
$g_2n_2$: 10
$g_3n_3$: 10

$q_1$: 2
$q_2$: 1
$q_3$: 1

$p_1$: 0.625
$p_2$: 0.3125
$p_3$: 0.3125

$c_1$: 18
$c_2$: 3
$c_3$: 4

Wrong output
$Lm$ : 42 $\rightarrow$ higher $Ln$
$g_1n_1$: 30
$g_2n_2$: 10
$g_3n_2$: 10

$q_1$: 2
$q_2$: 1
$q_3$: 1

$p_1$: 1.05 $\rightarrow$ $p_1$ $> 1$
$p_2$: 0.525
$p_3$: 0.525

$c_1$: 31 $\rightarrow$ higher than $g_1n$ : 30 Not possible
$c_2$: 5
$c_3$: 6

Any suggestions?

-