Refine calculate sum logic with solution