Time limit exceeded on test 18: http://codeforces.com/contest/385/submission/5928055