View Code of Problem 1068

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
int a[1001];
bool cmp(int a,int b)
{
	return a>b;
}//想不起greater怎么写了.... 
int work(int s,int e)
{
	if(e==s||e-s==1)return a[s];
	else if(e-s==2)return a[s]+a[s+1]+a[s+2];
	else {
		int A=a[e],B=a[e-1],z=a[s],y=a[s+1],s1=0;
		if(2*B>=A+y)s1=2*A+z+y;
		else s1=2*B+A+z;
	}
	return s1+work(s+2,e);
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,i,j,k;
		cin>>n;
		for(i=1;i<=n;i++)cin>>a[i];
		sort(a+1,a+1+n,cmp);
		cout<<work(1,n)<<endl;
	}
	return 0; 
}

Double click to view unformatted code.


Back to problem 1068