<pre name="code" class="cpp">#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; typedef long long ll; typedef unsigned long long ull; const int N=1000005; int inf=0x3f3f3f3f; ll mod=1e9+7; long long a[20010],ans; int main() { int n; while(~scanf("%d",&n)) { ans=0; for(int i=0;i<n;i++) scanf("%lld",&a[i]); while(n>1) { int sum; if(a[0]>a[1]) swap(a[0],a[1]); for(int i=2;i<n;i++) { if(a[0]>a[i]) { swap(a[0],a[i]); swap(a[1],a[i]); } else if(a[1]>a[i]) swap(a[1],a[i]); } sum=a[0]+a[1]; ans+=sum; a[0]=sum; a[1]=a[n-1]; n--; } printf("%lld\n",ans); } }
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> using namespace std; typedef long long ll; typedef unsigned long long ull; const int N=1000005; int inf=0x3f3f3f3f; ll mod=1e9+7; long long a[20010],ans; int main() { int n; while(~scanf("%d",&n)) { ans=0; for(int i=0;i<n;i++) scanf("%lld",&a[i]); priority_queue<ll, vector<ll>,greater<ll> >q; for(int i=0;i<n;i++) q.push(a[i]); while(q.size()>1) { ll a=q.top();q.pop(); ll b=q.top();q.pop(); ll sum=a+b; ans+=sum; q.push(sum); } printf("%lld\n",ans); } }