2 solutions

  • 2
    @ 2022-1-1 21:59:43

    我用的是优先队列,把每次都加用力最小的;

    #include<bits/stdc++.h>
    typedef long long ll; 
    using namespace std;
    int a[1000010];
    int main()
    {
    	priority_queue<int,vector<int>,greater<int> >q;
    	ll n,m,sum=0,ans=0;
    	cin>>n;
    	for(int i=0;i<n;i++)
    	{
    		cin>>m;
    		q.push(m);
    		sum+=m;
    	}
    	while(!(q.top()==sum))
    	{
    	int xx=q.top();
    		q.pop();
    	int yy=q.top();
    		q.pop();
    		q.push(xx+yy);
    	ans+=xx+yy;
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    
    • 0
      @ 2022-1-18 17:16:51

      用了multiset,利用它自己排序的特性,一直取前两个数字就可以取得最小值

      #include <bits/stdc++.h>
      using namespace std;
      int k[10007];
      int main()
      {
      	int n;
      	cin>>n;
      	multiset <int> s;
      	int temp;//读入数字
      	for(int i=0;i<n;i++)
      	{
      		cin>>temp;
      		s.insert(temp);
      	}
      	int sum=0,gcc=0;
      	while(!s.empty())
      	{
      		gcc=0;
      		gcc+=*s.begin();
      		s.erase(s.begin());	 
      		if(!s.empty())
      	  {
      		gcc+=*s.begin();
      		s.erase(s.begin());
            }
            if(!s.empty())s.insert(gcc);
      	 sum+=gcc;
      	} 
      	cout<<sum;  
      	return 0;
      }
      
      • 1

      Information

      ID
      300
      Time
      1000ms
      Memory
      128MiB
      Difficulty
      4
      Tags
      # Submissions
      41
      Accepted
      19
      Uploaded By