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. springmvc项目的搭建

    springmvc替代servlet的工作 Servlet - Springmvc        jsp ->Servlet (Springmvc)->Jsp springmvc配置文件 ...

  2. mqtt开源服务器 EMQX ,客户端MQTTX5.0,使用指南

    服务器 EMQX 官网: https://docs.emqx.io/broker/v3/cn/getstarted.html#mqtt-clients 一.安装启动 # 各平台下载https://ww ...

  3. Laravel Vuejs 实战:开发知乎 (1)项目环境配置和用户表设计

    1.使用laragon新建laravel项目 zhihu 2.配置env文件的database设置 DB_DATABASE=zhihu 3.分析users表需要的字段 4.修改数据库迁移文件: cla ...

  4. 【PAT甲级】1045 Favorite Color Stripe (30 分)(DP)

    题意: 输入一个正整数N(<=200),代表颜色总数,接下来输入一个正整数M(<=200),代表喜爱的颜色数量,接着输入M个正整数表示喜爱颜色的编号(同一颜色不会出现两次),接下来输入一个 ...

  5. pymongo 用户密码连接

    # db mongodbdb_mongo_attr = { 'host': '*', 'port': 27, 'database':'tease', 'username':'*v', 'passwor ...

  6. kali apt update 错误——下列签名无效: EXPKEYSIG ED444FF07D8D0BF6 Kali Linux Repository

    这是因为key过期了 wget -q -O - https://archive.kali.org/archive-key.asc | apt-key add apt update

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:段落中超出屏幕部分不换行

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  8. Python学习第十七课——组合

    组合1 #组合 1 class Hand: pass class Foot: pass class Trunk: pass class Head: pass class Person: def __i ...

  9. idea右键新建选项没有类和包的创建方式

    Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure

  10. Session 'app': Error Installing APKs app 在手机或虚拟机上调试报错

    解决方案: build --clean project