Solving a problem from the entrance exams at MFF UK Praha by analysis of the cases and by dynamic programming.