Initially charge on either capacitor is QA=QB=CV .
After dielectric is introduced, new capacitance of either capacitor =KC
After opening switch potential across capacitor A is V volts.
Let potential across capacitor B be V1
Therefore QB=CV=C1V1=KCV1
V1=VK
Initial energy in both capacitors =CV22+CV22=CV2
Final energy of capacitor A =KCV22
Final energy of capacitor B =KCV22K2=CV22K
Total final energy of both capacitors = KCV22+CV22K=(K2+12K)CV2
Ratio of the total electrostatic energy stored in both capacitors before and after the introduction of the dielectric= CV2(K2+12K)CV2=2KK2+1