maximum sum algorithm improved with dyanmic programming