# Maximize sum of all elements which are not a part of the Longest Increasing Subsequence

Given an array arr[], the task is to find the maximum sum of all the elements which are not a part of the longest increasing subsequence.
Examples:

Input: arr[] = {4, 6, 1, 2, 3, 8}
Output: 10
Explanation:
Elements are 4 and 6

Input: arr[] = {5, 4, 3, 2, 1}
Output: 14
Explanation:
Elements are 5, 4, 3, 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• The idea is to find the longest increasing subsequence with the minimum sum and then subtract it from the sum of all elements.
• To do this we will use the concept of LIS using Dynamic Programming and store the sum along with the length of the subsequences and update the minimum sum accordingly.

Below is the implementation of the above approach.

## C++

 `// C++ program to find the Maximum sum of ` `// all elements which are not a part of ` `// longest increasing sub sequence ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find maximum sum ` `int` `findSum(``int``* arr, ``int` `n) ` `{ ` `    ``int` `totalSum = 0; ` ` `  `    ``// Find total sum of array ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``totalSum += arr[i]; ` `    ``} ` ` `  `    ``// Maintain a 2D array ` `    ``int` `dp[n]; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``dp[i] = 1; ` `        ``dp[i] = arr[i]; ` `    ``} ` ` `  `    ``// Update the dp array along ` `    ``// with sum in the second row ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < i; j++) { ` `            ``if` `(arr[i] > arr[j]) { ` `                ``// In case of greater length ` `                ``// Update the length along ` `                ``// with sum ` `                ``if` `(dp[i] < dp[j] + 1) { ` `                    ``dp[i] = dp[j] + 1; ` `                    ``dp[i] = dp[j] ` `                               ``+ arr[i]; ` `                ``} ` ` `  `                ``// In case of equal length ` `                ``// find length update length ` `                ``// with minimum sum ` `                ``else` `if` `(dp[i] ` `                         ``== dp[j] + 1) { ` `                    ``dp[i] ` `                        ``= min(dp[i], ` `                              ``dp[j] ` `                                  ``+ arr[i]); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` `    ``int` `maxm = 0; ` `    ``int` `subtractSum = 0; ` ` `  `    ``// Find the sum that need to ` `    ``// be subtracted from total sum ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(dp[i] > maxm) { ` `            ``maxm = dp[i]; ` `            ``subtractSum = dp[i]; ` `        ``} ` `        ``else` `if` `(dp[i] == maxm) { ` `            ``subtractSum = min(subtractSum, ` `                              ``dp[i]); ` `        ``} ` `    ``} ` ` `  `    ``// Return the sum ` `    ``return` `totalSum - subtractSum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 6, 1, 2, 3, 8 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << findSum(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the Maximum sum of ` `// all elements which are not a part of ` `// longest increasing sub sequence ` `class` `GFG{ ` ` `  `// Function to find maximum sum ` `static` `int` `findSum(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `totalSum = ``0``; ` ` `  `    ``// Find total sum of array ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `       ``totalSum += arr[i]; ` `    ``} ` ` `  `    ``// Maintain a 2D array ` `    ``int` `[][]dp = ``new` `int``[``2``][n]; ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `       ``dp[``0``][i] = ``1``; ` `       ``dp[``1``][i] = arr[i]; ` `    ``} ` ` `  `    ``// Update the dp array along ` `    ``// with sum in the second row ` `    ``for``(``int` `i = ``1``; i < n; i++) ` `    ``{ ` `       ``for``(``int` `j = ``0``; j < i; j++) ` `       ``{ ` `          ``if` `(arr[i] > arr[j]) ` `          ``{ ` `               `  `              ``// In case of greater length ` `              ``// Update the length along ` `              ``// with sum ` `              ``if` `(dp[``0``][i] < dp[``0``][j] + ``1``) ` `              ``{ ` `                  ``dp[``0``][i] = dp[``0``][j] + ``1``; ` `                  ``dp[``1``][i] = dp[``1``][j] + arr[i]; ` `              ``} ` `               `  `              ``// In case of equal length ` `              ``// find length update length ` `              ``// with minimum sum ` `              ``else` `if` `(dp[``0``][i] == dp[``0``][j] + ``1``) ` `              ``{ ` `                  ``dp[``1``][i] = Math.min(dp[``1``][i],  ` `                                      ``dp[``1``][j] + arr[i]); ` `              ``} ` `          ``} ` `       ``} ` `    ``} ` `    ``int` `maxm = ``0``; ` `    ``int` `subtractSum = ``0``; ` ` `  `    ``// Find the sum that need to ` `    ``// be subtracted from total sum ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `       ``if` `(dp[``0``][i] > maxm) ` `       ``{ ` `           ``maxm = dp[``0``][i]; ` `           ``subtractSum = dp[``1``][i]; ` `       ``} ` `       ``else` `if` `(dp[``0``][i] == maxm) ` `       ``{ ` `           ``subtractSum = Math.min(subtractSum, dp[``1``][i]); ` `       ``} ` `    ``} ` ` `  `    ``// Return the sum ` `    ``return` `totalSum - subtractSum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``4``, ``6``, ``1``, ``2``, ``3``, ``8` `}; ` `    ``int` `n = arr.length; ` ` `  `    ``System.out.print(findSum(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

## C#

 `// C# program to find the Maximum sum of ` `// all elements which are not a part of ` `// longest increasing sub sequence ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find maximum sum ` `static` `int` `findSum(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `totalSum = 0; ` ` `  `    ``// Find total sum of array ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``totalSum += arr[i]; ` `    ``} ` ` `  `    ``// Maintain a 2D array ` `    ``int` `[,]dp = ``new` `int``[2, n]; ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``dp[0, i] = 1; ` `        ``dp[1, i] = arr[i]; ` `    ``} ` ` `  `    ``// Update the dp array along ` `    ``// with sum in the second row ` `    ``for``(``int` `i = 1; i < n; i++) ` `    ``{ ` `        ``for``(``int` `j = 0; j < i; j++) ` `        ``{ ` `            ``if` `(arr[i] > arr[j]) ` `            ``{ ` `                     `  `                ``// In case of greater length ` `                ``// Update the length along ` `                ``// with sum ` `                ``if` `(dp[0, i] < dp[0, j] + 1) ` `                ``{ ` `                    ``dp[0, i] = dp[0, j] + 1; ` `                    ``dp[1, i] = dp[1, j] + arr[i]; ` `                ``} ` `                     `  `                ``// In case of equal length ` `                ``// find length update length ` `                ``// with minimum sum ` `                ``else` `if` `(dp[0, i] == dp[0, j] + 1) ` `                ``{ ` `                    ``dp[1, i] = Math.Min(dp[1, i],  ` `                                        ``dp[1, j] + arr[i]); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` `    ``int` `maxm = 0; ` `    ``int` `subtractSum = 0; ` ` `  `    ``// Find the sum that need to ` `    ``// be subtracted from total sum ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``if` `(dp[0, i] > maxm) ` `        ``{ ` `            ``maxm = dp[0, i]; ` `            ``subtractSum = dp[1, i]; ` `        ``} ` `        ``else` `if` `(dp[0, i] == maxm) ` `        ``{ ` `            ``subtractSum = Math.Min(subtractSum, dp[1, i]); ` `        ``} ` `    ``} ` ` `  `    ``// Return the sum ` `    ``return` `totalSum - subtractSum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]arr = { 4, 6, 1, 2, 3, 8 }; ` `    ``int` `n = arr.Length; ` ` `  `    ``Console.Write(findSum(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```10
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : sapnasingh4991