title: Knapsack variation
body: <p>So I have an array of coupons, each with a price and the quantity of items that can be bought from it. I can only buy the given item quantity from a coupon, no more, no less. How to find the minimum cost to get the required number of items with coupons (and return -1 if not possible)?</p>
  
  <p>For example, on having 4 coupons: "Buy 3 at 10 dollars", "Buy 2 at 4 dollars",  "Buy 2  at  4 dollars"  and  "Buy  1  at  3  dollars",  and 4 items to buy, the minimum cost is 8 dollars.</p>
  
  <p>Knapsack works on finding maximums, but for minimum it'll just keep on not considering any coupon and come up with an answer of 0.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>int minimumCost(coupon_t coupons[], int numCoupons, int units) {
  
      if (units &lt;= 0 || numCoupons &lt;= 0)
          return 0;
  
      if (coupons[numCoupons-1].quantity &gt; units)
          return minimumCost(coupons, numCoupons-1, units);
  
      coupon_t coupon = coupons[numCoupons-1];
      return min(coupon.price + minimumCost(coupons, numCoupons-1, units-coupon.quantity),
              minimumCost(coupons, numCoupons-1, units));
  
  }
  </code></pre>