 # How is 109 minimum value for input : A : [ 4, 9, 13, 15, 6 ]

#1

The given test case for A : [ 4, 9, 13, 15, 6 ] has expected output of 109 but my code returns a value 104. I checked that 104 is possible if we consider 4+6 first = 10, then 10+9 = 19, then 13+15=28 , then 28+19=47
Total Cost = 10+19+28+47 = 104.

Can someone explain how 109 is minimum??

#2

#3

My question was related to answer given by the IDE and that was 109 which I felt was incorrect. As per my understanding it should have been 104.

But I figured out that 109 is the right answer because in the question, it is mentioned that we can only merge consecutive elements which I had missed.

104 would have been right answer if elements could be merged irrespective of the position.

I don’t know how you are getting 107 as the answer. That can’t be correct.

#4

[4 9 13 15 6]
Solve it as: ( ( (4 + 9) + 13) + (15 + 6) )
You will get 107 as minimum cost in this way.

#5

After merging we cant change the position of element that’s why greedy wont work.