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. 常见排序的Java实现

    插入排序 1.原理:在有序数组中从后向前扫描找到要插入元素的位置,将元素插入进去. 2.步骤:插入元素和依次和前一个元素进行比较,若比前一个元素小就交换位置,否则结束循环. 3.代码: package ...

  2. 吴裕雄--天生自然Numpy库学习笔记:NumPy Matplotlib

    Matplotlib 是 Python 的绘图库. 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案. 它也可以和图形工具包一起使用,如 PyQt 和 wxPython. W ...

  3. 吴裕雄 python 神经网络——TensorFlow pb文件保存方法

    import tensorflow as tf from tensorflow.python.framework import graph_util v1 = tf.Variable(tf.const ...

  4. Vue父组件主动获取子组件的数据和方法

    Vue父组件主动获取子组件的数据和方法 https://www.jianshu.com/p/bf88fc809131

  5. 【Html 页面布局】

    float:left方式布局 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...

  6. FastDFS文件上传和下载流程

    文件上传流程 客户端上传文件后存储服务器将文件 ID 返回给客户端,此文件 ID 用于以后访问该文件的索引信息.文件索引信息包括:组名,虚拟磁盘路径,数据两级目录,文件名.  组名:文件上传后所在的 ...

  7. office365激活码序列号密钥:

    亲测可用: NGCM9-FWB6X-9WKYC-GRWVD-63B3P  

  8. jmeter download historyList

    https://archive.apache.org/dist/jmeter/binaries/ 反馈,问题和评论应发送到Apache JMeter Users 邮件列表. 有关更多信息, 请访问Ap ...

  9. Dart语言学习(十四) Dart泛型

    什么是泛型? 通俗理解:泛型就是解决 类 接口 方法的复用性.以及对不特定数据类型的支持(类型校验) 如下代码,只能返回string类型的数据 String getData(String value) ...

  10. 前端学习 之 JavaScript DOM 与 BOM

    一. DOM介绍 1. 什么是DOM? DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构. 目的其实就是为了能让js操作html元素而制定的一个规范. DOM就 ...