Cpp solution