Let fraction of chain hanging be 1/n.
Now it is slowly lifted up,i.e. without any acceleration.
Now work done=inc. in PE [as inc. in KE=0]
The centre of mass of that part is lifted by a distance of L/2n.
Mass of this portion=M/n
Now assuming the mass to be concentrated in CM
Inc. in PE=mgh=M/n*L/2n*g=mgL/2n2