The evolution of resistance through costly acquired immunity