ACM-problem D. language: C++