Merged C++ solution