need editorial for KOL16E

problem link:KOL16E Problem - CodeChef

could anyone explain the approach of solving the problem.