Marbles problem

what’s wrong with my code

You solution is O(n*k) which will exceed the time limit. Try to think of a linear solution.
Feel free to ask here if you are unable to think of a solution.