#include <iostream>
#include <algorithm>
using namespace std;
int n,ans=0;
int apple[10001];
int main(){
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1;i <= n;i++) cin >> apple[i];
sort(apple+1,apple+1+n);
for (int i = 1;i < n;i++){
apple[i+1] += apple[i];
ans += apple[i+1];
apple[i] = 0;
sort(apple+i,apple+1+n);
}
cout << ans << endl;
return 0;
}
这也能过?!!(最长431ms,共2.19s) 而且我觉得这卡起来不难,正解只要最长5ms,共42ms。
#include <iostream>
#include <queue>
using namespace std;
int n,tmp,cnt,ans = 0;
priority_queue <int,vector <int>,greater <int> > q;
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n;
for (int i = 1;i <= n;i++) cin >> tmp,q.push(tmp);
for (int i = 1;i < n;i++){
tmp = q.top(),q.pop();
cnt = q.top(),q.pop();
ans += cnt + tmp;
q.push(cnt + tmp);
}
cout << ans << endl;
return 0;
}
建议将时限降至100ms