This question was asked in February Lunch Time.
In this question constraints was 1 <= n <= 10^5, and 1 <= k <= 10^5. And this is an O(n*k) solution which got AC. Why it is not getting TLE?
Testcases seem to be pretty weak for this Problem.
This is the solution.