Algorithms

We have n piles of apples, and we want to get them together.
Once a time we can combine two piles of apples with a and b apples seperately together, using a+b forces. We want to know the minimum forces we use to combine all piles of apples together.
Input:
apple(n,lst)
where lst contains n numbers representing the number of apples in each pile

Algorithms

We have n piles of apples, and we want to get them together.
Once a time we can combine two piles of apples with a and b apples seperately together, using a+b forces. We want to know the minimum forces we use to combine all piles of apples together.
Input:
apple(n,lst)
where lst contains n numbers representing the number of apples in each pile

#include<stdio.h>
#include<queue>
using namespace std;
int apple(int n,int* a)
{
        priority_queue<int,vector<int>,greater<int> >q;
        for(int i=0; i<n; i++)
            q.push(a[i]);
        int ans=0,tmp;
        while(q.size()>=2)
        {
            tmp=0;
            tmp+=q.top();
            q.pop();
            tmp+=q.top();
            q.pop();
            ans+=tmp;
            q.push(tmp);
        }
        return ans;
}