https://www.spoj.com/problems/NHAY/ seems like brute force is getting accepted. Check the comments in the problem. The problems’ time limit should be reduced or move this to tutorial section