Problem:
Problem H: Vending Machine

There has been marketing warfare among beverage vendors, and they have been working hard for in-
crease of their sales. The Kola-Coqua Company is one of the most successful vendors among those:
their impressive advertisements toward the world has brought the overwhelming market share of their
representative product called Koque.

This time, Kola-Coqua is focusing on vending machines. They think cusomters will be more pleasant as
the machines respond more quickly, so they have improved many parts of the machines.

In particular, they have developed a new device of change return. The new device can give one or more
kinds of coins at a time (in a single operation), although it can give only one coin for each kind at once.
For example, suppose there are 500-yen, 100-yen, 50-yen and 10-yen coins, change of 6540 yen can be
made by four operations of giving 500-yen and 10-yen coins and nine operations of giving 500-yen coins.
In conclusion, 6540 yen can be returned by thirteen operations. It is supposed that the new device allows
customers to make their purchase more quickly and so helps Kola-Coqua’s market share grow up.

However, the project leader says “No, it’s not optimal yet.” His suggesion is as follows: the real opti-
mization is to minimize the number of operations. For example, change of 6540 yen should be made
with ten of 500-yen coins, ten of 100-yen coins, ten of 50-yen coins, and four of 10-yen coins. This way,
6540 yen can be returned only with ten operations. This allows full speed-up in giving back change, even
though it sometimes results in a huge amount of coins.

Given which kinds of coins are available and how much change should be given back, you are to write a
program that calculates the minimum number of operations according to the above suggestion. You may
assume that there are enough amount of coins inside the vending machines.

Input

The input consists of multiple data sets. Each dataset is described by two lines. The first line contains N
(N ≤ 10) and M (M ≤ 100000) indicating the number of kinds of coins and the amount of change to be
made, respectively. The second line contains N integers representing the value of each kind of coin.

The input is terminated by a dataset of N = M = 0. This dataset must not be processed.

Output

For each dataset, output in a line the minimum number of operations needed to give back exactly the
specified amount of change.

Sample Input

6 330
1 5 10 50 100 500
7 127
1 2 4 8 16 32 64
2 10000
1000 2000
0 0

Output for the Sample Input

2
1
4

Solution in C++ (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
int a[15],n,w;bool dp[100020];
bool knapsack(vector<int>x){
	cout<<x.size()<<endl;
	for(int i=1;i<100020;i++)dp[i]=false;dp[0]=true;
	for(int i=0;i<(int)x.size();i++){
		for(int j=w-x[i];j>=0;j--){
			if(dp[j]==true)dp[j+x[i]]=true;
		}
	}
	return dp[w];
}
bool solve(int r){
	vector<int>J;
	for(int i=0;i<n;i++){
		int R=r;
		while(R>=1){
			J.push_back((R&-R)*a[i]);
			R-=(R&-R);
		}
	}
	return knapsack(J);
}
int main(){
	while(true){
		cin>>n>>w;if(n==0 && w==0)break;
		for(int i=0;i<n;i++)cin>>a[i];
		int L=1,R=w,M;
		while(true){
			M=(L+R)/2;
			bool p1=solve(M),p2=solve(M-1);
			if(p1==true && p2==false){cout<<M<<endl;break;}
			if(p2==true)R=M;
			if(p1==false)L=M;
		}
	}
	return 0;
}