How to solve this question

(Problem - 520B - Codeforces)
Please explain in detail .

You can check here