AXION Coding Question – Solved

2 Live
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2 * n students have come to Demid's exercise session, and he lined them up 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. Array or integers. The first element is n. The next n elements are the heights of the students in the first row. The next n elements are the heights of the students in the second row. Output Print a single integer - the maximum possible total height of players in a team Demid can choose. Examples Input: {5, 9, 3, 5, 7, 3, 5, 8, 1, 4, 5} Output: 29 Input: {3, 1, 2, 9, 10, 1, 1} Output: 19 Browse Resources Search for any documentation you might need for this problem. For example: array indexing, React props, etc. powered by Google

Asked in: AXION

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
This problem is essentially about selecting a subsequence of students from two rows, subject to certain constraints, to maximize the total height sum. The key challenge is to enforce two conditions simultaneously while maximizing the sum:

1. The chosen players must be strictly increasing in their column index (from left to right).
2. No two consecutive chosen players come from the same row.

Let's break down the problem and analyze it carefully:

Understanding the Setup:
- There are 2 rows and n columns (students per row). So, total 2*n students.
- Each student has a height value.
- You must select a sequence of students from left to right (strictly increasing indices).
- You cannot pick two students in a row consecutively from the same row. If the last chosen player was from the first row, the next must be from the second row, and vice versa.
- The first player can be chosen freely from any of the 2*n students.
- The team size is flexible; you can select any number of students (including just one).
- The goal is to maximize the sum of the chosen players’ heights.

Key Observations:
- Since students are chosen left to right, you are basically selecting indices i_1 < i_2 < ... < i_k from the range [1, n].
- For each chosen index, you pick exactly one student: either from the top row or the bottom row, alternating rows for consecutive picks.
- Because of the alternating-row constraint, the sequence alternates between rows, for example: top row → bottom row → top row → bottom row ... or bottom row → top row → bottom row → top row ...
- The starting choice is unrestricted: you can start from either row at any index.

Thinking About Dynamic Programming:
This problem fits well into a dynamic programming (DP) approach because the solution depends on decisions made at previous indices and rows, and you want to build up solutions optimally.

Consider the state variables:
- The current position (index in [1..n]).
- The row of the previously chosen player (top or bottom).
- Possibly, whether you have chosen any player yet or not (to handle the first pick without restriction).

For each position i and row r, you want to know the maximum sum achievable if the last chosen player was at index i in row r.

However, since you must pick players strictly increasing in index, you cannot choose multiple students at the same index. Instead, for each index i, you consider two possible decisions: picking the top row student or the bottom row student (or skipping both). But you cannot pick both because indices must strictly increase.

How To Define Subproblems:
Define two DP arrays:
- dpTop[i]: the maximum sum achievable if the last chosen player is at index i in the top row.
- dpBottom[i]: the maximum sum achievable if the last chosen player is at index i in the bottom row.

Transition:
To compute dpTop[i], you consider all positions j < i, and from those, only those where the last chosen player was in the bottom row (because you must alternate rows), and add the height of the top-row student at index i.

Similarly, dpBottom[i] is computed from positions j < i where the last chosen player was in the top row, plus the height at bottom row at index i.

Also, since the first player can be any student, dpTop[i] and dpBottom[i] can start fresh at index i (meaning the subsequence contains only that one student).

You may also consider skipping some indices (not picking students at certain positions), so you want to find the maximum dp value overall.

Optimizing the Solution:
Naively, the transitions look like they require O(n^2) time because for each i, you look back to all j < i. But with constraints n ≤ 10^5, this is not efficient.

Notice that when calculating dpTop[i], you only need the maximum dpBottom[j] for j < i. Similarly for dpBottom[i], you need the maximum dpTop[j] for j < i.

You can keep track of prefix maximums of dpTop and dpBottom to make this O(n):

- At position i, dpTop[i] = heightTop[i] + max(dpBottom[j] for j < i) or just heightTop[i] if no previous dpBottom.

- Similarly, dpBottom[i] = heightBottom[i] + max(dpTop[j] for j < i) or just heightBottom[i].

Implementing this, you keep updating running maximums while scanning from left to right.

Final Step:
After filling dpTop and dpBottom for all indices, the answer is the maximum value among all dpTop[i] and dpBottom[i], which represents the best total height sum achievable under the constraints.

Summary of the Approach:
- Represent the problem as finding the maximum alternating subsequence sum of heights, with strict increasing index constraints.
- Use two DP arrays to store max sums ending at each position for each row.
- Transitions depend on the max sum ending in the opposite row before the current index.
- Use prefix maxima to optimize the transitions from O(n^2) to O(n).
- Return the overall maximum among dpTop and dpBottom arrays.

By carefully building this DP solution and maintaining prefix maximums, you can efficiently find the maximum total height of a team that satisfies Demid’s constraints.
```


Related Questions