-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path3496090_TLE.cc
72 lines (69 loc) · 900 Bytes
/
3496090_TLE.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1000
int n,i,len,m,sum;
int s[MAXN];
bool used[MAXN];
bool found;
void dfs(int num,int cur,int next)
{
if(num==m)
{
found=true;
return;
}
for(int i=next;!found&&i<n;++i)
{
if(!used[i])
{
if(s[i]+cur<len)
{
used[i]=true;
dfs(num,s[i]+cur,i+1);
used[i]=false;
if(cur==0)
{
return;
}
}
else if(s[i]+cur==len)
{
used[i]=true;
dfs(num+1,0,0);
used[i]=false;
return;
}
else
{
return;
}
}
}
}
int main()
{
//freopen("d:/in.txt","r",stdin);
while(cin>>n&&n!=0)
{
sum=0;
for(i=0;i<n;++i)
{
cin>>s[i];
sum+=s[i];
}
sort(s,s+n);
found=false;
memset(used,false,sizeof(used));
for(len=s[n-1];!found&&len<=sum;++len)
{
if(sum%len==0)
{
m=sum/len;
dfs(0,0,0);
}
}
cout<<len-1<<endl;
}
return 0;
}