The Twin Towers

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Twin towers we see you standing tall, though a building's lost our faith will never fall.
Twin towers the world hears your call, though you're gone it only strengthens our resolve.
We couldn't make it through this without you Lord, through hard times we come together more. ... 

Twin Towers - A Song for America

In memory of the tragic events that unfolded on the morning of September 11, 2001, five-year-old Rosie decids to rebuild a tallest Twin Towers by using the crystals her brother has collected for years. Will she succeed in building the two towers of the same height?


Input

There are mutiple test cases.

One line forms a test case. The first integer N (N < 100) tells you the number of crystals her brother has collected. Then each of the next N integers describs the height of a certain crystal.

A negtive N indicats the end.

Note that all crytals are in cube shape. And the total height of crystals is smaller than 2000.


Output

If it is impossible, you would say "Sorry", otherwise tell her the height of the Twin Towers.


Sample Input

4 11 11 11 11
4 1 11 111 1111 
-1


Sample Output

22
Sorry

 

 

 

 1 #include <iostream>
 2 #include <math.h>
 3 #include <stdio.h>
 4 #include <string.h>
 5 using namespace std;
 6 #define mod 1000000000
 7 int a[101],b[101][1001];
 8 int ma(int x,int y)
 9 {
10     return x>y?x:y;
11 }
12 int main()
13 {
14     int n,i,j;
15     while(scanf("%d",&n))
16     {
17         if(n<0)break;
18         int s=0;
19         for(i=1; i<=n; i++)scanf("%d",&a[i]),s+=a[i];
20         s/=2;
21         memset(b,0,sizeof(b));
22         for(i=1; i<=n; i++)
23         {
24             for(j=0; j<=s; j++)
25             {
26                 if(b[i-1][j])
27                 {
28                     b[i][j+a[i]]=ma(b[i][j+a[i]],b[i-1][j]+a[i]);
29                     b[i][j-a[i]>0?j-a[i]:a[i]-j]=ma(b[i][j-a[i]>0?j-a[i]:a[i]-j],ma(b[i-1][j],b[i-1][j]-j+a[i]));
30                 }
31             }
32             b[i][a[i]]=ma(a[i],b[i][a[i]]);
33             for(j=0; j<=s; j++)
34                 b[i][j]=ma(b[i-1][j],b[i][j]);
35         }
36         if(b[n][0])
37             printf("%d\n",b[n][0]);
38         else puts("Sorry");
39     }
40 }
View Code

 

posted on 2014-03-03 19:01  ERKE  阅读(292)  评论(0编辑  收藏  举报