HELPHAND problem: https://www.codechef.com/DEM2020/problems/HELPHAND/

Can someone tell me the logic of this question.
https://www.codechef.com/problems/HELPHAND