Problem K: [usaco 2007 Dec] super bookshelf
The Description
Farmer John has recently acquired a huge library bookshelf for the cows, although it is so big, but it is almost instantly be filled with all kinds of books. Now, only the top of the bookshelf also left little space. All N (1 <= N <= 20) have a certain cows height H_i (1 <= H_i <= 1,000,000 - cows good high> _ <). All set for the cows height and S. Height of shelves is B, and to ensure that 1 <= B <= S. In order to reach the other end is even higher than the highest of the top shelves of dairy cows, the cows had to perform acrobatics like a general, one standing on the back of another head, folded into a "cow tower." Of course, the height of the tower, the tower is the sum of all the cow's height. In order to put things on top of the shelves, all the cows and height must not be less than the height of the shelves. The higher the stack would tower more unstable, so the cows are hoping to find a program that fold out of the tower at a height of not less than the height of the shelves under the circumstances, the height as small as possible. You can also guess your task: Write a program that calculates cows quartet built tower in the case meet the requirements, a minimum number of shelves than high.
Input
- Line 1: 2 with a space-separated integers: N and B
- 2 ... N + 1 of the line: first line i + 1 is an integer 1: H_i
Output
- Line 1: Output a non-negative integer, i.e. cows folded shelf column is higher than the minimum height
Sample Input
5 16 3 1 3 5 6
Sample Output
17 // we use cows 1,3,4,5 quartet built tower, their total height is 3 + 3 + 5 + 6 =
17. Any program can not stack the height of the tower 16, so the answer is 1.
HINT
#include<bits/stdc++.h>
using namespace std;
int n,b;
int high=100000,h[1000000];
void dfs(int u,int v){//u用来处理奶牛数量,v表示奶牛的升高
if(v>=b){
high=min(high,v);
}
if(u>n)return;//如果只有一头羊大于书架高度,什么都不用做。
dfs(u+1,v);//让他增加羊的个数才能
dfs(u+1,v+h[u]);//然后,我们加上增加羊的数量
}
int main(){
cin>>n>>b;
for(int i=1;i<=n;i++){
cin>>h[i];
}
dfs(1,0);
cout<<high-b;
return 0;
}