For, each i <= max_cost hit the query, with cost=i and sum up the result["total"]
Login before adding your answer.