Completed SRM 593.cpp