Sticks

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)

Total Submission(s) : 12 Accepted Submission(s) : 7

Problem Description

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

Input

The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.

Output

The output should contains the smallest possible length of original sticks, one per line.

Sample Input

9

5 2 1 5 2 1 5 2 1

4

1 2 3 4

0

Sample Output

6

5

Source

PKU

sort

^{?}Download sourceCode.cpp

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 | #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int n,i,sum; int a[70]; bool used[70]; int dfs(int unused,int left){ if(unused==0&&left==0) return 1; if(left==0){ left=i; } for(int j=0;j<n;j++){ if(used[j]||a[j]>left) continue; used[j]=1; if(dfs(unused-1,left-a[j])) return 1; used[j]=0; if(a[j]==left||left==i) break; } return 0; } bool cmp(int x,int y){ return x>y; } int main(){ while(1){ sum=0; memset(used,0,sizeof(used)); scanf("%d",&n); if(0==n) break; for(i=0;i<n;i++){ scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n,cmp); for(i=a[0];i<sum;i++){ if(sum%i!=0) continue; if(dfs(n,0)) break; } printf("%d\n",i); } return 0; } |

texttexttext`code`

more code

~~~~