# Maximum sum by picking elements from two arrays in order | Set 2

Given two arrays **A[]** and **B[]**, each of size **N**, and two integers **X** and **Y **denoting the maximum number of elements that can be picked from A[] and B[] respectively, the task is to find the maximum possible sum by selecting **N** elements in such a way that for any index **i**, either A[i] or B[i] can be chosen.

**Note:** It is guaranteed that **(X + Y) >= N.**

**Examples:**

Input:A[] = {1, 2, 3, 4, 5}, B[] = {5, 4, 3, 2, 1}, X = 3, Y = 2

Output:21

i = 0 -> 5 picked

i = 1 -> 4 picked

i = 2 -> 3 picked

i = 3 -> 4 picked

i = 4 -> 5 picked

5 + 4 + 3 + 4 + 5 = 21

Input:A[] = {1, 4, 3, 2, 7, 5, 9, 6}, B[] = {1, 2, 3, 6, 5, 4, 9, 8}, X = 4, Y = 4

Output:43

**Greedy Approach:** The greedy approach to solve this problem has already been discussed in this post.

**Dynamic Programming Approach:** In this article, we are discussing a Dynamic Programming based solution.

Follow the steps below to solve the problem:

- Atmost
**min (N, X)**elements can be selected from**A[]**and**min(N, Y)**elements can be selected from**B[]**. - initialise a 2D array
**dp[][]**such that**dp[i][j]**contains the maximum sum possible by selecting**i**elements from**A[]**and**j**elements from**B[]**where i and j ranges within**min (N, X)**and**min (N, X)**respectively. - Initialize a variable
**max_sum**to store maximum sum possible. - Traverse the array and for every array element do the following:
- The
**(i + j)**can either be^{th}element**A[i + j – 1]**or**B[i + j – 1]**. - If the
**(i + j)**element is selected from^{th}**A[]**, then the cost of**(i + 1)**element in A[] will be added to the total cost. Hence, the cost of selecting^{th}**(i + 1)**element is^{th}**dp[i][j] = dp[ i – 1 ][ j ] + A[ i + j – 1 ]**for this case. - If the
**(i + j)**element is selected from^{th}**B[]**, then the cost of selecting**(i + 1)**element is^{th}**dp[i][j] = dp[ i – 1 ][ j ] + B[ i + j – 1 ]**.

- The
- Now, the target is to maximize the cost. Hence, the recurrence relation is:

dp[i][j] = max(dp[ i – 1 ][ j ] + A[ i + j – 1 ], dp[ i – 1 ][ j ] + B[ i + j – 1 ])

- Keep updating
**max_sum**after every iteration. After complete traversal of the array print the final value of**max_sum**.

Below is the implementation of the above approach :

## C++

`// C++ program to find maximum sum ` `// possible by selecting an element ` `// from one of two arrays for every index ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate maximum sum ` `int` `maximumSum(` `int` `A[], ` `int` `B[], ` ` ` `int` `length, ` ` ` `int` `X, ` `int` `Y) ` `{ ` ` ` `int` `l = length; ` ` ` `// Maximum elements that can be ` ` ` `// chosen from array A ` ` ` `int` `l1 = min(length, X); ` ` ` ` ` `// Maximum elements that can be ` ` ` `// chosen from array B ` ` ` `int` `l2 = min(length, Y); ` ` ` ` ` `int` `dp[l1 + 1][l2 + 1]; ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` `dp[0][0] = 0; ` ` ` ` ` `// Stores the maximum ` ` ` `// sum possible ` ` ` `int` `max_sum = INT_MIN; ` ` ` ` ` `// Fill the dp[][] for base case when ` ` ` `// all elements are selected from A[] ` ` ` `for` `(` `int` `i = 1; i <= l1; i++) { ` ` ` `dp[i][0] = dp[i - 1][0] + A[i - 1]; ` ` ` `max_sum = max(max_sum, dp[i][0]); ` ` ` `} ` ` ` ` ` `// Fill the dp[][] for base case when ` ` ` `// all elements are selected from B[] ` ` ` `for` `(` `int` `i = 1; i <= l2; i++) { ` ` ` `dp[0][i] = dp[0][i - 1] + B[i - 1]; ` ` ` `max_sum = max(max_sum, dp[0][i]); ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 1; i <= l1; i++) { ` ` ` `for` `(` `int` `j = 1; j <= l2; j++) { ` ` ` `if` `(i + j <= l) ` ` ` `dp[i][j] ` ` ` `= max(dp[i - 1][j] ` ` ` `+ A[i + j - 1], ` ` ` `dp[i][j - 1] ` ` ` `+ B[i + j - 1]); ` ` ` `max_sum = max(dp[i][j], ` ` ` `max_sum); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final answer ` ` ` `return` `max_sum; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `A[] = { 1, 2, 3, 4, 5 }; ` ` ` `int` `B[] = { 5, 4, 3, 2, 1 }; ` ` ` `int` `X = 3, Y = 2; ` ` ` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `cout << maximumSum(A, B, N, X, Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

21

**Time Complexity:** O(N^{2})

**Auxiliary Space:** O(N^{2})

## Recommended Posts:

- Maximum sum by picking elements from two arrays in order
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Maximum sum of increasing order elements from n arrays
- Combinations from n arrays picking one element from each array
- Maximum array from two given arrays keeping order same
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximum area rectangle by picking four sides from array
- Divide array in two maximum equal length arrays of similar and dissimilar elements
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Merging two unsorted arrays in sorted order
- Number of ways to merge two arrays such retaining order
- Permute the elements of an array following given order
- Minimum size Subarray with maximum sum in non-increasing order
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Maximum OR sum of sub-arrays of two different arrays
- k smallest elements in same order using O(1) extra space
- Order of indices which is lexicographically smallest and sum of elements is <= X
- Find smallest subarray that contains all elements in same order

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.