POJ_3253_Fence Repair

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class compare
{
public:
bool operator()(const long long &a, const long long &b)
{
return a>b;
}
};
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
while (cin >> n)
{
priority_queue<long long, vector<long long>, compare>Q;
while (n--)
{
long long value; cin >> value;
Q.push(value);
}
long long cost = 0;
while (Q.size()>1)
{
long long first = Q.top(); Q.pop();
long long second = Q.top(); Q.pop();
Q.push(first + second);
cost += first + second;
}
cout << cost << endl;
}
return 0;
}

你可能感兴趣的:(Huffman)