Given an array of size n containing integers and a sum initialized to 0.

You have to perform n-1 operations.

In each operation, you find the smallest element and remove it. This element has one or two neighbours. Add the smallest neighbour to the sum.

Print the sum after n-1 operations, i.e. when only 1 element is left.

e.g.

arr[] = {4,1,2,3}

smallest element is 1 and smallest neighbour of 1 is 2

So after operation 1

arr[] = {4,2,3}

sum = 2

after second operation,

arr[] = {4,3}

sum = 2 + 3

after third operation

arr[] = {4}

sum = 2 + 3 + 4

How do I approach this problem in efficient manner? I am not able to understand the approach in editorial