I have been trying to solve the following spoj problem for quite a few days but hasn't been able to come up with a solution. The link to the problem is http://www.spoj.com/problems/HALCAND/. Even a slightest of help will be highly appreciated. Thanks in advance.