Speed up misc.cumsum using scan and kernel memoization.