changed max sum algorithm