Start: Jun, 05, 2019 15:00:00
2019春ACM通识课考试(第二场)
End: Jun, 05, 2019 18:00:00
Time elapsed:
Time remaining:

Problem_ID: G
Result: Accepted
Time: 9ms
Memory: 1716kB
Author: 2018212212128
In contest: 1271

#include<iostream>
#include<cmath>
#include<algorithm> 
using namespace std;
int guo(int *T,int n)
{
	if(n==1)
		return T[0];   
	else if(n==2)
		return T[0]>T[1]?T[0]:T[1];
	else  
	{
		sort(T,T+n);
		int sum=0;
		while(1)
		{
			if(n==2)
			{
				sum+=T[1];
				break;
			}
			else if(n==3)
			{
				sum+=T[0]+T[1]+T[2];
				break;
			}
			else
			{
				int t1=T[0]+T[1]+T[1]+T[n-1];
				int t2=T[0]+T[0]+T[n-1]+T[n-2];
				sum += (t1>t2?t2:t1);
				n-=2;
			}
		}
		return sum;
	}
}
int main()
{
	int a,n,T[1010];
	scanf("%d",&a);
	while(a--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",&T[i]);
		int x=guo(T,n);
		printf("%d\n",x);
	}
	return 0;
}