# Printing Maximum Sum Increasing Subsequence

The Maximum Sum Increasing Subsequence problem is to find the maximum sum subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.

Examples:

Input:[1, 101, 2, 3, 100, 4, 5]Output:[1, 2, 3, 100]Input:[3, 4, 5, 10]Output:[3, 4, 5, 10]Input:[10, 5, 4, 3]Output:[10]Input:[3, 2, 6, 4, 5, 1]Output:[3, 4, 5]

In previous post, we have discussed about Maximum Sum Increasing Subsequence problem. However, the post only covered code related to finding maximum sum of increasing subsequence, but not to the construction of subsequence. In this post, we will discuss how to construct Maximum Sum Increasing Subsequence itself.

Let arr[0..n-1] be the input array. We define vector L such that L[i] is itself is a vector that stores Maximum Sum Increasing Subsequence of arr[0..i] that ends with arr[i]. Therefore for an index i, L[i] can be recursively written as

L[0] = {arr[0]} L[i] = {MaxSum(L[j])} + arr[i] where j < i and arr[j] < arr[i] = arr[i], if there is no j such that arr[j] < arr[i]

For example, for array [3, 2, 6, 4, 5, 1],

L[0]: 3 L[1]: 2 L[2]: 3 6 L[3]: 3 4 L[4]: 3 4 5 L[5]: 1

Below is C++ implementation of above idea –

`/* Dynamic Programming solution to construct ` ` ` `Maximum Sum Increasing Subsequence */` `#include <iostream> ` `#include <vector> ` `using` `namespace` `std; ` ` ` `// Utility function to calculate sum of all ` `// vector elements ` `int` `findSum(vector<` `int` `> arr) ` `{ ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i: arr) ` ` ` `sum += i; ` ` ` `return` `sum; ` `} ` ` ` `// Function to construct Maximum Sum Increasing ` `// Subsequence ` `void` `printMaxSumIS(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// L[i] - The Maximum Sum Increasing ` ` ` `// Subsequence that ends with arr[i] ` ` ` `vector <vector<` `int` `> > L(n); ` ` ` ` ` `// L[0] is equal to arr[0] ` ` ` `L[0].push_back(arr[0]); ` ` ` ` ` `// start from index 1 ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `{ ` ` ` `// for every j less than i ` ` ` `for` `(` `int` `j = 0; j < i; j++) ` ` ` `{ ` ` ` `/* L[i] = {MaxSum(L[j])} + arr[i] ` ` ` `where j < i and arr[j] < arr[i] */` ` ` `if` `((arr[i] > arr[j]) && ` ` ` `(findSum(L[i]) < findSum(L[j]))) ` ` ` `L[i] = L[j]; ` ` ` `} ` ` ` ` ` `// L[i] ends with arr[i] ` ` ` `L[i].push_back(arr[i]); ` ` ` ` ` `// L[i] now stores Maximum Sum Increasing ` ` ` `// Subsequence of arr[0..i] that ends with ` ` ` `// arr[i] ` ` ` `} ` ` ` ` ` `vector<` `int` `> res = L[0]; ` ` ` ` ` `// find max ` ` ` `for` `(vector<` `int` `> x : L) ` ` ` `if` `(findSum(x) > findSum(res)) ` ` ` `res = x; ` ` ` ` ` `// max will contain result ` ` ` `for` `(` `int` `i : res) ` ` ` `cout << i << ` `" "` `; ` ` ` `cout << endl; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 2, 6, 4, 5, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// construct and print Max Sum IS of arr ` ` ` `printMaxSumIS(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3 4 5

We can optimize above DP solution by removing findSum() function. Instead, we can maintain another vector/array to store sum of maximum sum increasing subsequence that ends with arr[i]. The implementation can be seen here.

**Time complexity** of above Dynamic Programming solution is O(n^{2}).

**Auxiliary space** used by the program is O(n^{2}).

This article is contributed by **Aditya Goel**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Printing longest Increasing consecutive subsequence
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence
- Maximum Sum Increasing Subsequence | DP-14
- Maximum product of an increasing subsequence
- Find the Increasing subsequence of length three with maximum product
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must
- Printing Longest Common Subsequence
- Printing Longest Bitonic Subsequence
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence | DP-3
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- C++ Program for Longest Increasing Subsequence
- C/C++ Program for Longest Increasing Subsequence
- Longest Increasing Subsequence Size (N log N)