Selecting maximum element from previoulsy selected element
int sum = prev ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int max_smaller = INT_MIN ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev && a [ i ] [ j ] > max_smaller ) max_smaller = a [ i ] [ j ] ; }