dynamic programming version,
several bugfixes in genetic version