The third test case after simulation comes out to be wrong which isn't

This is the link to my code which i submitted before viewing the test cases. According to the the online checker third test case which is 3 2 0 2 is not satisfied, the answer to this is 2, which when I ran on the same code, gave me the same answer but it still shows wrong answer. Please look into it.

check for this input
5
1 2 1 4 5

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.