Improved the solution to the TapeEquilibrium problem in C++