poj 1011

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
	return a>b;
}
int n;
int a[66];
int v[66];
int dfs(int s,int s1,int minn)
{
	int i;
	if(s==0)
		return 1;
	if(s1 > s)
		return 0;
	if(s1==0)
		s1=minn;
	for(i=0;i<n;i++)
	{
		if(i&&(a[i]==a[i-1])&&(v[i-1]==0))
			continue;
		if((v[i]) || (s1 < a[i]))
			continue;
		v[i]=1;
		if(dfs(s - a[i] ,s1-a[i],minn))
			return 1;
		v[i]=0;
		if(a[i]==s1||s1==minn)
			return 0;
	}
        return 0;
}

int main()
{
	int i,j,m;
	while(scanf("%d",&n)&&n)
	{
		int s=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			s+=a[i];
			v[i]=0;
		}
		int minn;
		sort(a,a+n,cmp);
		minn=a[0];
		while(1)
		{
			while((s%minn)!=0)
				minn++;
			if(dfs(s,minn,minn))
			   break;
			minn++;
		}
		printf("%d\n",minn);
	}
	return 0;
}


你可能感兴趣的:(poj 1011)