1
var bestTeamScore = function (scores, ages) {3
.map((score, index) => ({ score, age: ages[index] }))4
.sort((a, b) => (a.score === b.score ? a.age - b.age : a.score - b.score));5
let memo = new Array(scores.length).fill(0).map((_) => new Array());8
function dfs(maxAge, index) {9
if (index === players.length) {13
if (memo[index][maxAge] !== undefined) {14
return memo[index][maxAge];18
let currentPlayer = players[index];20
// cannot take because I'm too young and better than an old guy in my team21
if (currentPlayer.age < maxAge) {22
memo[index][maxAge] = dfs(maxAge, index + 1);23
return memo[index][maxAge];29
currentPlayer.score + dfs(Math.max(maxAge, currentPlayer.age), index + 1)33
max = Math.max(max, dfs(maxAge, index + 1));35
memo[index][maxAge] = max;