C++ solution