{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Subsequence (MSIS) problem<br\/> <br\/># maxSumIS() returns the maximum sum of increasing subsequence in arr[] of<br\/># size n<br\/>def maxSumIS(arr, n):<br\/>    max = 0<br\/>    msis = [0 for x in range(n)]<br\/> <br\/>    # Initialize msis values for all indexes<br\/>    for i in range(n):<br\/>        msis[i] = arr[i]<br\/> <br\/>    # Compute maximum sum values in bottom up manner<br\/>    for i in range(1, n):<br\/>        for j in range(i):<br\/>            if arr[i] &gt; arr[j] and msis[i] &lt; msis[j] + arr[i]:<br\/>                msis[i] = msis[j] + arr[i]<br\/> <br\/>    # Pick maximum of all msis values<br\/>    for i in range(n):<br\/>        if max &lt; msis[i]:<br\/>            max = msis[i]<br\/> <br\/>    return max<br\/> <br\/># Driver program to test above function<br\/>arr = [1, 101, 2, 3, 100, 4, 5]<br\/>n = len(arr)<br\/>print(&quot;Sum of maximum sum increasing subsequence is &quot; +<br\/>       str(maxSumIS(arr, n)))<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\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}]}}