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

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 2000010
int n,m,a[maxn],dp[maxn];
int main(){while(~scanf("%d%d",&n,&m)){memset(dp,0,sizeof(dp));int sum=0;for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];for(int i=1;i<=n;i++){for(int j=sum;j>=a[i];j--) dp[j]=max(dp[j],dp[j-a[i]]+a[i]);}for(int i=1;i<=sum;i++){if(dp[i]>=m){printf("%d\n",dp[i]-m);break;}}}return 0;
}

POJ - 3628 Bookshelf 2相关推荐

  1. POJ 3628 Bookshelf 2

    POJ 3628 Bookshelf 2:http://poj.org/problem?id=3628 题意:有个书架,高度为B,现在FJ有N个奶牛,每个奶牛有个高度hi,现在将奶牛堆起来,使得堆起来 ...

  2. POJ 3628 Bookshelf 2 (01背包)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Description Farmer John recently bought anothe ...

  3. 【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)

    题干: Farmer John recently bought another bookshelf for the cow library, but the shelf is getting fill ...

  4. POJ 3627 Bookshelf 贪心 水~

    最近学业上堕落成渣了.得开始好好学习了. 还有呀,相家了,好久没回去啦~ 还有和那谁谁谁... 嗯,不能发表悲观言论.说好的. 如果这么点坎坷都过不去的话,那么这情感也太脆弱. ----------- ...

  5. POJ 3268 Bookshelf 2 动态规划法题解

    Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...

  6. POJ 3627 Bookshelf (贪心)

    Description Farmer John recently bought a bookshelf for cow library, but the shelf is getting filled ...

  7. POJ 3627 Bookshelf

    Bookshelf Time Limit: 1000MS Memory Limit: 65536K Description Farmer John recently bought a bookshel ...

  8. poj 3682 Bookshelf 2——背包问题(dfs)

    Bookshelf 2 Description Farmer John recently bought another bookshelf for the cow library, but the s ...

  9. DP分类题目 转载 《志当存高远》大神的 没有冒犯的意思 只是拿过来学习的

    A.各种背包,就根据背包九讲的内容来做吧! --不会背包的可以边看背包九讲边练习 =========================================================== ...

最新文章

  1. 鼠標滑過表格整行改變背景色
  2. logback Appender详解
  3. Weblogic EJB 学习笔记(2)精
  4. apache大师+伪静态_Apache开启伪静态示例
  5. 【Matlab】滤波器常用命令
  6. 这个韩国女星在节目里吃了“巨型蛤蜊” 可能要坐牢5年了...
  7. Python元组,列表,解构和循环
  8. IIS服务器证书的导入 IIS 5、IIS 6、IIS 7
  9. Linux音频驱动-声音采集过程
  10. 单词数 HDU - 2072 (字典树)
  11. 数字开头的字符可能有冒号可能没有,以分号分隔成数组
  12. 基于Matlab的车牌识别系统 GUI界面+模板库
  13. 建模matlab仿真视频教程,Simulink建模与仿真视频教程
  14. 怎么把HTML转换成swf用迅雷,什么工具能把MP4格式转换成SWF并不降低画质
  15. 如何放大图片而不影响清晰度?
  16. 关于qt 开发的灵异事件
  17. python sample函数
  18. 2022年1月语音合成(TTS)和语音识别(ASR)论文月报
  19. 解决U盘在Linux系统只读的问题:[sdb] Write cache: disabled, read cache: enabled, doesn‘t support DPO or FUA
  20. iredmail可以发邮件但是收不到邮件问题汇总

热门文章

  1. 海伦天气预报软件测试,海伦天气预报15天
  2. php判断是为字符串类型,PHP:检查变量是否为字符串类型并且不是空字符串?
  3. 告别极寒,科学家突破将量子计算机运行最低温提了15倍
  4. 新媒体运营:如何做好软文写作?
  5. 全球及中国发酵酸行业发展动态及投资前景预测报告(新版)2022-2027
  6. 用python写一个贪吃蛇小游戏
  7. 【高性能计算背景】《并行计算教程简介》翻译 - 中文 - 4 / 4
  8. 【知识梳理】平面几何
  9. mysql+mdl+解决办法_Mysql DDL出现长时间等待MDL问题分析
  10. 全新出炉:7 月编程语言最新排行榜