题目:https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=40&tqId=21520&tPage=2&rp=2&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

注意一下优先队列的写法!!

代码

  1. #include <queue>
  2. #include <vector>
  3. #include <cstdio>
  4. using namespace std;
  5. priority_queue<int, vector<int>, greater<int> > q;
  6. int main(){
  7. int a;
  8. while(scanf("%d", &a) != EOF){
  9. int temp, ans = 0;
  10. for(int i = 0; i < a; i++) {
  11. scanf("%d", &temp);
  12. q.push(temp);
  13. }
  14. while(q.size() > 1){
  15. int x = q.top();
  16. q.pop();
  17. int y = q.top();
  18. q.pop();
  19. q.push(x + y);
  20. ans += x + y;
  21. }
  22. while(!q.empty()) q.pop();
  23. printf("%d\n", ans);
  24. }
  25. }