Problem Description

As it is Tushar’s Birthday on March 1st, he decided to throw a party to all his friends at TGI Fridays in Pune.Given are the eating capacity of each friend, filling capacity of each dish and cost of each dish. A friend is satisfied if the sum of the filling capacity of dishes he ate is equal to his capacity. Find the minimum cost such that all of Tushar’s friends are satisfied (reached their eating capacity).

NOTE:

  1. Each dish is supposed to be eaten by only one person. Sharing is not allowed.
  2. Each friend can take any dish unlimited number of times.
  3. There always exists a dish with filling capacity 1 so that a solution always exists.

Input Format

First number denotes the size of the array

Friends : List of integers denoting eating capacity of friends separated by space. Capacity: List of integers denoting filling capacity of each type of dish. Cost : List of integers denoting cost of each type of dish.

**Constraints:**1 <= Capacity of friend <= 10001 <= No. of friends <= 10001 <= No. of dishes <= 1000

Example:

`Input: 2 4 6 2 1 3 2 5 3

Output: 14

Explanation: First friend will take 1st and 2nd dish, second friend will take 2nd dish twice. Thus, total cost = 5 + 3 + (3*2) = 14`

Problem Link

Tushar's Birthday Party - InterviewBit

Code

int Solution::solve(const vector<int> &A, const vector<int> &B, const vector<int> &C) {
    
    int wt = *max_element(A.begin(),A.end());//max capacity of the KS will be the max
                                             // eating capacity of a friend
    int n = B.size();
    
    vector<vector<int>> dp(n+1,vector<int>(wt+1,0));
    
    for(int i=1;i<=wt;i++)
    dp[0][i] = INT_MAX;
    
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=wt;j++)
        {
            if(j>=B[i-1]&&dp[i][j-B[i-1]]!=INT_MAX)
            dp[i][j] = min(dp[i-1][j],C[i-1]+dp[i][j-B[i-1]]);
            
            else
            dp[i][j] = dp[i-1][j];
            
        }
    }
    int res = 0;
    
    for(int ele:A) //finding total cost for individual friend's capacity
    res+=dp[n][ele];
    
    return res;
    
    
}