{"id":26223,"date":"2017-10-26T20:30:34","date_gmt":"2017-10-26T15:00:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26223"},"modified":"2017-10-26T20:30:34","modified_gmt":"2017-10-26T15:00:34","slug":"python-programming-maximum-sum-increasing-subsequence","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-maximum-sum-increasing-subsequence\/","title":{"rendered":"Python Programming &#8211; Maximum Sum Increasing Subsequence"},"content":{"rendered":"<p>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.<span id=\"more-19248\"><\/span> 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<\/p>\n<p><strong>Solution<\/strong><br \/>\nThis 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.<\/p>\n<p>Following are python\u00a0implementations for Dynamic Programming solution of the problem.<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%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)))\u201d message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>Sum of maximum sum increasing subsequence is 106<\/pre>\n<p>Time Complexity: O(n^2)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Python Programming &#8211; Maximum Sum Increasing Subsequence &#8211; Dynamic Programming Given an array of n positive integers. To find the sum of maximum sum<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,70145,4148],"tags":[72847,72842,72845,70483,72848,72840,72846,72994,72843,72992,72854,77933,72844,72850,72839,77930,77935,77934,77932,77928,77929,77931,77938,77936,77937,77927,72852,72855,72853,72851],"class_list":["post-26223","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-dynamic-programming","category-python","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-python","tag-dynamic-programming-problems","tag-dynamic-programming-python","tag-dynamic-programming-set-1","tag-dynamic-programming-set-15","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-increasing-subsequence-with-maximum-sum-in-python-code","tag-largest-sum-non-contiguous-subarray","tag-maximum-increasing-subsequence","tag-maximum-subsequence-sum","tag-maximum-sum-alternating-subsequence","tag-maximum-sum-increasing-subsequence-dynamic-programming","tag-maximum-sum-increasing-subsequence-nlogn","tag-maximum-sum-subarray-divide-and-conquer-in-python-code","tag-maximum-sum-subarray-dynamic-programming","tag-maximum-sum-subsequence-dynamic-programming","tag-printing-maximum-sum-increasing-subsequence","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26223"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26223\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}