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: https://www.codechef.com/viewsolution/14572124

Your code is giving wrong output in the following testcase

Input :-
1
2 2
ba

Correct Output :-
1

Your Output :-
0

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.