I'm trying to solve this problem:
http://codeforces.me/problemset/problem/455/D
My submission:
http://codeforces.me/contest/455/submission/12009574
My algorithm runs in O(q * sqrt(n)) so it should pass the test cases. So the question is: what's wrong in my code to make it giving me TLE?