4
public int bestTeamScore(int[] scores, int[] ages) {5
int len = scores.length;6
int[][] team = new int[len][2];7
for (int i = 0; i < len; i++) {9
team[i][1] = scores[i]; // team is [age, score]11
// double sort first by age then by score, then we can traverse from young to old12
Arrays.sort(team, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);14
int[] dp = new int[len];15
// dp is the max sum for all sequences (not necessarily consecutive) ending in current idx17
for (int i = 1; i < len; i++) {18
int max = team[i][1]; // At least it could start a new sequence by itself without extend19
// for each current idx, go visit all previous index to grow the sequences20
for (int j = 0; j < i; j++) {21
if (team[i][1] >= team[j][1]) {22
max = Math.max(max, dp[j] + team[i][1]);30
res = Math.max(res, num);