Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

FJ has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,000,000 - these are very tall cows). The bookshelf has a height of B (1 ≤ B ≤ S, where S is the sum of the heights of all cows).

To reach the top of the bookshelf, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of the bookshelf in order for the cows to reach the top.

Since a taller stack of cows than necessary can be dangerous, your job is to find the set of cows that produces a stack of the smallest height possible such that the stack can reach the bookshelf. Your program should print the minimal 'excess' height between the optimal stack of cows and the bookshelf.

Input

* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi

Output

* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows and the height of the shelf.

Sample Input

5 16
3
1
3
5
6

Sample Output

1

思路:读题读的头大,基础dp问题,01背包,dp[i][j]表示前i只牛,牛的高度为j,其能达到书架的最高高度,转移方程为选/不选第i只牛,dp[i][j]=max(dp[i-1][j],dp[i-1][j-h[i]]+h[i])
const int maxm = 2e7+;

int dp[maxm], sum, buf[];

int main() {
ios::sync_with_stdio(false), cin.tie();
int N, B;
cin >> N >> B;
for(int i = ; i <= N; ++i) {
cin >> buf[i];
sum += buf[i];
}
for(int i = ; i <= N; ++i) {
for(int j = sum; j >= buf[i]; --j)
dp[j] = max(dp[j], dp[j-buf[i]]+buf[i]);
}
for(int i = ; i <= sum; ++i)
if(dp[i] >= B) {
cout << dp[i] - B << endl;
break;
} return ;
}

Day9 - C - Bookshelf 2 POJ - 3628的更多相关文章

  1. poj 3628 Bookshelf 2

    http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...

  2. POJ 3628 Bookshelf 2 0-1背包

    传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...

  3. POJ 3628 Bookshelf 2(01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9488   Accepted: 4311 Descr ...

  4. POJ 3628 Bookshelf 2 (01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 Descr ...

  5. POJ 3628 Bookshelf 2【背包型DFS/选or不选】

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11105   Accepted: 4928 Desc ...

  6. POJ 3628 Bookshelf 2【01背包】

    题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...

  7. poj 3628 Bookshelf 2 基本01背包

    题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少. 题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp ...

  8. POJ 3628 Bookshelf 2 题解

    本题解法非常多,由于给出的数据特殊性故此能够使用DFS和BFS,也能够使用01背包DP思想来解. 由于一般大家都使用DFS,这里使用非常少人使用的BFS.缺点是比DFS更加耗内存,只是长处是速度比DF ...

  9. poj 3628 (搜索or背包)

    好久没看背包题目了!!!生疏了!!!! 这题是背包题!!!不过对于这题,解决方法还是搜索省时!!! 题意:第一行给你一个N和VV,接下来N行,每行一个数,求得是任选N个数组合求和,求组合的和大于VV而 ...

随机推荐

  1. 吴裕雄--天生自然Numpy库学习笔记:NumPy 算术函数

    NumPy 算术函数包含简单的加减乘除: add(),subtract(),multiply() 和 divide(). 需要注意的是数组必须具有相同的形状或符合数组广播规则. import nump ...

  2. 转专业后对于C语言补修的一些体会(1)

    在转入软件工程后,原来的C语言程序设计只有三学分,而信息学院的C语言程序设计有四学分.迫于无奈的我只能再补修一遍C语言,自我认为大一对于C语言的学习已经基本足够,但我发现信息学院用的是不一样的书后,对 ...

  3. vs2008每次build都会重新编译链接 && 项目已经过期

    转自:http://blog.csdn.net/movezzzz/article/details/6816605 无外乎两种情况: 1.时间问题,所创建的文件的时间比如是:2011-09-22 09: ...

  4. pip源、搭建虚拟环境、git

    一.pip源 1.1 介绍 1.采用国内源,加速下载模块的速度2.常用pip源:-- 豆瓣:https://pypi.douban.com/simple-- 阿里:https://mirrors.al ...

  5. [原]Java工程打包注意事项

    注意事项(持续增加...): 如果Java工程中用到了注解,在用eclipse打jar包时需要注意一下,勾上“Add directory entries”,否则注解的类会注册不上

  6. Mockito 中文文档 ( 2.0.26 beta )

    Mockito 中文文档 ( 2.0.26 beta ) 由于缺乏校对,难免有谬误之处,如果发现任何语句不通顺.翻译错误,都可以在github中的项目提出issue.谢谢~ Mockito框架官方地址 ...

  7. Redis的人门以及使用

    1.Redis的安装 1.1centos下安装Redis 1.1.1 安装gcc 1.1.2 安装过程  图一 图三 2.Redis的启动 2.1 前端模式启动(不推荐) 截图 2.2 后端模式(推荐 ...

  8. Java基础 -3.4

    反码(~) 在计算机中,负数以其正值的补码形式表达. 什么叫补码呢?这得从原码,反码说起. 原码:一个整数,按照绝对值大小转换成的二进制数,称为原码. 比如 00000000 00000000 000 ...

  9. checkbox 选中获取值

    1:jsp 页面 从页面获取checkbox 的值传到后台 <div id="divCheckbox" class="hide"> <tr&g ...

  10. Fiddler抓包(基本使用方法、web+app端抓包、篡改数据、模拟低速)

    1.HTTP代理原理图 http服务器代理:既是web服务器,又是web客户端 接口vs端口: 接口:包含地址和端口 端口:类似于USB接口 地址:127.0.0.1,端口默认:8888        ...