Maximum Sum Increasing Subsequence | DP-14 - GeeksforGeeks

21 Feb, 2022
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 integers 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 the Dynamic Programming solution to the problem :  
C++
C
Java
Python3
C#
PHP
Javascript
/* Dynamic Programming implementation of Maximum Sum Increasing Subsequence (MSIS) problem */#include <bits/stdc++.h>using namespace std;  /* maxSumIS() returns the maximum sum of increasing subsequence in arr[] of size n */int maxSumIS(int arr[], int n) {     int i, j, max = 0;     int msis[n];       /* Initialize msis values     for all indexes */    for ( i = 0; i < n; i++ )         msis[i] = arr[i];       /* Compute maximum sum values     in bottom up manner */    for ( i = 1; i < n; i++ )         for ( j = 0; j < i; j++ )             if (arr[i] > arr[j] &&                 msis[i] < msis[j] + arr[i])                 msis[i] = msis[j] + arr[i];       /* Pick maximum of     all msis values */    for ( i = 0; i < n; i++ )         if ( max < msis[i] )             max = msis[i];       return max; }   // Driver Code int main() {     int arr[] = {1, 101, 2, 3, 100, 4, 5};     int n = sizeof(arr)/sizeof(arr[0]);     cout << "Sum of maximum sum increasing "            "subsequence is " << maxSumIS( arr, n ) << endl;     return 0; }   // This is code is contributed by rathbhupendra
/* Dynamic Programming implementation of Maximum Sum Increasing Subsequence (MSIS) problem */#include<stdio.h>  /* maxSumIS() returns the maximum    sum of increasing subsequence   in arr[] of size n */int maxSumIS(int arr[], int n){    int i, j, max = 0;    int msis[n];      /* Initialize msis values       for all indexes */    for ( i = 0; i < n; i++ )        msis[i] = arr[i];      /* Compute maximum sum values        in bottom up manner */    for ( i = 1; i < n; i++ )        for ( j = 0; j < i; j++ )            if (arr[i] > arr[j] &&                 msis[i] < msis[j] + arr[i])                msis[i] = msis[j] + arr[i];      /* Pick maximum of       all msis values */    for ( i = 0; i < n; i++ )        if ( max < msis[i] )            max = msis[i];      return max;}  // Driver Codeint main(){    int arr[] = {1, 101, 2, 3, 100, 4, 5};    int n = sizeof(arr)/sizeof(arr[0]);    printf("Sum of maximum sum increasing "             "subsequence is %d\n",              maxSumIS( arr, n ) );    return 0;}
/* Dynamic Programming Java    implementation of Maximum Sum   Increasing Subsequence (MSIS)   problem */class GFG{    /* maxSumIS() returns the     maximum sum of increasing    subsequence in arr[] of size n */    static int maxSumIS(int arr[], int n)    {        int i, j, max = 0;        int msis[] = new int[n];          /* Initialize msis values            for all indexes */        for (i = 0; i < n; i++)            msis[i] = arr[i];          /* Compute maximum sum values           in bottom up manner */        for (i = 1; i < n; i++)            for (j = 0; j < i; j++)                if (arr[i] > arr[j] &&                    msis[i] < msis[j] + arr[i])                    msis[i] = msis[j] + arr[i];          /* Pick maximum of all           msis values */        for (i = 0; i < n; i++)            if (max < msis[i])                max = msis[i];          return max;    }      // Driver code    public static void main(String args[])    {        int arr[] = new int[]{1, 101, 2, 3, 100, 4, 5};        int n = arr.length;        System.out.println("Sum of maximum sum "+                            "increasing subsequence is "+                              maxSumIS(arr, n));    }}  // This code is contributed // by Rajat Mishra 
# Dynamic Programming based Python # implementation of Maximum Sum # Increasing Subsequence (MSIS)# problem  # maxSumIS() returns the maximum # sum of increasing subsequence # in arr[] of size ndef maxSumIS(arr, n):    max = 0    msis = [0 for x in range(n)]      # Initialize msis values    # for all indexes    for i in range(n):        msis[i] = arr[i]      # Compute maximum sum     # values in bottom up manner    for i in range(1, n):        for j in range(i):            if (arr[i] > arr[j] and                msis[i] < msis[j] + arr[i]):                msis[i] = msis[j] + arr[i]      # Pick maximum of    # all msis values    for i in range(n):        if max < msis[i]:            max = msis[i]      return max  # Driver Codearr = [1, 101, 2, 3, 100, 4, 5]n = len(arr)print("Sum of maximum sum increasing " +                      "subsequence is " +                  str(maxSumIS(arr, n)))  # This code is contributed # by Bhavya Jain
// Dynamic Programming C# implementation// of Maximum Sum Increasing Subsequence// (MSIS) problem using System;class GFG {          // maxSumIS() returns the     // maximum sum of increasing    // subsequence in arr[] of size n     static int maxSumIS( int []arr, int n )    {        int i, j, max = 0;        int []msis = new int[n];          /* Initialize msis values           for all indexes */        for ( i = 0; i < n; i++ )            msis[i] = arr[i];          /* Compute maximum sum values           in bottom up manner */        for ( i = 1; i < n; i++ )            for ( j = 0; j < i; j++ )                if ( arr[i] > arr[j] &&                    msis[i] < msis[j] + arr[i])                    msis[i] = msis[j] + arr[i];          /* Pick maximum of all            msis values */        for ( i = 0; i < n; i++ )            if ( max < msis[i] )                max = msis[i];          return max;    }          // Driver Code    public static void Main()    {        int []arr = new int[]{1, 101, 2, 3, 100, 4, 5};        int n = arr.Length;        Console.WriteLine("Sum of maximum sum increasing "+                                        " subsequence is "+        maxSumIS(arr, n));    }}  // This code is contributed by Sam007
<?php// Dynamic Programming implementation// of Maximum Sum Increasing// Subsequence (MSIS) problem   // maxSumIS() returns the maximum // sum of increasing subsequence// in arr[] of size n function maxSumIS($arr, $n){    $max = 0;    $msis= array($n);      // Initialize msis values    // for all indexes     for($i = 0; $i < $n; $i++ )        $msis[$i] = $arr[$i];      // Compute maximum sum values    // in bottom up manner     for($i = 1; $i < $n; $i++)        for($j = 0; $j < $i; $j++)            if ($arr[$i] > $arr[$j] &&                 $msis[$i] < $msis[$j] + $arr[$i])                $msis[$i] = $msis[$j] + $arr[$i];      // Pick maximum of all msis values    for($i = 0;$i < $n; $i++ )        if ($max < $msis[$i] )            $max = $msis[$i];      return $max;}      // Driver Code    $arr = array(1, 101, 2, 3, 100, 4, 5);    $n = count($arr);    echo "Sum of maximum sum increasing subsequence is "                                    .maxSumIS( $arr, $n );          // This code is contributed by Sam007?>
<script>  // Dynamic Programming implementation // of Maximum Sum Increasing Subsequence // (MSIS) problem   // maxSumIS() returns the maximum // sum of increasing subsequence // in arr[] of size n function maxSumIS(arr, n) {     let i, j, max = 0;     let msis = new Array(n);       // Initialize msis values     // for all indexes     for(i = 0; i < n; i++)         msis[i] = arr[i];       // Compute maximum sum values     // in bottom up manner     for(i = 1; i < n; i++)         for(j = 0; j < i; j++)             if (arr[i] > arr[j] &&                 msis[i] < msis[j] + arr[i])                 msis[i] = msis[j] + arr[i];       // Pick maximum of     // all msis values     for(i = 0; i < n; i++)         if (max < msis[i])             max = msis[i];       return max; }   // Driver Code let arr = [ 1, 101, 2, 3, 100, 4, 5 ]; let n = arr.length; document.write("Sum of maximum sum increasing " +                "subsequence is " + maxSumIS(arr, n));                  // This code is contributed by rishavmahato348  </script>
Output : 
Sum of maximum sum increasing subsequence is 106
Time Complexity: O(n^2) 
Space Complexity O(n) 
YouTubeGeeksforGeeks507K subscribersMaximum Sum Increasing Subsequence | Dynamic Programming | Set 14 | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:59•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=0NPYcQQWJ3Q" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
ParulShandilya
NikhilKumar23
rathbhupendra
rishavmahato348
saimayank2001
amartyaghoshgfg
simmytarika5
Amazon
LIS
Morgan Stanley
Arrays
Dynamic Programming
Morgan Stanley
Amazon
Arrays
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
0-1 Knapsack Problem | DP-10
Program for Fibonacci numbers
Longest Common Subsequence | DP-4
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16