提交时间:2024-08-20 17:03:24

运行 ID: 169780

#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){ ans+=a[n-1]+a[n-2]; n--; long long c=n; while(ans>a[c-2]&&c-2>=0) { a[c-2]=a[c-1]; c--; } a[c-1]=a[n]+a[n-1]; } cout<<ans<<endl; return 0; }