ZOJ 2059 the Twin Towers (Twin Towers DP)

Source: Internet
Author: User



The Twin Towers Time limit: 2 Seconds Memory Limit: 65536 KB Twin Towers We see your standing tall, though a building ' s lost our faith would never fall.
Twin Towers The world hears your call, though your ' re gone it only strengthens our resolve.
we couldn ' t make it through this without your 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 one, 2001, Five-year-old Rosie Decids to Rebui LD a tallest Twin Towers by using the crystals her brother have collected for years. Would she succeed in building the towers of the same height?


Input

There is mutiple test cases.

One line forms a-test case. The first integer n (n < a) tells you the number of crystals she 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 is in cube shape. The total height of crystals is smaller than 2000.


Output

If It is impossible, you would say "Sorry" and 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


Twin Towers DP

DP[I][2000] Represents the first brick, stacked into two towers of the height difference, each brick has two choices either not, either put the left tower, or put the tower on the right


#include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> #include < Math.h> #include <stdio.h>using namespace Std;int a[105];int dp[105][4005];int n;int main () {while (scanf ("%d",        &n) (!=eof) {if (n<0) break;        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        Memset (Dp,-1,sizeof (DP));        dp[0][0+2000]=0;            for (int i=1;i<=n;i++) {memcpy (dp[i],dp[i-1],sizeof (Dp[i]));                for (int j=-1999;j<=1999;j++) {if (dp[i-1][j+2000]==-1) continue;                    if (j<0) {Dp[i][j-a[i]+2000]=max (dp[i][j-a[i]+2000],dp[i-1][j+2000]+a[i]);                Dp[i][j+a[i]+2000]=max (Dp[i][j+a[i]+2000],dp[i-1][j+2000]+max (0,j+a[i)); } else {Dp[i][j+a[i]+2000]=max (dp[i][j+a[i]+2000],dp[i-1][j+2000]+a[i])                    ; Dp[i][j-a[i]+2000]=max (Dp[i][j-a[i]+2000],dp[i-1][j+2000]+max (0,a[i]-j));        }}} if (Dp[n][2000]!=0&&dp[n][2000]!=-1) printf ("%d\n", dp[n][2000]);    else printf ("sorry\n"); } return 0;}


The Twin Towers Time limit: 2 Seconds Memory Limit: 65536 KB Twin Towers We see your standing tall, though a building ' s lost our faith would never fall.
Twin Towers The world hears your call, though your ' re gone it only strengthens our resolve.
we couldn ' t make it through this without your 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 one, 2001, Five-year-old Rosie Decids to Rebui LD a tallest Twin Towers by using the crystals her brother have collected for years. Would she succeed in building the towers of the same height?


Input

There is mutiple test cases.

One line forms a-test case. The first integer n (n < a) tells you the number of crystals she 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 is in cube shape. The total height of crystals is smaller than 2000.


Output

If It is impossible, you would say "Sorry" and 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

ZOJ 2059 the Twin Towers (Twin Towers DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.