Given an array of n positive integers. Write a program to find the sum of maximum sum subsequence of the given array such that the intgers in the subsequence are sorted in increasing order. For example, if input is {1, 101, 2, 3, 100, 4, 5}, then output should be 106 (1 + 2 + 3 + 100), if the input array is {3, 4, 5, 10}, then output should be 22 (3 + 4 + 5 + 10) and if the input array is {10, 5, 4, 3}, then output should be 10

Solution
This problem is a variation of standard Longest Increasing Subsequence (LIS) problem. We need a slight change in the Dynamic Programming solution of LIS problem. All we need to change is to use sum as a criteria instead of length of increasing subsequence.

Following are python implementations for Dynamic Programming solution of the problem.

[pastacode lang=”python” manual=”%23%20Subsequence%20(MSIS)%20problem%0A%20%0A%23%20maxSumIS()%20returns%20the%20maximum%20sum%20of%20increasing%20subsequence%20in%20arr%5B%5D%20of%0A%23%20size%20n%0Adef%20maxSumIS(arr%2C%20n)%3A%0A%20%20%20%20max%20%3D%200%0A%20%20%20%20msis%20%3D%20%5B0%20for%20x%20in%20range(n)%5D%0A%20%0A%20%20%20%20%23%20Initialize%20msis%20values%20for%20all%20indexes%0A%20%20%20%20for%20i%20in%20range(n)%3A%0A%20%20%20%20%20%20%20%20msis%5Bi%5D%20%3D%20arr%5Bi%5D%0A%20%0A%20%20%20%20%23%20Compute%20maximum%20sum%20values%20in%20bottom%20up%20manner%0A%20%20%20%20for%20i%20in%20range(1%2C%20n)%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(i)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20arr%5Bi%5D%20%3E%20arr%5Bj%5D%20and%20msis%5Bi%5D%20%3C%20msis%5Bj%5D%20%2B%20arr%5Bi%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20msis%5Bi%5D%20%3D%20msis%5Bj%5D%20%2B%20arr%5Bi%5D%0A%20%0A%20%20%20%20%23%20Pick%20maximum%20of%20all%20msis%20values%0A%20%20%20%20for%20i%20in%20range(n)%3A%0A%20%20%20%20%20%20%20%20if%20max%20%3C%20msis%5Bi%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20max%20%3D%20msis%5Bi%5D%0A%20%0A%20%20%20%20return%20max%0A%20%0A%23%20Driver%20program%20to%20test%20above%20function%0Aarr%20%3D%20%5B1%2C%20101%2C%202%2C%203%2C%20100%2C%204%2C%205%5D%0An%20%3D%20len(arr)%0Aprint(%22Sum%20of%20maximum%20sum%20increasing%20subsequence%20is%20%22%20%2B%0A%20%20%20%20%20%20%20str(maxSumIS(arr%2C%20n)))” message=”Python” highlight=”” provider=”manual”/]

Output:

Sum of maximum sum increasing subsequence is 106

Time Complexity: O(n^2)

[ad type=”banner”]