# Maximum Sum Increasing Subsequence using DP in C++ program

C++Server Side ProgrammingProgramming

In this problem, we are given an array arr[] of size n. Our task is to create a program to find the maximum Sum Increasing Subsequence using DP in C++.

Problem Description − To find the maximum sum increasing subsequence, we will be creating a subsequence in which the next element is greater than the current element.

Let’s take an example to understand the problem,

## Input

arr[] = {4, 2, 3, 6, 5, 9}

## Output

20

## Explanation

Increasing subsequence with maximum sum:
{2, 3, 6, 9} = 2 + 3 + 6 + 9 = 20

## Solution Approach

To solve the problem using a dynamic Program Approach. We will create an array to store the maximum sum until the current element. Then return the maxSum from the array.

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
int retMaxVal(int x, int y){
if(x > y)
return x;
return y;
}
int calcMaxSubSeqSum(int arr[], int n) {
int maxSum = 0;
int sumDP[n];
for (int i = 0; i < n; i++ )
sumDP[i] = arr[i];
for (int i = 1; i < n; i++ )
for (int j = 0; j < i; j++ )
if ( (sumDP[i] < (sumDP[j] + arr[i])) && ( arr[i] >
arr[j] ) )
sumDP[i] = sumDP[j] + arr[i];
for (int i = 0; i < n; i++ )
maxSum = retMaxVal(sumDP[i], maxSum);
return maxSum;
}
int main() {
int arr[] = {4, 2, 3, 6, 5, 9};
int n = sizeof(arr)/sizeof(arr);
cout<<"The maximum Sum Increasing Subsequence using DP is
"<<calcMaxSubSeqSum(arr, n);
return 0;
}

## Output

Sum of maximum sum increasing subsequence is 20