Maximum Sum Increasing Subsequence | DP-14 in C++


In this tutorial, we will be discussing a program to find maximum Sum Increasing Subsequence.

For this we will be provided with an array containing N integers. Our task is to pick up elements from the array adding to the maximum sum such that the elements are in sorted order

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//returning the maximum sum
int maxSumIS(int arr[], int n) {
   int i, j, max = 0;
   int msis[n];
   for ( i = 0; i < n; i++ )
      msis[i] = arr[i];
   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];
      for ( i = 0; i < n; i++ )
         if ( max < msis[i] )
            max = msis[i];
         return max;
}
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;
}

Output

Sum of maximum sum increasing subsequence is 106

Updated on: 22-Jul-2020

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements