文章
27
粉丝
86
获赞
10
访问
30.4k
C++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(){
int n;
while(cin >> n){
priority_queue<int, vector<int>, greater<int>> q;
int num;
for(int i = 0; i < n; i++){
cin >> num;
q.push(num);
}
int a, b, sum = 0;
while(q.size() != 1){
a = q.top();
q.pop();
b = q.top();
q.pop();
sum += a + b;
q.push(a + b);
}
cout << sum << endl;
}
return 0;
}
登录后发布评论
刚才发错了
错误的,这是树,不是加减,没有通过就不要发
请问为什么我写的数据不是全通过
#include<cstdio>
#include<queue>
using namespace std;
int main(){
int n;
scanf("%d",&n);
priority_queue<long long,vector<long long>,greater<long long> > q;
for(int i=0;i<n;i++){
long long temp;
scanf("%d",&temp);
q.push(temp);
}
long long sum=0;
long long a,b;
while(q.size()!=1){
a=q.top();
q.pop();
b=q.top();
q.pop();
sum +=a+b;
q.push(a+b);
}
printf("%lld\n",sum);
return 0;
}