Iterate for all possible values of arr [ i ]
for ( int i = 2 ; i <= 100000 ; i ++ ) { int temp = ( freq . ContainsKey ( i ) ) ? freq [ i ] : 0 ; dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ i - 2 ] + i * temp ) ; }