Let's first establish the order we should buy the snacks. Suppose we want to buy a snack of type $$$i$$$ and a snack of type $$$j$$$ ($$$i < j$$$), which one should we buy first? Note that the total value of the snacks dispensed doesn't depend on the order we buy these two snacks. However, if there is only one snack of type $$$i$$$ we cannot buy snack $$$j$$$ first. So we can assume that snacks should be bought in non-descending order (i.e. from left to right).