- Arrays, Dynamic Programming, Game Theory, Mathematical, matrix-chain-multiplication, Memoization, Recursion

Maximize sum of odd-indexed array elements by repeatedly selecting at most 2*M array elements from the beginning

Maximize sum of odd-indexed array elements by repeatedly selecting at most 2*M array elements from the beginningGiven an array arr[] consisting of N integers and an integer M (initially 1), the task is to find the maximum sum of array elements chosen by Player A when two players A and B plays the game optimally according to the following rules:Player A starts the game.At every chance, X number of elements can be chosen from the beginning of the array, where X is inclusive over the range [1, 2*M] is chosen by the respective player in their turn.After choosing array elements in the above steps, remove those elements from the array and update the value of M as the maximum of X and M.The above process will continue till all the array elements are chosen.Examples:Input: arr[] = {2, 7, 9, 4, 4}Output: 10Explanation:Initially the array is arr[] = {2, 7, 9, 4, 4} and the value of M = 1, Below are the order of ch0osing array elements by both the players:Player A: The number of elements can be chosen over the range [1, 2*M] i.e., [1, 2]. So, choose element {2} and remove it. Now the array modifies to {7, 9, 4, 4} and the value of M is max(M, X) = max(1, 1) = 1(X is 1).Player B: The number of elements can be chosen over the range [1, 2*M] i.e., [1, 2]. So, choose element {7, 9} and remove it. Now the array modifies to {4, 4} and the value of M is max(M, X) = max(1, 2) = 2(X is 2).Player A: The number of elements can be chosen over the range [1, 2*2] i.e., [1, 1]. So, choose element {4, 4} and remove it. Now the array becomes empty.Therefore, the sum of elements chosen by the Player A is 2 + 4 + 4 = 10.Input: arr[] = {1}Output: 1Naive Approach: The simplest approach is to solve the given problem is to use recursion and generate all possible combinations of choosing elements for both the players from the beginning according to the given rules and print the maximum sum of chosen elements obtained for Player A. Follow the below steps to solve the given problem:Declare a recursive function, say recursiveChoosing(arr, start, M) that takes parameters array, starting index of the current array, and initial value of M and perform the following operations in this function:If the value of start is greater than N, then return 0.If the value of (N – start) is at most 2*M, then return the sum of the element of the array from the index start for the respective score of the player.Initialize a maxSum as 0 that stores the maximum sum of array elements chosen by Player A.Find the total sum of the array elements from the start and store it in a variable, say total.Iterate over the range [1, 2*M], and perform the following steps:For each element X, chooses X elements from the start and recursively calls for choosing elements from the remaining (N – X) elements. Let the value returned by this call be stored in maxSum.After the above recursive call ends update the value of maxSum to the maximum of maxSum and (total – maxSum).Return the value of maxSum in each recursive call.After completing the above steps, print the value returned by the function recursiveChoosing(arr, 0, 1).Below is the implementation of the above approach:Python3  def recursiveChoosing(arr, start, M):            if start >= N:         return 0                    if N – start = N:         return 0                    if N – start