AcWing 148. 合并果子
原题链接
简单
作者:
苍茫得胖帅
,
2021-03-09 20:51:05
,
所有人可见
,
阅读 3
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10005;
int a[N];
bool cmp (int a,int b)
{
return a > b;
}
int main()
{
int n ;
cin >> n;
int i;
for (i = 0;i < n; i++)
{
cin >> a[i];
}
make_heap(& a[0] , & a[n] , cmp);
int res = 0;
while (n > 1)
{
int x = a[0];
pop_heap(& a[0] , & a[n] , cmp);
int y = a[0];
pop_heap(& a[0] , & a[n-1] , cmp);
res += x + y;
a[n-2] = x + y;
push_heap(&a[0], &a[n-1], cmp);
n--;
}
cout << res <<endl;
return 0 ;
}