https://www.codechef.com/problems/H1

https://www.codechef.com/problems/H1

Can someone suggest some efficient algorithm to this problem?