In case of greater length update the length along with sum
if ( dp [ 0 ] [ i ] < dp [ 0 ] [ j ] + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ j ] + 1 NEW_LINE dp [ 1 ] [ i ] = dp [ 1 ] [ j ] + arr [ i ] NEW_LINE DEDENT