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

 #include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define mod 1000000000
int a[],b[][];
int ma(int x,int y)
{
return x>y?x:y;
}
int main()
{
int n,i,j;
while(scanf("%d",&n))
{
if(n<)break;
int s=;
for(i=; i<=n; i++)scanf("%d",&a[i]),s+=a[i];
s/=;
memset(b,,sizeof(b));
for(i=; i<=n; i++)
{
for(j=; j<=s; j++)
{
if(b[i-][j])
{
b[i][j+a[i]]=ma(b[i][j+a[i]],b[i-][j]+a[i]);
b[i][j-a[i]>?j-a[i]:a[i]-j]=ma(b[i][j-a[i]>?j-a[i]:a[i]-j],ma(b[i-][j],b[i-][j]-j+a[i]));
}
}
b[i][a[i]]=ma(a[i],b[i][a[i]]);
for(j=; j<=s; j++)
b[i][j]=ma(b[i-][j],b[i][j]);
}
if(b[n][])
printf("%d\n",b[n][]);
else puts("Sorry");
}
}

The Twin Towers zoj2059 DP的更多相关文章

  1. LightOJ1126 Building Twin Towers(DP)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...

  2. ZOJ 2059 The Twin Towers(双塔DP)

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

  3. lightoj 1126 - Building Twin Towers(dp,递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1126 题解:一道基础的dp就是简单的递推可以设dp[height_left][ ...

  4. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  5. UVA 10066 The Twin Towers

    裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...

  6. UVA 10066 The Twin Towers(LCS)

    Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...

  7. uva 10066 The Twin Towers (最长公共子)

    uva 10066 The Twin Towers 标题效果:最长公共子. 解题思路:最长公共子. #include<stdio.h> #include<string.h> # ...

  8. UVa 10192 - Vacation &amp; UVa 10066 The Twin Towers ( LCS 最长公共子串)

    链接:UVa 10192 题意:给定两个字符串.求最长公共子串的长度 思路:这个是最长公共子串的直接应用 #include<stdio.h> #include<string.h> ...

  9. Codeforces Round #273 (Div. 2) D. Red-Green Towers 背包dp

    D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. CDH-5.7.1离线安装

    CDH-5.7.1离线安装 参考自:http://blog.csdn.net/jdplus/article/details/45920733   1.文件下载 CDH (Cloudera's Dist ...

  2. java多线程sleep和wait方法的区别

    分别创建了三个类,一个测试类,两个线程类实现Runnable接口. 当有notify()唤醒线程时,执行的结果如下: 当把TestSleepaWait.class.notify();语句注释后,即没有 ...

  3. MongoDB三节点高可用模式安装

    设备: 三个1G.20G.1核的虚拟机,系统是SentOS7 min 清除原始自数据目录: rm -fr /home/mongosingle/ 创建目录: mkdir -p /home/mongosi ...

  4. python用ElemenTree快速高效的解析xml

    python解析xml有很多种方法,比较流行的由SAX,DOM和ElementTree,简要介绍一下这几种方法的异同: 方法 特点 SAX SAX解析通过流模式在解析XML的过程中触发对应的事件(st ...

  5. Day-7: 模块知识

    Python中,一个.py文件就是一个模块(module):而,包含了多个模块的一个目录,称为包. 每一个包中,都包含一个_init_.py文件,可以是一个空文件,这是Python将普通目录识别为包的 ...

  6. mac 终端常用目录跳转命令

    以前一直都是使用Windows系统,连命令行都没怎么用过.来到了Mac,在某位大神的诱导下,我开始尝试使用Mac Terminal,下面总结的是一些简单的目录跳转命令  (菜鸟级) .  文件目录 首 ...

  7. main方法和args参数

    第一次接触java常常奇怪main方法和其参数有什么用.我们只知道main方法是程序入口,其实main方法同时也是一个可以手动调用的静态方法. 我们可以利用main方法写简单的一个递归程序 publi ...

  8. javascript中=,==,与===的区别;以及特殊值NaN的讲解

    1.在js中"="是作为赋值.var a;  a=3:给a赋值为3. 2."=="称作相等,"==="被称作严格相等.当使用"== ...

  9. NFA的实现

    此次发表的是一个不确定的自动机(NFA),它可以根据输入的正规式输出由函数映像表示的结果. 此版本可以输入括号'(',')',但是,实现的过程理解起来有点吃力,所以,在时间允许的情况下,我还将写新文章 ...

  10. MySQL (八)-- 事务、变量、触发器

    1 事务 需求:有一张银行账户表,A用户给B用户转账,A账户先减少,B账户增加,但是A操作完之后断电了. 解决方案:A减少钱,但是不要立即修改数据表,B收到钱之后,同时修改数据表. 事务:一系列要发生 ...