USACO 4.1 Beef McNuggets

1 /*
2 PROG: nuggets
3 ID: jiafeim1
4 LANG: C++
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <math.h>
10 #include <algorithm>
11
12  long gcd(long a,long b){
13 return b?gcd(b,a%b):a;
14 }
15
16  int n;
17
18 long count[12];
19 bool can[65600]={false};
20 int main()
21 {
22 FILE *fin = fopen("nuggets.in", "r");
23 FILE *fout = fopen("nuggets.out", "w");
24
25 fscanf(fin,"%d",&n);
26 if(n==1)
27 {
28 fprintf(fout,"0\n");
29 return 0;
30 }
31 for(int i = 0 ;i<n;++i)
32 {
33 fscanf(fin,"%d",count+i);
34 }
35 std::sort(count,count+n);
36
37 long gcdx = count[0];
38 for(int i = 1;i<n;++i)
39 {
40 gcdx=gcd(gcdx,count[i]);
41 }
42
43 if(gcdx!=1 || count[0]==1)
44 {
45 fprintf(fout,"0\n");
46 return 0;
47 }
48
49 long max = count[n-1]*count[n-2];
50
51 can[0]=true;
52 for(int i = 0;i<n;++i)
53 {
54 for(int j = count[i];j<=max;++j)
55 {
56 if(can[j-count[i]])
57 can[j]=true;
58 }
59 for(int j = max;j>=1;--j)
60 {
61 if(!can[j])
62 {
63 max = j;
64 break;
65 }
66 }
67 if(max == 0)
68 {
69 fprintf(fout,"0\n");
70 return 0;
71 }
72 }
73 fprintf(fout,"%d\n",max);
74 fclose(fin);
75 fclose(fout);
76 return 0;
77 }
posted @ 2011-06-07 19:59  幻魇  阅读(303)  评论(0编辑  收藏  举报