样例如图,加上各个集合的和就好了,然后每次集合都是剔除最小值得到新集合的
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define rep(a, b) for(int i = (a); i <= (b); i++) #define rev(a, b, i) for(int i = (a); i >= (b); i--) #define FOR(a, b, i) for(int i = (a); i < (b); i++) typedef long long LL; int n; int a; struct cmp1{ bool operator ()(int &a,int &b){ return a>b;//最小值优先 } }; priority_queue<int, vector<int>, cmp1>q;//最小值优先 //priority_queue<int, vector<int>, greater<int> >q; LL ans, sum; void init() { while(!q.empty()) q.pop(); ans = 0, sum = 0; } void solve() { rep(1, n) { scanf("%d", &a); q.push(a); sum += a; } ans = sum; int x; while(!q.empty()) { x = q.top(); ans += x; sum -= x; ans += sum; //printf("%d ", ans); q.pop(); } //puts(""); printf("%lld\n", ans - x); } int main() { while(~scanf("%d", &n)) { init(); solve(); } return 0; }