2 solutions

  • 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;
    }
    

    Information

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