C++ solution for CareerCup edition5