|洛谷|堆|P1334 瑞瑞的木板

https://www.luogu.org/problem/show?pid=1334

类似合并果子,注意要用long long

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
int n;
priority_queue, greater > p;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) 
	{
		int x;
		scanf("%d", &x);
		p.push(x);
	}
	long long tot = 0;
	while (p.size()!=1)
	{
		long long a = p.top(); p.pop();
		long long b = p.top(); p.pop();
		tot += a+b;
		p.push(a+b);
	}
	printf("%lld\n", tot);
    return 0;
}



你可能感兴趣的:(树,-,堆,洛谷)