不過那邊測資比較強
在此附上在這邊跟那邊都AC的code
有興趣的可以去那邊挑戰
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main () {
int n;
while (cin >> n && n != 0) {
list<int> huffman;
{
int a[n];
for (int i=0; i<n; i++) cin >> a[i];
sort(a,a+n);
huffman.assign(a,a+n);
}
long long cost = 0;
while (huffman.size() > 1) {
long long sum = 0;
auto it = huffman.begin();
for (int i=0; i<2; i++) {
sum += *it;
it++;
}
cost += sum;
while (sum > *it && it != huffman.end()) it++;
huffman.insert(it,sum);
huffman.pop_front();
huffman.pop_front();
}
cout << cost << endl;
}
return 0;
}