1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6 using namespacestd;7 intN,a;8typedefLong Longll;9 ll ans;Ten voidReadint&x) { Onex=0;intf=1;Charch; A for(Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') f=-1; - for(; isdigit (ch); Ch=getchar ()) x=x*Ten+ch-'0'; x*=F; - } thepriority_queue<int,vector<int>,greater<int> >Heap; - intMain () { -Read (n), ans=0; - for(intI=1; i<=n;i++) Read (a), Heap.push (a); + for(intX,y,i=1; i<n;i++){ -x=heap.top (), Heap.pop (); +y=heap.top (), Heap.pop (); AAns+=x+y; Heap.push (x+y); at } -printf"%lld\n", ans); - return 0; -}
View Code
Practice: Heap Entry Questions, learn the usage of the system heap and fog (:
Dagen:priority_queue<int>heap;
Little Gan:priority_queue< int,vector<int>,greater<int> >heap;
Heap.top () function: The value of the element at the top of the heap;
Heap.pop () Process: Popup heap top element;
Heap.empty () function: heap empty or not, heap empty return 1, otherwise return 0;
Heap.push () Process: Adds an element to the heap and maintains the nature of the heap.
bzoj1724: [Usaco2006 nov]fence Repair Cutting Board