Ah, here's a little hint: Instead of using calculus you can just brute force the best solutions to the formula. Given that f and h will always be integers, and usually less than ~40, you can just do a brute force search to find the best amount of things to grab (not that I expect you to (I haven't done so myself (I just created a random formula (because I thought it would be fun (mostly) and also because it's a metaphor about life (probably) (I think)))).
Glad you liked the msuic. I probably spent more time on that than the game itself.