Basketball Exercise
Contest: Codeforces Round 574 (Div. 2)
Difficulty rating: 1400
Tags: dp

Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $$$2 \cdot n$$$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $$$n$$$ people in each row). Students are numbered from $$$1$$$ to $$$n$$$ in each row in order from left to right.

Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $$$2n$$$ students (there are no additional constraints), and a team can consist of any number of students.

Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

Input format:
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of students in each row.

The second line of the input contains $$$n$$$ integers $$$h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$$$ ($$$1 \le h_{1, i} \le 10^9$$$), where $$$h_{1, i}$$$ is the height of the $$$i$$$-th student in the first row.

The third line of the input contains $$$n$$$ integers $$$h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$$$ ($$$1 \le h_{2, i} \le 10^9$$$), where $$$h_{2, i}$$$ is the height of the $$$i$$$-th student in the second row.

Output format:
Print a single integer — the maximum possible total height of players in a team Demid can choose.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example Demid can choose the following team as follows:

In the second example Demid can choose the following team as follows:

Example input:
5
9 3 5 7 3
5 8 1 4 5

Example output:
29

Example input:
3
1 2 9
10 1 1

Example output:
19

Editorial:
This is pretty standard dynamic programming problem. Let $$$dp_{i, 1}$$$ be the maximum total height of team members if the last student taken has the position $$$(i-1, 1)$$$, $$$dp_{i, 2}$$$ is the same but the last student taken has the position $$$(i-1, 2)$$$ and $$$dp_{i, 3}$$$ the same but we didn't take any student from position $$$i-1$$$. Transitions are pretty easy:
• $$$dp_{i, 1} = max(dp_{i-1, 2} + h_{i, 1}, dp_{i-1, 3} + h_{i, 1}, h_{i, 1})$$$;
• $$$dp_{i, 2} = max(dp_{i-1, 1} + h_{i, 2}, dp_{i-1, 3} + h_{i, 2}, h_{i, 2})$$$;
• $$$dp_{i, 3} = max(dp_{i-1, 1}, dp_{i-1, 2})$$$.
This dynamic programming can be calculated almost without using memory because we need only the $$$i-1$$$-th row to calculate the $$$i$$$-th row of this dp. Moreover, we don't actually need $$$dp_{i, 3}$$$ if we will add transitions $$$dp_{i, 1} = max(dp_{i, 1}, dp_{i - 1, 1})$$$ and $$$dp_{i, 2} = max(dp_{i, 2}, dp_{i - 1, 2})$$$. These transition will change our dp a bit. Now $$$dp_{i, j}$$$ is the maximum total height of the team members if the last student taken has the position $$$(i-1,1)$$$ or less. The same with $$$dp_{i, 2}$$$. The answer is $$$max(dp_{n, 1}, dp_{n, 2})$$$.
Time complexity: $$$O(n)$$$.