The capacitance of capacitor with dielectric is C=KC0
Initial charge on capacitor is q=Cε=KC0ε
As cell is still connect so potential remain constant.
after slab is taken out the charge on capacitor is q′=C0ε
The charge flows through the cell is Δq=q−q′=KC0ε−C0ε=C0ε(K−1)
Energy absorbed by cell E=Δqε=C0ε2(K−1)
Energy stored in capacitor U=12qε−12q′ε=12C0ε2(K−1)
Work done W=E−U=C0ε2(K−1)−12C0ε2(K−1)=12C0ε2(K−1)