Compute the probabilies for all values from 2 to N
for ( int i = 2 ; i <= n ; i ++ ) { for ( auto a1 : dp [ i - 1 ] ) { for ( auto a2 : dp [ 1 ] ) { dp [ i ] [ a1 . first + a2 . first ] += a1 . second * a2 . second ; } } }