cook 81 ABREPEAT. getting WA; unable to get what is wrong in my logic.

I am unable to get what is wrong in my code or logic for this question. my approach is that for the given ‘b’ in the sequence I am calculating the number of ‘a’ before that and once I am done with that I use the sum of AP formula for each ‘b’ in the sequence.any help will be appreciated.

here is my solution link:

Your code is giving wrong output in the following testcase

Input :-
2 2

Correct Output :-

Your Output :-

The problem is that you are not considering the case where all the b’s appear before all the a’s.

thanks a lot! got my mistake.