Help in IMPACT

Can someone please explain the approach/solution to this problem?
https://www.codechef.com/COME2019/problems/IMPACT/

Thanks