Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length .
dp [ i ] [ l + 1 ] = max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] ) ; } } } } }