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: 6ms
Memory: 1724kB
Author: 2018212212307
In contest: 1271

#include<bits/stdc++.h>
#include<vector>
#include<iostream>
#include<queue>
#include<map>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
int t;
int n;
int x[2000];
ll ans=0;
int f1()
{
	return x[1]+x[n-1]+x[n];
}
int f2()
{
	return x[n]+x[2]+x[2];
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>x[i];
		}
		sort(x+1,x+1+n);
		ans=0;
		if(n==1)
		{
			cout<<x[1]<<endl;
		}
		else if(n==2)
		{
			cout<<x[2]<<endl;
		}
		else
		{
			for(int i=2;i<=n-2;i++)
			{
				ans+=x[i];
				ans+=x[1];
			}
			ans+=min(f1(),f2());
			cout<<ans<<endl;
		}
	}
}