Merge the Books
시간 제한1.5초메모리 제한1024 MB
책 더미 n개를 두 개씩 합치는데, 합칠 때마다 올리는 더미의 무게와 두 더미의 마모값을 더한 에너지가 들고 새 마모값은 둘 중 큰 값의 두 배에 1을 더한 값이 된다. 총 에너지의 최솟값을 구한다.
문제
Little C has books, each with a weight, and he decides to merge them into a pile.
Each time when Little C merges, he can put one pile of books on top of another to merge them into one pile. If Little C puts the -th pile of books on top of the -th pile of books, the energy Little C needs to consume is the weight of the -th pile of books plus the wear value of the two piles of books.
Initially, each book is in its own pile and the wear values are all . Whenever Little C merges two piles of books, the wear value of the new pile of books formed is twice the larger of the wear values of the two piles of books before merging, plus one. The weight of the new pile of books is the sum of the weights of the two piles of books before merging.
Your task is to design a merging order to minimize the total energy consumption of Little C and output the minimum total energy consumption.
입력
This problem has multiple test data sets.
The first line of the input contains an integers , which represents the number of test data sets.
Then, each set of test data is given as input in order. For each set of test data:
The first line of input contains a positive intege , which represents the number of books.
The second line of input contains positive integers , where represents the weight of the -th book.
출력
For each set of test data, output a line containing an integer, representing the minimum total energy consumption.
제한
For all test data, it is guaranteed that: .