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: Accepted)

/*
  AOJ 2138
  Title:Vending Machine
  @kankichi573
*/
#include <stdio.h>
#include <string.h>

#define LIMIT 100000
int n,m;
int denomi[10];
int table[LIMIT+1];
int gr;

int min(int x,int y)
{
  if(x<y)
    return(x);
  else
    return(y);

}
int max(int x,int y)
{
  if(x>y)
    return(x);
  else
    return(y);

}

void dump()
{
  int i;
  for(i=1;i<=m;i++)
    if(table[i]<LIMIT)
      printf("%d:%d,",i,table[i]);
  printf("\n");
}

greed(int kin[],int n,int m)
{
  int highest,min_,i,count;

  highest=kin[0];
  //printf("gr=%d [%d][%d]\n",m,highest,n);
  if(m==0)
    return(0);
  if(m < highest && n>1)
    return(greed(kin+1,n-1,m));
  if(n==1)
    {
      if((m%highest)==0)
	return(m/highest);
      else
	return(LIMIT);
    }
  min_=LIMIT;
  for(i=m/highest;i>=0;i--)
    {
      //printf("i=%d\n",i);
      count=max(i,greed(kin+1,n-1,m-i*highest));
      if(min_>count)
	{
	  min_=count;
	  break;
	}
    }
  return(min_);
}


void solve()
{
  int i,j,k,kin,wk;

  for(i=1;i<=m;i++)
    table[i]=LIMIT;

  kin=denomi[0];
  for(i=0;i*kin<=m;i++)
    table[i*kin]=i;
  //dump();
  for(i=1;i<n;i++)
    {
      kin=denomi[i];
      for(j=m;j>=0;j--)
	if(table[j]!=LIMIT)
	  {
	    for(k=1;j+kin*k<=m && k<=gr;k++)
	      {
		if(table[j+kin*k]>(wk=max(k,table[j])))
		  table[j+kin*k] = wk;
		else
		  break;
	      }
	  }
      //dump();
    }
    
}


int compare(const void *a, const void *b)
{
    return *(int*)b - *(int*)a;
}


main()
{
  int i,ret;
  while(scanf("%d %d",&n,&m) && (n||m))
    {
      for(i=0;i<n;i++)
	scanf("%d",&denomi[i]);
      qsort(denomi,n,sizeof(int),compare);
      gr=greed(denomi,n,m);
      //printf("greed=%d\n",gr);
      solve();
      printf("%d\n",table[m]);
    }
  return(0);
}