- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Reducing Dishes in C++

Suppose there is a chef. And he has collected data on the satisfaction level of his n dishes. The Chef can cook any dish in 1 unit of time. Like-time coefficient of a dish is actually the time taken

to cook that dish including previous dishes multiplied by its satisfaction level So time[i]*satisfaction[i].

We have to find the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation. Dishes can be ready in any order and the chef can discard some dishes to get this maximum value.

So, if the input is like [-1,-7,0,6,-7], then the output will be 17, After removing second and last dish, the maximum total like-time coefficient will be -1*1 + 0*2 + 6*3 = 17.

To solve this, we will follow these steps −

Define an array dp of size: 505 x 505.

Define a function solve(), this will take idx, time, an array v,

if idx is same as size of v, then −

return 0

if dp[idx, time] is not equal to -1, then −

return dp[idx, time]

ret := -inf

ret := maximum of solve(idx + 1, time, v) and v[idx] * time + solve(idx + 1, time + 1, v)

dp[idx, time] := ret

return ret

From the main method do the following −

Fill this -1 with dp

sort the array v

return solve(0, 1, v)

Let us see the following implementation to get better understanding −

## Example

#include <bits/stdc++.h> using namespace std; class Solution { public: int dp[505][505]; int solve(int idx, int time, vector <int>& v){ if(idx == v.size()) return 0; if(dp[idx][time] != -1) return dp[idx][time]; int ret = INT_MIN; ret = max(solve(idx + 1, time, v), v[idx] * time + solve(idx + 1, time + 1, v)); return dp[idx][time] = ret; } int maxSatisfaction(vector<int>& v) { memset(dp, -1, sizeof(dp)); sort(v.begin(), v.end()); return solve(0, 1, v); } }; main(){ Solution ob; vector<int> v = {-1,-7,0,6,-7}; cout << (ob.maxSatisfaction(v)); }

## Input

{-1,-7,0,6,-7}

## Output

17

- Related Questions & Answers
- Natural remedies for reducing skin pigmentation.
- Reducing array elements to all odds in JavaScript
- Program to find winner of number reducing game in Python
- How to use pre-cooked rice to make other dishes?
- Program to find winner of a rower reducing game in Python
- Explain the methods of reducing the number of microinstructions in computer architecture?
- Which is better, stop smoking at once or slowly reducing and finally stop?
- What are the easy dishes that can be made with vegetables available in winters - cauliflower, peas, carrot?
- What are the recipes of some dishes for those who fast during Navratri and Karwa Chauth?
- What is the reason behind using the number 65 to label dishes such as Panner 65, Chicken 65, etc?
- Built-in Exceptions in Java
- Built-in Exceptions in C#
- In-service register in 8259
- NavigationView in ActionBar in Android
- JSX in depth in React.js