What's wrong with my code?

Code-:Solution: 48809240 | CodeChef
Question-:Contest Page | CodeChef

Hi @jai02 Looking at your code it seems you are just comparing two adjacent elements that is
if the array is [2, 7, 9, 10]
with your approach you will get the answer as


but what is asked in question is that the array will change means the value will change too, basically what I mean is the sum should look somewhat like


Hint : Sort the array or find the minimum element in the array as that is what would be the result each time between an adjacent pair.