UVa 11517 – Exact Change

Idea:
1. This is a classical coin change problem. here dp array represent the minimum coin need to make an amount. example – dp[100] means we need at least dp[100] coins to make 100 cents.

2. At first calculate the dp[] table as described. then start a linear search from money to forward for finding first amount of money which value is not infinity as we set before dp calculation and when we find a such value that value is the payable money and dp value of that money is the minimum number of coins.

```
/*
MMP&quot;&quot;MM&quot;&quot;YMM   db      `7MN.   `7MF'`7MMM.     ,MMF' .g8&quot;&quot;8q.`YMM'   `MM'
P'   MM   `7  ;MM:       MMN.    M    MMMb    dPMM .dP'    `YM.VMA   ,V
MM      ,V^MM.      M YMb   M    M YM   ,M MM dM'      `MM VMA ,V
MM     ,M  `MM      M  `MN. M    M  Mb  M' MM MM        MM  VMMP
MM     AbmmmqMA     M   `MM.M    M  YM.P'  MM MM.      ,MP   MM
MM    A'     VML    M     YMM    M  `YM'   MM `Mb.    ,dP'   MM
.JMML..AMA.   .AMMA..JML.    YM  .JML. `'  .JMML. `&quot;bmmd&quot;'   .JMML.
*/

#include &lt;bits/stdc++.h&gt;

#define pii pair &lt;int,int&gt;
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1&lt;&lt;29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define CIN ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a) (int)a.size()
#define si(a) scanf(&quot;%d&quot;,&amp;a)
#define sii(a,b) scanf(&quot;%d%d&quot;,&amp;a,&amp;b)
#define siii(a,b,c) scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n) for(int i=0;i&lt;n;i++)
#define REP(i,a,b) for(int i=a;i&lt;b;i++)
#define TEST_CASE(t) for(int z=1;z&lt;=t;z++)
#define PRINT_CASE printf(&quot;Case %d: &quot;,z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

int dp[20100];

int coin[110];

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t;
si(t);
TEST_CASE(t)
{
int money;
si(money);
int n;
si(n);
loop(i,n)
{
si(coin[i]);
}
//sort(coin,coin+n,test);
ms(dp,inf);
dp[0]=0;
for(int i=0; i&lt;n; i++)
{
for(int j=money; j&gt;-1; j--)
{
dp[j+coin[i]]=min(dp[j]+1,dp[j+coin[i]]);
}
}

for(int i=money; i&lt;=30100; i++)
{
if(dp[i]&lt;10000)
{
pf(&quot;%d %d\n&quot;,i,dp[i]);
break;
}
}
}
return 0;
}

```

Another Solution using same idea. this one is from revise ðŸ˜› .

```

/*
+-+ +-+ +-+
|R| |.| |S|
+-+ +-+ +-+
*/

#include &lt;bits/stdc++.h&gt;

#define pii             pair &lt;int,int&gt;
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI              vector &lt;int&gt;
#define DBG             pf(&quot;Hi\n&quot;)
#define MOD             100007
#define MAX             10000
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf(&quot;%d&quot;,&amp;a)
#define sfl(a)          scanf(&quot;%lld&quot;,&amp;a)
#define sff(a,b)        scanf(&quot;%d %d&quot;,&amp;a,&amp;b)
#define sffl(a,b)       scanf(&quot;%lld %lld&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)     scanf(&quot;%d %d %d&quot;,&amp;a,&amp;b,&amp;c)
#define sfffl(a,b,c)    scanf(&quot;%lld %lld %lld&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n)       for(int i=0;i&lt;n;i++)
#define REP(i,a,b)      for(int i=a;i&lt;b;i++)
#define TEST_CASE(t)    for(int z=1;z&lt;=t;z++)
#define PRINT_CASE      printf(&quot;Case %d: &quot;,z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define ull             unsigned long long

using namespace std;

/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*------------------------------------------------*/

//int Set(int N,int pos){return N=N | (1&lt;&lt;pos);}
//int reset(int N,int pos){return N= N &amp; ~(1&lt;&lt;pos);}
//bool check(int N,int pos){return (bool)(N &amp; (1&lt;&lt;pos));}
/*------------------------------------------------*/

int dp[1000007];
int coin[110];

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);

int t;
sf(t);
TEST_CASE(t)
{
ms(dp,1000000);
int money,n;
sf(money);
sf(n);
loop(i,n) sf(coin[i]);
dp[0]=0;
for(int i=0; i&lt;n; i++)
{
for(int j=money; j&gt;=0; j--)
{
dp[j+coin[i]]=min(dp[j]+1,dp[j+coin[i]]);
}
}

for(int i=money;; i++)
{
if(dp[i]&lt;1000000 &amp;&amp; dp[i])
{
pf(&quot;%d %d\n&quot;,i,dp[i]);
break;
}
}

}

return 0;
}

```