You are struggling with trying to write all of these conditional portions of your code and not focusing on the root of the problem.
There are quite a few test cases where your output result is much higher than the correct answer.
Your first code was much better than this latest one, all you had to do was create space in the array for a value at a[10] and put a value in it that would guarantee correctness.