# UVa 10819 – Trouble of 13-Dots

Solution idea:

This is a classical knapsack problem. Set a correct base case is the most challenging part of this problem. If we read and think carefully about this problem we can see that if the expense exceeds 2000\$ then 200\$ will be refunded so it will be strictly greater than 2000. Now we can consider 3 things as base case. Those are here-

Let W is expense at current time and C = capacity or Total money.

•  When W > Capacity and W-Capacity > 200 then we can’t buy anything else and the current product so we will return a large negative value to cancel this solution.
• When Capacity < 1800 and Capacity < W then it's clear we can't buy anything and current product so we will cancel this.
•  And Last thing is when we are taking the last product then if W > Capacity and W <= 2000 then we can take these so we will return a Zero. Otherwise we will cancel this one by returning a large negative value.
```
/*
+-+ +-+ +-+
|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 inf             1000000
#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 capacity, n;

int z;

int dp[110][10300];
int weight[110],profit[110];

int knapsack(int idx, int w)
{
if(w&gt;capacity &amp;&amp; w-capacity&gt;200) return -100000;
if(capacity&lt;1800 &amp;&amp; w&gt;capacity) return -100000;

if(idx==n)
{
if(w&gt;capacity &amp;&amp; w&lt;=2000) return -100000;
return 0;
}

if(dp[idx][w]!=-1) return dp[idx][w];

return dp[idx][w]=max(profit[idx]+knapsack(idx+1,w+weight[idx]),knapsack(idx+1,w));
}

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
CIN;
while(cin&gt;&gt;capacity&gt;&gt;n)
{
z++;
loop(i,n) cin&gt;&gt;weight[i]&gt;&gt;profit[i];

ms(dp,-1);
cout&lt;&lt;knapsack(0,0)&lt;&lt;endl;

}

return 0;
}

```

Here is another solution 🙂

```
/*
+-+ +-+ +-+
|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 inf             1000000
#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 capacity, n;

pii input[110];
int z;
int dp[110][10210];
int cases[110][10210];
bool test=0;

int knapsack(int idx, int w)
{
if(w==capacity)
{
return 0;
}
if(idx&gt;=n) return 0;
int &amp;ret=dp[idx][w];
int &amp;cas=cases[idx][w];

if(cas==z) return ret;

cas=z;

int p=0,q=0;

if(w+input[idx].ff&lt;=capacity)
p=input[idx].ss+knapsack(idx+1,w+input[idx].ff);
else if(w+input[idx].ff&gt;capacity)
{
if((w+input[idx].ff&gt;2000) &amp;&amp; (w+input[idx].ff&lt;=capacity+200) &amp;&amp; !test)
{
test=1;
capacity+=200;
p=input[idx].ss+knapsack(idx+1,w+input[idx].ff);
test=0;
capacity-=200;
}
}
q=knapsack(idx+1,w);

return ret=max(p,q);
}

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

while(cin&gt;&gt;capacity&gt;&gt;n)
{
z++;

loop(i,n) cin&gt;&gt;input[i].ff&gt;&gt;input[i].ss;
test=0;
sort(input,input+n);
cout&lt;&lt;knapsack(0,0)&lt;&lt;endl;

}
return 0;
}

```