Time limit exceeded on test 9: http://codeforces.com/contest/385/submission/5928017