2
9
6
-1
HintFor the first sample case, there are two piles [1, 1] at the beginning. The best way is to merge [1, 1] into [2] directly, and the cost is 2. For the second sample case, there are three piles [1, 2, 3] at the beginning. The best way is to merge [1, 2] into [3] at first, and then merge [3, 3] into [6]. The minimum total cost is 3 + 6 = 9. For the third sample case, there are three piles [1, 2, 3] at the beginning. The best way is to merge [1, 2, 3] into [6] directly, and the cost is 6. For the fourth sample case, there are four piles [1, 2, 3, 4] at the beginning. Neither can she collect them into one pile directly nor by merging several times, so it is impossible for her to achieve the task.