You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.

However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.

Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and age of the ith player, respectively, return the highest overall score of all possible basketball teams.

&nbsp;
Example 1:

Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
Explanation:&nbsp;You can choose all the players.


Example 2:

Input: scores = [4,5,6,5], ages = [2,1,2,1]
Output: 16
Explanation:&nbsp;It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.


Example 3:

Input: scores = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Explanation:&nbsp;It is best to choose the first 3 players. 


&nbsp;
Constraints:


	1 &lt;= scores.length, ages.length &lt;= 1000
	scores.length == ages.length
	1 &lt;= scores[i] &lt;= 106
	1 &lt;= ages[i] &lt;= 1000


class Solution(object):
    def bestTeamScore(self, scores, ages):
        """
        :type scores: List[int]
        :type ages: List[int]
        :rtype: int
        """
        l = len(scores)
        mapped = [[ages[i], scores[i]] for i in range(l)]
        mapped = sorted(mapped, key = lambda x : (x[0], x[1]))
        dp = [i[1] for i in mapped]

        for i in range(l):
            for j in range(0, i):
                if mapped[j][1] <= mapped[i][1]:
                    dp[i] = max(dp[i], mapped[i][1] + dp[j])
                elif mapped[i][0] == mapped[j][0]:
                    dp[i] = max(dp[i], mapped[i][1] + dp[j])

        return max(dp)
class Solution {
    public int bestTeamScore(int[] scores, int[] ages) {
        int n = scores.length;
        // combine the arrays for ease in processing
        int[][] combined = new int[n][2];
        for (int i = 0; i < n; i++) {
            combined[i][0] = ages[i];
            combined[i][1] = scores[i];
        }
        
        // sort the arrays by age and scores
        Arrays.sort(combined, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
        
		// find max answer between all possible teams
        int ans = 0;
        for (int i = 0; i < n; i++)
            ans = Math.max(ans, recurse(combined, i));
        
        return ans;
    }
    
    private int recurse(int[][] combined, int cur) {
        // base case
        if (cur == -1)
            return 0;
        
        // try teaming player with others if no conflict found
        int ans = 0;
        for (int prev = 0; prev < cur; prev++) {
            if (combined[cur][0] == combined[prev][0] || combined[cur][1] >= combined[prev][1])
                ans = Math.max(ans, recurse(combined, prev));
        }
        
        // add score of current player
        ans += combined[cur][1];
        return ans;
    }
}
class Solution {
public:
    int dp[1005][1005];
    int bestTeamScore(vector<int>& scores, vector<int>& ages) {
        vector<vector<int>>grp;
        for(int i=0;i<scores.size();i++)
        {
            grp.push_back({scores[i],ages[i]});
        }
        sort(grp.begin(),grp.end());     
        memset(dp,-1,sizeof(dp));
        return recur(grp,0,ages.size(),0);
    }
    
    int recur(vector<vector<int>>&grp , int i , int n , int maxiAge)
    {
        if(i==n)
            return 0;
        if(dp[i][maxiAge]!=-1)
            return dp[i][maxiAge];
        // score is already greater than previous socre we need to check age 
        // if current age is  greater than previous maxiAge then two choices
        if(grp[i][1]>=maxiAge)
        {
            return dp[i][maxiAge] = max(grp[i][0]+recur(grp,i+1,n,grp[i][1]),recur(grp,i+1,n,maxiAge));
        }
        return dp[i][maxiAge] = recur(grp,i+1,n,maxiAge);
    }
};
var bestTeamScore = function(scores, ages) {
    const players = scores.map((score, index) => ({ score, age: ages[index] }))
        .sort((a,b) => a.score === b.score ? a.age - b.age : a.score - b.score);
    let memo = new Array(scores.length).fill(0).map(_ => new Array());
    return dfs(0, 0);
    
    function dfs(maxAge, index) {
        if (index === players.length) {
            return 0;
        }
        
        if (memo[index][maxAge] !== undefined) {
            return memo[index][maxAge];
        }
        
        let max = 0;
        let currentPlayer = players[index];

        // cannot take because I'm too young and better than an old guy in my team
        if (currentPlayer.age < maxAge) {
            memo[index][maxAge] = dfs(maxAge, index + 1)
            return memo[index][maxAge];
        }
        
        // take
        max = Math.max(max, currentPlayer.score + dfs(Math.max(maxAge, currentPlayer.age), index + 1));
        
        // not take
        max = Math.max(max, dfs(maxAge, index + 1));
        
        memo[index][maxAge] = max;
        return max;
    }
};
Best Team With No Conflicts