modify the sum method of candy algorithm