Tested on http://codeforces.com/problemset/problem/802/N and got TLE on test #6... input of just 500 from a maximum of 2200.