UVa 11450 - Wedding shopping

11450 - Wedding shopping

 * http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=2445

Summary
Given a money M (<= 200) and a list of garments C (<= 20). Each garment has K (<= 20) models. You want to buy one model for each garment and you want to spend your money as much as possible.

Explanation
This is a typical Dynamic Programming problem. Let dp(m,c) be the maximum money you can spend when you have m money and you want to buy one model for each garment from garment 0 to garment c-1. If it's not possible to buy one model for each garment then the value of dp(m,c) is -2 (or any other special value).

Then dp(m,c) can be computed recursively:

dp(m,c) =       -2,   if m is negative c,  if c is zero max(spending),  otherwise

Where spending is the maximum value of dp(m-ci, c-1) + ci for all the prices of model ci of garment c-1 and dp(m-ci, c-1) is not -2.

The total complexity is O(M*C*max(K)) which is around 200*20*20 = 80,000 operations per test case.

Implementations
Dynamic Programming can be implemented in a top-down manner or a bottom-up manner. Below is the top-down version (recursion model):

Below is the bottom-up version: