Your code is too slow. I got exactly the same problem, and it took me some time to understand what was happening.
In short, you should find a solution in O(n), while the trivial solution (probably the first idea for most people) is in O(n^2).
It is pity that the error is not more explicit. First, the text of the problem should indicate clearly that the expected solution must be optimal. For most other problems, speed is never an issue, so I guess it is unexpected to fail due to a slow program. Secondly, the error should be much more explicit.