Maximum sum increasing subsequence-dynamic programming
in an array of positive integers. Find the sum of maximum sum increasing subsequence .
n=7
a[7]=1 101 2 3 100 4 5
All the increasing subsequences are : (1,101); (1,2,3,100); (1,2,3,4,5). Out of these (1, 2, 3, 100) has maximum sum,i.e., 106.
so output=>106
lets use dynamic programming to solve this question:
c++ implementation:
t is the no.of test cases:
n=7
a[7]=1 101 2 3 100 4 5
All the increasing subsequences are : (1,101); (1,2,3,100); (1,2,3,4,5). Out of these (1, 2, 3, 100) has maximum sum,i.e., 106.
so output=>106
lets use dynamic programming to solve this question:
c++ implementation:
t is the no.of test cases:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void maxsumincreasing(int a[],int n,int dp[])
{
for(int i=1;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(a[i]>a[j])
dp[i]=max(dp[i],dp[j]+a[i]);
}
}
int maxele=dp[0];
for(int i=1;i<n;i++)
maxele=max(maxele,dp[i]);
cout<<maxele;
}
int main() { int t; cin>>t;
while(t--)
{
int n; cin>>n;
int a[n];int dp[n];
for(int i=0;i<n;i++)
{cin>>a[i];
dp[i]=a[i];
}
maxsumincreasing(a,n,dp);
cout<<endl;
}
//code
return 0;
}