We will subtract previously computed value so as to get the sum of elements from j + 1 to n in the ( i - 1 ) th row
cur_sum -= dp [ i - 1 ] [ j ] ; dp [ i ] [ j ] = arr [ j - 1 ] * cur_sum ; temp_sum += dp [ i ] [ j ] ; } cur_sum = temp_sum ; } return cur_sum ; }