提交时间:2024-08-20 17:35:29

运行 ID: 169852

#include <iostream> #include <algorithm> using namespace std; bool cmp(int a,int b){ return a>b; } int main() { long long n,a[100000],ans=0; cin>>n; for (int i = 0; i < n; i++){ cin>>a[i]; } sort(a,a+n,cmp); while (n!=1){ int h=a[n-1]+a[n-2]; ans+=h; n--; long long c=n; while(c-2>=0&& h>a[c-2]) { a[c-1]=a[c-2]; c--; } a[c-1]=h; } cout<<ans<<endl; return 0; }