题目链接:http://codeforces.com/gym/101128/my
具体题目大意可以看这个人的:http://blog.csdn.net/v5zsq/article/details/61427665
思路:反正最后都是颜色不一样的,所以我们只要逆向思维,每次挑两个颜色不一样的就可以了

//看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000") #define LL long long #define ALL(a) a.begin(), a.end() #define pb push_back #define mk make_pair #define fi first #define se second #define haha printf("haha ") const int maxn = 100000 + 5; int n; int a[maxn]; struct Point{ LL val; bool operator < (const Point &a) const{ return val > a.val; } }; int main(){ int t; cin >> t; while (t--){ scanf("%d", &n); priority_queue<Point> que; LL tmp; for (int i = 1; i <= n; i++){ scanf("%lld", &tmp); que.push(Point{tmp}); } LL ans = 0; while (que.size() >= 2){ LL t1 = que.top().val; que.pop(); LL t2 = que.top().val; que.pop(); ans += t1 + t2; //printf("t1 = %lld t2 = %lld ", t1, t2); t1 += t2; que.push(Point{t1}); } printf("%lld ", ans); } return 0; }