ちょっと変わっている動的計画法あるごり

最近トップコーダー・オープンのアルゴリズムで苦しんでいる。エムケーさんの答えが面白い動的計画法の実施だった。The key insight was that the totalnumber of bit flips after bits up to the jth position are decided depends on only the position j, the value of the bit there, and the number if flips used up to that point. Given a max bit sequence of 300, complexity is bounded at 300*300*2=180000. Note that the probabilities are factored in at each position by multiplying by p or 1-p then branching on the last bit placed in a memoized depth-first search call. All cases assume the number of moves for a string equals the number of bit flips. The one edge case is a string of ones (0 flips and one move).
Apparently, a much more concise (about 10 lines using only a for loop) solution exists. Basically, the code can iterate over all consecutive bit pairs, adding on the probability that the two bits differ (adding a flip). Thus is (pi*(1-p[i-1]))+(1-p[i])*p[i-1]. Then the probability that all bits are 1 (product over i of pi) is added, as this is the expectation of a function that is 1, if all bits are 1, and 0 otherwise.