Theory of the general CUSUM algorithm