Suppose we have two lists called scores and ages, where scores[i] and ages[i] represents the score and age of the ith player in a basketball game. We want to select the team with the highest overall score. Here the score of the team is the total sum of scores of all the players in the team. But we do not allow conflicts in the game. Here a conflict exists if a younger player has a strictly higher score than an older player.
So, if the input is like scores = [5,7,9,14,19], ages = [5,6,7,8,9], then the output will be 54 as we can select all players.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(scores, ages): sa = [[a,s] for a,s in zip(ages,scores)] sa.sort() scores = [s for a,s in sa] maxScore = 0 n = len(scores) dp =  * n for i in range(n): score = scores[i] dp[i] = score for j in range(i): if scores[j] <= score: dp[i] = max(dp[i],dp[j] + score) maxScore = max(maxScore, dp[i]) return maxScore scores = [5,7,9,14,19] ages = [5,6,7,8,9] print(solve(scores, ages))