> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Best Team With No Conflicts Python Solution

> Tested Python solution for LeetCode 1626 with 27 pytest cases. Generate a practice environment with lcpy.

LeetCode 1626, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/best-team-with-no-conflicts/description/).

Generate this problem as a practice environment: tested reference solution, 27 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1626   # by problem number
lcpy gen -s best_team_with_no_conflicts   # by problem name
```

## Problem

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 `i`th player, respectively, return *the highest overall score of all possible basketball teams*.

### Examples

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

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

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

### Constraints

* 1 \<= scores.length, ages.length \<= 1000
* scores.length == ages.length
* 1 \<= scores\[i] \<= 10^6
* 1 \<= ages\[i] \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/best_team_with_no_conflicts/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/best_team_with_no_conflicts/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def best_team_score(self, scores: list[int], ages: list[int]) -> int:
        pairs = sorted(zip(scores, ages, strict=True), key=lambda p: (p[1], p[0]))
        ranks = {score: i for i, score in enumerate(sorted(set(scores)))}
        size = len(ranks)
        tree: list[int] = [0] * (size + 1)

        def update(index: int, value: int) -> None:
            index += 1
            while index <= size:
                tree[index] = max(tree[index], value)
                index += index & (-index)

        def query(index: int) -> int:
            index += 1
            best = 0
            while index > 0:
                best = max(best, tree[index])
                index -= index & (-index)
            return best

        result = 0
        for score, _age in pairs:
            current = query(ranks[score]) + score
            result = max(result, current)
            update(ranks[score], current)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.