A general exact optimal sample allocation algorithm: With bounded cost and bounded sample sizes