Why did some dinosaurs have such long necks?

Long necks let plant-eaters reach high leaves without moving their heavy bodies much.