If I understand your explanation of the requirement, what you have here is what can be referred to as an optimization problem. You want to minimize the amount of time needed to purchase an object subject to the cost constraints, in this case the amount of time needed to accumulate mana and spells. You can find the minimal time using
linear programming. The link is to the Wikipedia entry on the subject which seems to be aimed at folks with fairly advanced knowledge of the subject, but I checked a couple of the external
links, which seemed to explain the basics. A cursory look at CPAN didn't turn up any LP modules, which surprised me. It's an interesting problem you've posed, but I've not got the time to work it out this morning:) Hope this helps...