# Codey and Team Selection

## Question

Codey is a coach, and it is assembling a team for a competitive sports league. It is going to select `n` players for the team. Each player possesses a skill point represented by $$a\_i$$. The player selection process consists of `n` steps, and in each step:

* Let `b` to be the sorted array of skill points for **unselected** players. Choose an integer `j`, where $$1 \le j \le k$$, and k is the current total of unselected players, that player joins the team and gains an additional skill point, $$s\_j$$. In other words, the total team skill increases by $$b\_j + s\_j$$.

Codey wants to strategically select players to **maximize** the overall team skill for the upcoming sports league, while following the constraints of the selection process. Can you help Codey to achieve this?

### Input Format

The first line contains a single integer `n`, which represents the number of players.

The second line contains `n` integers $$a\_1, a\_2, ..., a\_n$$, each representing the skill points of the $$i$$-th player.

The third line contains `n` integers $$s\_1, s\_2, ..., s\_n$$, each representing the additional skill points gained for selecting the $$j$$-th player in the array `b`.

### Constraints

$$
1 \le n \le 10^5
$$

$$
1 \le a\_i \le 10^5
$$

$$
1 \le s\_j \le 10^k
$$

$$
1 \le a\_1 \le a\_2 \le ... \le a\_n
$$

### Output Format

Output an integer representing the maximum total team skill.

### Sample Inputs:

{% tabs %}
{% tab title="Input 0" %}

#### Input

```
3
1 2 3
3 3 1
```

#### Output

```
15
```

#### Explanation

We can select the players as such:

1. The current team skill is $$0$$ and `b = [1, 2, 3]`. We choose `j = 1` and the team skill increase by $$b\_1 + s\_1 = 1 + 3 = 4$$.
2. The current team skill is $$0 + 4 = 4$$ and `b = [2, 3]`. We choose `j = 2` and the team skill increase by $$b\_2 + s\_2 = 3 + 3 = 6$$.
3. The current team skill is $$4 + 6 = 10$$ and `b = [2]`. We choose `j = 1` and the team skill increase by $$b\_1 + s\_1 = 2 + 3 = 5$$.
4. All players are selected, selection ended. The total team skill is $$0 + 4 + 6 + 5 = 15$$.
   {% endtab %}
   {% endtabs %}

***

<details>

<summary>Solution - Question is Confusing</summary>

This question is so confusing that I couldn't finish at the competition QAQ

After been three days of reading, finally I get it how the coach selects the players. Here's the details:

* Each player position affects how many skill points, sj, can be chosen.
  * For example, player\[0] can only choose s\[0], player\[1] can choose between s\[0] and s\[1], and so on.

Which means, we had to do two loops which:

1. loop through players and indexes
2. find the maximum skill points that player can get based on the position of player.

But this will get a **time limit exceeded**, as the array is large enough to cough the program out of time.

One way I thought to optimize it is by **memorization**, which is to by memorize the current maximum value from that array, next time only need to compare that max with the next index cell.

And not surprising, memorization works and passed all test cases!

Here's my code:

{% code overflow="wrap" lineNumbers="true" %}

```python
t = int(input().strip())
arr1 = list(map(int, input().strip().split()))
arr2 = list(map(int, input().strip().split()))

max_cache = 0
total = 0

for i, x in enumerate(arr1):
    max_cache = max(max_cache, arr2[i])
    total += x + max_cache

print(total)
```

{% endcode %}

</details>
