Time limit exceeded on test 8: http://codeforces.com/contest/387/submission/5930949