https://www.codechef.com/FEB19B/problems/MANRECT/

I was trying to understand the solution to this problem, but in i am not able to figure out the exact concept. Can anyone please explain in detail the concept behind the solution? What is the maths behind it?