The formation and evolution of a neural code for rewarding memories