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 ] NEW_LINE dp [ i ] [ j ] = arr [ j - 1 ] * cur_sum NEW_LINE temp_sum += dp [ i ] [ j ] NEW_LINE cur_sum = temp_sum NEW_LINE return cur_sum NEW_LINE