Problem-N Slimes

I can do it without using DP,

Logic-keep sorting and adding the first two numbers.

Time Complexity-O(N*logN * N)
But the problem is I’m getting WA in half of the test cases.

My Code:https://ide.codingblocks.com/s/300261

Please, check if my logic is wrong or the code!!

@Rhinorox,
Because you can only merge two slimes if they are adjacent, but you aren’t taking care of that.