Bookshelf 2

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

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<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std ;
int dp[] ;
int B , n ;
int a[] ;
int cmp (int x , int y ) {
return x > y ;
}
int main () {
// freopen ("a.txt" , "r" , stdin );
while (scanf ("%d%d" , &n , &B ) != EOF ) {
for (int i = ; i < n ; i++ ) {
scanf ("%d" , &a[i] ) ;
}
sort ( a , a + n , cmp ) ;
for (int i = ; i <= B + ; i++ ) {
dp[i] = ;
}
dp[] = ;
for (int i = ; i < n ; i++ ) {
for (int j = B + ; j >= a[i] ; j-- ) {
if ( dp[j - a[i]] ) {
dp[j] = ;
// printf("%d " , j) ;
}
}
// printf ("\n") ;
}
// printf("\n") ;
for (int j = B ; j <= B + ; j++ ) {
if ( dp[j] ) {
printf ("%d\n" , j - B ) ;
break ;
}
}
}
return ;
}

01背包

Bookshelf 2的更多相关文章

  1. bookshelf

    nodejs mysql ORM 比node-mysql好用多了. bookshelf 支持restful功能,用到的时候研究下:https://www.sitepoint.com/getting-s ...

  2. POJ3628 Bookshelf 2(01背包+dfs)

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 Descr ...

  3. POJ 3628 Bookshelf 2(01背包)

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

  4. POJ3628:Bookshelf 2【01背包】

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

  5. Node的关系型数据库ORM库:bookshelf

    NodeJs 关系数据库ORM库:Bookshelf.js bookshelf.js是基于knex的一个关系型数据库的ORM库.简单易用,内置了Promise的支持.这里主要罗列一些使用的例子,例子就 ...

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

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

  7. HOJ-2056 Bookshelf(线性动态规划)

    L is a rather sluttish guy. He almost never clean up his surroundings or regulate his personal goods ...

  8. poj_3628 Bookshelf 2

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

  9. 书架 bookshelf

    书架 bookshelf 题目描述 当Farmer John闲下来的时候,他喜欢坐下来读一本好书. 多年来,他已经收集了N本书 (1 <= N <= 100,000). 他想要建立一个多层 ...

随机推荐

  1. Provider 错误 '80004005' 未指定的错误 /conn.asp,行 23

    网站本来正常,由于网站修改了title标签的内容,想要百度快照快点更新. 就自己提交快照.在下面网站: http://zhanzhang.baidu.com/sitesubmit/index 提交UR ...

  2. 在ubuntu server上安装沸腾时刻环境

    1. 安装php5.6 http://phpave.com/upgrade-to-php-56-on-ubuntu-1404-lts/ 按照这篇文章的顺序来做,可以安装最新5.6版本php 安装好了以 ...

  3. apply与call

    看这个apply真正应用.bind这是一个绑定时间的函数 var bind=function(object,type,fn){ if(object.attachEvent){//IE浏览器 objec ...

  4. WebClient和HttpClient, 以及webapi上传图片

    httppost请求. applicationkey/x-www-form-urlencoded请求: Email=321a&Name=kkfewwebapi里面, 如果用实体, 能接受到. ...

  5. iOS边练边学--iOS中的(ARC下)单粒模式(GCD实现)

    一.ARC中实现单粒模式 在.m 保留一个全局的static的实例 static id _名称; 重写allocWithZone:方法,在这里创建唯一的实例 提供一个类方法让外界访问唯一的实例 实现c ...

  6. hdu1890 伸展树(区间反转)

    对于大神来说这题是水题.我搞这题花了快2天. 伸展树的优点有什么,就是树不管你怎么旋转序列是不会改变得,并且你要使区间反转,只要把第k大的点转到根结点,那么它的左子树就是要交换的区间[l,r),然后交 ...

  7. UVA5874 Social Holidaying 二分匹配

    二分匹配简单题,看懂题意,建图比较重要. #include<stdio.h> #include<string.h> #define maxn 1100 int map[maxn ...

  8. Activiti 学习资料收集

    Activiti工作流引擎使用 http://www.open-open.com/lib/view/open1350460225367.html Activiti初学者教程 http://blog.c ...

  9. 【CodeForces 624C】Graph and String

    题 题意 n个表示abc三个字符的点,所有a和b是相连的,所有b和c是相连的,所有相同的是相连的,现在给你n个点和他们之间的m条边,判断是否存在这样的字符串,存在则给出一个符合条件的. 分析 我的做法 ...

  10. hive 使用笔记(partition; HDFS乱码)

    6.  insert 语句 1) 因为目标表有partition, 所以刚开始我使用的语句是 insert overwrite table sa_r_item_sales_day_week_month ...