比赛 20160419s 评测结果 AAAAAAAAAA
题目名称 扑克游戏 最终得分 100
用户昵称 Fmuckss 运行时间 0.017 s
代码语言 C++ 内存使用 1.84 MiB
提交时间 2016-04-19 12:45:12
显示代码纯文本
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1e5+10;

int n;

template <class T> class heap{
private:
	T a[maxn*4];
	int en;
public:
	inline heap() { en=0; }
	inline int size() { return en; }
	inline T top() { return a[1]; } 
	inline bool empty() { return en == 0; }
	inline void insert(T num) {
		a[++en] = num;
		int now = en;
		while(now != 1 && num < a[now >> 1]){
			a[now] = a[now >> 1];
			a[now >> 1] = num;
			now = (now >> 1);
		}
	}
	inline void pop() {
		a[1]=a[en];
		int now = 1;
		T tmp = a[en--];
		while( (now << 1) <= en && min(a[now << 1], a[(now << 1) | 1]) < tmp) {
			if(a[now << 1] < a[(now << 1) | 1]) {
				a[now] = a[now << 1];
				a[now << 1] = tmp;
				now = (now << 1);
			} else {
				a[now] = a[(now << 1) | 1];
				a[(now << 1) | 1] = tmp;
				now = ((now << 1) | 1);
			}
		}
	}
};

heap <int> q;

void read() {
	scanf("%d", &n);
	int tmp;
	for(int i = 1; i <= n; i++) {
		scanf("%d", &tmp);
		q.insert(tmp);
	}
}

void solve() {
	int sum = 0;
	while(q.size() >= 2) {
		int a = q.top(); q.pop();
		int b = q.top(); q.pop();
		sum += a+b;
		q.insert(a+b);
	}
	printf("%d\n", sum);
}

int main() {
	freopen("poker.in", "r", stdin);
	freopen("poker.out", "w", stdout);
	read();
	solve();
	return 0;
}