An optimization problem and its application in population dynamics