Printing Maximum Sum Increasing Subsequence | Dynamic Programming solution to construct Maximum Sum Increasing Subsequence ; Utility function to calculate sum of all vector elements ; Function to construct Maximum Sum Increasing Subsequence ; L [ i ] - The Maximum Sum Increasing Subsequence that ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; for every j less than i ; * L [ i ] = { MaxSum ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores Maximum Sum Increasing Subsequence of arr [ 0. . i ] that ends with arr [ i ] ; find max ; max will contain result ; Driver Code ; construct and print Max Sum IS of arr
using System ; using System . Collections . Generic ; class GFG { static int findSum ( List < int > arr ) { int sum = 0 ; foreach ( int i in arr ) sum += i ; return sum ; } static void printMaxSumIs ( int [ ] arr , int n ) { List < int > [ ] L = new List < int > [ n ] ; for ( int i = 0 ; i < n ; i ++ ) L [ i ] = new List < int > ( ) ; L [ 0 ] . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( findSum ( L [ i ] ) < findSum ( L [ j ] ) ) ) { foreach ( int k in L [ j ] ) if ( ! L [ i ] . Contains ( k ) ) L [ i ] . Add ( k ) ; } } L [ i ] . Add ( arr [ i ] ) ; } List < int > res = new List < int > ( L [ 0 ] ) ; foreach ( List < int > x in L ) if ( findSum ( x ) > findSum ( res ) ) res = x ; foreach ( int i in res ) Console . Write ( i + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 6 , 4 , 5 , 1 } ; int n = arr . Length ; printMaxSumIs ( arr , n ) ; } }