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.
Author: HU, Shichao Source: ZOJ Monthly, December 2003 |
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-12
typedef long long ll;
using namespace std;
#define N 3005
int dp[105][N];
int n,a[N];
void DP()
{
int i,j;
memset(dp,-1,sizeof(dp));
dp[0][0]=0;
for(int i=0;i<n;i++)
for(int v=0;v<2005;v++)
{
dp[i+1][v]=max(dp[i+1][v],dp[i][v]);
if(dp[i][v]<0) continue;
if(v+a[i+1]<2005) dp[i+1][v+a[i+1]]=max(dp[i+1][v+a[i+1]],dp[i][v]+a[i+1]);
if(a[i+1]<=v)
dp[i+1][v-a[i+1]]=max(dp[i+1][v-a[i+1]],dp[i][v]);
if(a[i+1]>=v)
dp[i+1][a[i+1]-v]=max(dp[i+1][a[i+1]-v],dp[i][v]+a[i+1]-v);
}
}
int main()
{
int i,j;
while(scanf("%d",&n)&&n>0)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
DP();
if(dp[n][0]>0)
printf("%d\n",dp[n][0]);
else
printf("Sorry\n");
}
return 0;
}
/*
4 11 11 11 11
4 1 11 111 1111
-1
*/