<p>A chef has collected data on the <code>satisfaction</code> level of his <code>n</code> dishes. Chef can cook any dish in 1 unit of time.</p>

<p><strong>Like-time coefficient</strong> of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. <code>time[i] * satisfaction[i]</code>.</p>

<p>Return the maximum sum of <strong>like-time coefficient </strong>that the chef can obtain after preparing some amount of dishes.</p>

<p>Dishes can be prepared in <strong>any </strong>order and the chef can discard some dishes to get this maximum value.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> satisfaction = [-1,-8,0,5,-9]
<strong>Output:</strong> 14
<strong>Explanation:</strong> After Removing the second and last dish, the maximum total <strong>like-time coefficient</strong> will be equal to (-1*1 + 0*2 + 5*3 = 14).
Each dish is prepared in one unit of time.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> satisfaction = [4,3,2]
<strong>Output:</strong> 20
<strong>Explanation:</strong> Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> satisfaction = [-1,-4,-5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> People do not like the dishes. No dish is prepared.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == satisfaction.length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>-1000 &lt;= satisfaction[i] &lt;= 1000</code></li>
</ul>

auto speedUP = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return 'c';
}();
class Solution {
public:
    //Recursion
    // int maxSatifactionUsingRecursion(vector<int> &satis, int index, int time){
    //     if(index >= satis.size()) return 0;

    //     int includeLikeTime = time * satis[index] + maxSatifactionUsingRecursion(satis, index + 1, time + 1);
    //     int excludeLikeTime = maxSatifactionUsingRecursion(satis, index + 1, time);

    //     return max(includeLikeTime, excludeLikeTime);
    // }

    //Recursion + Memoization
    // int maxSatisfactionUsingMemoization(vector<int> &satis, int index, int time, vector<vector<int>> &dp){
    //     if(index >= satis.size()) return 0;

    //     if(dp[index][time] != -1) return dp[index][time];

    //     int includeLikeTime = time * satis[index] + maxSatisfactionUsingMemoization(satis, index + 1, time + 1, dp);
    //     int excludeLikeTime = maxSatisfactionUsingMemoization(satis, index + 1, time, dp);

    //     dp[index][time] = max(includeLikeTime, excludeLikeTime);
    //     return dp[index][time];
    // }

    //Tabular Method
    // int maxSatisfactionUsingTabular(vector<int>& satis){
    //     int n = satis.size();
    //     vector<vector<int>> dp(n + 1, vector<int>(n + 2, 0));

    //     for(int index = n - 1 ; index >= 0 ; index--){
    //         for(int time = n ; time >= 1 ; time--){
    //             int includeLikeTime = time * satis[index] + dp[index + 1][time + 1];
    //             int excludeLikeTime = dp[index + 1][time];

    //             dp[index][time] = max(includeLikeTime, excludeLikeTime);
    //         }
    //     }
    //     return dp[0][1];
    // }

    //Tabular Method + Space optimized
    int maxSatisfactionUsingTabularSO(vector<int>& satis){
        int n = satis.size();
        vector<int> curr(n + 2, 0);
        vector<int> next(n + 2, 0);

        for(int index = n - 1 ; index >= 0 ; index--){
            for(int time = n ; time >= 1 ; time--){
                int includeLikeTime = time * satis[index] + next[time + 1];
                int excludeLikeTime = next[time];

                curr[time] = max(includeLikeTime, excludeLikeTime);
            }
            //Shifting
            next = curr;
        }
        return next[1];
    }

    int maxSatisfaction(vector<int>& satisfaction) {
        sort(satisfaction.begin(), satisfaction.end());
        // return maxSatifactionUsingRecursion(satisfaction, 0, 1);

        // int n = satisfaction.size();
        // vector<vector<int>> dp(n + 1, vector<int>(n + 1, -1));
        // return maxSatisfactionUsingMemoization(satisfaction, 0, 1, dp);

        // return maxSatisfactionUsingTabular(satisfaction);

        return maxSatisfactionUsingTabularSO(satisfaction);
    }
};
11200