After searching the original problem on codechef, it becomes clear that B can be 1 and N be 1,000,000,000. In that case, your code will iterate all values and it will take long, as its O(n). This can be reduced to O(1).
You need to see this problem as a function, and once you find the correct function, you want to maximize it. In the first example, N is 10 and B is 2, so we can do at most 5 clicks on second button. If we decide to click first button, we will always need to click it 2 times, because if we leave 1 energy remaining we can´t use it for second button. Another important observation is that you always need to click the first button X times, and then spend the rest on second button. I leave that for you to think why.
So in this case, you can do the following:
0 * 5 (click 5 times second button and 0 the first)
2 * 4 (click 4 times second button and 2 the first)
4 * 3 (click 3 times second button and 4 the first)
6 * 2 (click 2 times second button and 6 the first)
8 * 1 (click 1 times second button and 8 the first)
It is clear that our function in this case is f(n) = n * (10 - 2n), with n being the amount of clicks on first button. Expanding, we get -2n^2 + 10n. Deriving that, we get -4n + 10. If we consider the maximum to be at 0 (which it is), n = 2.5 would archieve that maximum. If we replace on the ecuation, 2.5 * (10 - 2*2.5) = 2.5 * 5 = 12.5
However we can´t actually make 2.5 clicks on first button, so we need to consider either 2 clicks or 3, and take the best one. In this case, both cases will give us the answer : 12.
In cases where N is not a multiple of B, your first steps will always be clicking first button until remaining energy is a multiple of B and then you have the same problem as before.
More generally, f(n) = N % B + n * ((N - N % B) - B*n) and the n that maximizes the function is the solution of 2Bn = (N - N % B), so n = (N - N % B) / 2B. Be careful that n can be decimal, so you should consider using floor.
This way you can compute the answer in O(1) and remove your loop.