Sanket and the Strings problem

I get the correct ans if I provide the sample testcase as a custom input but incorrect answer when I compile it without providing custom input. Why is this happening? I am pasting the code here https://pastebin.pl/view/e92406e1 .

@18bcs036 You have made your code really complicated. You just have to loop twice and try to maximise the value of ‘a’ first then try to maximise the value of ‘b’.
I am providing a simple code for you to understand. And one suggestion, try to break the problem into smaller segments in order to solve harder problems as it would help you understand well as well as help in easy debugging.