Your approach works fine for positive numbers but what about if i give negative numbers as input? It is not giving correct ans.
For example
let n=10
-2,-2,1,1,-3,1,-3,-3,-4,-2
the output must be -4 but it is showing 0.
Suggest correct approach to solve this question.