Samsung Electronics interview question

Something to do with backtracking and optimization