How to solve equation with multiple variables with each of them having different "weight"? I need to solve equation where the goal is to buy specific number of snacks for the lowest price.
There are 7 types of snack packages, each of them contains different number of snacks and has different price, here is the list:

*

*A: 0.0051 $ / 4 snacks


*B: 0.0102 $ / 4 snacks


*C: 0.0204 $ / 8 snacks


*D: 0.0408 $ / 17 snacks


*E: 0.0816 $ / 35 snacks


*F: 0.1632 $ / 58 snacks


*G: 0.3264 $ / 58 snacks
Now I need to know, how to get the best "combo of packages" for lowest price when buying 10 snacks, 20 snacks..... up to 100 snacks. (When 10 is needed, buying some extra is not problem - such as buying 12 instead of 10.)
I tried bruteforcing (such as starting from maximum number of cheapest package and then combining with other packages), but I think there mast be some smarter way to solve this. :)
 A: You want to minimize the total cost, i.e.,
$$0.0051A+0.0102B+0.0204C+0.0408D+0.0816E+0.01632F+0.3264G$$
under the condition that
$$ 4A+4B+8C+17D+25E+58F+58G\geq T,$$
where $T$ is your target number of snacks. Given prices and quantities of snaks per package, the solution will be such that $G=0$ (because $F$ includes the same number of snacks and it's half the price) and $C>0$ implies that the solution is not unique (as $B$ results in the same price per snack).
A: Maybe a too long answer for what the outcome is.
This is generally referred to as the Knapsack problem.
Mathematically formulated as:
$$minimize \sum_{i=1}^n G_i(s_i)$$
s.t.
$$ \sum_{i=1}^n s_i = V,$$
$$ s_i \ge 0$$
where $G_i(s_i)$ is the price per snack times the number of snacks and $V$ is your total number of snacks you want to buy.
You can solve this via multiple ways (e.g. dynamic programming, Lagrangian relaxation, ...)
You can also do a marginal analysis algorithm, where you first calculate $\Delta G_i(s_i)$ for all $i$ and then as long as $\sum_{i=1}^n s_i < V$ you iteratively increases the amount for the snack where $ \frac{\Delta G_i}{s_i} $ is the smallest.
So in your problem: where the price per snack is the smallest.
Thus in your problem you just need to buy A...
