Where my solution is getting wrong?

please help me in finding the error in the logic in my mind i am unable to find

any TA please help on this

https://ide.codingblocks.com/s/306490 here is the improved code

@Shobhit_kumar
Consider this testcase
6 1
5 4 3 2 1 10
Ans should be “18” (5+3+10 =18) but your code is giving “10”