Optimal allocation of energy to growth and reproduction